Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e4b1fb5

Browse filesBrowse files
XadillaXjasnell
authored andcommitted
lib: expose DOMException as global
Refs: #39098 PR-URL: #39176 Fixes: #39098 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0a7f850 commit e4b1fb5
Copy full SHA for e4b1fb5

File tree

Expand file treeCollapse file tree

8 files changed

+49
-17
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

8 files changed

+49
-17
lines changed
Open diff view settings
Collapse file

‎.eslintrc.js‎

Copy file name to clipboardExpand all lines: .eslintrc.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ module.exports = {
349349
BigInt: 'readable',
350350
BigInt64Array: 'readable',
351351
BigUint64Array: 'readable',
352+
DOMException: 'readable',
352353
Event: 'readable',
353354
EventTarget: 'readable',
354355
MessageChannel: 'readable',
Collapse file

‎doc/api/globals.md‎

Copy file name to clipboardExpand all lines: doc/api/globals.md
+10Lines changed: 10 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,15 @@ added: v0.0.1
380380

381381
[`setTimeout`][] is described in the [timers][] section.
382382

383+
## `DOMException`
384+
<!-- YAML
385+
added: REPLACEME
386+
-->
387+
388+
<!-- type=global -->
389+
390+
The WHATWG `DOMException` class. See [`DOMException`][] for more details.
391+
383392
## `TextDecoder`
384393
<!-- YAML
385394
added: v11.0.0
@@ -430,6 +439,7 @@ The object that acts as the namespace for all W3C
430439
[Mozilla Developer Network][webassembly-mdn] for usage and compatibility.
431440

432441
[`AbortController`]: https://developer.mozilla.org/en-US/docs/Web/API/AbortController
442+
[`DOMException`]: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
433443
[`EventTarget` and `Event` API]: events.md#event-target-and-event-api
434444
[`MessageChannel`]: worker_threads.md#worker_threads_class_messagechannel
435445
[`MessageEvent`]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent
Collapse file

‎lib/.eslintrc.yaml‎

Copy file name to clipboardExpand all lines: lib/.eslintrc.yaml
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ rules:
3737
message: "Use `const { Atomics } = globalThis;` instead of the global."
3838
- name: Buffer
3939
message: "Use `const { Buffer } = require('buffer');` instead of the global."
40+
- name: DOMException
41+
message: "Use lazy function `const { lazyDOMException } = require('internal/util');` instead of the global."
4042
- name: Event
4143
message: "Use `const { Event } = require('internal/event_target');` instead of the global."
4244
- name: EventTarget
Collapse file

‎lib/internal/bootstrap/node.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const {
5252
globalThis,
5353
} = primordials;
5454
const config = internalBinding('config');
55-
const { deprecate } = require('internal/util');
55+
const { deprecate, lazyDOMExceptionClass } = require('internal/util');
5656

5757
setupProcessObject();
5858

@@ -201,6 +201,12 @@ if (!config.noBrowserGlobals) {
201201
exposeInterface(globalThis, 'URL', URL);
202202
// https://url.spec.whatwg.org/#urlsearchparams
203203
exposeInterface(globalThis, 'URLSearchParams', URLSearchParams);
204+
exposeGetterAndSetter(globalThis,
205+
'DOMException',
206+
lazyDOMExceptionClass,
207+
(value) => {
208+
exposeInterface(globalThis, 'DOMException', value);
209+
});
204210

205211
const {
206212
TextEncoder, TextDecoder
@@ -483,6 +489,15 @@ function exposeInterface(target, name, interfaceObject) {
483489
});
484490
}
485491

492+
function exposeGetterAndSetter(target, name, getter, setter = undefined) {
493+
ObjectDefineProperty(target, name, {
494+
enumerable: false,
495+
configurable: true,
496+
get: getter,
497+
set: setter,
498+
});
499+
}
500+
486501
// https://heycam.github.io/webidl/#define-the-operations
487502
function defineOperation(target, name, method) {
488503
ObjectDefineProperty(target, name, {
Collapse file

‎lib/internal/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/util.js
+9-4Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,15 @@ function createDeferredPromise() {
442442
return { promise, resolve, reject };
443443
}
444444

445-
let DOMException;
445+
let _DOMException;
446+
const lazyDOMExceptionClass = () => {
447+
_DOMException ??= internalBinding('messaging').DOMException;
448+
return _DOMException;
449+
};
450+
446451
const lazyDOMException = hideStackFrames((message, name) => {
447-
if (DOMException === undefined)
448-
DOMException = internalBinding('messaging').DOMException;
449-
return new DOMException(message, name);
452+
_DOMException ??= internalBinding('messaging').DOMException;
453+
return new _DOMException(message, name);
450454
});
451455

452456
function structuredClone(value) {
@@ -481,6 +485,7 @@ module.exports = {
481485
isInsideNodeModules,
482486
join,
483487
lazyDOMException,
488+
lazyDOMExceptionClass,
484489
normalizeEncoding,
485490
once,
486491
promisify,
Collapse file
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('assert');
6+
7+
assert.strictEqual(typeof DOMException, 'function');
8+
9+
assert.throws(() => {
10+
atob('我要抛错!');
11+
}, DOMException);
Collapse file

‎test/wpt/test-atob.js‎

Copy file name to clipboardExpand all lines: test/wpt/test-atob.js
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ runner.setFlags(['--expose-internals']);
1212
runner.setInitScript(`
1313
const { internalBinding } = require('internal/test/binding');
1414
const { atob, btoa } = require('buffer');
15-
const { DOMException } = internalBinding('messaging');
16-
global.DOMException = DOMException;
1715
`);
1816

1917
runner.runJsTests();
Collapse file

‎test/wpt/test-url.js‎

Copy file name to clipboardExpand all lines: test/wpt/test-url.js
-10Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,4 @@ const { WPTRunner } = require('../common/wpt');
55

66
const runner = new WPTRunner('url');
77

8-
// Needed to access to DOMException.
9-
runner.setFlags(['--expose-internals']);
10-
11-
// DOMException is needed by urlsearchparams-constructor.any.js
12-
runner.setInitScript(`
13-
const { internalBinding } = require('internal/test/binding');
14-
const { DOMException } = internalBinding('messaging');
15-
global.DOMException = DOMException;
16-
`);
17-
188
runner.runJsTests();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.