From e272c1bfd1aa1cdd6ebecf04b60fbfd72b14f57c Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 25 Oct 2023 01:08:24 -0700 Subject: [PATCH] Add `IllegalConstructor` error subclass --- .changeset/hot-wolves-exist.md | 5 +++++ packages/runtime/src/crypto.ts | 8 +++++--- packages/runtime/src/navigator.ts | 7 +++---- packages/runtime/src/navigator/battery.ts | 5 +++-- packages/runtime/src/utils.ts | 6 ++++++ 5 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 .changeset/hot-wolves-exist.md diff --git a/.changeset/hot-wolves-exist.md b/.changeset/hot-wolves-exist.md new file mode 100644 index 00000000..7527401f --- /dev/null +++ b/.changeset/hot-wolves-exist.md @@ -0,0 +1,5 @@ +--- +'nxjs-runtime': patch +--- + +Add `IllegalConstructor` error subclass diff --git a/packages/runtime/src/crypto.ts b/packages/runtime/src/crypto.ts index 7c9bdadd..3b3fd644 100644 --- a/packages/runtime/src/crypto.ts +++ b/packages/runtime/src/crypto.ts @@ -1,4 +1,4 @@ -import { def } from './utils'; +import { IllegalConstructor, def } from './utils'; import { type SwitchClass } from './switch'; import { type ArrayBufferView } from './types'; @@ -13,7 +13,9 @@ export class Crypto implements globalThis.Crypto { /** * @ignore */ - constructor() {} + constructor() { + throw new IllegalConstructor(); + } /** * The `crypto.subtle` interface is not yet implemented. @@ -83,5 +85,5 @@ def('Crypto', Crypto); * * @see https://developer.mozilla.org/docs/Web/API/crypto_property */ -export const crypto = new Crypto(); +export const crypto: Crypto = Object.create(Crypto.prototype); def('crypto', crypto); diff --git a/packages/runtime/src/navigator.ts b/packages/runtime/src/navigator.ts index f1587db9..b34a3f52 100644 --- a/packages/runtime/src/navigator.ts +++ b/packages/runtime/src/navigator.ts @@ -1,4 +1,4 @@ -import { def } from './utils'; +import { IllegalConstructor, def } from './utils'; import { BatteryManager } from './navigator/battery'; import { INTERNAL_SYMBOL } from './types'; import type { SwitchClass } from './switch'; @@ -24,7 +24,7 @@ export class Navigator { * @ignore */ constructor() { - throw new TypeError('Illegal constructor.'); + throw new IllegalConstructor(); } /** @@ -50,8 +50,7 @@ export class Navigator { } /** - * Returns a battery promise, which is resolved in a {@link BatteryManager} object - * providing also some new events you can handle to monitor the battery status. + * Returns a promise which is resolved to a {@link BatteryManager} instance. * * @see https://developer.mozilla.org/docs/Web/API/Navigator/getBattery */ diff --git a/packages/runtime/src/navigator/battery.ts b/packages/runtime/src/navigator/battery.ts index d99796fd..6572b3de 100644 --- a/packages/runtime/src/navigator/battery.ts +++ b/packages/runtime/src/navigator/battery.ts @@ -1,7 +1,8 @@ import { $ } from '../$'; import { INTERNAL_SYMBOL } from '../types'; -import { def } from '../utils'; +import { IllegalConstructor, def } from '../utils'; import type { SwitchClass } from '../switch'; +import type { Navigator } from '../navigator'; declare const Switch: SwitchClass; @@ -18,7 +19,7 @@ export class BatteryManager extends EventTarget { */ constructor() { if (arguments[0] !== INTERNAL_SYMBOL) { - throw new TypeError('Illegal constructor.'); + throw new IllegalConstructor(); } super(); $.batteryInit(); diff --git a/packages/runtime/src/utils.ts b/packages/runtime/src/utils.ts index 01f4b643..6e3f8dfc 100644 --- a/packages/runtime/src/utils.ts +++ b/packages/runtime/src/utils.ts @@ -34,3 +34,9 @@ export function asyncIteratorToStream(it: AsyncIterableIterator) { }, }); } + +export class IllegalConstructor extends TypeError { + constructor() { + super('Illegal constructor'); + } +}