Introduction
FixedLengthUint8ArrayTransformStream is a DENO open-source library (MIT license) that transforms a stream of variable-length Uint8Arrays into a stream of fixed-length Uint8Arrays.
This can for instance be useful for decrypting a stream.
API
class FixedLengthUint8ArrayTransformStream extends TransformStream
constructor(firstChunkLength: number, otherChunksLength: number, allowMultiples, transformFirstChunk, transformOtherChunks, transformLastChunk)
@param firstChunkLength
The length of the first chunk. If 0, the first chunk is handled like other chunks.
@param otherChunksLength
The length of each chunk except the first one if firstChunkLength > 0 and the last one if its length is strictly inferior to otherChunksLength.
@param allowMultiples
Applies to all chunks except the first one if firstChunkLength > 0 and the last one if its length is trictly inferior to otherChunksLength. If false (default value), then the afore-mentioned chunks will be of length otherChunksLengths. If true, then the afore-mentioned chunks will be of length k*otherChunksLengths (where k is a strictly positive integer).
@param transformFirstChunk
A callback that will be called for the first chunk if firstChunkLength > 0 and there are at least firstChunkLength bytes in the stream
@param transformOtherChunks
A callback that will be called for all chunks except the first one if firstChunkLength > 0 and the last one if its length is strictly inferior to otherChunksLength.
@param transformLastChunk
A callback that will be called for the last chunk if its length is strictly inferior to otherChunksLength. Note : if there is one single chunk whose length is inferior to firstChunkLength, this callback is called for that chunk.
@throws RangeError if firstChunkLength is not a positive integer or otherChunksLength is not a strictly positive integer.
Example
import { readableStreamFromIterable } from "https://deno.land/std@0.181.0/streams/readable_stream_from_iterable.ts";
import { FixedLengthUint8ArrayTransformStream } from "https://raw.githubusercontent.com/martinjeromelouis/FixedLengthUint8ArrayTransformStream/master/mod.ts";
const inputStream = readableStreamFromIterable<Uint8Array>([
new Uint8Array(12),
new Uint8Array(44),
new Uint8Array(8),
]);
const outputStream = inputStream.pipeThrough(
new FixedLengthUint8ArrayTransformStream(13, 16, false, true),
);
for await (const chunk of outputStream) console.log(chunk.length);This code is available at https://github.com/martinjeromelouis/FixedLengthUint8ArrayTransformStream/blob/master/examples/examples.ts.
Tests
License
MIT