@mcp-b/do-runtime 0.3.5 → 0.3.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/CHANGELOG.md +6 -0
- package/dist/gate.js +30 -6
- package/dist/gate.js.map +1 -1
- package/dist/src/gate.d.ts +4 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/gate.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import { l as tryCurrentContinuation, u as tryCurrentIoContext } from "./chunks/io-context-RmmjNtwm.js";
|
|
1
|
+
import { l as tryCurrentContinuation, r as atCheckpointEnd, u as tryCurrentIoContext } from "./chunks/io-context-RmmjNtwm.js";
|
|
2
2
|
//#region src/gate.ts
|
|
3
3
|
var TRANSFORMED_AWAIT = Symbol("@mcp-b/do-runtime/transformed-await");
|
|
4
|
+
/**
|
|
5
|
+
* Own the gap between publishing an await result and its `__resumeAwait` call.
|
|
6
|
+
* This is deliberately separate from the current-continuation ambient: a
|
|
7
|
+
* reservation serializes publishers but must never make its actor look current.
|
|
8
|
+
* See §2.3 and decision 8.
|
|
9
|
+
*/
|
|
10
|
+
var CURRENT_PUBLICATION = Symbol.for("@mcp-b/do-runtime/current-await-publication");
|
|
4
11
|
var warnedUngatedAwaits = /* @__PURE__ */ new Set();
|
|
5
12
|
function isThenable(value) {
|
|
6
13
|
return (typeof value === "object" && value !== null || typeof value === "function") && typeof Reflect.get(value, "then") === "function";
|
|
@@ -27,6 +34,7 @@ function __gateAwait(value, developmentSource) {
|
|
|
27
34
|
/** Restore the captured actor at the first instruction after a transformed await. */
|
|
28
35
|
function __resumeAwait(value) {
|
|
29
36
|
if (!isTransformedAwait(value)) return value;
|
|
37
|
+
clearPublication(value.reservation);
|
|
30
38
|
value.context.restoreContinuation();
|
|
31
39
|
if (value.outcome.ok) return value.outcome.value;
|
|
32
40
|
throw value.outcome.exception;
|
|
@@ -34,17 +42,31 @@ function __resumeAwait(value) {
|
|
|
34
42
|
function isTransformedAwait(value) {
|
|
35
43
|
return Reflect.get(Object(value), TRANSFORMED_AWAIT) === true;
|
|
36
44
|
}
|
|
45
|
+
function currentPublication() {
|
|
46
|
+
return Reflect.get(globalThis, CURRENT_PUBLICATION);
|
|
47
|
+
}
|
|
48
|
+
function reservePublication(context) {
|
|
49
|
+
if (tryCurrentContinuation() !== void 0 || currentPublication() !== void 0) return void 0;
|
|
50
|
+
const reservation = { context };
|
|
51
|
+
Reflect.set(globalThis, CURRENT_PUBLICATION, reservation);
|
|
52
|
+
atCheckpointEnd(() => clearPublication(reservation));
|
|
53
|
+
return reservation;
|
|
54
|
+
}
|
|
55
|
+
function clearPublication(reservation) {
|
|
56
|
+
if (currentPublication() === reservation) Reflect.deleteProperty(globalThis, CURRENT_PUBLICATION);
|
|
57
|
+
}
|
|
37
58
|
function publishOutcome(context, promise, finish) {
|
|
38
59
|
return new Promise((resolve, reject) => {
|
|
39
60
|
const publish = context.makeTransformReentryCallback((outcome) => {
|
|
40
|
-
|
|
61
|
+
const reservation = reservePublication(context);
|
|
62
|
+
if (reservation === void 0) {
|
|
41
63
|
schedulePublication({
|
|
42
64
|
publish: () => publish(outcome),
|
|
43
65
|
reject
|
|
44
66
|
});
|
|
45
67
|
return;
|
|
46
68
|
}
|
|
47
|
-
resolve(finish(outcome));
|
|
69
|
+
resolve(finish(outcome, reservation));
|
|
48
70
|
});
|
|
49
71
|
promise.then((value) => {
|
|
50
72
|
schedulePublication({
|
|
@@ -66,14 +88,16 @@ function publishOutcome(context, promise, finish) {
|
|
|
66
88
|
});
|
|
67
89
|
}
|
|
68
90
|
function resumeAwaitWithContext(context, promise) {
|
|
69
|
-
return publishOutcome(context, promise, (outcome) => ({
|
|
91
|
+
return publishOutcome(context, promise, (outcome, reservation) => ({
|
|
70
92
|
[TRANSFORMED_AWAIT]: true,
|
|
71
93
|
context,
|
|
72
|
-
outcome
|
|
94
|
+
outcome,
|
|
95
|
+
reservation
|
|
73
96
|
}));
|
|
74
97
|
}
|
|
75
98
|
function resumeWithContext(context, promise) {
|
|
76
|
-
return publishOutcome(context, promise, (outcome) => {
|
|
99
|
+
return publishOutcome(context, promise, (outcome, reservation) => {
|
|
100
|
+
clearPublication(reservation);
|
|
77
101
|
context.restoreContinuation();
|
|
78
102
|
if (outcome.ok) return outcome.value;
|
|
79
103
|
throw outcome.exception;
|
package/dist/gate.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gate.js","names":[],"sources":["../src/gate.ts"],"sourcesContent":["/* @do-runtime-gated */\n\nimport {\n tryCurrentContinuation,\n tryCurrentIoContext,\n type IoContext,\n} from \"./io/io-context\";\n\ntype Publication = {\n readonly publish: () => Promise<void>;\n readonly reject: (exception: unknown) => void;\n};\n\ntype Outcome<T> =\n | { readonly ok: true; readonly value: T }\n | { readonly ok: false; readonly exception: unknown };\n\nconst TRANSFORMED_AWAIT = Symbol(\"@mcp-b/do-runtime/transformed-await\");\nconst warnedUngatedAwaits = new Set<string>();\n\ntype TransformedAwait<T> = {\n readonly [TRANSFORMED_AWAIT]: true;\n readonly context: IoContext;\n readonly outcome: Outcome<T>;\n};\n\nfunction isThenable(value: unknown): value is PromiseLike<unknown> {\n return (\n (typeof value === \"object\" && value !== null) ||\n typeof value === \"function\"\n ) && typeof Reflect.get(value, \"then\") === \"function\";\n}\n\n/** Re-enter the actor that owns this transformed await; fail open outside actors. */\nexport function __gate<T>(value: T): T | Promise<Awaited<T>> {\n const context = tryCurrentIoContext();\n if (!isThenable(value) && context === undefined) return value;\n if (context === undefined) return value;\n return resumeWithContext(context, Promise.resolve(value));\n}\n\n/** Capture an actor await without publishing its context before the continuation runs. */\nexport function __gateAwait<T>(\n value: T,\n developmentSource?: string,\n): T | Promise<TransformedAwait<Awaited<T>>> {\n const context = tryCurrentIoContext();\n if (context === undefined) {\n if (developmentSource !== undefined && !warnedUngatedAwaits.has(developmentSource)) {\n warnedUngatedAwaits.add(developmentSource);\n console.warn(\n `do-runtime: transformed await in ${developmentSource} ran without an actor input lock; ` +\n \"an earlier await or entry path is not gated\",\n );\n }\n return value;\n }\n return resumeAwaitWithContext(context, Promise.resolve(value));\n}\n\n/** Restore the captured actor at the first instruction after a transformed await. */\nexport function __resumeAwait<T>(value: T | TransformedAwait<T>): T {\n if (!isTransformedAwait(value)) return value as T;\n\n value.context.restoreContinuation();\n if (value.outcome.ok) return value.outcome.value;\n throw value.outcome.exception;\n}\n\nfunction isTransformedAwait<T>(value: T | TransformedAwait<T>): value is TransformedAwait<T> {\n return Reflect.get(Object(value), TRANSFORMED_AWAIT) === true;\n}\n\nfunction publishOutcome<T, Result>(\n context: IoContext,\n promise: Promise<T>,\n finish: (outcome: Outcome<T>) => Result,\n): Promise<Result> {\n return new Promise<Result>((resolve, reject) => {\n const publish = context.makeTransformReentryCallback((outcome: Outcome<T>) => {\n if (tryCurrentContinuation() !== undefined) {\n schedulePublication({ publish: () => publish(outcome), reject });\n return;\n }\n resolve(finish(outcome));\n });\n void promise.then(\n (value) => {\n schedulePublication({ publish: () => publish({ ok: true, value }), reject });\n },\n (exception: unknown) => {\n schedulePublication({ publish: () => publish({ ok: false, exception }), reject });\n },\n );\n });\n}\n\nfunction resumeAwaitWithContext<T>(\n context: IoContext,\n promise: Promise<T>,\n): Promise<TransformedAwait<T>> {\n return publishOutcome(context, promise, (outcome) => ({\n [TRANSFORMED_AWAIT]: true,\n context,\n outcome,\n }));\n}\n\nfunction resumeWithContext<T>(context: IoContext, promise: Promise<T>): Promise<T> {\n return publishOutcome(context, promise, (outcome) => {\n context.restoreContinuation();\n if (outcome.ok) return outcome.value;\n throw outcome.exception;\n });\n}\n\n/**\n * Resolve one transformed await per task, inside a fresh actor slice. Admission\n * attempts are independent so a blocked actor cannot stall the actor that will\n * unblock it. The task boundary keeps each continuation ambient isolated.\n */\nfunction schedulePublication(publication: Publication): void {\n const channel = new MessageChannel();\n channel.port1.onmessage = () => {\n channel.port1.close();\n channel.port2.close();\n\n void publication.publish().catch((exception: unknown) => {\n publication.reject(exception);\n });\n };\n channel.port2.postMessage(undefined);\n}\n\nfunction iteratorFor<T>(iterable: AsyncIterable<T> | Iterable<T>): AsyncIterator<T> | Iterator<T> {\n const subject = Object(iterable);\n const asyncIterator: unknown = Reflect.get(subject, Symbol.asyncIterator);\n if (typeof asyncIterator === \"function\") return Reflect.apply(asyncIterator, iterable, []);\n const iterator: unknown = Reflect.get(subject, Symbol.iterator);\n if (typeof iterator === \"function\") return Reflect.apply(iterator, iterable, []);\n throw new TypeError(\"value is not async iterable or iterable\");\n}\n\nfunction gatedIterator<T>(iterable: AsyncIterable<T> | Iterable<T>): AsyncIterator<T> {\n const iterator = iteratorFor(iterable);\n\n function invoke(methodName: \"next\" | \"return\" | \"throw\", args: unknown[]): Promise<IteratorResult<T>> {\n const method: unknown = Reflect.get(iterator, methodName);\n if (typeof method === \"function\") {\n return Promise.resolve(__gate(Reflect.apply(method, iterator, args)));\n }\n if (methodName === \"throw\") return Promise.reject(args[0]);\n return Promise.resolve({ done: true, value: args[0] });\n }\n\n return {\n next: (...args: [] | [unknown]) => invoke(\"next\", args),\n return: (value?: unknown) => invoke(\"return\", [value]),\n throw: (exception?: unknown) => invoke(\"throw\", [exception]),\n };\n}\n\n/** Gate every operation used by `for await`, including early return and throw. */\nexport function __gateAsyncIterable<T, IterableType extends AsyncIterable<T> | Iterable<T>>(\n iterable: IterableType,\n): IterableType;\nexport function __gateAsyncIterable<T>(\n iterable: AsyncIterable<T> | Iterable<T>,\n): AsyncIterable<T> | Iterable<T> {\n const wrapper: AsyncIterable<T> = {\n [Symbol.asyncIterator]: () => gatedIterator(iterable),\n };\n if ((typeof iterable !== \"object\" || iterable === null) && typeof iterable !== \"function\") {\n return wrapper;\n }\n return new Proxy(iterable, {\n get(target, property, receiver): unknown {\n if (property === Symbol.asyncIterator) return wrapper[Symbol.asyncIterator];\n return Reflect.get(target, property, receiver);\n },\n });\n}\n"],"mappings":";;AAiBA,IAAM,oBAAoB,OAAO,qCAAqC;A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SAAkB,QAAQ,IAAI,UAAU,UAAU;EACxD,IAAI,OAAO,WAAW,YACpB,OAAO,QAAQ,QAAQ,OAAO,QAAQ,MAAM,QAAQ,UAAU,IAAI,CAAC,CAAC;EAEtE,IAAI,eAAe,SAAS,OAAO,QAAQ,OAAO,KAAK,EAAE;EACzD,OAAO,QAAQ,QAAQ;GAAE,MAAM;GAAM,OAAO,KAAK;EAAG,CAAC;CACvD;CAEA,OAAO;EACL,OAAO,GAAG,SAAyB,OAAO,QAAQ,IAAI;EACtD,SAAS,UAAoB,OAAO,UAAU,CAAC,KAAK,CAAC;EACrD,QAAQ,cAAwB,OAAO,SAAS,CAAC,SAAS,CAAC;CAC7D;AACF;AAMA,SAAgB,oBACd,UACgC;CAChC,MAAM,UAA4B,GAC/B,OAAO,sBAAsB,cAAc,QAAQ,EACtD;CACA,KAAK,OAAO,aAAa,YAAY,aAAa,SAAS,OAAO,aAAa,YAC7E,OAAO;CAET,OAAO,IAAI,MAAM,UAAU,EACzB,IAAI,QAAQ,UAAU,UAAmB;EACvC,IAAI,aAAa,OAAO,eAAe,OAAO,QAAQ,OAAO;EAC7D,OAAO,QAAQ,IAAI,QAAQ,UAAU,QAAQ;CAC/C,EACF,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"gate.js","names":[],"sources":["../src/gate.ts"],"sourcesContent":["/* @do-runtime-gated */\n\nimport {\n atCheckpointEnd,\n tryCurrentContinuation,\n tryCurrentIoContext,\n type IoContext,\n} from \"./io/io-context\";\n\ntype Publication = {\n readonly publish: () => Promise<void>;\n readonly reject: (exception: unknown) => void;\n};\n\ntype Outcome<T> =\n | { readonly ok: true; readonly value: T }\n | { readonly ok: false; readonly exception: unknown };\n\nconst TRANSFORMED_AWAIT = Symbol(\"@mcp-b/do-runtime/transformed-await\");\n/**\n * Own the gap between publishing an await result and its `__resumeAwait` call.\n * This is deliberately separate from the current-continuation ambient: a\n * reservation serializes publishers but must never make its actor look current.\n * See §2.3 and decision 8.\n */\nconst CURRENT_PUBLICATION = Symbol.for(\"@mcp-b/do-runtime/current-await-publication\");\nconst warnedUngatedAwaits = new Set<string>();\n\ntype PublicationReservation = {\n readonly context: IoContext;\n};\n\ntype TransformedAwait<T> = {\n readonly [TRANSFORMED_AWAIT]: true;\n readonly context: IoContext;\n readonly outcome: Outcome<T>;\n readonly reservation: PublicationReservation;\n};\n\nfunction isThenable(value: unknown): value is PromiseLike<unknown> {\n return (\n (typeof value === \"object\" && value !== null) ||\n typeof value === \"function\"\n ) && typeof Reflect.get(value, \"then\") === \"function\";\n}\n\n/** Re-enter the actor that owns this transformed await; fail open outside actors. */\nexport function __gate<T>(value: T): T | Promise<Awaited<T>> {\n const context = tryCurrentIoContext();\n if (!isThenable(value) && context === undefined) return value;\n if (context === undefined) return value;\n return resumeWithContext(context, Promise.resolve(value));\n}\n\n/** Capture an actor await without publishing its context before the continuation runs. */\nexport function __gateAwait<T>(\n value: T,\n developmentSource?: string,\n): T | Promise<TransformedAwait<Awaited<T>>> {\n const context = tryCurrentIoContext();\n if (context === undefined) {\n if (developmentSource !== undefined && !warnedUngatedAwaits.has(developmentSource)) {\n warnedUngatedAwaits.add(developmentSource);\n console.warn(\n `do-runtime: transformed await in ${developmentSource} ran without an actor input lock; ` +\n \"an earlier await or entry path is not gated\",\n );\n }\n return value;\n }\n return resumeAwaitWithContext(context, Promise.resolve(value));\n}\n\n/** Restore the captured actor at the first instruction after a transformed await. */\nexport function __resumeAwait<T>(value: T | TransformedAwait<T>): T {\n if (!isTransformedAwait(value)) return value as T;\n\n clearPublication(value.reservation);\n value.context.restoreContinuation();\n if (value.outcome.ok) return value.outcome.value;\n throw value.outcome.exception;\n}\n\nfunction isTransformedAwait<T>(value: T | TransformedAwait<T>): value is TransformedAwait<T> {\n return Reflect.get(Object(value), TRANSFORMED_AWAIT) === true;\n}\n\nfunction currentPublication(): PublicationReservation | undefined {\n return Reflect.get(globalThis, CURRENT_PUBLICATION) as PublicationReservation | undefined;\n}\n\nfunction reservePublication(context: IoContext): PublicationReservation | undefined {\n if (tryCurrentContinuation() !== undefined || currentPublication() !== undefined) return undefined;\n const reservation = { context };\n Reflect.set(globalThis, CURRENT_PUBLICATION, reservation);\n atCheckpointEnd(() => clearPublication(reservation));\n return reservation;\n}\n\nfunction clearPublication(reservation: PublicationReservation): void {\n if (currentPublication() === reservation) {\n Reflect.deleteProperty(globalThis, CURRENT_PUBLICATION);\n }\n}\n\nfunction publishOutcome<T, Result>(\n context: IoContext,\n promise: Promise<T>,\n finish: (outcome: Outcome<T>, reservation: PublicationReservation) => Result,\n): Promise<Result> {\n return new Promise<Result>((resolve, reject) => {\n const publish = context.makeTransformReentryCallback((outcome: Outcome<T>) => {\n const reservation = reservePublication(context);\n if (reservation === undefined) {\n schedulePublication({ publish: () => publish(outcome), reject });\n return;\n }\n resolve(finish(outcome, reservation));\n });\n void promise.then(\n (value) => {\n schedulePublication({ publish: () => publish({ ok: true, value }), reject });\n },\n (exception: unknown) => {\n schedulePublication({ publish: () => publish({ ok: false, exception }), reject });\n },\n );\n });\n}\n\nfunction resumeAwaitWithContext<T>(\n context: IoContext,\n promise: Promise<T>,\n): Promise<TransformedAwait<T>> {\n return publishOutcome(context, promise, (outcome, reservation) => ({\n [TRANSFORMED_AWAIT]: true,\n context,\n outcome,\n reservation,\n }));\n}\n\nfunction resumeWithContext<T>(context: IoContext, promise: Promise<T>): Promise<T> {\n return publishOutcome(context, promise, (outcome, reservation) => {\n clearPublication(reservation);\n context.restoreContinuation();\n if (outcome.ok) return outcome.value;\n throw outcome.exception;\n });\n}\n\n/**\n * Resolve one transformed await per task, inside a fresh actor slice. Admission\n * attempts are independent so a blocked actor cannot stall the actor that will\n * unblock it. The task boundary keeps each continuation ambient isolated.\n */\nfunction schedulePublication(publication: Publication): void {\n const channel = new MessageChannel();\n channel.port1.onmessage = () => {\n channel.port1.close();\n channel.port2.close();\n\n void publication.publish().catch((exception: unknown) => {\n publication.reject(exception);\n });\n };\n channel.port2.postMessage(undefined);\n}\n\nfunction iteratorFor<T>(iterable: AsyncIterable<T> | Iterable<T>): AsyncIterator<T> | Iterator<T> {\n const subject = Object(iterable);\n const asyncIterator: unknown = Reflect.get(subject, Symbol.asyncIterator);\n if (typeof asyncIterator === \"function\") return Reflect.apply(asyncIterator, iterable, []);\n const iterator: unknown = Reflect.get(subject, Symbol.iterator);\n if (typeof iterator === \"function\") return Reflect.apply(iterator, iterable, []);\n throw new TypeError(\"value is not async iterable or iterable\");\n}\n\nfunction gatedIterator<T>(iterable: AsyncIterable<T> | Iterable<T>): AsyncIterator<T> {\n const iterator = iteratorFor(iterable);\n\n function invoke(methodName: \"next\" | \"return\" | \"throw\", args: unknown[]): Promise<IteratorResult<T>> {\n const method: unknown = Reflect.get(iterator, methodName);\n if (typeof method === \"function\") {\n return Promise.resolve(__gate(Reflect.apply(method, iterator, args)));\n }\n if (methodName === \"throw\") return Promise.reject(args[0]);\n return Promise.resolve({ done: true, value: args[0] });\n }\n\n return {\n next: (...args: [] | [unknown]) => invoke(\"next\", args),\n return: (value?: unknown) => invoke(\"return\", [value]),\n throw: (exception?: unknown) => invoke(\"throw\", [exception]),\n };\n}\n\n/** Gate every operation used by `for await`, including early return and throw. */\nexport function __gateAsyncIterable<T, IterableType extends AsyncIterable<T> | Iterable<T>>(\n iterable: IterableType,\n): IterableType;\nexport function __gateAsyncIterable<T>(\n iterable: AsyncIterable<T> | Iterable<T>,\n): AsyncIterable<T> | Iterable<T> {\n const wrapper: AsyncIterable<T> = {\n [Symbol.asyncIterator]: () => gatedIterator(iterable),\n };\n if ((typeof iterable !== \"object\" || iterable === null) && typeof iterable !== \"function\") {\n return wrapper;\n }\n return new Proxy(iterable, {\n get(target, property, receiver): unknown {\n if (property === Symbol.asyncIterator) return wrapper[Symbol.asyncIterator];\n return Reflect.get(target, property, receiver);\n },\n });\n}\n"],"mappings":";;AAkBA,IAAM,oBAAoB,OAAO,qCAAqC;;;;;;;AAOtE,IAAM,sBAAsB,OAAO,IAAI,6CAA6C;AACpF,I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/C,EACF,CAAC;AACH"}
|
package/dist/src/gate.d.ts
CHANGED
|
@@ -7,10 +7,14 @@ type Outcome<T> = {
|
|
|
7
7
|
readonly exception: unknown;
|
|
8
8
|
};
|
|
9
9
|
declare const TRANSFORMED_AWAIT: unique symbol;
|
|
10
|
+
type PublicationReservation = {
|
|
11
|
+
readonly context: IoContext;
|
|
12
|
+
};
|
|
10
13
|
type TransformedAwait<T> = {
|
|
11
14
|
readonly [TRANSFORMED_AWAIT]: true;
|
|
12
15
|
readonly context: IoContext;
|
|
13
16
|
readonly outcome: Outcome<T>;
|
|
17
|
+
readonly reservation: PublicationReservation;
|
|
14
18
|
};
|
|
15
19
|
/** Re-enter the actor that owns this transformed await; fail open outside actors. */
|
|
16
20
|
export declare function __gate<T>(value: T): T | Promise<Awaited<T>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcp-b/do-runtime",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "Cloudflare's Durable Object runtime (workerd), ported to TypeScript: actors with input/output gates, SQLite storage, facets and alarms, running in the browser and in Node.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"actors",
|