Initial commit

This commit is contained in:
Marcelo
2025-11-20 15:27:34 -06:00
commit cc72c9fc5d
3221 changed files with 737477 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
/// <reference types="node" />
/// <reference types="node" />
/// <reference types="node" />
import { Transform, TransformCallback, TransformOptions } from 'stream';
export interface InterByteTimeoutOptions extends TransformOptions {
/** the period of silence in milliseconds after which data is emitted */
interval: number;
/** the maximum number of bytes after which data will be emitted. Defaults to 65536 */
maxBufferSize?: number;
}
/**
* A transform stream that buffers data and emits it after not receiving any bytes for the specified amount of time or hitting a max buffer size.
*/
export declare class InterByteTimeoutParser extends Transform {
maxBufferSize: number;
currentPacket: number[];
interval: number;
intervalID: NodeJS.Timeout | undefined;
constructor({ maxBufferSize, interval, ...transformOptions }: InterByteTimeoutOptions);
_transform(chunk: Buffer, encoding: BufferEncoding, cb: TransformCallback): void;
emitPacket(): void;
_flush(cb: TransformCallback): void;
}

View File

@@ -0,0 +1,61 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InterByteTimeoutParser = void 0;
const stream_1 = require("stream");
/**
* A transform stream that buffers data and emits it after not receiving any bytes for the specified amount of time or hitting a max buffer size.
*/
class InterByteTimeoutParser extends stream_1.Transform {
maxBufferSize;
currentPacket;
interval;
intervalID;
constructor({ maxBufferSize = 65536, interval, ...transformOptions }) {
super(transformOptions);
if (!interval) {
throw new TypeError('"interval" is required');
}
if (typeof interval !== 'number' || Number.isNaN(interval)) {
throw new TypeError('"interval" is not a number');
}
if (interval < 1) {
throw new TypeError('"interval" is not greater than 0');
}
if (typeof maxBufferSize !== 'number' || Number.isNaN(maxBufferSize)) {
throw new TypeError('"maxBufferSize" is not a number');
}
if (maxBufferSize < 1) {
throw new TypeError('"maxBufferSize" is not greater than 0');
}
this.maxBufferSize = maxBufferSize;
this.currentPacket = [];
this.interval = interval;
}
_transform(chunk, encoding, cb) {
if (this.intervalID) {
clearTimeout(this.intervalID);
}
for (let offset = 0; offset < chunk.length; offset++) {
this.currentPacket.push(chunk[offset]);
if (this.currentPacket.length >= this.maxBufferSize) {
this.emitPacket();
}
}
this.intervalID = setTimeout(this.emitPacket.bind(this), this.interval);
cb();
}
emitPacket() {
if (this.intervalID) {
clearTimeout(this.intervalID);
}
if (this.currentPacket.length > 0) {
this.push(Buffer.from(this.currentPacket));
}
this.currentPacket = [];
}
_flush(cb) {
this.emitPacket();
cb();
}
}
exports.InterByteTimeoutParser = InterByteTimeoutParser;