Skip to content

Commit

Permalink
feat: add decodeFromReader (#3)
Browse files Browse the repository at this point in the history
* chore: update bl dependency

* feat: add decodeFromReader

Instead of pulling in an entire chunk, that may consist of multiple messages,
decodeFromReader is able to read exactly the number of bytes from the reader
as needed. This helps prevent overreading when you have access to the reader.

This is particularly useful in situations where you need to perform a handshake
and then continue streaming data once the handshake is complete.

* chore: fix lint

* refactor: use try catch

fix: check for ERR_UNDER_READ
  • Loading branch information
jacobheun authored and Alan Shaw committed Nov 12, 2019
1 parent a421ed6 commit b61654d
Show file tree
Hide file tree
Showing 6 changed files with 905 additions and 530 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,16 @@ Returns a `BufferList` containing the encoded chunk.
- `onLength(len: Number)`: Called for every length prefix that is decoded from the stream
- `onData(data: BufferList)`: Called for every chunk of data that is decoded from the stream

All messages will be prefixed with a varint.
Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`BufferList`](https://www.npmjs.com/package/bl) objects.

### `decodeFromReader(reader, [opts])`

Behaves like `decode` except it only reads the exact number of bytes needed for each message in `reader`.

- `reader: Reader`: An [it-reader](https://github.com/alanshaw/it-reader)
- `opts: Object`, optional
- `maxDataLength`: If provided, will not decode messages longer than the size specified, if omitted will use the current default of 4MB.
- `onData(data: BufferList)`: Called for every chunk of data that is decoded from the stream

Returns a [transform](https://gist.github.com/alanshaw/591dc7dd54e4f99338a347ef568d6ee9#transform-it) that yields [`BufferList`](https://www.npmjs.com/package/bl) objects.

Expand Down
Loading

0 comments on commit b61654d

Please sign in to comment.