From: Justin Wind Date: Thu, 20 Jul 2023 22:27:14 +0000 (-0700) Subject: removed deprecated logger-related functions, default to console if no logger provided X-Git-Tag: v2.0.0^2~9 X-Git-Url: http://git.squeep.com/?p=squeep-api-dingus;a=commitdiff_plain;h=914e78e62c740792ed7f6de319360ea4aa60c67e removed deprecated logger-related functions, default to console if no logger provided --- diff --git a/CHANGELOG.md b/CHANGELOG.md index ffd23ff..7585cff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Releases and notable changes to this project are documented here. ## [Unreleased] -- +- removed deprecated functions ## [v1.2.10] - 2023-07-20 diff --git a/lib/common.js b/lib/common.js index 723f922..f839e7a 100644 --- a/lib/common.js +++ b/lib/common.js @@ -55,18 +55,6 @@ const generateETag = (_filePath, fileStat, fileData) => { */ const get = (obj, prop, def) => obj && prop && (prop in obj) ? obj[prop] : def; -/** - * @param {http.ClientRequest} req - * @param {http.ServerResponse} res - * @param {Object} ctx - * @deprecated after v1.2.5 (integrated into logger module) - */ -const handlerLogData = (req, res, ctx) => ({ - req: requestLogData(req), - res: responseLogData(res), - ctx, -}); - /** * Determine whether a client has already requested a resource, * based on If-Modified-Since and If-None-Match headers. @@ -171,71 +159,6 @@ const pick = (obj, props) => { return picked; }; -/** - * Return a subset of a request object, suitable for logging. - * Obscures sensitive header values. - * @param {http.ClientRequest} req - * @deprecated after v1.2.5 (integrated into logger module) - */ -const requestLogData = (req) => { - const data = pick(req, [ - 'method', - 'url', - 'httpVersion', - 'headers', - 'trailers', - ]); - scrubHeaderObject(data); - return data; -}; - - -/** - * Remove sensitive header data. - * @param {Object} data - * @param {Object} data.headers - * @deprecated after v1.2.5 (integrated into logger module) - */ -const scrubHeaderObject = (data) => { - if (data?.headers && 'authorization' in data.headers) { - data.headers = Object.assign({}, data.headers, { - authorization: obscureAuthorizationHeader(data.headers['authorization']), - }); - } -}; - - -/** - * Hide sensitive part of an Authorization header. - * @param {String} authHeader - * @returns {String} - * @deprecated after v1.2.5 (integrated into logger module) - */ -const obscureAuthorizationHeader = (authHeader) => { - if (!authHeader) { - return authHeader; - } - const space = authHeader.indexOf(' '); - // This blurs entire string if no space found, because -1. - return authHeader.slice(0, space + 1) + '*'.repeat(authHeader.length - (space + 1)); -}; - - -/** - * Return a subset of a response object, suitable for logging. - * @param {http.ServerResponse} res - * @deprecated after v1.2.5 (integrated into logger module) - */ -const responseLogData = (res) => { - const response = pick(res, [ - 'statusCode', - 'statusMessage', - ]); - response.headers = res.getHeaders(); - return response; -}; - - /** * Store all properties in defaultOptions on target from either options or defaultOptions. * @param {Object} target @@ -269,37 +192,6 @@ const requestId = () => { return uuid.v1(); }; -/** - * Do nothing. - */ -const nop = () => { /**/ }; - -/** - * A logger object which does nothing. - */ -const nullLogger = { - error: nop, - warn: nop, - info: nop, - log: nop, - debug: nop, -}; - -/** - * Populates any absent logger level functions on a logger object. - * @param {Object} logger - * @returns {Object} - * @deprecated after v1.2.9 (this is not our responsibility) - */ -const ensureLoggerLevels = (logger = {}) => { - for (const level in nullLogger) { - if (! (level in logger)) { - logger[level] = nullLogger[level]; - } - } - return logger; -}; - /** * Merges folded header lines * @param {String[]} lines @@ -322,23 +214,15 @@ const unfoldHeaderLines = (lines) => { }; module.exports = { - ensureLoggerLevels, fileScope, generateETag, get, - handlerLogData, httpStatusCodeClass, isClientCached, mergeDeep, mergeEnum, - nop, - nullLogger, - obscureAuthorizationHeader, pick, requestId, - requestLogData, - responseLogData, - scrubHeaderObject, setOptions, splitFirst, unfoldHeaderLines, diff --git a/lib/dingus.js b/lib/dingus.js index d8eed91..0057685 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -44,7 +44,7 @@ class Dingus { * @param {Boolean} options.trustProxy trust some header data to be provided by proxy * @param {Object} options.querystring alternate qs parser to use */ - constructor(logger = common.nullLogger, options = {}) { + constructor(logger = console, options = {}) { common.setOptions(this, defaultOptions, options); this.router = new Router(options); diff --git a/test/lib/common.js b/test/lib/common.js index 57c4113..15e9c4a 100644 --- a/test/lib/common.js +++ b/test/lib/common.js @@ -128,100 +128,6 @@ describe('common', function () { }); }); // pick - describe('requestLogData', function () { - it('gives data', function () { - const req = { - method: 'GET', - somethingElse: 'blah', - }; - const result = common.requestLogData(req); - assert.deepStrictEqual(result, { - method: 'GET', - }); - }); - }); // requestLogData - - describe('obscureAuthorizationHeader', function () { - it('obscures basic data', function () { - const authHeader = 'Basic Zm9vOmJhcg=='; - const expected = 'Basic ************'; - const result = common.obscureAuthorizationHeader(authHeader); - assert.strictEqual(result, expected); - }); - it('obscures all of other types', function () { - const authHeader = 'someWeirdAuth'; - const expected = '*************'; - const result = common.obscureAuthorizationHeader(authHeader); - assert.strictEqual(result, expected); - }); - it('does nothing when empty', function () { - const authHeader = undefined; - const expected = undefined; - const result = common.obscureAuthorizationHeader(authHeader); - assert.strictEqual(result, expected); - }); - }); // obscureAuthorizationHeader - - describe('scrubHeaderObject', function () { - it('', function () { - const data = { - headers: { - 'foo': 'bar', - 'authorization': 'Basic Zm9vOmJhcg==', - }, - }; - const expected = { - headers: { - 'foo': 'bar', - 'authorization': 'Basic ************', - }, - }; - common.scrubHeaderObject(data); - assert.deepStrictEqual(data, expected); - }); - }); // scrubHeaderObject - - describe('responseLogData', function () { - it('gives data', function () { - const res = { - getHeaders: () => ({}), - statusCode: 200, - blah: 'blah', - }; - const result = common.responseLogData(res); - assert.deepStrictEqual(result, { - headers: {}, - statusCode: 200, - }); - }); - }); // responseLogData - - describe('handlerLogData', function () { - it('covers', function () { - const req = { - method: 'GET', - somethingElse: 'blah', - }; - const res = { - getHeaders: () => ({}), - statusCode: 200, - blah: 'blah', - }; - const ctx = {}; - const result = common.handlerLogData(req, res, ctx); - assert.deepStrictEqual(result, { - req: { - method: 'GET', - }, - res: { - headers: {}, - statusCode: 200, - }, - ctx: {}, - }); - }); - }); // handlerLogData - describe('setOptions', function () { it('sets options', function () { const expected = { @@ -324,13 +230,6 @@ describe('common', function () { }); }); // requestId - describe('ensureLoggerLevels', function () { - it('adds missing levels', function () { - const result = common.ensureLoggerLevels(); - assert.deepStrictEqual(result, common.nullLogger); - }); - }); // ensureLoggerLevels - describe('httpStatusCodeClass', function () { it('works', function () { for (const [statusCode, statusClassExpected] of Object.entries({ diff --git a/test/lib/dingus.js b/test/lib/dingus.js index 12f40e8..b60caca 100644 --- a/test/lib/dingus.js +++ b/test/lib/dingus.js @@ -12,10 +12,15 @@ const Enum = require('../../lib/enum'); const noExpectedException = 'did not get expected exception'; +const noLogger = { + debug: () => {}, + error: () => {}, +}; + describe('Dingus', function () { let dingus; beforeEach(function () { - dingus = new Dingus(); + dingus = new Dingus(noLogger, {}); }); afterEach(function () { sinon.restore(); @@ -23,7 +28,7 @@ describe('Dingus', function () { describe('constructor', function () { it('covers', function () { - const d = new Dingus({}, {}); + const d = new Dingus(); assert(d); }); }); // constructor @@ -36,7 +41,7 @@ describe('Dingus', function () { }); it('returns normal path', function () { const p = '////a///b/./bar/..///c'; - const expected = '/a/b/c' + const expected = '/a/b/c'; const r = dingus._normalizePath(p); assert.strictEqual(r, expected); }); @@ -152,7 +157,7 @@ describe('Dingus', function () { const expected = { clientAddress: '', clientProtocol: 'http', - } + }; dingus.clientAddressContext(req, res, ctx); assert.deepStrictEqual(ctx, expected); assert(!req.getHeader.called); @@ -162,7 +167,7 @@ describe('Dingus', function () { const expected = { clientAddress: '::1', clientProtocol: 'https', - } + }; req.connection.remoteAddress = '::1'; req.connection.encrypted = true; dingus.clientAddressContext(req, res, ctx); @@ -549,7 +554,7 @@ describe('Dingus', function () { const req = {}; const res = {}; const ctx = {}; - sinon.stub(dingus, 'bodyData').resolves('{"foo":"bar"}') + sinon.stub(dingus, 'bodyData').resolves('{"foo":"bar"}'); sinon.stub(Dingus, 'getRequestContentType').returns(Enum.ContentType.ApplicationJson); await dingus.ingestBody(req, res, ctx); assert.deepStrictEqual(ctx.parsedBody, { foo: 'bar' }); diff --git a/test/lib/router.js b/test/lib/router.js index c89cc19..8a9d1e2 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -5,7 +5,7 @@ const assert = require('assert'); const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require const Router = require('../../lib/router'); -const PathParameter = require('../../lib/router/path-parameter') +const PathParameter = require('../../lib/router/path-parameter'); const { DingusError } = require('../../lib/errors'); const noExpectedException = 'did not get expected exception';