You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR allows a custom length encoding/decoding function to be passed to `encode`/`decode`.
There's also a int32BE fixed length encoder/decoder. e.g.
```js
const lp = require('it-length-prefixed')
const { int32BEDecode, int32BEDecode } = require('it-length-prefixed')
await pipe(
[Buffer.from('hello world')],
lp.encode({ lengthEncoder: int32BEDecode }),
lp.decode({ lengthDecoder: int32BEDecode }),
async source => {
for await (const chunk of source) {
console.log(chunk.toString())
}
}
)
```
See updated README for more info.
BREAKING CHANGE: Additional validation now checks for messages with a length that is too long to prevent a possible DoS attack. The error code `ERR_MSG_TOO_LONG` has changed to `ERR_MSG_DATA_TOO_LONG` and the error code `ERR_MSG_LENGTH_TOO_LONG` has been added.
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
Copy file name to clipboardExpand all lines: README.md
+16-6Lines changed: 16 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,23 +61,31 @@ console.log(decoded)
61
61
62
62
-`opts: Object`, optional
63
63
-`poolSize: 10 * 1024`: Buffer pool size to allocate up front
64
+
-`minPoolSize: 8`: The minimum size the pool can be before it is re-allocated. Note: it is important this value is greater than the maximum value that can be encoded by the `lengthEncoder` (see the next option). Since encoded lengths are written into a buffer pool, there needs to be enough space to hold the encoded value.
65
+
-`lengthEncoder: Function`: A function that encodes the length that will prefix each message. By default this is a [`varint`](https://www.npmjs.com/package/varint) encoder. It is passed a `value` to encode, an (optional) `target` buffer to write to and an (optional) `offset` to start writing from. The function should encode the `value` into the `target` (or alloc a new Buffer if not specified), set the `lengthEncoder.bytes` value (the number of bytes written) and return the `target`.
66
+
- The following additional length encoders are available:
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`BufferList`](https://www.npmjs.com/package/bl) objects. All messages will be prefixed with a length, determined by the `lengthEncoder` function.
66
70
67
-
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`BufferList`](https://www.npmjs.com/package/bl) objects.
68
-
69
-
### `encode.single(chunk)`
71
+
### `encode.single(chunk, [opts])`
70
72
71
73
-`chunk: Buffer|BufferList` chunk to encode
74
+
-`opts: Object`, optional
75
+
-`lengthEncoder: Function`: See description above. Note that this encoder will _not_ be passed a `target` or `offset` and so will need to allocate a buffer to write to.
72
76
73
77
Returns a `BufferList` containing the encoded chunk.
74
78
75
79
### `decode([opts])`
76
80
77
81
-`opts: Object`, optional
78
-
-`maxDataLength`: If provided, will not decode messages longer than the size specified, if omitted will use the current default of 4MB.
82
+
-`maxLengthLength`: If provided, will not decode messages whose length section exceeds the size specified, if omitted will use the default of 147 bytes.
83
+
-`maxDataLength`: If provided, will not decode messages whose data section exceeds the size specified, if omitted will use the default of 4MB.
79
84
-`onLength(len: Number)`: Called for every length prefix that is decoded from the stream
80
85
-`onData(data: BufferList)`: Called for every chunk of data that is decoded from the stream
86
+
-`lengthDecoder: Function`: A function that decodes the length that prefixes each message. By default this is a [`varint`](https://www.npmjs.com/package/varint) decoder. It is passed some `data` to decode which is a [`BufferList`](https://www.npmjs.com/package/bl). The function should decode the length, set the `lengthDecoder.bytes` value (the number of bytes read) and return the length. If the length cannot be decoded, the function should throw a `RangeError`.
87
+
- The following additional length decoders are available:
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`BufferList`](https://www.npmjs.com/package/bl) objects.
83
91
@@ -87,8 +95,10 @@ Behaves like `decode` except it only reads the exact number of bytes needed for
87
95
88
96
-`reader: Reader`: An [it-reader](https://github.com/alanshaw/it-reader)
89
97
-`opts: Object`, optional
90
-
-`maxDataLength`: If provided, will not decode messages longer than the size specified, if omitted will use the current default of 4MB.
98
+
-`maxLengthLength`: If provided, will not decode messages whose length section exceeds the size specified, if omitted will use the default of 147 bytes.
99
+
-`maxDataLength`: If provided, will not decode messages whose data section exceeds the size specified, if omitted will use the default of 4MB.
91
100
-`onData(data: BufferList)`: Called for every chunk of data that is decoded from the stream
101
+
-`lengthEncoder: Function`: See description above.
92
102
93
103
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`BufferList`](https://www.npmjs.com/package/bl) objects.
0 commit comments