Commit Graph

71 Commits

Author SHA1 Message Date
Damien Arrachequesne
0305b4a7a5 fix(typescript): properly import the TransformStream type
When compiling with TypeScript with module set to "node16" and
moduleResolution to "node16", the following error would be thrown:

> node_modules/engine.io-parser/build/cjs/index.d.ts:6:54 - error TS2304: Cannot find name 'TransformStream'.
> 6 export declare function createPacketEncoderStream(): TransformStream<Packet, any>;
>                                                        ~~~~~~~~~~~~~~~
> node_modules/engine.io-parser/build/cjs/index.d.ts:7:96 - error TS2304: Cannot find name 'TransformStream'.
> 7 export declare function createPacketDecoderStream(maxPayload: number, binaryType: BinaryType): TransformStream<Uint8Array, any>;
>                                                                                                  ~~~~~~~~~~~~~~~
> Found 2 errors in the same file, starting at: node_modules/engine.io-parser/build/cjs/index.d.ts:6

This is because the TransformStream object is not exposed in the global
scope in the `@types/node` package, even though it is since Node.js
`v18.0.0`.

Reference: https://nodejs.org/api/webstreams.html#class-transformstream

Note: we only import the TransformStream type (not value) because it
isn't defined on older Node.js versions.

Related:

- https://github.com/socketio/engine.io-parser/issues/136
- https://github.com/socketio/socket.io-client/issues/1606
2024-02-05 18:20:49 +01:00
Damien Arrachequesne
08cff77a48 chore: bump prettier to version 3
This change is necessary to be able to write "import type { ... }".
2024-02-05 18:20:30 +01:00
Damien Arrachequesne
0b5e98591e refactor: prepend a header to each WebTransport chunk
This commit updates the format of the header added in [1], in order to
match the format used for a WebSocket frame ([2]).

Two advantages:

- small payloads only need 1 byte instead of 4
- payloads larger than 2^31 bytes are supported

[1]: 6142324fa6
[2]: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#decoding_payload_length
2023-08-02 00:29:45 +02:00
Damien Arrachequesne
6142324fa6 feat: prepend a header to each WebTransport chunk
WebTransport is a stream-based protocol, so chunking boundaries are not
always preserved.

That's why we will now prepend a 4-bytes header to each chunk:

- first bit indicates whether the payload is plain text (0) or binary (1)
- next 31 bits indicate the length of the payload

