Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid global unlimited config #89

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ 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
Expand Down Expand Up @@ -91,8 +90,12 @@ function processWarning () {
}
}

Object.assign(opts, { unlimited })
emitted.set(code, unlimited)
// 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)
codes[code] = buildWarnOpts

return codes[code]
Expand Down Expand Up @@ -137,9 +140,10 @@ function processWarning () {
* @param {*} [c] Possible message interpolation value.
*/
function emit (code, a, b, c) {
if (emitted.get(code) === true && opts.unlimited === false) return
const state = emitted.get(code)
if (state === 2) return
if (codes[code] === undefined) throw new Error(`The code '${code}' does not exist`)
emitted.set(code, true)
emitted.set(code, state <= 0 ? -1 : (state + 1))

const warning = codes[code](a, b, c)
process.emitWarning(warning.message, warning.name, warning.code)
Expand All @@ -149,7 +153,12 @@ function processWarning () {
create,
createDeprecation,
emit,
emitted
emitted: {
get (code) {
const state = emitted.get(code)
return state === -1 || state === 2
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"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",
Expand Down
2 changes: 1 addition & 1 deletion test/emit-once-only.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const test = require('tap').test
const { test } = require('tap')
const build = require('..')

test('emit should emit a given code only once', t => {
Expand Down
26 changes: 26 additions & 0 deletions test/issue-88.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'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()
})
})
Loading