From 30471e1f7250f03e5366d09e2ad564f2f03f8d29 Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Sat, 2 Dec 2023 16:01:47 +0100 Subject: [PATCH] Revert "fix: avoid global unlimited config (#89)" This reverts commit d455a6e3090817eaa32a0ec959e28700644f5c0e. --- index.js | 21 ++++++--------------- package.json | 1 - test/emit-once-only.test.js | 2 +- test/issue-88.test.js | 26 -------------------------- 4 files changed, 7 insertions(+), 43 deletions(-) delete mode 100644 test/issue-88.test.js diff --git a/index.js b/index.js index 5919c8c..1e7f87f 100644 --- a/index.js +++ b/index.js @@ -43,6 +43,7 @@ const { format } = require('node:util') function processWarning () { const codes = {} const emitted = new Map() + const opts = Object.create(null) /** * Builds a new {@link ProcessWarning} and adds it to the @@ -90,12 +91,8 @@ function processWarning () { } } - // We need to manage 4 states: - // - START: code 0: unlimited emission - // - END: code -1: unlimited event emitted - // - START: code 1 (default): limited emission to 1 - // - END: code 2: limited event emitted - emitted.set(code, unlimited ? 0 : 1) + Object.assign(opts, { unlimited }) + emitted.set(code, unlimited) codes[code] = buildWarnOpts return codes[code] @@ -140,10 +137,9 @@ function processWarning () { * @param {*} [c] Possible message interpolation value. */ function emit (code, a, b, c) { - const state = emitted.get(code) - if (state === 2) return + if (emitted.get(code) === true && opts.unlimited === false) return if (codes[code] === undefined) throw new Error(`The code '${code}' does not exist`) - emitted.set(code, state <= 0 ? -1 : (state + 1)) + emitted.set(code, true) const warning = codes[code](a, b, c) process.emitWarning(warning.message, warning.name, warning.code) @@ -153,12 +149,7 @@ function processWarning () { create, createDeprecation, emit, - emitted: { - get (code) { - const state = emitted.get(code) - return state === -1 || state === 2 - } - } + emitted } } diff --git a/package.json b/package.json index 84d8b8f..2410cb3 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "scripts": { "lint": "standard", "lint:fix": "standard --fix", - "benchmark": "node benchmarks/warn.js", "test": "npm run test:unit && npm run test:jest && npm run test:typescript", "test:jest": "jest jest.test.js", "test:unit": "tap", diff --git a/test/emit-once-only.test.js b/test/emit-once-only.test.js index 75d9036..e1d8a30 100644 --- a/test/emit-once-only.test.js +++ b/test/emit-once-only.test.js @@ -1,6 +1,6 @@ 'use strict' -const { test } = require('tap') +const test = require('tap').test const build = require('..') test('emit should emit a given code only once', t => { diff --git a/test/issue-88.test.js b/test/issue-88.test.js deleted file mode 100644 index 79696c0..0000000 --- a/test/issue-88.test.js +++ /dev/null @@ -1,26 +0,0 @@ -'use strict' - -const { test } = require('tap') -const build = require('..') - -test('Must not overwrite the global config', t => { - t.plan(1) - - const { create, emit } = build() - - function onWarning (warning) { - t.equal(warning.code, 'CODE_1') - } - - create('FastifyWarning', 'CODE_1', 'Msg', { unlimited: false }) - create('FastifyWarning', 'CODE_2', 'Msg', { unlimited: true }) - - process.on('warning', onWarning) - emit('CODE_1') - emit('CODE_1') - - setImmediate(() => { - process.removeListener('warning', onWarning) - t.end() - }) -})