Skip to content

Commit

Permalink
Merge pull request #133 from MattiasBuelens/improve-domexception
Browse files Browse the repository at this point in the history
Improve `DOMException` polyfill
  • Loading branch information
MattiasBuelens authored Jan 3, 2024
2 parents da6e08e + 13392c5 commit 9cdb902
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 22 additions & 4 deletions src/stub/dom-exception.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="node" />
import { NativeDOMException } from './native';
import { globals } from '../globals';
import { setFunctionName } from '../lib/helpers/miscellaneous';

interface DOMException extends Error {
name: string;
Expand All @@ -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;
Expand All @@ -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 || '';
Expand All @@ -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 };
3 changes: 0 additions & 3 deletions src/stub/native.ts

This file was deleted.

0 comments on commit 9cdb902

Please sign in to comment.