-
Notifications
You must be signed in to change notification settings - Fork 22
Built-in support for message compression #181
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
Open
snichme
wants to merge
2
commits into
main
Choose a base branch
from
built-in-compression
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| /** | ||
| * Supported compression algorithms | ||
| */ | ||
| export type CompressionAlgorithm = "lz4" | "snappy" | "zstd" | "gzip" | ||
|
|
||
| /** | ||
| * Codec interface for compression/decompression | ||
| */ | ||
| export interface CompressionCodec { | ||
| /** Compress data */ | ||
| compress(data: Uint8Array): Uint8Array | ||
| /** Decompress data */ | ||
| decompress(data: Uint8Array): Uint8Array | ||
| /** Content-Encoding header value */ | ||
| readonly contentEncoding: string | ||
| } | ||
|
|
||
| /** | ||
| * Options for publish with compression | ||
| */ | ||
| export interface PublishOptions { | ||
| /** Compression algorithm to use */ | ||
| compression?: CompressionAlgorithm | ||
| /** Minimum body size in bytes before compression is applied (default: 0) */ | ||
| compressionThreshold?: number | ||
| } | ||
|
|
||
| /** | ||
| * Error thrown when a required compression codec is not available | ||
| */ | ||
| export class CompressionError extends Error { | ||
| constructor( | ||
| message: string, | ||
| public readonly algorithm?: CompressionAlgorithm, | ||
| ) { | ||
| super(message) | ||
| this.name = "CompressionError" | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Registry for compression codecs with lazy loading | ||
| */ | ||
| class CompressionRegistry { | ||
| private codecs = new Map<string, CompressionCodec | null>() | ||
| private loadPromises = new Map<string, Promise<CompressionCodec | null>>() | ||
|
|
||
| /** | ||
| * Get a codec by algorithm name, loading it if necessary | ||
| * Returns null if the codec library is not installed | ||
| */ | ||
| async getCodec(algorithm: CompressionAlgorithm): Promise<CompressionCodec | null> { | ||
| // Check cache first | ||
| if (this.codecs.has(algorithm)) { | ||
| return this.codecs.get(algorithm) || null | ||
| } | ||
|
|
||
| // Check if already loading | ||
| if (this.loadPromises.has(algorithm)) { | ||
| return this.loadPromises.get(algorithm)! | ||
| } | ||
|
|
||
| // Start loading | ||
| const loadPromise = this.loadCodec(algorithm) | ||
| this.loadPromises.set(algorithm, loadPromise) | ||
|
|
||
| const codec = await loadPromise | ||
| this.codecs.set(algorithm, codec) | ||
| this.loadPromises.delete(algorithm) | ||
|
|
||
| return codec | ||
| } | ||
|
|
||
| /** | ||
| * Register a custom codec | ||
| */ | ||
| registerCodec(contentEncoding: string, codec: CompressionCodec): void { | ||
| this.codecs.set(contentEncoding, codec) | ||
| } | ||
|
|
||
| /** | ||
| * Get a codec synchronously (only returns already-loaded codecs) | ||
| * Returns null if the codec is not loaded yet | ||
| */ | ||
| getCodecSync(algorithm: string): CompressionCodec | null { | ||
| return this.codecs.get(algorithm) || null | ||
| } | ||
|
|
||
| private async loadCodec(algorithm: CompressionAlgorithm): Promise<CompressionCodec | null> { | ||
| try { | ||
| switch (algorithm) { | ||
| case "gzip": | ||
| return await this.loadGzip() | ||
| case "lz4": | ||
| return await this.loadLz4() | ||
| case "snappy": | ||
| return await this.loadSnappy() | ||
| case "zstd": | ||
| return await this.loadZstd() | ||
| default: | ||
| return null | ||
| } | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| private async loadGzip(): Promise<CompressionCodec> { | ||
| // Use built-in zlib in Node.js, pako in browser | ||
| if (typeof process !== "undefined" && process.versions?.node) { | ||
| const zlib = await import("zlib") | ||
| return { | ||
| contentEncoding: "gzip", | ||
| compress: (data) => new Uint8Array(zlib.gzipSync(data)), | ||
| decompress: (data) => new Uint8Array(zlib.gunzipSync(data)), | ||
| } | ||
| } else { | ||
| // Browser: try pako | ||
| const pako = await import("pako") | ||
| return { | ||
| contentEncoding: "gzip", | ||
| compress: (data) => pako.gzip(data), | ||
| decompress: (data) => pako.ungzip(data), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async loadLz4(): Promise<CompressionCodec | null> { | ||
| try { | ||
| // Using lz4-napi for native bindings | ||
| const lz4 = await import("lz4-napi") | ||
| return { | ||
| contentEncoding: "lz4", | ||
| compress: (data) => new Uint8Array(lz4.compressSync(Buffer.from(data))), | ||
| decompress: (data) => new Uint8Array(lz4.uncompressSync(Buffer.from(data))), | ||
| } | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| private async loadSnappy(): Promise<CompressionCodec | null> { | ||
| try { | ||
| const snappy = await import("snappy") | ||
| return { | ||
| contentEncoding: "snappy", | ||
| compress: (data) => new Uint8Array(snappy.compressSync(data)), | ||
| decompress: (data) => new Uint8Array(snappy.uncompressSync(data)), | ||
| } | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| private async loadZstd(): Promise<CompressionCodec | null> { | ||
| // @mongodb-js/zstd only has async methods, not supported for sync compression | ||
| // TODO: Consider using a different zstd library with sync support | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| // Singleton instance | ||
| export const compressionRegistry = new CompressionRegistry() |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Type declarations for optional compression peer dependencies | ||
|
|
||
| declare module "lz4-napi" { | ||
| export function compress(data: Buffer): Promise<Buffer> | ||
| export function compressSync(data: Buffer): Buffer | ||
| export function uncompress(data: Buffer): Promise<Buffer> | ||
| export function uncompressSync(data: Buffer): Buffer | ||
| } | ||
|
|
||
| declare module "snappy" { | ||
| export function compress(data: Uint8Array | Buffer): Promise<Buffer> | ||
| export function compressSync(data: Uint8Array | Buffer): Buffer | ||
| export function uncompress(data: Uint8Array | Buffer): Promise<Buffer> | ||
| export function uncompressSync(data: Uint8Array | Buffer): Buffer | ||
| } | ||
|
|
||
| declare module "@mongodb-js/zstd" { | ||
| export function compress(data: Buffer): Promise<Buffer> | ||
| export function decompress(data: Buffer): Promise<Buffer> | ||
| } | ||
|
|
||
| declare module "pako" { | ||
| export function gzip(data: Uint8Array): Uint8Array | ||
| export function ungzip(data: Uint8Array): Uint8Array | ||
| } |
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
Oops, something went wrong.
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.
These methods should map closely to the protocol. Add compression to a high level client.
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.
I agree, we should build out BaseClient to have high level features like auto-reconnect, message coding, rpc client/server etc. Just like the ruby client. This is a WIP in that direction: #180