Skip to content

Commit

Permalink
Support zero-length compressed request and response messages on Node.…
Browse files Browse the repository at this point in the history
…js (#893)
  • Loading branch information
timostamm authored Oct 31, 2023
1 parent ba8a390 commit 623a6d1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/connect-node-test/src/compression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ describe("compression", () => {
const equal = payload.every((value, index) => b[index] === value);
expect(equal).toBeTrue();
});
it("should decompress zero-length payload", async () => {
const b = await compression.decompress(new Uint8Array(0), 0xffffffff);
expect(b.byteLength).toBe(0);
});
it("should raise resource_exhausted if maxReadBytes exceeded", async () => {
try {
await compression.decompress(payloadCompressed, 2);
Expand Down
6 changes: 6 additions & 0 deletions packages/connect-node/src/compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export const compressionGzip: Compression = {
return gzip(bytes, {});
},
decompress(bytes, readMaxBytes) {
if (bytes.length == 0) {
return Promise.resolve(new Uint8Array(0));
}
return wrapZLibErrors(
gunzip(bytes, {
maxOutputLength: readMaxBytes,
Expand All @@ -56,6 +59,9 @@ export const compressionBrotli: Compression = {
return brotliCompress(bytes, {});
},
decompress(bytes, readMaxBytes) {
if (bytes.length == 0) {
return Promise.resolve(new Uint8Array(0));
}
return wrapZLibErrors(
brotliDecompress(bytes, {
maxOutputLength: readMaxBytes,
Expand Down
2 changes: 2 additions & 0 deletions packages/connect/src/protocol/compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export interface Compression {
/**
* Decompress a chunk of data.
*
* A zero-length chunk is acceptable, and will return a zero-length result.
*
* Raises a ConnectError with Code.InvalidArgument if the decompressed
* size exceeds readMaxBytes.
*/
Expand Down

0 comments on commit 623a6d1

Please sign in to comment.