UNPKG

ndjson-stream

Version:

This library provides TransformStream for ndjson.

79 lines 2.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LineBreakerStream = exports.JsonParserStream = exports.NdjsonStream = void 0; class LineBreakerTransformer { constructor() { this.processing = ""; this.processing = ""; } start(controller) { this.processing = ""; } transform(chunk, controller) { const [first, ...others] = chunk.split("\n"); const firstLine = this.processing + first; switch (others.length) { case 0: this.processing = firstLine; break; case 1: controller.enqueue(firstLine); this.processing = others[0]; break; default: { controller.enqueue(firstLine); const last = others.pop(); for (const line of others) { controller.enqueue(line); } this.processing = last; break; } } } flush(controller) { // TODO: Should raise an error? if (this.processing !== "") { controller.enqueue(this.processing); } } } class LineBreakerStream extends TransformStream { constructor() { super(new LineBreakerTransformer()); } } exports.LineBreakerStream = LineBreakerStream; class JsonParserTransformer { constructor(ignoreEmptyLine) { this.ignoreEmptyLine = ignoreEmptyLine; } transform(chunk, controller) { if (this.ignoreEmptyLine && (chunk === "" || chunk === "\r")) { return; } try { const value = JSON.parse(chunk); controller.enqueue(value); } catch (e) { controller.error(e); } } } class JsonParserStream extends TransformStream { constructor(ignoreEmptyLine = true) { super(new JsonParserTransformer(ignoreEmptyLine)); } } exports.JsonParserStream = JsonParserStream; class NdjsonStream { constructor(ignoreEmptyLine = true) { const root = new TextDecoderStream(); const chained = root.readable.pipeThrough(new LineBreakerStream()).pipeThrough(new JsonParserStream(ignoreEmptyLine)); this.readable = chained; this.writable = root.writable; } } exports.NdjsonStream = NdjsonStream; //# sourceMappingURL=index.js.map