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

events: disabled manual construction AbortSignal #36094

Closed
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
16 changes: 15 additions & 1 deletion lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
const {
ObjectAssign,
ObjectDefineProperties,
ObjectSetPrototypeOf,
Symbol,
TypeError,
} = primordials;

const {
Expand Down Expand Up @@ -35,6 +37,11 @@ function customInspect(self, obj, depth, options) {
}

class AbortSignal extends EventTarget {
constructor() {
// eslint-disable-next-line no-restricted-syntax
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why is this comment needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why is this comment needed?

It's because of this:

node/lib/.eslintrc.yaml

Lines 22 to 23 in c7d2a45

- selector: "NewExpression[callee.name=/Error$/]:not([callee.name=/^(AssertionError|NghttpError)$/])"
message: "Use an error exported by the internal/errors module."
.

throw new TypeError('Illegal constructor');
}

get aborted() { return !!this[kAborted]; }

[customInspectSymbol](depth, options) {
Expand All @@ -50,6 +57,13 @@ ObjectDefineProperties(AbortSignal.prototype, {

defineEventHandler(AbortSignal.prototype, 'abort');

function createAbortSignal() {
const signal = new EventTarget();
ObjectSetPrototypeOf(signal, AbortSignal.prototype);
signal[kAborted] = false;
return signal;
}

function abortSignal(signal) {
if (signal[kAborted]) return;
signal[kAborted] = true;
Expand All @@ -65,7 +79,7 @@ function abortSignal(signal) {
const kSignal = Symbol('signal');
class AbortController {
constructor() {
this[kSignal] = new AbortSignal();
this[kSignal] = createAbortSignal();
emitExperimentalWarning('AbortController');
}

Expand Down
11 changes: 10 additions & 1 deletion test/parallel/test-abortcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const common = require('../common');

const { ok, strictEqual } = require('assert');
const { ok, strictEqual, throws } = require('assert');

{
// Tests that abort is fired with the correct event type on AbortControllers
Expand Down Expand Up @@ -51,3 +51,12 @@ const { ok, strictEqual } = require('assert');
strictEqual(firstTrusted, secondTrusted);
strictEqual(untrusted, firstTrusted);
}

{
// Tests that AbortSignal is impossible to construct manually
const ac = new AbortController();
throws(
() => new ac.signal.constructor(),
/^TypeError: Illegal constructor$/
);
}