deno.land / std@0.224.0 / io / to_readable_stream_test.ts
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";import { toReadableStream } from "./to_readable_stream.ts";import { Buffer } from "./buffer.ts";import { concat } from "../bytes/concat.ts";import { copy } from "../bytes/copy.ts";import type { Closer, Reader } from "./types.ts";
class MockReaderCloser implements Reader, Closer { chunks: Uint8Array[] = []; closeCall = 0;
read(p: Uint8Array): Promise<number | null> { if (this.closeCall) { throw new Error("already closed"); } if (p.length === 0) { return Promise.resolve(0); } const chunk = this.chunks.shift(); if (chunk) { const copied = copy(chunk, p); if (copied < chunk.length) { this.chunks.unshift(chunk.subarray(copied)); } return Promise.resolve(copied); } return Promise.resolve(null); }
close() { this.closeCall++; }}
Deno.test("toReadableStream()", async function () { const encoder = new TextEncoder(); const reader = new Buffer(encoder.encode("hello deno land")); const stream = toReadableStream(reader); const actual = await Array.fromAsync(stream); const decoder = new TextDecoder(); assertEquals(decoder.decode(concat(actual)), "hello deno land");});
Deno.test("toReadableStream() handles close", async function () { const encoder = new TextEncoder(); const reader = new MockReaderCloser(); reader.chunks = [ encoder.encode("hello "), encoder.encode("deno "), encoder.encode("land"), ]; const stream = toReadableStream(reader); const actual = await Array.fromAsync(stream); const decoder = new TextDecoder(); assertEquals(decoder.decode(concat(actual)), "hello deno land"); assertEquals(reader.closeCall, 1);});
Deno.test("toReadableStream() doesn't call close with `autoClose` is false", async function () { const encoder = new TextEncoder(); const reader = new MockReaderCloser(); reader.chunks = [ encoder.encode("hello "), encoder.encode("deno "), encoder.encode("land"), ]; const stream = toReadableStream(reader, { autoClose: false }); const actual = await Array.fromAsync(stream); const decoder = new TextDecoder(); assertEquals(decoder.decode(concat(actual)), "hello deno land"); assertEquals(reader.closeCall, 0);});
Deno.test("toReadableStream() handles `chunkSize` option", async function () { const encoder = new TextEncoder(); const reader = new MockReaderCloser(); reader.chunks = [ encoder.encode("hello "), encoder.encode("deno "), encoder.encode("land"), ]; const stream = toReadableStream(reader, { chunkSize: 2 }); const actual = await Array.fromAsync(stream); const decoder = new TextDecoder(); assertEquals(actual.length, 8); assertEquals(decoder.decode(concat(actual)), "hello deno land"); assertEquals(reader.closeCall, 1);});
Version Info