Skip to content

Commit

Permalink
chore: add comments regarding polyfills for the tests (nodejs#30)
Browse files Browse the repository at this point in the history
And fix the implementation to be closer to the native one.
  • Loading branch information
aduh95 authored Jul 25, 2022
1 parent ba8fd71 commit 9585a8c
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ function expectsError (validator, exact) {
}

if (typeof AbortSignal.timeout !== 'function') {
// `AbortSignal.timeout` is not available on Node.js 14.x, we need to polyfill
// it because some tests are using it. End-users don't need to it.

class AbortError extends Error {
constructor (message = 'The operation was aborted', options = undefined) {
super(message, options)
Expand All @@ -119,22 +122,22 @@ if (typeof AbortSignal.timeout !== 'function') {
}

if (process.version.startsWith('v14.') || process.version.startsWith('v16.')) {
AbortSignal.abort = () => {
// Implementation of AbortSignal and AbortController differ slightly with the
// v18.x one, creating some difference on the TAP output which makes the tests
// fail. We are overriding the built-ins to make the test pass, however that's
// not necessary to do for the library to work (i.e. end-users don't need it).

const defaultAbortError = new Error('This operation was aborted')
defaultAbortError.code = 20

AbortSignal.abort = function abort (reason = defaultAbortError) {
const controller = new AbortController()
const error = new Error('This operation was aborted')
error.code = 20
controller.abort(error)
controller.abort(reason)
return controller.signal
}
const nativeAbort = AbortController.prototype.abort
AbortController.prototype.abort = function abort (reason) {
if (arguments.length === 0) {
reason = new Error('This operation was aborted')
reason.code = 20
}
if (process.version.startsWith('v14.')) {
this.signal.reason = reason
}
AbortController.prototype.abort = function abort (reason = defaultAbortError) {
this.signal.reason = reason
nativeAbort.call(this, reason)
}
}
Expand Down

0 comments on commit 9585a8c

Please sign in to comment.