UNPKG

@cloudpss/ubjson

Version:

Opinionated UBJSON encoder/decoder for CloudPSS.

27 lines (23 loc) 903 B
import { StreamEncoderHelper } from '../stream-helper/encoder.js'; import type { EncodeOptions } from '../options.js'; /** 流式编码 UBJSON */ export class EncodeTransformer implements Transformer<unknown, Uint8Array> { constructor(options?: EncodeOptions) { this.helper = new StreamEncoderHelper(options, (binary) => { this.controller.enqueue(binary); }); } private readonly helper; private controller!: TransformStreamDefaultController<Uint8Array>; /** @inheritdoc */ transform(obj: unknown, controller: TransformStreamDefaultController<Uint8Array>): void { this.controller = controller; this.helper.encode(obj); } /** @inheritdoc */ flush(controller: TransformStreamDefaultController<Uint8Array>): void { this.controller = controller; this.helper.destroy(); controller.terminate(); } }