mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
lib: expose DOMException
as global
Refs: https://github.com/nodejs/node/issues/39098 PR-URL: https://github.com/nodejs/node/pull/39176 Fixes: https://github.com/nodejs/node/issues/39098 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
0a7f850123
commit
e4b1fb5e64
@ -349,6 +349,7 @@ module.exports = {
|
||||
BigInt: 'readable',
|
||||
BigInt64Array: 'readable',
|
||||
BigUint64Array: 'readable',
|
||||
DOMException: 'readable',
|
||||
Event: 'readable',
|
||||
EventTarget: 'readable',
|
||||
MessageChannel: 'readable',
|
||||
|
@ -380,6 +380,15 @@ added: v0.0.1
|
||||
|
||||
[`setTimeout`][] is described in the [timers][] section.
|
||||
|
||||
## `DOMException`
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
<!-- type=global -->
|
||||
|
||||
The WHATWG `DOMException` class. See [`DOMException`][] for more details.
|
||||
|
||||
## `TextDecoder`
|
||||
<!-- YAML
|
||||
added: v11.0.0
|
||||
@ -430,6 +439,7 @@ The object that acts as the namespace for all W3C
|
||||
[Mozilla Developer Network][webassembly-mdn] for usage and compatibility.
|
||||
|
||||
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
|
||||
[`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
|
||||
[`EventTarget` and `Event` API]: events.md#event-target-and-event-api
|
||||
[`MessageChannel`]: worker_threads.md#worker_threads_class_messagechannel
|
||||
[`MessageEvent`]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent
|
||||
|
@ -37,6 +37,8 @@ rules:
|
||||
message: "Use `const { Atomics } = globalThis;` instead of the global."
|
||||
- name: Buffer
|
||||
message: "Use `const { Buffer } = require('buffer');` instead of the global."
|
||||
- name: DOMException
|
||||
message: "Use lazy function `const { lazyDOMException } = require('internal/util');` instead of the global."
|
||||
- name: Event
|
||||
message: "Use `const { Event } = require('internal/event_target');` instead of the global."
|
||||
- name: EventTarget
|
||||
|
@ -52,7 +52,7 @@ const {
|
||||
globalThis,
|
||||
} = primordials;
|
||||
const config = internalBinding('config');
|
||||
const { deprecate } = require('internal/util');
|
||||
const { deprecate, lazyDOMExceptionClass } = require('internal/util');
|
||||
|
||||
setupProcessObject();
|
||||
|
||||
@ -201,6 +201,12 @@ if (!config.noBrowserGlobals) {
|
||||
exposeInterface(globalThis, 'URL', URL);
|
||||
// https://url.spec.whatwg.org/#urlsearchparams
|
||||
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
|
||||
exposeGetterAndSetter(globalThis,
|
||||
'DOMException',
|
||||
lazyDOMExceptionClass,
|
||||
(value) => {
|
||||
exposeInterface(globalThis, 'DOMException', value);
|
||||
});
|
||||
|
||||
const {
|
||||
TextEncoder, TextDecoder
|
||||
@ -483,6 +489,15 @@ function exposeInterface(target, name, interfaceObject) {
|
||||
});
|
||||
}
|
||||
|
||||
function exposeGetterAndSetter(target, name, getter, setter = undefined) {
|
||||
ObjectDefineProperty(target, name, {
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
get: getter,
|
||||
set: setter,
|
||||
});
|
||||
}
|
||||
|
||||
// https://heycam.github.io/webidl/#define-the-operations
|
||||
function defineOperation(target, name, method) {
|
||||
ObjectDefineProperty(target, name, {
|
||||
|
@ -442,11 +442,15 @@ function createDeferredPromise() {
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
||||
let DOMException;
|
||||
let _DOMException;
|
||||
const lazyDOMExceptionClass = () => {
|
||||
_DOMException ??= internalBinding('messaging').DOMException;
|
||||
return _DOMException;
|
||||
};
|
||||
|
||||
const lazyDOMException = hideStackFrames((message, name) => {
|
||||
if (DOMException === undefined)
|
||||
DOMException = internalBinding('messaging').DOMException;
|
||||
return new DOMException(message, name);
|
||||
_DOMException ??= internalBinding('messaging').DOMException;
|
||||
return new _DOMException(message, name);
|
||||
});
|
||||
|
||||
function structuredClone(value) {
|
||||
@ -481,6 +485,7 @@ module.exports = {
|
||||
isInsideNodeModules,
|
||||
join,
|
||||
lazyDOMException,
|
||||
lazyDOMExceptionClass,
|
||||
normalizeEncoding,
|
||||
once,
|
||||
promisify,
|
||||
|
11
test/parallel/test-global-domexception.js
Normal file
11
test/parallel/test-global-domexception.js
Normal file
@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
assert.strictEqual(typeof DOMException, 'function');
|
||||
|
||||
assert.throws(() => {
|
||||
atob('我要抛错!');
|
||||
}, DOMException);
|
@ -12,8 +12,6 @@ runner.setFlags(['--expose-internals']);
|
||||
runner.setInitScript(`
|
||||
const { internalBinding } = require('internal/test/binding');
|
||||
const { atob, btoa } = require('buffer');
|
||||
const { DOMException } = internalBinding('messaging');
|
||||
global.DOMException = DOMException;
|
||||
`);
|
||||
|
||||
runner.runJsTests();
|
||||
|
@ -5,14 +5,4 @@ const { WPTRunner } = require('../common/wpt');
|
||||
|
||||
const runner = new WPTRunner('url');
|
||||
|
||||
// Needed to access to DOMException.
|
||||
runner.setFlags(['--expose-internals']);
|
||||
|
||||
// DOMException is needed by urlsearchparams-constructor.any.js
|
||||
runner.setInitScript(`
|
||||
const { internalBinding } = require('internal/test/binding');
|
||||
const { DOMException } = internalBinding('messaging');
|
||||
global.DOMException = DOMException;
|
||||
`);
|
||||
|
||||
runner.runJsTests();
|
||||
|
Loading…
Reference in New Issue
Block a user