aboutsummaryrefslogtreecommitdiffstats
path: root/packages/service-inject/src/utils.ts
blob: 4bb3fbaa95c684ba07ff3c79c6cd9e5436b3e272 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (C)  2021-2022 Kristóf Marussy <kristof@marussy.com>
 *
 * This file is part of Sophie.
 *
 * Sophie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

/**
 * Simulates a function defined in native code, i.e., one with
 * `[native code]` in its `toString`.
 *
 * @param name The name of the function.
 * @param f The function to transform.
 * @return The transformed function.
 */
export function simulateNativeFunction<T, P extends unknown[]>(
  name: string,
  f: (this: null, ...args: P) => T,
): (...args: P) => T {
  // Bound functions say `[native code]`, but unfortunately they omit the function name:
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString#description
  // The type of `f` contains type variables, so we need some magic type casting.
  const boundFunc = f.bind(null as ThisParameterType<typeof f>);
  Object.defineProperty(boundFunc, 'name', {
    configurable: true,
    enumerable: false,
    value: name,
    writable: false,
  });
  return boundFunc;
}

/**
 * Simulates a native class available on `globalThis`.
 *
 * @param name The name of the class.
 * @param constructor The constructor function. Must already be a constructor (a named `function`).
 * @param properties The properties to define on the prototype.
 */
export function simulateNativeClass(
  name: string,
  constructor: () => void,
  properties: PropertyDescriptorMap,
) {
  Object.defineProperties(constructor.prototype, {
    [Symbol.toStringTag]: {
      configurable: true,
      enumerable: false,
      value: name,
      writable: false,
    },
    ...properties,
  });
  const simulatedConstructor = simulateNativeFunction(name, constructor);
  Object.defineProperty(globalThis, name, {
    configurable: true,
    enumerable: true,
    value: simulatedConstructor,
    writable: true,
  });
  return simulatedConstructor;
}

/**
 * Defines a property on the prototype of an object.
 *
 * Only use this with singleton objects, e.g., `window.navigator`.
 *
 * @param o The object to modify. Must be a singleton.
 * @param property The key of the property being defined or modified.
 * @param attributes The descriptor of the property being defined or modified.
 */
export function defineProtoProperty(
  o: object,
  property: PropertyKey,
  attributes: PropertyDescriptor,
): void {
  Object.defineProperty(Object.getPrototypeOf(o), property, attributes);
}

/**
 * Deletes a property from the prototype of an object.
 *
 * Only use this with singleton objects, e.g., `window.navigator`.
 *
 * @param o The object to modify. Must be a singleton.
 * @param property The key of the property being deleted.
 */
export function deleteProtoProperty(o: object, property: PropertyKey): void {
  Reflect.deleteProperty(Object.getPrototypeOf(o), property);
}