Add decodeSingle() to decode a single object in a buffer that may have extra bytes#293
Open
KAMRONBEK wants to merge 1 commit into
Open
Add decodeSingle() to decode a single object in a buffer that may have extra bytes#293KAMRONBEK wants to merge 1 commit into
KAMRONBEK wants to merge 1 commit into
Conversation
Author
|
@gfx TL;DR: adds the |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
There is no public API to decode a single MessagePack object from a buffer that contains trailing non-MessagePack bytes.
decode()throwsRangeError: Extra N byte(s) found..., and even if you catch it there is no way to learn how many bytes the object occupied. This breaks framed-stream protocols of the form[msgpack object][opaque bytes][msgpack object], where the msgpack object acts as a header describing the opaque payload that follows (see the use cases described in #128 by @mcclure, @covert-encryption, and @ansemjo).Root cause
Decoder#decode()deliberately callscreateExtraByteError()when bytes remain afterdoDecodeSync(), and bothdoDecodeSync()(which decodes exactly one object) andpos(which tracks bytes consumed) are private, so callers cannot opt out of the extra-bytes check or find the object boundary.Fix
Following the direction discussed in the issue thread, this adds a purely additive API — zero behavior change to existing functions:
Decoder#decodeSingle(buffer): DecodeSingleResult— same re-entrancy handling asdecode(), decodes exactly one object via the existingdoDecodeSync(), and returns{ value, bytesConsumed }instead of throwing on remaining bytes. Truncated or empty buffers still throwRangeError, and invalid data still throwsDecodeError, exactly likedecode().decodeSingle(buffer, options?)top-level function insrc/decode.ts, mirroringdecode()/decodeMulti().DecodeSingleResulttype:{ value: unknown; bytesConsumed: number }—bytesConsumedis the position where the extra bytes begin, so callers canbuffer.subarray(bytesConsumed)to continue with the rest of the frame.src/index.ts; README gets a TOC entry, a full API section with a framed-buffer example, and a pointer from thedecode()section.This covers the in-memory-buffer case; a
ReadableStreamvariant (decode one object from an async byte source, as some commenters describe) would be a natural follow-up if wanted.Tests
test/decodeSingle.test.ts(mocha + assert, matching the existing test style):bytesConsumedequal to the encoded length[msgpack object][0xc1 opaque bytes][msgpack object]: decodes the first object without throwing, skips the opaque bytes usingbytesConsumed, decodes the second objectRangeErrorRangeErrorDecoder#decodeSingleworks repeatedly on a reused instanceFull suite: 332 passing;
npm run lintclean.Refs #128