forked from dignifiedquire/pull-length-prefixed
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom length encoding and decoding (#8)
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>
- Loading branch information
Alan Shaw
authored
Nov 13, 2019
1 parent
b651ba4
commit e419b63
Showing
15 changed files
with
216 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
language: node_js | ||
node_js: | ||
- 10 | ||
- 12 | ||
|
||
script: | ||
- npm run lint | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
const int32BEDecode = data => { | ||
if (data.length < 4) throw RangeError('Could not decode int32BE') | ||
return data.readInt32BE(0) | ||
} | ||
|
||
int32BEDecode.bytes = 4 // Always because fixed length | ||
|
||
module.exports = int32BEDecode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict' | ||
|
||
const { Buffer } = require('buffer') | ||
|
||
const int32BEEncode = (value, target, offset) => { | ||
target = target || Buffer.allocUnsafe(4) | ||
target.writeInt32BE(value, offset) | ||
return target | ||
} | ||
|
||
int32BEEncode.bytes = 4 // Always because fixed length | ||
|
||
module.exports = int32BEEncode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict' | ||
|
||
const Varint = require('varint') | ||
|
||
const toBufferProxy = bl => new Proxy({}, { | ||
get: (_, prop) => prop[0] === 'l' ? bl[prop] : bl.get(parseInt(prop)) | ||
}) | ||
|
||
const varintDecode = data => { | ||
const len = Varint.decode(Buffer.isBuffer(data) ? data : toBufferProxy(data)) | ||
varintDecode.bytes = Varint.decode.bytes | ||
return len | ||
} | ||
|
||
module.exports = varintDecode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
const Varint = require('varint') | ||
const { Buffer } = require('buffer') | ||
|
||
// Encode the passed length `value` to the `target` buffer at the given `offset` | ||
const varintEncode = (value, target, offset) => { | ||
const ret = Varint.encode(value, target, offset) | ||
varintEncode.bytes = Varint.encode.bytes | ||
// If no target, create Buffer from returned array | ||
return target || Buffer.from(ret) | ||
} | ||
|
||
module.exports = varintEncode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
|
||
const { map } = require('streaming-iterables') | ||
const randomInt = require('random-int') | ||
const randomBytes = require('random-bytes') | ||
|
||
module.exports.toBuffer = map(c => c.slice()) | ||
module.exports.times = (n, fn) => Array.from(Array(n), fn) | ||
module.exports.someBytes = n => randomBytes(randomInt(1, n || 32)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.