Skip to content

Commit

Permalink
Add IllegalConstructor error subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 25, 2023
1 parent c31b8d9 commit e272c1b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-wolves-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Add `IllegalConstructor` error subclass
8 changes: 5 additions & 3 deletions packages/runtime/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { def } from './utils';
import { IllegalConstructor, def } from './utils';
import { type SwitchClass } from './switch';
import { type ArrayBufferView } from './types';

Expand All @@ -13,7 +13,9 @@ export class Crypto implements globalThis.Crypto {
/**
* @ignore
*/
constructor() {}
constructor() {
throw new IllegalConstructor();
}

/**
* The `crypto.subtle` interface is not yet implemented.
Expand Down Expand Up @@ -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);
7 changes: 3 additions & 4 deletions packages/runtime/src/navigator.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,7 +24,7 @@ export class Navigator {
* @ignore
*/
constructor() {
throw new TypeError('Illegal constructor.');
throw new IllegalConstructor();
}

/**
Expand All @@ -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
*/
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime/src/navigator/battery.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions packages/runtime/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ export function asyncIteratorToStream<T>(it: AsyncIterableIterator<T>) {
},
});
}

export class IllegalConstructor extends TypeError {
constructor() {
super('Illegal constructor');
}
}

0 comments on commit e272c1b

Please sign in to comment.