Skip to content
Open
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
14 changes: 13 additions & 1 deletion lib/ours/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,19 @@ function E(code, message, Base) {
})
NodeError.prototype.code = code
NodeError.prototype[kIsNodeError] = true
codes[code] = NodeError
// Node 18 calls `ERR_MULTIPLE_CALLBACK()` from `_final` with no `new`.
// A class throws TypeError before this body runs, and that throw escapes
// a callback scheduled with `nextTick`, so the process dies instead of
// emitting ERR_MULTIPLE_CALLBACK. The function builds the same instance
// either way. `new` still returns it because a constructor that returns
// an object uses that object.
function createNodeError(...args) {
return new NodeError(...args)
}
Object.setPrototypeOf(createNodeError, NodeError)
createNodeError.prototype = NodeError.prototype

codes[code] = createNodeError
}
function hideStackFrames(fn) {
// We rename the functions that will be hidden to cut off the stacktrace
Expand Down
14 changes: 13 additions & 1 deletion src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,19 @@ function E(code, message, Base) {
NodeError.prototype.code = code
NodeError.prototype[kIsNodeError] = true

codes[code] = NodeError
// Node 18 calls `ERR_MULTIPLE_CALLBACK()` from `_final` with no `new`.
// A class throws TypeError before this body runs, and that throw escapes
// a callback scheduled with `nextTick`, so the process dies instead of
// emitting ERR_MULTIPLE_CALLBACK. The function builds the same instance
// either way. `new` still returns it because a constructor that returns
// an object uses that object.
function createNodeError(...args) {
return new NodeError(...args)
}
Object.setPrototypeOf(createNodeError, NodeError)
createNodeError.prototype = NodeError.prototype

codes[code] = createNodeError
}

function hideStackFrames(fn) {
Expand Down
12 changes: 11 additions & 1 deletion src/test/ours/test-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function checkError(err, Base, name, code, message) {
}

// Update this numbers based on the number of checkError below multiplied by the assertions within checkError
t.plan(17 * 4)
t.plan(19 * 4 + 1)

checkError(
new errors.ERR_INVALID_ARG_VALUE('name', 0),
Expand Down Expand Up @@ -102,6 +102,16 @@ checkError(
'ERR_MULTIPLE_CALLBACK',
'Callback called multiple times'
)
const multipleCallback = errors.ERR_MULTIPLE_CALLBACK()
checkError(multipleCallback, Error, 'Error', 'ERR_MULTIPLE_CALLBACK', 'Callback called multiple times')
t.ok(multipleCallback instanceof errors.ERR_MULTIPLE_CALLBACK)
checkError(
errors.ERR_INVALID_ARG_VALUE('name', 0),
TypeError,
'TypeError',
'ERR_INVALID_ARG_VALUE',
"The argument 'name' is invalid. Received 0"
)

checkError(new errors.ERR_STREAM_CANNOT_PIPE(), Error, 'Error', 'ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable')

Expand Down
43 changes: 43 additions & 0 deletions src/test/ours/test-stream-writable-final-cb-twice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

const t = require('tap')
const { Writable } = require('../../lib/ours/index')

t.test('a second synchronous _final callback emits ERR_MULTIPLE_CALLBACK', (t) => {
t.plan(2)
const writable = new Writable({
write(buf, enc, cb) {
cb()
},
final(cb) {
cb()
cb()
}
})
writable.on('error', (err) => {
t.equal(err.code, 'ERR_MULTIPLE_CALLBACK')
t.equal(err.name, 'Error')
})
writable.end('hi')
})

t.test('a second _final callback on nextTick does not crash', (t) => {
t.plan(1)
const writable = new Writable({
write(buf, enc, cb) {
cb()
},
final(cb) {
cb()
process.nextTick(() => {
// finish already destroyed the stream, as in Node core.
// The bug was the TypeError thrown before that check.
t.doesNotThrow(() => cb())
})
}
})
writable.on('error', (err) => {
t.fail(err.message)
})
writable.end('hi')
})
12 changes: 11 additions & 1 deletion test/ours/test-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function checkError(err, Base, name, code, message) {
}

// Update this numbers based on the number of checkError below multiplied by the assertions within checkError
t.plan(17 * 4)
t.plan(19 * 4 + 1)
checkError(
new errors.ERR_INVALID_ARG_VALUE('name', 0),
TypeError,
Expand Down Expand Up @@ -89,6 +89,16 @@ checkError(
'ERR_MULTIPLE_CALLBACK',
'Callback called multiple times'
)
const multipleCallback = errors.ERR_MULTIPLE_CALLBACK()
checkError(multipleCallback, Error, 'Error', 'ERR_MULTIPLE_CALLBACK', 'Callback called multiple times')
t.ok(multipleCallback instanceof errors.ERR_MULTIPLE_CALLBACK)
checkError(
errors.ERR_INVALID_ARG_VALUE('name', 0),
TypeError,
'TypeError',
'ERR_INVALID_ARG_VALUE',
"The argument 'name' is invalid. Received 0"
)
checkError(new errors.ERR_STREAM_CANNOT_PIPE(), Error, 'Error', 'ERR_STREAM_CANNOT_PIPE', 'Cannot pipe, not readable')
checkError(new errors.ERR_STREAM_WRITE_AFTER_END(), Error, 'Error', 'ERR_STREAM_WRITE_AFTER_END', 'write after end')
checkError(
Expand Down
43 changes: 43 additions & 0 deletions test/ours/test-stream-writable-final-cb-twice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

const t = require('tap')
const { Writable } = require('../../lib/ours/index')

t.test('a second synchronous _final callback emits ERR_MULTIPLE_CALLBACK', (t) => {
t.plan(2)
const writable = new Writable({
write(buf, enc, cb) {
cb()
},
final(cb) {
cb()
cb()
}
})
writable.on('error', (err) => {
t.equal(err.code, 'ERR_MULTIPLE_CALLBACK')
t.equal(err.name, 'Error')
})
writable.end('hi')
})

t.test('a second _final callback on nextTick does not crash', (t) => {
t.plan(1)
const writable = new Writable({
write(buf, enc, cb) {
cb()
},
final(cb) {
cb()
process.nextTick(() => {
// finish already destroyed the stream, as in Node core.
// The bug was the TypeError thrown before that check.
t.doesNotThrow(() => cb())
})
}
})
writable.on('error', (err) => {
t.fail(err.message)
})
writable.end('hi')
})