Skip to main content
Deno 2 is finally here 🎉️
Learn more

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

FixedLengthUint8ArrayTransformStream.prototype.constructor

Parameters

firstChunkLength

  • Type : number
  • Definition : The length of the first chunk. If 0, first chunk is handled like other chunks.

otherChunksLength

  • Type : number
  • Definition : The length of each chunk except the first one if firstChunkLength > 0 (and potentially the last one if allowDifferentLengthForLastChunk=true).

allowMultiples

  • Type : boolean
  • Default value : false
  • Definition : Applies to all chunks except the first one if firstChunkLength > 0 (and potentially the last one if allowDifferentLengthForLastChunk=true). If false, then the targetted chunks will be of length otherChunksLengths. If true, then the targeted chunks will be of length k*otherChunksLengths (where k is a strictly positive integer).

allowDifferentLengthForLastChunk

  • Type : boolean
  • Default value : false
  • Definition : If true, the remaining bytes will be sent in a last chunk. For instance, if the input stream is 21 bytes long, firstChunkLength = 2 and otherChunksLength=3, the last chunk will be 21 - 2 - 3x6 = 1 byte long.

Exceptions thrown

RangeError

This exception is thrown if firstChunkLength is not a positive integer or otherChunksLength is not a strictly positive integer.

MalformedStreamError

This exception is thrown if allowDifferentLengthForLastChunk=false and a last chunk of less than otherChunksLengths bytes remains.

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

See https://github.com/martinjeromelouis/FixedLengthUint8ArrayTransformStream/blob/master/tests/FixedLengthUint8ArrayTransformStream_test.ts

License

MIT