diff --git a/types/whatwg-streams/whatwg-streams-tests.ts b/types/whatwg-streams/whatwg-streams-tests.ts index 0bb1068f31..c7ed11928b 100644 --- a/types/whatwg-streams/whatwg-streams-tests.ts +++ b/types/whatwg-streams/whatwg-streams-tests.ts @@ -444,3 +444,38 @@ class LipFuzzTransformer implements TransformStreamTransformer { }) ); } + + +// 8.10. A transform stream created from a sync mapper function + +function mapperTransformStream(mapperFunction: (chunk: W) => R) { + return new TransformStream({ + transform(chunk, controller) { + controller.enqueue(mapperFunction(chunk)); + } + }); +} + +{ + const ts = mapperTransformStream((chunk: string) => chunk.toUpperCase()); + const writer = ts.writable.getWriter(); + const reader = ts.readable.getReader(); + + writer.write("No need to shout"); + + // Logs "NO NEED TO SHOUT": + reader.read().then(({ value }) => console.log(value)); + +} + +{ + const ts = mapperTransformStream((chunk: string) => JSON.parse(chunk)); + const writer = ts.writable.getWriter(); + const reader = ts.readable.getReader(); + + writer.write("[1, "); + + // Logs a SyntaxError, twice: + reader.read().catch(e => console.error(e)); + writer.write("{}").catch(e => console.error(e)); +}