@signalk/streams 1.16.2 → 2.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.
package/README.md CHANGED
@@ -0,0 +1,14 @@
1
+ # @signalk/streams
2
+
3
+ Used within the [Signal K](http://signalk.org) [Node Server](https://github.com/SignalK/signalk-server-node) for streaming and converting input data in different formats.
4
+
5
+ The code is not compiled and is not more effective, but allows using the streams and their conversions independently outside the server.
6
+
7
+ ## Development
8
+
9
+ * Install dev packages with `npm i`.
10
+ * Edit files with `/src`.
11
+ * `npm link`
12
+ * `cd ../../`
13
+ * `npm link @signalk/streams`
14
+ * Restart signalk `npm start`
package/canboatjs.js CHANGED
@@ -30,7 +30,7 @@ function CanboatJs (options) {
30
30
  debug(`[warning] ${pgn.pgn} ${warning}`)
31
31
  })
32
32
 
33
- this.fromPgn.on('error', (err) => {console.log(err)} )
33
+ this.fromPgn.on('error', (pgn, err) => {console.log(err)} )
34
34
 
35
35
  this.app = options.app
36
36
  }
package/logging.js CHANGED
@@ -28,6 +28,53 @@ module.exports = {
28
28
  listLogFiles
29
29
  }
30
30
 
31
+ class FileTimestampStreamWithDelete extends FileTimestampStream {
32
+ constructor(app, fullLogDir, filesToKeep, options){
33
+ super(options)
34
+ this.app = app
35
+ this.filesToKeep = filesToKeep
36
+ this.fullLogDir = fullLogDir
37
+ this.prevFilename = undefined
38
+ }
39
+
40
+ // This method of base class is called when new file name is contemplated
41
+ // So let's override it to check how many files are there and delete the oldest ones
42
+ newFilename() {
43
+ if (this.prevFilename !== this.currentFilename){ // Only do that after new file created
44
+ this.prevFilename = this.currentFilename
45
+ this.deleteOldFiles()
46
+ }
47
+ return super.newFilename()
48
+ }
49
+
50
+ deleteOldFiles(){
51
+ debug(`Checking for old log files`)
52
+ listLogFiles(this.app, (err, files) => {
53
+ if (err) {
54
+ console.error(err);
55
+ }else{
56
+ if (files.length > this.filesToKeep) {
57
+ const sortedFiles = files.sort();
58
+ const numToDelete = files.length - this.filesToKeep;
59
+ debug(`Will delete ${numToDelete} files`)
60
+ for(let i = 0; i < numToDelete; i++){
61
+ const fileName = path.join(this.fullLogDir, sortedFiles[i])
62
+ debug(`Deleting ${fileName}`)
63
+ fs.unlink(fileName, (err) => {
64
+ if (err){
65
+ console.error(err)
66
+ }
67
+ else {
68
+ debug(`${fileName} was deleted`)
69
+ }
70
+ });
71
+ }
72
+ }
73
+ }
74
+ });
75
+ }
76
+ }
77
+
31
78
  function getLogger (app, discriminator = '', logdir) {
32
79
  const fullLogdir = getFullLogDir(app, logdir)
33
80
 
@@ -36,10 +83,21 @@ function getLogger (app, discriminator = '', logdir) {
36
83
 
37
84
  debug(`logging to ${fileName}`)
38
85
 
39
- loggers[fullLogdir] = new FileTimestampStream({
40
- path: fileName
41
- })
86
+ let fileTimestampStream
87
+ if (app.config.settings.keepMostRecentLogsOnly){ // Delete old logs
88
+ fileTimestampStream = new FileTimestampStreamWithDelete(
89
+ app, fullLogdir, app.config.settings.logCountToKeep,
90
+ { path: fileName }
91
+ )
92
+ }else{ // Don't delete any logs
93
+ fileTimestampStream = new FileTimestampStream(
94
+ { path: fileName }
95
+ )
96
+ }
97
+
98
+ loggers[fullLogdir] = fileTimestampStream
42
99
  }
100
+
43
101
  const logger = loggers[fullLogdir]
44
102
  logger.on('error', err => {
45
103
  console.error(`Error opening data logging file: ${err.message}`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@signalk/streams",
3
- "version": "1.16.2",
3
+ "version": "2.0.0",
4
4
  "description": "Utilities for handling streams of Signal K data",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/s3.js CHANGED
@@ -15,7 +15,15 @@
15
15
  */
16
16
 
17
17
  var Transform = require('stream').Transform
18
- const AWS = require('aws-sdk')
18
+ /*
19
+ aws-sdk is not included in dependencies because of the
20
+ persistent deprecation warnings caused by its transitive
21
+ dependencies. This feature is not in wide use, especially
22
+ not in signalk-server where people encounter the scary looking
23
+ deprecation warnings.
24
+ Known to work with ^2.413.0
25
+ */
26
+ const AWS = require('aws-sdk')
19
27
  const debug = require('debug')('signalk:streams:s3-provider')
20
28
 
21
29
  function S3Provider ({ bucket, prefix }) {
package/simple.js CHANGED
@@ -27,6 +27,10 @@ const pigpioSeatalk = require('./pigpio-seatalk')
27
27
  function Simple (options) {
28
28
  Transform.call(this, { objectMode: true })
29
29
 
30
+ const { emitPropertyValue, onPropertyValues } = options
31
+ options = { ...options }
32
+ options.subOptions = { ...options.subOptions, emitPropertyValue, onPropertyValues }
33
+
30
34
  options.subOptions.providerId = options.providerId
31
35
  const dataType = options.subOptions.dataType || options.type
32
36
  if (!dataType) {
@@ -167,7 +171,7 @@ const dataTypeMapping = {
167
171
  return result.concat([new N2kToSignalK(options.subOptions)])
168
172
  },
169
173
  NMEA2000YD: options => {
170
- const result = [new Ydwg02(options, options.subOptions.type === 'ydwg02-usb-canboatjs' ? 'usb' : 'network')]
174
+ const result = [new Ydwg02(options.subOptions, options.subOptions.type === 'ydwg02-usb-canboatjs' ? 'usb' : 'network')]
171
175
  if (options.type === 'FileStream') {
172
176
  result.push(new TimestampThrottle())
173
177
  }