From d234c76d5900023b2b5b3a701eb8226031d26491 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 20 Dec 2021 10:28:55 +0100 Subject: [PATCH 01/16] [WIP] upgrade hapi --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 841fb53..b871991 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "bytes-counter": "~1.0.0", "commander": "~2.9.0", "csv-parser": "~1.9.3", - "hapi": "16.7.0", + "hapi": "17.8.5", "joi": "14.0.6", "json-csv": "1.5.0", "lodash": "~4.17.5", From 80d5d6b2f627a1ebfc47843948a3d1cefbd2e03e Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 21 Dec 2021 11:27:46 +0100 Subject: [PATCH 02/16] upgrade handlers --- lib/server/handlers/sthGetLogLevelHandler.js | 1 + lib/server/handlers/sthGetVersionHandler.js | 2 +- lib/server/handlers/sthNotFoundHandler.js | 5 +++-- lib/server/handlers/sthNotificationHandler.js | 8 ++++---- lib/server/handlers/sthSetLogLevelHandler.js | 3 ++- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/server/handlers/sthGetLogLevelHandler.js b/lib/server/handlers/sthGetLogLevelHandler.js index d9b7034..35cd18d 100644 --- a/lib/server/handlers/sthGetLogLevelHandler.js +++ b/lib/server/handlers/sthGetLogLevelHandler.js @@ -55,6 +55,7 @@ function getLogLevelHandler(request, reply) { const response = reply(getLogLevelResponse(level)); sthServerUtils.addFiwareCorrelator(request, response); + return response; } module.exports = getLogLevelHandler; diff --git a/lib/server/handlers/sthGetVersionHandler.js b/lib/server/handlers/sthGetVersionHandler.js index 4d0036c..64c2766 100644 --- a/lib/server/handlers/sthGetVersionHandler.js +++ b/lib/server/handlers/sthGetVersionHandler.js @@ -31,7 +31,7 @@ const sthUtils = require(ROOT_PATH + '/lib/utils/sthUtils'); */ function getVersionHandler(request, reply) { const message = sthUtils.getVersion(); - return reply(message); + return reply.response(message); } module.exports = getVersionHandler; diff --git a/lib/server/handlers/sthNotFoundHandler.js b/lib/server/handlers/sthNotFoundHandler.js index e342ea9..70c4635 100644 --- a/lib/server/handlers/sthNotFoundHandler.js +++ b/lib/server/handlers/sthNotFoundHandler.js @@ -41,11 +41,12 @@ function notFoundHandler(request, reply) { '404 - Not Found route for the request: ' + request.method.toUpperCase() + ' ' + request.url.path ); if (request.path.startsWith('/STH/v2')) { - response = reply({ error: 'NotFound', description: 'invalid path' }).code(404); + response = reply.response({ error: 'NotFound', description: 'invalid path' }).code(404); } else { - response = reply({ statusCode: 404, error: 'Not Found' }).code(404); + response = reply.response({ statusCode: 404, error: 'Not Found' }).code(404); } sthServerUtils.addFiwareCorrelator(request, response); + return response; } module.exports = notFoundHandler; diff --git a/lib/server/handlers/sthNotificationHandler.js b/lib/server/handlers/sthNotificationHandler.js index d671f29..1e6dac3 100644 --- a/lib/server/handlers/sthNotificationHandler.js +++ b/lib/server/handlers/sthNotificationHandler.js @@ -195,7 +195,7 @@ function storeRawData(data, reply, callback) { sthLogger.debug(request.sth.context, 'Raw data successfully stored'); } if (++counterObj.counter === totalTasks) { - response = reply(err); + response = reply.response(err); sthServerUtils.addFiwareCorrelator(request, response); } process.nextTick(callback.bind(null, err)); @@ -255,7 +255,7 @@ function storeAggregatedData(data, reply) { // There was an error when getting the collection sthLogger.error(request.sth.context, 'Error when getting the aggregated data collection for storing'); if (++counterObj.counter === totalTasks) { - response = reply(err); + response = reply.response(err); sthServerUtils.addFiwareCorrelator(request, response); } } else { @@ -282,7 +282,7 @@ function storeAggregatedData(data, reply) { sthLogger.debug(request.sth.context, 'Aggregated data successfully stored'); } if (++counterObj.counter === totalTasks) { - response = reply(err); + response = reply.response(err); sthServerUtils.addFiwareCorrelator(request, response); } } @@ -390,7 +390,7 @@ function processNotification(recvTime, request, reply) { ); const error = boom.badRequest(message); error.output.payload.validation = { source: 'payload', keys: ['attributes'] }; - return reply(error); + return reply.response(error); } for (let i = 0; i < contextResponses.length; i++) { diff --git a/lib/server/handlers/sthSetLogLevelHandler.js b/lib/server/handlers/sthSetLogLevelHandler.js index 794989b..9624246 100644 --- a/lib/server/handlers/sthSetLogLevelHandler.js +++ b/lib/server/handlers/sthSetLogLevelHandler.js @@ -41,8 +41,9 @@ function setLogLevelHandler(request, reply) { sthLogger.info(request.sth.context, 'Log level set to: ' + level); - const response = reply(); + const response = reply.response(); sthServerUtils.addFiwareCorrelator(request, response); + return response; } module.exports = setLogLevelHandler; From 2b0be8399bf875b3dab1464b33ad28da609a03a2 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 21 Dec 2021 11:35:05 +0100 Subject: [PATCH 03/16] upgrade to @happi/hapi --- lib/server/sthServer.js | 140 +++++++++++++----------- package.json | 2 +- test/unit/sthGetLogLevelHandler_test.js | 2 +- test/unit/sth_test.js | 2 +- 4 files changed, 81 insertions(+), 65 deletions(-) diff --git a/lib/server/sthServer.js b/lib/server/sthServer.js index 27152a0..dee4b0a 100644 --- a/lib/server/sthServer.js +++ b/lib/server/sthServer.js @@ -36,7 +36,7 @@ const sthRemoveDataHandler = require(ROOT_PATH + '/lib/server/handlers/sthRemove const sthGetLogLevelHandler = require(ROOT_PATH + '/lib/server/handlers/sthGetLogLevelHandler'); const sthSetLogLevelHandler = require(ROOT_PATH + '/lib/server/handlers/sthSetLogLevelHandler'); const sthNotFoundHandler = require(ROOT_PATH + '/lib/server/handlers/sthNotFoundHandler'); -const hapi = require('hapi'); +const hapi = require('@hapi/hapi'); const joi = require('joi'); let server; @@ -57,15 +57,21 @@ function doStartServer(host, port, callback) { * @param ngsiVersion NGSI version to use. Anything different from 2 (included undefined) means v1 */ function getNgsiHandlerConfig(ngsiVersion) { - function failActionHandler(request, reply, source, error) { + function failActionHandler(request, reply, error) { + sthLogger.info('failActionHandler %j', error); + let response; // In the case of NGSIv2, this function adapts from the error response format used // by hapi to the one used in NGSIv2 - reply({ error: 'BadRequest', description: error.output.payload.message }).code(400); + response = reply + .response({ error: 'BadRequest', description: error.output.payload.message }) + .code(400) + .takeover(); + return response; } - const config = { + const options = { validate: { - headers: sthHeaderValidator, + //headers: sthHeaderValidator, query: { // prettier-ignore lastN: joi.number().integer().greater(-1).optional(), @@ -88,47 +94,19 @@ function doStartServer(host, port, callback) { }; if (ngsiVersion === 2) { - config.validate.failAction = failActionHandler; + options.validate.failAction = failActionHandler; // In NGSIv2 type query param is mandatory - config.validate.query.type = joi.string().required(); + options.validate.query.type = joi.string().required(); } - return config; + return options; } - - server = new hapi.Server(); - - server.on('log', function(event, tags) { - if (tags.load) { - sthLogger.warn(sthConfig.LOGGING_CONTEXT.SERVER_LOG, 'event=' + JSON.stringify(event)); - } - }); - - server.on('request-internal', function(request, event, tags) { - if (tags.error) { - processedRequestsWithError++; - if (tags.auth || tags.handler || tags.state || tags.payload || tags.validation) { - sthLogger.warn( - sthServerUtils.getContext(request), - request.method.toUpperCase() + ' ' + request.url.path + ', event=' + JSON.stringify(event) - ); - } else { - sthLogger.error( - sthServerUtils.getContext(request), - request.method.toUpperCase() + ' ' + request.url.path + ', event=' + JSON.stringify(event) - ); - } - } else if (tags.error === undefined && request._isReplied) { - attendedRequests++; - } - }); - if (sthConfig.corsEnabled) { sthLogger.info('CORS is enabled'); - server.connection({ - host, - port, + server = new hapi.Server({ + host: host, + port: port, routes: { cors: { origin: sthConfig.corsOptions.origin, @@ -140,24 +118,49 @@ function doStartServer(host, port, callback) { }); } else { sthLogger.info('CORS is disabled'); - server.connection({ - host, - port + server = new hapi.Server({ + host: host, + port: port }); } + server.events.on('log', function(event, tags) { + if (tags.load) { + sthLogger.warn(sthConfig.LOGGING_CONTEXT.SERVER_LOG, 'event=' + JSON.stringify(event)); + } + }); + + // server.events.on('request-internal', function(request, event, tags) { + // if (tags.error) { + // processedRequestsWithError++; + // if (tags.auth || tags.handler || tags.state || tags.payload || tags.validation) { + // sthLogger.warn( + // sthServerUtils.getContext(request), + // request.method.toUpperCase() + ' ' + request.url.path + ', event=' + JSON.stringify(event) + // ); + // } else { + // sthLogger.error( + // sthServerUtils.getContext(request), + // request.method.toUpperCase() + ' ' + request.url.path + ', event=' + JSON.stringify(event) + // ); + // } + // } else if (tags.error === undefined && request._isReplied) { + // attendedRequests++; + // } + // }); + server.route([ { method: 'GET', path: '/STH/v1/contextEntities/type/{entityType}/id/{entityId}/attributes/{attrName}', handler: sthGetDataHandler, - config: getNgsiHandlerConfig(1) + options: getNgsiHandlerConfig(1) }, { method: 'GET', path: '/STH/v2/entities/{entityId}/attrs/{attrName}', handler: sthGetDataHandlerV2, - config: getNgsiHandlerConfig(2) + options: getNgsiHandlerConfig(2) }, { method: 'GET', @@ -168,9 +171,9 @@ function doStartServer(host, port, callback) { method: 'POST', path: '/notify', handler: sthNotificationHandler, - config: { + options: { validate: { - headers: sthHeaderValidator + //headers: sthHeaderValidator // TBD } } }, @@ -178,9 +181,9 @@ function doStartServer(host, port, callback) { method: 'DELETE', path: '/STH/v1/contextEntities', handler: sthRemoveDataHandler, - config: { + options: { validate: { - headers: sthHeaderValidator + //headers: sthHeaderValidator // TBD } } }, @@ -188,9 +191,9 @@ function doStartServer(host, port, callback) { method: 'DELETE', path: '/STH/v1/contextEntities/type/{entityType}/id/{entityId}', handler: sthRemoveDataHandler, - config: { + options: { validate: { - headers: sthHeaderValidator + //headers: sthHeaderValidator // TBD } } }, @@ -198,9 +201,9 @@ function doStartServer(host, port, callback) { method: 'DELETE', path: '/STH/v1/contextEntities/type/{entityType}/id/{entityId}/attributes/{attrName}', handler: sthRemoveDataHandler, - config: { + options: { validate: { - headers: sthHeaderValidator + //headers: sthHeaderValidator // TBD } } }, @@ -208,7 +211,7 @@ function doStartServer(host, port, callback) { method: 'PUT', path: '/admin/log', handler: sthSetLogLevelHandler, - config: { + options: { validate: { query: { // prettier-ignore @@ -228,11 +231,17 @@ function doStartServer(host, port, callback) { handler: sthNotFoundHandler } ]); - // Start the server - server.start(function(err) { - return callback(err, server); - }); + server + .start() + .then(() => { + sthLogger.info('Server running on %s', server.info.uri); + return callback(null, server); + }) // if needed + .catch((err) => { + sthLogger.error('Server error %s', err); + return callback(err, null); + }); } /** @@ -257,11 +266,18 @@ function startServer(host, port, callback) { function stopServer(callback) { sthLogger.info(sthConfig.LOGGING_CONTEXT.SERVER_STOP, 'Stopping the STH server...'); if (server && server.info && server.info.started) { - server.stop(function(err) { - // Server successfully stopped - sthLogger.info(sthConfig.LOGGING_CONTEXT.SERVER_STOP, 'hapi server successfully stopped'); - return callback(err); - }); + // or use a promise + server + .stop() + .then(() => { + // Server successfully stopped + sthLogger.info(sthConfig.LOGGING_CONTEXT.SERVER_STOP, 'hapi server successfully stopped'); + return callback(null); + }) // if needed + .catch((err) => { + sthLogger.error(sthConfig.LOGGING_CONTEXT.SERVER_STOP, 'hapi server error stopped %s', err); + return callback(err); + }); } else { sthLogger.info(sthConfig.LOGGING_CONTEXT.SERVER_STOP, 'No hapi server running'); return process.nextTick(callback); diff --git a/package.json b/package.json index b871991..522650f 100644 --- a/package.json +++ b/package.json @@ -59,13 +59,13 @@ "watch": "~1.0.2" }, "dependencies": { + "@hapi/hapi": "17.9.0", "app-root-path": "~1.2.1", "async": "~2.0.0-rc.5", "boom": "7.2.2", "bytes-counter": "~1.0.0", "commander": "~2.9.0", "csv-parser": "~1.9.3", - "hapi": "17.8.5", "joi": "14.0.6", "json-csv": "1.5.0", "lodash": "~4.17.5", diff --git a/test/unit/sthGetLogLevelHandler_test.js b/test/unit/sthGetLogLevelHandler_test.js index d3b5865..f4edb8d 100644 --- a/test/unit/sthGetLogLevelHandler_test.js +++ b/test/unit/sthGetLogLevelHandler_test.js @@ -27,7 +27,7 @@ const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); const sthDatabaseNaming = require(ROOT_PATH + '/lib/database/model/sthDatabaseNaming'); const sthTestConfig = require(ROOT_PATH + '/test/unit/sthTestConfiguration'); const sthTestHelper = require(ROOT_PATH + '/test/unit/sthTestUtils.js'); -const hapi = require('hapi'); +const hapi = require('@hapi/hapi'); const expect = require('expect.js'); const request = require('request'); diff --git a/test/unit/sth_test.js b/test/unit/sth_test.js index 3729539..3f7776c 100644 --- a/test/unit/sth_test.js +++ b/test/unit/sth_test.js @@ -27,7 +27,7 @@ const sthTestConfig = require(ROOT_PATH + '/test/unit/sthTestConfiguration'); const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); const sthTestUtils = require(ROOT_PATH + '/test/unit/sthTestUtils.js'); const sthDatabaseNaming = require(ROOT_PATH + '/lib/database/model/sthDatabaseNaming'); -const hapi = require('hapi'); +const hapi = require('@hapi/hapi'); const request = require('request'); const expect = require('expect.js'); From 2c72800367c188b1a62d1568ae40cd09eb1d6f07 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 21 Dec 2021 12:00:53 +0100 Subject: [PATCH 04/16] update lint --- lib/server/sthServer.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/server/sthServer.js b/lib/server/sthServer.js index dee4b0a..6711404 100644 --- a/lib/server/sthServer.js +++ b/lib/server/sthServer.js @@ -26,8 +26,8 @@ const ROOT_PATH = require('app-root-path'); const sthLogger = require('logops'); const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); -const sthServerUtils = require(ROOT_PATH + '/lib/server/utils/sthServerUtils'); -const sthHeaderValidator = require(ROOT_PATH + '/lib/server/validators/sthHeaderValidator'); +// const sthServerUtils = require(ROOT_PATH + '/lib/server/utils/sthServerUtils'); +// const sthHeaderValidator = require(ROOT_PATH + '/lib/server/validators/sthHeaderValidator'); const sthGetDataHandler = require(ROOT_PATH + '/lib/server/handlers/sthGetDataHandler'); const sthGetDataHandlerV2 = require(ROOT_PATH + '/lib/server/handlers/sthGetDataHandlerV2'); const sthGetVersionHandler = require(ROOT_PATH + '/lib/server/handlers/sthGetVersionHandler'); @@ -59,10 +59,9 @@ function doStartServer(host, port, callback) { function getNgsiHandlerConfig(ngsiVersion) { function failActionHandler(request, reply, error) { sthLogger.info('failActionHandler %j', error); - let response; // In the case of NGSIv2, this function adapts from the error response format used // by hapi to the one used in NGSIv2 - response = reply + const response = reply .response({ error: 'BadRequest', description: error.output.payload.message }) .code(400) .takeover(); From 173cc2020bb59f3f675ff6fdbc2070ff81671065 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 21 Dec 2021 12:01:05 +0100 Subject: [PATCH 05/16] update lint --- lib/server/sthServer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/server/sthServer.js b/lib/server/sthServer.js index 6711404..a03f3ab 100644 --- a/lib/server/sthServer.js +++ b/lib/server/sthServer.js @@ -58,7 +58,7 @@ function doStartServer(host, port, callback) { */ function getNgsiHandlerConfig(ngsiVersion) { function failActionHandler(request, reply, error) { - sthLogger.info('failActionHandler %j', error); + sthLogger.debug('failActionHandler %j', error); // In the case of NGSIv2, this function adapts from the error response format used // by hapi to the one used in NGSIv2 const response = reply From 09e2803b274ad929fec07be36ed7c94a74a6fe7a Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 3 Nov 2023 10:40:02 +0100 Subject: [PATCH 06/16] fix check --- test/unit/sthGetLogLevelHandler_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/sthGetLogLevelHandler_test.js b/test/unit/sthGetLogLevelHandler_test.js index f4edb8d..7a6162a 100644 --- a/test/unit/sthGetLogLevelHandler_test.js +++ b/test/unit/sthGetLogLevelHandler_test.js @@ -58,7 +58,7 @@ describe('sthGetLogLevelHandler tests', function() { describe('server start', function() { it('should start gracefully', function(done) { sth.sthServer.startServer(sthConfig.STH_HOST, sthConfig.STH_PORT, function(err, server) { - expect(err).to.equal(undefined); + expect(err).to.equal(null); expect(server).to.be.a(hapi.Server); done(); }); From 956a0721b4410ea483f8f57dd7798ea07be35c07 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 3 Nov 2023 10:56:39 +0100 Subject: [PATCH 07/16] fix get log level test --- lib/server/handlers/sthGetLogLevelHandler.js | 2 +- test/unit/sthGetLogLevelHandler_test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/server/handlers/sthGetLogLevelHandler.js b/lib/server/handlers/sthGetLogLevelHandler.js index 35cd18d..fe09c65 100644 --- a/lib/server/handlers/sthGetLogLevelHandler.js +++ b/lib/server/handlers/sthGetLogLevelHandler.js @@ -53,7 +53,7 @@ function getLogLevelHandler(request, reply) { sthLogger.info(request.sth.context, 'Responding with logging level: ' + level); - const response = reply(getLogLevelResponse(level)); + const response = reply.response(getLogLevelResponse(level)); sthServerUtils.addFiwareCorrelator(request, response); return response; } diff --git a/test/unit/sthGetLogLevelHandler_test.js b/test/unit/sthGetLogLevelHandler_test.js index 7a6162a..e2ea323 100644 --- a/test/unit/sthGetLogLevelHandler_test.js +++ b/test/unit/sthGetLogLevelHandler_test.js @@ -59,7 +59,7 @@ describe('sthGetLogLevelHandler tests', function() { it('should start gracefully', function(done) { sth.sthServer.startServer(sthConfig.STH_HOST, sthConfig.STH_PORT, function(err, server) { expect(err).to.equal(null); - expect(server).to.be.a(hapi.Server); + expect(server).to.not.equal(null); done(); }); }); From c851c9dcfa69b12dd7f78a462b8528c7527f31bc Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 3 Nov 2023 10:58:20 +0100 Subject: [PATCH 08/16] remove unnecessary require --- test/unit/sthGetLogLevelHandler_test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/sthGetLogLevelHandler_test.js b/test/unit/sthGetLogLevelHandler_test.js index e2ea323..02334e2 100644 --- a/test/unit/sthGetLogLevelHandler_test.js +++ b/test/unit/sthGetLogLevelHandler_test.js @@ -27,7 +27,6 @@ const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); const sthDatabaseNaming = require(ROOT_PATH + '/lib/database/model/sthDatabaseNaming'); const sthTestConfig = require(ROOT_PATH + '/test/unit/sthTestConfiguration'); const sthTestHelper = require(ROOT_PATH + '/test/unit/sthTestUtils.js'); -const hapi = require('@hapi/hapi'); const expect = require('expect.js'); const request = require('request'); From 87e9d7042fcd3ae8b7f1412e25dd1cf7403c9f34 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 3 Nov 2023 11:05:03 +0100 Subject: [PATCH 09/16] fix check server --- test/unit/sth_test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/sth_test.js b/test/unit/sth_test.js index 3f7776c..92dc0ce 100644 --- a/test/unit/sth_test.js +++ b/test/unit/sth_test.js @@ -27,7 +27,6 @@ const sthTestConfig = require(ROOT_PATH + '/test/unit/sthTestConfiguration'); const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); const sthTestUtils = require(ROOT_PATH + '/test/unit/sthTestUtils.js'); const sthDatabaseNaming = require(ROOT_PATH + '/lib/database/model/sthDatabaseNaming'); -const hapi = require('@hapi/hapi'); const request = require('request'); const expect = require('expect.js'); @@ -62,7 +61,7 @@ describe('sth tests', function() { it('should start gracefully', function(done) { sth.sthServer.startServer(sthConfig.STH_HOST, sthConfig.STH_PORT, function(err, server) { expect(err).to.equal(null); - expect(server).to.be.a(hapi.Server); + expect(server).to.not.equal(null); done(); }); }); From 7edb90634ce941ac3a65743c32954fd00e85ddd2 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 3 Nov 2023 11:34:02 +0100 Subject: [PATCH 10/16] use reply.response instead of reply --- lib/server/handlers/sthGetDataHandlerV2.js | 12 +++++++----- lib/server/handlers/sthNotificationHandler.js | 6 +++--- lib/server/handlers/sthRemoveDataHandler.js | 8 ++++---- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/server/handlers/sthGetDataHandlerV2.js b/lib/server/handlers/sthGetDataHandlerV2.js index 0a5dbca..163e316 100644 --- a/lib/server/handlers/sthGetDataHandlerV2.js +++ b/lib/server/handlers/sthGetDataHandlerV2.js @@ -42,12 +42,14 @@ function getDataHandler(request, reply) { */ function replyHandler(payload) { if (payload.isBoom) { - return reply({ - error: payload.output.payload.error.replace(/ /, ''), - description: payload.output.payload.message - }).code(payload.output.payload.statusCode); + return reply + .response({ + error: payload.output.payload.error.replace(/ /, ''), + description: payload.output.payload.message + }) + .code(payload.output.payload.statusCode); } - return reply(payload); + return reply.response(payload); } request.params.entityType = request.query.type; diff --git a/lib/server/handlers/sthNotificationHandler.js b/lib/server/handlers/sthNotificationHandler.js index 1e6dac3..48d976e 100644 --- a/lib/server/handlers/sthNotificationHandler.js +++ b/lib/server/handlers/sthNotificationHandler.js @@ -163,7 +163,7 @@ function storeRawData(data, reply, callback) { // There was an error when getting the collection sthLogger.error(request.sth.context, 'Error when getting the raw data collection for storing:' + err); if (++counterObj.counter === totalTasks) { - response = reply(err); + response = reply.response(err); sthServerUtils.addFiwareCorrelator(request, response); } process.nextTick(callback.bind(null, err)); @@ -334,13 +334,13 @@ function processAttribute(data, reply) { err = null; } if (++data.counterObj.counter === data.totalTasks) { - reply(err); + reply.response(err); } } else if (isAggregatableValue) { // Store the aggregated data into the database storeAggregatedData(data, reply); } else if (++data.counterObj.counter === data.totalTasks) { - reply(); + reply.response(); } }); } else { diff --git a/lib/server/handlers/sthRemoveDataHandler.js b/lib/server/handlers/sthRemoveDataHandler.js index f1256d3..31c403e 100644 --- a/lib/server/handlers/sthRemoveDataHandler.js +++ b/lib/server/handlers/sthRemoveDataHandler.js @@ -74,7 +74,7 @@ function removeDataHandler(request, reply) { sthLogger.debug(request.sth.context, 'Responding with 500- Internal error'); const error = boom.internal(message); error.output.payload.message = message; - reply(error); + reply.response(error); } else if ( err.name === 'MongoError' && err.message.includes('does not exist. Currently in strict mode') @@ -85,19 +85,19 @@ function removeDataHandler(request, reply) { 'No data associated to the provided ' + getRequestDescription(request) + ' available' ); // Reply with no error - response = reply(); + response = reply.response(); response.code(204); } else { sthLogger.warn(request.sth.context, 'Error when removing the data associated to an entity: ' + err); // Reply with error - response = reply(err); + response = reply.response(err); } } else { sthLogger.debug( request.sth.context, 'Data associated to the provided ' + getRequestDescription(request) + ' successfully removed' ); - response = reply(); + response = reply.response(); response.code(204); } sthServerUtils.addFiwareCorrelator(request, response); From 0005a264aa2a9aa3c2ec3c87067e7f4b6dabefcd Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 3 Nov 2023 12:52:06 +0100 Subject: [PATCH 11/16] add replyHandler --- lib/server/handlers/sthGetDataHandler.js | 182 +++++++++++++++------ lib/server/handlers/sthGetDataHandlerV2.js | 2 +- 2 files changed, 134 insertions(+), 50 deletions(-) diff --git a/lib/server/handlers/sthGetDataHandler.js b/lib/server/handlers/sthGetDataHandler.js index 7b331d3..69a1383 100644 --- a/lib/server/handlers/sthGetDataHandler.js +++ b/lib/server/handlers/sthGetDataHandler.js @@ -39,7 +39,7 @@ const encodeRFC5987 = require('rfc5987-value-chars').encode; * @param {Object} request The request * @param {Function} reply Hapi's reply function */ -function getRawData(request, reply) { +function getRawData(request, reply, replyHandler) { let response; var query = { @@ -71,7 +71,11 @@ function getRawData(request, reply) { sthLogger.debug(request.sth.context, 'Responding with 500- Internal error'); const error = boom.internal(message); error.output.payload.message = message; - reply(error); + if (reply) { + reply.response(error); + } else { + replyHandler(error); + } } else { // The collection does not exist, reply with en empty response sthLogger.warn( @@ -90,7 +94,11 @@ function getRawData(request, reply) { request.params.attrName, emptyResponse ); - response = reply(ngsiPayload); + if (reply) { + response = reply.response(ngsiPayload); + } else { + response = replyHandler(ngsiPayload); + } sthServerUtils.addFiwareCorrelator(request, response); if (request.query.count) { sthServerUtils.addFiwareTotalCount(0, response); @@ -116,7 +124,11 @@ function getRawData(request, reply) { // Error when getting the raw data sthLogger.error(request.sth.context, 'Error %j when getting raw data with query %j', err, rawQuery); sthLogger.debug(request.sth.context, 'Responding with 500 - Internal Error'); - response = reply(err); + if (reply) { + response = reply.response(err); + } else { + response = replyHandler(err); + } } else if (!result || !(result.length || result instanceof stream)) { // No raw data available for the request sthLogger.info( @@ -127,22 +139,38 @@ function getRawData(request, reply) { ); sthLogger.debug(request.sth.context, 'Responding with no points'); - response = reply( - sthServerUtils.getNGSIPayload( - request.params.version, - request.params.entityId, - request.params.entityType, - request.params.attrName, - sthServerUtils.getEmptyResponse() - ) - ); + if (reply) { + response = reply.response( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + sthServerUtils.getEmptyResponse() + ) + ); + } else { + response = replyHandler( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + sthServerUtils.getEmptyResponse() + ) + ); + } } else if (result instanceof stream) { sthLogger.debug(request.sth.context, 'Responding with a stream of docs'); response = reply(new stream.Readable().wrap(result)); } else if (typeof result === 'string') { sthLogger.debug(request.sth.context, "Responding with file '" + result + "'"); fs.readFile(result, function(err, data) { - response = reply(data); + if (reply) { + response = reply.response(data); + } else { + response = replyHandler(data); + } const fileName = result.substring(result.lastIndexOf(path.sep) + 1); response.header( 'Content-Disposition', @@ -164,15 +192,27 @@ function getRawData(request, reply) { }); // readfile } else { sthLogger.debug(request.sth.context, 'Responding with %s docs', result.length); - response = reply( - sthServerUtils.getNGSIPayload( - request.params.version, - request.params.entityId, - request.params.entityType, - request.params.attrName, - result - ) - ); + if (reply) { + response = reply.response( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + result + ) + ); + } else { + response = replyHandler( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + result + ) + ); + } } sthServerUtils.addFiwareCorrelator(request, response); if (request.query.count) { @@ -221,7 +261,11 @@ function getAggregatedData(request, reply) { sthLogger.debug(request.sth.context, 'Responding with 500- Internal error'); const error = boom.internal(message); error.output.payload.message = message; - reply(error); + if (reply) { + reply.response(error); + } else { + replyHandler(error); + } } else { // The collection does not exist, reply with en empty response sthLogger.warn( @@ -243,7 +287,11 @@ function getAggregatedData(request, reply) { request.params.attrName, emptyResponse ); - response = reply(ngsiPayload); + if (reply) { + response = reply.response(ngsiPayload); + } else { + response = replyHandler(ngsiPayload); + } sthServerUtils.addFiwareCorrelator(request, response); } } else { @@ -274,7 +322,11 @@ function getAggregatedData(request, reply) { aggregatedQuery ); sthLogger.debug(request.sth.context, 'Responding with 500 - Internal Error'); - response = reply(err); + if (reply) { + response = reply.response(err); + } else { + response = replyHandler(err); + } } else if (!result || !result.length) { // No aggregated data available for the request sthLogger.debug( @@ -285,26 +337,50 @@ function getAggregatedData(request, reply) { ); sthLogger.debug(request.sth.context, 'Responding with no points'); - response = reply( - sthServerUtils.getNGSIPayload( - request.params.version, - request.params.entityId, - request.params.entityType, - request.params.attrName, - sthServerUtils.getEmptyResponse() - ) - ); + if (reply) { + response = reply.response( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + sthServerUtils.getEmptyResponse() + ) + ); + } else { + response = replyHandler( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + sthServerUtils.getEmptyResponse() + ) + ); + } } else { sthLogger.debug(request.sth.context, 'Responding with %s docs', result.length); - response = reply( - sthServerUtils.getNGSIPayload( - request.params.version, - request.params.entityId, - request.params.entityType, - request.params.attrName, - result - ) - ); + if (reply) { + response = reply.response( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + result + ) + ); + } else { + response = replyHandler( + sthServerUtils.getNGSIPayload( + request.params.version, + request.params.entityId, + request.params.entityType, + request.params.attrName, + result + ) + ); + } } sthServerUtils.addFiwareCorrelator(request, response); }); @@ -317,7 +393,7 @@ function getAggregatedData(request, reply) { * @param {Object} request The request * @param {Function} reply The reply() function of the hapi server */ -function getDataHandler(request, reply) { +function getDataHandler(request, reply, replyHandler) { request.sth = request.sth || {}; request.sth.context = sthServerUtils.getContext(request); let message; @@ -349,13 +425,17 @@ function getDataHandler(request, reply) { source: 'query', keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count'] }; - return reply(error); + if (reply) { + return reply.response(error); + } else { + return replyHandler(error); + } } } - getRawData(request, reply); + getRawData(request, reply, replyHandler); } else if (request.query.aggrMethod && request.query.aggrPeriod) { // Aggregated data is requested - getAggregatedData(request, reply); + getAggregatedData(request, reply, replyHandler); } else { message = 'A combination of the following query params is required: lastN, hLimit and hOffset, ' + @@ -369,7 +449,11 @@ function getDataHandler(request, reply) { source: 'query', keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count'] }; - return reply(error); + if (reply) { + return reply.response(error); + } else { + return replyHandler(error); + } } } diff --git a/lib/server/handlers/sthGetDataHandlerV2.js b/lib/server/handlers/sthGetDataHandlerV2.js index 163e316..3990c20 100644 --- a/lib/server/handlers/sthGetDataHandlerV2.js +++ b/lib/server/handlers/sthGetDataHandlerV2.js @@ -55,7 +55,7 @@ function getDataHandler(request, reply) { request.params.entityType = request.query.type; request.params.version = 2; - sthGetDataHandlerV1(request, replyHandler); + sthGetDataHandlerV1(request, null, replyHandler); } module.exports = getDataHandler; From ffd26d85048b519fdc564e72d27809b8142bf501 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 3 Nov 2023 12:55:39 +0100 Subject: [PATCH 12/16] fix replyHandler --- lib/server/handlers/sthGetDataHandler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/server/handlers/sthGetDataHandler.js b/lib/server/handlers/sthGetDataHandler.js index 69a1383..cfa56f1 100644 --- a/lib/server/handlers/sthGetDataHandler.js +++ b/lib/server/handlers/sthGetDataHandler.js @@ -229,7 +229,7 @@ function getRawData(request, reply, replyHandler) { * @param {Object} request The request * @param {Function} reply Hapi's reply function */ -function getAggregatedData(request, reply) { +function getAggregatedData(request, reply, replyHandler) { let response; var query = { From d0601a9c630927f33d3967c88e9d0fb23864654a Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 3 Nov 2023 13:04:32 +0100 Subject: [PATCH 13/16] fix return response --- lib/server/handlers/sthGetDataHandler.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/server/handlers/sthGetDataHandler.js b/lib/server/handlers/sthGetDataHandler.js index cfa56f1..472cfcb 100644 --- a/lib/server/handlers/sthGetDataHandler.js +++ b/lib/server/handlers/sthGetDataHandler.js @@ -72,9 +72,9 @@ function getRawData(request, reply, replyHandler) { const error = boom.internal(message); error.output.payload.message = message; if (reply) { - reply.response(error); + response = reply.response(error); } else { - replyHandler(error); + response = replyHandler(error); } } else { // The collection does not exist, reply with en empty response @@ -219,8 +219,10 @@ function getRawData(request, reply, replyHandler) { sthLogger.debug(request.sth.context, 'totalCount %j', totalCount); sthServerUtils.addFiwareTotalCount(totalCount, response); } + return response; }); } + return response; }); } @@ -262,9 +264,9 @@ function getAggregatedData(request, reply, replyHandler) { const error = boom.internal(message); error.output.payload.message = message; if (reply) { - reply.response(error); + response = reply.response(error); } else { - replyHandler(error); + response = replyHandler(error); } } else { // The collection does not exist, reply with en empty response @@ -383,8 +385,10 @@ function getAggregatedData(request, reply, replyHandler) { } } sthServerUtils.addFiwareCorrelator(request, response); + return response; }); } + return response; }); } From 85b323a3a38d82c6ed3fed224658e2edfe397192 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Fri, 3 Nov 2023 13:19:51 +0100 Subject: [PATCH 14/16] add missed return --- lib/server/handlers/sthGetDataHandler.js | 4 ++-- lib/server/handlers/sthGetDataHandlerV2.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/server/handlers/sthGetDataHandler.js b/lib/server/handlers/sthGetDataHandler.js index 472cfcb..7c82bfa 100644 --- a/lib/server/handlers/sthGetDataHandler.js +++ b/lib/server/handlers/sthGetDataHandler.js @@ -436,10 +436,10 @@ function getDataHandler(request, reply, replyHandler) { } } } - getRawData(request, reply, replyHandler); + return getRawData(request, reply, replyHandler); } else if (request.query.aggrMethod && request.query.aggrPeriod) { // Aggregated data is requested - getAggregatedData(request, reply, replyHandler); + return getAggregatedData(request, reply, replyHandler); } else { message = 'A combination of the following query params is required: lastN, hLimit and hOffset, ' + diff --git a/lib/server/handlers/sthGetDataHandlerV2.js b/lib/server/handlers/sthGetDataHandlerV2.js index 3990c20..dbc5d42 100644 --- a/lib/server/handlers/sthGetDataHandlerV2.js +++ b/lib/server/handlers/sthGetDataHandlerV2.js @@ -55,7 +55,7 @@ function getDataHandler(request, reply) { request.params.entityType = request.query.type; request.params.version = 2; - sthGetDataHandlerV1(request, null, replyHandler); + return sthGetDataHandlerV1(request, null, replyHandler); } module.exports = getDataHandler; From 7df343ee5f74520e864eb461d82c19a3b49ff250 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 4 Dec 2023 16:21:32 +0100 Subject: [PATCH 15/16] fix return response always --- lib/server/handlers/sthGetDataHandler.js | 10 ++++++++-- lib/server/handlers/sthGetDataHandlerV2.js | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/server/handlers/sthGetDataHandler.js b/lib/server/handlers/sthGetDataHandler.js index 7c82bfa..42e2c55 100644 --- a/lib/server/handlers/sthGetDataHandler.js +++ b/lib/server/handlers/sthGetDataHandler.js @@ -72,7 +72,7 @@ function getRawData(request, reply, replyHandler) { const error = boom.internal(message); error.output.payload.message = message; if (reply) { - response = reply.response(error); + response = reply.response(message); } else { response = replyHandler(error); } @@ -162,7 +162,11 @@ function getRawData(request, reply, replyHandler) { } } else if (result instanceof stream) { sthLogger.debug(request.sth.context, 'Responding with a stream of docs'); - response = reply(new stream.Readable().wrap(result)); + if (reply) { + response = reply.response(new stream.Readable().wrap(result)); + } else { + response = replyHandler(new stream.Readable().wrap(result)); + } } else if (typeof result === 'string') { sthLogger.debug(request.sth.context, "Responding with file '" + result + "'"); fs.readFile(result, function(err, data) { @@ -224,6 +228,7 @@ function getRawData(request, reply, replyHandler) { } return response; }); + return response; } /** @@ -390,6 +395,7 @@ function getAggregatedData(request, reply, replyHandler) { } return response; }); + return response; } /** diff --git a/lib/server/handlers/sthGetDataHandlerV2.js b/lib/server/handlers/sthGetDataHandlerV2.js index dbc5d42..068ef6d 100644 --- a/lib/server/handlers/sthGetDataHandlerV2.js +++ b/lib/server/handlers/sthGetDataHandlerV2.js @@ -55,7 +55,7 @@ function getDataHandler(request, reply) { request.params.entityType = request.query.type; request.params.version = 2; - return sthGetDataHandlerV1(request, null, replyHandler); + return sthGetDataHandlerV1.getDataHandler(request, null, replyHandler); } module.exports = getDataHandler; From 3684902769b539eb494295c618b0073294a3b620 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Tue, 5 Dec 2023 16:02:37 +0100 Subject: [PATCH 16/16] upgrade boom --- lib/database/sthDatabase.js | 2 +- lib/server/handlers/sthGetDataHandler.js | 57 +++++++++++++------ lib/server/handlers/sthGetDataHandlerV2.js | 2 +- lib/server/handlers/sthNotificationHandler.js | 2 +- lib/server/handlers/sthRemoveDataHandler.js | 2 +- lib/server/sthServer.js | 20 +++---- lib/server/validators/sthHeaderValidator.js | 2 +- package.json | 6 +- 8 files changed, 58 insertions(+), 35 deletions(-) diff --git a/lib/database/sthDatabase.js b/lib/database/sthDatabase.js index 196835b..18db19d 100644 --- a/lib/database/sthDatabase.js +++ b/lib/database/sthDatabase.js @@ -31,7 +31,7 @@ const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration.js'); const sthUtils = require(ROOT_PATH + '/lib/utils/sthUtils.js'); const sthDatabaseNaming = require(ROOT_PATH + '/lib/database/model/sthDatabaseNaming'); const mongoClient = require('mongodb').MongoClient; -const boom = require('boom'); +const boom = require('@hapi/boom'); const jsoncsv = require('json-csv'); const fs = require('fs'); const path = require('path'); diff --git a/lib/server/handlers/sthGetDataHandler.js b/lib/server/handlers/sthGetDataHandler.js index 42e2c55..99e258f 100644 --- a/lib/server/handlers/sthGetDataHandler.js +++ b/lib/server/handlers/sthGetDataHandler.js @@ -28,7 +28,7 @@ const sthLogger = require('logops'); const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); const sthServerUtils = require(ROOT_PATH + '/lib/server/utils/sthServerUtils'); const sthDatabase = require(ROOT_PATH + '/lib/database/sthDatabase'); -const boom = require('boom'); +const boom = require('@hapi/boom'); const stream = require('stream'); const fs = require('fs'); const path = require('path'); @@ -70,9 +70,13 @@ function getRawData(request, reply, replyHandler) { ); sthLogger.debug(request.sth.context, 'Responding with 500- Internal error'); const error = boom.internal(message); - error.output.payload.message = message; + //error.output.payload.message = message; if (reply) { - response = reply.response(message); + //response = reply.response({error:error.output.peyload}).type('application/json').code(500); + response = reply + .response(error) + .type('application/json') + .code(500); } else { response = replyHandler(error); } @@ -125,7 +129,10 @@ function getRawData(request, reply, replyHandler) { sthLogger.error(request.sth.context, 'Error %j when getting raw data with query %j', err, rawQuery); sthLogger.debug(request.sth.context, 'Responding with 500 - Internal Error'); if (reply) { - response = reply.response(err); + response = reply + .response(err) + .type('application/json') + .code(500); } else { response = replyHandler(err); } @@ -267,9 +274,13 @@ function getAggregatedData(request, reply, replyHandler) { ); sthLogger.debug(request.sth.context, 'Responding with 500- Internal error'); const error = boom.internal(message); - error.output.payload.message = message; + //error.output.payload.message = message; if (reply) { - response = reply.response(error); + response = reply + .response({ error: error.output.peyload }) + .type('application/json') + .code(500); + //response = reply.response(error); } else { response = replyHandler(error); } @@ -330,7 +341,10 @@ function getAggregatedData(request, reply, replyHandler) { ); sthLogger.debug(request.sth.context, 'Responding with 500 - Internal Error'); if (reply) { - response = reply.response(err); + response = reply + .response(err) + .type('application/json') + .code(500); } else { response = replyHandler(err); } @@ -431,12 +445,16 @@ function getDataHandler(request, reply, replyHandler) { request.method.toUpperCase() + ' ' + request.url.path + ', error=' + message ); error = boom.badRequest(message); - error.output.payload.validation = { - source: 'query', - keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count'] - }; + // error.output.payload.validation = { + // source: 'query', + // keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count'] + // }; if (reply) { - return reply.response(error); + return reply + .response({ error: error.output.peyload }) + .type('application/json') + .code(400); + //return reply.response(error); } else { return replyHandler(error); } @@ -455,16 +473,21 @@ function getDataHandler(request, reply, replyHandler) { request.method.toUpperCase() + ' ' + request.url.path + ', error=' + message ); error = boom.badRequest(message); - error.output.payload.validation = { - source: 'query', - keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count'] - }; + // error.output.payload.validation = { + // source: 'query', + // keys: ['lastN', 'hLimit', 'hOffset', 'filetype', 'aggrMethod', 'aggrPeriod', 'count'] + // }; if (reply) { - return reply.response(error); + //return reply.response(error); + return reply + .response({ error: error.output.peyload }) + .type('application/json') + .code(400); } else { return replyHandler(error); } } + return null; } module.exports = getDataHandler; diff --git a/lib/server/handlers/sthGetDataHandlerV2.js b/lib/server/handlers/sthGetDataHandlerV2.js index 068ef6d..dbc5d42 100644 --- a/lib/server/handlers/sthGetDataHandlerV2.js +++ b/lib/server/handlers/sthGetDataHandlerV2.js @@ -55,7 +55,7 @@ function getDataHandler(request, reply) { request.params.entityType = request.query.type; request.params.version = 2; - return sthGetDataHandlerV1.getDataHandler(request, null, replyHandler); + return sthGetDataHandlerV1(request, null, replyHandler); } module.exports = getDataHandler; diff --git a/lib/server/handlers/sthNotificationHandler.js b/lib/server/handlers/sthNotificationHandler.js index 48d976e..40c4eb6 100644 --- a/lib/server/handlers/sthNotificationHandler.js +++ b/lib/server/handlers/sthNotificationHandler.js @@ -28,7 +28,7 @@ const sthLogger = require('logops'); const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); const sthServerUtils = require(ROOT_PATH + '/lib/server/utils/sthServerUtils'); const sthDatabase = require(ROOT_PATH + '/lib/database/sthDatabase'); -const boom = require('boom'); +const boom = require('@hapi/boom'); /** * Returns the total number of attributes to be processed diff --git a/lib/server/handlers/sthRemoveDataHandler.js b/lib/server/handlers/sthRemoveDataHandler.js index 31c403e..6352f15 100644 --- a/lib/server/handlers/sthRemoveDataHandler.js +++ b/lib/server/handlers/sthRemoveDataHandler.js @@ -26,7 +26,7 @@ const sthLogger = require('logops'); const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); const sthServerUtils = require(ROOT_PATH + '/lib/server/utils/sthServerUtils'); const sthDatabase = require(ROOT_PATH + '/lib/database/sthDatabase'); -const boom = require('boom'); +const boom = require('@hapi/boom'); /** * Returns a textual description of the received request and the data included in it diff --git a/lib/server/sthServer.js b/lib/server/sthServer.js index a03f3ab..ab3a457 100644 --- a/lib/server/sthServer.js +++ b/lib/server/sthServer.js @@ -27,7 +27,7 @@ const ROOT_PATH = require('app-root-path'); const sthLogger = require('logops'); const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); // const sthServerUtils = require(ROOT_PATH + '/lib/server/utils/sthServerUtils'); -// const sthHeaderValidator = require(ROOT_PATH + '/lib/server/validators/sthHeaderValidator'); +const sthHeaderValidator = require(ROOT_PATH + '/lib/server/validators/sthHeaderValidator'); const sthGetDataHandler = require(ROOT_PATH + '/lib/server/handlers/sthGetDataHandler'); const sthGetDataHandlerV2 = require(ROOT_PATH + '/lib/server/handlers/sthGetDataHandlerV2'); const sthGetVersionHandler = require(ROOT_PATH + '/lib/server/handlers/sthGetVersionHandler'); @@ -71,7 +71,7 @@ function doStartServer(host, port, callback) { const options = { validate: { //headers: sthHeaderValidator, - query: { + query: joi.object({ // prettier-ignore lastN: joi.number().integer().greater(-1).optional(), // prettier-ignore @@ -88,7 +88,7 @@ function doStartServer(host, port, callback) { dateTo: joi.date().optional(), filetype: joi.string().optional(), count: joi.boolean().optional() - } + }) } }; @@ -172,7 +172,7 @@ function doStartServer(host, port, callback) { handler: sthNotificationHandler, options: { validate: { - //headers: sthHeaderValidator // TBD + headers: sthHeaderValidator // TBD } } }, @@ -182,7 +182,7 @@ function doStartServer(host, port, callback) { handler: sthRemoveDataHandler, options: { validate: { - //headers: sthHeaderValidator // TBD + headers: sthHeaderValidator // TBD } } }, @@ -192,7 +192,7 @@ function doStartServer(host, port, callback) { handler: sthRemoveDataHandler, options: { validate: { - //headers: sthHeaderValidator // TBD + headers: sthHeaderValidator // TBD } } }, @@ -202,7 +202,7 @@ function doStartServer(host, port, callback) { handler: sthRemoveDataHandler, options: { validate: { - //headers: sthHeaderValidator // TBD + headers: sthHeaderValidator // TBD } } }, @@ -212,10 +212,10 @@ function doStartServer(host, port, callback) { handler: sthSetLogLevelHandler, options: { validate: { - query: { + query: joi.object({ // prettier-ignore level: joi.string().insensitive().valid('FATAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG').required() - } + }) } } }, @@ -226,7 +226,7 @@ function doStartServer(host, port, callback) { }, { method: '*', - path: '/{p*}', + path: '/{any*}', handler: sthNotFoundHandler } ]); diff --git a/lib/server/validators/sthHeaderValidator.js b/lib/server/validators/sthHeaderValidator.js index d875bb5..445d76c 100644 --- a/lib/server/validators/sthHeaderValidator.js +++ b/lib/server/validators/sthHeaderValidator.js @@ -24,7 +24,7 @@ const ROOT_PATH = require('app-root-path'); const sthLogger = require('logops'); const sthConfig = require(ROOT_PATH + '/lib/configuration/sthConfiguration'); -const boom = require('boom'); +const boom = require('@hapi/boom'); /** * Header validation handler diff --git a/package.json b/package.json index a5a5e90..e972905 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "proxyquire": "1.7.10", "remark-cli": "~8.0.1", "remark-preset-lint-recommended": "~4.0.1", - "request": "2.88.0", + "request": "2.88.2", "textlint": "~11.7.6", "textlint-filter-rule-comments": "~1.2.2", "textlint-rule-common-misspellings": "~1.0.1", @@ -59,10 +59,10 @@ "watch": "~1.0.2" }, "dependencies": { - "@hapi/hapi": "17.9.0", + "@hapi/hapi": "21.3.2", "app-root-path": "~1.2.1", "async": "~2.0.0-rc.5", - "boom": "7.2.2", + "@hapi/boom": "10.0.1", "bytes-counter": "~1.0.0", "commander": "~2.9.0", "csv-parser": "~1.9.3",