From a11a8b4c83e3de5b5cf4c7d0687d7b7db932aa6e Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Sat, 27 May 2023 15:05:05 -0700 Subject: [PATCH] update dependency, fixes for logger update --- .eslintrc.json | 2 +- package-lock.json | 12 ++++++------ package.json | 2 +- server.js | 16 ++++++++++------ src/service.js | 15 +++++++++++++-- test/src/service.js | 17 +++++++++++++++-- test/src/worker.js | 12 +++++++++++- 7 files changed, 57 insertions(+), 19 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 10477c0..5ab981d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -12,7 +12,7 @@ "plugin:sonarjs/recommended" ], "parserOptions": { - "ecmaVersion": 2018 + "ecmaVersion": "latest" }, "plugins": [ "node", diff --git a/package-lock.json b/package-lock.json index e784499..d95efa3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@squeep/api-dingus": "git+https://git.squeep.com/squeep-api-dingus/#v1.2.9", "@squeep/authentication-module": "git+https://git.squeep.com/squeep-authentication-module/#v1.3.0", "@squeep/html-template-helper": "git+https://git.squeep.com/squeep-html-template-helper#v1.4.0", - "@squeep/logger-json-console": "git+https://git.squeep.com/squeep-logger-json-console#v1.0.2", + "@squeep/logger-json-console": "git+https://git.squeep.com/squeep-logger-json-console#v2.0.0", "@squeep/web-linking": "^1.0.7", "axios": "^1.4.0", "feedparser": "^2.2.10", @@ -880,11 +880,11 @@ } }, "node_modules/@squeep/logger-json-console": { - "version": "1.0.2", - "resolved": "git+https://git.squeep.com/squeep-logger-json-console#dbff0fa5f018f7a302f73250e55f761c0ccf24b1", + "version": "2.0.0", + "resolved": "git+https://git.squeep.com/squeep-logger-json-console#9df1ebe7b067a00a012b1deb303278e51a711e37", "license": "ISC", "engines": { - "node": ">=12.0.0" + "node": ">=14" } }, "node_modules/@squeep/mystery-box": { @@ -6169,8 +6169,8 @@ "integrity": "sha512-wRdR4IOqWXoDMArx0HPo5MtM2Wk5wemAULbZ6PabVw1ylSQekkzKfoAUuupxsKuzjcRPjZvbpGDv+i04hBMnQw==" }, "@squeep/logger-json-console": { - "version": "git+https://git.squeep.com/squeep-logger-json-console#dbff0fa5f018f7a302f73250e55f761c0ccf24b1", - "from": "@squeep/logger-json-console@git+https://git.squeep.com/squeep-logger-json-console#v1.0.2" + "version": "git+https://git.squeep.com/squeep-logger-json-console#9df1ebe7b067a00a012b1deb303278e51a711e37", + "from": "@squeep/logger-json-console@git+https://git.squeep.com/squeep-logger-json-console#v2.0.0" }, "@squeep/mystery-box": { "version": "2.0.1", diff --git a/package.json b/package.json index 6dac33c..71f8255 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "@squeep/api-dingus": "git+https://git.squeep.com/squeep-api-dingus/#v1.2.9", "@squeep/authentication-module": "git+https://git.squeep.com/squeep-authentication-module/#v1.3.0", "@squeep/html-template-helper": "git+https://git.squeep.com/squeep-html-template-helper#v1.4.0", - "@squeep/logger-json-console": "git+https://git.squeep.com/squeep-logger-json-console#v1.0.2", + "@squeep/logger-json-console": "git+https://git.squeep.com/squeep-logger-json-console#v2.0.0", "@squeep/web-linking": "^1.0.7", "axios": "^1.4.0", "feedparser": "^2.2.10", diff --git a/server.js b/server.js index 49ce8f6..c12d19a 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,7 @@ 'use strict'; -const http = require('http'); +const http = require('node:http'); +const { AsyncLocalStorage } = require('node:async_hooks'); const DB = require('./src/db'); const Logger = require('./src/logger'); const Service = require('./src/service'); @@ -16,14 +17,17 @@ const ADDR = process.env.LISTEN_ADDR || '127.0.0.1'; (async function main () { let config, logger, db, service; try { + const asyncLocalStorage = new AsyncLocalStorage(); config = new Config(process.env.NODE_ENV); - logger = new Logger(config); + logger = new Logger(config.logger, { nodeId: config.nodeId }, asyncLocalStorage); db = new DB(logger, config); await db.initialize(); - service = new Service(logger, db, config); + service = new Service(logger, db, config, asyncLocalStorage); - http.createServer((req, res) => { - service.dispatch(req, res); + http.createServer(async (req, res) => { + await asyncLocalStorage.run({}, async () => { + await service.dispatch(req, res); + }); }).listen(PORT, ADDR, (err) => { if (err) { logger.error(_scope, 'error creating server', err); @@ -33,6 +37,6 @@ const ADDR = process.env.LISTEN_ADDR || '127.0.0.1'; }); } catch (e) { (logger || console).error(_scope, 'error starting server', e); - db && db.listener && await db.listener.stop(); + db?.listener && await db.listener.stop(); } })(); \ No newline at end of file diff --git a/src/service.js b/src/service.js index e769599..b498829 100644 --- a/src/service.js +++ b/src/service.js @@ -15,12 +15,12 @@ const path = require('path'); const _fileScope = common.fileScope(__filename); class Service extends Dingus { - constructor(logger, db, options) { + constructor(logger, db, options, asyncLocalStorage) { super(logger, { ...options.dingus, ignoreTrailingSlash: false, }); - + this.asyncLocalStorage = asyncLocalStorage; this.manager = new Manager(logger, db, options); this.authenticator = new Authenticator(logger, db, options); this.sessionManager = new SessionManager(logger, this.authenticator, options); @@ -70,6 +70,17 @@ class Service extends Dingus { } + /** + * Rearrange logging data. + */ + async preHandler(req, res, ctx) { + await super.preHandler(req, res, ctx); + const logObject = this.asyncLocalStorage.getStore(); + logObject.requestId = ctx.requestId; + delete ctx.requestId; + } + + /** * @param {http.ClientRequest} req * @param {http.ServerResponse} res diff --git a/test/src/service.js b/test/src/service.js index 62c0059..2de3b52 100644 --- a/test/src/service.js +++ b/test/src/service.js @@ -10,15 +10,17 @@ const stubDb = require('../stub-db'); const stubLogger = require('../stub-logger'); const Service = require('../../src/service'); const Config = require('../../config'); +const { AsyncLocalStorage } = require('node:async_hooks'); describe('Service', function () { - let service, options; + let service, options, asyncLocalStorage; let req, res, ctx; beforeEach(function () { + asyncLocalStorage = new AsyncLocalStorage(); options = new Config('test'); - service = new Service(stubLogger, stubDb, options); + service = new Service(stubLogger, stubDb, options, asyncLocalStorage); sinon.stub(service.manager); sinon.stub(service.sessionManager); sinon.stub(service.authenticator); @@ -46,6 +48,17 @@ describe('Service', function () { assert(service); }); + describe('preHandler', function () { + it('logs requestId', async () => { + sinon.stub(service.__proto__.__proto__, 'preHandler').resolves(); + await service.asyncLocalStorage.run({}, async () => { + await service.preHandler(req, res, ctx); + const logObject = service.asyncLocalStorage.getStore(); + assert('requestId' in logObject); + }); + }); + }); // preHandler + describe('maybeIngestBody', function () { beforeEach(function () { sinon.stub(service, 'bodyData'); diff --git a/test/src/worker.js b/test/src/worker.js index 5c97c0b..6e13158 100644 --- a/test/src/worker.js +++ b/test/src/worker.js @@ -131,6 +131,12 @@ describe('Worker', function () { stubCtx = {}; }); it('gets tasks', async function () { + /** + * In older versions, could just deepStrictEqual un-awaited promises for equality, + * but post 14 or so, async_id symbol properties are included in comparison, and + * in some executions of test suites these are somehow different in value so the test + * was failing. So now we settle everything prior to comparison. + */ const expected = [ Promise.resolve('first'), Promise.reject('bad'), @@ -138,7 +144,11 @@ describe('Worker', function () { ]; worker.promiseGiver.resolves(expected); const result = await worker._getWork(stubCtx); - assert.deepStrictEqual(result, expected); + + const expectedResolved = await Promise.allSettled(expected); + const resultResolved = await Promise.allSettled(result); + assert.deepStrictEqual(resultResolved, expectedResolved); + assert.strictEqual(worker.inFlight.length, expected.length); }); it('covers none wanted', async function () { -- 2.45.2