-
-
Notifications
You must be signed in to change notification settings - Fork 317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: update snappy frame decompress #7333
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
packages/reqresp/src/encodingStrategies/sszSnappy/snappyFrames/common.ts
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,9 +1,38 @@ | ||
import crc32c from "@chainsafe/fast-crc32c"; | ||
|
||
export enum ChunkType { | ||
IDENTIFIER = 0xff, | ||
COMPRESSED = 0x00, | ||
UNCOMPRESSED = 0x01, | ||
PADDING = 0xfe, | ||
SKIPPABLE = 0x80, | ||
} | ||
|
||
export const IDENTIFIER = Buffer.from([0x73, 0x4e, 0x61, 0x50, 0x70, 0x59]); | ||
export const IDENTIFIER_FRAME = Buffer.from([0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59]); | ||
|
||
/** | ||
* As per the snappy framing format for streams, the size of any uncompressed chunk can be | ||
* no longer than 65536 bytes. | ||
* | ||
* From: https://github.com/google/snappy/blob/main/framing_format.txt#L90:L92 | ||
*/ | ||
export const UNCOMPRESSED_CHUNK_SIZE = 65536; | ||
|
||
export function crc(value: Uint8Array): Buffer { | ||
// this function doesn't actually need a buffer | ||
// see https://github.com/napi-rs/node-rs/blob/main/packages/crc32/index.d.ts | ||
const x = crc32c.calculate(value as Buffer); | ||
const result = Buffer.allocUnsafe?.(4) ?? Buffer.alloc(4); | ||
|
||
// As defined in section 3 of https://github.com/google/snappy/blob/master/framing_format.txt | ||
// And other implementations for reference: | ||
// Go: https://github.com/golang/snappy/blob/2e65f85255dbc3072edf28d6b5b8efc472979f5a/snappy.go#L97 | ||
// Python: https://github.com/andrix/python-snappy/blob/602e9c10d743f71bef0bac5e4c4dffa17340d7b3/snappy/snappy.py#L70 | ||
// Mask the right hand to (32 - 17) = 15 bits -> 0x7fff, to keep correct 32 bit values. | ||
// Shift the left hand with >>> for correct 32 bit intermediate result. | ||
// Then final >>> 0 for 32 bits output | ||
result.writeUInt32LE((((x >>> 15) | ((x & 0x7fff) << 17)) + 0xa282ead8) >>> 0, 0); | ||
|
||
return result; | ||
} |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use 128 here? Guessing this is a snappy spec thing but curious none-the-less
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just an artifact from a test case I had been provided, there's nothing special about 128.