From eeccb67b372345d885f60f43cff90a093b7359da Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 28 Mar 2024 10:16:34 +0000 Subject: [PATCH 1/2] Remove Object.hasOwn --- lib/logging/log.js | 9 +++++---- lib/utils.js | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/logging/log.js b/lib/logging/log.js index 92d4cda..fbc89c9 100644 --- a/lib/logging/log.js +++ b/lib/logging/log.js @@ -1,4 +1,5 @@ const LogBuffer = require('./logBuffer') +const { hasProperty } = require('../utils') let buffer let mqtt @@ -21,13 +22,13 @@ function NRlog (msg) { let jsMsg try { jsMsg = JSON.parse(msg) + if (!hasProperty(jsMsg, 'ts') && !hasProperty(jsMsg, 'level')) { + // not a NR log message + jsMsg = { ts: Date.now(), level: '', msg } + } } catch (eee) { jsMsg = { ts: Date.now(), level: '', msg } } - if (!Object.hasOwn(jsMsg, 'ts') && !Object.hasOwn(jsMsg, 'level')) { - // not a NR log message - jsMsg = { ts: Date.now(), level: '', msg } - } const date = new Date(jsMsg.ts) if (typeof jsMsg.msg !== 'string') { jsMsg.msg = JSON.stringify(jsMsg.msg) diff --git a/lib/utils.js b/lib/utils.js index e3285ac..2665964 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -73,7 +73,7 @@ function isObject (object) { } /** - * Test if an object has a property + * Test if an object has a property - Node 14 friendly version of Object.hasOwn * @param {Object} object - an object to check for a property * @param {String} property - the name of the property to check for * @returns `true` if the object has the property, `false` otherwise From b461a4f27f3edf14ef1faff2c9fda332c9e87283 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 28 Mar 2024 10:33:48 +0000 Subject: [PATCH 2/2] Update tests to include logging tests --- package.json | 4 ++-- test/unit/lib/logging/log_spec.js | 7 +------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index d4c8830..7e361a0 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,8 @@ "lint": "eslint -c .eslintrc \"*.js\" \"lib/**/*.js\" \"frontend/**/*.js\" \"frontend/**/*.html\" \"test/**/*.js\"", "lint:fix": "eslint -c .eslintrc \"*.js\" \"lib/**/*.js\" \"frontend/**/*.js\" \"frontend/**/*.html\" \"test/**/*.js\" --fix", "test": "npm run test:lib && npm run test:frontend", - "test:lib": "mocha test/unit/**/*_spec.js", - "test:frontend": "mocha test/unit/frontend/**/*.spec.js" + "test:lib": "mocha 'test/unit/**/*_spec.js'", + "test:frontend": "mocha 'test/unit/frontend/**/*.spec.js'" }, "bin": { "flowforge-device-agent": "./index.js", diff --git a/test/unit/lib/logging/log_spec.js b/test/unit/lib/logging/log_spec.js index 7697419..c2904ac 100644 --- a/test/unit/lib/logging/log_spec.js +++ b/test/unit/lib/logging/log_spec.js @@ -29,12 +29,7 @@ describe('Log', function () { msg: 'm5' })) Log.info('m6') - Log.NRlog(JSON.stringify({ - level: 'info', - ts: Date.now(), - msg: 'm7' - })) - + Log.NRlog('m7') const bufferedMessages = Log.getBufferedMessages() // Verify we buffered the last 5 bufferedMessages.should.have.length(5)