protobuf-array-stream
Version:
Stream and decode Protocol Buffer arrays without memory issues - perfect for processing large datasets
84 lines (80 loc) • 3.14 kB
TypeScript
import { Transform, TransformCallback } from 'stream';
import protobuf from 'protobufjs';
/**
* A transform stream for processing Protocol Buffer repeated fields one element at a time.
* This stream accepts a Protocol Buffer message containing exactly one repeated field
* and emits each element of that field as a separate data event.
*
* @example
* ```typescript
* // Load proto definitions
* const root = RepeatedFieldStream.loadRootFromFileSync('path/to/proto');
* // Create stream
* const stream = new RepeatedFieldStream({
* root,
* messageName: "YourMessageName"
* });
*
* stream.on('data', (datum) => {
* console.log('Received:', datum);
* });
*
* stream.end(message);
* ```
*/
declare class RepeatedFieldStream<T = unknown> extends Transform {
private repeatedField;
private currentPayloadSize;
private buffer;
private tag;
private readOffset;
private nextToken;
/**
* Creates a new RepeatedFieldStream.
*
* @param {Object} options - Configuration options
* @param {protobuf.Root} options.root - Root namespace from protobufjs containing message definitions
* @param {string} options.messageName - Name of the message type to decode. Must be a message with exactly one repeated field.
* @throws {RepeatedFieldStreamError} If the message type doesn't exist
* @throws {RepeatedFieldStreamError} If the message has no fields
* @throws {RepeatedFieldStreamError} If the message has more than one field
* @throws {RepeatedFieldStreamError} If the field is not marked as repeated
*/
constructor(options: {
root: protobuf.Root;
messageName: string;
});
/**
* Exposes protobuf.loadSync from protobufjs to load Protocol Buffer definitions from a file synchronously.
* @returns {protobuf.Root} Root namespace
*/
static loadRootFromFileSync: typeof protobuf.loadSync;
/**
* Exposes protobuf.load from protobufjs to load Protocol Buffer definitions from a file asynchronously.
* @returns {Promise<protobuf.Root>} Root namespace
*/
static loadRootFromFile: typeof protobuf.load;
/**
* Exposes protobuf.Root.fromJSON from protobufjs to load Protocol Buffer definitions from a JSON object.
* @returns {protobuf.Root} Root namespace
*/
static loadRootFromJSON: typeof protobuf.Root.fromJSON;
/**
* Exposes protobuf.parse from protobufjs to load Protocol Buffer definitions from a string.
* @returns {protobuf.Root} Root namespace
*/
static loadRootFromSource: (source: string, options?: protobuf.IParseOptions) => protobuf.Root;
private static validateMessage;
private static getRepeatedFieldDetails;
private static getPayloadSizeInBytes;
private static decodeVarint;
private decodePayload;
private cleanup;
private detectVarint;
private detectPayload;
private decode;
_transform(chunk: Buffer, _encoding: BufferEncoding, callback: TransformCallback): void;
_final(callback: TransformCallback): void;
_flush(callback: TransformCallback): void;
}
export { RepeatedFieldStream };