-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware-compression): compressStream for Node
- Loading branch information
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/middleware-compression/src/compressStream.spec.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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Readable } from "stream"; | ||
import { createGzip } from "zlib"; | ||
|
||
import { compressStream } from "./compressStream"; | ||
|
||
jest.mock("zlib", () => ({ | ||
createGzip: jest.fn().mockImplementation(() => { | ||
const mockStream = new Readable({ read() {} }); // Create a mock readable stream | ||
mockStream.pipe = jest.fn().mockReturnValue(mockStream); // Mock the pipe method | ||
return mockStream; | ||
}), | ||
})); | ||
|
||
describe(compressStream.name, () => { | ||
let testDataStream: Readable; | ||
|
||
beforeEach(() => { | ||
// Create a readable stream from a string | ||
testDataStream = Readable.from("test data"); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should compress a readble stream using gzip", async () => { | ||
const receivedOutput = await compressStream(testDataStream); | ||
|
||
expect(receivedOutput).toBeInstanceOf(Readable); | ||
expect(createGzip).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should throw an error if compression fails", async () => { | ||
const compressionErrorMsg = "compression error message"; | ||
const compressionError = new Error(compressionErrorMsg); | ||
(createGzip as jest.Mock).mockImplementationOnce(() => { | ||
throw compressionError; | ||
}); | ||
|
||
await expect(compressStream(testDataStream)).rejects.toThrow( | ||
new Error("Failure during compression: " + compressionErrorMsg) | ||
); | ||
|
||
expect(createGzip).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,6 @@ | ||
import { Readable } from "stream"; | ||
import { createGzip } from "zlib"; | ||
|
||
export const compressStream = async (body: Readable): Promise<Readable> => { | ||
return body.pipe(createGzip()); | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/middleware-compression/src/compressString.browser.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { toUint8Array } from "@smithy/util-utf8"; | ||
|
||
import { compressStream } from "./compressStream.browser"; | ||
|
||
export const compressString = async (body: any): Promise<Uint8Array> => { | ||
// Only gzip shall be supported initial release. | ||
|
||
const inputUint8Array = toUint8Array(body); | ||
const inputStream = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue(inputUint8Array); | ||
controller.close(); | ||
}, | ||
}); | ||
|
||
const outputStream = await compressStream(inputStream); | ||
|
||
const reader = outputStream.getReader(); | ||
const chunks = []; | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
chunks.push(...value); | ||
} | ||
|
||
return new Uint8Array(chunks); | ||
}; |