|
| 1 | +import assert from "node:assert"; |
| 2 | +import { ReadableStream, TransformStream, WritableStream } from "node:stream/web"; |
| 3 | +import type { QueuingStrategySize } from "node:stream/web"; |
| 4 | + |
| 5 | +async function readResultHasRequiredValueProperty() { |
| 6 | + const stream = ReadableStream.from(["test"]); |
| 7 | + const reader = stream.getReader(); |
| 8 | + { |
| 9 | + const result = await reader.read(); |
| 10 | + assert(!result.done, "Expected a non-done result"); |
| 11 | + // $ExpectType string |
| 12 | + result.value; |
| 13 | + assert.strictEqual(result.value, "test"); |
| 14 | + } |
| 15 | + { |
| 16 | + const result = await reader.read(); |
| 17 | + assert(result.done, "Expected a done result"); |
| 18 | + // $ExpectType string | undefined |
| 19 | + result.value; |
| 20 | + |
| 21 | + assert.strictEqual(Object.prototype.hasOwnProperty.call(result, "value"), true); |
| 22 | + assert.strictEqual(result.value, undefined); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +async function readableStreamControllerTreatsExplicitUndefinedAsAChunk() { |
| 27 | + const stream = new ReadableStream<string | undefined>({ |
| 28 | + start(controller) { |
| 29 | + // @ts-expect-error `chunk` is required; pass `undefined` explicitly when it is part of the chunk type. |
| 30 | + controller.enqueue(); |
| 31 | + controller.enqueue(undefined); |
| 32 | + controller.close(); |
| 33 | + }, |
| 34 | + }); |
| 35 | + const values: (string | undefined)[] = []; |
| 36 | + for await (const chunk of stream) { |
| 37 | + values.push(chunk); |
| 38 | + } |
| 39 | + // enqueue() and enqueue(undefined) should have the same effect |
| 40 | + assert.deepStrictEqual(values, [undefined, undefined]); |
| 41 | +} |
| 42 | + |
| 43 | +async function transformStreamControllerTreatsExplicitUndefinedAsAChunk() { |
| 44 | + const stream = new TransformStream<string, string | undefined>({ |
| 45 | + transform(chunk, controller) { |
| 46 | + assert.strictEqual(chunk, "input"); |
| 47 | + |
| 48 | + // @ts-expect-error `chunk` is required; pass `undefined` explicitly when it is part of the chunk type. |
| 49 | + controller.enqueue(); |
| 50 | + controller.enqueue(undefined); |
| 51 | + }, |
| 52 | + }); |
| 53 | + const values: (string | undefined)[] = []; |
| 54 | + for await (const chunk of ReadableStream.from(["input"]).pipeThrough(stream)) { |
| 55 | + values.push(chunk); |
| 56 | + } |
| 57 | + // enqueue() and enqueue(undefined) should have the same effect |
| 58 | + assert.deepStrictEqual(values, [undefined, undefined]); |
| 59 | +} |
| 60 | + |
| 61 | +async function writableStreamWriterTreatsExplicitUndefinedAsAChunk() { |
| 62 | + const writtenChunks: (string | undefined)[] = []; |
| 63 | + |
| 64 | + const stream = new WritableStream<string | undefined>({ |
| 65 | + write(chunk) { |
| 66 | + writtenChunks.push(chunk); |
| 67 | + }, |
| 68 | + }); |
| 69 | + const writer = stream.getWriter(); |
| 70 | + // @ts-expect-error `chunk` is required; pass `undefined` explicitly when it is part of the chunk type. |
| 71 | + await writer.write(); |
| 72 | + await writer.write(undefined); |
| 73 | + await writer.close(); |
| 74 | + |
| 75 | + assert.deepStrictEqual(writtenChunks, [undefined, undefined]); |
| 76 | +} |
| 77 | + |
| 78 | +async function queuingStrategySizeReceivesTheChunk() { |
| 79 | + const sizeChunks: string[] = []; |
| 80 | + |
| 81 | + const size: QueuingStrategySize<string> = (chunk) => { |
| 82 | + // $ExpectType string |
| 83 | + chunk; |
| 84 | + sizeChunks.push(chunk); |
| 85 | + return chunk.length; |
| 86 | + }; |
| 87 | + |
| 88 | + const stream = new ReadableStream<string>( |
| 89 | + { |
| 90 | + start(controller) { |
| 91 | + controller.enqueue("size"); |
| 92 | + controller.close(); |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + highWaterMark: 10, |
| 97 | + size, |
| 98 | + }, |
| 99 | + ); |
| 100 | + const result = await stream.getReader().read(); |
| 101 | + assert.deepStrictEqual(result, { done: false, value: "size" }); |
| 102 | + assert.deepStrictEqual(sizeChunks, ["size"]); |
| 103 | +} |
0 commit comments