@ricsam/isolate 0.1.4 → 0.1.6
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/README.md +45 -2
- package/dist/cjs/internal/async-context/index.cjs +401 -0
- package/dist/cjs/internal/async-context/index.cjs.map +10 -0
- package/dist/cjs/internal/client/connection.cjs +175 -123
- package/dist/cjs/internal/client/connection.cjs.map +3 -3
- package/dist/cjs/internal/console/index.cjs +2 -2
- package/dist/cjs/internal/console/index.cjs.map +2 -2
- package/dist/cjs/internal/core/index.cjs +22 -6
- package/dist/cjs/internal/core/index.cjs.map +3 -3
- package/dist/cjs/internal/crypto/index.cjs +2 -2
- package/dist/cjs/internal/crypto/index.cjs.map +2 -2
- package/dist/cjs/internal/daemon/connection.cjs +77 -12
- package/dist/cjs/internal/daemon/connection.cjs.map +3 -3
- package/dist/cjs/internal/encoding/index.cjs.map +1 -1
- package/dist/cjs/internal/fetch/index.cjs +119 -18
- package/dist/cjs/internal/fetch/index.cjs.map +3 -3
- package/dist/cjs/internal/fetch/stream-state.cjs.map +1 -1
- package/dist/cjs/internal/fs/index.cjs +2 -2
- package/dist/cjs/internal/fs/index.cjs.map +2 -2
- package/dist/cjs/internal/module-loader/bundle.cjs +277 -1
- package/dist/cjs/internal/module-loader/bundle.cjs.map +3 -3
- package/dist/cjs/internal/path/index.cjs.map +1 -1
- package/dist/cjs/internal/playwright/index.cjs +2 -2
- package/dist/cjs/internal/playwright/index.cjs.map +2 -2
- package/dist/cjs/internal/runtime/index.cjs +78 -6
- package/dist/cjs/internal/runtime/index.cjs.map +3 -3
- package/dist/cjs/internal/test-environment/index.cjs +2 -2
- package/dist/cjs/internal/test-environment/index.cjs.map +2 -2
- package/dist/cjs/internal/timers/index.cjs +42 -7
- package/dist/cjs/internal/timers/index.cjs.map +3 -3
- package/dist/cjs/internal/typecheck/isolate-types.cjs +36 -1
- package/dist/cjs/internal/typecheck/isolate-types.cjs.map +3 -3
- package/dist/cjs/package.json +1 -1
- package/dist/mjs/internal/async-context/index.mjs +361 -0
- package/dist/mjs/internal/async-context/index.mjs.map +10 -0
- package/dist/mjs/internal/client/connection.mjs +176 -123
- package/dist/mjs/internal/client/connection.mjs.map +3 -3
- package/dist/mjs/internal/console/index.mjs +2 -2
- package/dist/mjs/internal/console/index.mjs.map +2 -2
- package/dist/mjs/internal/core/index.mjs +22 -6
- package/dist/mjs/internal/core/index.mjs.map +3 -3
- package/dist/mjs/internal/crypto/index.mjs +2 -2
- package/dist/mjs/internal/crypto/index.mjs.map +2 -2
- package/dist/mjs/internal/daemon/connection.mjs +77 -12
- package/dist/mjs/internal/daemon/connection.mjs.map +3 -3
- package/dist/mjs/internal/encoding/index.mjs.map +1 -1
- package/dist/mjs/internal/fetch/index.mjs +119 -18
- package/dist/mjs/internal/fetch/index.mjs.map +3 -3
- package/dist/mjs/internal/fetch/stream-state.mjs.map +1 -1
- package/dist/mjs/internal/fs/index.mjs +2 -2
- package/dist/mjs/internal/fs/index.mjs.map +2 -2
- package/dist/mjs/internal/module-loader/bundle.mjs +277 -1
- package/dist/mjs/internal/module-loader/bundle.mjs.map +3 -3
- package/dist/mjs/internal/path/index.mjs.map +1 -1
- package/dist/mjs/internal/playwright/index.mjs +2 -2
- package/dist/mjs/internal/playwright/index.mjs.map +2 -2
- package/dist/mjs/internal/runtime/index.mjs +78 -6
- package/dist/mjs/internal/runtime/index.mjs.map +3 -3
- package/dist/mjs/internal/test-environment/index.mjs +2 -2
- package/dist/mjs/internal/test-environment/index.mjs.map +2 -2
- package/dist/mjs/internal/timers/index.mjs +42 -7
- package/dist/mjs/internal/timers/index.mjs.map +3 -3
- package/dist/mjs/internal/typecheck/isolate-types.mjs +36 -1
- package/dist/mjs/internal/typecheck/isolate-types.mjs.map +3 -3
- package/dist/mjs/package.json +1 -1
- package/dist/types/internal/async-context/index.d.ts +5 -0
- package/dist/types/internal/console/index.d.ts +1 -1
- package/dist/types/internal/core/index.d.ts +2 -2
- package/dist/types/internal/crypto/index.d.ts +1 -1
- package/dist/types/internal/daemon/types.d.ts +1 -0
- package/dist/types/internal/encoding/index.d.ts +1 -1
- package/dist/types/internal/fetch/index.d.ts +1 -1
- package/dist/types/internal/fetch/stream-state.d.ts +1 -1
- package/dist/types/internal/fs/index.d.ts +1 -1
- package/dist/types/internal/path/index.d.ts +1 -1
- package/dist/types/internal/playwright/index.d.ts +1 -1
- package/dist/types/internal/test-environment/index.d.ts +1 -1
- package/dist/types/internal/timers/index.d.ts +1 -1
- package/dist/types/internal/typecheck/isolate-types.d.ts +2 -2
- package/package.json +8 -3
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
// src/internal/async-context/index.ts
|
|
2
|
+
var ASYNC_CONTEXT_BOOTSTRAP = `
|
|
3
|
+
(function() {
|
|
4
|
+
if (globalThis.__isolateAsyncContextInternals) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const AsyncContext = globalThis.AsyncContext;
|
|
9
|
+
const native = globalThis.__ivmAsyncContextInternal;
|
|
10
|
+
if (
|
|
11
|
+
!AsyncContext
|
|
12
|
+
|| typeof AsyncContext.Variable !== "function"
|
|
13
|
+
|| typeof AsyncContext.Snapshot !== "function"
|
|
14
|
+
|| !native
|
|
15
|
+
|| typeof native.getContinuationPreservedEmbedderData !== "function"
|
|
16
|
+
|| typeof native.setContinuationPreservedEmbedderData !== "function"
|
|
17
|
+
|| typeof native.setPromiseHooks !== "function"
|
|
18
|
+
) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"The installed isolated-vm runtime does not expose async context support. " +
|
|
21
|
+
"Install the async-context-enabled isolate engine build."
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class AsyncContextFrame extends Map {
|
|
26
|
+
constructor(store, value) {
|
|
27
|
+
super(AsyncContextFrame.current() ?? undefined);
|
|
28
|
+
if (arguments.length > 0) {
|
|
29
|
+
this.set(store, value);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static current() {
|
|
34
|
+
return native.getContinuationPreservedEmbedderData();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static set(frame) {
|
|
38
|
+
native.setContinuationPreservedEmbedderData(frame);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static exchange(frame) {
|
|
42
|
+
const prior = this.current();
|
|
43
|
+
this.set(frame);
|
|
44
|
+
return prior;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static disable(store) {
|
|
48
|
+
const frame = this.current();
|
|
49
|
+
frame?.delete(store);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
Object.defineProperty(AsyncContextFrame, "enabled", {
|
|
54
|
+
configurable: true,
|
|
55
|
+
enumerable: false,
|
|
56
|
+
value: true,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const topLevelResource = {};
|
|
60
|
+
const topLevelExecutionState = {
|
|
61
|
+
asyncId: 1,
|
|
62
|
+
triggerAsyncId: 0,
|
|
63
|
+
type: "ROOT",
|
|
64
|
+
resource: topLevelResource,
|
|
65
|
+
destroyed: false,
|
|
66
|
+
};
|
|
67
|
+
const currentExecutionState = new AsyncContext.Variable({
|
|
68
|
+
name: "isolate.executionState",
|
|
69
|
+
defaultValue: topLevelExecutionState,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const promiseStateByPromise = new WeakMap();
|
|
73
|
+
const activeHooks = new Map();
|
|
74
|
+
const promiseFrameStack = [];
|
|
75
|
+
const kWrappedState = Symbol("isolate.asyncResourceState");
|
|
76
|
+
const kWrappedDestroy = Symbol("isolate.destroyAsyncResource");
|
|
77
|
+
let nextAsyncId = 2;
|
|
78
|
+
let hookDispatchDepth = 0;
|
|
79
|
+
let promiseHooksEnabled = false;
|
|
80
|
+
|
|
81
|
+
function getCurrentExecutionState() {
|
|
82
|
+
return currentExecutionState.get();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function normalizeType(type, fallback) {
|
|
86
|
+
if (typeof type === "string" && type.length > 0) {
|
|
87
|
+
return type;
|
|
88
|
+
}
|
|
89
|
+
return fallback;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function normalizeTriggerAsyncId(triggerAsyncId) {
|
|
93
|
+
return Number.isSafeInteger(triggerAsyncId) && triggerAsyncId >= 0
|
|
94
|
+
? triggerAsyncId
|
|
95
|
+
: undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function dispatchHook(name, args) {
|
|
99
|
+
if (hookDispatchDepth > 0 || activeHooks.size === 0) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
hookDispatchDepth++;
|
|
104
|
+
try {
|
|
105
|
+
for (const [hook, callbacks] of Array.from(activeHooks.entries())) {
|
|
106
|
+
const callback = callbacks[name];
|
|
107
|
+
if (typeof callback === "function") {
|
|
108
|
+
Reflect.apply(callback, hook, args);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
} finally {
|
|
112
|
+
hookDispatchDepth--;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function createResource(type, resource, options = {}) {
|
|
117
|
+
const normalizedOptions =
|
|
118
|
+
options && typeof options === "object" ? options : {};
|
|
119
|
+
const state = {
|
|
120
|
+
asyncId: nextAsyncId++,
|
|
121
|
+
triggerAsyncId:
|
|
122
|
+
normalizeTriggerAsyncId(normalizedOptions.triggerAsyncId)
|
|
123
|
+
?? getCurrentExecutionState().asyncId,
|
|
124
|
+
type: normalizeType(type, "isolate.resource"),
|
|
125
|
+
resource:
|
|
126
|
+
resource !== undefined && resource !== null ? resource : {},
|
|
127
|
+
destroyed: false,
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
if (normalizedOptions.emitInit !== false) {
|
|
131
|
+
dispatchHook("init", [
|
|
132
|
+
state.asyncId,
|
|
133
|
+
state.type,
|
|
134
|
+
state.triggerAsyncId,
|
|
135
|
+
state.resource,
|
|
136
|
+
]);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return state;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function enterResource(resourceState) {
|
|
143
|
+
return AsyncContextFrame.exchange(
|
|
144
|
+
new AsyncContextFrame(currentExecutionState, resourceState),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function destroyResource(resourceState) {
|
|
149
|
+
if (!resourceState || resourceState.destroyed) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
resourceState.destroyed = true;
|
|
153
|
+
dispatchHook("destroy", [resourceState.asyncId]);
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function runWithResource(resourceState, fn, thisArg, args) {
|
|
158
|
+
const priorFrame = enterResource(resourceState);
|
|
159
|
+
let didRunBeforeHook = false;
|
|
160
|
+
try {
|
|
161
|
+
dispatchHook("before", [resourceState.asyncId]);
|
|
162
|
+
didRunBeforeHook = true;
|
|
163
|
+
return Reflect.apply(fn, thisArg, args);
|
|
164
|
+
} finally {
|
|
165
|
+
try {
|
|
166
|
+
if (didRunBeforeHook) {
|
|
167
|
+
dispatchHook("after", [resourceState.asyncId]);
|
|
168
|
+
}
|
|
169
|
+
} finally {
|
|
170
|
+
AsyncContextFrame.set(priorFrame);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function wrapCallback(callback, options = {}) {
|
|
176
|
+
if (typeof callback !== "function") {
|
|
177
|
+
return callback;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const normalizedOptions =
|
|
181
|
+
options && typeof options === "object" ? options : {};
|
|
182
|
+
const snapshot = new AsyncContext.Snapshot();
|
|
183
|
+
const resourceState = createResource(
|
|
184
|
+
normalizedOptions.type,
|
|
185
|
+
normalizedOptions.resource,
|
|
186
|
+
normalizedOptions,
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
function wrapped(...args) {
|
|
190
|
+
const thisArg = normalizedOptions.thisArg === undefined
|
|
191
|
+
? this
|
|
192
|
+
: normalizedOptions.thisArg;
|
|
193
|
+
return snapshot.run(
|
|
194
|
+
() => runWithResource(resourceState, callback, thisArg, args),
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
try {
|
|
199
|
+
Object.defineProperty(wrapped, "name", {
|
|
200
|
+
configurable: true,
|
|
201
|
+
value: callback.name ? "wrapped " + callback.name : "wrapped",
|
|
202
|
+
});
|
|
203
|
+
} catch {}
|
|
204
|
+
|
|
205
|
+
Object.defineProperty(wrapped, kWrappedState, {
|
|
206
|
+
configurable: false,
|
|
207
|
+
enumerable: false,
|
|
208
|
+
value: resourceState,
|
|
209
|
+
writable: false,
|
|
210
|
+
});
|
|
211
|
+
Object.defineProperty(wrapped, kWrappedDestroy, {
|
|
212
|
+
configurable: false,
|
|
213
|
+
enumerable: false,
|
|
214
|
+
value: () => destroyResource(resourceState),
|
|
215
|
+
writable: false,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
return wrapped;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function releaseCallback(callback) {
|
|
222
|
+
if (typeof callback !== "function") {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
const destroy = callback[kWrappedDestroy];
|
|
226
|
+
if (typeof destroy === "function") {
|
|
227
|
+
return destroy();
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function onPromiseInit(promise, parentPromise) {
|
|
233
|
+
const parentState = (
|
|
234
|
+
parentPromise && typeof parentPromise === "object"
|
|
235
|
+
? promiseStateByPromise.get(parentPromise)
|
|
236
|
+
: undefined
|
|
237
|
+
);
|
|
238
|
+
const promiseState = createResource("PROMISE", promise, {
|
|
239
|
+
triggerAsyncId: parentState?.asyncId ?? getCurrentExecutionState().asyncId,
|
|
240
|
+
});
|
|
241
|
+
promiseStateByPromise.set(promise, promiseState);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function onPromiseBefore(promise) {
|
|
245
|
+
const promiseState = promiseStateByPromise.get(promise);
|
|
246
|
+
if (!promiseState) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
const priorFrame = enterResource(promiseState);
|
|
250
|
+
promiseFrameStack.push(priorFrame);
|
|
251
|
+
try {
|
|
252
|
+
dispatchHook("before", [promiseState.asyncId]);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
promiseFrameStack.pop();
|
|
255
|
+
AsyncContextFrame.set(priorFrame);
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function onPromiseAfter(promise) {
|
|
261
|
+
const promiseState = promiseStateByPromise.get(promise);
|
|
262
|
+
if (!promiseState) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const priorFrame = promiseFrameStack.pop();
|
|
266
|
+
try {
|
|
267
|
+
dispatchHook("after", [promiseState.asyncId]);
|
|
268
|
+
} finally {
|
|
269
|
+
AsyncContextFrame.set(priorFrame);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function onPromiseResolve(promise) {
|
|
274
|
+
const promiseState = promiseStateByPromise.get(promise);
|
|
275
|
+
if (!promiseState) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
dispatchHook("promiseResolve", [promiseState.asyncId]);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function refreshPromiseHooks() {
|
|
282
|
+
if (activeHooks.size > 0) {
|
|
283
|
+
if (!promiseHooksEnabled) {
|
|
284
|
+
native.setPromiseHooks(
|
|
285
|
+
onPromiseInit,
|
|
286
|
+
onPromiseBefore,
|
|
287
|
+
onPromiseAfter,
|
|
288
|
+
onPromiseResolve,
|
|
289
|
+
);
|
|
290
|
+
promiseHooksEnabled = true;
|
|
291
|
+
}
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (promiseHooksEnabled) {
|
|
296
|
+
native.setPromiseHooks(undefined, undefined, undefined, undefined);
|
|
297
|
+
promiseFrameStack.length = 0;
|
|
298
|
+
promiseHooksEnabled = false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function enableHook(hook, callbacks) {
|
|
303
|
+
activeHooks.set(hook, callbacks);
|
|
304
|
+
refreshPromiseHooks();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function disableHook(hook) {
|
|
308
|
+
activeHooks.delete(hook);
|
|
309
|
+
refreshPromiseHooks();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
Object.defineProperty(globalThis, "__isolateAsyncContextInternals", {
|
|
313
|
+
configurable: true,
|
|
314
|
+
enumerable: false,
|
|
315
|
+
writable: false,
|
|
316
|
+
value: {
|
|
317
|
+
AsyncContextFrame,
|
|
318
|
+
topLevelExecutionState,
|
|
319
|
+
currentExecutionState,
|
|
320
|
+
getCurrentExecutionState,
|
|
321
|
+
executionAsyncId() {
|
|
322
|
+
return getCurrentExecutionState().asyncId;
|
|
323
|
+
},
|
|
324
|
+
triggerAsyncId() {
|
|
325
|
+
return getCurrentExecutionState().triggerAsyncId;
|
|
326
|
+
},
|
|
327
|
+
executionAsyncResource() {
|
|
328
|
+
return getCurrentExecutionState().resource;
|
|
329
|
+
},
|
|
330
|
+
createResource,
|
|
331
|
+
runWithResource,
|
|
332
|
+
destroyResource,
|
|
333
|
+
wrapCallback,
|
|
334
|
+
releaseCallback,
|
|
335
|
+
enableHook,
|
|
336
|
+
disableHook,
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
})();
|
|
340
|
+
`;
|
|
341
|
+
async function setupAsyncContext(context) {
|
|
342
|
+
const supported = context.evalSync(`
|
|
343
|
+
typeof globalThis.AsyncContext === "object"
|
|
344
|
+
&& typeof globalThis.AsyncContext?.Variable === "function"
|
|
345
|
+
&& typeof globalThis.AsyncContext?.Snapshot === "function"
|
|
346
|
+
&& typeof globalThis.__ivmAsyncContextInternal === "object"
|
|
347
|
+
&& typeof globalThis.__ivmAsyncContextInternal?.getContinuationPreservedEmbedderData === "function"
|
|
348
|
+
&& typeof globalThis.__ivmAsyncContextInternal?.setContinuationPreservedEmbedderData === "function"
|
|
349
|
+
&& typeof globalThis.__ivmAsyncContextInternal?.setPromiseHooks === "function"
|
|
350
|
+
`);
|
|
351
|
+
if (!supported) {
|
|
352
|
+
throw new Error("The installed isolated-vm runtime does not support AsyncContext. " + "Use the async-context-enabled isolate engine build.");
|
|
353
|
+
}
|
|
354
|
+
context.evalSync(ASYNC_CONTEXT_BOOTSTRAP);
|
|
355
|
+
return { supported };
|
|
356
|
+
}
|
|
357
|
+
export {
|
|
358
|
+
setupAsyncContext
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
//# debugId=8F09A2B6E55A095964756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/internal/async-context/index.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type ivm from \"@ricsam/isolated-vm\";\n\nconst ASYNC_CONTEXT_BOOTSTRAP = `\n(function() {\n if (globalThis.__isolateAsyncContextInternals) {\n return;\n }\n\n const AsyncContext = globalThis.AsyncContext;\n const native = globalThis.__ivmAsyncContextInternal;\n if (\n !AsyncContext\n || typeof AsyncContext.Variable !== \"function\"\n || typeof AsyncContext.Snapshot !== \"function\"\n || !native\n || typeof native.getContinuationPreservedEmbedderData !== \"function\"\n || typeof native.setContinuationPreservedEmbedderData !== \"function\"\n || typeof native.setPromiseHooks !== \"function\"\n ) {\n throw new Error(\n \"The installed isolated-vm runtime does not expose async context support. \" +\n \"Install the async-context-enabled isolate engine build.\"\n );\n }\n\n class AsyncContextFrame extends Map {\n constructor(store, value) {\n super(AsyncContextFrame.current() ?? undefined);\n if (arguments.length > 0) {\n this.set(store, value);\n }\n }\n\n static current() {\n return native.getContinuationPreservedEmbedderData();\n }\n\n static set(frame) {\n native.setContinuationPreservedEmbedderData(frame);\n }\n\n static exchange(frame) {\n const prior = this.current();\n this.set(frame);\n return prior;\n }\n\n static disable(store) {\n const frame = this.current();\n frame?.delete(store);\n }\n }\n\n Object.defineProperty(AsyncContextFrame, \"enabled\", {\n configurable: true,\n enumerable: false,\n value: true,\n });\n\n const topLevelResource = {};\n const topLevelExecutionState = {\n asyncId: 1,\n triggerAsyncId: 0,\n type: \"ROOT\",\n resource: topLevelResource,\n destroyed: false,\n };\n const currentExecutionState = new AsyncContext.Variable({\n name: \"isolate.executionState\",\n defaultValue: topLevelExecutionState,\n });\n\n const promiseStateByPromise = new WeakMap();\n const activeHooks = new Map();\n const promiseFrameStack = [];\n const kWrappedState = Symbol(\"isolate.asyncResourceState\");\n const kWrappedDestroy = Symbol(\"isolate.destroyAsyncResource\");\n let nextAsyncId = 2;\n let hookDispatchDepth = 0;\n let promiseHooksEnabled = false;\n\n function getCurrentExecutionState() {\n return currentExecutionState.get();\n }\n\n function normalizeType(type, fallback) {\n if (typeof type === \"string\" && type.length > 0) {\n return type;\n }\n return fallback;\n }\n\n function normalizeTriggerAsyncId(triggerAsyncId) {\n return Number.isSafeInteger(triggerAsyncId) && triggerAsyncId >= 0\n ? triggerAsyncId\n : undefined;\n }\n\n function dispatchHook(name, args) {\n if (hookDispatchDepth > 0 || activeHooks.size === 0) {\n return;\n }\n\n hookDispatchDepth++;\n try {\n for (const [hook, callbacks] of Array.from(activeHooks.entries())) {\n const callback = callbacks[name];\n if (typeof callback === \"function\") {\n Reflect.apply(callback, hook, args);\n }\n }\n } finally {\n hookDispatchDepth--;\n }\n }\n\n function createResource(type, resource, options = {}) {\n const normalizedOptions =\n options && typeof options === \"object\" ? options : {};\n const state = {\n asyncId: nextAsyncId++,\n triggerAsyncId:\n normalizeTriggerAsyncId(normalizedOptions.triggerAsyncId)\n ?? getCurrentExecutionState().asyncId,\n type: normalizeType(type, \"isolate.resource\"),\n resource:\n resource !== undefined && resource !== null ? resource : {},\n destroyed: false,\n };\n\n if (normalizedOptions.emitInit !== false) {\n dispatchHook(\"init\", [\n state.asyncId,\n state.type,\n state.triggerAsyncId,\n state.resource,\n ]);\n }\n\n return state;\n }\n\n function enterResource(resourceState) {\n return AsyncContextFrame.exchange(\n new AsyncContextFrame(currentExecutionState, resourceState),\n );\n }\n\n function destroyResource(resourceState) {\n if (!resourceState || resourceState.destroyed) {\n return false;\n }\n resourceState.destroyed = true;\n dispatchHook(\"destroy\", [resourceState.asyncId]);\n return true;\n }\n\n function runWithResource(resourceState, fn, thisArg, args) {\n const priorFrame = enterResource(resourceState);\n let didRunBeforeHook = false;\n try {\n dispatchHook(\"before\", [resourceState.asyncId]);\n didRunBeforeHook = true;\n return Reflect.apply(fn, thisArg, args);\n } finally {\n try {\n if (didRunBeforeHook) {\n dispatchHook(\"after\", [resourceState.asyncId]);\n }\n } finally {\n AsyncContextFrame.set(priorFrame);\n }\n }\n }\n\n function wrapCallback(callback, options = {}) {\n if (typeof callback !== \"function\") {\n return callback;\n }\n\n const normalizedOptions =\n options && typeof options === \"object\" ? options : {};\n const snapshot = new AsyncContext.Snapshot();\n const resourceState = createResource(\n normalizedOptions.type,\n normalizedOptions.resource,\n normalizedOptions,\n );\n\n function wrapped(...args) {\n const thisArg = normalizedOptions.thisArg === undefined\n ? this\n : normalizedOptions.thisArg;\n return snapshot.run(\n () => runWithResource(resourceState, callback, thisArg, args),\n );\n }\n\n try {\n Object.defineProperty(wrapped, \"name\", {\n configurable: true,\n value: callback.name ? \"wrapped \" + callback.name : \"wrapped\",\n });\n } catch {}\n\n Object.defineProperty(wrapped, kWrappedState, {\n configurable: false,\n enumerable: false,\n value: resourceState,\n writable: false,\n });\n Object.defineProperty(wrapped, kWrappedDestroy, {\n configurable: false,\n enumerable: false,\n value: () => destroyResource(resourceState),\n writable: false,\n });\n\n return wrapped;\n }\n\n function releaseCallback(callback) {\n if (typeof callback !== \"function\") {\n return false;\n }\n const destroy = callback[kWrappedDestroy];\n if (typeof destroy === \"function\") {\n return destroy();\n }\n return false;\n }\n\n function onPromiseInit(promise, parentPromise) {\n const parentState = (\n parentPromise && typeof parentPromise === \"object\"\n ? promiseStateByPromise.get(parentPromise)\n : undefined\n );\n const promiseState = createResource(\"PROMISE\", promise, {\n triggerAsyncId: parentState?.asyncId ?? getCurrentExecutionState().asyncId,\n });\n promiseStateByPromise.set(promise, promiseState);\n }\n\n function onPromiseBefore(promise) {\n const promiseState = promiseStateByPromise.get(promise);\n if (!promiseState) {\n return;\n }\n const priorFrame = enterResource(promiseState);\n promiseFrameStack.push(priorFrame);\n try {\n dispatchHook(\"before\", [promiseState.asyncId]);\n } catch (error) {\n promiseFrameStack.pop();\n AsyncContextFrame.set(priorFrame);\n throw error;\n }\n }\n\n function onPromiseAfter(promise) {\n const promiseState = promiseStateByPromise.get(promise);\n if (!promiseState) {\n return;\n }\n const priorFrame = promiseFrameStack.pop();\n try {\n dispatchHook(\"after\", [promiseState.asyncId]);\n } finally {\n AsyncContextFrame.set(priorFrame);\n }\n }\n\n function onPromiseResolve(promise) {\n const promiseState = promiseStateByPromise.get(promise);\n if (!promiseState) {\n return;\n }\n dispatchHook(\"promiseResolve\", [promiseState.asyncId]);\n }\n\n function refreshPromiseHooks() {\n if (activeHooks.size > 0) {\n if (!promiseHooksEnabled) {\n native.setPromiseHooks(\n onPromiseInit,\n onPromiseBefore,\n onPromiseAfter,\n onPromiseResolve,\n );\n promiseHooksEnabled = true;\n }\n return;\n }\n\n if (promiseHooksEnabled) {\n native.setPromiseHooks(undefined, undefined, undefined, undefined);\n promiseFrameStack.length = 0;\n promiseHooksEnabled = false;\n }\n }\n\n function enableHook(hook, callbacks) {\n activeHooks.set(hook, callbacks);\n refreshPromiseHooks();\n }\n\n function disableHook(hook) {\n activeHooks.delete(hook);\n refreshPromiseHooks();\n }\n\n Object.defineProperty(globalThis, \"__isolateAsyncContextInternals\", {\n configurable: true,\n enumerable: false,\n writable: false,\n value: {\n AsyncContextFrame,\n topLevelExecutionState,\n currentExecutionState,\n getCurrentExecutionState,\n executionAsyncId() {\n return getCurrentExecutionState().asyncId;\n },\n triggerAsyncId() {\n return getCurrentExecutionState().triggerAsyncId;\n },\n executionAsyncResource() {\n return getCurrentExecutionState().resource;\n },\n createResource,\n runWithResource,\n destroyResource,\n wrapCallback,\n releaseCallback,\n enableHook,\n disableHook,\n },\n });\n})();\n`;\n\nexport interface AsyncContextHandle {\n supported: boolean;\n}\n\nexport async function setupAsyncContext(context: ivm.Context): Promise<AsyncContextHandle> {\n const supported = context.evalSync(`\n typeof globalThis.AsyncContext === \"object\"\n && typeof globalThis.AsyncContext?.Variable === \"function\"\n && typeof globalThis.AsyncContext?.Snapshot === \"function\"\n && typeof globalThis.__ivmAsyncContextInternal === \"object\"\n && typeof globalThis.__ivmAsyncContextInternal?.getContinuationPreservedEmbedderData === \"function\"\n && typeof globalThis.__ivmAsyncContextInternal?.setContinuationPreservedEmbedderData === \"function\"\n && typeof globalThis.__ivmAsyncContextInternal?.setPromiseHooks === \"function\"\n `) as boolean;\n\n if (!supported) {\n throw new Error(\n \"The installed isolated-vm runtime does not support AsyncContext. \" +\n \"Use the async-context-enabled isolate engine build.\"\n );\n }\n\n context.evalSync(ASYNC_CONTEXT_BOOTSTRAP);\n return { supported };\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";AAEA,IAAM,0BAA0B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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