mirror of
https://github.com/nodejs/node.git
synced 2025-05-02 03:31:35 +00:00
lib: use private field in AbortController
Instead of validating the receiver ourselves, let V8 handle the validation using the semantics of private fields. PR-URL: https://github.com/nodejs/node/pull/43820 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
e54ee80d0b
commit
73ba8830d5
@ -292,34 +292,21 @@ function abortSignal(signal, reason) {
|
||||
signal.dispatchEvent(event);
|
||||
}
|
||||
|
||||
// TODO(joyeecheung): use private fields and we'll get invalid access
|
||||
// validation from V8 instead of throwing ERR_INVALID_THIS ourselves.
|
||||
const kSignal = Symbol('signal');
|
||||
|
||||
function validateAbortController(obj) {
|
||||
if (obj?.[kSignal] === undefined)
|
||||
throw new ERR_INVALID_THIS('AbortController');
|
||||
}
|
||||
|
||||
class AbortController {
|
||||
constructor() {
|
||||
this[kSignal] = createAbortSignal();
|
||||
}
|
||||
#signal = createAbortSignal();
|
||||
|
||||
/**
|
||||
* @type {AbortSignal}
|
||||
*/
|
||||
get signal() {
|
||||
validateAbortController(this);
|
||||
return this[kSignal];
|
||||
return this.#signal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} reason
|
||||
*/
|
||||
abort(reason = new DOMException('This operation was aborted', 'AbortError')) {
|
||||
validateAbortController(this);
|
||||
abortSignal(this[kSignal], reason);
|
||||
abortSignal(this.#signal, reason);
|
||||
}
|
||||
|
||||
[customInspectSymbol](depth, options) {
|
||||
|
@ -108,11 +108,11 @@ const { setTimeout: sleep } = require('timers/promises');
|
||||
for (const badController of badAbortControllers) {
|
||||
throws(
|
||||
() => acSignalGet.call(badController),
|
||||
{ code: 'ERR_INVALID_THIS', name: 'TypeError' }
|
||||
{ name: 'TypeError' }
|
||||
);
|
||||
throws(
|
||||
() => acAbort.call(badController),
|
||||
{ code: 'ERR_INVALID_THIS', name: 'TypeError' }
|
||||
{ name: 'TypeError' }
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -139,7 +139,7 @@ const { setTimeout: sleep } = require('timers/promises');
|
||||
for (const badSignal of badAbortSignals) {
|
||||
throws(
|
||||
() => signalAbortedGet.call(badSignal),
|
||||
{ code: 'ERR_INVALID_THIS', name: 'TypeError' }
|
||||
{ name: 'TypeError' }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user