node-pkware
Version:
node.js implementation of StormLib's pkware compressor/de-compressor
47 lines (46 loc) • 2.45 kB
TypeScript
import { Buffer } from 'node:buffer';
import { Transform, Writable, type TransformCallback } from 'node:stream';
export type StreamHandler = (this: Transform, chunk: Buffer, encoding: NodeJS.BufferEncoding, callback: TransformCallback) => void | Promise<void>;
type TransformPredicate = (chunk: Buffer) => [left: Buffer, right: Buffer, isLeftDone: boolean];
/**
* Creates a {@link TransformPredicate} function, that awaits Buffers, keeps an internal counter
* of the bytes from them and splits the appropriate buffer at the given index.
*
* Splitting is done by returning an array with `[left: Buffer, right: Buffer, isLeftDone: bool]`.
*
* If you want to split data at the 100th byte and you keep feeding 60 byte long buffers to the function
* returned by `splitAt(100)`, then it will return arrays in the following manner:
* 1. `[inputBuffer, emptyBuffer, false]`
* 2. `[inputBuffer.slice(0, 40), inputBuffer.slice(40, 60), true]`
* 3. `[emptyBuffer, inputBuffer, true]`
* 4. `[emptyBuffer, inputBuffer, true]`
* 5. ... and so on
* @param index - a positive integer at which to split the buffer
*/
export declare function splitAt(index: number): TransformPredicate;
/**
* A `transform._transform` type function, which lets the input chunks through without any changes
*/
export declare function transformIdentity(): StreamHandler;
/**
* A `transform._transform` type function, which for every input chunk will output an empty buffer
*/
export declare function transformEmpty(): StreamHandler;
/**
* Takes a `transform._transform` type function and turns it into a Transform stream instance
* @param handler a `transform._transform` type function
* @returns a Transform stream instance
*/
export declare function through(handler: StreamHandler): Transform;
/**
* Higher order function for introducing conditional logic to `transform._transform` functions.
* This is used internally to handle offsets for `explode()`.
* @returns a `transform._transform` type function
*/
export declare function transformSplitBy(predicate: TransformPredicate, leftHandler: StreamHandler, rightHandler: StreamHandler): StreamHandler;
/**
* Data can be piped to the returned function from a stream and it will concatenate all chunks into a single buffer.
* @param done a callback function, which will receive the concatenated buffer as a parameter
*/
export declare function toBuffer(done: (buffer: Buffer) => void): Writable;
export {};