| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,11 @@ const { | |||
| 23 | 23 | emitExperimentalWarning | |
| 24 | 24 | } = require('internal/util'); | |
| 25 | 25 | const { inspect } = require('internal/util/inspect'); | |
| 26 | + const { | ||
| 27 | + codes: { | ||
| 28 | + ERR_INVALID_THIS, | ||
| 29 | + } | ||
| 30 | + } = require('internal/errors'); | ||
| 26 | 31 | ||
| 27 | 32 | const kAborted = Symbol('kAborted'); | |
| 28 | 33 | ||
@@ -37,13 +42,21 @@ function customInspect(self, obj, depth, options) { | |||
| 37 | 42 | return `${self.constructor.name} ${inspect(obj, opts)}`; | |
| 38 | 43 | } | |
| 39 | 44 | ||
| 45 | + function validateAbortSignal(obj) { | ||
| 46 | + if (obj?.[kAborted] === undefined) | ||
| 47 | + throw new ERR_INVALID_THIS('AbortSignal'); | ||
| 48 | + } | ||
| 49 | + | ||
| 40 | 50 | class AbortSignal extends EventTarget { | |
| 41 | 51 | constructor() { | |
| 42 | 52 | // eslint-disable-next-line no-restricted-syntax | |
| 43 | 53 | throw new TypeError('Illegal constructor'); | |
| 44 | 54 | } | |
| 45 | 55 | ||
| 46 | - get aborted() { return !!this[kAborted]; } | ||
| 56 | + get aborted() { | ||
| 57 | + validateAbortSignal(this); | ||
| 58 | + return !!this[kAborted]; | ||
| 59 | + } | ||
| 47 | 60 | ||
| 48 | 61 | [customInspectSymbol](depth, options) { | |
| 49 | 62 | return customInspect(this, { | |
@@ -89,14 +102,27 @@ function abortSignal(signal) { | |||
| 89 | 102 | // initializers for now: | |
| 90 | 103 | // https://bugs.chromium.org/p/v8/issues/detail?id=10704 | |
| 91 | 104 | const kSignal = Symbol('signal'); | |
| 105 | + | ||
| 106 | + function validateAbortController(obj) { | ||
| 107 | + if (obj?.[kSignal] === undefined) | ||
| 108 | + throw new ERR_INVALID_THIS('AbortController'); | ||
| 109 | + } | ||
| 110 | + | ||
| 92 | 111 | class AbortController { | |
| 93 | 112 | constructor() { | |
| 94 | 113 | this[kSignal] = createAbortSignal(); | |
| 95 | 114 | emitExperimentalWarning('AbortController'); | |
| 96 | 115 | } | |
| 97 | 116 | ||
| 98 | - get signal() { return this[kSignal]; } | ||
| 99 | - abort() { abortSignal(this[kSignal]); } | ||
| 117 | + get signal() { | ||
| 118 | + validateAbortController(this); | ||
| 119 | + return this[kSignal]; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + abort() { | ||
| 123 | + validateAbortController(this); | ||
| 124 | + abortSignal(this[kSignal]); | ||
| 125 | + } | ||
| 100 | 126 | ||
| 101 | 127 | [customInspectSymbol](depth, options) { | |
| 102 | 128 | return customInspect(this, { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -73,3 +73,63 @@ const { Event } = require('internal/event_target'); | |||
| 73 | 73 | const signal = AbortSignal.abort(); | |
| 74 | 74 | ok(signal.aborted); | |
| 75 | 75 | } | |
| 76 | + | ||
| 77 | + { | ||
| 78 | + // Test that AbortController properties and methods validate the receiver | ||
| 79 | + const acSignalGet = Object.getOwnPropertyDescriptor( | ||
| 80 | + AbortController.prototype, | ||
| 81 | + 'signal' | ||
| 82 | + ).get; | ||
| 83 | + const acAbort = AbortController.prototype.abort; | ||
| 84 | + | ||
| 85 | + const goodController = new AbortController(); | ||
| 86 | + ok(acSignalGet.call(goodController)); | ||
| 87 | + acAbort.call(goodController); | ||
| 88 | + | ||
| 89 | + const badAbortControllers = [ | ||
| 90 | + null, | ||
| 91 | + undefined, | ||
| 92 | + 0, | ||
| 93 | + NaN, | ||
| 94 | + true, | ||
| 95 | + 'AbortController', | ||
| 96 | + Object.create(AbortController.prototype) | ||
| 97 | + ]; | ||
| 98 | + for (const badController of badAbortControllers) { | ||
| 99 | + throws( | ||
| 100 | + () => acSignalGet.call(badController), | ||
| 101 | + { code: 'ERR_INVALID_THIS', name: 'TypeError' } | ||
| 102 | + ); | ||
| 103 | + throws( | ||
| 104 | + () => acAbort.call(badController), | ||
| 105 | + { code: 'ERR_INVALID_THIS', name: 'TypeError' } | ||
| 106 | + ); | ||
| 107 | + } | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + { | ||
| 111 | + // Test that AbortSignal properties validate the receiver | ||
| 112 | + const signalAbortedGet = Object.getOwnPropertyDescriptor( | ||
| 113 | + AbortSignal.prototype, | ||
| 114 | + 'aborted' | ||
| 115 | + ).get; | ||
| 116 | + | ||
| 117 | + const goodSignal = new AbortController().signal; | ||
| 118 | + strictEqual(signalAbortedGet.call(goodSignal), false); | ||
| 119 | + | ||
| 120 | + const badAbortSignals = [ | ||
| 121 | + null, | ||
| 122 | + undefined, | ||
| 123 | + 0, | ||
| 124 | + NaN, | ||
| 125 | + true, | ||
| 126 | + 'AbortSignal', | ||
| 127 | + Object.create(AbortSignal.prototype) | ||
| 128 | + ]; | ||
| 129 | + for (const badSignal of badAbortSignals) { | ||
| 130 | + throws( | ||
| 131 | + () => signalAbortedGet.call(badSignal), | ||
| 132 | + { code: 'ERR_INVALID_THIS', name: 'TypeError' } | ||
| 133 | + ); | ||
| 134 | + } | ||
| 135 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments