async-browser-context 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +21 -21
- package/README.md +51 -8
- package/dist/babel/name-coroutines.d.ts +25 -0
- package/dist/babel/name-coroutines.d.ts.map +1 -0
- package/dist/babel/name-coroutines.js +116 -0
- package/dist/babel/name-coroutines.js.map +1 -0
- package/dist/babel/preset.d.ts +4 -2
- package/dist/babel/preset.d.ts.map +1 -1
- package/dist/babel/preset.js +6 -2
- package/dist/babel/preset.js.map +1 -1
- package/dist/core/callback-hook.d.ts +24 -0
- package/dist/core/callback-hook.d.ts.map +1 -0
- package/dist/core/callback-hook.js +13 -0
- package/dist/core/callback-hook.js.map +1 -0
- package/dist/core/coroutine.d.ts +2 -12
- package/dist/core/coroutine.d.ts.map +1 -1
- package/dist/core/coroutine.js +23 -8
- package/dist/core/coroutine.js.map +1 -1
- package/dist/core/hooks.d.ts +15 -0
- package/dist/core/hooks.d.ts.map +1 -0
- package/dist/core/hooks.js +28 -0
- package/dist/core/hooks.js.map +1 -0
- package/dist/core/store.d.ts +28 -3
- package/dist/core/store.d.ts.map +1 -1
- package/dist/core/store.js +81 -7
- package/dist/core/store.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/node/index.d.ts +10 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +14 -0
- package/dist/node/index.js.map +1 -1
- package/dist/patches/callbacks.d.ts.map +1 -1
- package/dist/patches/callbacks.js +10 -8
- package/dist/patches/callbacks.js.map +1 -1
- package/dist/patches/events.d.ts.map +1 -1
- package/dist/patches/events.js +39 -22
- package/dist/patches/events.js.map +1 -1
- package/dist/patches/handler-interfaces.d.ts +16 -0
- package/dist/patches/handler-interfaces.d.ts.map +1 -0
- package/dist/patches/handler-interfaces.js +42 -0
- package/dist/patches/handler-interfaces.js.map +1 -0
- package/dist/patches/observers.js +1 -1
- package/dist/patches/observers.js.map +1 -1
- package/dist/patches/patch.d.ts +5 -2
- package/dist/patches/patch.d.ts.map +1 -1
- package/dist/patches/patch.js +6 -3
- package/dist/patches/patch.js.map +1 -1
- package/dist/patches/promise.js +1 -1
- package/dist/patches/promise.js.map +1 -1
- package/dist/patches/streams.d.ts.map +1 -1
- package/dist/patches/streams.js +12 -7
- package/dist/patches/streams.js.map +1 -1
- package/dist/patches/timers.d.ts.map +1 -1
- package/dist/patches/timers.js +6 -5
- package/dist/patches/timers.js.map +1 -1
- package/package.json +2 -1
- package/src/babel/name-coroutines.ts +128 -0
- package/src/babel/preset.ts +6 -2
- package/src/core/callback-hook.ts +34 -0
- package/src/core/coroutine.ts +27 -8
- package/src/core/hooks.ts +30 -0
- package/src/core/store.ts +108 -7
- package/src/index.ts +1 -0
- package/src/node/index.ts +16 -0
- package/src/patches/callbacks.ts +10 -8
- package/src/patches/events.ts +38 -22
- package/src/patches/handler-interfaces.ts +41 -0
- package/src/patches/observers.ts +1 -1
- package/src/patches/patch.ts +6 -3
- package/src/patches/promise.ts +2 -2
- package/src/patches/streams.ts +12 -7
- package/src/patches/timers.ts +6 -5
package/src/core/store.ts
CHANGED
|
@@ -34,6 +34,25 @@ export interface ContextStore {
|
|
|
34
34
|
/** The original function of each patched function. `Function.prototype.toString` shows the original. */
|
|
35
35
|
readonly originals : WeakMap<object, object>;
|
|
36
36
|
readonly intrinsics : Intrinsics;
|
|
37
|
+
/**
|
|
38
|
+
* The callback hooks (`addCallbackHook`), or `undefined` if there are none. Each change
|
|
39
|
+
* makes a new array. The store of an older copy of the library does not
|
|
40
|
+
* have this property.
|
|
41
|
+
*/
|
|
42
|
+
hooks? : readonly HookEntry[] | undefined;
|
|
43
|
+
/** `true` while a hook operates. Callbacks that start in a hook do not start the hook methods. */
|
|
44
|
+
inHook? : boolean | undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The methods of a callback hook. The public type is `CallbackHook` in `hooks.ts`. */
|
|
48
|
+
export interface HookMethods {
|
|
49
|
+
before?(callback : Function, source : string) : void;
|
|
50
|
+
after?(callback : Function, source : string) : void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One registration of a hook. Each `addCallbackHook` call makes a new entry. */
|
|
54
|
+
export interface HookEntry {
|
|
55
|
+
readonly hook : HookMethods;
|
|
37
56
|
}
|
|
38
57
|
|
|
39
58
|
/** The key of the store on `globalThis`. The version changes when the store format changes. */
|
|
@@ -53,6 +72,9 @@ function createStore() : ContextStore {
|
|
|
53
72
|
promiseResolve : NativePromise.resolve.bind(NativePromise) as Intrinsics["promiseResolve"],
|
|
54
73
|
functionToString : Function.prototype.toString,
|
|
55
74
|
}),
|
|
75
|
+
// Own properties from the start: each callback reads `hooks`, and the read of an own property is faster
|
|
76
|
+
hooks : undefined,
|
|
77
|
+
inHook : false,
|
|
56
78
|
};
|
|
57
79
|
}
|
|
58
80
|
|
|
@@ -143,17 +165,92 @@ export function findValue(frame : Frame, variable : object) : unknown {
|
|
|
143
165
|
return ValueFrame.find(frame, variable);
|
|
144
166
|
}
|
|
145
167
|
|
|
168
|
+
/** This function gives the error of a hook to the host. A hook error does not stop the callback. */
|
|
169
|
+
function reportHookError(error : unknown) : void {
|
|
170
|
+
const report = (globalThis as { reportError? : unknown }).reportError;
|
|
171
|
+
if (typeof report === "function") {
|
|
172
|
+
(report as (error : unknown) => void)(error);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
console.error(error);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* This function starts one method of each hook. `before` uses the order of
|
|
180
|
+
* registration, and `after` uses the opposite order. Each hook operates in
|
|
181
|
+
* the current frame, and a hook cannot change the frame of the callback.
|
|
182
|
+
*/
|
|
183
|
+
function callHooks(hooks : readonly HookEntry[], phase : "before" | "after", callback : Function, source : string) : void {
|
|
184
|
+
const frame = store.current;
|
|
185
|
+
store.inHook = true;
|
|
186
|
+
try {
|
|
187
|
+
const count = hooks.length;
|
|
188
|
+
for (let step = 0; step < count; step++) {
|
|
189
|
+
const { hook } = hooks[phase === "before" ? step : count - 1 - step] as HookEntry;
|
|
190
|
+
const method = hook[phase];
|
|
191
|
+
if (typeof method !== "function") {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
method.call(hook, callback, source);
|
|
196
|
+
} catch (error) {
|
|
197
|
+
reportHookError(error);
|
|
198
|
+
}
|
|
199
|
+
store.current = frame;
|
|
200
|
+
}
|
|
201
|
+
} finally {
|
|
202
|
+
store.inHook = false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* This function starts `fn` between the `before` and the `after` methods of
|
|
208
|
+
* the hooks. The current frame must be the frame of the callback. The hooks
|
|
209
|
+
* get `callback` and `source`. A callback that starts in a hook does not start
|
|
210
|
+
* the hooks again.
|
|
211
|
+
*/
|
|
212
|
+
export function applyWithHooks<T, A extends unknown[], R>(
|
|
213
|
+
hooks : readonly HookEntry[],
|
|
214
|
+
callback : Function,
|
|
215
|
+
source : string,
|
|
216
|
+
fn : (this : T, ...args : A) => R,
|
|
217
|
+
thisValue : T,
|
|
218
|
+
args : A,
|
|
219
|
+
) : R {
|
|
220
|
+
if (store.inHook === true) {
|
|
221
|
+
return fn.apply(thisValue, args);
|
|
222
|
+
}
|
|
223
|
+
callHooks(hooks, "before", callback, source);
|
|
224
|
+
try {
|
|
225
|
+
return fn.apply(thisValue, args);
|
|
226
|
+
} finally {
|
|
227
|
+
callHooks(hooks, "after", callback, source);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/*
|
|
232
|
+
* Each wrapper has the name `asyncBrowserContextWrapper`. Profilers, stack
|
|
233
|
+
* traces and the Long Animation Frames API show the static name of a
|
|
234
|
+
* function, so they show this name in place of the name of the callback. The
|
|
235
|
+
* name tells the reader that the next frame is the callback.
|
|
236
|
+
*/
|
|
237
|
+
|
|
146
238
|
/**
|
|
147
239
|
* This function wraps `fn`. The wrapper starts `fn` in `frame`, with the `this`
|
|
148
240
|
* value and the arguments of the call. After `fn` ends, also with an error, the
|
|
149
|
-
* previous frame is current again.
|
|
241
|
+
* previous frame is current again. `source` is the name of the API that got
|
|
242
|
+
* `fn`, for the callback hooks.
|
|
150
243
|
*/
|
|
151
|
-
export function bindToFrame<T, A extends unknown[], R>(frame : Frame, fn : (this : T, ...args : A) => R) : (this : T, ...args : A) => R {
|
|
152
|
-
return function
|
|
244
|
+
export function bindToFrame<T, A extends unknown[], R>(frame : Frame, fn : (this : T, ...args : A) => R, source : string) : (this : T, ...args : A) => R {
|
|
245
|
+
return function asyncBrowserContextWrapper(this : T, ...args : A) : R {
|
|
153
246
|
const previous = store.current;
|
|
154
247
|
store.current = frame;
|
|
155
248
|
try {
|
|
156
|
-
|
|
249
|
+
const hooks = store.hooks;
|
|
250
|
+
if (hooks === undefined) {
|
|
251
|
+
return fn.apply(this, args);
|
|
252
|
+
}
|
|
253
|
+
return applyWithHooks(hooks, fn, source, fn, this, args);
|
|
157
254
|
} finally {
|
|
158
255
|
store.current = previous;
|
|
159
256
|
}
|
|
@@ -165,12 +262,16 @@ export function bindToFrame<T, A extends unknown[], R>(frame : Frame, fn : (this
|
|
|
165
262
|
* wrapper does not make an array of the arguments, so it is faster than
|
|
166
263
|
* `bindToFrame` on the hot path.
|
|
167
264
|
*/
|
|
168
|
-
export function bindOneArgument<T, A, R>(frame : Frame, fn : (this : T, argument : A) => R) : (this : T, argument : A) => R {
|
|
169
|
-
return function
|
|
265
|
+
export function bindOneArgument<T, A, R>(frame : Frame, fn : (this : T, argument : A) => R, source : string) : (this : T, argument : A) => R {
|
|
266
|
+
return function asyncBrowserContextWrapper(this : T, argument : A) : R {
|
|
170
267
|
const previous = store.current;
|
|
171
268
|
store.current = frame;
|
|
172
269
|
try {
|
|
173
|
-
|
|
270
|
+
const hooks = store.hooks;
|
|
271
|
+
if (hooks === undefined) {
|
|
272
|
+
return fn.call(this, argument);
|
|
273
|
+
}
|
|
274
|
+
return applyWithHooks(hooks, fn, source, fn, this, [argument]);
|
|
174
275
|
} finally {
|
|
175
276
|
store.current = previous;
|
|
176
277
|
}
|
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { Snapshot } from "./core/snapshot.js";
|
|
|
15
15
|
import { Variable } from "./core/variable.js";
|
|
16
16
|
|
|
17
17
|
export { AsyncLocalStorage, type AsyncLocalStorageOptions } from "./core/async-local-storage.js";
|
|
18
|
+
export { addCallbackHook, type CallbackHook } from "./core/hooks.js";
|
|
18
19
|
export { Snapshot } from "./core/snapshot.js";
|
|
19
20
|
export { Variable, type VariableOptions } from "./core/variable.js";
|
|
20
21
|
|
package/src/node/index.ts
CHANGED
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
* @packageDocumentation
|
|
9
9
|
*/
|
|
10
10
|
import { AsyncLocalStorage as NodeAsyncLocalStorage } from "node:async_hooks";
|
|
11
|
+
import { assertCallbackHook, type CallbackHook } from "../core/callback-hook.js";
|
|
11
12
|
import type { VariableOptions } from "../core/variable.js";
|
|
12
13
|
import { createCompatibleStorage } from "./compatible-storage.js";
|
|
13
14
|
|
|
14
15
|
export type { AsyncLocalStorageOptions } from "../core/async-local-storage.js";
|
|
16
|
+
export type { CallbackHook } from "../core/callback-hook.js";
|
|
15
17
|
export type { VariableOptions } from "../core/variable.js";
|
|
16
18
|
|
|
17
19
|
/** The value of a `Variable` in one context. The box tells "a value is set" apart from `undefined`. */
|
|
@@ -98,3 +100,17 @@ export type AsyncVariable<T> = Variable<T>;
|
|
|
98
100
|
export const AsyncSnapshot : typeof Snapshot = Snapshot;
|
|
99
101
|
/** Another name for `Snapshot`. */
|
|
100
102
|
export type AsyncSnapshot = Snapshot;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* On Node.js, this function does nothing. The library does not wrap callbacks
|
|
106
|
+
* on Node.js, because the native `AsyncLocalStorage` keeps the context. Use
|
|
107
|
+
* `node:diagnostics_channel` or `node:async_hooks` to measure callbacks. The
|
|
108
|
+
* function examines the hook as the browser function does, and gives a
|
|
109
|
+
* function that does nothing.
|
|
110
|
+
*/
|
|
111
|
+
export function addCallbackHook(hook : CallbackHook) : () => void {
|
|
112
|
+
assertCallbackHook(hook);
|
|
113
|
+
return function removeCallbackHook() : void {
|
|
114
|
+
// There is no hook to remove
|
|
115
|
+
};
|
|
116
|
+
}
|
package/src/patches/callbacks.ts
CHANGED
|
@@ -27,22 +27,24 @@ const STATIC_METHODS : readonly (readonly [string, readonly string[]])[] = [
|
|
|
27
27
|
["Array", ["fromAsync"]],
|
|
28
28
|
];
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
/** `source` is the name of the method, for the callback hooks. */
|
|
31
|
+
function wrapArguments(source : string) : (original : Method) => Method {
|
|
32
|
+
return (original) => function (this : unknown, ...args : unknown[]) : unknown {
|
|
33
|
+
return original.apply(this, wrapFunctionArguments(args, source));
|
|
33
34
|
};
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
/** `startViewTransition()` gets a callback, or an object with an `update` callback. */
|
|
37
38
|
function wrapViewTransition(original : Method) : Method {
|
|
39
|
+
const source = "Document.startViewTransition";
|
|
38
40
|
return function (this : unknown, ...args : unknown[]) : unknown {
|
|
39
41
|
const [first] = args;
|
|
40
42
|
if (typeof first === "object" && first !== null && typeof (first as { update? : unknown }).update === "function") {
|
|
41
43
|
const options = first as { update : Method };
|
|
42
|
-
args[0] = { ...options, update : bindToFrame(store.current, options.update) };
|
|
44
|
+
args[0] = { ...options, update : bindToFrame(store.current, options.update, source) };
|
|
43
45
|
return original.apply(this, args);
|
|
44
46
|
}
|
|
45
|
-
return original.apply(this, wrapFunctionArguments(args));
|
|
47
|
+
return original.apply(this, wrapFunctionArguments(args, source));
|
|
46
48
|
};
|
|
47
49
|
}
|
|
48
50
|
|
|
@@ -52,7 +54,7 @@ function wrapIntercept(original : Method) : Method {
|
|
|
52
54
|
const [first] = args;
|
|
53
55
|
if (typeof first === "object" && first !== null && typeof (first as { handler? : unknown }).handler === "function") {
|
|
54
56
|
const options = first as { handler : Method };
|
|
55
|
-
args[0] = { ...options, handler : bindToFrame(store.current, options.handler) };
|
|
57
|
+
args[0] = { ...options, handler : bindToFrame(store.current, options.handler, "NavigateEvent.intercept") };
|
|
56
58
|
}
|
|
57
59
|
return original.apply(this, args);
|
|
58
60
|
};
|
|
@@ -69,7 +71,7 @@ export function installCallbackPatches() : void {
|
|
|
69
71
|
continue;
|
|
70
72
|
}
|
|
71
73
|
for (const method of methods) {
|
|
72
|
-
replaceFunction<Method>(prototype, method, wrapArguments);
|
|
74
|
+
replaceFunction<Method>(prototype, method, wrapArguments(`${constructorName}.${method}`));
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
for (const [constructorName, methods] of STATIC_METHODS) {
|
|
@@ -78,7 +80,7 @@ export function installCallbackPatches() : void {
|
|
|
78
80
|
continue;
|
|
79
81
|
}
|
|
80
82
|
for (const method of methods) {
|
|
81
|
-
replaceFunction<Method>(constructor, method, wrapArguments);
|
|
83
|
+
replaceFunction<Method>(constructor, method, wrapArguments(`${constructorName}.${method}`));
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
const documentPrototype = globalPrototype("Document");
|
package/src/patches/events.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { store, type Frame } from "../core/store.js";
|
|
1
|
+
import { applyWithHooks, store, type Frame } from "../core/store.js";
|
|
2
|
+
import { HANDLER_INTERFACES } from "./handler-interfaces.js";
|
|
2
3
|
import { claim, globalPrototype, ownDataValue, rememberOriginal, replaceFunction } from "./patch.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -37,16 +38,27 @@ function frameForCall(registered : Frame) : Frame {
|
|
|
37
38
|
return current === store.root ? registered : current;
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
|
|
41
|
+
/** `source` is `addEventListener:` and the event type, for the callback hooks. */
|
|
42
|
+
function createWrapper(listener : Listener, frame : Frame, onFirstCall : (() => void) | undefined, source : string) : EventListener {
|
|
43
|
+
return function asyncBrowserContextWrapper(this : unknown, event : Event) : unknown {
|
|
42
44
|
onFirstCall?.();
|
|
43
45
|
const previous = store.current;
|
|
44
46
|
store.current = frameForCall(frame);
|
|
45
47
|
try {
|
|
48
|
+
let fn : Handler;
|
|
49
|
+
let thisValue : unknown;
|
|
46
50
|
if (typeof listener === "function") {
|
|
47
|
-
|
|
51
|
+
fn = listener as Handler;
|
|
52
|
+
thisValue = this;
|
|
53
|
+
} else {
|
|
54
|
+
fn = listener.handleEvent as Handler;
|
|
55
|
+
thisValue = listener;
|
|
48
56
|
}
|
|
49
|
-
|
|
57
|
+
const hooks = store.hooks;
|
|
58
|
+
if (hooks === undefined || typeof fn !== "function") {
|
|
59
|
+
return fn.call(thisValue, event);
|
|
60
|
+
}
|
|
61
|
+
return applyWithHooks(hooks, fn, source, fn, thisValue, [event]);
|
|
50
62
|
} finally {
|
|
51
63
|
store.current = previous;
|
|
52
64
|
}
|
|
@@ -89,7 +101,7 @@ function installListenerPatch() : void {
|
|
|
89
101
|
}
|
|
90
102
|
};
|
|
91
103
|
const once = typeof options === "object" && options.once === true;
|
|
92
|
-
registration = { frame : store.current, wrapper : createWrapper(listener, store.current, once ? forget : undefined) };
|
|
104
|
+
registration = { frame : store.current, wrapper : createWrapper(listener, store.current, once ? forget : undefined, `addEventListener:${String(type)}`) };
|
|
93
105
|
listeners.set(listener, registration);
|
|
94
106
|
const signal = typeof options === "object" ? options.signal : undefined;
|
|
95
107
|
if (signal !== undefined) {
|
|
@@ -127,12 +139,17 @@ function installListenerPatch() : void {
|
|
|
127
139
|
/** The handler and its wrapper of each `on...` property of each target. */
|
|
128
140
|
const handlers = new WeakMap<object, Map<string, { readonly handler : Handler; readonly wrapper : Handler }>>();
|
|
129
141
|
|
|
130
|
-
|
|
131
|
-
|
|
142
|
+
/** `source` is the name of the `on...` property, for the callback hooks. */
|
|
143
|
+
function createHandlerWrapper(handler : Handler, frame : Frame, source : string) : Handler {
|
|
144
|
+
return function asyncBrowserContextWrapper(this : unknown, ...args : unknown[]) : unknown {
|
|
132
145
|
const previous = store.current;
|
|
133
146
|
store.current = frameForCall(frame);
|
|
134
147
|
try {
|
|
135
|
-
|
|
148
|
+
const hooks = store.hooks;
|
|
149
|
+
if (hooks === undefined) {
|
|
150
|
+
return handler.apply(this, args);
|
|
151
|
+
}
|
|
152
|
+
return applyWithHooks(hooks, handler, source, handler, this, args);
|
|
136
153
|
} finally {
|
|
137
154
|
store.current = previous;
|
|
138
155
|
}
|
|
@@ -170,7 +187,7 @@ function patchHandlerProperties(owner : object) : void {
|
|
|
170
187
|
byName = new Map();
|
|
171
188
|
handlers.set(target, byName);
|
|
172
189
|
}
|
|
173
|
-
const wrapper = createHandlerWrapper(value as Handler, store.current);
|
|
190
|
+
const wrapper = createHandlerWrapper(value as Handler, store.current, name);
|
|
174
191
|
byName.set(name, { handler : value as Handler, wrapper });
|
|
175
192
|
nativeSet.call(target, wrapper);
|
|
176
193
|
},
|
|
@@ -185,7 +202,11 @@ function patchHandlerProperties(owner : object) : void {
|
|
|
185
202
|
}
|
|
186
203
|
}
|
|
187
204
|
|
|
188
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* This function patches the `on...` properties of the global object and of the prototypes of
|
|
207
|
+
* the `EventTarget` classes in `HANDLER_INTERFACES`. It does not read the
|
|
208
|
+
* other globals: for each global that code reads for the first time, the browser makes an interface object.
|
|
209
|
+
*/
|
|
189
210
|
function installHandlerPropertyPatch() : void {
|
|
190
211
|
const eventTarget = ownDataValue(globalThis, "EventTarget");
|
|
191
212
|
if (typeof eventTarget !== "function") {
|
|
@@ -193,19 +214,14 @@ function installHandlerPropertyPatch() : void {
|
|
|
193
214
|
}
|
|
194
215
|
const eventTargetPrototype = (eventTarget as { prototype : object }).prototype;
|
|
195
216
|
const done = new Set<object>();
|
|
196
|
-
for (const name of
|
|
197
|
-
const
|
|
198
|
-
|
|
217
|
+
for (const name of HANDLER_INTERFACES) {
|
|
218
|
+
const prototype = globalPrototype(name);
|
|
219
|
+
// An alias, for example webkitMediaStream, can give a prototype a second time
|
|
220
|
+
if (prototype === undefined || done.has(prototype) || !Object.prototype.isPrototypeOf.call(eventTargetPrototype, prototype)) {
|
|
199
221
|
continue;
|
|
200
222
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
if (prototype === eventTargetPrototype || Object.prototype.isPrototypeOf.call(eventTargetPrototype, prototype)) {
|
|
206
|
-
done.add(prototype);
|
|
207
|
-
patchHandlerProperties(prototype);
|
|
208
|
-
}
|
|
223
|
+
done.add(prototype);
|
|
224
|
+
patchHandlerProperties(prototype);
|
|
209
225
|
}
|
|
210
226
|
// In browsers, the `on...` properties of window are own properties of the global object
|
|
211
227
|
patchHandlerProperties(globalThis);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The global interfaces whose prototype has its own `on...` properties. The
|
|
3
|
+
* event patch reads only these globals. For each global that code reads for
|
|
4
|
+
* the first time, the browser makes the interface object, and a page has
|
|
5
|
+
* approximately 1,000 interfaces. Thus, a read of all globals costs 10 to 20
|
|
6
|
+
* ms when the page loads, and this list costs 3 to 7 ms.
|
|
7
|
+
*
|
|
8
|
+
* The list is the union of Chromium 156, Firefox 157 and WebKit 27.2 in a
|
|
9
|
+
* secure context. It also has some interfaces of Safari, of mobile browsers
|
|
10
|
+
* and of workers. The test "patches each on... property of each interface" in
|
|
11
|
+
* `test/browser/c13-events.test.ts` reads all globals in each browser of the
|
|
12
|
+
* tests, and fails for an `on...` property without the patch. Add the
|
|
13
|
+
* interface here if the test fails.
|
|
14
|
+
*/
|
|
15
|
+
export const HANDLER_INTERFACES : readonly string[] = [
|
|
16
|
+
"AbortSignal", "Animation", "ApplePaySession", "AudioContext", "AudioDecoder", "AudioEncoder",
|
|
17
|
+
"AudioScheduledSourceNode", "AudioTrackList", "AudioWorkletNode", "BackgroundFetchRegistration",
|
|
18
|
+
"BaseAudioContext", "BatteryManager", "Bluetooth", "BluetoothDevice", "BluetoothRemoteGATTCharacteristic",
|
|
19
|
+
"BluetoothRemoteGATTService", "BroadcastChannel", "CaptureController", "Clipboard", "CloseWatcher", "CookieStore",
|
|
20
|
+
"CreateMonitor", "DedicatedWorkerGlobalScope", "DevicePosture", "Document", "DocumentPictureInPicture",
|
|
21
|
+
"EditContext", "Element", "EventSource", "FileReader", "FontFaceSet", "GPUDevice", "HID", "HIDDevice",
|
|
22
|
+
"HTMLBodyElement", "HTMLCameraElement", "HTMLElement", "HTMLFrameSetElement", "HTMLGeolocationElement",
|
|
23
|
+
"HTMLMediaElement", "HTMLMicrophoneElement", "HTMLUserMediaElement", "HTMLVideoElement", "IDBDatabase",
|
|
24
|
+
"IDBOpenDBRequest", "IDBRequest", "IDBTransaction", "IdleDetector", "LanguageModel", "ManagedMediaSource",
|
|
25
|
+
"ManagedSourceBuffer", "MathMLElement", "MediaDevices", "MediaKeySession", "MediaQueryList", "MediaRecorder",
|
|
26
|
+
"MediaSource", "MediaStream", "MediaStreamTrack", "MessagePort", "MIDIAccess", "MIDIInput", "MIDIPort",
|
|
27
|
+
"Navigation", "NavigationHistoryEntry", "NavigatorManagedData", "NDEFReader", "NetworkInformation",
|
|
28
|
+
"Notification", "OfflineAudioContext", "OffscreenCanvas", "PaymentRequest", "PaymentResponse", "Performance",
|
|
29
|
+
"PermissionStatus", "PictureInPictureWindow", "PresentationAvailability", "PresentationConnection",
|
|
30
|
+
"PresentationConnectionList", "PresentationRequest", "RemotePlayback", "RTCDataChannel", "RTCDtlsTransport",
|
|
31
|
+
"RTCDTMFSender", "RTCIceTransport", "RTCPeerConnection", "RTCSctpTransport", "Screen", "ScreenDetails",
|
|
32
|
+
"ScreenOrientation", "ScriptProcessorNode", "Sensor", "Serial", "SerialPort", "ServiceWorker",
|
|
33
|
+
"ServiceWorkerContainer", "ServiceWorkerGlobalScope", "ServiceWorkerRegistration", "ShadowRoot", "SharedWorker",
|
|
34
|
+
"SharedWorkerGlobalScope", "SourceBuffer", "SourceBufferList", "SpeechRecognition", "SpeechSynthesis",
|
|
35
|
+
"SpeechSynthesisUtterance", "SVGAnimationElement", "SVGElement", "TaskSignal", "TextTrack", "TextTrackCue",
|
|
36
|
+
"TextTrackList", "USB", "VideoDecoder", "VideoEncoder", "VideoTrackList", "VirtualKeyboard", "VisualViewport",
|
|
37
|
+
"WakeLockSentinel", "WebKitMediaKeySession", "webkitMediaStream", "webkitRTCPeerConnection",
|
|
38
|
+
"webkitSpeechRecognition", "WebSocket", "Window", "WindowControlsOverlay", "Worker", "WorkerGlobalScope",
|
|
39
|
+
"XMLHttpRequest", "XMLHttpRequestEventTarget", "XRCubeLayer", "XRCylinderLayer", "XREquirectLayer",
|
|
40
|
+
"XRLightProbe", "XRQuadLayer", "XRReferenceSpace", "XRSession", "XRSystem",
|
|
41
|
+
];
|
package/src/patches/observers.ts
CHANGED
|
@@ -43,7 +43,7 @@ export function installObserverPatches() : void {
|
|
|
43
43
|
construct(target, args : unknown[], newTarget : Function) : object {
|
|
44
44
|
const callback = args[0];
|
|
45
45
|
if (typeof callback === "function") {
|
|
46
|
-
args[0] = bindToFrame(store.current, callback as (...values : unknown[]) => unknown);
|
|
46
|
+
args[0] = bindToFrame(store.current, callback as (...values : unknown[]) => unknown, name);
|
|
47
47
|
}
|
|
48
48
|
return Reflect.construct(target, args, newTarget) as object;
|
|
49
49
|
},
|
package/src/patches/patch.ts
CHANGED
|
@@ -71,14 +71,17 @@ export function globalPrototype(name : string) : object | undefined {
|
|
|
71
71
|
return typeof prototype === "object" && prototype !== null ? prototype : undefined;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
/**
|
|
75
|
-
|
|
74
|
+
/**
|
|
75
|
+
* This function wraps each function argument with the current frame. Other arguments do not change.
|
|
76
|
+
* `source` is the name of the API, for the callback hooks.
|
|
77
|
+
*/
|
|
78
|
+
export function wrapFunctionArguments(args : unknown[], source : string) : unknown[] {
|
|
76
79
|
const frame = store.current;
|
|
77
80
|
let changed : unknown[] | undefined;
|
|
78
81
|
for (const [index, argument] of args.entries()) {
|
|
79
82
|
if (typeof argument === "function") {
|
|
80
83
|
changed ??= [...args];
|
|
81
|
-
changed[index] = bindToFrame(frame, argument as (...values : unknown[]) => unknown);
|
|
84
|
+
changed[index] = bindToFrame(frame, argument as (...values : unknown[]) => unknown, source);
|
|
82
85
|
}
|
|
83
86
|
}
|
|
84
87
|
return changed ?? args;
|
package/src/patches/promise.ts
CHANGED
|
@@ -27,8 +27,8 @@ export function installPromisePatch() : void {
|
|
|
27
27
|
const frame = store.current;
|
|
28
28
|
return nativeThen.call(
|
|
29
29
|
this,
|
|
30
|
-
typeof onFulfilled === "function" ? bindOneArgument(frame, onFulfilled) : onFulfilled,
|
|
31
|
-
typeof onRejected === "function" ? bindOneArgument(frame, onRejected) : onRejected,
|
|
30
|
+
typeof onFulfilled === "function" ? bindOneArgument(frame, onFulfilled, "Promise.then") : onFulfilled,
|
|
31
|
+
typeof onRejected === "function" ? bindOneArgument(frame, onRejected, "Promise.then") : onRejected,
|
|
32
32
|
) as Promise<R1 | R2>;
|
|
33
33
|
} as Then);
|
|
34
34
|
}
|
package/src/patches/streams.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { store, type Frame } from "../core/store.js";
|
|
1
|
+
import { applyWithHooks, store, type Frame } from "../core/store.js";
|
|
2
2
|
import { claim, ownDataValue, rememberOriginal } from "./patch.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -20,19 +20,24 @@ type Method = (this : unknown, ...args : unknown[]) => unknown;
|
|
|
20
20
|
* Each method gets `underlying` as its `this` value, as the specification
|
|
21
21
|
* tells.
|
|
22
22
|
*/
|
|
23
|
-
function wrapUnderlying(underlying : object, methods : readonly string[], frame : Frame) : object {
|
|
23
|
+
function wrapUnderlying(underlying : object, methods : readonly string[], frame : Frame, name : string) : object {
|
|
24
24
|
const wrapped = Object.create(underlying) as Record<string, unknown>;
|
|
25
|
-
const
|
|
25
|
+
const properties = underlying as Record<string, unknown>;
|
|
26
26
|
for (const method of methods) {
|
|
27
|
-
const original =
|
|
27
|
+
const original = properties[method];
|
|
28
28
|
if (typeof original !== "function") {
|
|
29
29
|
continue;
|
|
30
30
|
}
|
|
31
|
-
|
|
31
|
+
const source = `${name}.${method}`;
|
|
32
|
+
wrapped[method] = function asyncBrowserContextWrapper(...args : unknown[]) : unknown {
|
|
32
33
|
const previous = store.current;
|
|
33
34
|
store.current = frame;
|
|
34
35
|
try {
|
|
35
|
-
|
|
36
|
+
const hooks = store.hooks;
|
|
37
|
+
if (hooks === undefined) {
|
|
38
|
+
return (original as Method).apply(underlying, args);
|
|
39
|
+
}
|
|
40
|
+
return applyWithHooks(hooks, original as Method, source, original as Method, underlying, args);
|
|
36
41
|
} finally {
|
|
37
42
|
store.current = previous;
|
|
38
43
|
}
|
|
@@ -57,7 +62,7 @@ export function installStreamPatches() : void {
|
|
|
57
62
|
construct(target, args : unknown[], newTarget : Function) : object {
|
|
58
63
|
const underlying = args[0];
|
|
59
64
|
if (typeof underlying === "object" && underlying !== null) {
|
|
60
|
-
args[0] = wrapUnderlying(underlying, methods, store.current);
|
|
65
|
+
args[0] = wrapUnderlying(underlying, methods, store.current, name);
|
|
61
66
|
}
|
|
62
67
|
return Reflect.construct(target, args, newTarget) as object;
|
|
63
68
|
},
|
package/src/patches/timers.ts
CHANGED
|
@@ -25,9 +25,10 @@ const PROTOTYPE_SCHEDULERS : readonly (readonly [string, string])[] = [
|
|
|
25
25
|
];
|
|
26
26
|
// Stryker restore all
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
/** `source` is the name of the scheduler, for the callback hooks. */
|
|
29
|
+
function wrapFirstArgument(source : string) : (original : Scheduler) => Scheduler {
|
|
30
|
+
return (original) => function (this : unknown, callback : unknown, ...rest : unknown[]) : unknown {
|
|
31
|
+
const wrapped = typeof callback === "function" ? bindToFrame(store.current, callback as (...args : unknown[]) => unknown, source) : callback;
|
|
31
32
|
return original.call(this, wrapped, ...rest);
|
|
32
33
|
};
|
|
33
34
|
}
|
|
@@ -42,13 +43,13 @@ export function installTimerPatches() : void {
|
|
|
42
43
|
return;
|
|
43
44
|
}
|
|
44
45
|
for (const name of GLOBAL_SCHEDULERS) {
|
|
45
|
-
replaceFunction<Scheduler>(globalThis, name, wrapFirstArgument);
|
|
46
|
+
replaceFunction<Scheduler>(globalThis, name, wrapFirstArgument(name));
|
|
46
47
|
}
|
|
47
48
|
// Stryker disable all: Node.js has none of these classes. The browser tests examine them.
|
|
48
49
|
for (const [constructorName, method] of PROTOTYPE_SCHEDULERS) {
|
|
49
50
|
const prototype = globalPrototype(constructorName);
|
|
50
51
|
if (prototype !== undefined) {
|
|
51
|
-
replaceFunction<Scheduler>(prototype, method, wrapFirstArgument);
|
|
52
|
+
replaceFunction<Scheduler>(prototype, method, wrapFirstArgument(`${constructorName}.${method}`));
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
55
|
// Stryker restore all
|