ByteSliceStream
import { ByteSliceStream } from "jsr:@ende/streams";class
class ByteSliceStream extends TransformStream<Uint8Array, Uint8Array>A transform stream that only transforms from the zero-indexed start and
end bytes (both inclusive).
Example: Basic usage
import { ByteSliceStream } from "@ende/streams/byte-slice-stream";
import { assertEquals } from "@ende/assert";
const stream = ReadableStream.from([
new Uint8Array([0, 1]),
new Uint8Array([2, 3, 4]),
]);
const slicedStream = stream.pipeThrough(new ByteSliceStream(1, 3));
assertEquals(
await Array.fromAsync(slicedStream),
[new Uint8Array([1]), new Uint8Array([2, 3])]
);
Example: Get a range of bytes from a fetch response body
import { ByteSliceStream } from "@ende/streams/byte-slice-stream";
import { assertEquals } from "@ende/assert";
const response = await fetch("https://example.com");
const rangedStream = response.body!
.pipeThrough(new ByteSliceStream(3, 8));
const collected = await Array.fromAsync(rangedStream);
assertEquals(collected[0]?.length, 6);
Constructors
constructor
new ByteSliceStream(start: number = 0, end: number = Infinity)Constructs a new instance.
start: number = 0The zero-indexed byte index to start reading from.
end: number = InfinityThe zero-indexed byte index to stop reading at. Inclusive.