diff options
Diffstat (limited to 'packages/service-inject/src/utils.ts')
-rw-r--r-- | packages/service-inject/src/utils.ts | 104 |
1 files changed, 0 insertions, 104 deletions
diff --git a/packages/service-inject/src/utils.ts b/packages/service-inject/src/utils.ts deleted file mode 100644 index 4bb3fba..0000000 --- a/packages/service-inject/src/utils.ts +++ /dev/null | |||
@@ -1,104 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * Simulates a function defined in native code, i.e., one with | ||
23 | * `[native code]` in its `toString`. | ||
24 | * | ||
25 | * @param name The name of the function. | ||
26 | * @param f The function to transform. | ||
27 | * @return The transformed function. | ||
28 | */ | ||
29 | export function simulateNativeFunction<T, P extends unknown[]>( | ||
30 | name: string, | ||
31 | f: (this: null, ...args: P) => T, | ||
32 | ): (...args: P) => T { | ||
33 | // Bound functions say `[native code]`, but unfortunately they omit the function name: | ||
34 | // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString#description | ||
35 | // The type of `f` contains type variables, so we need some magic type casting. | ||
36 | const boundFunc = f.bind(null as ThisParameterType<typeof f>); | ||
37 | Object.defineProperty(boundFunc, 'name', { | ||
38 | configurable: true, | ||
39 | enumerable: false, | ||
40 | value: name, | ||
41 | writable: false, | ||
42 | }); | ||
43 | return boundFunc; | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * Simulates a native class available on `globalThis`. | ||
48 | * | ||
49 | * @param name The name of the class. | ||
50 | * @param constructor The constructor function. Must already be a constructor (a named `function`). | ||
51 | * @param properties The properties to define on the prototype. | ||
52 | */ | ||
53 | export function simulateNativeClass( | ||
54 | name: string, | ||
55 | constructor: () => void, | ||
56 | properties: PropertyDescriptorMap, | ||
57 | ) { | ||
58 | Object.defineProperties(constructor.prototype, { | ||
59 | [Symbol.toStringTag]: { | ||
60 | configurable: true, | ||
61 | enumerable: false, | ||
62 | value: name, | ||
63 | writable: false, | ||
64 | }, | ||
65 | ...properties, | ||
66 | }); | ||
67 | const simulatedConstructor = simulateNativeFunction(name, constructor); | ||
68 | Object.defineProperty(globalThis, name, { | ||
69 | configurable: true, | ||
70 | enumerable: true, | ||
71 | value: simulatedConstructor, | ||
72 | writable: true, | ||
73 | }); | ||
74 | return simulatedConstructor; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * Defines a property on the prototype of an object. | ||
79 | * | ||
80 | * Only use this with singleton objects, e.g., `window.navigator`. | ||
81 | * | ||
82 | * @param o The object to modify. Must be a singleton. | ||
83 | * @param property The key of the property being defined or modified. | ||
84 | * @param attributes The descriptor of the property being defined or modified. | ||
85 | */ | ||
86 | export function defineProtoProperty( | ||
87 | o: object, | ||
88 | property: PropertyKey, | ||
89 | attributes: PropertyDescriptor, | ||
90 | ): void { | ||
91 | Object.defineProperty(Object.getPrototypeOf(o), property, attributes); | ||
92 | } | ||
93 | |||
94 | /** | ||
95 | * Deletes a property from the prototype of an object. | ||
96 | * | ||
97 | * Only use this with singleton objects, e.g., `window.navigator`. | ||
98 | * | ||
99 | * @param o The object to modify. Must be a singleton. | ||
100 | * @param property The key of the property being deleted. | ||
101 | */ | ||
102 | export function deleteProtoProperty(o: object, property: PropertyKey): void { | ||
103 | Reflect.deleteProperty(Object.getPrototypeOf(o), property); | ||
104 | } | ||