@ricsam/isolate 0.1.5 → 0.1.7
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 +26 -6
- package/dist/cjs/internal/async-context/index.cjs +269 -8
- package/dist/cjs/internal/async-context/index.cjs.map +3 -3
- package/dist/cjs/internal/core/index.cjs +12 -4
- package/dist/cjs/internal/core/index.cjs.map +3 -3
- package/dist/cjs/internal/fetch/index.cjs +52 -13
- package/dist/cjs/internal/fetch/index.cjs.map +3 -3
- package/dist/cjs/internal/module-loader/bundle.cjs +111 -35
- package/dist/cjs/internal/module-loader/bundle.cjs.map +3 -3
- package/dist/cjs/internal/runtime/index.cjs +2 -2
- package/dist/cjs/internal/runtime/index.cjs.map +2 -2
- package/dist/cjs/internal/server/index.cjs +150 -19
- package/dist/cjs/internal/server/index.cjs.map +3 -3
- package/dist/cjs/internal/timers/index.cjs +39 -9
- package/dist/cjs/internal/timers/index.cjs.map +3 -3
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/server/app-server.cjs +12 -10
- package/dist/cjs/server/app-server.cjs.map +3 -3
- package/dist/mjs/internal/async-context/index.mjs +269 -8
- package/dist/mjs/internal/async-context/index.mjs.map +3 -3
- package/dist/mjs/internal/core/index.mjs +12 -4
- package/dist/mjs/internal/core/index.mjs.map +3 -3
- package/dist/mjs/internal/fetch/index.mjs +52 -13
- package/dist/mjs/internal/fetch/index.mjs.map +3 -3
- package/dist/mjs/internal/module-loader/bundle.mjs +111 -35
- package/dist/mjs/internal/module-loader/bundle.mjs.map +3 -3
- package/dist/mjs/internal/runtime/index.mjs +2 -2
- package/dist/mjs/internal/runtime/index.mjs.map +2 -2
- package/dist/mjs/internal/server/index.mjs +150 -19
- package/dist/mjs/internal/server/index.mjs.map +3 -3
- package/dist/mjs/internal/timers/index.mjs +39 -9
- package/dist/mjs/internal/timers/index.mjs.map +3 -3
- package/dist/mjs/package.json +1 -1
- package/dist/mjs/server/app-server.mjs +12 -10
- package/dist/mjs/server/app-server.mjs.map +3 -3
- package/dist/types/internal/server/index.d.ts +7 -2
- package/package.json +2 -2
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/internal/async-context/index.ts"],
|
|
4
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 ) {\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 const currentAsyncResource = new AsyncContext.Variable({\n name: \"isolate.asyncResource\",\n defaultValue: undefined,\n });\n\n const wrapCallback = (callback) => {\n if (typeof callback !== \"function\") {\n return callback;\n }\n return AsyncContext.Snapshot.wrap(callback);\n };\n\n Object.defineProperty(globalThis, \"__isolateAsyncContextInternals\", {\n configurable: true,\n enumerable: false,\n writable: false,\n value: {\n AsyncContextFrame,\n currentAsyncResource,\n wrapCallback,\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 `) 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"
|
|
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
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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";AAEA,IAAM,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eakMap for private state (similar to Blob pattern)
|
|
1678
1678
|
const _abortSignalState = new WeakMap();
|
|
1679
1679
|
const __wrapAsyncContextCallback = (callback) => (
|
|
1680
|
-
typeof callback === 'function' && globalThis.
|
|
1681
|
-
? globalThis.
|
|
1680
|
+
typeof callback === 'function' && globalThis.__isolateAsyncContextInternals?.wrapCallback
|
|
1681
|
+
? globalThis.__isolateAsyncContextInternals.wrapCallback(callback, { type: 'isolate.abort' })
|
|
1682
1682
|
: callback
|
|
1683
1683
|
);
|
|
1684
|
+
const __releaseAsyncContextCallback = (callback) => (
|
|
1685
|
+
typeof callback === 'function' && globalThis.__isolateAsyncContextInternals?.releaseCallback
|
|
1686
|
+
? globalThis.__isolateAsyncContextInternals.releaseCallback(callback)
|
|
1687
|
+
: false
|
|
1688
|
+
);
|
|
1684
1689
|
|
|
1685
1690
|
class AbortSignal {
|
|
1686
1691
|
constructor() {
|
|
@@ -1741,7 +1746,10 @@ async function injectAbortController(context) {
|
|
|
1741
1746
|
const idx = state.listeners.findIndex((entry) => (
|
|
1742
1747
|
entry.original === listener || entry.wrapped === listener
|
|
1743
1748
|
));
|
|
1744
|
-
if (idx !== -1)
|
|
1749
|
+
if (idx !== -1) {
|
|
1750
|
+
__releaseAsyncContextCallback(state.listeners[idx].wrapped);
|
|
1751
|
+
state.listeners.splice(idx, 1);
|
|
1752
|
+
}
|
|
1745
1753
|
}
|
|
1746
1754
|
}
|
|
1747
1755
|
|
|
@@ -2686,4 +2694,4 @@ export {
|
|
|
2686
2694
|
cleanupUnmarshaledHandles
|
|
2687
2695
|
};
|
|
2688
2696
|
|
|
2689
|
-
//# debugId=
|
|
2697
|
+
//# debugId=10E47F91FBBA287964756E2164756E21
|