GeezerVison is showing github.com — events: disabled manual construction AbortSignal · nodejs/node@64cd54b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/abort_controller.js +15-1Lines changed: 15 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 |
| 6 | 6 | const { | 7 | 7 | Object, | | 8 | + ObjectSetPrototypeOf, | 8 | 9 | Symbol, | | 10 | + TypeError, | 9 | 11 | } = primordials; | 10 | 12 |
| 11 | 13 | const { | @@ -34,6 +36,11 @@ function customInspect(self, obj, depth, options) { | 34 | 36 | } | 35 | 37 |
| 36 | 38 | class AbortSignal extends EventTarget { | | 39 | + constructor() { | | 40 | + // eslint-disable-next-line no-restricted-syntax | | 41 | + throw new TypeError('Illegal constructor'); | | 42 | + } | | 43 | + | 37 | 44 | get aborted() { return !!this[kAborted]; } | 38 | 45 |
| 39 | 46 | [customInspectSymbol](depth, options) { | @@ -49,6 +56,13 @@ Object.defineProperties(AbortSignal.prototype, { | 49 | 56 |
| 50 | 57 | defineEventHandler(AbortSignal.prototype, 'abort'); | 51 | 58 |
| | 59 | +function createAbortSignal() { | | 60 | + const signal = new EventTarget(); | | 61 | + ObjectSetPrototypeOf(signal, AbortSignal.prototype); | | 62 | + signal[kAborted] = false; | | 63 | + return signal; | | 64 | +} | | 65 | + | 52 | 66 | function abortSignal(signal) { | 53 | 67 | if (signal[kAborted]) return; | 54 | 68 | signal[kAborted] = true; | @@ -64,7 +78,7 @@ function abortSignal(signal) { | 64 | 78 | const kSignal = Symbol('signal'); | 65 | 79 | class AbortController { | 66 | 80 | constructor() { | 67 | | - this[kSignal] = new AbortSignal(); | | 81 | + this[kSignal] = createAbortSignal(); | 68 | 82 | emitExperimentalWarning('AbortController'); | 69 | 83 | } | 70 | 84 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-abortcontroller.js +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | const common = require('../common'); | 5 | 5 |
| 6 | | -const { ok, strictEqual } = require('assert'); | | 6 | +const { ok, strictEqual, throws } = require('assert'); | 7 | 7 | const { Event } = require('internal/event_target'); | 8 | 8 |
| 9 | 9 | { | @@ -52,3 +52,12 @@ const { Event } = require('internal/event_target'); | 52 | 52 | strictEqual(firstTrusted, secondTrusted); | 53 | 53 | strictEqual(untrusted, firstTrusted); | 54 | 54 | } | | 55 | + | | 56 | +{ | | 57 | + // Tests that AbortSignal is impossible to construct manually | | 58 | + const ac = new AbortController(); | | 59 | + throws( | | 60 | + () => new ac.signal.constructor(), | | 61 | + /^TypeError: Illegal constructor$/ | | 62 | + ); | | 63 | +} |
|