|
| 1 | +import * as zlib from 'zlib'; |
| 2 | + |
| 3 | +import chai = require("chai"); |
| 4 | +const expect = chai.expect; |
| 5 | + |
| 6 | +import { |
| 7 | + createGzipStream, |
| 8 | + createGunzipStream |
| 9 | +} from '../src/index'; |
| 10 | + |
| 11 | +// Helper to collect all chunks from a ReadableStream into a single Uint8Array |
| 12 | +async function collectStream(stream: ReadableStream<Uint8Array>): Promise<Uint8Array> { |
| 13 | + const reader = stream.getReader(); |
| 14 | + const chunks: Uint8Array[] = []; |
| 15 | + let totalLength = 0; |
| 16 | + |
| 17 | + while (true) { |
| 18 | + const { done, value } = await reader.read(); |
| 19 | + if (done) break; |
| 20 | + chunks.push(value); |
| 21 | + totalLength += value.length; |
| 22 | + } |
| 23 | + |
| 24 | + const result = new Uint8Array(totalLength); |
| 25 | + let offset = 0; |
| 26 | + for (const chunk of chunks) { |
| 27 | + result.set(chunk, offset); |
| 28 | + offset += chunk.length; |
| 29 | + } |
| 30 | + return result; |
| 31 | +} |
| 32 | + |
| 33 | +// Helper to create a ReadableStream from a Uint8Array |
| 34 | +function createReadableStream(data: Uint8Array): ReadableStream<Uint8Array> { |
| 35 | + return new ReadableStream({ |
| 36 | + start(controller) { |
| 37 | + controller.enqueue(data); |
| 38 | + controller.close(); |
| 39 | + } |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +describe("Streaming", () => { |
| 44 | + describe("Gzip", () => { |
| 45 | + it('should compress data with gzip stream', async () => { |
| 46 | + const input = Buffer.from('Hello streaming gzip world!'); |
| 47 | + const inputStream = createReadableStream(input); |
| 48 | + |
| 49 | + const compressedStream = inputStream.pipeThrough(createGzipStream()); |
| 50 | + const compressed = await collectStream(compressedStream); |
| 51 | + |
| 52 | + // Verify the compressed data can be decompressed with zlib |
| 53 | + const decompressed = zlib.gunzipSync(Buffer.from(compressed)); |
| 54 | + expect(decompressed.toString()).to.equal('Hello streaming gzip world!'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should decompress gzip data with stream', async () => { |
| 58 | + const original = 'Hello streaming gunzip world!'; |
| 59 | + const compressed = zlib.gzipSync(original); |
| 60 | + const inputStream = createReadableStream(compressed); |
| 61 | + |
| 62 | + const decompressedStream = inputStream.pipeThrough(createGunzipStream()); |
| 63 | + const decompressed = await collectStream(decompressedStream); |
| 64 | + |
| 65 | + expect(Buffer.from(decompressed).toString()).to.equal(original); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should handle round-trip compression and decompression', async () => { |
| 69 | + const original = 'Round-trip streaming test with some repeated data data data data'; |
| 70 | + const inputStream = createReadableStream(Buffer.from(original)); |
| 71 | + |
| 72 | + const compressedStream = inputStream.pipeThrough(createGzipStream()); |
| 73 | + const decompressedStream = compressedStream.pipeThrough(createGunzipStream()); |
| 74 | + const result = await collectStream(decompressedStream); |
| 75 | + |
| 76 | + expect(Buffer.from(result).toString()).to.equal(original); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should handle multiple chunks', async () => { |
| 80 | + const chunks = ['chunk1', 'chunk2', 'chunk3', 'chunk4']; |
| 81 | + const inputStream = new ReadableStream<Uint8Array>({ |
| 82 | + start(controller) { |
| 83 | + for (const chunk of chunks) { |
| 84 | + controller.enqueue(Buffer.from(chunk)); |
| 85 | + } |
| 86 | + controller.close(); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + const compressedStream = inputStream.pipeThrough(createGzipStream()); |
| 91 | + const decompressedStream = compressedStream.pipeThrough(createGunzipStream()); |
| 92 | + const result = await collectStream(decompressedStream); |
| 93 | + |
| 94 | + expect(Buffer.from(result).toString()).to.equal(chunks.join('')); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should handle empty input', async () => { |
| 98 | + const inputStream = createReadableStream(new Uint8Array(0)); |
| 99 | + |
| 100 | + const compressedStream = inputStream.pipeThrough(createGzipStream()); |
| 101 | + const decompressedStream = compressedStream.pipeThrough(createGunzipStream()); |
| 102 | + const result = await collectStream(decompressedStream); |
| 103 | + |
| 104 | + expect(result.length).to.equal(0); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle large data', async () => { |
| 108 | + // Create a 1MB buffer of repeated data |
| 109 | + const pattern = 'This is a test pattern that will be repeated many times. '; |
| 110 | + const repeated = pattern.repeat(20000); |
| 111 | + const inputStream = createReadableStream(Buffer.from(repeated)); |
| 112 | + |
| 113 | + const compressedStream = inputStream.pipeThrough(createGzipStream()); |
| 114 | + const compressed = await collectStream(compressedStream); |
| 115 | + |
| 116 | + // Compressed should be smaller than original due to repetition |
| 117 | + expect(compressed.length).to.be.lessThan(repeated.length); |
| 118 | + |
| 119 | + // Verify decompression |
| 120 | + const decompressed = zlib.gunzipSync(Buffer.from(compressed)); |
| 121 | + expect(decompressed.toString()).to.equal(repeated); |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments