This release is 47 versions behind 1.0.16 — the latest version of @std/streams. Jump to latest
Works with
•JSR Score100%•This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




Downloads18,474/wk
•Published2 years ago (0.203.0)
Utilities for working with the Web Streams API
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import { assertEquals, assertThrows } from "jsr:@std/assert@^0.203.0"; import { ByteSliceStream } from "./byte_slice_stream.ts"; Deno.test("[streams] ByteSliceStream", async function () { function createStream(start = 0, end = Infinity) { return new ReadableStream({ start(controller) { controller.enqueue(new Uint8Array([0, 1])); controller.enqueue(new Uint8Array([2, 3])); controller.close(); }, }).pipeThrough(new ByteSliceStream(start, end)); } let chunks = []; for await (const chunk of createStream(0, 3)) { chunks.push(chunk); } assertEquals(chunks, [ new Uint8Array([0, 1]), new Uint8Array([2, 3]), ]); chunks = []; for await (const chunk of createStream(0, 1)) { chunks.push(chunk); } assertEquals(chunks, [ new Uint8Array([0, 1]), ]); chunks = []; for await (const chunk of createStream(0, 2)) { chunks.push(chunk); } assertEquals(chunks, [ new Uint8Array([0, 1]), new Uint8Array([2]), ]); chunks = []; for await (const chunk of createStream(0, 3)) { chunks.push(chunk); } assertEquals(chunks, [ new Uint8Array([0, 1]), new Uint8Array([2, 3]), ]); chunks = []; for await (const chunk of createStream(1, 3)) { chunks.push(chunk); } assertEquals(chunks, [ new Uint8Array([1]), new Uint8Array([2, 3]), ]); chunks = []; for await (const chunk of createStream(2, 3)) { chunks.push(chunk); } assertEquals(chunks, [ new Uint8Array([2, 3]), ]); chunks = []; for await (const chunk of createStream(0, 10)) { chunks.push(chunk); } assertEquals(chunks, [ new Uint8Array([0, 1]), new Uint8Array([2, 3]), ]); assertThrows(() => createStream(-1, Infinity)); });