@ntrip/caster
Version:
NTRIP caster
153 lines (152 loc) • 5.63 kB
JavaScript
"use strict";
/*
* This file is part of the @ntrip/caster distribution (https://github.com/node-ntrip/caster).
* Copyright (c) 2020 Nebojsa Cvetkovic.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = exports.Server = exports.Connection = void 0;
const events = require("events");
const rtcm_1 = require("@gnss/rtcm");
const nmea_1 = require("@gnss/nmea");
/**
* Connection of a server/client to a single mountpoint
*/
class Connection extends events.EventEmitter {
constructor(parameters) {
var _a, _b, _c, _d, _e, _f;
super();
this.closed = false;
this._connectionTime = new Date();
this.transport = parameters.transport;
this.source = parameters.source;
this.token = parameters.token;
this.mountpoint = parameters.mountpoint;
this.input = (_a = parameters.input) !== null && _a !== void 0 ? _a : parameters.stream;
this.output = (_b = parameters.output) !== null && _b !== void 0 ? _b : parameters.stream;
// Disconnect if streams close
(_c = this.input) === null || _c === void 0 ? void 0 : _c.on('close', () => this.close());
(_d = this.input) === null || _d === void 0 ? void 0 : _d.on('error', (error) => this.error(error));
if (this.input !== this.output) {
(_e = this.output) === null || _e === void 0 ? void 0 : _e.on('close', () => this.close());
(_f = this.output) === null || _f === void 0 ? void 0 : _f.on('error', (error) => this.error(error));
}
}
get connectionTime() { return this._connectionTime; }
get disconnectionTime() { return this._disconnectionTime; }
/** Connection duration, in ms */
get duration() { var _a, _b; return ((_b = (_a = this._disconnectionTime) === null || _a === void 0 ? void 0 : _a.getTime()) !== null && _b !== void 0 ? _b : Date.now()) - this._connectionTime.getTime(); }
error(error) {
//this.emit('error', error);
this.close(error);
}
close(error) {
var _a, _b, _c, _d;
if (this.closed)
return;
if (!((_a = this.input) === null || _a === void 0 ? void 0 : _a.destroyed))
(_b = this.input) === null || _b === void 0 ? void 0 : _b.destroy(error);
if (!((_c = this.output) === null || _c === void 0 ? void 0 : _c.destroyed))
(_d = this.output) === null || _d === void 0 ? void 0 : _d.destroy(error);
this.closed = true;
this._disconnectionTime = new Date();
this.emit('close');
}
pipe(connection) {
this.input.pipe(connection.output, { end: false });
}
unpipe(connection) {
this.input.unpipe(connection.output);
}
}
exports.Connection = Connection;
/**
* Server connection to a single mountpoint for pushing data
*
* Servers must have an input stream, and can optionally have an output stream e.g. for VRS.
*/
class Server extends Connection {
constructor(parameters) {
if (parameters.input === undefined)
throw new Error("Server input stream must be provided");
super(parameters);
}
get type() { return 'server'; }
set str(str) {
this._str = str;
this.emit('str', str);
}
get str() {
return this._str;
}
get rtcm() {
var _a;
if (this._rtcm === undefined) {
this._rtcm = new rtcm_1.RtcmDecodeTransformStream({
closeOnError: false,
synchronizedInitially: false
});
(_a = this.input) === null || _a === void 0 ? void 0 : _a.pipe(this._rtcm);
}
return this._rtcm;
}
}
exports.Server = Server;
/**
* Client connection to a single mountpoint for receiving data
*
* Clients must have an output stream, and can optionally have an input stream e.g. for VRS.
*/
class Client extends Connection {
constructor(parameters) {
if (parameters.output === undefined)
throw new Error("Client output stream must be provided");
super(parameters);
}
get type() { return 'client'; }
set gga(gga) {
this._gga = gga;
this.emit('gga', gga);
this.parseGga();
}
get gga() {
return this._gga;
}
get nmea() {
var _a;
if (this._nmea === undefined) {
this._nmea = new nmea_1.NmeaDecodeTransformStream({
closeOnError: false,
synchronizedInitially: false
});
(_a = this.input) === null || _a === void 0 ? void 0 : _a.pipe(this._nmea);
this.parseGga();
}
return this._nmea;
}
parseGga() {
if (this._gga === undefined)
return;
if (this._nmea === undefined)
return;
try {
this._nmea.push(nmea_1.NmeaTransport.decode(this._gga));
}
catch (err) {
// Ignore invalid GGA
}
}
}
exports.Client = Client;