async-browser-context 0.1.1 → 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 +13 -0
- package/README.md +26 -3
- 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/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
|