|
| 1 | +import { Readable } from "stream"; |
| 2 | + |
| 3 | +import { headStream } from "./headStream"; |
| 4 | +import { headStream as headWebStream } from "./headStream.browser"; |
| 5 | +import { splitStream } from "./splitStream"; |
| 6 | +import { splitStream as splitWebStream } from "./splitStream.browser"; |
| 7 | + |
| 8 | +const CHUNK_SIZE = 4; |
| 9 | +const a32 = "abcd".repeat(32_000 / CHUNK_SIZE); |
| 10 | +const a16 = "abcd".repeat(16_000 / CHUNK_SIZE); |
| 11 | +const a8 = "abcd".repeat(8); |
| 12 | +const a4 = "abcd".repeat(4); |
| 13 | +const a2 = "abcd".repeat(2); |
| 14 | +const a1 = "abcd".repeat(1); |
| 15 | + |
| 16 | +describe(headStream.name, () => { |
| 17 | + it("should collect the head of a Node.js stream", async () => { |
| 18 | + const data = Buffer.from(a32); |
| 19 | + const myStream = Readable.from(data); |
| 20 | + |
| 21 | + const head = await headStream(myStream, 16_000); |
| 22 | + |
| 23 | + expect(Buffer.from(head).toString()).toEqual(a16); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should collect the head of a web stream", async () => { |
| 27 | + if (typeof ReadableStream !== "undefined") { |
| 28 | + const buffer = Buffer.from(a32); |
| 29 | + const data = Array.from(new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)); |
| 30 | + |
| 31 | + const myStream = new ReadableStream({ |
| 32 | + start(controller) { |
| 33 | + for (const inputChunk of data) { |
| 34 | + controller.enqueue(new Uint8Array([inputChunk])); |
| 35 | + } |
| 36 | + controller.close(); |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + const head = await headWebStream(myStream, 16_000); |
| 41 | + expect(Buffer.from(head).toString()).toEqual(a16); |
| 42 | + } |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe("splitStream and headStream integration", () => { |
| 47 | + it("should split and head streams for Node.js", async () => { |
| 48 | + const data = Buffer.from(a32); |
| 49 | + const myStream = Readable.from(data); |
| 50 | + |
| 51 | + const [a, _1] = await splitStream(myStream); |
| 52 | + const [b, _2] = await splitStream(_1); |
| 53 | + const [c, _3] = await splitStream(_2); |
| 54 | + const [d, _4] = await splitStream(_3); |
| 55 | + const [e, f] = await splitStream(_4); |
| 56 | + |
| 57 | + const byteArr1 = await headStream(a, Infinity); |
| 58 | + const byteArr2 = await headStream(b, 16_000); |
| 59 | + const byteArr3 = await headStream(c, 8 * CHUNK_SIZE); |
| 60 | + const byteArr4 = await headStream(d, 4 * CHUNK_SIZE); |
| 61 | + const byteArr5 = await headStream(e, 2 * CHUNK_SIZE); |
| 62 | + const byteArr6 = await headStream(f, CHUNK_SIZE); |
| 63 | + |
| 64 | + await Promise.all([a, b, c, d, e, f].map((stream) => stream.destroy())); |
| 65 | + |
| 66 | + expect(Buffer.from(byteArr1).toString()).toEqual(a32); |
| 67 | + expect(Buffer.from(byteArr2).toString()).toEqual(a16); |
| 68 | + expect(Buffer.from(byteArr3).toString()).toEqual(a8); |
| 69 | + expect(Buffer.from(byteArr4).toString()).toEqual(a4); |
| 70 | + expect(Buffer.from(byteArr5).toString()).toEqual(a2); |
| 71 | + expect(Buffer.from(byteArr6).toString()).toEqual(a1); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should split and head streams for web streams API", async () => { |
| 75 | + if (typeof ReadableStream !== "undefined") { |
| 76 | + const buffer = Buffer.from(a8); |
| 77 | + const data = Array.from(new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)); |
| 78 | + |
| 79 | + const myStream = new ReadableStream({ |
| 80 | + start(controller) { |
| 81 | + for (let i = 0; i < data.length; i += CHUNK_SIZE) { |
| 82 | + controller.enqueue(new Uint8Array(data.slice(i, i + CHUNK_SIZE))); |
| 83 | + } |
| 84 | + controller.close(); |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + const [a, _1] = await splitWebStream(myStream); |
| 89 | + const [b, _2] = await splitWebStream(_1); |
| 90 | + const [c, _3] = await splitWebStream(_2); |
| 91 | + const [d, e] = await splitWebStream(_3); |
| 92 | + |
| 93 | + const byteArr1 = await headWebStream(a, Infinity); |
| 94 | + const byteArr2 = await headWebStream(b, 8 * CHUNK_SIZE); |
| 95 | + const byteArr3 = await headWebStream(c, 4 * CHUNK_SIZE); |
| 96 | + const byteArr4 = await headWebStream(d, 2 * CHUNK_SIZE); |
| 97 | + const byteArr5 = await headWebStream(e, CHUNK_SIZE); |
| 98 | + |
| 99 | + await Promise.all([a, b, c, d, e].map((stream) => stream.cancel())); |
| 100 | + |
| 101 | + expect(Buffer.from(byteArr1).toString()).toEqual(a8); |
| 102 | + expect(Buffer.from(byteArr2).toString()).toEqual(a8); |
| 103 | + expect(Buffer.from(byteArr3).toString()).toEqual(a4); |
| 104 | + expect(Buffer.from(byteArr4).toString()).toEqual(a2); |
| 105 | + expect(Buffer.from(byteArr5).toString()).toEqual(a1); |
| 106 | + } |
| 107 | + }); |
| 108 | +}); |
0 commit comments