See also: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#format
2023-07-31 08:09:27 +02:00
Damien Arrachequesne
bed70a4f25 feat: implement WebTransport-related encoding/decoding 2023-06-11 07:41:51 +02:00
Mauricio Narvaez
351ba8245b fix: properly encode empty buffer in base64 encoding (#131)
An empty buffer was encoded into `bundefined` instead of `b`
(reproduced on Chrome v108 / Ubuntu).

Co-authored-by: Mauricio Narvaez <nvz@fb.com>
2023-01-06 11:26:29 +01:00
Damien Arrachequesne
bc7400a5e8 refactor: include base64-arraybuffer in the repository
In order to reduce the number of dependencies and the attack surface in
case of supply chain attacks.
2022-04-30 13:59:34 +02:00
Damien Arrachequesne
a421bbec7b fix: add missing file extension for ESM import
Related: https://github.com/socketio/engine.io-parser/issues/127
2022-04-30 12:42:03 +02:00
Damien Arrachequesne
039b45cc65 fix(typings): update the type of RawData
We could also split the declaration of RawData with the "browser"
field:

```
// for Node.js
export type RawData = string | Buffer | ArrayBuffer | ArrayBufferView; // no Blob
// for the browser
export type RawData = string | ArrayBuffer | ArrayBufferView | Blob; // no Buffer
```

But it does not seem supported by the TypeScript compiler, so we'll
revert to just using "any" for now.

Related: https://github.com/socketio/engine.io-parser/issues/128
2022-04-30 12:36:57 +02:00
Damien Arrachequesne
ad5bd7ddf5 refactor: add additional types
Merged from https://github.com/socketio/engine.io/pull/630
2022-01-17 10:23:16 +01:00
Damien Arrachequesne
25a4b2bbf5 chore: replace base64-arraybuffer by @socket.io/base64-arraybuffer
The "base64-arraybuffer" package has been temporarily forked to [1],
until changes are merged upstream.

[1]: https://github.com/socketio/base64-arraybuffer

Related:

- https://github.com/niklasvh/base64-arraybuffer/issues/32
- https://github.com/socketio/socket.io-client/issues/1520
- https://github.com/socketio/socket.io-website/issues/313
- https://github.com/socketio/socket.io/discussions/4209
2022-01-17 10:23:11 +01:00
Damien Arrachequesne
965a567ca0 chore(release): 5.0.0
Diff: https://github.com/socketio/engine.io-parser/compare/4.0.3...5.0.0
2021-10-04 11:02:35 +02:00
Damien Arrachequesne
e4d9fce4d8 chore: migrate to TypeScript
This change introduces an ESM build which will allow tree shaking. A
CJS build is also provided for backward compatibility.
2021-10-01 10:39:52 +02:00
Damien Arrachequesne
6d7dd76130 fix: respect the offset and length of TypedArray objects
The parser now returns the TypedArray object instead of the underlying
array buffer, so its offset and length are now taken in account (which
is the behavior of the classic WebSocket implementation).

Notes:

- this was already the case for the Node.js implementation

- in v3, the TypedArray object was always converted to an array buffer
because a byte was added at the beginning ("message" type)

- we could also have used "data.slice().buffer", but it would have
resulted in the copy of the buffer

Related:

- https://github.com/socketio/engine.io-parser/issues/122
- https://github.com/socketio/engine.io-client/issues/677
2021-08-29 07:54:03 +02:00
Damien Arrachequesne
886f9ea7c4 fix: use a terser-compatible representation of the separator
By default, terser (the mangler used by webpack) will replace the '\x1e'
by an unreadable character in the optimized output.

One solution would be to use the 'ascii_only' option:

```
module.exports = {
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          output: {
            ascii_only: true
          }
        }
      })
    ]
  }
};
```

But it would require anyone who tries to bundle the engine.io client to
use this plugin, so we'll rather use String.fromCharCode().

Note: String.fromCharCode(30) === '\u001e' === '\x1e'
2020-09-11 00:24:56 +02:00
Damien Arrachequesne
8edf2d1478 fix: keep track of the buffer initial length
The write buffer is mutable, so its initial length must be saved when
encoding the payload.
2020-09-08 01:30:32 +02:00
Damien Arrachequesne
6efedfa0f3 feat: restore the upgrade mechanism
See 660263fae2
2020-09-08 01:30:32 +02:00
Damien Arrachequesne
cab7db0404 feat: implement the version 4 of the protocol
See https://github.com/socketio/engine.io-protocol for the list of
changes.

Note: The 'base64-arraybuffer' dependency must now be explicitly
included by the client (not needed by the server).
2020-05-19 15:07:53 +02:00
Damien Arrachequesne
50853738e0 fix: properly decode binary packets
This fixes the previous commit, allowing to properly decode binary packets in the browser (based on
the binaryType attribute).
2020-01-15 00:00:33 +01:00
Damien Arrachequesne
a947ae59a2 feat: remove packet type when encoding binary packets
Only packets of 'message' type can contain binary data, so it makes sense to remove the prefix in
order to avoid the concatenation of the packet type.

Breaking change: the packet containing binary data will now be sent without any transformation

Protocol v3: { type: 'message', data: <Buffer 01 02 03> } => <Buffer 04 01 02 03>
Protocol v4: { type: 'message', data: <Buffer 01 02 03> } => <Buffer 01 02 03>
2020-01-14 21:54:05 +01:00
Damien Arrachequesne
8eb58a689a refactor: remove Object.keys polyfill
This polyfill is now useless, as we do not plan to support IE < 9 anymore.
2020-01-14 21:54:05 +01:00
Damien Arrachequesne
744bf40d4f refactor: migrate to ES6 syntax and use prettier to format code 2020-01-14 13:28:12 +01:00
Gaubee
47893e1145 [perf] use Buffer.from instead of manually copying the ArrayBuffer (#108)
Ref: https://nodejs.org/docs/latest/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length
2019-09-13 11:01:01 +02:00
Damien Arrachequesne
aedf8eb29e [refactor] Use Buffer.allocUnsafe instead of new Buffer (#104)
Breaking change: drop support for Node.js 4 (since Buffer.allocUnsafe was added in v5.10.0)

Reference: https://nodejs.org/docs/latest/api/buffer.html#buffer_class_method_buffer_allocunsafe_size
2019-03-19 23:56:03 +01:00
Damien Arrachequesne
2377dcc613 [fix] Do not mutate the input upon packet encoding (#105)
Fixes #95
2018-11-01 06:26:16 +01:00
Damien Arrachequesne
d848f0fc82 [refactor] Remove any reference to the global variable (#103)
Related #99
2018-10-31 06:23:32 +01:00
Damien Arrachequesne
3bc16f5938 [refactor] Remove custom exports from utf8.js file (#102)
AMD and browser exports are useless here.
2018-10-31 06:23:08 +01:00
Damien Arrachequesne
bffb63ebfa [revert] "[fix] Enable to utf8-decode string payloads (#88)" (#91)
This reverts commit 278a7e45e7.
2017-04-24 13:53:17 +02:00
Damien Arrachequesne
528345470c [chore] Use has-binary2 (#90) 2017-04-05 23:17:20 +02:00
Damien Arrachequesne
278a7e45e7 [fix] Enable to utf8-decode string payloads (#88)
That will allow clients receiving the xhr payload with
responseType = 'arraybuffer' to properly decode the message, which is
not sent as binary by the backend anymore since 292c00c (#85).
2017-03-21 14:35:00 +01:00
Damien Arrachequesne
292c00c183 [fix] Encode string payloads as strings even if binary supported (#85)
This reverts commit 44c7aa5, which caused string payloads to be encoded
as binary (so that huge string payloads were needlessly UTF-8-encoded).

Related: https://github.com/socketio/socket.io/issues/2872
2017-03-06 12:24:11 +01:00
Gatsbill
89a465a4b8 [perf] Micro optimisations (#84) 2016-12-23 07:19:13 +01:00
Damien Arrachequesne
d1c2680ca9 [fix] Sanitize strings by removing lone surrogates (#82) 2016-12-21 22:01:19 +01:00
Billouboq
2d83f9da17 [perf] Use strict equality where possible (#77) 2016-12-21 09:52:23 +01:00
Damien Arrachequesne
ebf92a5097 [style] Minor style changes (#83) 2016-12-21 09:31:14 +01:00
Damien Arrachequesne
181acef657 [fix] Fix double utf8 encoding for payloads (#81) 2016-12-21 09:15:07 +01:00
Damien Arrachequesne
6741900f37 [fix] Handle undefined case properly when decoding packet (#74) 2016-10-20 14:39:04 +02:00
jm
5aecaa914d [fix] decodePacket now accepts both Buffer and ArrayBuffer as data
Closes #64, fixes #60
2016-10-20 14:17:02 +02:00
Jack X
4a818a0ed7 [fix] Add safety check for global object
Closes #71
2016-10-18 01:40:29 +02:00
Damien Arrachequesne
f5d966bd66 [chore] Use wtf-8 instead of utf8 to prevent lone surrogates from generating parsing error (#68) 2016-09-26 03:44:29 +02:00
Tom Atkinson
26162afe01 [perf] Split try catch into separate function (#65) 2016-09-26 03:40:17 +02:00
Theodore Ni
c0d166b861 Require base64-arraybuffer module conditionally.
Since base64-arraybuffer version 0.1.5 introduced a change that fails
at require time in a browser without ArrayBuffer support, the require
must be guarded.
2016-06-25 22:12:44 -07:00
Dana Woodman
748144b50a Ensure navigator is defined. 2016-03-09 12:34:38 -08:00
Dana Woodman
a10c487518 Fix crash in React Native
This change lets people use Socket.io in React Native

Related to an issue on socket.io-client repo: https://github.com/socketio/socket.io-client/issues/945

All credit for this goes to @stevecass
2016-03-09 12:28:48 -08:00
Damien Arrachequesne
96fa4f907a Fix ArrayBuffer encoding in base64 string 2015-12-02 10:49:33 +01:00
nkzawa
685cb719e8 fix encoding blob as base64 2015-11-22 16:05:26 +09:00
Tom Atkinson
664f2af8e7 Fix some additional test failures found in main engine.io test suite 2015-08-22 04:07:37 +02:00
Tom Atkinson
32980f8f63 Fix iojs 3.0 Buffer.buffer issue 2015-08-20 23:46:46 +02:00
Tony Kovanen
b21717b66d Pass has-binary result to encodePacket
Needs to be done so thet individual packets don't get encoded as binary.
2015-01-16 21:28:01 +02:00
Tony Kovanen
44c7aa5ab9 Fix parse error
We always need to send binary when encoding payloads when sending from
server to client, because the polling transport has to know the response
type ahead of time.
2015-01-16 21:19:27 +02:00