diff options
Diffstat (limited to 'test/bun.js/streams.test.js')
-rw-r--r-- | test/bun.js/streams.test.js | 426 |
1 files changed, 214 insertions, 212 deletions
diff --git a/test/bun.js/streams.test.js b/test/bun.js/streams.test.js index 4e869aeaa..0e28e512a 100644 --- a/test/bun.js/streams.test.js +++ b/test/bun.js/streams.test.js @@ -7,236 +7,236 @@ new Uint8Array(); beforeEach(() => gc()); afterEach(() => gc()); -it("exists globally", () => { - expect(typeof ReadableStream).toBe("function"); - expect(typeof ReadableStreamBYOBReader).toBe("function"); - expect(typeof ReadableStreamBYOBRequest).toBe("function"); - expect(typeof ReadableStreamDefaultController).toBe("function"); - expect(typeof ReadableStreamDefaultReader).toBe("function"); - expect(typeof TransformStream).toBe("function"); - expect(typeof TransformStreamDefaultController).toBe("function"); - expect(typeof WritableStream).toBe("function"); - expect(typeof WritableStreamDefaultController).toBe("function"); - expect(typeof WritableStreamDefaultWriter).toBe("function"); - expect(typeof ByteLengthQueuingStrategy).toBe("function"); - expect(typeof CountQueuingStrategy).toBe("function"); -}); +// it("exists globally", () => { +// expect(typeof ReadableStream).toBe("function"); +// expect(typeof ReadableStreamBYOBReader).toBe("function"); +// expect(typeof ReadableStreamBYOBRequest).toBe("function"); +// expect(typeof ReadableStreamDefaultController).toBe("function"); +// expect(typeof ReadableStreamDefaultReader).toBe("function"); +// expect(typeof TransformStream).toBe("function"); +// expect(typeof TransformStreamDefaultController).toBe("function"); +// expect(typeof WritableStream).toBe("function"); +// expect(typeof WritableStreamDefaultController).toBe("function"); +// expect(typeof WritableStreamDefaultWriter).toBe("function"); +// expect(typeof ByteLengthQueuingStrategy).toBe("function"); +// expect(typeof CountQueuingStrategy).toBe("function"); +// }); -it("ReadableStream (readMany)", async () => { - var stream = new ReadableStream({ - pull(controller) { - controller.enqueue("hello"); - controller.enqueue("world"); - controller.close(); - }, - cancel() {}, - }); - var reader = stream.getReader(); - const chunk = await reader.readMany(); - expect(chunk.value.join("")).toBe("helloworld"); - expect((await reader.read()).done).toBe(true); -}); +// it("ReadableStream (readMany)", async () => { +// var stream = new ReadableStream({ +// pull(controller) { +// controller.enqueue("hello"); +// controller.enqueue("world"); +// controller.close(); +// }, +// cancel() {}, +// }); +// var reader = stream.getReader(); +// const chunk = await reader.readMany(); +// expect(chunk.value.join("")).toBe("helloworld"); +// expect((await reader.read()).done).toBe(true); +// }); -it("ReadableStream (direct)", async () => { - var stream = new ReadableStream({ - pull(controller) { - controller.write("hello"); - controller.write("world"); - controller.close(); - }, - cancel() {}, - type: "direct", - }); - var reader = stream.getReader(); - const chunk = await reader.read(); - expect(chunk.value.join("")).toBe(Buffer.from("helloworld").join("")); - expect((await reader.read()).done).toBe(true); - expect((await reader.read()).done).toBe(true); -}); +// it("ReadableStream (direct)", async () => { +// var stream = new ReadableStream({ +// pull(controller) { +// controller.write("hello"); +// controller.write("world"); +// controller.close(); +// }, +// cancel() {}, +// type: "direct", +// }); +// var reader = stream.getReader(); +// const chunk = await reader.read(); +// expect(chunk.value.join("")).toBe(Buffer.from("helloworld").join("")); +// expect((await reader.read()).done).toBe(true); +// expect((await reader.read()).done).toBe(true); +// }); -it("ReadableStream (bytes)", async () => { - var stream = new ReadableStream({ - start(controller) { - controller.enqueue(Buffer.from("abdefgh")); - }, - pull(controller) {}, - cancel() {}, - type: "bytes", - }); - const chunks = []; - const chunk = await stream.getReader().read(); - chunks.push(chunk.value); - expect(chunks[0].join("")).toBe(Buffer.from("abdefgh").join("")); -}); +// it("ReadableStream (bytes)", async () => { +// var stream = new ReadableStream({ +// start(controller) { +// controller.enqueue(Buffer.from("abdefgh")); +// }, +// pull(controller) {}, +// cancel() {}, +// type: "bytes", +// }); +// const chunks = []; +// const chunk = await stream.getReader().read(); +// chunks.push(chunk.value); +// expect(chunks[0].join("")).toBe(Buffer.from("abdefgh").join("")); +// }); -it("ReadableStream (default)", async () => { - var stream = new ReadableStream({ - start(controller) { - controller.enqueue(Buffer.from("abdefgh")); - controller.close(); - }, - pull(controller) {}, - cancel() {}, - }); - const chunks = []; - const chunk = await stream.getReader().read(); - chunks.push(chunk.value); - expect(chunks[0].join("")).toBe(Buffer.from("abdefgh").join("")); -}); +// it("ReadableStream (default)", async () => { +// var stream = new ReadableStream({ +// start(controller) { +// controller.enqueue(Buffer.from("abdefgh")); +// controller.close(); +// }, +// pull(controller) {}, +// cancel() {}, +// }); +// const chunks = []; +// const chunk = await stream.getReader().read(); +// chunks.push(chunk.value); +// expect(chunks[0].join("")).toBe(Buffer.from("abdefgh").join("")); +// }); -it("readableStreamToArray", async () => { - var queue = [Buffer.from("abdefgh")]; - var stream = new ReadableStream({ - pull(controller) { - var chunk = queue.shift(); - if (chunk) { - controller.enqueue(chunk); - } else { - controller.close(); - } - }, - cancel() {}, - type: "bytes", - }); +// it("readableStreamToArray", async () => { +// var queue = [Buffer.from("abdefgh")]; +// var stream = new ReadableStream({ +// pull(controller) { +// var chunk = queue.shift(); +// if (chunk) { +// controller.enqueue(chunk); +// } else { +// controller.close(); +// } +// }, +// cancel() {}, +// type: "bytes", +// }); - const chunks = await readableStreamToArray(stream); +// const chunks = await readableStreamToArray(stream); - expect(chunks[0].join("")).toBe(Buffer.from("abdefgh").join("")); -}); +// expect(chunks[0].join("")).toBe(Buffer.from("abdefgh").join("")); +// }); -it("readableStreamToArrayBuffer (bytes)", async () => { - var queue = [Buffer.from("abdefgh")]; - var stream = new ReadableStream({ - pull(controller) { - var chunk = queue.shift(); - if (chunk) { - controller.enqueue(chunk); - } else { - controller.close(); - } - }, - cancel() {}, - type: "bytes", - }); - const buffer = await readableStreamToArrayBuffer(stream); - expect(new TextDecoder().decode(new Uint8Array(buffer))).toBe("abdefgh"); -}); +// it("readableStreamToArrayBuffer (bytes)", async () => { +// var queue = [Buffer.from("abdefgh")]; +// var stream = new ReadableStream({ +// pull(controller) { +// var chunk = queue.shift(); +// if (chunk) { +// controller.enqueue(chunk); +// } else { +// controller.close(); +// } +// }, +// cancel() {}, +// type: "bytes", +// }); +// const buffer = await readableStreamToArrayBuffer(stream); +// expect(new TextDecoder().decode(new Uint8Array(buffer))).toBe("abdefgh"); +// }); -it("readableStreamToArrayBuffer (default)", async () => { - var queue = [Buffer.from("abdefgh")]; - var stream = new ReadableStream({ - pull(controller) { - var chunk = queue.shift(); - if (chunk) { - controller.enqueue(chunk); - } else { - controller.close(); - } - }, - cancel() {}, - }); +// it("readableStreamToArrayBuffer (default)", async () => { +// var queue = [Buffer.from("abdefgh")]; +// var stream = new ReadableStream({ +// pull(controller) { +// var chunk = queue.shift(); +// if (chunk) { +// controller.enqueue(chunk); +// } else { +// controller.close(); +// } +// }, +// cancel() {}, +// }); - const buffer = await readableStreamToArrayBuffer(stream); - expect(new TextDecoder().decode(new Uint8Array(buffer))).toBe("abdefgh"); -}); +// const buffer = await readableStreamToArrayBuffer(stream); +// expect(new TextDecoder().decode(new Uint8Array(buffer))).toBe("abdefgh"); +// }); -it("ReadableStream for Blob", async () => { - var blob = new Blob(["abdefgh", "ijklmnop"]); - expect(await blob.text()).toBe("abdefghijklmnop"); - var stream; - try { - stream = blob.stream(); - stream = blob.stream(); - } catch (e) { - console.error(e); - console.error(e.stack); - } - const chunks = []; - var reader; - reader = stream.getReader(); +// it("ReadableStream for Blob", async () => { +// var blob = new Blob(["abdefgh", "ijklmnop"]); +// expect(await blob.text()).toBe("abdefghijklmnop"); +// var stream; +// try { +// stream = blob.stream(); +// stream = blob.stream(); +// } catch (e) { +// console.error(e); +// console.error(e.stack); +// } +// const chunks = []; +// var reader; +// reader = stream.getReader(); - while (true) { - var chunk; - try { - chunk = await reader.read(); - } catch (e) { - console.error(e); - console.error(e.stack); - } +// while (true) { +// var chunk; +// try { +// chunk = await reader.read(); +// } catch (e) { +// console.error(e); +// console.error(e.stack); +// } - if (chunk.done) break; - chunks.push(new TextDecoder().decode(chunk.value)); - } - expect(chunks.join("")).toBe( - new TextDecoder().decode(Buffer.from("abdefghijklmnop")) - ); -}); +// if (chunk.done) break; +// chunks.push(new TextDecoder().decode(chunk.value)); +// } +// expect(chunks.join("")).toBe( +// new TextDecoder().decode(Buffer.from("abdefghijklmnop")) +// ); +// }); -it("ReadableStream for File", async () => { - var blob = file(import.meta.dir + "/fetch.js.txt"); - var stream = blob.stream(24); - const chunks = []; - var reader = stream.getReader(); - stream = undefined; - while (true) { - const chunk = await reader.read(); - gc(true); - if (chunk.done) break; - chunks.push(chunk.value); - expect(chunk.value.byteLength <= 24).toBe(true); - gc(true); - } - reader = undefined; - const output = new Uint8Array(await blob.arrayBuffer()).join(""); - const input = chunks.map((a) => a.join("")).join(""); - expect(output).toBe(input); - gc(true); -}); +// it("ReadableStream for File", async () => { +// var blob = file(import.meta.dir + "/fetch.js.txt"); +// var stream = blob.stream(24); +// const chunks = []; +// var reader = stream.getReader(); +// stream = undefined; +// while (true) { +// const chunk = await reader.read(); +// gc(true); +// if (chunk.done) break; +// chunks.push(chunk.value); +// expect(chunk.value.byteLength <= 24).toBe(true); +// gc(true); +// } +// reader = undefined; +// const output = new Uint8Array(await blob.arrayBuffer()).join(""); +// const input = chunks.map((a) => a.join("")).join(""); +// expect(output).toBe(input); +// gc(true); +// }); -it("ReadableStream for File errors", async () => { - try { - var blob = file(import.meta.dir + "/fetch.js.txt.notfound"); - blob.stream().getReader(); - throw new Error("should not reach here"); - } catch (e) { - expect(e.code).toBe("ENOENT"); - expect(e.syscall).toBe("open"); - } -}); +// it("ReadableStream for File errors", async () => { +// try { +// var blob = file(import.meta.dir + "/fetch.js.txt.notfound"); +// blob.stream().getReader(); +// throw new Error("should not reach here"); +// } catch (e) { +// expect(e.code).toBe("ENOENT"); +// expect(e.syscall).toBe("open"); +// } +// }); -it("ReadableStream for empty blob closes immediately", async () => { - var blob = new Blob([]); - var stream = blob.stream(); - const chunks = []; - var reader = stream.getReader(); - while (true) { - const chunk = await reader.read(); - if (chunk.done) break; - chunks.push(chunk.value); - } +// it("ReadableStream for empty blob closes immediately", async () => { +// var blob = new Blob([]); +// var stream = blob.stream(); +// const chunks = []; +// var reader = stream.getReader(); +// while (true) { +// const chunk = await reader.read(); +// if (chunk.done) break; +// chunks.push(chunk.value); +// } - expect(chunks.length).toBe(0); -}); +// expect(chunks.length).toBe(0); +// }); -it("ReadableStream for empty file closes immediately", async () => { - writeFileSync("/tmp/bun-empty-file-123456", ""); - var blob = file("/tmp/bun-empty-file-123456"); - var stream; - try { - stream = blob.stream(); - } catch (e) { - console.error(e.stack); - } - const chunks = []; - var reader = stream.getReader(); - while (true) { - const chunk = await reader.read(); - if (chunk.done) break; - chunks.push(chunk.value); - } +// it("ReadableStream for empty file closes immediately", async () => { +// writeFileSync("/tmp/bun-empty-file-123456", ""); +// var blob = file("/tmp/bun-empty-file-123456"); +// var stream; +// try { +// stream = blob.stream(); +// } catch (e) { +// console.error(e.stack); +// } +// const chunks = []; +// var reader = stream.getReader(); +// while (true) { +// const chunk = await reader.read(); +// if (chunk.done) break; +// chunks.push(chunk.value); +// } - expect(chunks.length).toBe(0); -}); +// expect(chunks.length).toBe(0); +// }); it("new Response(stream).arrayBuffer() (bytes)", async () => { var queue = [Buffer.from("abdefgh")]; @@ -320,7 +320,9 @@ it("new Response(stream).blob() (default)", async () => { }, cancel() {}, }); - const blob = await new Response(stream).blob(); + const response = new Response(stream); + const blob = await response.blob(); + console.log(blob.size); expect(await blob.text()).toBe('{"hello":true}'); }); |