@signalk/streams 5.1.3 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/dist/actisense-serial.d.ts +3 -0
  2. package/dist/actisense-serial.d.ts.map +1 -0
  3. package/dist/actisense-serial.js +4 -0
  4. package/dist/autodetect.d.ts +54 -0
  5. package/dist/autodetect.d.ts.map +1 -0
  6. package/dist/autodetect.js +186 -0
  7. package/dist/canboatjs.d.ts +25 -0
  8. package/dist/canboatjs.d.ts.map +1 -0
  9. package/dist/canboatjs.js +57 -0
  10. package/dist/canbus.d.ts +3 -0
  11. package/dist/canbus.d.ts.map +1 -0
  12. package/dist/canbus.js +4 -0
  13. package/dist/execute.d.ts +34 -0
  14. package/dist/execute.d.ts.map +1 -0
  15. package/dist/execute.js +101 -0
  16. package/dist/filestream.d.ts +24 -0
  17. package/dist/filestream.d.ts.map +1 -0
  18. package/dist/filestream.js +58 -0
  19. package/dist/folderstream.d.ts +12 -0
  20. package/dist/folderstream.d.ts.map +1 -0
  21. package/dist/folderstream.js +36 -0
  22. package/dist/from_json.d.ts +6 -0
  23. package/dist/from_json.d.ts.map +1 -0
  24. package/dist/from_json.js +22 -0
  25. package/dist/gpiod-seatalk.d.ts +19 -0
  26. package/dist/gpiod-seatalk.d.ts.map +1 -0
  27. package/{gpiod-seatalk.js → dist/gpiod-seatalk.js} +18 -16
  28. package/dist/gpsd.d.ts +23 -0
  29. package/dist/gpsd.d.ts.map +1 -0
  30. package/dist/gpsd.js +55 -0
  31. package/dist/index.d.ts +5 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +14 -0
  34. package/dist/keys-filter.d.ts +27 -0
  35. package/dist/keys-filter.d.ts.map +1 -0
  36. package/dist/keys-filter.js +59 -0
  37. package/dist/liner.d.ts +14 -0
  38. package/dist/liner.d.ts.map +1 -0
  39. package/dist/liner.js +35 -0
  40. package/dist/log.d.ts +15 -0
  41. package/dist/log.d.ts.map +1 -0
  42. package/dist/log.js +17 -0
  43. package/dist/logging.d.ts +14 -0
  44. package/dist/logging.d.ts.map +1 -0
  45. package/dist/logging.js +125 -0
  46. package/dist/mdns-ws.d.ts +37 -0
  47. package/dist/mdns-ws.d.ts.map +1 -0
  48. package/dist/mdns-ws.js +144 -0
  49. package/dist/multiplexedlog.d.ts +3 -0
  50. package/dist/multiplexedlog.d.ts.map +1 -0
  51. package/dist/multiplexedlog.js +7 -0
  52. package/dist/n2k-signalk.d.ts +39 -0
  53. package/dist/n2k-signalk.d.ts.map +1 -0
  54. package/dist/n2k-signalk.js +180 -0
  55. package/dist/n2kAnalyzer.d.ts +19 -0
  56. package/dist/n2kAnalyzer.d.ts.map +1 -0
  57. package/dist/n2kAnalyzer.js +61 -0
  58. package/dist/nmea0183-signalk.d.ts +33 -0
  59. package/dist/nmea0183-signalk.d.ts.map +1 -0
  60. package/dist/nmea0183-signalk.js +105 -0
  61. package/dist/nullprovider.d.ts +5 -0
  62. package/dist/nullprovider.d.ts.map +1 -0
  63. package/dist/nullprovider.js +9 -0
  64. package/dist/pigpio-seatalk.d.ts +19 -0
  65. package/dist/pigpio-seatalk.d.ts.map +1 -0
  66. package/{pigpio-seatalk.js → dist/pigpio-seatalk.js} +21 -18
  67. package/dist/replacer.d.ts +13 -0
  68. package/dist/replacer.d.ts.map +1 -0
  69. package/dist/replacer.js +20 -0
  70. package/dist/s3.d.ts +13 -0
  71. package/dist/s3.d.ts.map +1 -0
  72. package/dist/s3.js +82 -0
  73. package/dist/serialport.d.ts +35 -0
  74. package/dist/serialport.d.ts.map +1 -0
  75. package/dist/serialport.js +125 -0
  76. package/dist/simple.d.ts +78 -0
  77. package/dist/simple.d.ts.map +1 -0
  78. package/dist/simple.js +446 -0
  79. package/dist/splitting-liner.d.ts +12 -0
  80. package/dist/splitting-liner.d.ts.map +1 -0
  81. package/dist/splitting-liner.js +19 -0
  82. package/dist/tcp.d.ts +32 -0
  83. package/dist/tcp.d.ts.map +1 -0
  84. package/dist/tcp.js +113 -0
  85. package/dist/tcpserver.d.ts +14 -0
  86. package/dist/tcpserver.d.ts.map +1 -0
  87. package/dist/tcpserver.js +18 -0
  88. package/dist/test-helpers.d.ts +52 -0
  89. package/dist/test-helpers.d.ts.map +1 -0
  90. package/dist/test-helpers.js +73 -0
  91. package/dist/throttle.d.ts +3 -0
  92. package/dist/throttle.d.ts.map +1 -0
  93. package/dist/throttle.js +4 -0
  94. package/dist/timestamp-throttle.d.ts +17 -0
  95. package/dist/timestamp-throttle.d.ts.map +1 -0
  96. package/dist/timestamp-throttle.js +41 -0
  97. package/dist/types.d.ts +8 -0
  98. package/dist/types.d.ts.map +1 -0
  99. package/dist/types.js +2 -0
  100. package/dist/udp.d.ts +26 -0
  101. package/dist/udp.d.ts.map +1 -0
  102. package/dist/udp.js +53 -0
  103. package/package.json +46 -4
  104. package/actisense-serial.js +0 -1
  105. package/autodetect.js +0 -198
  106. package/canboatjs.js +0 -71
  107. package/canbus.js +0 -17
  108. package/execute.js +0 -137
  109. package/filestream.js +0 -90
  110. package/folderstream.js +0 -36
  111. package/from_json.js +0 -51
  112. package/gpsd.js +0 -94
  113. package/keys-filter.js +0 -81
  114. package/liner.js +0 -68
  115. package/log.js +0 -51
  116. package/logging.js +0 -149
  117. package/mdns-ws.js +0 -167
  118. package/multiplexedlog.js +0 -3
  119. package/n2k-signalk.js +0 -195
  120. package/n2kAnalyzer.js +0 -82
  121. package/nmea0183-signalk.js +0 -119
  122. package/nullprovider.js +0 -31
  123. package/replacer.js +0 -57
  124. package/s3.js +0 -87
  125. package/serialport.js +0 -209
  126. package/simple.js +0 -446
  127. package/splitting-liner.js +0 -46
  128. package/tcp.js +0 -138
  129. package/tcpserver.js +0 -40
  130. package/throttle.js +0 -31
  131. package/timestamp-throttle.js +0 -63
  132. package/udp.js +0 -95
