UNPKG

@signalk/streams

Version:

Utilities for handling streams of Signal K data

59 lines (58 loc) 1.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const path_1 = require("path"); const stream_1 = require("stream"); class EndIgnoringPassThrough extends stream_1.PassThrough { end() { return this; } } class FileStream { options; keepRunning; pipeTo = null; endIgnoringPassThrough = null; filestream = null; constructor(options) { this.options = options; this.keepRunning = options.keepRunning ?? true; } pipe(pipeTo) { this.pipeTo = pipeTo; this.endIgnoringPassThrough = new EndIgnoringPassThrough(); this.endIgnoringPassThrough.pipe(pipeTo); this.startStream(); return pipeTo; } startStream() { let filename; if ((0, path_1.isAbsolute)(this.options.filename)) { filename = this.options.filename; } else { filename = (0, path_1.join)(this.options.app.config.configPath, this.options.filename); if (!(0, fs_1.existsSync)(filename)) { filename = (0, path_1.join)(__dirname, '..', this.options.filename); } } this.filestream = (0, fs_1.createReadStream)(filename); this.filestream.on('error', (err) => { console.error(err.message); this.keepRunning = false; }); if (this.keepRunning) { this.filestream.on('end', () => this.startStream()); } this.filestream.pipe(this.endIgnoringPassThrough); } end() { if (this.pipeTo) { this.pipeTo.end(); } if (this.filestream) { this.filestream.close(); } } } exports.default = FileStream;