diff --git a/CHANGELOG.md b/CHANGELOG.md index 97d2853..d993e8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ even when TypeScript doesn't yet have a built-in type definition for them. * 💥 The type definitions now require TypeScript 3.5 or higher. ([#130](https://github.com/MattiasBuelens/web-streams-polyfill/pull/130)) * 🐛 Prevent [warnings from Bluebird](http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-created-in-a-handler-but-was-not-returned-from-it) about a promise being created within a handler but not being returned from a handler. ([#131](https://github.com/MattiasBuelens/web-streams-polyfill/pull/131)) +* 🏠 Improve internal `DOMException` polyfill. ([#133](https://github.com/MattiasBuelens/web-streams-polyfill/pull/133)) ## v3.2.1 (2022-04-07) diff --git a/src/stub/dom-exception.ts b/src/stub/dom-exception.ts index 2a06a46..b676c8c 100644 --- a/src/stub/dom-exception.ts +++ b/src/stub/dom-exception.ts @@ -1,5 +1,6 @@ /// -import { NativeDOMException } from './native'; +import { globals } from '../globals'; +import { setFunctionName } from '../lib/helpers/miscellaneous'; interface DOMException extends Error { name: string; @@ -12,6 +13,9 @@ function isDOMExceptionConstructor(ctor: unknown): ctor is DOMExceptionConstruct if (!(typeof ctor === 'function' || typeof ctor === 'object')) { return false; } + if ((ctor as DOMExceptionConstructor).name !== 'DOMException') { + return false; + } try { new (ctor as DOMExceptionConstructor)(); return true; @@ -20,7 +24,21 @@ function isDOMExceptionConstructor(ctor: unknown): ctor is DOMExceptionConstruct } } -function createDOMExceptionPolyfill(): DOMExceptionConstructor { +/** + * Support: + * - Web browsers + * - Node 18 and higher (https://github.com/nodejs/node/commit/e4b1fb5e6422c1ff151234bb9de792d45dd88d87) + */ +function getFromGlobal(): DOMExceptionConstructor | undefined { + const ctor = globals?.DOMException; + return isDOMExceptionConstructor(ctor) ? ctor : undefined; +} + +/** + * Support: + * - All platforms + */ +function createPolyfill(): DOMExceptionConstructor { // eslint-disable-next-line no-shadow const ctor = function DOMException(this: DOMException, message?: string, name?: string) { this.message = message || ''; @@ -29,13 +47,13 @@ function createDOMExceptionPolyfill(): DOMExceptionConstructor { Error.captureStackTrace(this, this.constructor); } } as any; + setFunctionName(ctor, 'DOMException'); ctor.prototype = Object.create(Error.prototype); Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true }); return ctor; } // eslint-disable-next-line no-redeclare -const DOMException: DOMExceptionConstructor = - isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill(); +const DOMException: DOMExceptionConstructor = getFromGlobal() || createPolyfill(); export { DOMException }; diff --git a/src/stub/native.ts b/src/stub/native.ts deleted file mode 100644 index d76db48..0000000 --- a/src/stub/native.ts +++ /dev/null @@ -1,3 +0,0 @@ -/// -export const NativeDOMException: typeof DOMException | undefined = - typeof DOMException !== 'undefined' ? DOMException : undefined;