package/dist/simple.js ADDED
@@ -0,0 +1,446 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const stream_1 = require("stream");
7
+ const n2kAnalyzer_1 = __importDefault(require("./n2kAnalyzer"));
8
+ const from_json_1 = __importDefault(require("./from_json"));
9
+ const multiplexedlog_1 = __importDefault(require("./multiplexedlog"));
10
+ const nmea0183_signalk_1 = __importDefault(require("./nmea0183-signalk"));
11
+ const n2k_signalk_1 = __importDefault(require("./n2k-signalk"));
12
+ const log_1 = __importDefault(require("./log"));
13
+ const liner_1 = __importDefault(require("./liner"));
14
+ const splitting_liner_1 = __importDefault(require("./splitting-liner"));
15
+ const execute_1 = __importDefault(require("./execute"));
16
+ const udp_1 = __importDefault(require("./udp"));
17
+ const tcp_1 = __importDefault(require("./tcp"));
18
+ const tcpserver_1 = __importDefault(require("./tcpserver"));
19
+ const filestream_1 = __importDefault(require("./filestream"));
20
+ const replacer_1 = __importDefault(require("./replacer"));
21
+ const throttle_1 = __importDefault(require("./throttle"));
22
+ const timestamp_throttle_1 = __importDefault(require("./timestamp-throttle"));
23
+ const canboatjs_1 = __importDefault(require("./canboatjs"));
24
+ const canboatjs_2 = require("@canboat/canboatjs");
25
+ const gpsd_1 = __importDefault(require("./gpsd"));
26
+ const pigpio_seatalk_1 = __importDefault(require("./pigpio-seatalk"));
27
+ const gpiod_seatalk_1 = __importDefault(require("./gpiod-seatalk"));
28
+ const W2k01Ctor = canboatjs_2.W2k01;
29
+ const Ydwg02Ctor = canboatjs_2.Ydwg02;
30
+ const iKonvertCtor = canboatjs_2.iKonvert;
31
+ const discriminatorByDataType = {
32
+ NMEA2000JS: 'A',
33
+ NMEA2000IK: 'A',
34
+ NMEA2000YD: 'A',
35
+ NMEA2000: 'A',
36
+ NMEA0183: 'N',
37
+ SignalK: 'I',
38
+ Seatalk: 'N'
39
+ };
40
+ const dataTypeMapping = {
41
+ SignalK: (options) => options.subOptions.type !== 'wss' && options.subOptions.type !== 'ws'
42
+ ? [new from_json_1.default()]
43
+ : [],
44
+ Seatalk: (options) => [
45
+ new nmea0183_signalk_1.default({
46
+ ...options.subOptions,
47
+ validateChecksum: false
48
+ })
49
+ ],
50
+ NMEA0183: (options) => {
51
+ const result = [new nmea0183_signalk_1.default(options.subOptions)];
52
+ if (options.type === 'FileStream') {
53
+ result.unshift(new throttle_1.default({
54
+ rate: options.subOptions.throttleRate ?? 1000,
55
+ chunksize: options.subOptions.throttleRate ?? 1000
56
+ }));
57
+ }
58
+ return result;
59
+ },
60
+ NMEA2000: (options) => {
61
+ const result = [new n2kAnalyzer_1.default(options.subOptions)];
62
+ if (options.type === 'FileStream') {
63
+ result.push(new timestamp_throttle_1.default());
64
+ }
65
+ return [...result, new n2k_signalk_1.default(options.subOptions)];
66
+ },
67
+ NMEA2000JS: (options) => {
68
+ const result = [new canboatjs_1.default(options.subOptions)];
69
+ if (options.type === 'FileStream') {
70
+ result.push(new timestamp_throttle_1.default());
71
+ }
72
+ return [...result, new n2k_signalk_1.default(options.subOptions)];
73
+ },
74
+ NMEA2000IK: (options) => {
75
+ const result = [new canboatjs_1.default(options.subOptions)];
76
+ if (options.type === 'FileStream') {
77
+ result.push(new timestamp_throttle_1.default({
78
+ getMilliseconds: (msg) => {
79
+ return msg.timer * 1000;
80
+ }
81
+ }));
82
+ }
83
+ {
84
+ let subOptions;
85
+ if (options.subOptions.type === 'navlink2-tcp-canboatjs') {
86
+ subOptions = { ...options.subOptions, tcp: true };
87
+ }
88
+ else {
89
+ subOptions = options.subOptions;
90
+ }
91
+ result.unshift(new iKonvertCtor(subOptions));
92
+ }
93
+ return [...result, new n2k_signalk_1.default(options.subOptions)];
94
+ },
95
+ NMEA2000YD: (options) => {
96
+ const result = [
97
+ new Ydwg02Ctor({ ...options.subOptions }, options.subOptions.type === 'ydwg02-usb-canboatjs' ? 'usb' : 'network')
98
+ ];
99
+ if (options.type === 'FileStream') {
100
+ result.push(new timestamp_throttle_1.default());
101
+ }
102
+ return [...result, new n2k_signalk_1.default(options.subOptions)];
103
+ },
104
+ NMEA2000W2K_ASCII: (options) => {
105
+ const result = [
106
+ new W2k01Ctor({
107
+ format: 'ascii',
108
+ ...options.subOptions
109
+ })
110
+ ];
111
+ if (options.type === 'FileStream') {
112
+ result.push(new timestamp_throttle_1.default());
113
+ }
114
+ return [...result, new n2k_signalk_1.default(options.subOptions)];
115
+ },
116
+ NMEA2000W2K_ACTISENSE: (options) => {
117
+ const result = [
118
+ new W2k01Ctor({
119
+ format: 'actisense',
120
+ ...options.subOptions
121
+ })
122
+ ];
123
+ if (options.type === 'FileStream') {
124
+ result.push(new timestamp_throttle_1.default());
125
+ }
126
+ return [...result, new n2k_signalk_1.default(options.subOptions)];
127
+ },
128
+ Multiplexed: (options) => [new multiplexedlog_1.default(options.subOptions)]
129
+ };
130
+ function nmea2000input(subOptions, logging) {
131
+ if (subOptions.type === 'ngt-1-canboatjs') {
132
+ const ActisenseSerial = require('./actisense-serial');
133
+ const Ctor = ActisenseSerial.default ?? ActisenseSerial;
134
+ return [
135
+ new Ctor({
136
+ ...subOptions,
137
+ plainText: logging
138
+ })
139
+ ];
140
+ }
141
+ else if (subOptions.type === 'canbus-canboatjs') {
142
+ const Canbus = require('./canbus');
143
+ const Ctor = Canbus.default ?? Canbus;
144
+ return [
145
+ new Ctor({
146
+ ...subOptions,
147
+ canDevice: subOptions.interface
148
+ })
149
+ ];
150
+ }
151
+ else if (subOptions.type === 'ikonvert-canboatjs') {
152
+ const Serialport = require('./serialport');
153
+ const Ctor = Serialport.default ?? Serialport;
154
+ return [
155
+ new Ctor({
156
+ ...subOptions,
157
+ baudrate: 230400,
158
+ toStdout: 'ikonvertOut'
159
+ })
160
+ ];
161
+ }
162
+ else if (subOptions.type === 'ydwg02-canboatjs') {
163
+ return [
164
+ new tcp_1.default({ ...subOptions, outEvent: 'ydwg02-out' }),
165
+ new liner_1.default(subOptions)
166
+ ];
167
+ }
168
+ else if (subOptions.type === 'ydwg02-udp-canboatjs') {
169
+ return [
170
+ new udp_1.default({ ...subOptions, outEvent: 'ydwg02-out' }),
171
+ new liner_1.default(subOptions)
172
+ ];
173
+ }
174
+ else if (subOptions.type === 'navlink2-tcp-canboatjs') {
175
+ return [
176
+ new tcp_1.default({ ...subOptions, outEvent: 'navlink2-out' }),
177
+ new liner_1.default(subOptions)
178
+ ];
179
+ }
180
+ else if (subOptions.type === 'w2k-1-n2k-ascii-canboatjs') {
181
+ return [
182
+ new tcp_1.default({ ...subOptions, outEvent: 'w2k-1-out' }),
183
+ new liner_1.default(subOptions),
184
+ new W2k01Ctor(subOptions, 'ascii', 'w2k-1-out')
185
+ ];
186
+ }
187
+ else if (subOptions.type === 'w2k-1-n2k-actisense-canboatjs') {
188
+ return [
189
+ new tcp_1.default({ ...subOptions, outEvent: 'w2k-1-out' }),
190
+ new W2k01Ctor(subOptions, 'actisense', 'w2k-1-out')
191
+ ];
192
+ }
193
+ else if (subOptions.type === 'navlink2-udp-canboatjs') {
194
+ return [
195
+ new udp_1.default(subOptions),
196
+ new liner_1.default(subOptions)
197
+ ];
198
+ }
199
+ else if (subOptions.type === 'ydwg02-usb-canboatjs') {
200
+ const Serialport = require('./serialport');
201
+ const Ctor = Serialport.default ?? Serialport;
202
+ return [
203
+ new Ctor({
204
+ ...subOptions,
205
+ baudrate: 38400,
206
+ toStdout: 'ydwg02-out'
207
+ })
208
+ ];
209
+ }
210
+ else {
211
+ let command;
212
+ let toChildProcess;
213
+ if (subOptions.type === 'ngt-1') {
214
+ command = `actisense-serial -s ${subOptions.baudrate ?? 115200} ${subOptions.device}`;
215
+ toChildProcess = 'nmea2000out';
216
+ }
217
+ else if (subOptions.type === 'canbus') {
218
+ command = `candump ${subOptions.interface} | candump2analyzer`;
219
+ toChildProcess = undefined;
220
+ }
221
+ else {
222
+ throw new Error(`unknown NMEA2000 type ${subOptions.type}`);
223
+ }
224
+ return [
225
+ new execute_1.default({
226
+ command,
227
+ toChildProcess,
228
+ app: subOptions.app,
229
+ providerId: subOptions.providerId
230
+ }),
231
+ new liner_1.default(subOptions)
232
+ ];
233
+ }
234
+ }
235
+ function nmea0183input(subOptions) {
236
+ let pipePart;
237
+ if (subOptions.type === 'tcp') {
238
+ pipePart = [
239
+ new tcp_1.default(subOptions),
240
+ new liner_1.default(subOptions)
241
+ ];
242
+ }
243
+ else if (subOptions.type === 'tcpserver') {
244
+ pipePart = [new tcpserver_1.default(subOptions), new liner_1.default(subOptions)];
245
+ }
246
+ else if (subOptions.type === 'udp') {
247
+ pipePart = [
248
+ new udp_1.default(subOptions),
249
+ new splitting_liner_1.default(subOptions)
250
+ ];
251
+ }
252
+ else if (subOptions.type === 'serial') {
253
+ const Serialport = require('./serialport');
254
+ const Ctor = Serialport.default ?? Serialport;
255
+ pipePart = [new Ctor(subOptions)];
256
+ }
257
+ else if (subOptions.type === 'gpsd') {
258
+ pipePart = [new gpsd_1.default(subOptions)];
259
+ }
260
+ if (pipePart) {
261
+ if (subOptions.removeNulls) {
262
+ pipePart.push(new replacer_1.default({ regexp: '\u0000', template: '' }));
263
+ }
264
+ if (subOptions.ignoredSentences) {
265
+ console.log(subOptions.ignoredSentences);
266
+ subOptions.ignoredSentences.forEach((sentence) => {
267
+ if (sentence.length > 0) {
268
+ pipePart.push(new replacer_1.default({ regexp: `^...${sentence}.*`, template: '' }));
269
+ }
270
+ });
271
+ }
272
+ return pipePart;
273
+ }
274
+ else {
275
+ throw new Error(`Unknown networking type: ${subOptions.type}`);
276
+ }
277
+ }
278
+ function executeInput(subOptions) {
279
+ return [
280
+ new execute_1.default(subOptions),
281
+ new liner_1.default(subOptions)
282
+ ];
283
+ }
284
+ function fileInput(subOptions) {
285
+ return [
286
+ new filestream_1.default(subOptions),
287
+ new liner_1.default(subOptions)
288
+ ];
289
+ }
290
+ function signalKInput(subOptions) {
291
+ if (subOptions.type === 'ws' || subOptions.type === 'wss') {
292
+ const MdnsWs = require('./mdns-ws');
293
+ const Ctor = MdnsWs.default ?? MdnsWs;
294
+ return [new Ctor(subOptions)];
295
+ }
296
+ else if (subOptions.type === 'tcp') {
297
+ return [
298
+ new tcp_1.default(subOptions),
299
+ new liner_1.default(subOptions)
300
+ ];
301
+ }
302
+ else if (subOptions.type === 'udp') {
303
+ return [new udp_1.default(subOptions)];
304
+ }
305
+ else if (subOptions.type === 'serial') {
306
+ const Serialport = require('./serialport');
307
+ const Ctor = Serialport.default ?? Serialport;
308
+ return [new Ctor(subOptions)];
309
+ }
310
+ throw new Error(`unknown SignalK type: ${subOptions.type}`);
311
+ }
312
+ function seatalkInput(subOptions) {
313
+ if (subOptions.type === 'gpiod') {
314
+ return [new gpiod_seatalk_1.default(subOptions)];
315
+ }
316
+ else {
317
+ return [new pigpio_seatalk_1.default(subOptions)];
318
+ }
319
+ }
320
+ const pipeStartByType = {
321
+ NMEA2000: nmea2000input,
322
+ NMEA0183: nmea0183input,
323
+ Execute: executeInput,
324
+ FileStream: fileInput,
325
+ SignalK: signalKInput,
326
+ Seatalk: seatalkInput
327
+ };
328
+ function getLoggerPipeline(app, logging, discriminator) {
329
+ if (!logging) {
330
+ return [];
331
+ }
332
+ return [
333
+ new log_1.default({
334
+ app,
335
+ discriminator
336
+ })
337
+ ];
338
+ }
339
+ class Simple extends stream_1.Transform {
340
+ pipeline;
341
+ constructor(options) {
342
+ super({ objectMode: true });
343
+ const { emitPropertyValue, onPropertyValues, createDebug } = options;
344
+ const opts = { ...options };
345
+ opts.subOptions = {
346
+ ...options.subOptions,
347
+ emitPropertyValue,
348
+ onPropertyValues,
349
+ createDebug
350
+ };
351
+ opts.subOptions.providerId = options.providerId;
352
+ const dataType = opts.subOptions.dataType ?? options.type;
353
+ if (!dataType) {
354
+ throw new Error(`Unknown data type for ${options.type}`);
355
+ }
356
+ if (!pipeStartByType[options.type]) {
357
+ throw new Error(`Invalid input type: ${options.type}`);
358
+ }
359
+ if (!dataTypeMapping[dataType]) {
360
+ throw new Error(`Unknown data type: ${dataType}`);
361
+ }
362
+ if (dataType !== 'Multiplexed' && !discriminatorByDataType[dataType]) {
363
+ throw new Error(`No discriminator for: ${dataType}`);
364
+ }
365
+ opts.subOptions.app = options.app;
366
+ let mappingType = dataType;
367
+ if (options.type === 'NMEA2000' && opts.subOptions) {
368
+ if (opts.subOptions.type === 'ngt-1-canboatjs' ||
369
+ opts.subOptions.type === 'canbus-canboatjs' ||
370
+ opts.subOptions.type === 'w2k-1-n2k-actisense-canboatjs' ||
371
+ opts.subOptions.type === 'w2k-1-n2k-ascii-canboatjs') {
372
+ mappingType = 'NMEA2000JS';
373
+ }
374
+ else if (opts.subOptions.type === 'ikonvert-canboatjs' ||
375
+ opts.subOptions.type === 'navlink2-tcp-canboatjs') {
376
+ mappingType = 'NMEA2000IK';
377
+ }
378
+ else if (opts.subOptions.type === 'ydwg02-canboatjs' ||
379
+ opts.subOptions.type === 'ydwg02-udp-canboatjs' ||
380
+ opts.subOptions.type === 'ydwg02-usb-canboatjs') {
381
+ mappingType = 'NMEA2000YD';
382
+ }
383
+ options.app.on('nmea2000out', () => {
384
+ setImmediate(() => options.app.emit('connectionwrite', {
385
+ providerId: options.providerId
386
+ }));
387
+ });
388
+ options.app.on('nmea2000JsonOut', () => {
389
+ setImmediate(() => options.app.emit('connectionwrite', {
390
+ providerId: options.providerId
391
+ }));
392
+ });
393
+ }
394
+ const pipeStart = pipeStartByType[options.type];
395
+ const dataMapper = dataTypeMapping[mappingType];
396
+ const pipeline = [
397
+ ...pipeStart(opts.subOptions, options.logging),
398
+ ...getLoggerPipeline(options.app, options.logging, discriminatorByDataType[dataType]),
399
+ ...dataMapper(opts)
400
+ ];
401
+ const dataReceivedEventName = `${opts.subOptions.providerId}-received`;
402
+ const spy = new stream_1.Transform({
403
+ objectMode: true,
404
+ transform(chunk, encoding, callback) {
405
+ options.app.emit(dataReceivedEventName, chunk);
406
+ callback(null, chunk);
407
+ }
408
+ });
409
+ pipeline.splice(pipeline.length - 1, 0, spy);
410
+ opts.subOptions.app.emitPropertyValue('pipedprovider', {
411
+ id: opts.subOptions.providerId,
412
+ type: mappingType,
413
+ eventNames: {
414
+ received: dataReceivedEventName
415
+ }
416
+ });
417
+ if (opts.subOptions.overrideTimestamp) {
418
+ pipeline.push(new stream_1.Transform({
419
+ objectMode: true,
420
+ transform(delta, encoding, callback) {
421
+ if (delta.updates) {
422
+ const now = new Date().toISOString();
423
+ delta.updates.forEach((update) => {
424
+ update.timestamp = now;
425
+ });
426
+ }
427
+ callback(null, delta);
428
+ }
429
+ }));
430
+ }
431
+ for (let i = pipeline.length - 2; i >= 0; i--) {
432
+ pipeline[i].pipe(pipeline[i + 1]);
433
+ }
434
+ pipeline[pipeline.length - 1].pipe(this);
435
+ this.pipeline = pipeline;
436
+ }
437
+ _transform(msg, encoding, done) {
438
+ this.push(msg);
439
+ done();
440
+ }
441
+ end() {
442
+ this.pipeline[0].end();
443
+ return this;
444
+ }
445
+ }
446
+ exports.default = Simple;
@@ -0,0 +1,12 @@
1
+ import { Transform, TransformCallback } from 'stream';
2
+ interface SplittingLinerOptions {
3
+ lineSeparator?: string;
4
+ [key: string]: unknown;
5
+ }
6
+ export default class SplittingLiner extends Transform {
7
+ private readonly lineSeparator;
8
+ constructor(options?: SplittingLinerOptions);
9
+ _transform(chunk: Buffer, encoding: BufferEncoding, done: TransformCallback): void;
10
+ }
11
+ export {};
12
+ //# sourceMappingURL=splitting-liner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"splitting-liner.d.ts","sourceRoot":"","sources":["../src/splitting-liner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAA;AAErD,UAAU,qBAAqB;IAC7B,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACvB;AAED,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,SAAS;IACnD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAQ;gBAE1B,OAAO,GAAE,qBAA0B;IAK/C,UAAU,CACR,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,cAAc,EACxB,IAAI,EAAE,iBAAiB,GACtB,IAAI;CAQR"}
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const stream_1 = require("stream");
4
+ class SplittingLiner extends stream_1.Transform {
5
+ lineSeparator;
6
+ constructor(options = {}) {
7
+ super({ objectMode: true });
8
+ this.lineSeparator = options.lineSeparator ?? '\n';
9
+ }
10
+ _transform(chunk, encoding, done) {
11
+ const data = chunk.toString();
12
+ const lines = data.split(this.lineSeparator);
13
+ for (const line of lines) {
14
+ this.push(line);
15
+ }
16
+ done();
17
+ }
18
+ }
19
+ exports.default = SplittingLiner;
package/dist/tcp.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { Transform, TransformCallback } from 'stream';
2
+ import type { CreateDebug } from './types';
3
+ interface TcpOptions {
4
+ host: string;
5
+ port: number;
6
+ app: {
7
+ on(event: string, cb: (...args: any[]) => void): void;
8
+ emit(event: string, ...args: unknown[]): void;
9
+ setProviderStatus(id: string, msg: string): void;
10
+ setProviderError(id: string, msg: string): void;
11
+ };
12
+ providerId: string;
13
+ noDataReceivedTimeout?: string | number;
14
+ outEvent?: string;
15
+ toStdout?: string | string[];
16
+ createDebug?: CreateDebug;
17
+ [key: string]: unknown;
18
+ }
19
+ export default class TcpStream extends Transform {
20
+ private readonly options;
21
+ private readonly debug;
22
+ private readonly debugData;
23
+ private readonly noDataReceivedTimeout;
24
+ private tcpStream;
25
+ private reconnector;
26
+ constructor(options: TcpOptions);
27
+ pipe<T extends NodeJS.WritableStream>(pipeTo: T): T;
28
+ end(): this;
29
+ _transform(data: Buffer, encoding: BufferEncoding, callback: TransformCallback): void;
30
+ }
31
+ export {};
32
+ //# sourceMappingURL=tcp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tcp.d.ts","sourceRoot":"","sources":["../src/tcp.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAA;AAErD,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,SAAS,CAAA;AAEvD,UAAU,UAAU;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE;QAEH,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI,CAAA;QACrD,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;QAC7C,iBAAiB,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;QAChD,gBAAgB,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;KAChD,CAAA;IACD,UAAU,EAAE,MAAM,CAAA;IAClB,qBAAqB,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAC5B,WAAW,CAAC,EAAE,WAAW,CAAA;IACzB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACvB;AAED,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,SAAS;IAC9C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAY;IACpC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;IACnC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAQ;IAC9C,OAAO,CAAC,SAAS,CAAwB;IACzC,OAAO,CAAC,WAAW,CAAsC;gBAE7C,OAAO,EAAE,UAAU;IAW/B,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC;IAmFnD,GAAG,IAAI,IAAI;IAOX,UAAU,CACR,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,iBAAiB,GAC1B,IAAI;CAGR"}
package/dist/tcp.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const net_1 = __importDefault(require("net"));
7
+ const stream_1 = require("stream");
8
+ const reconnect_core_1 = __importDefault(require("reconnect-core"));
9
+ class TcpStream extends stream_1.Transform {
10
+ options;
11
+ debug;
12
+ debugData;
13
+ noDataReceivedTimeout;
14
+ tcpStream;
15
+ reconnector = null;
16
+ constructor(options) {
17
+ super();
18
+ this.options = options;
19
+ this.noDataReceivedTimeout =
20
+ Number.parseInt((this.options.noDataReceivedTimeout + '').trim()) * 1000;
21
+ const createDebug = options.createDebug ?? require('debug');
22
+ this.debug = createDebug('signalk:streams:tcp');
23
+ this.debug(`noDataReceivedTimeout:${this.noDataReceivedTimeout}`);
24
+ this.debugData = createDebug('signalk:streams:tcp-data');
25
+ }
26
+ pipe(pipeTo) {
27
+ if (this.options.outEvent) {
28
+ this.options.app.on(this.options.outEvent, (d) => {
29
+ if (this.tcpStream) {
30
+ this.debug('sending %s', d);
31
+ this.tcpStream.write(d);
32
+ setImmediate(() => {
33
+ this.options.app.emit('connectionwrite', {
34
+ providerId: this.options.providerId
35
+ });
36
+ });
37
+ }
38
+ });
39
+ }
40
+ const stdOutEvent = this.options.toStdout;
41
+ if (stdOutEvent) {
42
+ const events = Array.isArray(stdOutEvent) ? stdOutEvent : [stdOutEvent];
43
+ for (const stdEvent of events) {
44
+ this.options.app.on(stdEvent, (d) => {
45
+ if (this.tcpStream) {
46
+ this.tcpStream.write(d + '\r\n');
47
+ this.debug('event %s sending %s', stdEvent, d);
48
+ }
49
+ });
50
+ }
51
+ }
52
+ this.reconnector = (0, reconnect_core_1.default)((opts) => {
53
+ return net_1.default.connect(opts);
54
+ })({ maxDelay: 5 * 1000 }, (tcpStream) => {
55
+ if (!isNaN(this.noDataReceivedTimeout)) {
56
+ tcpStream.setTimeout(this.noDataReceivedTimeout);
57
+ this.debug(`Setting socket idle timeout ${this.options.host}:${this.options.port} ${this.noDataReceivedTimeout}`);
58
+ tcpStream.on('timeout', () => {
59
+ this.debug(`Idle timeout, closing socket ${this.options.host}:${this.options.port}`);
60
+ tcpStream.end();
61
+ });
62
+ }
63
+ tcpStream.on('data', (data) => {
64
+ if (this.debugData.enabled) {
65
+ this.debugData(data.toString());
66
+ }
67
+ this.write(data);
68
+ });
69
+ })
70
+ .on('connect', (con) => {
71
+ this.tcpStream = con;
72
+ const msg = `Connected to ${this.options.host} ${this.options.port}`;
73
+ this.options.app.setProviderStatus(this.options.providerId, msg);
74
+ this.debug(msg);
75
+ })
76
+ .on('reconnect', (n, delay) => {
77
+ const msg = `Reconnect ${this.options.host} ${this.options.port} retry ${n} delay ${delay}`;
78
+ this.options.app.setProviderError(this.options.providerId, msg);
79
+ this.debug(msg);
80
+ })
81
+ .on('disconnect', () => {
82
+ this.tcpStream = undefined;
83
+ this.debug(`Disconnected ${this.options.host} ${this.options.port}`);
84
+ })
85
+ .on('error', (err) => {
86
+ let msg;
87
+ if (err.message && err.message.length > 0) {
88
+ msg = err.message;
89
+ }
90
+ else if (err.errors) {
91
+ msg = err.errors.toString();
92
+ }
93
+ else {
94
+ msg = err.toString();
95
+ }
96
+ this.options.app.setProviderError(this.options.providerId, msg);
97
+ console.error('TcpProvider:' + msg);
98
+ })
99
+ .connect(this.options);
100
+ super.pipe(pipeTo);
101
+ return pipeTo;
102
+ }
103
+ end() {
104
+ if (this.reconnector) {
105
+ this.reconnector.disconnect();
106
+ }
107
+ return this;
108
+ }
109
+ _transform(data, encoding, callback) {
110
+ callback(null, data);
111
+ }
112
+ }
113
+ exports.default = TcpStream;
@@ -0,0 +1,14 @@
1
+ import { Transform, TransformCallback } from 'stream';
2
+ interface TcpServerOptions {
3
+ app: {
4
+ on(event: string, cb: (...args: any[]) => void): void;
5
+ };
6
+ }
7
+ export default class TcpServer extends Transform {
8
+ private readonly options;
9
+ constructor(options: TcpServerOptions);
10
+ pipe<T extends NodeJS.WritableStream>(pipeTo: T): T;
11
+ _transform(data: Buffer, encoding: BufferEncoding, callback: TransformCallback): void;
12
+ }
13
+ export {};
14
+ //# sourceMappingURL=tcpserver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tcpserver.d.ts","sourceRoot":"","sources":["../src/tcpserver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAA;AAErD,UAAU,gBAAgB;IACxB,GAAG,EAAE;QAEH,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI,CAAA;KACtD,CAAA;CACF;AAED,MAAM,CAAC,OAAO,OAAO,SAAU,SAAQ,SAAS;IAC9C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkB;gBAE9B,OAAO,EAAE,gBAAgB;IAKrC,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,cAAc,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC;IAKnD,UAAU,CACR,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,cAAc,EACxB,QAAQ,EAAE,iBAAiB,GAC1B,IAAI;CAGR"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const stream_1 = require("stream");
4
+ class TcpServer extends stream_1.Transform {
5
+ options;
6
+ constructor(options) {
7
+ super();
8
+ this.options = options;
9
+ }
10
+ pipe(pipeTo) {
11
+ this.options.app.on('tcpserver0183data', (d) => this.write(d));
12
+ return super.pipe(pipeTo);
13
+ }
14
+ _transform(data, encoding, callback) {
15
+ callback(null, data);
16
+ }
17
+ }
18
+ exports.default = TcpServer;