-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Zstandard compression
- Loading branch information
1 parent
0a2070f
commit 88c8ad4
Showing
12 changed files
with
260 additions
and
76 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type Shard from "../Shard"; | ||
|
||
export default abstract class Compression { | ||
shard!: Shard; | ||
abstract decompress(data: Buffer): Promise<Buffer | null>; | ||
constructor(shard: Shard) { | ||
Object.defineProperty(this, "shard", { | ||
value: shard, | ||
configurable: false, | ||
enumerable: false, | ||
writable: false | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Compression from "./base"; | ||
import type Shard from "../Shard"; | ||
import GatewayError from "../../util/Errors"; | ||
import { Inflate, constants } from "pako"; | ||
|
||
interface PakoExtra { | ||
chunks: Array<Buffer>; | ||
strm: { | ||
next_out: number; | ||
output: Buffer; | ||
}; | ||
} | ||
|
||
export default class PakoCompression extends Compression { | ||
_sharedZLib: Inflate & PakoExtra; | ||
constructor(shard: Shard) { | ||
super(shard); | ||
this._sharedZLib = new Inflate({ chunkSize: 128 * 1024 }) as Inflate & PakoExtra; | ||
} | ||
|
||
async decompress(data: Buffer): Promise<Buffer | null> { | ||
if (data.length >= 4 && data.readUInt32BE(data.length - 4) === 0xFFFF) { | ||
// store the current pointer for slicing buffers after pushing. | ||
const currentPointer: number | undefined = this._sharedZLib.strm?.next_out; | ||
this._sharedZLib.push(data, constants.Z_SYNC_FLUSH); | ||
if (this._sharedZLib.err) { | ||
this.shard.client.emit("error", new GatewayError(`zlib error ${this._sharedZLib.err}: ${this._sharedZLib.msg ?? ""}`, 0)); | ||
return null; | ||
} | ||
|
||
if (this._sharedZLib.chunks.length === 0) { | ||
// The current buffer hasn't been flushed | ||
data = Buffer.from(this._sharedZLib.strm!.output.slice(currentPointer)); | ||
} else { | ||
// Buffers have been flushed one or more times | ||
data = Buffer.concat([ | ||
this._sharedZLib.chunks[0].slice(currentPointer), | ||
...this._sharedZLib.chunks.slice(1), | ||
this._sharedZLib.strm.output | ||
]); | ||
this._sharedZLib.chunks = []; | ||
} | ||
|
||
return data; | ||
} else { | ||
this._sharedZLib.push(data, false); | ||
return null; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Compression from "./base"; | ||
import type Shard from "../Shard"; | ||
import GatewayError from "../../util/Errors"; | ||
import ZlibSync from "zlib-sync"; | ||
|
||
export default class ZlibSyncCompression extends Compression { | ||
_sharedZLib: ZlibSync.Inflate; | ||
constructor(shard: Shard) { | ||
super(shard); | ||
this._sharedZLib = new ZlibSync.Inflate({ chunkSize: 128 * 1024 }); | ||
} | ||
|
||
async decompress(data: Buffer): Promise<Buffer | null> { | ||
if (data.length >= 4 && data.readUInt32BE(data.length - 4) === 0xFFFF) { | ||
// store the current pointer for slicing buffers after pushing. | ||
this._sharedZLib.push(data, ZlibSync.Z_SYNC_FLUSH); | ||
if (this._sharedZLib.err) { | ||
this.shard.client.emit("error", new GatewayError(`zlib error ${this._sharedZLib.err}: ${this._sharedZLib.msg ?? ""}`, 0)); | ||
return null; | ||
} | ||
|
||
data = Buffer.from(this._sharedZLib.result ?? ""); | ||
|
||
return data; | ||
} else { | ||
this._sharedZLib.push(data, false); | ||
return null; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Compression from "./base"; | ||
import type Shard from "../Shard"; | ||
import fzstd from "fzstd"; | ||
|
||
export default class ZstdCompression extends Compression { | ||
_resolvePromise?: (data: Uint8Array) => void; | ||
_resultPromise?: Promise<Uint8Array>; | ||
stream: fzstd.Decompress; | ||
constructor(shard: Shard) { | ||
super(shard); | ||
this.stream = new fzstd.Decompress(data => { | ||
this._resolvePromise!(data); | ||
}); | ||
} | ||
|
||
async decompress(data: Buffer): Promise<Buffer> { | ||
if (this._resultPromise) { | ||
await this._resultPromise; | ||
} | ||
this._resultPromise = new Promise(resolve => { | ||
this._resolvePromise = resolve; | ||
}); | ||
this.stream.push(data); | ||
const result = await this._resultPromise; | ||
return Buffer.from(result); | ||
} | ||
} |
Oops, something went wrong.