@nice-code/action 0.20.0 → 0.21.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/README.md +49 -50
- package/build/{ActionDevtoolsCore-D_JvgPmz.d.mts → ActionDevtoolsCore-baIHExfj.d.mts} +2 -2
- package/build/{ActionDevtoolsCore-dV-IVPcP.d.cts → ActionDevtoolsCore-dApyYvTS.d.cts} +2 -2
- package/build/{ActionPayload.types-CnfWlkA1.d.cts → ActionPayload.types-CQM1HRw_.d.cts} +161 -194
- package/build/{ActionPayload.types-D0DM-g65.d.mts → ActionPayload.types-DXGiw1SF.d.mts} +161 -194
- package/build/devtools/browser/index.d.cts +1 -1
- package/build/devtools/browser/index.d.mts +1 -1
- package/build/devtools/server/index.d.cts +1 -1
- package/build/devtools/server/index.d.mts +1 -1
- package/build/index.cjs +1763 -1722
- package/build/index.cjs.map +1 -1
- package/build/index.d.cts +2 -2
- package/build/index.d.mts +2 -2
- package/build/index.mjs +1764 -1721
- package/build/index.mjs.map +1 -1
- package/build/platform/cloudflare/index.d.cts +1 -1
- package/build/platform/cloudflare/index.d.mts +1 -1
- package/build/react-query/index.d.cts +1 -1
- package/build/react-query/index.d.mts +1 -1
- package/package.json +4 -4
package/build/index.cjs
CHANGED
|
@@ -1457,16 +1457,16 @@ var ActionRuntime = class {
|
|
|
1457
1457
|
return this;
|
|
1458
1458
|
}
|
|
1459
1459
|
/**
|
|
1460
|
+
* @internal Low-level primitive — the public way to open a connection is `connectChannel`, which
|
|
1461
|
+
* derives routing from a channel and binds the crypto identity for you. This stays as the raw building
|
|
1462
|
+
* block it sits on (it restates domain lists by hand) and is not part of the supported surface.
|
|
1463
|
+
*
|
|
1460
1464
|
* Declare an external "backend client" in one call: build an
|
|
1461
1465
|
* {@link ConnectorHandler} for `externalCoordinate` carrying the given
|
|
1462
1466
|
* `transports`, route the listed `domains`/`actions` to it, register it (plus any
|
|
1463
1467
|
* `localHandlers` — e.g. server→client push handlers that share the same channel)
|
|
1464
1468
|
* on this runtime, and `apply()`. Returns the external handler so the caller can
|
|
1465
1469
|
* later `clearTransportCache()` it.
|
|
1466
|
-
*
|
|
1467
|
-
* Sugar over `new ConnectorHandler(...).forDomain(...)` + `addHandlers([...])`,
|
|
1468
|
-
* so a single runtime can host one handler per backend target with its transports
|
|
1469
|
-
* declared once and reused across every action routed to that backend.
|
|
1470
1470
|
*/
|
|
1471
1471
|
connectTo(externalCoordinate, options) {
|
|
1472
1472
|
const handler = new ConnectorHandler({
|
|
@@ -2970,1924 +2970,1967 @@ function createSecureAcceptorHandler(options) {
|
|
|
2970
2970
|
});
|
|
2971
2971
|
}
|
|
2972
2972
|
//#endregion
|
|
2973
|
-
//#region src/ActionRuntime/
|
|
2973
|
+
//#region src/ActionRuntime/Transport/Carrier/Carrier.types.ts
|
|
2974
2974
|
/**
|
|
2975
|
-
*
|
|
2976
|
-
*
|
|
2977
|
-
*
|
|
2978
|
-
* accepted as a legacy alias for `toAcceptor`.)
|
|
2975
|
+
* Narrow a carrier source to the exchange shape via its `shape` discriminant — the one branch the
|
|
2976
|
+
* transport factories ({@link secureTransport}, {@link plainTransport}) use to pick the duplex vs
|
|
2977
|
+
* exchange transport. A duplex source carries no `shape`, so the `else` branch is the duplex one.
|
|
2979
2978
|
*/
|
|
2980
|
-
function
|
|
2981
|
-
return
|
|
2982
|
-
toAcceptorDomains: options.toAcceptor,
|
|
2983
|
-
toConnectorDomains: options.toConnector
|
|
2984
|
-
};
|
|
2979
|
+
function isExchangeCarrierSource(carrier) {
|
|
2980
|
+
return "shape" in carrier && carrier.shape === "exchange";
|
|
2985
2981
|
}
|
|
2982
|
+
//#endregion
|
|
2983
|
+
//#region src/ActionRuntime/Transport/Transport.ts
|
|
2986
2984
|
/**
|
|
2987
|
-
*
|
|
2988
|
-
*
|
|
2989
|
-
* `
|
|
2990
|
-
*
|
|
2991
|
-
*
|
|
2992
|
-
* fallback).
|
|
2993
|
-
*
|
|
2994
|
-
* Sugar over {@link ActionRuntime.connectTo}. Returns the acceptor handler so the caller can later
|
|
2995
|
-
* `clearTransportCache()` it.
|
|
2985
|
+
* Reusable transport definition. Devs construct these (`secureTransport({ carrier: wsCarrier(url) })`,
|
|
2986
|
+
* `plainTransport({ carrier: httpCarrier(...) })`, …) and pass them to a
|
|
2987
|
+
* `ConnectorHandler`. A single
|
|
2988
|
+
* definition can be shared across multiple handlers — each handler builds its own live
|
|
2989
|
+
* {@link TransportConnection} via {@link TransportConnection._createConnection}.
|
|
2996
2990
|
*/
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
localHandlers: pushHandlers,
|
|
3003
|
-
defaultTimeout: options.defaultTimeout
|
|
3004
|
-
});
|
|
2991
|
+
var Transport = class {};
|
|
2992
|
+
//#endregion
|
|
2993
|
+
//#region src/ActionRuntime/Transport/SecureSession/exchangeProtocol.ts
|
|
2994
|
+
function encodeExchange(envelope) {
|
|
2995
|
+
return JSON.stringify(envelope);
|
|
3005
2996
|
}
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
* `toAcceptor` domains are served together with one merged, connection-aware case map (each case gets
|
|
3009
|
-
* the primed request + the originating connection, as with
|
|
3010
|
-
* {@link AcceptorHandler.forConnectionDomainCases}). The domain list is taken from the channel,
|
|
3011
|
-
* never restated. Add the returned handler to the runtime alongside the acceptor handler:
|
|
3012
|
-
* ```ts
|
|
3013
|
-
* runtime.addHandlers([acceptChannelConnections(serverHandler, channel, { … }), serverHandler]);
|
|
3014
|
-
* ```
|
|
3015
|
-
*/
|
|
3016
|
-
function acceptChannelConnections(serverHandler, channel, cases) {
|
|
3017
|
-
return serverHandler.forConnectionDomainCasesMulti(channel.toAcceptorDomains, cases);
|
|
2997
|
+
function decodeExchangeRequest(raw) {
|
|
2998
|
+
return parse(raw);
|
|
3018
2999
|
}
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
* {@link connectChannel}. It folds in the same boilerplate as {@link createSecureAcceptorHandler} (the
|
|
3022
|
-
* `ClientCryptoKeyLink` + storage-backed TOFU resolver from one `storageAdapter`, the channel's codec +
|
|
3023
|
-
* dictionary version, the `security` block from the runtime coordinate) but takes the `(runtime, channel,
|
|
3024
|
-
* options)` shape of the channel family. Pair it with {@link acceptChannelConnections} for execution:
|
|
3025
|
-
* ```ts
|
|
3026
|
-
* const acceptor = acceptChannel(runtime, gameChannel, { clientEnv, storageAdapter, send });
|
|
3027
|
-
* runtime.addHandlers([acceptChannelConnections(acceptor, gameChannel, { … }), acceptor]);
|
|
3028
|
-
* ```
|
|
3029
|
-
*/
|
|
3030
|
-
function acceptChannel(runtime, channel, options) {
|
|
3031
|
-
return createSecureAcceptorHandler({
|
|
3032
|
-
channel,
|
|
3033
|
-
runtime,
|
|
3034
|
-
clientEnv: options.clientEnv,
|
|
3035
|
-
storageAdapter: options.storageAdapter,
|
|
3036
|
-
link: options.link,
|
|
3037
|
-
send: options.send,
|
|
3038
|
-
securityLevel: options.securityLevel,
|
|
3039
|
-
verifyKeyResolver: options.verifyKeyResolver,
|
|
3040
|
-
defaultTimeout: options.defaultTimeout
|
|
3041
|
-
});
|
|
3000
|
+
function decodeExchangeReply(raw) {
|
|
3001
|
+
return parse(raw);
|
|
3042
3002
|
}
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
*/
|
|
3049
|
-
const PayloadTypeToInt = {
|
|
3050
|
-
["request"]: 0,
|
|
3051
|
-
["result"]: 1,
|
|
3052
|
-
["progress"]: 2
|
|
3053
|
-
};
|
|
3054
|
-
const ReversePayloadType = [
|
|
3055
|
-
"request",
|
|
3056
|
-
"result",
|
|
3057
|
-
"progress"
|
|
3058
|
-
];
|
|
3059
|
-
/**
|
|
3060
|
-
* Build the positional `domain:id` ↔ integer dictionary. Both ends of a channel MUST build it from
|
|
3061
|
-
* the same domains in the same order — the mapping is positional, so a mismatch routes to the wrong
|
|
3062
|
-
* action. Add new transported domains to the end of the list.
|
|
3063
|
-
*/
|
|
3064
|
-
function buildActionRouteDictionary(domains) {
|
|
3065
|
-
const routeToInt = /* @__PURE__ */ new Map();
|
|
3066
|
-
const intToRoute = [];
|
|
3067
|
-
for (const dom of domains) for (const actionId of Object.keys(dom.actionSchema)) {
|
|
3068
|
-
const routeKey = `${dom.domain}:${actionId}`;
|
|
3069
|
-
if (routeToInt.has(routeKey)) continue;
|
|
3070
|
-
routeToInt.set(routeKey, intToRoute.length);
|
|
3071
|
-
intToRoute.push({
|
|
3072
|
-
domain: dom.domain,
|
|
3073
|
-
id: actionId,
|
|
3074
|
-
allDomains: dom.allDomains
|
|
3075
|
-
});
|
|
3003
|
+
function parse(raw) {
|
|
3004
|
+
try {
|
|
3005
|
+
return JSON.parse(raw);
|
|
3006
|
+
} catch {
|
|
3007
|
+
return;
|
|
3076
3008
|
}
|
|
3077
|
-
return {
|
|
3078
|
-
routeToInt,
|
|
3079
|
-
intToRoute
|
|
3080
|
-
};
|
|
3081
3009
|
}
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
if (json.type === "progress") return json.progress;
|
|
3010
|
+
function bytesToBase64(bytes) {
|
|
3011
|
+
let binary = "";
|
|
3012
|
+
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
3013
|
+
return btoa(binary);
|
|
3087
3014
|
}
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
...base,
|
|
3110
|
-
type: "result",
|
|
3111
|
-
result: payloadData,
|
|
3112
|
-
outputHash: ""
|
|
3015
|
+
function base64ToBytes(base64) {
|
|
3016
|
+
const binary = atob(base64);
|
|
3017
|
+
const bytes = new Uint8Array(binary.length);
|
|
3018
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
3019
|
+
return bytes;
|
|
3020
|
+
}
|
|
3021
|
+
//#endregion
|
|
3022
|
+
//#region src/ActionRuntime/Transport/SecureSession/establishExchangeSession.ts
|
|
3023
|
+
const textEncoder$1 = new TextEncoder();
|
|
3024
|
+
const textDecoder$1 = new TextDecoder();
|
|
3025
|
+
/** Plain path (no handshake/token): every action rides a bare `act` envelope, plaintext both ways. */
|
|
3026
|
+
function finalizePlainExchangeMethods(ctx) {
|
|
3027
|
+
return buildExchangeMethods(ctx, {});
|
|
3028
|
+
}
|
|
3029
|
+
/** Secure path: run the handshake (two exchanges) once at bring-up, then reuse the token + crypto. */
|
|
3030
|
+
async function finalizeSecureExchangeMethods(ctx) {
|
|
3031
|
+
return buildExchangeMethods(ctx, await runConnectorExchangeHandshake(ctx.carrier, ctx.secure));
|
|
3032
|
+
}
|
|
3033
|
+
function buildExchangeMethods(ctx, state) {
|
|
3034
|
+
const sendActionData = (inputs) => {
|
|
3035
|
+
runExchange(ctx.carrier, state, inputs).catch((err) => inputs.runningAction._abort(err));
|
|
3113
3036
|
};
|
|
3114
3037
|
return {
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
progress: payloadData
|
|
3038
|
+
sendActionData,
|
|
3039
|
+
updateRunConfig: ctx.updateRunConfig
|
|
3118
3040
|
};
|
|
3119
3041
|
}
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
};
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
const
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
}
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
for (const [key, entry] of map) {
|
|
3155
|
-
if (now - entry.time <= ttlMs) break;
|
|
3156
|
-
map.delete(key);
|
|
3042
|
+
async function runExchange(carrier, state, inputs) {
|
|
3043
|
+
const { action, runningAction, timeout } = inputs;
|
|
3044
|
+
const ac = new AbortController();
|
|
3045
|
+
let timedOut = false;
|
|
3046
|
+
const timeoutId = setTimeout(() => {
|
|
3047
|
+
timedOut = true;
|
|
3048
|
+
ac.abort();
|
|
3049
|
+
}, timeout);
|
|
3050
|
+
const unsubscribe = runningAction.addUpdateListeners([(update) => {
|
|
3051
|
+
if (update.type === "finished") {
|
|
3052
|
+
clearTimeout(timeoutId);
|
|
3053
|
+
ac.abort();
|
|
3054
|
+
}
|
|
3055
|
+
}]);
|
|
3056
|
+
try {
|
|
3057
|
+
const request = await buildRequestEnvelope(state, action);
|
|
3058
|
+
const replyRaw = await carrier.exchange(encodeExchange(request), { signal: ac.signal });
|
|
3059
|
+
if (action.type !== "request") return;
|
|
3060
|
+
const reply = decodeExchangeReply(asString(replyRaw));
|
|
3061
|
+
if (reply == null) throw err_nice_transport.fromId("invalid_action_response", { actionId: action.id });
|
|
3062
|
+
if (reply.k === "err") throw err_nice_transport.fromId("send_failed", {
|
|
3063
|
+
actionState: action.type,
|
|
3064
|
+
actionId: action.id,
|
|
3065
|
+
message: reply.message
|
|
3066
|
+
});
|
|
3067
|
+
const wire = await extractReplyWire(state, reply);
|
|
3068
|
+
if (wire == null || !isActionPayload_Result_JsonObject(wire)) throw err_nice_transport.fromId("invalid_action_response", { actionId: action.id });
|
|
3069
|
+
runningAction._completeWithResult(action._domain.hydrateResultPayload(wire));
|
|
3070
|
+
} catch (err) {
|
|
3071
|
+
if (timedOut) throw err_nice_transport.fromId("timeout", { timeout });
|
|
3072
|
+
throw err;
|
|
3073
|
+
} finally {
|
|
3074
|
+
clearTimeout(timeoutId);
|
|
3075
|
+
unsubscribe();
|
|
3157
3076
|
}
|
|
3158
3077
|
}
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
* gets its own correlation + identity state.
|
|
3164
|
-
*
|
|
3165
|
-
* On top of everything {@link createBinaryWireAdapter} drops, a session also drops:
|
|
3166
|
-
* - **`cuid`** — replaced by a per-connection integer correlation id. The initiator maps it to its
|
|
3167
|
-
* real cuid; the responder echoes it; each side reconstructs the cuid from its own map. Correlation
|
|
3168
|
-
* only needs to be unique per socket, so a counter suffices.
|
|
3169
|
-
* - **`originClient` after the first request** — the first request each side sends carries its
|
|
3170
|
-
* identity; the peer remembers it and injects it into later frames. Replies omit it entirely (a
|
|
3171
|
-
* reply carries the initiator's own origin, which the initiator already knows).
|
|
3172
|
-
*
|
|
3173
|
-
* Both ends MUST build the factory from the same domains in the same order (positional dictionary).
|
|
3174
|
-
* Text frames still return `undefined` from `incoming`, so JSON clients remain interoperable.
|
|
3175
|
-
*
|
|
3176
|
-
* Hibernation note: after a server connection is evicted its session resets, so a still-connected
|
|
3177
|
-
* client (whose session persists) will keep omitting `originClient`. The server must therefore restore
|
|
3178
|
-
* the connection→client binding from its own store (see `AcceptorHandler.rehydrateConnection`) and
|
|
3179
|
-
* inject `originClient` from there — the session alone can't recover it.
|
|
3180
|
-
*/
|
|
3181
|
-
function createBinaryWireSessionFactory(domains, options) {
|
|
3182
|
-
const { routeToInt, intToRoute } = buildActionRouteDictionary(domains);
|
|
3183
|
-
const unknownIdentity = RuntimeCoordinate.unknown.toJsonObject();
|
|
3184
|
-
const ttlMs = options?.correlationTtlMs ?? DEFAULT_CORRELATION_TTL_MS;
|
|
3185
|
-
return () => {
|
|
3186
|
-
let outCounter = 0;
|
|
3187
|
-
const corrToCuid = /* @__PURE__ */ new Map();
|
|
3188
|
-
const cuidToCorr = /* @__PURE__ */ new Map();
|
|
3189
|
-
let selfIdentity;
|
|
3190
|
-
let peerIdentity;
|
|
3078
|
+
async function buildRequestEnvelope(state, action) {
|
|
3079
|
+
const wire = action.toJsonObject();
|
|
3080
|
+
if (state.crypto != null) {
|
|
3081
|
+
const ciphertext = await state.crypto.encryptFrame(textEncoder$1.encode(JSON.stringify(wire)));
|
|
3191
3082
|
return {
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
const routeInt = routeToInt.get(routeKey);
|
|
3196
|
-
if (routeInt == null) throw new Error(`[binary-wire] Cannot pack unregistered action route: ${routeKey}`);
|
|
3197
|
-
const now = Date.now();
|
|
3198
|
-
pruneExpired(corrToCuid, now, ttlMs);
|
|
3199
|
-
pruneExpired(cuidToCorr, now, ttlMs);
|
|
3200
|
-
let corr;
|
|
3201
|
-
let wireIdentity;
|
|
3202
|
-
if (json.type === "request") {
|
|
3203
|
-
corr = outCounter++;
|
|
3204
|
-
corrToCuid.set(corr, {
|
|
3205
|
-
value: json.context.cuid,
|
|
3206
|
-
time: now
|
|
3207
|
-
});
|
|
3208
|
-
if (selfIdentity == null && isKnownIdentity(json.context.originClient)) {
|
|
3209
|
-
selfIdentity = json.context.originClient;
|
|
3210
|
-
wireIdentity = json.context.originClient;
|
|
3211
|
-
}
|
|
3212
|
-
} else {
|
|
3213
|
-
corr = cuidToCorr.get(json.context.cuid)?.value ?? -1;
|
|
3214
|
-
if (json.type === "result") cuidToCorr.delete(json.context.cuid);
|
|
3215
|
-
}
|
|
3216
|
-
const envelope = new Array(ENVELOPE_LENGTH$1);
|
|
3217
|
-
envelope[ENVELOPE$1.route] = routeInt;
|
|
3218
|
-
envelope[ENVELOPE$1.type] = PayloadTypeToInt[json.type];
|
|
3219
|
-
envelope[ENVELOPE$1.corr] = corr;
|
|
3220
|
-
envelope[ENVELOPE$1.time] = json.time;
|
|
3221
|
-
envelope[ENVELOPE$1.originClient] = wireIdentity;
|
|
3222
|
-
envelope[ENVELOPE$1.payload] = extractWirePayload(json);
|
|
3223
|
-
return (0, msgpackr.pack)(envelope);
|
|
3224
|
-
},
|
|
3225
|
-
incoming: (frame) => {
|
|
3226
|
-
let buffer;
|
|
3227
|
-
if (frame instanceof ArrayBuffer) buffer = new Uint8Array(frame);
|
|
3228
|
-
else if (frame instanceof Uint8Array) buffer = frame;
|
|
3229
|
-
else return;
|
|
3230
|
-
try {
|
|
3231
|
-
const envelope = (0, msgpackr.unpack)(buffer);
|
|
3232
|
-
if (!Array.isArray(envelope) || envelope.length !== ENVELOPE_LENGTH$1) return void 0;
|
|
3233
|
-
const routeMeta = intToRoute[envelope[ENVELOPE$1.route]];
|
|
3234
|
-
const payloadType = ReversePayloadType[envelope[ENVELOPE$1.type]];
|
|
3235
|
-
if (routeMeta == null || payloadType == null) return void 0;
|
|
3236
|
-
const now = Date.now();
|
|
3237
|
-
pruneExpired(corrToCuid, now, ttlMs);
|
|
3238
|
-
pruneExpired(cuidToCorr, now, ttlMs);
|
|
3239
|
-
const corr = envelope[ENVELOPE$1.corr];
|
|
3240
|
-
const time = envelope[ENVELOPE$1.time];
|
|
3241
|
-
const wireIdentity = envelope[ENVELOPE$1.originClient];
|
|
3242
|
-
let cuid;
|
|
3243
|
-
let originClient;
|
|
3244
|
-
if (payloadType === "request") {
|
|
3245
|
-
cuid = (0, nanoid.nanoid)();
|
|
3246
|
-
cuidToCorr.set(cuid, {
|
|
3247
|
-
value: corr,
|
|
3248
|
-
time: now
|
|
3249
|
-
});
|
|
3250
|
-
if (isKnownIdentity(wireIdentity)) peerIdentity = wireIdentity;
|
|
3251
|
-
originClient = peerIdentity ?? unknownIdentity;
|
|
3252
|
-
} else {
|
|
3253
|
-
cuid = corrToCuid.get(corr)?.value ?? (0, nanoid.nanoid)();
|
|
3254
|
-
if (payloadType === "result") corrToCuid.delete(corr);
|
|
3255
|
-
originClient = selfIdentity ?? unknownIdentity;
|
|
3256
|
-
}
|
|
3257
|
-
return assembleWireJson(routeMeta, payloadType, time, {
|
|
3258
|
-
cuid,
|
|
3259
|
-
timeCreated: time,
|
|
3260
|
-
routing: [],
|
|
3261
|
-
originClient
|
|
3262
|
-
}, envelope[ENVELOPE$1.payload]);
|
|
3263
|
-
} catch (e) {
|
|
3264
|
-
console.error("[binary-wire] Failed to unpack binary action session frame", e);
|
|
3265
|
-
return;
|
|
3266
|
-
}
|
|
3267
|
-
}
|
|
3083
|
+
k: "act",
|
|
3084
|
+
t: state.token,
|
|
3085
|
+
c: bytesToBase64(ciphertext)
|
|
3268
3086
|
};
|
|
3087
|
+
}
|
|
3088
|
+
return {
|
|
3089
|
+
k: "act",
|
|
3090
|
+
t: state.token,
|
|
3091
|
+
w: wire
|
|
3269
3092
|
};
|
|
3270
3093
|
}
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
*/
|
|
3278
|
-
function deriveDictionaryVersion(domains) {
|
|
3279
|
-
const { intToRoute } = buildActionRouteDictionary(domains);
|
|
3280
|
-
const signature = intToRoute.map((route) => `${route.domain}:${route.id}`).join(",");
|
|
3281
|
-
let hash = 2166136261;
|
|
3282
|
-
for (let i = 0; i < signature.length; i++) {
|
|
3283
|
-
hash ^= signature.charCodeAt(i);
|
|
3284
|
-
hash = Math.imul(hash, 16777619);
|
|
3094
|
+
async function extractReplyWire(state, reply) {
|
|
3095
|
+
if (reply.k !== "act") return void 0;
|
|
3096
|
+
if ("c" in reply) {
|
|
3097
|
+
if (state.crypto == null) return void 0;
|
|
3098
|
+
const plain = await state.crypto.decryptFrame(base64ToBytes(reply.c));
|
|
3099
|
+
return JSON.parse(textDecoder$1.decode(plain));
|
|
3285
3100
|
}
|
|
3286
|
-
return
|
|
3101
|
+
return reply.w;
|
|
3287
3102
|
}
|
|
3288
|
-
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
* {@link connectChannel} and `acceptChannelConnections`) instead of being restated at each end. The
|
|
3296
|
-
* wire dictionary spans `[...toAcceptor, ...toConnector]` in that order; add new domains to the end of
|
|
3297
|
-
* their list to keep older peers compatible. (`domains` is still accepted as a legacy alias for
|
|
3298
|
-
* `toAcceptor`.)
|
|
3299
|
-
*/
|
|
3300
|
-
function defineSecureChannel(options) {
|
|
3301
|
-
const base = defineChannel({
|
|
3302
|
-
toAcceptor: options.toAcceptor,
|
|
3303
|
-
toConnector: options.toConnector
|
|
3103
|
+
async function runConnectorExchangeHandshake(carrier, secure) {
|
|
3104
|
+
await secure.link.initialize();
|
|
3105
|
+
const handshake = createClientHandshake({
|
|
3106
|
+
link: secure.link,
|
|
3107
|
+
localCoordinate: secure.localCoordinate,
|
|
3108
|
+
dictionaryVersion: secure.dictionaryVersion,
|
|
3109
|
+
securityLevel: secure.securityLevel
|
|
3304
3110
|
});
|
|
3305
|
-
const
|
|
3111
|
+
const hsid = (0, nanoid.nanoid)();
|
|
3112
|
+
const hello = await handshake.createHello();
|
|
3113
|
+
const welcomeReply = decodeExchangeReply(asString(await carrier.exchange(encodeExchange({
|
|
3114
|
+
k: "hs",
|
|
3115
|
+
hsid,
|
|
3116
|
+
m: encodeHandshakeMessage(hello)
|
|
3117
|
+
}))));
|
|
3118
|
+
if (welcomeReply?.k !== "hs") throw new Error("[exchange-handshake] expected a welcome reply");
|
|
3119
|
+
const welcome = decodeHandshakeMessage(welcomeReply.m);
|
|
3120
|
+
if (welcome == null) throw new Error("[exchange-handshake] malformed welcome");
|
|
3121
|
+
if (welcome.t === "reject") throw new Error(`[exchange-handshake] rejected by peer: ${welcome.reason}`);
|
|
3122
|
+
if (welcome.t !== "welcome") throw new Error(`[exchange-handshake] expected welcome, got ${welcome.t}`);
|
|
3123
|
+
const prove = await handshake.onWelcome(welcome);
|
|
3124
|
+
const acceptReply = decodeExchangeReply(asString(await carrier.exchange(encodeExchange({
|
|
3125
|
+
k: "hs",
|
|
3126
|
+
hsid,
|
|
3127
|
+
m: encodeHandshakeMessage(prove)
|
|
3128
|
+
}))));
|
|
3129
|
+
if (acceptReply?.k !== "hs") throw new Error("[exchange-handshake] expected an accept reply");
|
|
3130
|
+
const accept = decodeHandshakeMessage(acceptReply.m);
|
|
3131
|
+
if (accept == null) throw new Error("[exchange-handshake] malformed accept");
|
|
3132
|
+
if (accept.t === "reject") throw new Error(`[exchange-handshake] rejected by peer: ${accept.reason}`);
|
|
3133
|
+
if (accept.t !== "accept") throw new Error(`[exchange-handshake] expected accept, got ${accept.t}`);
|
|
3134
|
+
if (acceptReply.t == null) throw new Error("[exchange-handshake] accept missing session token");
|
|
3135
|
+
const result = await handshake.onAccept(accept);
|
|
3136
|
+
const crypto = result.securityLevel === "encrypted" ? createActionFrameCrypto({
|
|
3137
|
+
link: secure.link,
|
|
3138
|
+
linkedClientId: result.linkedClientId
|
|
3139
|
+
}) : void 0;
|
|
3306
3140
|
return {
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
createCodec: createBinaryWireSessionFactory(allDomains, options.sessionOptions)
|
|
3141
|
+
token: acceptReply.t,
|
|
3142
|
+
crypto
|
|
3310
3143
|
};
|
|
3311
3144
|
}
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3315
|
-
return JSON.stringify(envelope);
|
|
3316
|
-
}
|
|
3317
|
-
function decodeExchangeRequest(raw) {
|
|
3318
|
-
return parse(raw);
|
|
3319
|
-
}
|
|
3320
|
-
function decodeExchangeReply(raw) {
|
|
3321
|
-
return parse(raw);
|
|
3322
|
-
}
|
|
3323
|
-
function parse(raw) {
|
|
3324
|
-
try {
|
|
3325
|
-
return JSON.parse(raw);
|
|
3326
|
-
} catch {
|
|
3327
|
-
return;
|
|
3328
|
-
}
|
|
3329
|
-
}
|
|
3330
|
-
function bytesToBase64(bytes) {
|
|
3331
|
-
let binary = "";
|
|
3332
|
-
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
3333
|
-
return btoa(binary);
|
|
3145
|
+
function asString(frame) {
|
|
3146
|
+
if (typeof frame === "string") return frame;
|
|
3147
|
+
return textDecoder$1.decode(frame instanceof ArrayBuffer ? new Uint8Array(frame) : frame);
|
|
3334
3148
|
}
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
3149
|
+
//#endregion
|
|
3150
|
+
//#region src/ActionRuntime/Transport/helpers/addTransportStatusMetadata.ts
|
|
3151
|
+
function addTransportStatusMetadata(transportStatus) {
|
|
3152
|
+
if (transportStatus.status === "ready") return {
|
|
3153
|
+
status: "ready",
|
|
3154
|
+
readyData: transportStatus.readyData
|
|
3155
|
+
};
|
|
3156
|
+
if (transportStatus.status === "initializing") return {
|
|
3157
|
+
status: "initializing",
|
|
3158
|
+
initializationPromise: transportStatus.initializationPromise,
|
|
3159
|
+
timeStarted: Date.now()
|
|
3160
|
+
};
|
|
3161
|
+
if (transportStatus.status === "failed") return {
|
|
3162
|
+
status: "failed",
|
|
3163
|
+
error: transportStatus.error,
|
|
3164
|
+
timeFailed: Date.now()
|
|
3165
|
+
};
|
|
3166
|
+
if (transportStatus.status === "unsupported") return { status: "unsupported" };
|
|
3167
|
+
return { status: "uninitialized" };
|
|
3340
3168
|
}
|
|
3341
3169
|
//#endregion
|
|
3342
|
-
//#region src/ActionRuntime/Transport/
|
|
3343
|
-
|
|
3344
|
-
const textDecoder$1 = new TextDecoder();
|
|
3170
|
+
//#region src/ActionRuntime/Transport/TransportConnection.ts
|
|
3171
|
+
let transportOrd = 0;
|
|
3345
3172
|
/**
|
|
3346
|
-
*
|
|
3347
|
-
*
|
|
3348
|
-
*
|
|
3349
|
-
* requests can't rely on channel ordering), mints a session **token** on accept, and on every later `act`
|
|
3350
|
-
* POST resolves the session by token, decrypts the body (at `encrypted`), routes it through the runtime,
|
|
3351
|
-
* and returns the (encrypted) result inline as the reply.
|
|
3352
|
-
*
|
|
3353
|
-
* Sessions and in-flight handshakes are held in memory — fine for a single-instance server. (Surviving a
|
|
3354
|
-
* Durable-Object eviction would persist each token's `keyMaterial` and re-derive the key on a miss, the
|
|
3355
|
-
* same primitive `AcceptorSecureSession.rehydrate` uses; left as a follow-up.)
|
|
3173
|
+
* Live, per-handler transport runtime built from a reusable {@link Transport} definition. Holds the
|
|
3174
|
+
* connection-scoped state (ordinal, initialized config, sockets / abort sets) that must not be shared
|
|
3175
|
+
* across handlers. Construct these via `definition._createConnection(...)`, never directly.
|
|
3356
3176
|
*/
|
|
3357
|
-
var
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
constructor(
|
|
3365
|
-
this.
|
|
3366
|
-
this.
|
|
3367
|
-
this.
|
|
3368
|
-
this._noneAllowed = this._allowedLevels.includes("none");
|
|
3177
|
+
var TransportConnection = class {
|
|
3178
|
+
def;
|
|
3179
|
+
transOrd = transportOrd++;
|
|
3180
|
+
type;
|
|
3181
|
+
initialized;
|
|
3182
|
+
/** Backref to the public definition that created this connection (used for devtools route info). */
|
|
3183
|
+
definition;
|
|
3184
|
+
constructor(def) {
|
|
3185
|
+
this.def = def;
|
|
3186
|
+
this.type = def.type;
|
|
3187
|
+
this.initialized = def.initialize();
|
|
3369
3188
|
}
|
|
3370
|
-
/**
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3189
|
+
/**
|
|
3190
|
+
* Devtools route info for an action routed through this live connection. Defaults to the stateless
|
|
3191
|
+
* {@link definition}'s info; connections override to enrich it from live state (e.g. the actual
|
|
3192
|
+
* resolved socket URL) when the definition couldn't resolve it on its own.
|
|
3193
|
+
*/
|
|
3194
|
+
getRouteInfo(input) {
|
|
3195
|
+
return this.definition?.getRouteInfo(input);
|
|
3376
3196
|
}
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3197
|
+
/**
|
|
3198
|
+
* Whether a `ready`-status transport still needs asynchronous bring-up before its methods exist —
|
|
3199
|
+
* awaiting the carrier to open and/or running a handshake. Default `false`: a stateless transport
|
|
3200
|
+
* (HTTP) is usable the instant `getTransport` reports `ready`, so it stays a terminal *synchronous*
|
|
3201
|
+
* fallback in {@link ConnectionTransportManager}. Stream carriers (Link/WS) override to `true`.
|
|
3202
|
+
*/
|
|
3203
|
+
_needsAsyncBringUp(_readyData) {
|
|
3204
|
+
return false;
|
|
3205
|
+
}
|
|
3206
|
+
/** Await the carrier becoming ready to send (e.g. a socket `open`). Default: nothing to await. */
|
|
3207
|
+
_awaitCarrierReady(_readyData) {
|
|
3208
|
+
return Promise.resolve();
|
|
3209
|
+
}
|
|
3210
|
+
/**
|
|
3211
|
+
* Finalize during async bring-up — may run a handshake, so it can be async. Defaults to the
|
|
3212
|
+
* synchronous {@link _finalizeTransportMethods}; secure stream carriers override to branch plain/secure.
|
|
3213
|
+
*/
|
|
3214
|
+
_finalizeReady(readyData) {
|
|
3215
|
+
return this._finalizeTransportMethods(readyData);
|
|
3216
|
+
}
|
|
3217
|
+
_getCacheKey(input) {
|
|
3218
|
+
const parts = this.initialized.getTransportCacheKey?.(input);
|
|
3219
|
+
if (parts == null) return null;
|
|
3220
|
+
return parts.join("\0");
|
|
3221
|
+
}
|
|
3222
|
+
getCacheKey(input) {
|
|
3223
|
+
const inner = this._getCacheKey(input);
|
|
3224
|
+
if (inner == null) return null;
|
|
3225
|
+
return `${this.transOrd}:${inner}`;
|
|
3226
|
+
}
|
|
3227
|
+
/**
|
|
3228
|
+
* Whether this transport can serve the given action right now. Consulted by the manager before
|
|
3229
|
+
* cache-key resolution and `getTransport`; a `false` result skips this transport (treated as
|
|
3230
|
+
* `unsupported`) and the manager falls through to the next in preference order. Defaults to `true`
|
|
3231
|
+
* when the transport declares no gate.
|
|
3232
|
+
*/
|
|
3233
|
+
isAvailable(input) {
|
|
3234
|
+
return this.initialized.isAvailable?.(input) ?? true;
|
|
3235
|
+
}
|
|
3236
|
+
getTransport(input) {
|
|
3237
|
+
return this._processTransportStatus(input);
|
|
3238
|
+
}
|
|
3239
|
+
_processTransportStatus(input) {
|
|
3240
|
+
const statusInfo = addTransportStatusMetadata(this.initialized.getTransport(input));
|
|
3241
|
+
if (statusInfo.status === "ready") {
|
|
3242
|
+
if (!this._needsAsyncBringUp(statusInfo.readyData)) return {
|
|
3243
|
+
status: "ready",
|
|
3244
|
+
readyData: this._finalizeTransportMethods(statusInfo.readyData)
|
|
3245
|
+
};
|
|
3420
3246
|
return {
|
|
3421
|
-
|
|
3422
|
-
|
|
3247
|
+
status: "initializing",
|
|
3248
|
+
timeStarted: Date.now(),
|
|
3249
|
+
initializationPromise: this._bringUp(statusInfo.readyData)
|
|
3423
3250
|
};
|
|
3424
3251
|
}
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
let session;
|
|
3432
|
-
let candidate;
|
|
3433
|
-
if (request.t != null) {
|
|
3434
|
-
session = this._sessions.get(request.t);
|
|
3435
|
-
if (session == null) return {
|
|
3436
|
-
k: "err",
|
|
3437
|
-
message: "unknown or expired session token"
|
|
3438
|
-
};
|
|
3439
|
-
if ("c" in request) {
|
|
3440
|
-
if (session.crypto == null) return {
|
|
3441
|
-
k: "err",
|
|
3442
|
-
message: "session is not encrypted"
|
|
3443
|
-
};
|
|
3444
|
-
const plain = await session.crypto.decryptFrame(base64ToBytes(request.c));
|
|
3445
|
-
candidate = JSON.parse(textDecoder$1.decode(plain));
|
|
3446
|
-
} else candidate = request.w;
|
|
3447
|
-
} else {
|
|
3448
|
-
if (!this._noneAllowed || "c" in request) return {
|
|
3449
|
-
k: "err",
|
|
3450
|
-
message: "missing session token"
|
|
3252
|
+
if (statusInfo.status === "initializing") {
|
|
3253
|
+
const initializationPromise = statusInfo.initializationPromise.then((result) => result.status === "ready" ? this._bringUp(result.readyData) : result);
|
|
3254
|
+
return {
|
|
3255
|
+
status: "initializing",
|
|
3256
|
+
timeStarted: statusInfo.timeStarted,
|
|
3257
|
+
initializationPromise
|
|
3451
3258
|
};
|
|
3452
|
-
candidate = request.w;
|
|
3453
3259
|
}
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
if (session != null && wire.type === "request") wire.context.originClient = session.client.toJsonObject();
|
|
3460
|
-
const resultWire = (await (await this._runtime.handleActionPayloadWire(wire)).waitForResultPayload()).toJsonObject();
|
|
3461
|
-
if (session?.crypto != null) return {
|
|
3462
|
-
k: "act",
|
|
3463
|
-
c: bytesToBase64(await session.crypto.encryptFrame(textEncoder$1.encode(JSON.stringify(resultWire))))
|
|
3464
|
-
};
|
|
3260
|
+
return statusInfo;
|
|
3261
|
+
}
|
|
3262
|
+
/** Await carrier readiness, then finalize (possibly running a handshake) into the live methods. */
|
|
3263
|
+
async _bringUp(readyData) {
|
|
3264
|
+
await this._awaitCarrierReady(readyData);
|
|
3465
3265
|
return {
|
|
3466
|
-
|
|
3467
|
-
|
|
3266
|
+
status: "ready",
|
|
3267
|
+
readyData: await this._finalizeReady(readyData)
|
|
3468
3268
|
};
|
|
3469
3269
|
}
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3270
|
+
};
|
|
3271
|
+
//#endregion
|
|
3272
|
+
//#region src/ActionRuntime/Transport/Exchange/ExchangeConnection.ts
|
|
3273
|
+
/**
|
|
3274
|
+
* Carrier-agnostic live connection for the exchange (request → single reply) shape — the HTTP
|
|
3275
|
+
* counterpart to {@link LinkConnection}. It owns only the bring-up (run the secure handshake on first
|
|
3276
|
+
* use); the request/reply lifecycle + crypto live in the shared `establishExchangeSession`.
|
|
3277
|
+
*/
|
|
3278
|
+
var ExchangeConnection = class extends TransportConnection {
|
|
3279
|
+
constructor(def) {
|
|
3280
|
+
super({
|
|
3281
|
+
...def,
|
|
3282
|
+
type: "exchange"
|
|
3283
|
+
});
|
|
3284
|
+
}
|
|
3285
|
+
_getCacheKey(input) {
|
|
3286
|
+
return this.initialized.getTransportCacheKey?.(input).join("\0") ?? "";
|
|
3287
|
+
}
|
|
3288
|
+
_needsAsyncBringUp(data) {
|
|
3289
|
+
return data.secureChannel != null && data.secureChannel.securityLevel !== "none";
|
|
3290
|
+
}
|
|
3291
|
+
_finalizeReady(data) {
|
|
3292
|
+
const secure = data.secureChannel;
|
|
3293
|
+
if (secure != null && secure.securityLevel !== "none") return finalizeSecureExchangeMethods({
|
|
3294
|
+
...this._sessionContext(data),
|
|
3295
|
+
secure
|
|
3474
3296
|
});
|
|
3297
|
+
return this._finalizeTransportMethods(data);
|
|
3298
|
+
}
|
|
3299
|
+
_finalizeTransportMethods(data) {
|
|
3300
|
+
return finalizePlainExchangeMethods(this._sessionContext(data));
|
|
3301
|
+
}
|
|
3302
|
+
_sessionContext(data) {
|
|
3303
|
+
return {
|
|
3304
|
+
carrier: data.carrier,
|
|
3305
|
+
updateRunConfig: data.updateRunConfig,
|
|
3306
|
+
secure: data.secureChannel
|
|
3307
|
+
};
|
|
3475
3308
|
}
|
|
3476
3309
|
};
|
|
3477
3310
|
//#endregion
|
|
3478
|
-
//#region src/ActionRuntime/
|
|
3479
|
-
/** Permissive defaults — fine for a public action endpoint; override (or disable) via `cors`. */
|
|
3480
|
-
const DEFAULT_CORS_HEADERS = {
|
|
3481
|
-
"Access-Control-Allow-Origin": "*",
|
|
3482
|
-
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
|
3483
|
-
"Access-Control-Allow-Headers": "Content-Type",
|
|
3484
|
-
"Access-Control-Max-Age": "86400"
|
|
3485
|
-
};
|
|
3311
|
+
//#region src/ActionRuntime/Transport/Exchange/ExchangeTransport.ts
|
|
3486
3312
|
/**
|
|
3487
|
-
*
|
|
3488
|
-
*
|
|
3489
|
-
*
|
|
3490
|
-
*
|
|
3491
|
-
*
|
|
3492
|
-
* It only touches web-standard `Request`/`Response`, so it stays transport-agnostic — the one
|
|
3493
|
-
* environment-specific bit (the WS upgrade) is injected via {@link IActionFetchHandlerOptions.onWebSocketUpgrade}:
|
|
3494
|
-
* ```ts
|
|
3495
|
-
* this.fetchHandler = createActionFetchHandler(this.runtime, {
|
|
3496
|
-
* onWebSocketUpgrade: () => {
|
|
3497
|
-
* const pair = new WebSocketPair();
|
|
3498
|
-
* this.ctx.acceptWebSocket(pair[1]);
|
|
3499
|
-
* return new Response(null, { status: 101, webSocket: pair[0] });
|
|
3500
|
-
* },
|
|
3501
|
-
* });
|
|
3502
|
-
* // async fetch(request) { return this.fetchHandler(request); }
|
|
3503
|
-
* ```
|
|
3313
|
+
* A carrier-agnostic exchange (request → single reply) transport: it drives nice-action's secure session
|
|
3314
|
+
* over any {@link IExchangeCarrier} (HTTP being the one built-in). The duplex counterpart is
|
|
3315
|
+
* {@link LinkTransport}; this is the no-push half — its reply rides the response to its own request, so it
|
|
3316
|
+
* can't deliver an unsolicited frame (the runtime never picks it for the return path).
|
|
3504
3317
|
*/
|
|
3505
|
-
|
|
3506
|
-
const corsHeaders = options.cors === false ? {} : options.cors ?? DEFAULT_CORS_HEADERS;
|
|
3507
|
-
const isActionPath = options.isActionPath ?? ((url) => url.pathname.endsWith("/action"));
|
|
3508
|
-
const isWebSocketPath = options.isWebSocketPath ?? ((url) => url.pathname.endsWith("/ws"));
|
|
3509
|
-
const exchangeAcceptor = options.security != null ? new ExchangeAcceptor({
|
|
3510
|
-
runtime,
|
|
3511
|
-
security: options.security
|
|
3512
|
-
}) : void 0;
|
|
3513
|
-
const withCors = (response) => {
|
|
3514
|
-
if (options.cors === false) return response;
|
|
3515
|
-
const headers = new Headers(response.headers);
|
|
3516
|
-
for (const [key, value] of Object.entries(corsHeaders)) headers.set(key, value);
|
|
3517
|
-
return new Response(response.body, {
|
|
3518
|
-
status: response.status,
|
|
3519
|
-
headers
|
|
3520
|
-
});
|
|
3521
|
-
};
|
|
3522
|
-
return async (request) => {
|
|
3523
|
-
if (request.method === "OPTIONS") return withCors(new Response(null, { status: 204 }));
|
|
3524
|
-
const url = new URL(request.url);
|
|
3525
|
-
const isWebSocketUpgrade = options.isWebSocketUpgrade ?? ((req, u) => req.headers.get("Upgrade") === "websocket" && isWebSocketPath(u));
|
|
3526
|
-
if (options.onWebSocketUpgrade != null && isWebSocketUpgrade(request, url)) return options.onWebSocketUpgrade(request, url);
|
|
3527
|
-
if (request.method === "POST" && isActionPath(url)) {
|
|
3528
|
-
if (exchangeAcceptor != null) {
|
|
3529
|
-
const reply = await exchangeAcceptor.handlePost(await request.text());
|
|
3530
|
-
return withCors(new Response(reply, {
|
|
3531
|
-
status: 200,
|
|
3532
|
-
headers: { "Content-Type": "application/json" }
|
|
3533
|
-
}));
|
|
3534
|
-
}
|
|
3535
|
-
return withCors((await (await runtime.handleActionPayloadWire(await request.json())).waitForResultPayload()).toHttpResponse({ useErrorStatus: options.useErrorStatus }));
|
|
3536
|
-
}
|
|
3537
|
-
return withCors(new Response("Not found", { status: 404 }));
|
|
3538
|
-
};
|
|
3539
|
-
}
|
|
3540
|
-
//#endregion
|
|
3541
|
-
//#region src/ActionRuntime/Handler/PeerLink/Acceptor/Hibernation/ConnectionStateStore.ts
|
|
3542
|
-
/**
|
|
3543
|
-
* A typed per-connection state store that co-owns the app state and the acceptor handler's routing
|
|
3544
|
-
* binding in one attachment, so neither the consumer nor the handler has to hand-merge the two. Create
|
|
3545
|
-
* it through {@link createConnectionStateStore} (which also wires binding persistence and replays
|
|
3546
|
-
* surviving connections after a wake), then `get`/`set`/`clearApp` the app state directly.
|
|
3547
|
-
*
|
|
3548
|
-
* The mechanism is carrier-neutral — it only needs read/write/enumerate callbacks for the connection's
|
|
3549
|
-
* attachment — but it pays off on transports whose connections outlive process eviction (e.g. a
|
|
3550
|
-
* Durable Object's hibernatable WebSockets), which is why it lives beside the hibernation adapter.
|
|
3551
|
-
*
|
|
3552
|
-
* ```ts
|
|
3553
|
-
* const players = createConnectionStateStore(serverHandler, {
|
|
3554
|
-
* schema: vs_player,
|
|
3555
|
-
* read: (ws) => ws.deserializeAttachment(),
|
|
3556
|
-
* write: (ws, v) => ws.serializeAttachment(v),
|
|
3557
|
-
* getConnections: () => ctx.getWebSockets(),
|
|
3558
|
-
* });
|
|
3559
|
-
* players.set(ws, player); // binding is preserved automatically
|
|
3560
|
-
* const player = players.get(ws);
|
|
3561
|
-
* ```
|
|
3562
|
-
*/
|
|
3563
|
-
var ConnectionStateStore = class {
|
|
3318
|
+
var ExchangeTransport = class ExchangeTransport extends Transport {
|
|
3564
3319
|
options;
|
|
3320
|
+
type = "exchange";
|
|
3565
3321
|
constructor(options) {
|
|
3322
|
+
super();
|
|
3566
3323
|
this.options = options;
|
|
3567
3324
|
}
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
return this._readAttachment(connection).app ?? null;
|
|
3571
|
-
}
|
|
3572
|
-
/** Set the app state, preserving the runtime binding already pinned to the connection. */
|
|
3573
|
-
set(connection, app) {
|
|
3574
|
-
const existing = this._readAttachment(connection);
|
|
3575
|
-
this.options.write(connection, {
|
|
3576
|
-
app,
|
|
3577
|
-
binding: existing.binding
|
|
3578
|
-
});
|
|
3579
|
-
}
|
|
3580
|
-
/** Clear the app state but keep the binding (e.g. a spectator that stopped watching). */
|
|
3581
|
-
clearApp(connection) {
|
|
3582
|
-
const existing = this._readAttachment(connection);
|
|
3583
|
-
this.options.write(connection, { binding: existing.binding });
|
|
3584
|
-
}
|
|
3585
|
-
/** Every live connection paired with its (validated) app state — for rebuilding in-memory state after a wake. */
|
|
3586
|
-
entries() {
|
|
3587
|
-
return this.options.getConnections().map((connection) => [connection, this._readAttachment(connection).app ?? null]);
|
|
3588
|
-
}
|
|
3589
|
-
/** @internal Persist a freshly-bound connection's binding, preserving any app state already stored. */
|
|
3590
|
-
_persistBinding(connection, binding) {
|
|
3591
|
-
const existing = this._readAttachment(connection);
|
|
3592
|
-
this.options.write(connection, {
|
|
3593
|
-
app: existing.app,
|
|
3594
|
-
binding
|
|
3595
|
-
});
|
|
3596
|
-
}
|
|
3597
|
-
/** @internal The persisted binding for a connection, if any (used to replay routing after a wake). */
|
|
3598
|
-
_readBinding(connection) {
|
|
3599
|
-
return this._readAttachment(connection).binding;
|
|
3325
|
+
static create(options) {
|
|
3326
|
+
return new ExchangeTransport(options);
|
|
3600
3327
|
}
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
3609
|
-
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
}
|
|
3328
|
+
_createConnection(_ctx) {
|
|
3329
|
+
const options = this.options;
|
|
3330
|
+
return new ExchangeConnection({ initialize: () => ({
|
|
3331
|
+
getTransportCacheKey: options.getTransportCacheKey,
|
|
3332
|
+
isAvailable: options.available,
|
|
3333
|
+
getTransport: (input) => ({
|
|
3334
|
+
status: "ready",
|
|
3335
|
+
readyData: {
|
|
3336
|
+
carrier: options.openCarrier(input),
|
|
3337
|
+
secureChannel: options.security,
|
|
3338
|
+
updateRunConfig: options.updateRunConfig
|
|
3339
|
+
}
|
|
3340
|
+
})
|
|
3341
|
+
}) });
|
|
3616
3342
|
}
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
return result.value;
|
|
3343
|
+
getRouteInfo(input) {
|
|
3344
|
+
if (this.options.getRouteInfo != null) return this.options.getRouteInfo(input);
|
|
3345
|
+
return {
|
|
3346
|
+
carrierLabel: this.options.label ?? "exchange",
|
|
3347
|
+
summary: this.options.label ?? "exchange"
|
|
3348
|
+
};
|
|
3624
3349
|
}
|
|
3625
3350
|
};
|
|
3626
|
-
/**
|
|
3627
|
-
* Build a per-connection {@link ConnectionStateStore} bound to an {@link AcceptorHandler}: it registers
|
|
3628
|
-
* itself as the handler's connection-bound persistence callback (so bindings are written without
|
|
3629
|
-
* overwriting app state) and immediately replays every live connection's stored binding via
|
|
3630
|
-
* {@link AcceptorHandler.rehydrateConnection} — so on a transport that resumes after eviction (e.g. a
|
|
3631
|
-
* Durable Object waking from hibernation) both the app identity and the action routing come back from a
|
|
3632
|
-
* single attachment, with no storage reads and no hand-rolled merge.
|
|
3633
|
-
*
|
|
3634
|
-
* Lives outside the handler so the generic {@link AcceptorHandler} stays free of any attachment/
|
|
3635
|
-
* hibernation concern — it exposes only the neutral `setOnConnectionBound` + `rehydrateConnection`
|
|
3636
|
-
* hooks this builder drives.
|
|
3637
|
-
*/
|
|
3638
|
-
function createConnectionStateStore(handler, options) {
|
|
3639
|
-
const store = new ConnectionStateStore(options);
|
|
3640
|
-
handler.setOnConnectionBound((connection, binding) => store._persistBinding(connection, binding));
|
|
3641
|
-
for (const connection of options.getConnections()) {
|
|
3642
|
-
const binding = store._readBinding(connection);
|
|
3643
|
-
if (binding != null) handler.rehydrateConnection(connection, binding);
|
|
3644
|
-
}
|
|
3645
|
-
return store;
|
|
3646
|
-
}
|
|
3647
3351
|
//#endregion
|
|
3648
|
-
//#region src/ActionRuntime/
|
|
3352
|
+
//#region src/ActionRuntime/Transport/helpers/createUnsetTransportResolvers.ts
|
|
3353
|
+
const createUnsetTransportResolvers = (transportLabel) => ({ onIncomingActionDataJson: (json) => {
|
|
3354
|
+
console.warn(`Received incoming action JSON [${json.domain}:${json.id}] on Transport [${transportLabel}] but no incoming data listener has been set.`);
|
|
3355
|
+
} });
|
|
3356
|
+
//#endregion
|
|
3357
|
+
//#region src/ActionRuntime/Transport/SecureSession/establishLinkSession.ts
|
|
3358
|
+
const HANDSHAKE_TIMEOUT_MS = 15e3;
|
|
3359
|
+
/** Plain path (no handshake): route every inbound frame to the runtime; send without crypto. */
|
|
3360
|
+
function finalizePlainLinkMethods(ctx) {
|
|
3361
|
+
const disconnectListeners = [];
|
|
3362
|
+
const abortSet = /* @__PURE__ */ new Set();
|
|
3363
|
+
const pipe = makePipe(ctx, void 0);
|
|
3364
|
+
ctx.channel.attach({
|
|
3365
|
+
onMessage: (frame) => void handleIncomingActionFrame(ctx, pipe, frame),
|
|
3366
|
+
onClose: () => onChannelClosed(ctx, disconnectListeners, abortSet),
|
|
3367
|
+
onError: () => onChannelClosed(ctx, disconnectListeners, abortSet)
|
|
3368
|
+
});
|
|
3369
|
+
return buildSendMethods(ctx, pipe, disconnectListeners, abortSet);
|
|
3370
|
+
}
|
|
3649
3371
|
/**
|
|
3650
|
-
*
|
|
3651
|
-
*
|
|
3652
|
-
*
|
|
3653
|
-
* live connection's stored binding via `getAttachment`, so results/pushes still route after a wake.
|
|
3654
|
-
*
|
|
3655
|
-
* Layered on top of the generic {@link AcceptorHandler} — it touches only the handler's neutral
|
|
3656
|
-
* `setOnConnectionBound` / `rehydrateConnection` / `receive` / `dropConnection` surface, so no
|
|
3657
|
-
* hibernation concern leaks into the handler itself.
|
|
3658
|
-
*
|
|
3659
|
-
* Construct it once when the handler is built, then forward connection events:
|
|
3660
|
-
* ```ts
|
|
3661
|
-
* const duplex = createHibernatableWsServerAdapter({ handler, getConnections, getAttachment, setAttachment });
|
|
3662
|
-
* // webSocketMessage(ws, msg) => duplex.receive(ws, msg);
|
|
3663
|
-
* // webSocketClose/Error(ws) => duplex.drop(ws);
|
|
3664
|
-
* ```
|
|
3372
|
+
* Secure path: a single message handler feeds the handshake until it completes, then routes action
|
|
3373
|
+
* frames (decrypting at the `encrypted` level). Frames that race ahead of activation are buffered and
|
|
3374
|
+
* flushed once the handshake lands, so nothing is lost.
|
|
3665
3375
|
*/
|
|
3666
|
-
function
|
|
3667
|
-
const
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
|
|
3675
|
-
|
|
3376
|
+
async function finalizeSecureLinkMethods(ctx) {
|
|
3377
|
+
const disconnectListeners = [];
|
|
3378
|
+
const abortSet = /* @__PURE__ */ new Set();
|
|
3379
|
+
const session = {};
|
|
3380
|
+
let active = false;
|
|
3381
|
+
const handshakeQueue = [];
|
|
3382
|
+
const handshakeWaiters = [];
|
|
3383
|
+
const pendingActionFrames = [];
|
|
3384
|
+
ctx.channel.attach({
|
|
3385
|
+
onMessage: (frame) => {
|
|
3386
|
+
if (active && session.pipe != null) {
|
|
3387
|
+
handleIncomingActionFrame(ctx, session.pipe, frame);
|
|
3388
|
+
return;
|
|
3389
|
+
}
|
|
3390
|
+
if (typeof frame === "string") {
|
|
3391
|
+
const message = decodeHandshakeMessage(frame);
|
|
3392
|
+
if (message != null) {
|
|
3393
|
+
const waiter = handshakeWaiters.shift();
|
|
3394
|
+
if (waiter != null) waiter(message);
|
|
3395
|
+
else handshakeQueue.push(message);
|
|
3396
|
+
return;
|
|
3397
|
+
}
|
|
3398
|
+
}
|
|
3399
|
+
pendingActionFrames.push(frame);
|
|
3400
|
+
},
|
|
3401
|
+
onClose: () => onChannelClosed(ctx, disconnectListeners, abortSet),
|
|
3402
|
+
onError: () => onChannelClosed(ctx, disconnectListeners, abortSet)
|
|
3403
|
+
});
|
|
3404
|
+
const nextHandshakeMessage = () => {
|
|
3405
|
+
const queued = handshakeQueue.shift();
|
|
3406
|
+
if (queued != null) return Promise.resolve(queued);
|
|
3407
|
+
return new Promise((resolve, reject) => {
|
|
3408
|
+
const timeout = setTimeout(() => reject(/* @__PURE__ */ new Error("[link-handshake] timed out waiting for peer reply")), HANDSHAKE_TIMEOUT_MS);
|
|
3409
|
+
handshakeWaiters.push((message) => {
|
|
3410
|
+
clearTimeout(timeout);
|
|
3411
|
+
resolve(message);
|
|
3412
|
+
});
|
|
3413
|
+
});
|
|
3676
3414
|
};
|
|
3415
|
+
const pipe = makePipe(ctx, await runClientHandshake(ctx.channel, ctx.secure, nextHandshakeMessage));
|
|
3416
|
+
session.pipe = pipe;
|
|
3417
|
+
active = true;
|
|
3418
|
+
for (const frame of pendingActionFrames) await handleIncomingActionFrame(ctx, pipe, frame);
|
|
3419
|
+
pendingActionFrames.length = 0;
|
|
3420
|
+
return buildSendMethods(ctx, pipe, disconnectListeners, abortSet);
|
|
3677
3421
|
}
|
|
3678
|
-
|
|
3679
|
-
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
3683
|
-
"authenticated",
|
|
3684
|
-
"encrypted"
|
|
3685
|
-
];
|
|
3686
|
-
function serveChannel(runtime, channel, options) {
|
|
3687
|
-
const duplexCarriers = options.carriers.filter((carrier) => !require_wsAcceptorCarrier.isExchangeAcceptorCarrier(carrier));
|
|
3688
|
-
const exchangeCarriers = options.carriers.filter(require_wsAcceptorCarrier.isExchangeAcceptorCarrier);
|
|
3689
|
-
if (exchangeCarriers.length > 1) throw new Error("serveChannel: at most one exchange carrier is supported");
|
|
3690
|
-
const exchangeCarrier = exchangeCarriers[0];
|
|
3691
|
-
const singleDuplex = duplexCarriers.length === 1;
|
|
3692
|
-
if (options.connectionState != null && !singleDuplex) throw new Error("serveChannel: `connectionState` requires exactly one duplex carrier");
|
|
3693
|
-
if (options.channelCases != null && !singleDuplex) throw new Error("serveChannel: `channelCases` requires exactly one duplex carrier");
|
|
3694
|
-
const exchangeSecure = exchangeCarrier != null && (exchangeCarrier.secure ?? true);
|
|
3695
|
-
const anyDuplexSecure = duplexCarriers.some((carrier) => carrier.secure ?? true);
|
|
3696
|
-
const securityLevel = options.securityLevel ?? DEFAULT_SERVER_SECURITY_LEVELS;
|
|
3697
|
-
let secure;
|
|
3698
|
-
if (anyDuplexSecure || exchangeSecure) {
|
|
3699
|
-
const storage = options.storage;
|
|
3700
|
-
if (storage == null) throw new Error("serveChannel: a secure carrier requires `storage`. Pass it, or set `secure: false` on the carrier for a plain endpoint.");
|
|
3701
|
-
secure = {
|
|
3702
|
-
storage,
|
|
3703
|
-
link: options.link ?? new _nice_code_util.ClientCryptoKeyLink({ storageAdapter: storage }),
|
|
3704
|
-
verifyKeyResolver: options.verifyKeyResolver ?? createStorageTofuVerifyKeyResolver(storage)
|
|
3705
|
-
};
|
|
3706
|
-
}
|
|
3707
|
-
const plainRouter = (handler) => ({
|
|
3708
|
-
receive: (connection, frame) => handler.receive(connection, frame),
|
|
3709
|
-
drop: (connection) => handler.dropConnection(connection)
|
|
3422
|
+
function makePipe(ctx, crypto) {
|
|
3423
|
+
return createFrameCryptoPipe({
|
|
3424
|
+
write: (frame) => ctx.channel.send(frame),
|
|
3425
|
+
isOpen: () => ctx.channel.isOpen(),
|
|
3426
|
+
crypto
|
|
3710
3427
|
});
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
const handler = (carrier.secure ?? true) && secure != null ? acceptChannel(runtime, channel, {
|
|
3716
|
-
clientEnv: options.clientEnv,
|
|
3717
|
-
storageAdapter: secure.storage,
|
|
3718
|
-
link: secure.link,
|
|
3719
|
-
verifyKeyResolver: secure.verifyKeyResolver,
|
|
3720
|
-
securityLevel,
|
|
3721
|
-
send: carrier.send,
|
|
3722
|
-
defaultTimeout: options.defaultTimeout
|
|
3723
|
-
}) : createAcceptorHandler({
|
|
3724
|
-
clientEnv: options.clientEnv,
|
|
3725
|
-
createFormatMessage: channel.createCodec,
|
|
3726
|
-
send: carrier.send,
|
|
3727
|
-
runtime,
|
|
3728
|
-
defaultTimeout: options.defaultTimeout
|
|
3729
|
-
});
|
|
3730
|
-
const attach = carrier.attachmentStore;
|
|
3731
|
-
let router;
|
|
3732
|
-
if (attach == null) router = plainRouter(handler);
|
|
3733
|
-
else if (options.connectionState != null) {
|
|
3734
|
-
connections = createConnectionStateStore(handler, {
|
|
3735
|
-
schema: options.connectionState.schema,
|
|
3736
|
-
getConnections: attach.getConnections,
|
|
3737
|
-
read: attach.read,
|
|
3738
|
-
write: attach.write
|
|
3739
|
-
});
|
|
3740
|
-
router = plainRouter(handler);
|
|
3741
|
-
} else router = createHibernatableWsServerAdapter({
|
|
3742
|
-
handler,
|
|
3743
|
-
getConnections: attach.getConnections,
|
|
3744
|
-
getAttachment: (connection) => attach.read(connection)?.binding,
|
|
3745
|
-
setAttachment: (connection, binding) => attach.write(connection, {
|
|
3746
|
-
...asObject(attach.read(connection)),
|
|
3747
|
-
binding
|
|
3748
|
-
})
|
|
3749
|
-
});
|
|
3750
|
-
carrier._activate(router);
|
|
3751
|
-
handlers.push(handler);
|
|
3752
|
-
}
|
|
3753
|
-
runtime.addHandlers([...options.handlers ?? [], ...handlers]);
|
|
3754
|
-
if (options.channelCases != null) runtime.addHandlers([acceptChannelConnections(handlers[0], channel, options.channelCases)]);
|
|
3755
|
-
const exchangeSecurity = exchangeSecure && secure != null ? {
|
|
3428
|
+
}
|
|
3429
|
+
async function runClientHandshake(channel, secure, nextHandshakeMessage) {
|
|
3430
|
+
await secure.link.initialize();
|
|
3431
|
+
const handshake = createClientHandshake({
|
|
3756
3432
|
link: secure.link,
|
|
3757
|
-
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
securityLevel
|
|
3761
|
-
} : void 0;
|
|
3762
|
-
const defaultIsUpgrade = (request) => request.headers.get("Upgrade") === "websocket";
|
|
3763
|
-
const upgraders = [];
|
|
3764
|
-
for (const carrier of duplexCarriers) {
|
|
3765
|
-
if (carrier.upgrade == null) continue;
|
|
3766
|
-
upgraders.push({
|
|
3767
|
-
isUpgrade: carrier.isUpgrade ?? defaultIsUpgrade,
|
|
3768
|
-
upgrade: carrier.upgrade
|
|
3769
|
-
});
|
|
3770
|
-
}
|
|
3771
|
-
const fetch = createActionFetchHandler(runtime, {
|
|
3772
|
-
cors: exchangeCarrier?.cors,
|
|
3773
|
-
onWebSocketUpgrade: upgraders.length === 0 ? void 0 : (request, url) => (upgraders.find((u) => u.isUpgrade(request, url)) ?? upgraders[0]).upgrade(request, url),
|
|
3774
|
-
isWebSocketUpgrade: upgraders.length === 0 ? void 0 : (request, url) => upgraders.some((u) => u.isUpgrade(request, url)),
|
|
3775
|
-
isActionPath: exchangeCarrier != null ? exchangeCarrier.isActionPath ?? (() => true) : () => false,
|
|
3776
|
-
security: exchangeSecurity,
|
|
3777
|
-
useErrorStatus: exchangeCarrier?.useErrorStatus
|
|
3433
|
+
localCoordinate: secure.localCoordinate,
|
|
3434
|
+
dictionaryVersion: secure.dictionaryVersion,
|
|
3435
|
+
securityLevel: secure.securityLevel
|
|
3778
3436
|
});
|
|
3779
|
-
|
|
3780
|
-
const
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
3789
|
-
|
|
3790
|
-
|
|
3791
|
-
};
|
|
3792
|
-
return {
|
|
3793
|
-
handlers,
|
|
3794
|
-
fetch,
|
|
3795
|
-
duplex,
|
|
3796
|
-
pushToClient,
|
|
3797
|
-
broadcast,
|
|
3798
|
-
connections
|
|
3799
|
-
};
|
|
3437
|
+
channel.send(encodeHandshakeMessage(await handshake.createHello()));
|
|
3438
|
+
const welcome = await nextHandshakeMessage();
|
|
3439
|
+
if (welcome.t === "reject") throw new Error(`[link-handshake] rejected by peer: ${welcome.reason}`);
|
|
3440
|
+
if (welcome.t !== "welcome") throw new Error(`[link-handshake] expected welcome, got ${welcome.t}`);
|
|
3441
|
+
channel.send(encodeHandshakeMessage(await handshake.onWelcome(welcome)));
|
|
3442
|
+
const accept = await nextHandshakeMessage();
|
|
3443
|
+
if (accept.t === "reject") throw new Error(`[link-handshake] rejected by peer: ${accept.reason}`);
|
|
3444
|
+
if (accept.t !== "accept") throw new Error(`[link-handshake] expected accept, got ${accept.t}`);
|
|
3445
|
+
const result = await handshake.onAccept(accept);
|
|
3446
|
+
return result.securityLevel === "encrypted" ? createActionFrameCrypto({
|
|
3447
|
+
link: secure.link,
|
|
3448
|
+
linkedClientId: result.linkedClientId
|
|
3449
|
+
}) : void 0;
|
|
3800
3450
|
}
|
|
3801
|
-
|
|
3802
|
-
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
serverClose?.();
|
|
3823
|
-
});
|
|
3451
|
+
function buildSendMethods(ctx, pipe, disconnectListeners, abortSet) {
|
|
3452
|
+
const channel = ctx.channel;
|
|
3453
|
+
const sendActionData = (inputs) => {
|
|
3454
|
+
const { action, runningAction, timeout } = inputs;
|
|
3455
|
+
if (!channel.isOpen()) {
|
|
3456
|
+
if (action.type === "request") runningAction._abort(ctx.makeDisconnectError(action.id));
|
|
3457
|
+
return;
|
|
3458
|
+
}
|
|
3459
|
+
if (action.type === "request") {
|
|
3460
|
+
abortSet.add(runningAction);
|
|
3461
|
+
const timeoutId = setTimeout(() => {
|
|
3462
|
+
runningAction._abort(err_nice_transport.fromId("timeout", { timeout }));
|
|
3463
|
+
}, timeout);
|
|
3464
|
+
runningAction.addUpdateListeners([(update) => {
|
|
3465
|
+
if (update.type === "finished") {
|
|
3466
|
+
clearTimeout(timeoutId);
|
|
3467
|
+
abortSet.delete(runningAction);
|
|
3468
|
+
}
|
|
3469
|
+
}]);
|
|
3470
|
+
}
|
|
3471
|
+
pipe.send(ctx.formatMessage?.outgoing(inputs) ?? JSON.stringify(inputs.action.toJsonObject()));
|
|
3824
3472
|
};
|
|
3825
3473
|
return {
|
|
3826
|
-
|
|
3827
|
-
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
if (!open) return;
|
|
3831
|
-
queueMicrotask(() => serverMessage?.(frame));
|
|
3832
|
-
},
|
|
3833
|
-
attach: ({ onMessage, onClose }) => {
|
|
3834
|
-
clientMessage = onMessage;
|
|
3835
|
-
clientClose = onClose;
|
|
3836
|
-
},
|
|
3837
|
-
close: closeBoth,
|
|
3838
|
-
label: "in-memory"
|
|
3474
|
+
sendActionData,
|
|
3475
|
+
updateRunConfig: ctx.updateRunConfig,
|
|
3476
|
+
addOnDisconnectListener: (cb) => {
|
|
3477
|
+
disconnectListeners.push(cb);
|
|
3839
3478
|
},
|
|
3840
|
-
|
|
3841
|
-
|
|
3842
|
-
|
|
3843
|
-
|
|
3844
|
-
|
|
3845
|
-
|
|
3846
|
-
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
close: closeBoth
|
|
3479
|
+
disconnect: () => {
|
|
3480
|
+
try {
|
|
3481
|
+
channel.close();
|
|
3482
|
+
} catch {}
|
|
3483
|
+
},
|
|
3484
|
+
sendReturnData: (payload, clients) => {
|
|
3485
|
+
const formatted = clients != null ? ctx.formatMessage?.outgoing({
|
|
3486
|
+
action: payload,
|
|
3487
|
+
...clients
|
|
3488
|
+
}) : void 0;
|
|
3489
|
+
pipe.send(formatted ?? JSON.stringify(payload.toJsonObject()));
|
|
3852
3490
|
}
|
|
3853
3491
|
};
|
|
3854
3492
|
}
|
|
3493
|
+
async function handleIncomingActionFrame(ctx, pipe, frame) {
|
|
3494
|
+
const decoded = await pipe.decryptIncoming(frame);
|
|
3495
|
+
if (decoded === void 0) return;
|
|
3496
|
+
const rawJson = decodeActionFrame(decoded, ctx.formatMessage);
|
|
3497
|
+
if (rawJson != null) ctx.resolvers.onIncomingActionDataJson(rawJson);
|
|
3498
|
+
}
|
|
3499
|
+
function onChannelClosed(ctx, disconnectListeners, abortSet) {
|
|
3500
|
+
for (const cb of disconnectListeners) cb();
|
|
3501
|
+
const error = ctx.makeDisconnectError("—");
|
|
3502
|
+
for (const ra of [...abortSet]) ra._abort(error);
|
|
3503
|
+
}
|
|
3855
3504
|
//#endregion
|
|
3856
|
-
//#region src/ActionRuntime/Transport/
|
|
3505
|
+
//#region src/ActionRuntime/Transport/Link/LinkConnection.ts
|
|
3506
|
+
/** Abort error for a closed link channel (carrier-neutral — the carrier itself isn't named). */
|
|
3507
|
+
function linkDisconnectError(actionId) {
|
|
3508
|
+
return err_nice_transport.fromId("send_failed", {
|
|
3509
|
+
actionId,
|
|
3510
|
+
actionState: "request",
|
|
3511
|
+
message: "link channel disconnected"
|
|
3512
|
+
});
|
|
3513
|
+
}
|
|
3857
3514
|
/**
|
|
3858
|
-
*
|
|
3859
|
-
*
|
|
3860
|
-
*
|
|
3861
|
-
*
|
|
3515
|
+
* Carrier-agnostic live connection. It owns only the *bring-up* (open the carrier, then run the secure
|
|
3516
|
+
* session); the session itself — handshake, frame crypto, codec, send/receive — lives in the shared
|
|
3517
|
+
* {@link finalizeSecureLinkMethods}/{@link finalizePlainLinkMethods}, so a WebSocket, a WebRTC data
|
|
3518
|
+
* channel, a Bluetooth characteristic, and an in-memory pipe all run the identical secure layer.
|
|
3862
3519
|
*/
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3520
|
+
var LinkConnection = class extends TransportConnection {
|
|
3521
|
+
resolvers;
|
|
3522
|
+
constructor(def, resolvers) {
|
|
3523
|
+
super({
|
|
3524
|
+
...def,
|
|
3525
|
+
type: "duplex"
|
|
3526
|
+
});
|
|
3527
|
+
this.resolvers = resolvers ?? createUnsetTransportResolvers("link");
|
|
3528
|
+
}
|
|
3529
|
+
_getCacheKey(input) {
|
|
3530
|
+
return this.initialized.getTransportCacheKey?.(input).join("\0") ?? "";
|
|
3531
|
+
}
|
|
3532
|
+
_needsAsyncBringUp() {
|
|
3533
|
+
return true;
|
|
3534
|
+
}
|
|
3535
|
+
_awaitCarrierReady(data) {
|
|
3536
|
+
return data.channel.ready;
|
|
3537
|
+
}
|
|
3538
|
+
_finalizeReady(data) {
|
|
3539
|
+
const secure = data.secureChannel;
|
|
3540
|
+
if (secure != null && secure.securityLevel !== "none") return finalizeSecureLinkMethods({
|
|
3541
|
+
...this._sessionContext(data),
|
|
3542
|
+
secure
|
|
3543
|
+
});
|
|
3544
|
+
return this._finalizeTransportMethods(data);
|
|
3545
|
+
}
|
|
3546
|
+
_sessionContext(data) {
|
|
3547
|
+
return {
|
|
3548
|
+
channel: data.channel,
|
|
3549
|
+
resolvers: this.resolvers,
|
|
3550
|
+
formatMessage: data.formatMessage,
|
|
3551
|
+
updateRunConfig: data.updateRunConfig,
|
|
3552
|
+
makeDisconnectError: linkDisconnectError
|
|
3553
|
+
};
|
|
3554
|
+
}
|
|
3555
|
+
_finalizeTransportMethods(data) {
|
|
3556
|
+
return finalizePlainLinkMethods(this._sessionContext(data));
|
|
3557
|
+
}
|
|
3558
|
+
};
|
|
3874
3559
|
//#endregion
|
|
3875
|
-
//#region src/ActionRuntime/Transport/
|
|
3560
|
+
//#region src/ActionRuntime/Transport/Link/LinkTransport.ts
|
|
3876
3561
|
/**
|
|
3877
|
-
*
|
|
3878
|
-
*
|
|
3879
|
-
*
|
|
3880
|
-
*
|
|
3881
|
-
* The data channel must already be created (its negotiation/signaling is the app's concern); this only
|
|
3882
|
-
* drives bytes over it. Binary frames are requested as `ArrayBuffer` so the binary session codec unpacks
|
|
3883
|
-
* them synchronously; a `Blob` (if the channel hands one back) is normalized to a buffer.
|
|
3562
|
+
* A carrier-agnostic transport: it drives nice-action's secure session + action routing over any
|
|
3563
|
+
* {@link IDuplexCarrier}. The WebSocket transport is the special case that opens a `WebSocket`;
|
|
3564
|
+
* this opens whatever `openChannel` returns, so the identical secure layer works over WebRTC, Bluetooth,
|
|
3565
|
+
* or an in-memory pipe. Reported with an overridable carrier label in the devtools (defaults to "link").
|
|
3884
3566
|
*/
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
}
|
|
3895
|
-
|
|
3896
|
-
const
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
3567
|
+
var LinkTransport = class LinkTransport extends Transport {
|
|
3568
|
+
options;
|
|
3569
|
+
type = "duplex";
|
|
3570
|
+
constructor(options) {
|
|
3571
|
+
super();
|
|
3572
|
+
this.options = options;
|
|
3573
|
+
}
|
|
3574
|
+
static create(options) {
|
|
3575
|
+
return new LinkTransport(options);
|
|
3576
|
+
}
|
|
3577
|
+
_createConnection(ctx) {
|
|
3578
|
+
const options = this.options;
|
|
3579
|
+
return new LinkConnection({ initialize: () => ({
|
|
3580
|
+
getTransportCacheKey: options.getTransportCacheKey,
|
|
3581
|
+
isAvailable: options.available,
|
|
3582
|
+
getTransport: (input) => ({
|
|
3583
|
+
status: "ready",
|
|
3584
|
+
readyData: {
|
|
3585
|
+
channel: options.openChannel(input),
|
|
3586
|
+
formatMessage: options.createFormatMessage?.() ?? options.formatMessage,
|
|
3587
|
+
updateRunConfig: options.updateRunConfig,
|
|
3588
|
+
secureChannel: options.security
|
|
3589
|
+
}
|
|
3590
|
+
})
|
|
3591
|
+
}) }, ctx.resolvers);
|
|
3592
|
+
}
|
|
3593
|
+
getRouteInfo(input) {
|
|
3594
|
+
if (this.options.getRouteInfo != null) return this.options.getRouteInfo(input);
|
|
3595
|
+
return {
|
|
3596
|
+
carrierLabel: this.options.label ?? "link",
|
|
3597
|
+
summary: this.options.label ?? "link"
|
|
3598
|
+
};
|
|
3599
|
+
}
|
|
3600
|
+
};
|
|
3601
|
+
//#endregion
|
|
3602
|
+
//#region src/ActionRuntime/Transport/plainTransport.ts
|
|
3603
|
+
function plainTransport(options) {
|
|
3604
|
+
const carrier = options.carrier;
|
|
3605
|
+
if (isExchangeCarrierSource(carrier)) return ExchangeTransport.create({
|
|
3606
|
+
openCarrier: carrier.open,
|
|
3607
|
+
getTransportCacheKey: carrier.getCacheKey,
|
|
3608
|
+
available: options.available,
|
|
3609
|
+
getRouteInfo: carrier.getRouteInfo,
|
|
3610
|
+
label: options.label ?? carrier.carrierLabel,
|
|
3611
|
+
updateRunConfig: options.updateRunConfig
|
|
3902
3612
|
});
|
|
3903
|
-
|
|
3904
|
-
|
|
3905
|
-
|
|
3613
|
+
return LinkTransport.create({
|
|
3614
|
+
openChannel: carrier.open,
|
|
3615
|
+
formatMessage: options.formatMessage,
|
|
3616
|
+
createFormatMessage: options.createFormatMessage,
|
|
3617
|
+
getTransportCacheKey: carrier.getCacheKey,
|
|
3618
|
+
available: options.available,
|
|
3619
|
+
getRouteInfo: carrier.getRouteInfo,
|
|
3620
|
+
label: options.label ?? carrier.carrierLabel,
|
|
3621
|
+
updateRunConfig: options.updateRunConfig
|
|
3906
3622
|
});
|
|
3907
|
-
return {
|
|
3908
|
-
ready: new Promise((resolve, reject) => {
|
|
3909
|
-
if (dc.readyState === "open") {
|
|
3910
|
-
resolve();
|
|
3911
|
-
return;
|
|
3912
|
-
}
|
|
3913
|
-
dc.addEventListener("open", () => resolve(), { once: true });
|
|
3914
|
-
dc.addEventListener("error", (event) => reject(event), { once: true });
|
|
3915
|
-
dc.addEventListener("close", () => reject(/* @__PURE__ */ new Error("RTCDataChannel closed before open")), { once: true });
|
|
3916
|
-
}),
|
|
3917
|
-
isOpen: () => dc.readyState === "open",
|
|
3918
|
-
send: (frame) => {
|
|
3919
|
-
if (typeof frame === "string" || frame instanceof ArrayBuffer) dc.send(frame);
|
|
3920
|
-
else dc.send(new Uint8Array(frame));
|
|
3921
|
-
},
|
|
3922
|
-
attach: (handlers) => {
|
|
3923
|
-
onMessage = handlers.onMessage;
|
|
3924
|
-
onClose = handlers.onClose;
|
|
3925
|
-
for (const frame of preAttach) handlers.onMessage(frame);
|
|
3926
|
-
preAttach.length = 0;
|
|
3927
|
-
},
|
|
3928
|
-
close: () => {
|
|
3929
|
-
intentional = true;
|
|
3930
|
-
try {
|
|
3931
|
-
dc.close();
|
|
3932
|
-
} catch {}
|
|
3933
|
-
},
|
|
3934
|
-
get label() {
|
|
3935
|
-
return dc.label != null && dc.label !== "" ? dc.label : void 0;
|
|
3936
|
-
}
|
|
3937
|
-
};
|
|
3938
3623
|
}
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3624
|
+
//#endregion
|
|
3625
|
+
//#region src/ActionRuntime/Transport/secureTransport.ts
|
|
3626
|
+
function secureTransport(options) {
|
|
3627
|
+
const link = options.link ?? (options.storageAdapter != null ? new _nice_code_util.ClientCryptoKeyLink({ storageAdapter: options.storageAdapter }) : void 0);
|
|
3628
|
+
if (link == null) throw new Error("secureTransport: provide `link` or `storageAdapter` for the crypto identity.");
|
|
3629
|
+
const security = {
|
|
3630
|
+
securityLevel: options.securityLevel,
|
|
3631
|
+
link,
|
|
3632
|
+
localCoordinate: options.runtime.coordinate.toJsonObject(),
|
|
3633
|
+
dictionaryVersion: options.channel.dictionaryVersion
|
|
3634
|
+
};
|
|
3635
|
+
const carrier = options.carrier;
|
|
3636
|
+
if (isExchangeCarrierSource(carrier)) return ExchangeTransport.create({
|
|
3637
|
+
openCarrier: carrier.open,
|
|
3638
|
+
getTransportCacheKey: carrier.getCacheKey,
|
|
3639
|
+
available: options.available,
|
|
3640
|
+
getRouteInfo: carrier.getRouteInfo,
|
|
3641
|
+
label: carrier.carrierLabel,
|
|
3642
|
+
security
|
|
3643
|
+
});
|
|
3644
|
+
return LinkTransport.create({
|
|
3645
|
+
openChannel: carrier.open,
|
|
3646
|
+
createFormatMessage: options.channel.createCodec,
|
|
3647
|
+
getTransportCacheKey: carrier.getCacheKey,
|
|
3648
|
+
available: options.available,
|
|
3649
|
+
getRouteInfo: carrier.getRouteInfo,
|
|
3650
|
+
label: carrier.carrierLabel,
|
|
3651
|
+
security
|
|
3652
|
+
});
|
|
3942
3653
|
}
|
|
3943
3654
|
//#endregion
|
|
3944
|
-
//#region src/ActionRuntime/
|
|
3655
|
+
//#region src/ActionRuntime/Channel/ActionChannel.ts
|
|
3945
3656
|
/**
|
|
3946
|
-
*
|
|
3947
|
-
*
|
|
3948
|
-
*
|
|
3657
|
+
* Declare a transport-agnostic channel by role. Use it for HTTP, custom transports, or as the routing
|
|
3658
|
+
* half of a richer channel. The order of each list is part of the contract for wire formats that pack
|
|
3659
|
+
* positionally (see `defineSecureChannel`) — add new domains to the end of their list. (`domains` is
|
|
3660
|
+
* accepted as a legacy alias for `toAcceptor`.)
|
|
3949
3661
|
*/
|
|
3950
|
-
function
|
|
3662
|
+
function defineChannel(options) {
|
|
3951
3663
|
return {
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
getCacheKey: options.getTransportCacheKey ?? (() => ["webrtc"]),
|
|
3955
|
-
getRouteInfo: options.getRouteInfo
|
|
3664
|
+
toAcceptorDomains: options.toAcceptor,
|
|
3665
|
+
toConnectorDomains: options.toConnector
|
|
3956
3666
|
};
|
|
3957
3667
|
}
|
|
3958
|
-
//#endregion
|
|
3959
|
-
//#region src/ActionRuntime/Transport/Carrier/duplex/ws/err_nice_transport_ws.ts
|
|
3960
|
-
let EErrId_NiceTransport_WebSocket = /* @__PURE__ */ function(EErrId_NiceTransport_WebSocket) {
|
|
3961
|
-
EErrId_NiceTransport_WebSocket["ws_disconnected"] = "ws_disconnected";
|
|
3962
|
-
EErrId_NiceTransport_WebSocket["ws_create_failed"] = "ws_create_failed";
|
|
3963
|
-
EErrId_NiceTransport_WebSocket["ws_error"] = "ws_error";
|
|
3964
|
-
return EErrId_NiceTransport_WebSocket;
|
|
3965
|
-
}({});
|
|
3966
|
-
const err_nice_transport_ws = err_nice_transport.createChildDomain({
|
|
3967
|
-
domain: "ws_transport",
|
|
3968
|
-
schema: {
|
|
3969
|
-
["ws_disconnected"]: (0, _nice_code_error.err)({ message: () => `WebSocket transport disconnected.` }),
|
|
3970
|
-
["ws_create_failed"]: (0, _nice_code_error.err)({ message: ({ originalError }) => `Failed to create WebSocket transport.${originalError ? ` Original error: ${originalError.message}` : ""}` }),
|
|
3971
|
-
["ws_error"]: (0, _nice_code_error.err)({ message: ({ originalError }) => `WebSocket transport error.${originalError ? ` Original error: ${originalError.message}` : ""}` })
|
|
3972
|
-
}
|
|
3973
|
-
});
|
|
3974
|
-
//#endregion
|
|
3975
|
-
//#region src/ActionRuntime/Transport/Carrier/duplex/ws/ws_util.ts
|
|
3976
3668
|
/**
|
|
3977
|
-
*
|
|
3978
|
-
*
|
|
3979
|
-
*
|
|
3980
|
-
*
|
|
3669
|
+
* Open a connection to a peer from a single call — the dial-out dual of `serveChannel`. The channel is
|
|
3670
|
+
* the single source of truth for *what* is routed (`toAcceptor` domains forwarded to the peer,
|
|
3671
|
+
* `toConnector` pushes handled locally from `onPush`); the call binds the shared facts — the channel's
|
|
3672
|
+
* codec/dictionary version, the runtime, and one crypto identity (a {@link ClientCryptoKeyLink} over
|
|
3673
|
+
* `storage`) — into every transport in `transports`, so none of them restate the channel or runtime.
|
|
3674
|
+
* List several transports to make the path transport-agnostic (secure WS preferred, HTTP fallback):
|
|
3675
|
+
* ```ts
|
|
3676
|
+
* const handler = connectChannel(runtime, lobbyChannel, {
|
|
3677
|
+
* peer: runtime_coordinate_lobby_do,
|
|
3678
|
+
* storage,
|
|
3679
|
+
* transports: [{ carrier: wsCarrier(url) }, { carrier: httpCarrier(...), secure: false }],
|
|
3680
|
+
* onPush: { player_joined: (p) => { … } },
|
|
3681
|
+
* });
|
|
3682
|
+
* ```
|
|
3683
|
+
* Returns the {@link ConnectorHandler} so the caller can later `clearTransportCache()` it.
|
|
3981
3684
|
*/
|
|
3982
|
-
function
|
|
3983
|
-
|
|
3984
|
-
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
|
|
3685
|
+
function connectChannel(runtime, channel, options) {
|
|
3686
|
+
const securityLevel = options.securityLevel ?? "authenticated";
|
|
3687
|
+
const anySecure = options.transports.some((transport) => transport.secure ?? true);
|
|
3688
|
+
let link = options.link;
|
|
3689
|
+
if (anySecure && link == null) {
|
|
3690
|
+
if (options.storage == null) throw new Error("connectChannel: a secure transport requires `storage` (or `link`). Pass it, or set `secure: false` on the transport for a plain connection.");
|
|
3691
|
+
link = new _nice_code_util.ClientCryptoKeyLink({ storageAdapter: options.storage });
|
|
3692
|
+
}
|
|
3693
|
+
const transports = options.transports.map((transport) => {
|
|
3694
|
+
const carrier = transport.carrier;
|
|
3695
|
+
const secure = transport.secure ?? true;
|
|
3696
|
+
if (isExchangeCarrierSource(carrier)) return secure ? secureTransport({
|
|
3697
|
+
channel,
|
|
3698
|
+
runtime,
|
|
3699
|
+
link,
|
|
3700
|
+
securityLevel: transport.securityLevel ?? securityLevel,
|
|
3701
|
+
available: transport.available,
|
|
3702
|
+
carrier
|
|
3703
|
+
}) : plainTransport({
|
|
3704
|
+
carrier,
|
|
3705
|
+
available: transport.available,
|
|
3706
|
+
label: transport.label
|
|
3707
|
+
});
|
|
3708
|
+
return secure ? secureTransport({
|
|
3709
|
+
channel,
|
|
3710
|
+
runtime,
|
|
3711
|
+
link,
|
|
3712
|
+
securityLevel: transport.securityLevel ?? securityLevel,
|
|
3713
|
+
available: transport.available,
|
|
3714
|
+
carrier
|
|
3715
|
+
}) : plainTransport({
|
|
3716
|
+
carrier,
|
|
3717
|
+
createFormatMessage: channel.createCodec,
|
|
3718
|
+
available: transport.available,
|
|
3719
|
+
label: transport.label
|
|
3720
|
+
});
|
|
3721
|
+
});
|
|
3722
|
+
const pushHandlers = options.onPush != null ? channel.toConnectorDomains.map((domain) => domain.wrapAsPartialLocalHandler(options.onPush)) : [];
|
|
3723
|
+
return runtime.connectTo(options.peer, {
|
|
3724
|
+
transports,
|
|
3725
|
+
domains: [...channel.toAcceptorDomains],
|
|
3726
|
+
localHandlers: pushHandlers,
|
|
3727
|
+
defaultTimeout: options.defaultTimeout
|
|
3728
|
+
});
|
|
3988
3729
|
}
|
|
3989
|
-
/**
|
|
3990
|
-
|
|
3991
|
-
|
|
3992
|
-
|
|
3993
|
-
|
|
3994
|
-
|
|
3995
|
-
|
|
3996
|
-
|
|
3730
|
+
/**
|
|
3731
|
+
* Register an acceptor handler's execution for a channel straight from its definition: the channel's
|
|
3732
|
+
* `toAcceptor` domains are served together with one merged, connection-aware case map (each case gets
|
|
3733
|
+
* the primed request + the originating connection, as with
|
|
3734
|
+
* {@link AcceptorHandler.forConnectionDomainCases}). The domain list is taken from the channel,
|
|
3735
|
+
* never restated. Add the returned handler to the runtime alongside the acceptor handler:
|
|
3736
|
+
* ```ts
|
|
3737
|
+
* runtime.addHandlers([acceptChannelConnections(serverHandler, channel, { … }), serverHandler]);
|
|
3738
|
+
* ```
|
|
3739
|
+
*/
|
|
3740
|
+
function acceptChannelConnections(serverHandler, channel, cases) {
|
|
3741
|
+
return serverHandler.forConnectionDomainCasesMulti(channel.toAcceptorDomains, cases);
|
|
3997
3742
|
}
|
|
3998
|
-
//#endregion
|
|
3999
|
-
//#region src/ActionRuntime/Transport/Carrier/duplex/ws/webSocketByteChannel.ts
|
|
4000
3743
|
/**
|
|
4001
|
-
*
|
|
4002
|
-
*
|
|
4003
|
-
*
|
|
4004
|
-
*
|
|
4005
|
-
*
|
|
3744
|
+
* Build the secure {@link AcceptorHandler} for a channel — the accept-in counterpart to
|
|
3745
|
+
* {@link connectChannel}. It folds in the same boilerplate as {@link createSecureAcceptorHandler} (the
|
|
3746
|
+
* `ClientCryptoKeyLink` + storage-backed TOFU resolver from one `storageAdapter`, the channel's codec +
|
|
3747
|
+
* dictionary version, the `security` block from the runtime coordinate) but takes the `(runtime, channel,
|
|
3748
|
+
* options)` shape of the channel family. Pair it with {@link acceptChannelConnections} for execution:
|
|
3749
|
+
* ```ts
|
|
3750
|
+
* const acceptor = acceptChannel(runtime, gameChannel, { clientEnv, storageAdapter, send });
|
|
3751
|
+
* runtime.addHandlers([acceptChannelConnections(acceptor, gameChannel, { … }), acceptor]);
|
|
3752
|
+
* ```
|
|
4006
3753
|
*/
|
|
4007
|
-
function
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
if (frame !== void 0) deliver(frame);
|
|
4019
|
-
});
|
|
4020
|
-
ws.addEventListener("close", (event) => {
|
|
4021
|
-
if (!intentional) console.error("WebSocket closed:", event);
|
|
4022
|
-
onClose?.();
|
|
4023
|
-
});
|
|
4024
|
-
ws.addEventListener("error", (event) => {
|
|
4025
|
-
console.error("WebSocket error:", event);
|
|
4026
|
-
onClose?.();
|
|
3754
|
+
function acceptChannel(runtime, channel, options) {
|
|
3755
|
+
return createSecureAcceptorHandler({
|
|
3756
|
+
channel,
|
|
3757
|
+
runtime,
|
|
3758
|
+
clientEnv: options.clientEnv,
|
|
3759
|
+
storageAdapter: options.storageAdapter,
|
|
3760
|
+
link: options.link,
|
|
3761
|
+
send: options.send,
|
|
3762
|
+
securityLevel: options.securityLevel,
|
|
3763
|
+
verifyKeyResolver: options.verifyKeyResolver,
|
|
3764
|
+
defaultTimeout: options.defaultTimeout
|
|
4027
3765
|
});
|
|
4028
|
-
return {
|
|
4029
|
-
ready: new Promise((resolve, reject) => {
|
|
4030
|
-
if (ws.readyState === WebSocket.OPEN) {
|
|
4031
|
-
resolve();
|
|
4032
|
-
return;
|
|
4033
|
-
}
|
|
4034
|
-
ws.addEventListener("open", () => resolve(), { once: true });
|
|
4035
|
-
ws.addEventListener("error", (event) => reject(event), { once: true });
|
|
4036
|
-
ws.addEventListener("close", (event) => reject(/* @__PURE__ */ new Error(`WebSocket closed before open: code=${event.code}`)), { once: true });
|
|
4037
|
-
}),
|
|
4038
|
-
isOpen: () => ws.readyState === WebSocket.OPEN,
|
|
4039
|
-
send: (frame) => sendFrame(ws, frame),
|
|
4040
|
-
attach: (handlers) => {
|
|
4041
|
-
onMessage = handlers.onMessage;
|
|
4042
|
-
onClose = handlers.onClose;
|
|
4043
|
-
for (const frame of preAttach) handlers.onMessage(frame);
|
|
4044
|
-
preAttach.length = 0;
|
|
4045
|
-
},
|
|
4046
|
-
close: () => {
|
|
4047
|
-
intentional = true;
|
|
4048
|
-
try {
|
|
4049
|
-
ws.close();
|
|
4050
|
-
} catch {}
|
|
4051
|
-
},
|
|
4052
|
-
get label() {
|
|
4053
|
-
return ws.url != null && ws.url !== "" ? ws.url : void 0;
|
|
4054
|
-
}
|
|
4055
|
-
};
|
|
4056
|
-
}
|
|
4057
|
-
/** Accept text + binary frames (ArrayBuffer / Uint8Array / Blob); Blobs are converted to a buffer. */
|
|
4058
|
-
async function normalizeFrame(data) {
|
|
4059
|
-
if (typeof data === "string" || data instanceof ArrayBuffer || data instanceof Uint8Array) return data;
|
|
4060
|
-
if (typeof Blob !== "undefined" && data instanceof Blob) return await data.arrayBuffer();
|
|
4061
3766
|
}
|
|
4062
3767
|
//#endregion
|
|
4063
|
-
//#region src/ActionRuntime/Transport/
|
|
3768
|
+
//#region src/ActionRuntime/Transport/codec/actionWireCodec.ts
|
|
4064
3769
|
/**
|
|
4065
|
-
*
|
|
4066
|
-
*
|
|
4067
|
-
* now "just another carrier" under the shared secure session, with no WS-specific transport class.
|
|
3770
|
+
* Tiny integer codes for the payload type, so the verbose `"request"`/`"result"`/`"progress"`
|
|
3771
|
+
* strings never hit the wire. The index in {@link ReversePayloadType} must line up with the value.
|
|
4068
3772
|
*/
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
};
|
|
4080
|
-
}
|
|
4081
|
-
function defaultWebSocket(url) {
|
|
4082
|
-
const ws = new WebSocket(url);
|
|
4083
|
-
ws.binaryType = "arraybuffer";
|
|
4084
|
-
return ws;
|
|
4085
|
-
}
|
|
4086
|
-
//#endregion
|
|
4087
|
-
//#region src/ActionRuntime/Transport/Carrier/exchange/http/httpAcceptorCarrier.ts
|
|
3773
|
+
const PayloadTypeToInt = {
|
|
3774
|
+
["request"]: 0,
|
|
3775
|
+
["result"]: 1,
|
|
3776
|
+
["progress"]: 2
|
|
3777
|
+
};
|
|
3778
|
+
const ReversePayloadType = [
|
|
3779
|
+
"request",
|
|
3780
|
+
"result",
|
|
3781
|
+
"progress"
|
|
3782
|
+
];
|
|
4088
3783
|
/**
|
|
4089
|
-
*
|
|
4090
|
-
*
|
|
4091
|
-
*
|
|
4092
|
-
* levels are all supplied centrally by `serveChannel`, so this only needs to say which requests carry an
|
|
4093
|
-
* action envelope and how to answer CORS.
|
|
3784
|
+
* Build the positional `domain:id` ↔ integer dictionary. Both ends of a channel MUST build it from
|
|
3785
|
+
* the same domains in the same order — the mapping is positional, so a mismatch routes to the wrong
|
|
3786
|
+
* action. Add new transported domains to the end of the list.
|
|
4094
3787
|
*/
|
|
4095
|
-
function
|
|
3788
|
+
function buildActionRouteDictionary(domains) {
|
|
3789
|
+
const routeToInt = /* @__PURE__ */ new Map();
|
|
3790
|
+
const intToRoute = [];
|
|
3791
|
+
for (const dom of domains) for (const actionId of Object.keys(dom.actionSchema)) {
|
|
3792
|
+
const routeKey = `${dom.domain}:${actionId}`;
|
|
3793
|
+
if (routeToInt.has(routeKey)) continue;
|
|
3794
|
+
routeToInt.set(routeKey, intToRoute.length);
|
|
3795
|
+
intToRoute.push({
|
|
3796
|
+
domain: dom.domain,
|
|
3797
|
+
id: actionId,
|
|
3798
|
+
allDomains: dom.allDomains
|
|
3799
|
+
});
|
|
3800
|
+
}
|
|
4096
3801
|
return {
|
|
4097
|
-
|
|
4098
|
-
|
|
4099
|
-
secure: options.secure,
|
|
4100
|
-
isActionPath: options.isActionPath,
|
|
4101
|
-
cors: options.cors,
|
|
4102
|
-
useErrorStatus: options.useErrorStatus
|
|
3802
|
+
routeToInt,
|
|
3803
|
+
intToRoute
|
|
4103
3804
|
};
|
|
4104
3805
|
}
|
|
4105
|
-
|
|
4106
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
} catch {
|
|
4111
|
-
return url;
|
|
4112
|
-
}
|
|
3806
|
+
/** Pull the type-specific payload (`input` / `result` / `progress`) out of a wire JSON object. */
|
|
3807
|
+
function extractWirePayload(json) {
|
|
3808
|
+
if (json.type === "request") return json.input;
|
|
3809
|
+
if (json.type === "result") return json.result;
|
|
3810
|
+
if (json.type === "progress") return json.progress;
|
|
4113
3811
|
}
|
|
4114
3812
|
/**
|
|
4115
|
-
*
|
|
4116
|
-
*
|
|
4117
|
-
*
|
|
4118
|
-
* the request/reply correlation provided for free by the HTTP transaction.
|
|
4119
|
-
*
|
|
4120
|
-
* `createRequest` derives the URL/headers per action (keep it simple with `() => ({ url })`). The body is
|
|
4121
|
-
* the session's responsibility, so it is never built here.
|
|
3813
|
+
* Reassemble a full wire JSON object from its decoded parts. `inputHash`/`outputHash` are emitted
|
|
3814
|
+
* empty — the hydration constructors recompute them — and the result still satisfies
|
|
3815
|
+
* `isActionPayload_Any_JsonObject` so it flows through validation like a JSON frame.
|
|
4122
3816
|
*/
|
|
4123
|
-
function
|
|
4124
|
-
const
|
|
3817
|
+
function assembleWireJson(routeMeta, payloadType, time, context, payloadData) {
|
|
3818
|
+
const base = {
|
|
3819
|
+
form: "data",
|
|
3820
|
+
domain: routeMeta.domain,
|
|
3821
|
+
id: routeMeta.id,
|
|
3822
|
+
allDomains: routeMeta.allDomains,
|
|
3823
|
+
time,
|
|
3824
|
+
context
|
|
3825
|
+
};
|
|
3826
|
+
if (payloadType === "request") return {
|
|
3827
|
+
...base,
|
|
3828
|
+
type: "request",
|
|
3829
|
+
input: payloadData,
|
|
3830
|
+
inputHash: ""
|
|
3831
|
+
};
|
|
3832
|
+
if (payloadType === "result") return {
|
|
3833
|
+
...base,
|
|
3834
|
+
type: "result",
|
|
3835
|
+
result: payloadData,
|
|
3836
|
+
outputHash: ""
|
|
3837
|
+
};
|
|
4125
3838
|
return {
|
|
4126
|
-
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
const request = createRequest(input);
|
|
4130
|
-
return {
|
|
4131
|
-
label: request.url,
|
|
4132
|
-
exchange: async (frame, opts) => {
|
|
4133
|
-
return await (await doFetch(request.url, {
|
|
4134
|
-
method: "POST",
|
|
4135
|
-
headers: {
|
|
4136
|
-
"Content-Type": "application/json",
|
|
4137
|
-
...request.headers
|
|
4138
|
-
},
|
|
4139
|
-
body: typeof frame === "string" ? frame : new Uint8Array(frame),
|
|
4140
|
-
signal: opts?.signal
|
|
4141
|
-
})).text();
|
|
4142
|
-
}
|
|
4143
|
-
};
|
|
4144
|
-
},
|
|
4145
|
-
getCacheKey: options.getTransportCacheKey ?? ((input) => [createRequest(input).url]),
|
|
4146
|
-
getRouteInfo: options.getRouteInfo ?? ((input) => {
|
|
4147
|
-
const { url } = createRequest(input);
|
|
4148
|
-
return {
|
|
4149
|
-
carrierLabel: "http",
|
|
4150
|
-
method: "POST",
|
|
4151
|
-
url,
|
|
4152
|
-
summary: `POST ${shortPath(url)}`
|
|
4153
|
-
};
|
|
4154
|
-
})
|
|
3839
|
+
...base,
|
|
3840
|
+
type: "progress",
|
|
3841
|
+
progress: payloadData
|
|
4155
3842
|
};
|
|
4156
3843
|
}
|
|
4157
3844
|
//#endregion
|
|
4158
|
-
//#region src/ActionRuntime/Transport/codec/
|
|
3845
|
+
//#region src/ActionRuntime/Transport/codec/createBinaryWireSessionFactory.ts
|
|
4159
3846
|
/**
|
|
4160
|
-
* Positional layout of the
|
|
4161
|
-
*
|
|
4162
|
-
*
|
|
4163
|
-
* routing).
|
|
4164
|
-
*
|
|
4165
|
-
* [ routeInt, typeInt, time, cuid, originClient, payloadData ]
|
|
3847
|
+
* Positional layout of the *session* binary envelope — the leanest frame. Compared to the stateless
|
|
3848
|
+
* adapter it replaces the 21-char `cuid` with a small per-connection integer and only carries
|
|
3849
|
+
* `originClient` on the very first request of each direction (the peer remembers it afterwards).
|
|
4166
3850
|
*
|
|
4167
|
-
*
|
|
4168
|
-
* `context.timeCreated` (reconstructed from `time`) and `context.routing` (rebuilt empty — the
|
|
4169
|
-
* receiver re-stamps its own route items as it handles the action). For the leanest possible frames
|
|
4170
|
-
* (integer correlation, identity dropped after a handshake), use `createBinaryWireSessionFactory`.
|
|
3851
|
+
* [ routeInt, typeInt, corrId, time, originClient?, payloadData ]
|
|
4171
3852
|
*/
|
|
4172
|
-
const ENVELOPE = {
|
|
3853
|
+
const ENVELOPE$1 = {
|
|
4173
3854
|
route: 0,
|
|
4174
3855
|
type: 1,
|
|
4175
|
-
|
|
4176
|
-
|
|
3856
|
+
corr: 2,
|
|
3857
|
+
time: 3,
|
|
4177
3858
|
originClient: 4,
|
|
4178
3859
|
payload: 5
|
|
4179
3860
|
};
|
|
4180
|
-
const ENVELOPE_LENGTH = 6;
|
|
3861
|
+
const ENVELOPE_LENGTH$1 = 6;
|
|
4181
3862
|
/**
|
|
4182
|
-
*
|
|
4183
|
-
*
|
|
4184
|
-
*
|
|
4185
|
-
*
|
|
4186
|
-
* (
|
|
3863
|
+
* How long a pending correlation entry is kept before it's swept. A correlation only matters until its
|
|
3864
|
+
* action resolves or times out, so anything older than the longest realistic action timeout can be
|
|
3865
|
+
* dropped — this bounds memory when requests time out or a connection dies mid-flight (their replies
|
|
3866
|
+
* would never arrive, leaving the entry orphaned). Generous default so live correlations are never
|
|
3867
|
+
* pruned (the default transport timeout is 10s).
|
|
3868
|
+
*/
|
|
3869
|
+
const DEFAULT_CORRELATION_TTL_MS = 5 * 6e4;
|
|
3870
|
+
function isKnownIdentity(coordinate) {
|
|
3871
|
+
return coordinate != null && coordinate.envId !== "_unset_";
|
|
3872
|
+
}
|
|
3873
|
+
/**
|
|
3874
|
+
* Drop entries older than `ttlMs`. Maps keep insertion order and entries are inserted in time order,
|
|
3875
|
+
* so the oldest are first — stop sweeping at the first live entry.
|
|
3876
|
+
*/
|
|
3877
|
+
function pruneExpired(map, now, ttlMs) {
|
|
3878
|
+
for (const [key, entry] of map) {
|
|
3879
|
+
if (now - entry.time <= ttlMs) break;
|
|
3880
|
+
map.delete(key);
|
|
3881
|
+
}
|
|
3882
|
+
}
|
|
3883
|
+
/**
|
|
3884
|
+
* Builds a factory of *stateful, per-connection* codecs for {@link LinkTransport} /
|
|
3885
|
+
* `AcceptorHandler` — the maximally compact binary wire. Call the returned factory once per live
|
|
3886
|
+
* connection (each socket on the client, each accepted connection on the server) so every channel
|
|
3887
|
+
* gets its own correlation + identity state.
|
|
4187
3888
|
*
|
|
4188
|
-
*
|
|
4189
|
-
*
|
|
4190
|
-
*
|
|
3889
|
+
* On top of everything {@link createBinaryWireAdapter} drops, a session also drops:
|
|
3890
|
+
* - **`cuid`** — replaced by a per-connection integer correlation id. The initiator maps it to its
|
|
3891
|
+
* real cuid; the responder echoes it; each side reconstructs the cuid from its own map. Correlation
|
|
3892
|
+
* only needs to be unique per socket, so a counter suffices.
|
|
3893
|
+
* - **`originClient` after the first request** — the first request each side sends carries its
|
|
3894
|
+
* identity; the peer remembers it and injects it into later frames. Replies omit it entirely (a
|
|
3895
|
+
* reply carries the initiator's own origin, which the initiator already knows).
|
|
4191
3896
|
*
|
|
4192
|
-
* Both ends
|
|
4193
|
-
*
|
|
3897
|
+
* Both ends MUST build the factory from the same domains in the same order (positional dictionary).
|
|
3898
|
+
* Text frames still return `undefined` from `incoming`, so JSON clients remain interoperable.
|
|
4194
3899
|
*
|
|
4195
|
-
*
|
|
4196
|
-
*
|
|
3900
|
+
* Hibernation note: after a server connection is evicted its session resets, so a still-connected
|
|
3901
|
+
* client (whose session persists) will keep omitting `originClient`. The server must therefore restore
|
|
3902
|
+
* the connection→client binding from its own store (see `AcceptorHandler.rehydrateConnection`) and
|
|
3903
|
+
* inject `originClient` from there — the session alone can't recover it.
|
|
4197
3904
|
*/
|
|
4198
|
-
function
|
|
3905
|
+
function createBinaryWireSessionFactory(domains, options) {
|
|
4199
3906
|
const { routeToInt, intToRoute } = buildActionRouteDictionary(domains);
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4221
|
-
|
|
4222
|
-
|
|
4223
|
-
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
|
|
4229
|
-
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
}
|
|
4233
|
-
|
|
4234
|
-
|
|
4235
|
-
|
|
3907
|
+
const unknownIdentity = RuntimeCoordinate.unknown.toJsonObject();
|
|
3908
|
+
const ttlMs = options?.correlationTtlMs ?? DEFAULT_CORRELATION_TTL_MS;
|
|
3909
|
+
return () => {
|
|
3910
|
+
let outCounter = 0;
|
|
3911
|
+
const corrToCuid = /* @__PURE__ */ new Map();
|
|
3912
|
+
const cuidToCorr = /* @__PURE__ */ new Map();
|
|
3913
|
+
let selfIdentity;
|
|
3914
|
+
let peerIdentity;
|
|
3915
|
+
return {
|
|
3916
|
+
outgoing: (input) => {
|
|
3917
|
+
const json = input.action.toJsonObject();
|
|
3918
|
+
const routeKey = `${json.domain}:${json.id}`;
|
|
3919
|
+
const routeInt = routeToInt.get(routeKey);
|
|
3920
|
+
if (routeInt == null) throw new Error(`[binary-wire] Cannot pack unregistered action route: ${routeKey}`);
|
|
3921
|
+
const now = Date.now();
|
|
3922
|
+
pruneExpired(corrToCuid, now, ttlMs);
|
|
3923
|
+
pruneExpired(cuidToCorr, now, ttlMs);
|
|
3924
|
+
let corr;
|
|
3925
|
+
let wireIdentity;
|
|
3926
|
+
if (json.type === "request") {
|
|
3927
|
+
corr = outCounter++;
|
|
3928
|
+
corrToCuid.set(corr, {
|
|
3929
|
+
value: json.context.cuid,
|
|
3930
|
+
time: now
|
|
3931
|
+
});
|
|
3932
|
+
if (selfIdentity == null && isKnownIdentity(json.context.originClient)) {
|
|
3933
|
+
selfIdentity = json.context.originClient;
|
|
3934
|
+
wireIdentity = json.context.originClient;
|
|
3935
|
+
}
|
|
3936
|
+
} else {
|
|
3937
|
+
corr = cuidToCorr.get(json.context.cuid)?.value ?? -1;
|
|
3938
|
+
if (json.type === "result") cuidToCorr.delete(json.context.cuid);
|
|
3939
|
+
}
|
|
3940
|
+
const envelope = new Array(ENVELOPE_LENGTH$1);
|
|
3941
|
+
envelope[ENVELOPE$1.route] = routeInt;
|
|
3942
|
+
envelope[ENVELOPE$1.type] = PayloadTypeToInt[json.type];
|
|
3943
|
+
envelope[ENVELOPE$1.corr] = corr;
|
|
3944
|
+
envelope[ENVELOPE$1.time] = json.time;
|
|
3945
|
+
envelope[ENVELOPE$1.originClient] = wireIdentity;
|
|
3946
|
+
envelope[ENVELOPE$1.payload] = extractWirePayload(json);
|
|
3947
|
+
return (0, msgpackr.pack)(envelope);
|
|
3948
|
+
},
|
|
3949
|
+
incoming: (frame) => {
|
|
3950
|
+
let buffer;
|
|
3951
|
+
if (frame instanceof ArrayBuffer) buffer = new Uint8Array(frame);
|
|
3952
|
+
else if (frame instanceof Uint8Array) buffer = frame;
|
|
3953
|
+
else return;
|
|
3954
|
+
try {
|
|
3955
|
+
const envelope = (0, msgpackr.unpack)(buffer);
|
|
3956
|
+
if (!Array.isArray(envelope) || envelope.length !== ENVELOPE_LENGTH$1) return void 0;
|
|
3957
|
+
const routeMeta = intToRoute[envelope[ENVELOPE$1.route]];
|
|
3958
|
+
const payloadType = ReversePayloadType[envelope[ENVELOPE$1.type]];
|
|
3959
|
+
if (routeMeta == null || payloadType == null) return void 0;
|
|
3960
|
+
const now = Date.now();
|
|
3961
|
+
pruneExpired(corrToCuid, now, ttlMs);
|
|
3962
|
+
pruneExpired(cuidToCorr, now, ttlMs);
|
|
3963
|
+
const corr = envelope[ENVELOPE$1.corr];
|
|
3964
|
+
const time = envelope[ENVELOPE$1.time];
|
|
3965
|
+
const wireIdentity = envelope[ENVELOPE$1.originClient];
|
|
3966
|
+
let cuid;
|
|
3967
|
+
let originClient;
|
|
3968
|
+
if (payloadType === "request") {
|
|
3969
|
+
cuid = (0, nanoid.nanoid)();
|
|
3970
|
+
cuidToCorr.set(cuid, {
|
|
3971
|
+
value: corr,
|
|
3972
|
+
time: now
|
|
3973
|
+
});
|
|
3974
|
+
if (isKnownIdentity(wireIdentity)) peerIdentity = wireIdentity;
|
|
3975
|
+
originClient = peerIdentity ?? unknownIdentity;
|
|
3976
|
+
} else {
|
|
3977
|
+
cuid = corrToCuid.get(corr)?.value ?? (0, nanoid.nanoid)();
|
|
3978
|
+
if (payloadType === "result") corrToCuid.delete(corr);
|
|
3979
|
+
originClient = selfIdentity ?? unknownIdentity;
|
|
3980
|
+
}
|
|
3981
|
+
return assembleWireJson(routeMeta, payloadType, time, {
|
|
3982
|
+
cuid,
|
|
3983
|
+
timeCreated: time,
|
|
3984
|
+
routing: [],
|
|
3985
|
+
originClient
|
|
3986
|
+
}, envelope[ENVELOPE$1.payload]);
|
|
3987
|
+
} catch (e) {
|
|
3988
|
+
console.error("[binary-wire] Failed to unpack binary action session frame", e);
|
|
3989
|
+
return;
|
|
3990
|
+
}
|
|
4236
3991
|
}
|
|
4237
|
-
}
|
|
3992
|
+
};
|
|
4238
3993
|
};
|
|
4239
3994
|
}
|
|
4240
3995
|
//#endregion
|
|
4241
|
-
//#region src/ActionRuntime/
|
|
3996
|
+
//#region src/ActionRuntime/Channel/secureChannel.ts
|
|
4242
3997
|
/**
|
|
4243
|
-
*
|
|
4244
|
-
*
|
|
4245
|
-
*
|
|
4246
|
-
* definition can be shared across multiple handlers — each handler builds its own live
|
|
4247
|
-
* {@link TransportConnection} via {@link TransportConnection._createConnection}.
|
|
3998
|
+
* Derive a stable wire-dictionary version from the ordered route list (FNV-1a over `domain:id,…`), so
|
|
3999
|
+
* the version moves automatically whenever the transported domains change — a stale peer is then
|
|
4000
|
+
* rejected by the handshake instead of silently misrouting a positionally-packed frame.
|
|
4248
4001
|
*/
|
|
4249
|
-
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
}
|
|
4258
|
-
/** Secure path: run the handshake (two exchanges) once at bring-up, then reuse the token + crypto. */
|
|
4259
|
-
async function finalizeSecureExchangeMethods(ctx) {
|
|
4260
|
-
return buildExchangeMethods(ctx, await runConnectorExchangeHandshake(ctx.carrier, ctx.secure));
|
|
4002
|
+
function deriveDictionaryVersion(domains) {
|
|
4003
|
+
const { intToRoute } = buildActionRouteDictionary(domains);
|
|
4004
|
+
const signature = intToRoute.map((route) => `${route.domain}:${route.id}`).join(",");
|
|
4005
|
+
let hash = 2166136261;
|
|
4006
|
+
for (let i = 0; i < signature.length; i++) {
|
|
4007
|
+
hash ^= signature.charCodeAt(i);
|
|
4008
|
+
hash = Math.imul(hash, 16777619);
|
|
4009
|
+
}
|
|
4010
|
+
return `auto:${(hash >>> 0).toString(16).padStart(8, "0")}`;
|
|
4261
4011
|
}
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
|
|
4265
|
-
|
|
4012
|
+
/**
|
|
4013
|
+
* Bundle a secure channel's shared identity from its transported domains. Both ends MUST call this
|
|
4014
|
+
* with the same domains in the same order (the binary wire dictionary is positional). The
|
|
4015
|
+
* `dictionaryVersion` is derived from those domains unless you pin an explicit one.
|
|
4016
|
+
*
|
|
4017
|
+
* Declare the domains *by role* — `toAcceptor` (connector→acceptor requests) and `toConnector`
|
|
4018
|
+
* (acceptor→connector pushes) — so the routing for both ends is derived from the channel (see
|
|
4019
|
+
* {@link connectChannel} and `acceptChannelConnections`) instead of being restated at each end. The
|
|
4020
|
+
* wire dictionary spans `[...toAcceptor, ...toConnector]` in that order; add new domains to the end of
|
|
4021
|
+
* their list to keep older peers compatible. (`domains` is still accepted as a legacy alias for
|
|
4022
|
+
* `toAcceptor`.)
|
|
4023
|
+
*/
|
|
4024
|
+
function defineSecureChannel(options) {
|
|
4025
|
+
const base = defineChannel({
|
|
4026
|
+
toAcceptor: options.toAcceptor,
|
|
4027
|
+
toConnector: options.toConnector
|
|
4028
|
+
});
|
|
4029
|
+
const allDomains = [...base.toAcceptorDomains, ...base.toConnectorDomains];
|
|
4266
4030
|
return {
|
|
4267
|
-
|
|
4268
|
-
|
|
4031
|
+
...base,
|
|
4032
|
+
dictionaryVersion: options.dictionaryVersion ?? deriveDictionaryVersion(allDomains),
|
|
4033
|
+
createCodec: createBinaryWireSessionFactory(allDomains, options.sessionOptions)
|
|
4269
4034
|
};
|
|
4270
4035
|
}
|
|
4271
|
-
|
|
4272
|
-
|
|
4273
|
-
|
|
4274
|
-
|
|
4275
|
-
|
|
4276
|
-
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
|
|
4280
|
-
|
|
4281
|
-
|
|
4282
|
-
|
|
4283
|
-
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
|
|
4291
|
-
|
|
4292
|
-
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
|
|
4298
|
-
|
|
4299
|
-
} catch (err) {
|
|
4300
|
-
if (timedOut) throw err_nice_transport.fromId("timeout", { timeout });
|
|
4301
|
-
throw err;
|
|
4302
|
-
} finally {
|
|
4303
|
-
clearTimeout(timeoutId);
|
|
4304
|
-
unsubscribe();
|
|
4036
|
+
//#endregion
|
|
4037
|
+
//#region src/ActionRuntime/Transport/SecureSession/exchangeAcceptor.ts
|
|
4038
|
+
const textEncoder = new TextEncoder();
|
|
4039
|
+
const textDecoder = new TextDecoder();
|
|
4040
|
+
/**
|
|
4041
|
+
* Acceptor (accept-in) side of the secure exchange protocol — the HTTP counterpart to
|
|
4042
|
+
* {@link AcceptorSecureSession}. Each POST body is one {@link decodeExchangeRequest} envelope; the
|
|
4043
|
+
* acceptor drives the server handshake over the two `hs` POSTs (correlated by `hsid`, since stateless
|
|
4044
|
+
* requests can't rely on channel ordering), mints a session **token** on accept, and on every later `act`
|
|
4045
|
+
* POST resolves the session by token, decrypts the body (at `encrypted`), routes it through the runtime,
|
|
4046
|
+
* and returns the (encrypted) result inline as the reply.
|
|
4047
|
+
*
|
|
4048
|
+
* Sessions and in-flight handshakes are held in memory — fine for a single-instance server. (Surviving a
|
|
4049
|
+
* Durable-Object eviction would persist each token's `keyMaterial` and re-derive the key on a miss, the
|
|
4050
|
+
* same primitive `AcceptorSecureSession.rehydrate` uses; left as a follow-up.)
|
|
4051
|
+
*/
|
|
4052
|
+
var ExchangeAcceptor = class {
|
|
4053
|
+
_security;
|
|
4054
|
+
_runtime;
|
|
4055
|
+
_allowedLevels;
|
|
4056
|
+
_noneAllowed;
|
|
4057
|
+
_pendingHandshakes = /* @__PURE__ */ new Map();
|
|
4058
|
+
_sessions = /* @__PURE__ */ new Map();
|
|
4059
|
+
constructor(config) {
|
|
4060
|
+
this._security = config.security;
|
|
4061
|
+
this._runtime = config.runtime;
|
|
4062
|
+
this._allowedLevels = Array.isArray(config.security.securityLevel) ? config.security.securityLevel : [config.security.securityLevel];
|
|
4063
|
+
this._noneAllowed = this._allowedLevels.includes("none");
|
|
4305
4064
|
}
|
|
4306
|
-
|
|
4307
|
-
async
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4065
|
+
/** Process one POST body (an exchange envelope), returning the reply body to send back. */
|
|
4066
|
+
async handlePost(body) {
|
|
4067
|
+
const request = decodeExchangeRequest(body);
|
|
4068
|
+
if (request == null) return this._err("malformed exchange request");
|
|
4069
|
+
if (request.k === "hs") return encodeExchange(await this._handleHandshake(request));
|
|
4070
|
+
return encodeExchange(await this._handleAction(request));
|
|
4071
|
+
}
|
|
4072
|
+
async _handleHandshake(request) {
|
|
4073
|
+
const message = decodeHandshakeMessage(request.m);
|
|
4074
|
+
if (message == null) return {
|
|
4075
|
+
k: "err",
|
|
4076
|
+
message: "malformed handshake message"
|
|
4077
|
+
};
|
|
4078
|
+
const security = this._security;
|
|
4079
|
+
await security.link.initialize();
|
|
4080
|
+
let handshake = this._pendingHandshakes.get(request.hsid);
|
|
4081
|
+
if (handshake == null) {
|
|
4082
|
+
handshake = createServerHandshake({
|
|
4083
|
+
link: security.link,
|
|
4084
|
+
localCoordinate: security.localCoordinate,
|
|
4085
|
+
dictionaryVersion: security.dictionaryVersion,
|
|
4086
|
+
securityLevel: security.securityLevel,
|
|
4087
|
+
verifyKeyResolver: security.verifyKeyResolver
|
|
4088
|
+
});
|
|
4089
|
+
this._pendingHandshakes.set(request.hsid, handshake);
|
|
4090
|
+
}
|
|
4091
|
+
if (message.t === "hello") return {
|
|
4092
|
+
k: "hs",
|
|
4093
|
+
m: encodeHandshakeMessage(await handshake.onHello(message))
|
|
4094
|
+
};
|
|
4095
|
+
if (message.t === "prove") {
|
|
4096
|
+
const reply = await handshake.onProve(message);
|
|
4097
|
+
this._pendingHandshakes.delete(request.hsid);
|
|
4098
|
+
const result = handshake.getResult();
|
|
4099
|
+
if (reply.t === "accept" && result != null) {
|
|
4100
|
+
const token = (0, nanoid.nanoid)();
|
|
4101
|
+
this._sessions.set(token, {
|
|
4102
|
+
client: new RuntimeCoordinate(result.remote),
|
|
4103
|
+
securityLevel: result.securityLevel,
|
|
4104
|
+
crypto: result.securityLevel === "encrypted" ? createActionFrameCrypto({
|
|
4105
|
+
link: security.link,
|
|
4106
|
+
linkedClientId: result.linkedClientId
|
|
4107
|
+
}) : void 0
|
|
4108
|
+
});
|
|
4109
|
+
return {
|
|
4110
|
+
k: "hs",
|
|
4111
|
+
m: encodeHandshakeMessage(reply),
|
|
4112
|
+
t: token
|
|
4113
|
+
};
|
|
4114
|
+
}
|
|
4115
|
+
return {
|
|
4116
|
+
k: "hs",
|
|
4117
|
+
m: encodeHandshakeMessage(reply)
|
|
4118
|
+
};
|
|
4119
|
+
}
|
|
4311
4120
|
return {
|
|
4121
|
+
k: "err",
|
|
4122
|
+
message: `unexpected handshake message ${message.t}`
|
|
4123
|
+
};
|
|
4124
|
+
}
|
|
4125
|
+
async _handleAction(request) {
|
|
4126
|
+
let session;
|
|
4127
|
+
let candidate;
|
|
4128
|
+
if (request.t != null) {
|
|
4129
|
+
session = this._sessions.get(request.t);
|
|
4130
|
+
if (session == null) return {
|
|
4131
|
+
k: "err",
|
|
4132
|
+
message: "unknown or expired session token"
|
|
4133
|
+
};
|
|
4134
|
+
if ("c" in request) {
|
|
4135
|
+
if (session.crypto == null) return {
|
|
4136
|
+
k: "err",
|
|
4137
|
+
message: "session is not encrypted"
|
|
4138
|
+
};
|
|
4139
|
+
const plain = await session.crypto.decryptFrame(base64ToBytes(request.c));
|
|
4140
|
+
candidate = JSON.parse(textDecoder.decode(plain));
|
|
4141
|
+
} else candidate = request.w;
|
|
4142
|
+
} else {
|
|
4143
|
+
if (!this._noneAllowed || "c" in request) return {
|
|
4144
|
+
k: "err",
|
|
4145
|
+
message: "missing session token"
|
|
4146
|
+
};
|
|
4147
|
+
candidate = request.w;
|
|
4148
|
+
}
|
|
4149
|
+
if (!isActionPayload_Any_JsonObject(candidate)) return {
|
|
4150
|
+
k: "err",
|
|
4151
|
+
message: "malformed action wire"
|
|
4152
|
+
};
|
|
4153
|
+
const wire = candidate;
|
|
4154
|
+
if (session != null && wire.type === "request") wire.context.originClient = session.client.toJsonObject();
|
|
4155
|
+
const resultWire = (await (await this._runtime.handleActionPayloadWire(wire)).waitForResultPayload()).toJsonObject();
|
|
4156
|
+
if (session?.crypto != null) return {
|
|
4312
4157
|
k: "act",
|
|
4313
|
-
|
|
4314
|
-
|
|
4158
|
+
c: bytesToBase64(await session.crypto.encryptFrame(textEncoder.encode(JSON.stringify(resultWire))))
|
|
4159
|
+
};
|
|
4160
|
+
return {
|
|
4161
|
+
k: "act",
|
|
4162
|
+
w: resultWire
|
|
4315
4163
|
};
|
|
4316
4164
|
}
|
|
4317
|
-
|
|
4318
|
-
|
|
4319
|
-
|
|
4320
|
-
|
|
4321
|
-
|
|
4322
|
-
}
|
|
4323
|
-
async function extractReplyWire(state, reply) {
|
|
4324
|
-
if (reply.k !== "act") return void 0;
|
|
4325
|
-
if ("c" in reply) {
|
|
4326
|
-
if (state.crypto == null) return void 0;
|
|
4327
|
-
const plain = await state.crypto.decryptFrame(base64ToBytes(reply.c));
|
|
4328
|
-
return JSON.parse(textDecoder.decode(plain));
|
|
4165
|
+
_err(message) {
|
|
4166
|
+
return encodeExchange({
|
|
4167
|
+
k: "err",
|
|
4168
|
+
message
|
|
4169
|
+
});
|
|
4329
4170
|
}
|
|
4330
|
-
|
|
4331
|
-
}
|
|
4332
|
-
async function runConnectorExchangeHandshake(carrier, secure) {
|
|
4333
|
-
await secure.link.initialize();
|
|
4334
|
-
const handshake = createClientHandshake({
|
|
4335
|
-
link: secure.link,
|
|
4336
|
-
localCoordinate: secure.localCoordinate,
|
|
4337
|
-
dictionaryVersion: secure.dictionaryVersion,
|
|
4338
|
-
securityLevel: secure.securityLevel
|
|
4339
|
-
});
|
|
4340
|
-
const hsid = (0, nanoid.nanoid)();
|
|
4341
|
-
const hello = await handshake.createHello();
|
|
4342
|
-
const welcomeReply = decodeExchangeReply(asString(await carrier.exchange(encodeExchange({
|
|
4343
|
-
k: "hs",
|
|
4344
|
-
hsid,
|
|
4345
|
-
m: encodeHandshakeMessage(hello)
|
|
4346
|
-
}))));
|
|
4347
|
-
if (welcomeReply?.k !== "hs") throw new Error("[exchange-handshake] expected a welcome reply");
|
|
4348
|
-
const welcome = decodeHandshakeMessage(welcomeReply.m);
|
|
4349
|
-
if (welcome == null) throw new Error("[exchange-handshake] malformed welcome");
|
|
4350
|
-
if (welcome.t === "reject") throw new Error(`[exchange-handshake] rejected by peer: ${welcome.reason}`);
|
|
4351
|
-
if (welcome.t !== "welcome") throw new Error(`[exchange-handshake] expected welcome, got ${welcome.t}`);
|
|
4352
|
-
const prove = await handshake.onWelcome(welcome);
|
|
4353
|
-
const acceptReply = decodeExchangeReply(asString(await carrier.exchange(encodeExchange({
|
|
4354
|
-
k: "hs",
|
|
4355
|
-
hsid,
|
|
4356
|
-
m: encodeHandshakeMessage(prove)
|
|
4357
|
-
}))));
|
|
4358
|
-
if (acceptReply?.k !== "hs") throw new Error("[exchange-handshake] expected an accept reply");
|
|
4359
|
-
const accept = decodeHandshakeMessage(acceptReply.m);
|
|
4360
|
-
if (accept == null) throw new Error("[exchange-handshake] malformed accept");
|
|
4361
|
-
if (accept.t === "reject") throw new Error(`[exchange-handshake] rejected by peer: ${accept.reason}`);
|
|
4362
|
-
if (accept.t !== "accept") throw new Error(`[exchange-handshake] expected accept, got ${accept.t}`);
|
|
4363
|
-
if (acceptReply.t == null) throw new Error("[exchange-handshake] accept missing session token");
|
|
4364
|
-
const result = await handshake.onAccept(accept);
|
|
4365
|
-
const crypto = result.securityLevel === "encrypted" ? createActionFrameCrypto({
|
|
4366
|
-
link: secure.link,
|
|
4367
|
-
linkedClientId: result.linkedClientId
|
|
4368
|
-
}) : void 0;
|
|
4369
|
-
return {
|
|
4370
|
-
token: acceptReply.t,
|
|
4371
|
-
crypto
|
|
4372
|
-
};
|
|
4373
|
-
}
|
|
4374
|
-
function asString(frame) {
|
|
4375
|
-
if (typeof frame === "string") return frame;
|
|
4376
|
-
return textDecoder.decode(frame instanceof ArrayBuffer ? new Uint8Array(frame) : frame);
|
|
4377
|
-
}
|
|
4171
|
+
};
|
|
4378
4172
|
//#endregion
|
|
4379
|
-
//#region src/ActionRuntime/
|
|
4380
|
-
|
|
4381
|
-
|
|
4382
|
-
|
|
4383
|
-
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4173
|
+
//#region src/ActionRuntime/Handler/PeerLink/Acceptor/createActionFetchHandler.ts
|
|
4174
|
+
/** Permissive defaults — fine for a public action endpoint; override (or disable) via `cors`. */
|
|
4175
|
+
const DEFAULT_CORS_HEADERS = {
|
|
4176
|
+
"Access-Control-Allow-Origin": "*",
|
|
4177
|
+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
|
4178
|
+
"Access-Control-Allow-Headers": "Content-Type",
|
|
4179
|
+
"Access-Control-Max-Age": "86400"
|
|
4180
|
+
};
|
|
4181
|
+
/**
|
|
4182
|
+
* Build the `fetch` handler a server/Durable-Object exposes for action traffic, folding in the
|
|
4183
|
+
* boilerplate every endpoint repeats: CORS (incl. the `OPTIONS` preflight), routing the `/action`
|
|
4184
|
+
* `POST` body through the runtime (`handleActionPayloadWire` → `waitForResultPayload` →
|
|
4185
|
+
* `toHttpResponse`), an optional WebSocket-upgrade hook, and a `404` fallback.
|
|
4186
|
+
*
|
|
4187
|
+
* It only touches web-standard `Request`/`Response`, so it stays transport-agnostic — the one
|
|
4188
|
+
* environment-specific bit (the WS upgrade) is injected via {@link IActionFetchHandlerOptions.onWebSocketUpgrade}:
|
|
4189
|
+
* ```ts
|
|
4190
|
+
* this.fetchHandler = createActionFetchHandler(this.runtime, {
|
|
4191
|
+
* onWebSocketUpgrade: () => {
|
|
4192
|
+
* const pair = new WebSocketPair();
|
|
4193
|
+
* this.ctx.acceptWebSocket(pair[1]);
|
|
4194
|
+
* return new Response(null, { status: 101, webSocket: pair[0] });
|
|
4195
|
+
* },
|
|
4196
|
+
* });
|
|
4197
|
+
* // async fetch(request) { return this.fetchHandler(request); }
|
|
4198
|
+
* ```
|
|
4199
|
+
*/
|
|
4200
|
+
function createActionFetchHandler(runtime, options = {}) {
|
|
4201
|
+
const corsHeaders = options.cors === false ? {} : options.cors ?? DEFAULT_CORS_HEADERS;
|
|
4202
|
+
const isActionPath = options.isActionPath ?? ((url) => url.pathname.endsWith("/action"));
|
|
4203
|
+
const isWebSocketPath = options.isWebSocketPath ?? ((url) => url.pathname.endsWith("/ws"));
|
|
4204
|
+
const exchangeAcceptor = options.security != null ? new ExchangeAcceptor({
|
|
4205
|
+
runtime,
|
|
4206
|
+
security: options.security
|
|
4207
|
+
}) : void 0;
|
|
4208
|
+
const withCors = (response) => {
|
|
4209
|
+
if (options.cors === false) return response;
|
|
4210
|
+
const headers = new Headers(response.headers);
|
|
4211
|
+
for (const [key, value] of Object.entries(corsHeaders)) headers.set(key, value);
|
|
4212
|
+
return new Response(response.body, {
|
|
4213
|
+
status: response.status,
|
|
4214
|
+
headers
|
|
4215
|
+
});
|
|
4389
4216
|
};
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4217
|
+
return async (request) => {
|
|
4218
|
+
if (request.method === "OPTIONS") return withCors(new Response(null, { status: 204 }));
|
|
4219
|
+
const url = new URL(request.url);
|
|
4220
|
+
const isWebSocketUpgrade = options.isWebSocketUpgrade ?? ((req, u) => req.headers.get("Upgrade") === "websocket" && isWebSocketPath(u));
|
|
4221
|
+
if (options.onWebSocketUpgrade != null && isWebSocketUpgrade(request, url)) return options.onWebSocketUpgrade(request, url);
|
|
4222
|
+
if (request.method === "POST" && isActionPath(url)) {
|
|
4223
|
+
if (exchangeAcceptor != null) {
|
|
4224
|
+
const reply = await exchangeAcceptor.handlePost(await request.text());
|
|
4225
|
+
return withCors(new Response(reply, {
|
|
4226
|
+
status: 200,
|
|
4227
|
+
headers: { "Content-Type": "application/json" }
|
|
4228
|
+
}));
|
|
4229
|
+
}
|
|
4230
|
+
return withCors((await (await runtime.handleActionPayloadWire(await request.json())).waitForResultPayload()).toHttpResponse({ useErrorStatus: options.useErrorStatus }));
|
|
4231
|
+
}
|
|
4232
|
+
return withCors(new Response("Not found", { status: 404 }));
|
|
4394
4233
|
};
|
|
4395
|
-
if (transportStatus.status === "unsupported") return { status: "unsupported" };
|
|
4396
|
-
return { status: "uninitialized" };
|
|
4397
4234
|
}
|
|
4398
4235
|
//#endregion
|
|
4399
|
-
//#region src/ActionRuntime/
|
|
4400
|
-
let transportOrd = 0;
|
|
4236
|
+
//#region src/ActionRuntime/Handler/PeerLink/Acceptor/Hibernation/ConnectionStateStore.ts
|
|
4401
4237
|
/**
|
|
4402
|
-
*
|
|
4403
|
-
*
|
|
4404
|
-
*
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4425
|
-
|
|
4426
|
-
/**
|
|
4427
|
-
* Whether a `ready`-status transport still needs asynchronous bring-up before its methods exist —
|
|
4428
|
-
* awaiting the carrier to open and/or running a handshake. Default `false`: a stateless transport
|
|
4429
|
-
* (HTTP) is usable the instant `getTransport` reports `ready`, so it stays a terminal *synchronous*
|
|
4430
|
-
* fallback in {@link ConnectionTransportManager}. Stream carriers (Link/WS) override to `true`.
|
|
4431
|
-
*/
|
|
4432
|
-
_needsAsyncBringUp(_readyData) {
|
|
4433
|
-
return false;
|
|
4238
|
+
* A typed per-connection state store that co-owns the app state and the acceptor handler's routing
|
|
4239
|
+
* binding in one attachment, so neither the consumer nor the handler has to hand-merge the two. Create
|
|
4240
|
+
* it through {@link createConnectionStateStore} (which also wires binding persistence and replays
|
|
4241
|
+
* surviving connections after a wake), then `get`/`set`/`clearApp` the app state directly.
|
|
4242
|
+
*
|
|
4243
|
+
* The mechanism is carrier-neutral — it only needs read/write/enumerate callbacks for the connection's
|
|
4244
|
+
* attachment — but it pays off on transports whose connections outlive process eviction (e.g. a
|
|
4245
|
+
* Durable Object's hibernatable WebSockets), which is why it lives beside the hibernation adapter.
|
|
4246
|
+
*
|
|
4247
|
+
* ```ts
|
|
4248
|
+
* const players = createConnectionStateStore(serverHandler, {
|
|
4249
|
+
* schema: vs_player,
|
|
4250
|
+
* read: (ws) => ws.deserializeAttachment(),
|
|
4251
|
+
* write: (ws, v) => ws.serializeAttachment(v),
|
|
4252
|
+
* getConnections: () => ctx.getWebSockets(),
|
|
4253
|
+
* });
|
|
4254
|
+
* players.set(ws, player); // binding is preserved automatically
|
|
4255
|
+
* const player = players.get(ws);
|
|
4256
|
+
* ```
|
|
4257
|
+
*/
|
|
4258
|
+
var ConnectionStateStore = class {
|
|
4259
|
+
options;
|
|
4260
|
+
constructor(options) {
|
|
4261
|
+
this.options = options;
|
|
4434
4262
|
}
|
|
4435
|
-
/**
|
|
4436
|
-
|
|
4437
|
-
return
|
|
4263
|
+
/** The validated app state for a connection, or `null` if unset / invalid. */
|
|
4264
|
+
get(connection) {
|
|
4265
|
+
return this._readAttachment(connection).app ?? null;
|
|
4438
4266
|
}
|
|
4439
|
-
/**
|
|
4440
|
-
|
|
4441
|
-
|
|
4442
|
-
|
|
4443
|
-
|
|
4444
|
-
|
|
4267
|
+
/** Set the app state, preserving the runtime binding already pinned to the connection. */
|
|
4268
|
+
set(connection, app) {
|
|
4269
|
+
const existing = this._readAttachment(connection);
|
|
4270
|
+
this.options.write(connection, {
|
|
4271
|
+
app,
|
|
4272
|
+
binding: existing.binding
|
|
4273
|
+
});
|
|
4445
4274
|
}
|
|
4446
|
-
|
|
4447
|
-
|
|
4448
|
-
|
|
4449
|
-
|
|
4275
|
+
/** Clear the app state but keep the binding (e.g. a spectator that stopped watching). */
|
|
4276
|
+
clearApp(connection) {
|
|
4277
|
+
const existing = this._readAttachment(connection);
|
|
4278
|
+
this.options.write(connection, { binding: existing.binding });
|
|
4450
4279
|
}
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
|
|
4454
|
-
return `${this.transOrd}:${inner}`;
|
|
4280
|
+
/** Every live connection paired with its (validated) app state — for rebuilding in-memory state after a wake. */
|
|
4281
|
+
entries() {
|
|
4282
|
+
return this.options.getConnections().map((connection) => [connection, this._readAttachment(connection).app ?? null]);
|
|
4455
4283
|
}
|
|
4456
|
-
/**
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4462
|
-
|
|
4463
|
-
return this.initialized.isAvailable?.(input) ?? true;
|
|
4284
|
+
/** @internal Persist a freshly-bound connection's binding, preserving any app state already stored. */
|
|
4285
|
+
_persistBinding(connection, binding) {
|
|
4286
|
+
const existing = this._readAttachment(connection);
|
|
4287
|
+
this.options.write(connection, {
|
|
4288
|
+
app: existing.app,
|
|
4289
|
+
binding
|
|
4290
|
+
});
|
|
4464
4291
|
}
|
|
4465
|
-
|
|
4466
|
-
|
|
4292
|
+
/** @internal The persisted binding for a connection, if any (used to replay routing after a wake). */
|
|
4293
|
+
_readBinding(connection) {
|
|
4294
|
+
return this._readAttachment(connection).binding;
|
|
4467
4295
|
}
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
if (
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
}
|
|
4481
|
-
|
|
4482
|
-
const initializationPromise = statusInfo.initializationPromise.then((result) => result.status === "ready" ? this._bringUp(result.readyData) : result);
|
|
4483
|
-
return {
|
|
4484
|
-
status: "initializing",
|
|
4485
|
-
timeStarted: statusInfo.timeStarted,
|
|
4486
|
-
initializationPromise
|
|
4487
|
-
};
|
|
4296
|
+
_readAttachment(connection) {
|
|
4297
|
+
try {
|
|
4298
|
+
const raw = this.options.read(connection);
|
|
4299
|
+
if (typeof raw !== "object" || raw === null) return {};
|
|
4300
|
+
const attachment = raw;
|
|
4301
|
+
const result = {};
|
|
4302
|
+
if (attachment.binding != null) result.binding = attachment.binding;
|
|
4303
|
+
if (attachment.app !== void 0) {
|
|
4304
|
+
const app = this._validateApp(attachment.app);
|
|
4305
|
+
if (app !== void 0) result.app = app;
|
|
4306
|
+
}
|
|
4307
|
+
return result;
|
|
4308
|
+
} catch {
|
|
4309
|
+
return {};
|
|
4488
4310
|
}
|
|
4489
|
-
return statusInfo;
|
|
4490
4311
|
}
|
|
4491
|
-
|
|
4492
|
-
|
|
4493
|
-
|
|
4494
|
-
|
|
4495
|
-
|
|
4496
|
-
|
|
4497
|
-
|
|
4312
|
+
_validateApp(value) {
|
|
4313
|
+
const schema = this.options.schema;
|
|
4314
|
+
if (schema == null) return value;
|
|
4315
|
+
const result = schema["~standard"].validate(value);
|
|
4316
|
+
if (result instanceof Promise) return void 0;
|
|
4317
|
+
if (result.issues != null) return void 0;
|
|
4318
|
+
return result.value;
|
|
4498
4319
|
}
|
|
4499
4320
|
};
|
|
4500
|
-
//#endregion
|
|
4501
|
-
//#region src/ActionRuntime/Transport/Exchange/ExchangeConnection.ts
|
|
4502
4321
|
/**
|
|
4503
|
-
*
|
|
4504
|
-
*
|
|
4505
|
-
*
|
|
4322
|
+
* Build a per-connection {@link ConnectionStateStore} bound to an {@link AcceptorHandler}: it registers
|
|
4323
|
+
* itself as the handler's connection-bound persistence callback (so bindings are written without
|
|
4324
|
+
* overwriting app state) and immediately replays every live connection's stored binding via
|
|
4325
|
+
* {@link AcceptorHandler.rehydrateConnection} — so on a transport that resumes after eviction (e.g. a
|
|
4326
|
+
* Durable Object waking from hibernation) both the app identity and the action routing come back from a
|
|
4327
|
+
* single attachment, with no storage reads and no hand-rolled merge.
|
|
4328
|
+
*
|
|
4329
|
+
* Lives outside the handler so the generic {@link AcceptorHandler} stays free of any attachment/
|
|
4330
|
+
* hibernation concern — it exposes only the neutral `setOnConnectionBound` + `rehydrateConnection`
|
|
4331
|
+
* hooks this builder drives.
|
|
4506
4332
|
*/
|
|
4507
|
-
|
|
4508
|
-
|
|
4509
|
-
|
|
4510
|
-
|
|
4511
|
-
|
|
4512
|
-
|
|
4513
|
-
}
|
|
4514
|
-
_getCacheKey(input) {
|
|
4515
|
-
return this.initialized.getTransportCacheKey?.(input).join("\0") ?? "";
|
|
4516
|
-
}
|
|
4517
|
-
_needsAsyncBringUp(data) {
|
|
4518
|
-
return data.secureChannel != null && data.secureChannel.securityLevel !== "none";
|
|
4519
|
-
}
|
|
4520
|
-
_finalizeReady(data) {
|
|
4521
|
-
const secure = data.secureChannel;
|
|
4522
|
-
if (secure != null && secure.securityLevel !== "none") return finalizeSecureExchangeMethods({
|
|
4523
|
-
...this._sessionContext(data),
|
|
4524
|
-
secure
|
|
4525
|
-
});
|
|
4526
|
-
return this._finalizeTransportMethods(data);
|
|
4527
|
-
}
|
|
4528
|
-
_finalizeTransportMethods(data) {
|
|
4529
|
-
return finalizePlainExchangeMethods(this._sessionContext(data));
|
|
4530
|
-
}
|
|
4531
|
-
_sessionContext(data) {
|
|
4532
|
-
return {
|
|
4533
|
-
carrier: data.carrier,
|
|
4534
|
-
updateRunConfig: data.updateRunConfig,
|
|
4535
|
-
secure: data.secureChannel
|
|
4536
|
-
};
|
|
4333
|
+
function createConnectionStateStore(handler, options) {
|
|
4334
|
+
const store = new ConnectionStateStore(options);
|
|
4335
|
+
handler.setOnConnectionBound((connection, binding) => store._persistBinding(connection, binding));
|
|
4336
|
+
for (const connection of options.getConnections()) {
|
|
4337
|
+
const binding = store._readBinding(connection);
|
|
4338
|
+
if (binding != null) handler.rehydrateConnection(connection, binding);
|
|
4537
4339
|
}
|
|
4538
|
-
|
|
4340
|
+
return store;
|
|
4341
|
+
}
|
|
4539
4342
|
//#endregion
|
|
4540
|
-
//#region src/ActionRuntime/
|
|
4343
|
+
//#region src/ActionRuntime/Handler/PeerLink/Acceptor/Hibernation/createHibernatableWsServerAdapter.ts
|
|
4541
4344
|
/**
|
|
4542
|
-
*
|
|
4543
|
-
*
|
|
4544
|
-
*
|
|
4545
|
-
*
|
|
4345
|
+
* Wire the hibernation lifecycle for an acceptor handler on a transport whose connections outlive process
|
|
4346
|
+
* eviction (e.g. a Durable Object's hibernatable WebSockets). It owns persistence end to end:
|
|
4347
|
+
* registers `setAttachment` as the handler's connection-bound callback and immediately replays every
|
|
4348
|
+
* live connection's stored binding via `getAttachment`, so results/pushes still route after a wake.
|
|
4349
|
+
*
|
|
4350
|
+
* Layered on top of the generic {@link AcceptorHandler} — it touches only the handler's neutral
|
|
4351
|
+
* `setOnConnectionBound` / `rehydrateConnection` / `receive` / `dropConnection` surface, so no
|
|
4352
|
+
* hibernation concern leaks into the handler itself.
|
|
4353
|
+
*
|
|
4354
|
+
* Construct it once when the handler is built, then forward connection events:
|
|
4355
|
+
* ```ts
|
|
4356
|
+
* const duplex = createHibernatableWsServerAdapter({ handler, getConnections, getAttachment, setAttachment });
|
|
4357
|
+
* // webSocketMessage(ws, msg) => duplex.receive(ws, msg);
|
|
4358
|
+
* // webSocketClose/Error(ws) => duplex.drop(ws);
|
|
4359
|
+
* ```
|
|
4546
4360
|
*/
|
|
4547
|
-
|
|
4548
|
-
options;
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4361
|
+
function createHibernatableWsServerAdapter(options) {
|
|
4362
|
+
const { handler, getConnections, getAttachment, setAttachment } = options;
|
|
4363
|
+
handler.setOnConnectionBound(setAttachment);
|
|
4364
|
+
for (const connection of getConnections()) {
|
|
4365
|
+
const binding = getAttachment(connection);
|
|
4366
|
+
if (binding != null) handler.rehydrateConnection(connection, binding);
|
|
4553
4367
|
}
|
|
4554
|
-
|
|
4555
|
-
|
|
4368
|
+
return {
|
|
4369
|
+
receive: (connection, frame) => handler.receive(connection, frame),
|
|
4370
|
+
drop: (connection) => handler.dropConnection(connection)
|
|
4371
|
+
};
|
|
4372
|
+
}
|
|
4373
|
+
//#endregion
|
|
4374
|
+
//#region src/ActionRuntime/Channel/serveChannel.ts
|
|
4375
|
+
/** Default accepted set, shared by every carrier: negotiate per connection to whatever the client picks. */
|
|
4376
|
+
const DEFAULT_SERVER_SECURITY_LEVELS = [
|
|
4377
|
+
"none",
|
|
4378
|
+
"authenticated",
|
|
4379
|
+
"encrypted"
|
|
4380
|
+
];
|
|
4381
|
+
function serveChannel(runtime, channel, options) {
|
|
4382
|
+
const duplexCarriers = options.carriers.filter((carrier) => !require_wsAcceptorCarrier.isExchangeAcceptorCarrier(carrier));
|
|
4383
|
+
const exchangeCarriers = options.carriers.filter(require_wsAcceptorCarrier.isExchangeAcceptorCarrier);
|
|
4384
|
+
if (exchangeCarriers.length > 1) throw new Error("serveChannel: at most one exchange carrier is supported");
|
|
4385
|
+
const exchangeCarrier = exchangeCarriers[0];
|
|
4386
|
+
const singleDuplex = duplexCarriers.length === 1;
|
|
4387
|
+
if (options.connectionState != null && !singleDuplex) throw new Error("serveChannel: `connectionState` requires exactly one duplex carrier");
|
|
4388
|
+
if (options.channelCases != null && !singleDuplex) throw new Error("serveChannel: `channelCases` requires exactly one duplex carrier");
|
|
4389
|
+
const exchangeSecure = exchangeCarrier != null && (exchangeCarrier.secure ?? true);
|
|
4390
|
+
const anyDuplexSecure = duplexCarriers.some((carrier) => carrier.secure ?? true);
|
|
4391
|
+
const securityLevel = options.securityLevel ?? DEFAULT_SERVER_SECURITY_LEVELS;
|
|
4392
|
+
let secure;
|
|
4393
|
+
if (anyDuplexSecure || exchangeSecure) {
|
|
4394
|
+
const storage = options.storage;
|
|
4395
|
+
if (storage == null) throw new Error("serveChannel: a secure carrier requires `storage`. Pass it, or set `secure: false` on the carrier for a plain endpoint.");
|
|
4396
|
+
secure = {
|
|
4397
|
+
storage,
|
|
4398
|
+
link: options.link ?? new _nice_code_util.ClientCryptoKeyLink({ storageAdapter: storage }),
|
|
4399
|
+
verifyKeyResolver: options.verifyKeyResolver ?? createStorageTofuVerifyKeyResolver(storage)
|
|
4400
|
+
};
|
|
4556
4401
|
}
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4564
|
-
|
|
4565
|
-
|
|
4566
|
-
|
|
4567
|
-
|
|
4568
|
-
|
|
4402
|
+
const plainRouter = (handler) => ({
|
|
4403
|
+
receive: (connection, frame) => handler.receive(connection, frame),
|
|
4404
|
+
drop: (connection) => handler.dropConnection(connection)
|
|
4405
|
+
});
|
|
4406
|
+
const asObject = (value) => typeof value === "object" && value != null ? value : {};
|
|
4407
|
+
const handlers = [];
|
|
4408
|
+
let connections;
|
|
4409
|
+
for (const carrier of duplexCarriers) {
|
|
4410
|
+
const handler = (carrier.secure ?? true) && secure != null ? acceptChannel(runtime, channel, {
|
|
4411
|
+
clientEnv: options.clientEnv,
|
|
4412
|
+
storageAdapter: secure.storage,
|
|
4413
|
+
link: secure.link,
|
|
4414
|
+
verifyKeyResolver: secure.verifyKeyResolver,
|
|
4415
|
+
securityLevel,
|
|
4416
|
+
send: carrier.send,
|
|
4417
|
+
defaultTimeout: options.defaultTimeout
|
|
4418
|
+
}) : createAcceptorHandler({
|
|
4419
|
+
clientEnv: options.clientEnv,
|
|
4420
|
+
createFormatMessage: channel.createCodec,
|
|
4421
|
+
send: carrier.send,
|
|
4422
|
+
runtime,
|
|
4423
|
+
defaultTimeout: options.defaultTimeout
|
|
4424
|
+
});
|
|
4425
|
+
const attach = carrier.attachmentStore;
|
|
4426
|
+
let router;
|
|
4427
|
+
if (attach == null) router = plainRouter(handler);
|
|
4428
|
+
else if (options.connectionState != null) {
|
|
4429
|
+
connections = createConnectionStateStore(handler, {
|
|
4430
|
+
schema: options.connectionState.schema,
|
|
4431
|
+
getConnections: attach.getConnections,
|
|
4432
|
+
read: attach.read,
|
|
4433
|
+
write: attach.write
|
|
4434
|
+
});
|
|
4435
|
+
router = plainRouter(handler);
|
|
4436
|
+
} else router = createHibernatableWsServerAdapter({
|
|
4437
|
+
handler,
|
|
4438
|
+
getConnections: attach.getConnections,
|
|
4439
|
+
getAttachment: (connection) => attach.read(connection)?.binding,
|
|
4440
|
+
setAttachment: (connection, binding) => attach.write(connection, {
|
|
4441
|
+
...asObject(attach.read(connection)),
|
|
4442
|
+
binding
|
|
4569
4443
|
})
|
|
4570
|
-
})
|
|
4444
|
+
});
|
|
4445
|
+
carrier._activate(router);
|
|
4446
|
+
handlers.push(handler);
|
|
4571
4447
|
}
|
|
4572
|
-
|
|
4573
|
-
|
|
4574
|
-
|
|
4575
|
-
|
|
4576
|
-
|
|
4577
|
-
|
|
4448
|
+
runtime.addHandlers([...options.handlers ?? [], ...handlers]);
|
|
4449
|
+
if (options.channelCases != null) runtime.addHandlers([acceptChannelConnections(handlers[0], channel, options.channelCases)]);
|
|
4450
|
+
const exchangeSecurity = exchangeSecure && secure != null ? {
|
|
4451
|
+
link: secure.link,
|
|
4452
|
+
verifyKeyResolver: secure.verifyKeyResolver,
|
|
4453
|
+
localCoordinate: runtime.coordinate.toJsonObject(),
|
|
4454
|
+
dictionaryVersion: channel.dictionaryVersion,
|
|
4455
|
+
securityLevel
|
|
4456
|
+
} : void 0;
|
|
4457
|
+
const defaultIsUpgrade = (request) => request.headers.get("Upgrade") === "websocket";
|
|
4458
|
+
const upgraders = [];
|
|
4459
|
+
for (const carrier of duplexCarriers) {
|
|
4460
|
+
if (carrier.upgrade == null) continue;
|
|
4461
|
+
upgraders.push({
|
|
4462
|
+
isUpgrade: carrier.isUpgrade ?? defaultIsUpgrade,
|
|
4463
|
+
upgrade: carrier.upgrade
|
|
4464
|
+
});
|
|
4578
4465
|
}
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
4582
|
-
|
|
4583
|
-
|
|
4584
|
-
|
|
4585
|
-
|
|
4586
|
-
//#region src/ActionRuntime/Transport/SecureSession/establishLinkSession.ts
|
|
4587
|
-
const HANDSHAKE_TIMEOUT_MS = 15e3;
|
|
4588
|
-
/** Plain path (no handshake): route every inbound frame to the runtime; send without crypto. */
|
|
4589
|
-
function finalizePlainLinkMethods(ctx) {
|
|
4590
|
-
const disconnectListeners = [];
|
|
4591
|
-
const abortSet = /* @__PURE__ */ new Set();
|
|
4592
|
-
const pipe = makePipe(ctx, void 0);
|
|
4593
|
-
ctx.channel.attach({
|
|
4594
|
-
onMessage: (frame) => void handleIncomingActionFrame(ctx, pipe, frame),
|
|
4595
|
-
onClose: () => onChannelClosed(ctx, disconnectListeners, abortSet),
|
|
4596
|
-
onError: () => onChannelClosed(ctx, disconnectListeners, abortSet)
|
|
4466
|
+
const fetch = createActionFetchHandler(runtime, {
|
|
4467
|
+
cors: exchangeCarrier?.cors,
|
|
4468
|
+
onWebSocketUpgrade: upgraders.length === 0 ? void 0 : (request, url) => (upgraders.find((u) => u.isUpgrade(request, url)) ?? upgraders[0]).upgrade(request, url),
|
|
4469
|
+
isWebSocketUpgrade: upgraders.length === 0 ? void 0 : (request, url) => upgraders.some((u) => u.isUpgrade(request, url)),
|
|
4470
|
+
isActionPath: exchangeCarrier != null ? exchangeCarrier.isActionPath ?? (() => true) : () => false,
|
|
4471
|
+
security: exchangeSecurity,
|
|
4472
|
+
useErrorStatus: exchangeCarrier?.useErrorStatus
|
|
4597
4473
|
});
|
|
4598
|
-
|
|
4474
|
+
const duplex = duplexCarriers.length === 1 ? duplexCarriers[0] : void 0;
|
|
4475
|
+
const pushToClient = (target, request, pushOptions) => {
|
|
4476
|
+
const owner = target instanceof RuntimeCoordinate ? handlers.find((handler) => handler.ownsLiveConnectionFor(target)) : handlers.find((handler) => handler.hasConnection(target));
|
|
4477
|
+
if (owner == null) throw new Error("serveChannel: no duplex carrier holds a connection for the push target");
|
|
4478
|
+
return owner.pushToClient(runtime, target, request, pushOptions);
|
|
4479
|
+
};
|
|
4480
|
+
const broadcast = (makeRequest, broadcastOptions) => {
|
|
4481
|
+
if (!singleDuplex) throw new Error("serveChannel: broadcast requires exactly one duplex carrier — broadcast over a specific handlers[i] instead");
|
|
4482
|
+
handlers[0].broadcast(makeRequest, {
|
|
4483
|
+
runtime,
|
|
4484
|
+
...broadcastOptions
|
|
4485
|
+
});
|
|
4486
|
+
};
|
|
4487
|
+
return {
|
|
4488
|
+
handlers,
|
|
4489
|
+
fetch,
|
|
4490
|
+
duplex,
|
|
4491
|
+
pushToClient,
|
|
4492
|
+
broadcast,
|
|
4493
|
+
connections
|
|
4494
|
+
};
|
|
4599
4495
|
}
|
|
4496
|
+
//#endregion
|
|
4497
|
+
//#region src/ActionRuntime/Transport/Carrier/duplex/inMemory/createInMemoryChannel.ts
|
|
4600
4498
|
/**
|
|
4601
|
-
*
|
|
4602
|
-
*
|
|
4603
|
-
*
|
|
4499
|
+
* Two cross-wired in-process byte channels — a loopback carrier with no socket. The client end is a
|
|
4500
|
+
* {@link IDuplexCarrier} you hand to a {@link LinkTransport}; the server end plugs into an
|
|
4501
|
+
* `AcceptorHandler` (`send: (_, f) => serverEndpoint.send(f)`, and `serverEndpoint.onMessage(f =>
|
|
4502
|
+
* handler.receive(conn, f))`). Frames are delivered on a microtask, so each side observes the other
|
|
4503
|
+
* asynchronously — exactly like a real transport — which makes this ideal for tests and for running
|
|
4504
|
+
* two runtimes in one process (or proving a non-WS carrier end to end).
|
|
4604
4505
|
*/
|
|
4605
|
-
|
|
4606
|
-
|
|
4607
|
-
|
|
4608
|
-
|
|
4609
|
-
let
|
|
4610
|
-
|
|
4611
|
-
const
|
|
4612
|
-
|
|
4613
|
-
|
|
4614
|
-
|
|
4615
|
-
|
|
4616
|
-
|
|
4617
|
-
return;
|
|
4618
|
-
}
|
|
4619
|
-
if (typeof frame === "string") {
|
|
4620
|
-
const message = decodeHandshakeMessage(frame);
|
|
4621
|
-
if (message != null) {
|
|
4622
|
-
const waiter = handshakeWaiters.shift();
|
|
4623
|
-
if (waiter != null) waiter(message);
|
|
4624
|
-
else handshakeQueue.push(message);
|
|
4625
|
-
return;
|
|
4626
|
-
}
|
|
4627
|
-
}
|
|
4628
|
-
pendingActionFrames.push(frame);
|
|
4629
|
-
},
|
|
4630
|
-
onClose: () => onChannelClosed(ctx, disconnectListeners, abortSet),
|
|
4631
|
-
onError: () => onChannelClosed(ctx, disconnectListeners, abortSet)
|
|
4632
|
-
});
|
|
4633
|
-
const nextHandshakeMessage = () => {
|
|
4634
|
-
const queued = handshakeQueue.shift();
|
|
4635
|
-
if (queued != null) return Promise.resolve(queued);
|
|
4636
|
-
return new Promise((resolve, reject) => {
|
|
4637
|
-
const timeout = setTimeout(() => reject(/* @__PURE__ */ new Error("[link-handshake] timed out waiting for peer reply")), HANDSHAKE_TIMEOUT_MS);
|
|
4638
|
-
handshakeWaiters.push((message) => {
|
|
4639
|
-
clearTimeout(timeout);
|
|
4640
|
-
resolve(message);
|
|
4641
|
-
});
|
|
4506
|
+
function createInMemoryChannelPair() {
|
|
4507
|
+
let clientMessage;
|
|
4508
|
+
let clientClose;
|
|
4509
|
+
let serverMessage;
|
|
4510
|
+
let serverClose;
|
|
4511
|
+
let open = true;
|
|
4512
|
+
const closeBoth = () => {
|
|
4513
|
+
if (!open) return;
|
|
4514
|
+
open = false;
|
|
4515
|
+
queueMicrotask(() => {
|
|
4516
|
+
clientClose?.();
|
|
4517
|
+
serverClose?.();
|
|
4642
4518
|
});
|
|
4643
4519
|
};
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4647
|
-
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
|
|
4653
|
-
|
|
4654
|
-
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
|
|
4669
|
-
|
|
4670
|
-
|
|
4671
|
-
const accept = await nextHandshakeMessage();
|
|
4672
|
-
if (accept.t === "reject") throw new Error(`[link-handshake] rejected by peer: ${accept.reason}`);
|
|
4673
|
-
if (accept.t !== "accept") throw new Error(`[link-handshake] expected accept, got ${accept.t}`);
|
|
4674
|
-
const result = await handshake.onAccept(accept);
|
|
4675
|
-
return result.securityLevel === "encrypted" ? createActionFrameCrypto({
|
|
4676
|
-
link: secure.link,
|
|
4677
|
-
linkedClientId: result.linkedClientId
|
|
4678
|
-
}) : void 0;
|
|
4679
|
-
}
|
|
4680
|
-
function buildSendMethods(ctx, pipe, disconnectListeners, abortSet) {
|
|
4681
|
-
const channel = ctx.channel;
|
|
4682
|
-
const sendActionData = (inputs) => {
|
|
4683
|
-
const { action, runningAction, timeout } = inputs;
|
|
4684
|
-
if (!channel.isOpen()) {
|
|
4685
|
-
if (action.type === "request") runningAction._abort(ctx.makeDisconnectError(action.id));
|
|
4686
|
-
return;
|
|
4687
|
-
}
|
|
4688
|
-
if (action.type === "request") {
|
|
4689
|
-
abortSet.add(runningAction);
|
|
4690
|
-
const timeoutId = setTimeout(() => {
|
|
4691
|
-
runningAction._abort(err_nice_transport.fromId("timeout", { timeout }));
|
|
4692
|
-
}, timeout);
|
|
4693
|
-
runningAction.addUpdateListeners([(update) => {
|
|
4694
|
-
if (update.type === "finished") {
|
|
4695
|
-
clearTimeout(timeoutId);
|
|
4696
|
-
abortSet.delete(runningAction);
|
|
4697
|
-
}
|
|
4698
|
-
}]);
|
|
4520
|
+
return {
|
|
4521
|
+
clientChannel: {
|
|
4522
|
+
ready: Promise.resolve(),
|
|
4523
|
+
isOpen: () => open,
|
|
4524
|
+
send: (frame) => {
|
|
4525
|
+
if (!open) return;
|
|
4526
|
+
queueMicrotask(() => serverMessage?.(frame));
|
|
4527
|
+
},
|
|
4528
|
+
attach: ({ onMessage, onClose }) => {
|
|
4529
|
+
clientMessage = onMessage;
|
|
4530
|
+
clientClose = onClose;
|
|
4531
|
+
},
|
|
4532
|
+
close: closeBoth,
|
|
4533
|
+
label: "in-memory"
|
|
4534
|
+
},
|
|
4535
|
+
serverEndpoint: {
|
|
4536
|
+
send: (frame) => {
|
|
4537
|
+
if (!open) return;
|
|
4538
|
+
queueMicrotask(() => clientMessage?.(frame));
|
|
4539
|
+
},
|
|
4540
|
+
onMessage: (handler) => {
|
|
4541
|
+
serverMessage = handler;
|
|
4542
|
+
},
|
|
4543
|
+
onClose: (handler) => {
|
|
4544
|
+
serverClose = handler;
|
|
4545
|
+
},
|
|
4546
|
+
close: closeBoth
|
|
4699
4547
|
}
|
|
4700
|
-
pipe.send(ctx.formatMessage?.outgoing(inputs) ?? JSON.stringify(inputs.action.toJsonObject()));
|
|
4701
4548
|
};
|
|
4549
|
+
}
|
|
4550
|
+
//#endregion
|
|
4551
|
+
//#region src/ActionRuntime/Transport/Carrier/duplex/inMemory/inMemoryCarrier.ts
|
|
4552
|
+
/**
|
|
4553
|
+
* A loopback duplex carrier with no socket — two cross-wired in-process ends. The connector end is an
|
|
4554
|
+
* {@link IDuplexCarrierSource} for {@link secureTransport}; the acceptor end plugs into an
|
|
4555
|
+
* `AcceptorHandler`. Ideal for tests and for running two runtimes in one process, or proving a
|
|
4556
|
+
* non-WS carrier end to end.
|
|
4557
|
+
*/
|
|
4558
|
+
function inMemoryCarrier() {
|
|
4559
|
+
const { clientChannel, serverEndpoint } = createInMemoryChannelPair();
|
|
4560
|
+
return {
|
|
4561
|
+
carrier: {
|
|
4562
|
+
carrierLabel: "memory",
|
|
4563
|
+
open: () => clientChannel,
|
|
4564
|
+
getCacheKey: () => ["memory"]
|
|
4565
|
+
},
|
|
4566
|
+
serverEndpoint
|
|
4567
|
+
};
|
|
4568
|
+
}
|
|
4569
|
+
//#endregion
|
|
4570
|
+
//#region src/ActionRuntime/Transport/Carrier/duplex/rtc/rtcDataChannelByteChannel.ts
|
|
4571
|
+
/**
|
|
4572
|
+
* Adapt a WebRTC `RTCDataChannel` to the carrier-agnostic {@link IDuplexCarrier}, so two browsers
|
|
4573
|
+
* (or two mobile apps) linked peer-to-peer — no server in the middle — run the *same* secure session as
|
|
4574
|
+
* a WebSocket. Hand it to `createSecureLinkTransport({ openChannel: () => rtcDataChannelByteChannel(dc) })`.
|
|
4575
|
+
*
|
|
4576
|
+
* The data channel must already be created (its negotiation/signaling is the app's concern); this only
|
|
4577
|
+
* drives bytes over it. Binary frames are requested as `ArrayBuffer` so the binary session codec unpacks
|
|
4578
|
+
* them synchronously; a `Blob` (if the channel hands one back) is normalized to a buffer.
|
|
4579
|
+
*/
|
|
4580
|
+
function rtcDataChannelByteChannel(dc) {
|
|
4581
|
+
dc.binaryType = "arraybuffer";
|
|
4582
|
+
let intentional = false;
|
|
4583
|
+
let onMessage;
|
|
4584
|
+
let onClose;
|
|
4585
|
+
const preAttach = [];
|
|
4586
|
+
const deliver = (frame) => {
|
|
4587
|
+
if (onMessage != null) onMessage(frame);
|
|
4588
|
+
else preAttach.push(frame);
|
|
4589
|
+
};
|
|
4590
|
+
dc.addEventListener("message", async (event) => {
|
|
4591
|
+
const frame = await normalizeFrame$1(event.data);
|
|
4592
|
+
if (frame !== void 0) deliver(frame);
|
|
4593
|
+
});
|
|
4594
|
+
dc.addEventListener("close", () => {
|
|
4595
|
+
if (!intentional) console.error("RTCDataChannel closed");
|
|
4596
|
+
onClose?.();
|
|
4597
|
+
});
|
|
4598
|
+
dc.addEventListener("error", (event) => {
|
|
4599
|
+
console.error("RTCDataChannel error:", event);
|
|
4600
|
+
onClose?.();
|
|
4601
|
+
});
|
|
4702
4602
|
return {
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4603
|
+
ready: new Promise((resolve, reject) => {
|
|
4604
|
+
if (dc.readyState === "open") {
|
|
4605
|
+
resolve();
|
|
4606
|
+
return;
|
|
4607
|
+
}
|
|
4608
|
+
dc.addEventListener("open", () => resolve(), { once: true });
|
|
4609
|
+
dc.addEventListener("error", (event) => reject(event), { once: true });
|
|
4610
|
+
dc.addEventListener("close", () => reject(/* @__PURE__ */ new Error("RTCDataChannel closed before open")), { once: true });
|
|
4611
|
+
}),
|
|
4612
|
+
isOpen: () => dc.readyState === "open",
|
|
4613
|
+
send: (frame) => {
|
|
4614
|
+
if (typeof frame === "string" || frame instanceof ArrayBuffer) dc.send(frame);
|
|
4615
|
+
else dc.send(new Uint8Array(frame));
|
|
4707
4616
|
},
|
|
4708
|
-
|
|
4617
|
+
attach: (handlers) => {
|
|
4618
|
+
onMessage = handlers.onMessage;
|
|
4619
|
+
onClose = handlers.onClose;
|
|
4620
|
+
for (const frame of preAttach) handlers.onMessage(frame);
|
|
4621
|
+
preAttach.length = 0;
|
|
4622
|
+
},
|
|
4623
|
+
close: () => {
|
|
4624
|
+
intentional = true;
|
|
4709
4625
|
try {
|
|
4710
|
-
|
|
4626
|
+
dc.close();
|
|
4711
4627
|
} catch {}
|
|
4712
4628
|
},
|
|
4713
|
-
|
|
4714
|
-
|
|
4715
|
-
action: payload,
|
|
4716
|
-
...clients
|
|
4717
|
-
}) : void 0;
|
|
4718
|
-
pipe.send(formatted ?? JSON.stringify(payload.toJsonObject()));
|
|
4629
|
+
get label() {
|
|
4630
|
+
return dc.label != null && dc.label !== "" ? dc.label : void 0;
|
|
4719
4631
|
}
|
|
4720
4632
|
};
|
|
4721
4633
|
}
|
|
4722
|
-
async function
|
|
4723
|
-
|
|
4724
|
-
if (
|
|
4725
|
-
const rawJson = decodeActionFrame(decoded, ctx.formatMessage);
|
|
4726
|
-
if (rawJson != null) ctx.resolvers.onIncomingActionDataJson(rawJson);
|
|
4727
|
-
}
|
|
4728
|
-
function onChannelClosed(ctx, disconnectListeners, abortSet) {
|
|
4729
|
-
for (const cb of disconnectListeners) cb();
|
|
4730
|
-
const error = ctx.makeDisconnectError("—");
|
|
4731
|
-
for (const ra of [...abortSet]) ra._abort(error);
|
|
4634
|
+
async function normalizeFrame$1(data) {
|
|
4635
|
+
if (typeof data === "string" || data instanceof ArrayBuffer || data instanceof Uint8Array) return data;
|
|
4636
|
+
if (typeof Blob !== "undefined" && data instanceof Blob) return await data.arrayBuffer();
|
|
4732
4637
|
}
|
|
4733
4638
|
//#endregion
|
|
4734
|
-
//#region src/ActionRuntime/Transport/
|
|
4735
|
-
/** Abort error for a closed link channel (carrier-neutral — the carrier itself isn't named). */
|
|
4736
|
-
function linkDisconnectError(actionId) {
|
|
4737
|
-
return err_nice_transport.fromId("send_failed", {
|
|
4738
|
-
actionId,
|
|
4739
|
-
actionState: "request",
|
|
4740
|
-
message: "link channel disconnected"
|
|
4741
|
-
});
|
|
4742
|
-
}
|
|
4639
|
+
//#region src/ActionRuntime/Transport/Carrier/duplex/rtc/rtcCarrier.ts
|
|
4743
4640
|
/**
|
|
4744
|
-
*
|
|
4745
|
-
*
|
|
4746
|
-
*
|
|
4747
|
-
* channel, a Bluetooth characteristic, and an in-memory pipe all run the identical secure layer.
|
|
4641
|
+
* A WebRTC {@link IDuplexCarrierSource} over an already-negotiated `RTCDataChannel` (signaling is the
|
|
4642
|
+
* app's concern). Hand it to {@link secureTransport} so two browsers/apps linked peer-to-peer run the
|
|
4643
|
+
* identical secure session as a WebSocket.
|
|
4748
4644
|
*/
|
|
4749
|
-
|
|
4750
|
-
|
|
4751
|
-
|
|
4752
|
-
|
|
4753
|
-
|
|
4754
|
-
|
|
4755
|
-
|
|
4756
|
-
|
|
4757
|
-
|
|
4758
|
-
|
|
4759
|
-
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
4764
|
-
|
|
4765
|
-
|
|
4766
|
-
|
|
4767
|
-
|
|
4768
|
-
|
|
4769
|
-
|
|
4770
|
-
|
|
4771
|
-
secure
|
|
4772
|
-
});
|
|
4773
|
-
return this._finalizeTransportMethods(data);
|
|
4645
|
+
function rtcCarrier(dataChannel, options = {}) {
|
|
4646
|
+
return {
|
|
4647
|
+
carrierLabel: "webrtc",
|
|
4648
|
+
open: () => rtcDataChannelByteChannel(dataChannel),
|
|
4649
|
+
getCacheKey: options.getTransportCacheKey ?? (() => ["webrtc"]),
|
|
4650
|
+
getRouteInfo: options.getRouteInfo
|
|
4651
|
+
};
|
|
4652
|
+
}
|
|
4653
|
+
//#endregion
|
|
4654
|
+
//#region src/ActionRuntime/Transport/Carrier/duplex/ws/err_nice_transport_ws.ts
|
|
4655
|
+
let EErrId_NiceTransport_WebSocket = /* @__PURE__ */ function(EErrId_NiceTransport_WebSocket) {
|
|
4656
|
+
EErrId_NiceTransport_WebSocket["ws_disconnected"] = "ws_disconnected";
|
|
4657
|
+
EErrId_NiceTransport_WebSocket["ws_create_failed"] = "ws_create_failed";
|
|
4658
|
+
EErrId_NiceTransport_WebSocket["ws_error"] = "ws_error";
|
|
4659
|
+
return EErrId_NiceTransport_WebSocket;
|
|
4660
|
+
}({});
|
|
4661
|
+
const err_nice_transport_ws = err_nice_transport.createChildDomain({
|
|
4662
|
+
domain: "ws_transport",
|
|
4663
|
+
schema: {
|
|
4664
|
+
["ws_disconnected"]: (0, _nice_code_error.err)({ message: () => `WebSocket transport disconnected.` }),
|
|
4665
|
+
["ws_create_failed"]: (0, _nice_code_error.err)({ message: ({ originalError }) => `Failed to create WebSocket transport.${originalError ? ` Original error: ${originalError.message}` : ""}` }),
|
|
4666
|
+
["ws_error"]: (0, _nice_code_error.err)({ message: ({ originalError }) => `WebSocket transport error.${originalError ? ` Original error: ${originalError.message}` : ""}` })
|
|
4774
4667
|
}
|
|
4775
|
-
|
|
4776
|
-
|
|
4777
|
-
|
|
4778
|
-
|
|
4779
|
-
|
|
4780
|
-
|
|
4781
|
-
|
|
4782
|
-
|
|
4668
|
+
});
|
|
4669
|
+
//#endregion
|
|
4670
|
+
//#region src/ActionRuntime/Transport/Carrier/duplex/ws/ws_util.ts
|
|
4671
|
+
/**
|
|
4672
|
+
* Send a text or binary frame over a socket. A binary formatter may hand back a `Uint8Array` whose
|
|
4673
|
+
* backing buffer is typed as `ArrayBufferLike` (msgpackr pools buffers / may be `SharedArrayBuffer`),
|
|
4674
|
+
* which `WebSocket.send`'s `BufferSource` parameter rejects — copy it into a fresh `ArrayBuffer`-backed
|
|
4675
|
+
* view so the type (and the bytes) are safe to send.
|
|
4676
|
+
*/
|
|
4677
|
+
function sendFrame(ws, data) {
|
|
4678
|
+
if (typeof data === "string" || data instanceof ArrayBuffer) {
|
|
4679
|
+
ws.send(data);
|
|
4680
|
+
return;
|
|
4783
4681
|
}
|
|
4784
|
-
|
|
4785
|
-
|
|
4682
|
+
ws.send(new Uint8Array(data));
|
|
4683
|
+
}
|
|
4684
|
+
/** Compact a WebSocket URL to `host/pathname` for devtools display, falling back to the raw url. */
|
|
4685
|
+
function shortWs(url) {
|
|
4686
|
+
try {
|
|
4687
|
+
const u = new URL(url);
|
|
4688
|
+
return `${u.host}${u.pathname}`;
|
|
4689
|
+
} catch {
|
|
4690
|
+
return url;
|
|
4786
4691
|
}
|
|
4787
|
-
}
|
|
4692
|
+
}
|
|
4788
4693
|
//#endregion
|
|
4789
|
-
//#region src/ActionRuntime/Transport/
|
|
4694
|
+
//#region src/ActionRuntime/Transport/Carrier/duplex/ws/webSocketByteChannel.ts
|
|
4695
|
+
/**
|
|
4696
|
+
* Adapt a `WebSocket` to the carrier-agnostic {@link IDuplexCarrier}, so the WebSocket becomes
|
|
4697
|
+
* "just another carrier" under the shared secure session. It owns every WebSocket-specific concern —
|
|
4698
|
+
* awaiting `open`, normalizing `Blob` frames to bytes, suppressing the log on a deliberate `close`, and
|
|
4699
|
+
* buffering any frame that arrives before the session attaches its handler — leaving the session itself
|
|
4700
|
+
* carrier-neutral.
|
|
4701
|
+
*/
|
|
4702
|
+
function webSocketByteChannel(ws) {
|
|
4703
|
+
let intentional = false;
|
|
4704
|
+
let onMessage;
|
|
4705
|
+
let onClose;
|
|
4706
|
+
const preAttach = [];
|
|
4707
|
+
const deliver = (frame) => {
|
|
4708
|
+
if (onMessage != null) onMessage(frame);
|
|
4709
|
+
else preAttach.push(frame);
|
|
4710
|
+
};
|
|
4711
|
+
ws.addEventListener("message", async (event) => {
|
|
4712
|
+
const frame = await normalizeFrame(event.data);
|
|
4713
|
+
if (frame !== void 0) deliver(frame);
|
|
4714
|
+
});
|
|
4715
|
+
ws.addEventListener("close", (event) => {
|
|
4716
|
+
if (!intentional) console.error("WebSocket closed:", event);
|
|
4717
|
+
onClose?.();
|
|
4718
|
+
});
|
|
4719
|
+
ws.addEventListener("error", (event) => {
|
|
4720
|
+
console.error("WebSocket error:", event);
|
|
4721
|
+
onClose?.();
|
|
4722
|
+
});
|
|
4723
|
+
return {
|
|
4724
|
+
ready: new Promise((resolve, reject) => {
|
|
4725
|
+
if (ws.readyState === WebSocket.OPEN) {
|
|
4726
|
+
resolve();
|
|
4727
|
+
return;
|
|
4728
|
+
}
|
|
4729
|
+
ws.addEventListener("open", () => resolve(), { once: true });
|
|
4730
|
+
ws.addEventListener("error", (event) => reject(event), { once: true });
|
|
4731
|
+
ws.addEventListener("close", (event) => reject(/* @__PURE__ */ new Error(`WebSocket closed before open: code=${event.code}`)), { once: true });
|
|
4732
|
+
}),
|
|
4733
|
+
isOpen: () => ws.readyState === WebSocket.OPEN,
|
|
4734
|
+
send: (frame) => sendFrame(ws, frame),
|
|
4735
|
+
attach: (handlers) => {
|
|
4736
|
+
onMessage = handlers.onMessage;
|
|
4737
|
+
onClose = handlers.onClose;
|
|
4738
|
+
for (const frame of preAttach) handlers.onMessage(frame);
|
|
4739
|
+
preAttach.length = 0;
|
|
4740
|
+
},
|
|
4741
|
+
close: () => {
|
|
4742
|
+
intentional = true;
|
|
4743
|
+
try {
|
|
4744
|
+
ws.close();
|
|
4745
|
+
} catch {}
|
|
4746
|
+
},
|
|
4747
|
+
get label() {
|
|
4748
|
+
return ws.url != null && ws.url !== "" ? ws.url : void 0;
|
|
4749
|
+
}
|
|
4750
|
+
};
|
|
4751
|
+
}
|
|
4752
|
+
/** Accept text + binary frames (ArrayBuffer / Uint8Array / Blob); Blobs are converted to a buffer. */
|
|
4753
|
+
async function normalizeFrame(data) {
|
|
4754
|
+
if (typeof data === "string" || data instanceof ArrayBuffer || data instanceof Uint8Array) return data;
|
|
4755
|
+
if (typeof Blob !== "undefined" && data instanceof Blob) return await data.arrayBuffer();
|
|
4756
|
+
}
|
|
4757
|
+
//#endregion
|
|
4758
|
+
//#region src/ActionRuntime/Transport/Carrier/duplex/ws/wsCarrier.ts
|
|
4790
4759
|
/**
|
|
4791
|
-
* A
|
|
4792
|
-
*
|
|
4793
|
-
*
|
|
4794
|
-
* or an in-memory pipe. Reported with an overridable carrier label in the devtools (defaults to "link").
|
|
4760
|
+
* A WebSocket {@link IDuplexCarrierSource}: opens an `arraybuffer` socket to `url` (cached per endpoint)
|
|
4761
|
+
* and adapts it to a carrier. Hand it to {@link secureTransport} (or `LinkTransport`) — the WebSocket is
|
|
4762
|
+
* now "just another carrier" under the shared secure session, with no WS-specific transport class.
|
|
4795
4763
|
*/
|
|
4796
|
-
|
|
4797
|
-
|
|
4798
|
-
|
|
4799
|
-
|
|
4800
|
-
|
|
4801
|
-
|
|
4802
|
-
|
|
4803
|
-
|
|
4804
|
-
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
|
|
4808
|
-
|
|
4809
|
-
|
|
4810
|
-
|
|
4811
|
-
|
|
4812
|
-
|
|
4813
|
-
readyData: {
|
|
4814
|
-
channel: options.openChannel(input),
|
|
4815
|
-
formatMessage: options.createFormatMessage?.() ?? options.formatMessage,
|
|
4816
|
-
updateRunConfig: options.updateRunConfig,
|
|
4817
|
-
secureChannel: options.security
|
|
4818
|
-
}
|
|
4819
|
-
})
|
|
4820
|
-
}) }, ctx.resolvers);
|
|
4821
|
-
}
|
|
4822
|
-
getRouteInfo(input) {
|
|
4823
|
-
if (this.options.getRouteInfo != null) return this.options.getRouteInfo(input);
|
|
4824
|
-
return {
|
|
4825
|
-
carrierLabel: this.options.label ?? "link",
|
|
4826
|
-
summary: this.options.label ?? "link"
|
|
4827
|
-
};
|
|
4828
|
-
}
|
|
4829
|
-
};
|
|
4764
|
+
function wsCarrier(url, options = {}) {
|
|
4765
|
+
return {
|
|
4766
|
+
carrierLabel: "ws",
|
|
4767
|
+
open: (input) => webSocketByteChannel(options.createWebSocket?.(input) ?? defaultWebSocket(url)),
|
|
4768
|
+
getCacheKey: options.getTransportCacheKey ?? (() => [url]),
|
|
4769
|
+
getRouteInfo: options.getRouteInfo ?? (() => ({
|
|
4770
|
+
carrierLabel: "ws",
|
|
4771
|
+
url,
|
|
4772
|
+
summary: `ws ${shortWs(url)}`
|
|
4773
|
+
}))
|
|
4774
|
+
};
|
|
4775
|
+
}
|
|
4776
|
+
function defaultWebSocket(url) {
|
|
4777
|
+
const ws = new WebSocket(url);
|
|
4778
|
+
ws.binaryType = "arraybuffer";
|
|
4779
|
+
return ws;
|
|
4780
|
+
}
|
|
4830
4781
|
//#endregion
|
|
4831
|
-
//#region src/ActionRuntime/Transport/Carrier/
|
|
4782
|
+
//#region src/ActionRuntime/Transport/Carrier/exchange/http/httpAcceptorCarrier.ts
|
|
4832
4783
|
/**
|
|
4833
|
-
*
|
|
4834
|
-
*
|
|
4835
|
-
*
|
|
4784
|
+
* An HTTP {@link IExchangeAcceptorCarrier}: the accept-in dual of {@link httpCarrier}. It serves the
|
|
4785
|
+
* secure exchange protocol (handshake → token session → encrypted frames) over web-standard
|
|
4786
|
+
* `Request`/`Response`. The crypto identity, runtime coordinate, dictionary version, and accepted security
|
|
4787
|
+
* levels are all supplied centrally by `serveChannel`, so this only needs to say which requests carry an
|
|
4788
|
+
* action envelope and how to answer CORS.
|
|
4836
4789
|
*/
|
|
4837
|
-
function
|
|
4838
|
-
return
|
|
4790
|
+
function httpAcceptorCarrier(options = {}) {
|
|
4791
|
+
return {
|
|
4792
|
+
shape: "exchange",
|
|
4793
|
+
carrierLabel: options.carrierLabel ?? "http",
|
|
4794
|
+
secure: options.secure,
|
|
4795
|
+
isActionPath: options.isActionPath,
|
|
4796
|
+
cors: options.cors,
|
|
4797
|
+
useErrorStatus: options.useErrorStatus
|
|
4798
|
+
};
|
|
4839
4799
|
}
|
|
4840
4800
|
//#endregion
|
|
4841
|
-
//#region src/ActionRuntime/Transport/
|
|
4842
|
-
function
|
|
4843
|
-
|
|
4844
|
-
|
|
4845
|
-
|
|
4846
|
-
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
|
|
4801
|
+
//#region src/ActionRuntime/Transport/Carrier/exchange/http/httpCarrier.ts
|
|
4802
|
+
function shortPath(url) {
|
|
4803
|
+
try {
|
|
4804
|
+
return new URL(url).pathname || url;
|
|
4805
|
+
} catch {
|
|
4806
|
+
return url;
|
|
4807
|
+
}
|
|
4808
|
+
}
|
|
4809
|
+
/**
|
|
4810
|
+
* An HTTP {@link IExchangeCarrierSource}: each `exchange` POSTs one frame body to the action endpoint and
|
|
4811
|
+
* resolves with the response body as the single correlated reply. Hand it to {@link secureTransport} —
|
|
4812
|
+
* HTTP then runs the *same* secure session as a duplex carrier (handshake → token → encrypted frames),
|
|
4813
|
+
* the request/reply correlation provided for free by the HTTP transaction.
|
|
4814
|
+
*
|
|
4815
|
+
* `createRequest` derives the URL/headers per action (keep it simple with `() => ({ url })`). The body is
|
|
4816
|
+
* the session's responsibility, so it is never built here.
|
|
4817
|
+
*/
|
|
4818
|
+
function httpCarrier(createRequest, options = {}) {
|
|
4819
|
+
const doFetch = options.fetch ?? fetch;
|
|
4820
|
+
return {
|
|
4821
|
+
shape: "exchange",
|
|
4822
|
+
carrierLabel: "http",
|
|
4823
|
+
open: (input) => {
|
|
4824
|
+
const request = createRequest(input);
|
|
4825
|
+
return {
|
|
4826
|
+
label: request.url,
|
|
4827
|
+
exchange: async (frame, opts) => {
|
|
4828
|
+
return await (await doFetch(request.url, {
|
|
4829
|
+
method: "POST",
|
|
4830
|
+
headers: {
|
|
4831
|
+
"Content-Type": "application/json",
|
|
4832
|
+
...request.headers
|
|
4833
|
+
},
|
|
4834
|
+
body: typeof frame === "string" ? frame : new Uint8Array(frame),
|
|
4835
|
+
signal: opts?.signal
|
|
4836
|
+
})).text();
|
|
4837
|
+
}
|
|
4838
|
+
};
|
|
4839
|
+
},
|
|
4840
|
+
getCacheKey: options.getTransportCacheKey ?? ((input) => [createRequest(input).url]),
|
|
4841
|
+
getRouteInfo: options.getRouteInfo ?? ((input) => {
|
|
4842
|
+
const { url } = createRequest(input);
|
|
4843
|
+
return {
|
|
4844
|
+
carrierLabel: "http",
|
|
4845
|
+
method: "POST",
|
|
4846
|
+
url,
|
|
4847
|
+
summary: `POST ${shortPath(url)}`
|
|
4848
|
+
};
|
|
4849
|
+
})
|
|
4850
|
+
};
|
|
4862
4851
|
}
|
|
4863
4852
|
//#endregion
|
|
4864
|
-
//#region src/ActionRuntime/Transport/
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
4870
|
-
|
|
4871
|
-
|
|
4853
|
+
//#region src/ActionRuntime/Transport/codec/createBinaryWireAdapter.ts
|
|
4854
|
+
/**
|
|
4855
|
+
* Positional layout of the stateless binary envelope. A flat tuple (rather than an object) strips the
|
|
4856
|
+
* repeated `domain`/`id`/`form`/`type` and context key names from every frame, and we carry only the
|
|
4857
|
+
* context fields the receiver can't recompute: `cuid` (correlation) and `originClient` (return
|
|
4858
|
+
* routing).
|
|
4859
|
+
*
|
|
4860
|
+
* [ routeInt, typeInt, time, cuid, originClient, payloadData ]
|
|
4861
|
+
*
|
|
4862
|
+
* Dropped vs the JSON wire: `form`/`type` strings, `inputHash`/`outputHash` (recomputed on hydrate),
|
|
4863
|
+
* `context.timeCreated` (reconstructed from `time`) and `context.routing` (rebuilt empty — the
|
|
4864
|
+
* receiver re-stamps its own route items as it handles the action). For the leanest possible frames
|
|
4865
|
+
* (integer correlation, identity dropped after a handshake), use `createBinaryWireSessionFactory`.
|
|
4866
|
+
*/
|
|
4867
|
+
const ENVELOPE = {
|
|
4868
|
+
route: 0,
|
|
4869
|
+
type: 1,
|
|
4870
|
+
time: 2,
|
|
4871
|
+
cuid: 3,
|
|
4872
|
+
originClient: 4,
|
|
4873
|
+
payload: 5
|
|
4874
|
+
};
|
|
4875
|
+
const ENVELOPE_LENGTH = 6;
|
|
4876
|
+
/**
|
|
4877
|
+
* Builds a *stateless* `formatMessage` pipeline for {@link LinkTransport}, packing action
|
|
4878
|
+
* payloads into a compact msgpackr binary frame instead of JSON. The `domain`/`id` route collapses to
|
|
4879
|
+
* a single integer drawn from a shared dictionary; `form`/`type`, the recomputable
|
|
4880
|
+
* `inputHash`/`outputHash`, and the per-frame `context.routing`/`context.timeCreated` are all dropped
|
|
4881
|
+
* (see {@link ENVELOPE}).
|
|
4882
|
+
*
|
|
4883
|
+
* No validation runs here: `incoming` blindly reconstructs the wire JSON shape and hands it back to
|
|
4884
|
+
* the connection, which flows into `ActionRuntime` → `domain.hydrateAnyAction()` where the Valibot
|
|
4885
|
+
* schemas validate it exactly as they would for a JSON frame.
|
|
4886
|
+
*
|
|
4887
|
+
* Both ends of the socket MUST construct the adapter with the same domains in the same order — the
|
|
4888
|
+
* integer dictionary is positional. Mismatched dictionaries will route to the wrong action.
|
|
4889
|
+
*
|
|
4890
|
+
* Because `incoming` returns `undefined` for text frames, a binary server can still serve plain-JSON
|
|
4891
|
+
* clients on the same runtime (the connection falls back to its built-in JSON parser).
|
|
4892
|
+
*/
|
|
4893
|
+
function createBinaryWireAdapter(domains) {
|
|
4894
|
+
const { routeToInt, intToRoute } = buildActionRouteDictionary(domains);
|
|
4895
|
+
return {
|
|
4896
|
+
outgoing: (input) => {
|
|
4897
|
+
const json = input.action.toJsonObject();
|
|
4898
|
+
const routeKey = `${json.domain}:${json.id}`;
|
|
4899
|
+
const routeInt = routeToInt.get(routeKey);
|
|
4900
|
+
if (routeInt == null) throw new Error(`[binary-wire] Cannot pack unregistered action route: ${routeKey}`);
|
|
4901
|
+
const envelope = new Array(ENVELOPE_LENGTH);
|
|
4902
|
+
envelope[ENVELOPE.route] = routeInt;
|
|
4903
|
+
envelope[ENVELOPE.type] = PayloadTypeToInt[json.type];
|
|
4904
|
+
envelope[ENVELOPE.time] = json.time;
|
|
4905
|
+
envelope[ENVELOPE.cuid] = json.context.cuid;
|
|
4906
|
+
envelope[ENVELOPE.originClient] = json.context.originClient;
|
|
4907
|
+
envelope[ENVELOPE.payload] = extractWirePayload(json);
|
|
4908
|
+
return (0, msgpackr.pack)(envelope);
|
|
4909
|
+
},
|
|
4910
|
+
incoming: (frame) => {
|
|
4911
|
+
let buffer;
|
|
4912
|
+
if (frame instanceof ArrayBuffer) buffer = new Uint8Array(frame);
|
|
4913
|
+
else if (frame instanceof Uint8Array) buffer = frame;
|
|
4914
|
+
else return;
|
|
4915
|
+
try {
|
|
4916
|
+
const envelope = (0, msgpackr.unpack)(buffer);
|
|
4917
|
+
if (!Array.isArray(envelope) || envelope.length !== ENVELOPE_LENGTH) return void 0;
|
|
4918
|
+
const routeMeta = intToRoute[envelope[ENVELOPE.route]];
|
|
4919
|
+
const payloadType = ReversePayloadType[envelope[ENVELOPE.type]];
|
|
4920
|
+
if (routeMeta == null || payloadType == null) return void 0;
|
|
4921
|
+
const time = envelope[ENVELOPE.time];
|
|
4922
|
+
return assembleWireJson(routeMeta, payloadType, time, {
|
|
4923
|
+
cuid: envelope[ENVELOPE.cuid],
|
|
4924
|
+
timeCreated: time,
|
|
4925
|
+
routing: [],
|
|
4926
|
+
originClient: envelope[ENVELOPE.originClient]
|
|
4927
|
+
}, envelope[ENVELOPE.payload]);
|
|
4928
|
+
} catch (e) {
|
|
4929
|
+
console.error("[binary-wire] Failed to unpack binary action frame", e);
|
|
4930
|
+
return;
|
|
4931
|
+
}
|
|
4932
|
+
}
|
|
4872
4933
|
};
|
|
4873
|
-
const carrier = options.carrier;
|
|
4874
|
-
if (isExchangeCarrierSource(carrier)) return ExchangeTransport.create({
|
|
4875
|
-
openCarrier: carrier.open,
|
|
4876
|
-
getTransportCacheKey: carrier.getCacheKey,
|
|
4877
|
-
available: options.available,
|
|
4878
|
-
getRouteInfo: carrier.getRouteInfo,
|
|
4879
|
-
label: carrier.carrierLabel,
|
|
4880
|
-
security
|
|
4881
|
-
});
|
|
4882
|
-
return LinkTransport.create({
|
|
4883
|
-
openChannel: carrier.open,
|
|
4884
|
-
createFormatMessage: options.channel.createCodec,
|
|
4885
|
-
getTransportCacheKey: carrier.getCacheKey,
|
|
4886
|
-
available: options.available,
|
|
4887
|
-
getRouteInfo: carrier.getRouteInfo,
|
|
4888
|
-
label: carrier.carrierLabel,
|
|
4889
|
-
security
|
|
4890
|
-
});
|
|
4891
4934
|
}
|
|
4892
4935
|
//#endregion
|
|
4893
4936
|
exports.AcceptorHandler = AcceptorHandler;
|
|
@@ -4958,11 +5001,9 @@ exports.isActionPayload_Any_JsonObject = isActionPayload_Any_JsonObject;
|
|
|
4958
5001
|
exports.isActionPayload_Request_JsonObject = isActionPayload_Request_JsonObject;
|
|
4959
5002
|
exports.isActionPayload_Result_JsonObject = isActionPayload_Result_JsonObject;
|
|
4960
5003
|
exports.isExchangeAcceptorCarrier = require_wsAcceptorCarrier.isExchangeAcceptorCarrier;
|
|
4961
|
-
exports.plainTransport = plainTransport;
|
|
4962
5004
|
exports.rtcCarrier = rtcCarrier;
|
|
4963
5005
|
exports.rtcDataChannelByteChannel = rtcDataChannelByteChannel;
|
|
4964
5006
|
exports.runtimeLinkId = runtimeLinkId;
|
|
4965
|
-
exports.secureTransport = secureTransport;
|
|
4966
5007
|
exports.serveChannel = serveChannel;
|
|
4967
5008
|
exports.wsAcceptorCarrier = require_wsAcceptorCarrier.wsAcceptorCarrier;
|
|
4968
5009
|
exports.wsCarrier = wsCarrier;
|