UNPKG

@signalk/streams

Version:

Utilities for handling streams of Signal K data

36 lines (35 loc) 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const stream_1 = require("stream"); class Liner extends stream_1.Transform { lineSeparator; lastLineData = null; constructor(options = {}) { super({ objectMode: true }); this.lineSeparator = options.lineSeparator ?? '\n'; } _transform(chunk, encoding, done) { let data = chunk.toString(); if (this.lastLineData) { data = this.lastLineData + data; } const lines = data.split(this.lineSeparator); this.lastLineData = lines.splice(lines.length - 1, 1)[0] ?? null; if (this.lastLineData && this.lastLineData.length > 2048) { console.error('Are you sure you are using the correct line terminator? Not going to handle lines longer than 2048 chars.'); this.lastLineData = ''; } for (const line of lines) { this.push(line); } done(); } _flush(done) { if (this.lastLineData) { this.push(this.lastLineData); } this.lastLineData = null; done(); } } exports.default = Liner;