fix(parser): honor toJSON() when deconstructing a binary packet#5518
Open
spokodev wants to merge 1 commit into
Open
fix(parser): honor toJSON() when deconstructing a binary packet#5518spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
hasBinary() follows an object's toJSON() method, so a packet whose toJSON() returns binary is encoded as a binary packet. However deconstructPacket() did not call toJSON(), so it walked the original object instead of its serialized form: binary nested in the toJSON() result was not extracted as an attachment and the emitted shape did not match the toJSON() output (as it does for non-binary packets via JSON.stringify). Apply toJSON() in _deconstructPacket() as hasBinary() and JSON.stringify already do, so the two encoding paths stay consistent.
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.
Description
socket.io-parsergives a binary packet a different shape depending on whether it contains binary, becausedeconstructPacketdoes not honortoJSON().hasBinary()(which decides whether a packet takes the binary-encoding path) follows an object'stoJSON(). But_deconstructPacket()walked the original object instead of itstoJSON()output. So for a class instance whosetoJSON()returns the data to send:toJSON()keys, andtoJSON()result was never extracted as an attachment (aBufferreturned directly bytoJSON()was corrupted into{"0":..,"1":..}).The same object is therefore delivered with a different structure depending only on whether it carries binary.
Reproduction
Emitting a model instance (Mongoose document, ORM entity, DTO) that exposes
toJSON()and holds a file or avatar buffer, a common pattern:Fix
Apply
toJSON()in_deconstructPacket()with the same one-shot recursion guard thathasBinary()andJSON.stringifyuse, so both the binary-detection and the deconstruction paths walk an identical tree. The pre-existingDateguard is preserved, soDateserialization is unchanged.Test
Added a case to
packages/socket.io-parser/test/buffer.jsfor aBuffernested behind atoJSON(). It fails before the change (private key leaks, attachment shape wrong) and passes after. Full parser suite stays green (34 passing).