Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DOMException polyfill #133

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.