@actuallab/fusion-rpc 12.1.62
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/dist/index.cjs +168 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +149 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
FUSION_CALL_TYPE_ID: () => FUSION_CALL_TYPE_ID,
|
|
24
|
+
FusionHub: () => FusionHub,
|
|
25
|
+
RpcOutboundComputeCall: () => RpcOutboundComputeCall,
|
|
26
|
+
defineComputeService: () => defineComputeService
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/fusion-hub.ts
|
|
31
|
+
var import_rpc2 = require("@actuallab/rpc");
|
|
32
|
+
var import_fusion = require("@actuallab/fusion");
|
|
33
|
+
var import_core2 = require("@actuallab/core");
|
|
34
|
+
|
|
35
|
+
// src/rpc-outbound-compute-call.ts
|
|
36
|
+
var import_core = require("@actuallab/core");
|
|
37
|
+
var import_rpc = require("@actuallab/rpc");
|
|
38
|
+
var RpcOutboundComputeCall = class extends import_rpc.RpcOutboundCall {
|
|
39
|
+
removeOnOk = false;
|
|
40
|
+
whenInvalidated = new import_core.PromiseSource();
|
|
41
|
+
onDisconnect() {
|
|
42
|
+
this.whenInvalidated.resolve();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// src/fusion-hub.ts
|
|
47
|
+
var FUSION_CALL_TYPE_ID = 1;
|
|
48
|
+
var FUSION_INVALIDATE_METHOD = "$sys-c.Invalidate:0";
|
|
49
|
+
function defineComputeService(name, methods) {
|
|
50
|
+
const withCallType = {};
|
|
51
|
+
for (const [methodName, input] of Object.entries(methods)) {
|
|
52
|
+
withCallType[methodName] = { ...input, callTypeId: input.callTypeId ?? FUSION_CALL_TYPE_ID };
|
|
53
|
+
}
|
|
54
|
+
return (0, import_rpc2.defineRpcService)(name, withCallType, { ctOffset: 1 });
|
|
55
|
+
}
|
|
56
|
+
var FusionHub = class extends import_rpc2.RpcHub {
|
|
57
|
+
/** Accept an incoming WebSocket and create a server peer. */
|
|
58
|
+
acceptConnection(ws) {
|
|
59
|
+
const ref = `server://${crypto.randomUUID()}`;
|
|
60
|
+
const conn = new import_rpc2.RpcWebSocketConnection(ws);
|
|
61
|
+
const peer = this.getServerPeer(ref);
|
|
62
|
+
peer.accept(conn);
|
|
63
|
+
return peer;
|
|
64
|
+
}
|
|
65
|
+
/** Accept an RpcConnection and create a server peer. */
|
|
66
|
+
acceptRpcConnection(conn) {
|
|
67
|
+
const ref = `server://${crypto.randomUUID()}`;
|
|
68
|
+
const peer = this.getServerPeer(ref);
|
|
69
|
+
peer.accept(conn);
|
|
70
|
+
return peer;
|
|
71
|
+
}
|
|
72
|
+
/** Handle compute-specific system calls ($sys-c.Invalidate), then delegate to base. */
|
|
73
|
+
handleSystemCall(message, args, outbound, inbound) {
|
|
74
|
+
const method = message.Method;
|
|
75
|
+
const relatedId = message.RelatedId ?? 0;
|
|
76
|
+
if (method === FUSION_INVALIDATE_METHOD) {
|
|
77
|
+
const call = outbound.remove(relatedId);
|
|
78
|
+
if (call instanceof RpcOutboundComputeCall) {
|
|
79
|
+
call.whenInvalidated.resolve();
|
|
80
|
+
}
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
super.handleSystemCall(message, args, outbound, inbound);
|
|
84
|
+
}
|
|
85
|
+
/** Override to apply ctOffset=1 default and FUSION_CALL_TYPE_ID for compute methods. */
|
|
86
|
+
_buildServiceDef(cls) {
|
|
87
|
+
const svcMeta = (0, import_rpc2.getServiceMeta)(cls);
|
|
88
|
+
if (svcMeta === void 0) throw new Error("Contract class missing @rpcService metadata");
|
|
89
|
+
const methodsMeta = (0, import_rpc2.getMethodsMeta)(cls) ?? {};
|
|
90
|
+
const ctOffset = svcMeta.ctOffset ?? 1;
|
|
91
|
+
const methods = /* @__PURE__ */ new Map();
|
|
92
|
+
for (const [name, meta] of Object.entries(methodsMeta)) {
|
|
93
|
+
const wireArgCount = meta.argCount + ctOffset;
|
|
94
|
+
const mapKey = `${name}:${wireArgCount}`;
|
|
95
|
+
methods.set(mapKey, {
|
|
96
|
+
name,
|
|
97
|
+
serviceName: svcMeta.name,
|
|
98
|
+
argCount: meta.argCount,
|
|
99
|
+
wireArgCount,
|
|
100
|
+
callTypeId: meta.compute === true ? FUSION_CALL_TYPE_ID : 0,
|
|
101
|
+
stream: meta.stream ?? false,
|
|
102
|
+
noWait: meta.noWait ?? false
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return { name: svcMeta.name, methods };
|
|
106
|
+
}
|
|
107
|
+
_wrapServerMethod(methodDef, fn, impl) {
|
|
108
|
+
if (methodDef.callTypeId !== FUSION_CALL_TYPE_ID)
|
|
109
|
+
return fn;
|
|
110
|
+
const cf = new import_fusion.ComputeFunction(methodDef.name, fn);
|
|
111
|
+
return async (...args) => {
|
|
112
|
+
const context = extractDispatchContext(args);
|
|
113
|
+
const cleanArgs = context !== void 0 ? args.slice(0, -1) : args;
|
|
114
|
+
const computed = await cf.invoke(impl, cleanArgs);
|
|
115
|
+
if (context !== void 0) {
|
|
116
|
+
computed.onInvalidated.add(() => {
|
|
117
|
+
const msg = (0, import_rpc2.serializeMessage)({
|
|
118
|
+
Method: FUSION_INVALIDATE_METHOD,
|
|
119
|
+
RelatedId: context.callId
|
|
120
|
+
});
|
|
121
|
+
context.connection.send(msg);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return computed.value;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
_createClientMethod(peer, methodDef) {
|
|
128
|
+
if (methodDef.callTypeId !== FUSION_CALL_TYPE_ID)
|
|
129
|
+
return super._createClientMethod(peer, methodDef);
|
|
130
|
+
const wireName = (0, import_rpc2.wireMethodName)(methodDef);
|
|
131
|
+
const rpcImpl = async function(...args) {
|
|
132
|
+
const computeCtx = import_fusion.ComputeContext.from(import_core2.AsyncContext.current);
|
|
133
|
+
const callOptions = {
|
|
134
|
+
callTypeId: FUSION_CALL_TYPE_ID,
|
|
135
|
+
outboundCallFactory: (id, m) => new RpcOutboundComputeCall(id, m)
|
|
136
|
+
};
|
|
137
|
+
const outboundCall = peer.call(wireName, args, callOptions);
|
|
138
|
+
const value = await outboundCall.result.promise;
|
|
139
|
+
if (computeCtx) {
|
|
140
|
+
outboundCall.whenInvalidated.promise.then(() => computeCtx.computed.invalidate());
|
|
141
|
+
}
|
|
142
|
+
return value;
|
|
143
|
+
};
|
|
144
|
+
const cf = new import_fusion.ComputeFunction(methodDef.name, rpcImpl);
|
|
145
|
+
const syntheticInstance = {};
|
|
146
|
+
return (...args) => {
|
|
147
|
+
const last = args[args.length - 1];
|
|
148
|
+
const sliced = args.slice(0, methodDef.argCount);
|
|
149
|
+
const invokeArgs = last instanceof import_core2.AsyncContext ? [...sliced, last] : sliced;
|
|
150
|
+
return cf.invoke(syntheticInstance, invokeArgs).then((c) => c.value);
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
function extractDispatchContext(args) {
|
|
155
|
+
const last = args[args.length - 1];
|
|
156
|
+
if (last !== null && typeof last === "object" && "__rpcDispatch" in last) {
|
|
157
|
+
return last;
|
|
158
|
+
}
|
|
159
|
+
return void 0;
|
|
160
|
+
}
|
|
161
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
162
|
+
0 && (module.exports = {
|
|
163
|
+
FUSION_CALL_TYPE_ID,
|
|
164
|
+
FusionHub,
|
|
165
|
+
RpcOutboundComputeCall,
|
|
166
|
+
defineComputeService
|
|
167
|
+
});
|
|
168
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/fusion-hub.ts","../src/rpc-outbound-compute-call.ts"],"sourcesContent":["export { FusionHub, defineComputeService, FUSION_CALL_TYPE_ID } from \"./fusion-hub.js\";\r\nexport { RpcOutboundComputeCall } from \"./rpc-outbound-compute-call.js\";\r\n","// .NET counterparts:\r\n// RpcComputeServiceDef — extends RpcServiceDef with ComputedOptionsProvider.\r\n// RpcComputeMethodDef — extends RpcMethodDef with ComputedOptions and sets\r\n// CallType = RpcComputeCallType (custom call type with its own\r\n// RpcOutboundComputeCall / RpcInboundComputeCall factories).\r\n// RpcInboundComputeCallHandler — an IRpcMiddleware that wraps inbound compute\r\n// calls in a ComputeContext, captures the Computed<T> instance, and stores\r\n// it on RpcInboundComputeCall.Computed for invalidation tracking.\r\n// RemoteComputeServiceInterceptor — the client-side interceptor that creates\r\n// compute-aware outbound calls, tracking server invalidation signals.\r\n//\r\n// In .NET this is split across ~10 files using DI, middleware, interceptors,\r\n// and the RpcComputeCallType custom call type. TS consolidates it into a\r\n// single FusionHub subclass with two method overrides.\r\n//\r\n// Omitted from .NET:\r\n// - RpcComputeCallType / typed RpcInboundComputeCall<T> / RpcOutboundComputeCall<T> —\r\n// .NET uses a separate call type with custom factories, result handling, and\r\n// stage tracking. TS extends the base RpcOutboundComputeCall with a\r\n// whenInvalidated promise (sufficient for the TS use case).\r\n// - ComputedOptionsProvider / ComputedOptions per method — .NET resolves\r\n// per-method caching/invalidation options via DI. TS uses default\r\n// ComputeFunction behavior for all methods.\r\n// - RpcInboundComputeCallHandler middleware — .NET wraps the inbound call in\r\n// ComputeContext using the middleware pipeline. TS wraps it directly in\r\n// _wrapServerMethod by calling cf.invoke() (which sets up\r\n// ComputeContext internally).\r\n// - LocalExecutionMode / ConstrainedEntry — .NET controls whether compute calls\r\n// can execute locally or must go remote (for Fusion's consistency guarantees).\r\n// TS always executes locally (server-side) or remotely (client-side).\r\n// - Invalidation via RpcInboundComputeCall.Computed.Invalidated event — .NET\r\n// attaches an invalidation callback to the captured Computed<T> that calls\r\n// SystemCallSender.Complete(peer, call, invalidatedResult). TS uses\r\n// $sys-c.Invalidate system call instead (equivalent wire effect).\r\n// - RpcOutboundComputeCall.SetError → SetInvalidatedUnsafe — .NET invalidates\r\n// the local computed when an error or reroute occurs. TS resolves\r\n// whenInvalidated on disconnect via rejectAll().\r\n\r\nimport {\r\n RpcHub,\r\n RpcServerPeer,\r\n RpcWebSocketConnection,\r\n serializeMessage,\r\n defineRpcService,\r\n wireMethodName,\r\n type RpcConnection,\r\n type WebSocketLike,\r\n type RpcServiceDef,\r\n type RpcMethodDef,\r\n type RpcMethodDefInput,\r\n type RpcServiceImpl,\r\n type RpcDispatchContext,\r\n type RpcPeer,\r\n type RpcMessage,\r\n type RpcCallOptions,\r\n RpcOutboundCallTracker,\r\n RpcInboundCallTracker,\r\n getServiceMeta,\r\n getMethodsMeta,\r\n} from \"@actuallab/rpc\";\r\nimport {\r\n ComputeFunction,\r\n ComputeContext,\r\n type ComputeFunctionImpl,\r\n} from \"@actuallab/fusion\";\r\nimport { AsyncContext } from \"@actuallab/core\";\r\nimport { RpcOutboundComputeCall } from \"./rpc-outbound-compute-call.js\";\r\n\r\n/** Call type ID for Fusion compute calls — matches .NET's RpcComputeCallType.Id. */\r\nexport const FUSION_CALL_TYPE_ID = 1;\r\n\r\n/** Wire method name for invalidation system call. */\r\nconst FUSION_INVALIDATE_METHOD = \"$sys-c.Invalidate:0\";\r\n\r\n/** Creates a compute service definition — all methods default to FUSION_CALL_TYPE_ID. */\r\nexport function defineComputeService(\r\n name: string,\r\n methods: Record<string, RpcMethodDefInput>,\r\n): RpcServiceDef {\r\n const withCallType: Record<string, RpcMethodDefInput> = {};\r\n for (const [methodName, input] of Object.entries(methods)) {\r\n withCallType[methodName] = { ...input, callTypeId: input.callTypeId ?? FUSION_CALL_TYPE_ID };\r\n }\r\n return defineRpcService(name, withCallType, { ctOffset: 1 });\r\n}\r\n\r\n/** Central coordinator for Fusion + RPC — manages compute services, invalidation wiring. */\r\nexport class FusionHub extends RpcHub {\r\n /** Accept an incoming WebSocket and create a server peer. */\r\n acceptConnection(ws: WebSocketLike): RpcServerPeer {\r\n const ref = `server://${crypto.randomUUID()}`;\r\n const conn = new RpcWebSocketConnection(ws);\r\n const peer = this.getServerPeer(ref);\r\n peer.accept(conn);\r\n return peer;\r\n }\r\n\r\n /** Accept an RpcConnection and create a server peer. */\r\n acceptRpcConnection(conn: RpcConnection): RpcServerPeer {\r\n const ref = `server://${crypto.randomUUID()}`;\r\n const peer = this.getServerPeer(ref);\r\n peer.accept(conn);\r\n return peer;\r\n }\r\n\r\n /** Handle compute-specific system calls ($sys-c.Invalidate), then delegate to base. */\r\n override handleSystemCall(\r\n message: RpcMessage,\r\n args: unknown[],\r\n outbound: RpcOutboundCallTracker,\r\n inbound: RpcInboundCallTracker,\r\n ): void {\r\n const method = message.Method;\r\n const relatedId = message.RelatedId ?? 0;\r\n\r\n if (method === FUSION_INVALIDATE_METHOD) {\r\n // Server invalidated a compute call — remove from tracker and resolve\r\n const call = outbound.remove(relatedId);\r\n if (call instanceof RpcOutboundComputeCall) {\r\n call.whenInvalidated.resolve();\r\n }\r\n return;\r\n }\r\n\r\n super.handleSystemCall(message, args, outbound, inbound);\r\n }\r\n\r\n /** Override to apply ctOffset=1 default and FUSION_CALL_TYPE_ID for compute methods. */\r\n protected override _buildServiceDef(cls: abstract new (...args: any[]) => any): RpcServiceDef {\r\n const svcMeta = getServiceMeta(cls);\r\n if (svcMeta === undefined) throw new Error(\"Contract class missing @rpcService metadata\");\r\n\r\n const methodsMeta = getMethodsMeta(cls) ?? {};\r\n // FusionHub defaults ctOffset to 1 (CancellationToken convention)\r\n const ctOffset = svcMeta.ctOffset ?? 1;\r\n const methods = new Map<string, RpcMethodDef>();\r\n\r\n for (const [name, meta] of Object.entries(methodsMeta)) {\r\n const wireArgCount = meta.argCount + ctOffset;\r\n const mapKey = `${name}:${wireArgCount}`;\r\n methods.set(mapKey, {\r\n name,\r\n serviceName: svcMeta.name,\r\n argCount: meta.argCount,\r\n wireArgCount,\r\n callTypeId: (meta as any).compute === true ? FUSION_CALL_TYPE_ID : 0,\r\n stream: meta.stream ?? false,\r\n noWait: meta.noWait ?? false,\r\n });\r\n }\r\n\r\n return { name: svcMeta.name, methods };\r\n }\r\n\r\n protected override _wrapServerMethod(\r\n methodDef: RpcMethodDef,\r\n fn: (...args: unknown[]) => unknown, impl: object,\r\n ): (...args: unknown[]) => unknown {\r\n if (methodDef.callTypeId !== FUSION_CALL_TYPE_ID)\r\n return fn;\r\n\r\n // Route through ComputeFunction + wire invalidation → $sys-c.Invalidate\r\n const cf = new ComputeFunction(methodDef.name, fn as ComputeFunctionImpl);\r\n return async (...args: unknown[]) => {\r\n const context = extractDispatchContext(args);\r\n const cleanArgs = context !== undefined ? args.slice(0, -1) : args;\r\n\r\n const computed = await cf.invoke(impl, cleanArgs);\r\n\r\n // Wire invalidation → send $sys-c.Invalidate to the client\r\n if (context !== undefined) {\r\n computed.onInvalidated.add(() => {\r\n const msg = serializeMessage({\r\n Method: FUSION_INVALIDATE_METHOD,\r\n RelatedId: context.callId,\r\n });\r\n context.connection.send(msg);\r\n });\r\n }\r\n\r\n return computed.value;\r\n };\r\n }\r\n\r\n protected override _createClientMethod(\r\n peer: RpcPeer, methodDef: RpcMethodDef,\r\n ): (...args: unknown[]) => unknown {\r\n if (methodDef.callTypeId !== FUSION_CALL_TYPE_ID)\r\n return super._createClientMethod(peer, methodDef);\r\n\r\n const wireName = wireMethodName(methodDef);\r\n // Local ComputeFunction whose impl makes an RPC call\r\n const rpcImpl: ComputeFunctionImpl = async function(...args: unknown[]) {\r\n // Capture ComputeContext synchronously — AsyncContext.current is the child\r\n // context set by ComputeFunction.invoke() and won't survive the await below.\r\n const computeCtx = ComputeContext.from(AsyncContext.current);\r\n const callOptions: RpcCallOptions = {\r\n callTypeId: FUSION_CALL_TYPE_ID,\r\n outboundCallFactory: (id, m) => new RpcOutboundComputeCall(id, m),\r\n };\r\n const outboundCall = peer.call(wireName, args, callOptions) as RpcOutboundComputeCall;\r\n const value = await outboundCall.result.promise;\r\n // Wire server invalidation → local computed invalidation\r\n if (computeCtx) {\r\n outboundCall.whenInvalidated.promise.then(() => computeCtx.computed.invalidate());\r\n }\r\n return value;\r\n };\r\n const cf = new ComputeFunction(methodDef.name, rpcImpl);\r\n const syntheticInstance = {};\r\n return (...args: unknown[]) => {\r\n const last = args[args.length - 1];\r\n const sliced = args.slice(0, methodDef.argCount);\r\n const invokeArgs = last instanceof AsyncContext ? [...sliced, last] : sliced;\r\n return cf.invoke(syntheticInstance, invokeArgs).then(c => c.value);\r\n };\r\n }\r\n}\r\n\r\nfunction extractDispatchContext(args: unknown[]): RpcDispatchContext | undefined {\r\n const last = args[args.length - 1];\r\n if (last !== null && typeof last === \"object\" && \"__rpcDispatch\" in last) {\r\n return last as RpcDispatchContext;\r\n }\r\n return undefined;\r\n}\r\n","import { PromiseSource } from \"@actuallab/core\";\r\nimport { RpcOutboundCall } from \"@actuallab/rpc\";\r\n\r\n/** Tracks a pending outbound compute RPC call — stays in tracker until invalidated. */\r\nexport class RpcOutboundComputeCall extends RpcOutboundCall {\r\n override readonly removeOnOk = false;\r\n readonly whenInvalidated = new PromiseSource<void>();\r\n\r\n override onDisconnect(): void {\r\n this.whenInvalidated.resolve();\r\n }\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACsCA,IAAAA,cAqBO;AACP,oBAIO;AACP,IAAAC,eAA6B;;;ACjE7B,kBAA8B;AAC9B,iBAAgC;AAGzB,IAAM,yBAAN,cAAqC,2BAAgB;AAAA,EACxC,aAAa;AAAA,EACtB,kBAAkB,IAAI,0BAAoB;AAAA,EAE1C,eAAqB;AAC5B,SAAK,gBAAgB,QAAQ;AAAA,EAC/B;AACF;;;AD0DO,IAAM,sBAAsB;AAGnC,IAAM,2BAA2B;AAG1B,SAAS,qBACd,MACA,SACe;AACf,QAAM,eAAkD,CAAC;AACzD,aAAW,CAAC,YAAY,KAAK,KAAK,OAAO,QAAQ,OAAO,GAAG;AACzD,iBAAa,UAAU,IAAI,EAAE,GAAG,OAAO,YAAY,MAAM,cAAc,oBAAoB;AAAA,EAC7F;AACA,aAAO,8BAAiB,MAAM,cAAc,EAAE,UAAU,EAAE,CAAC;AAC7D;AAGO,IAAM,YAAN,cAAwB,mBAAO;AAAA;AAAA,EAEpC,iBAAiB,IAAkC;AACjD,UAAM,MAAM,YAAY,OAAO,WAAW,CAAC;AAC3C,UAAM,OAAO,IAAI,mCAAuB,EAAE;AAC1C,UAAM,OAAO,KAAK,cAAc,GAAG;AACnC,SAAK,OAAO,IAAI;AAChB,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,oBAAoB,MAAoC;AACtD,UAAM,MAAM,YAAY,OAAO,WAAW,CAAC;AAC3C,UAAM,OAAO,KAAK,cAAc,GAAG;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C/E,QAAM,OAAO,KAAK,KAAK,SAAS,CAAC;AACjC,MAAI,SAAS,QAAQ,OAAO,SAAS,YAAY,mBAAmB,MAAM;AACxE,WAAO;AAAA,EACT;AACA,SAAO;AACT;","names":["import_rpc","import_core"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RpcHub, WebSocketLike, RpcServerPeer, RpcConnection, RpcMessage, RpcOutboundCallTracker, RpcInboundCallTracker, RpcServiceDef, RpcMethodDef, RpcPeer, RpcMethodDefInput, RpcOutboundCall } from '@actuallab/rpc';
|
|
2
|
+
import { PromiseSource } from '@actuallab/core';
|
|
3
|
+
|
|
4
|
+
/** Call type ID for Fusion compute calls — matches .NET's RpcComputeCallType.Id. */
|
|
5
|
+
declare const FUSION_CALL_TYPE_ID = 1;
|
|
6
|
+
/** Creates a compute service definition — all methods default to FUSION_CALL_TYPE_ID. */
|
|
7
|
+
declare function defineComputeService(name: string, methods: Record<string, RpcMethodDefInput>): RpcServiceDef;
|
|
8
|
+
/** Central coordinator for Fusion + RPC — manages compute services, invalidation wiring. */
|
|
9
|
+
declare class FusionHub extends RpcHub {
|
|
10
|
+
/** Accept an incoming WebSocket and create a server peer. */
|
|
11
|
+
acceptConnection(ws: WebSocketLike): RpcServerPeer;
|
|
12
|
+
/** Accept an RpcConnection and create a server peer. */
|
|
13
|
+
acceptRpcConnection(conn: RpcConnection): RpcServerPeer;
|
|
14
|
+
/** Handle compute-specific system calls ($sys-c.Invalidate), then delegate to base. */
|
|
15
|
+
handleSystemCall(message: RpcMessage, args: unknown[], outbound: RpcOutboundCallTracker, inbound: RpcInboundCallTracker): void;
|
|
16
|
+
/** Override to apply ctOffset=1 default and FUSION_CALL_TYPE_ID for compute methods. */
|
|
17
|
+
protected _buildServiceDef(cls: abstract new (...args: any[]) => any): RpcServiceDef;
|
|
18
|
+
protected _wrapServerMethod(methodDef: RpcMethodDef, fn: (...args: unknown[]) => unknown, impl: object): (...args: unknown[]) => unknown;
|
|
19
|
+
protected _createClientMethod(peer: RpcPeer, methodDef: RpcMethodDef): (...args: unknown[]) => unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Tracks a pending outbound compute RPC call — stays in tracker until invalidated. */
|
|
23
|
+
declare class RpcOutboundComputeCall extends RpcOutboundCall {
|
|
24
|
+
readonly removeOnOk = false;
|
|
25
|
+
readonly whenInvalidated: PromiseSource<void>;
|
|
26
|
+
onDisconnect(): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { FUSION_CALL_TYPE_ID, FusionHub, RpcOutboundComputeCall, defineComputeService };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RpcHub, WebSocketLike, RpcServerPeer, RpcConnection, RpcMessage, RpcOutboundCallTracker, RpcInboundCallTracker, RpcServiceDef, RpcMethodDef, RpcPeer, RpcMethodDefInput, RpcOutboundCall } from '@actuallab/rpc';
|
|
2
|
+
import { PromiseSource } from '@actuallab/core';
|
|
3
|
+
|
|
4
|
+
/** Call type ID for Fusion compute calls — matches .NET's RpcComputeCallType.Id. */
|
|
5
|
+
declare const FUSION_CALL_TYPE_ID = 1;
|
|
6
|
+
/** Creates a compute service definition — all methods default to FUSION_CALL_TYPE_ID. */
|
|
7
|
+
declare function defineComputeService(name: string, methods: Record<string, RpcMethodDefInput>): RpcServiceDef;
|
|
8
|
+
/** Central coordinator for Fusion + RPC — manages compute services, invalidation wiring. */
|
|
9
|
+
declare class FusionHub extends RpcHub {
|
|
10
|
+
/** Accept an incoming WebSocket and create a server peer. */
|
|
11
|
+
acceptConnection(ws: WebSocketLike): RpcServerPeer;
|
|
12
|
+
/** Accept an RpcConnection and create a server peer. */
|
|
13
|
+
acceptRpcConnection(conn: RpcConnection): RpcServerPeer;
|
|
14
|
+
/** Handle compute-specific system calls ($sys-c.Invalidate), then delegate to base. */
|
|
15
|
+
handleSystemCall(message: RpcMessage, args: unknown[], outbound: RpcOutboundCallTracker, inbound: RpcInboundCallTracker): void;
|
|
16
|
+
/** Override to apply ctOffset=1 default and FUSION_CALL_TYPE_ID for compute methods. */
|
|
17
|
+
protected _buildServiceDef(cls: abstract new (...args: any[]) => any): RpcServiceDef;
|
|
18
|
+
protected _wrapServerMethod(methodDef: RpcMethodDef, fn: (...args: unknown[]) => unknown, impl: object): (...args: unknown[]) => unknown;
|
|
19
|
+
protected _createClientMethod(peer: RpcPeer, methodDef: RpcMethodDef): (...args: unknown[]) => unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Tracks a pending outbound compute RPC call — stays in tracker until invalidated. */
|
|
23
|
+
declare class RpcOutboundComputeCall extends RpcOutboundCall {
|
|
24
|
+
readonly removeOnOk = false;
|
|
25
|
+
readonly whenInvalidated: PromiseSource<void>;
|
|
26
|
+
onDisconnect(): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { FUSION_CALL_TYPE_ID, FusionHub, RpcOutboundComputeCall, defineComputeService };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// src/fusion-hub.ts
|
|
2
|
+
import {
|
|
3
|
+
RpcHub,
|
|
4
|
+
RpcWebSocketConnection,
|
|
5
|
+
serializeMessage,
|
|
6
|
+
defineRpcService,
|
|
7
|
+
wireMethodName,
|
|
8
|
+
getServiceMeta,
|
|
9
|
+
getMethodsMeta
|
|
10
|
+
} from "@actuallab/rpc";
|
|
11
|
+
import {
|
|
12
|
+
ComputeFunction,
|
|
13
|
+
ComputeContext
|
|
14
|
+
} from "@actuallab/fusion";
|
|
15
|
+
import { AsyncContext } from "@actuallab/core";
|
|
16
|
+
|
|
17
|
+
// src/rpc-outbound-compute-call.ts
|
|
18
|
+
import { PromiseSource } from "@actuallab/core";
|
|
19
|
+
import { RpcOutboundCall } from "@actuallab/rpc";
|
|
20
|
+
var RpcOutboundComputeCall = class extends RpcOutboundCall {
|
|
21
|
+
removeOnOk = false;
|
|
22
|
+
whenInvalidated = new PromiseSource();
|
|
23
|
+
onDisconnect() {
|
|
24
|
+
this.whenInvalidated.resolve();
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// src/fusion-hub.ts
|
|
29
|
+
var FUSION_CALL_TYPE_ID = 1;
|
|
30
|
+
var FUSION_INVALIDATE_METHOD = "$sys-c.Invalidate:0";
|
|
31
|
+
function defineComputeService(name, methods) {
|
|
32
|
+
const withCallType = {};
|
|
33
|
+
for (const [methodName, input] of Object.entries(methods)) {
|
|
34
|
+
withCallType[methodName] = { ...input, callTypeId: input.callTypeId ?? FUSION_CALL_TYPE_ID };
|
|
35
|
+
}
|
|
36
|
+
return defineRpcService(name, withCallType, { ctOffset: 1 });
|
|
37
|
+
}
|
|
38
|
+
var FusionHub = class extends RpcHub {
|
|
39
|
+
/** Accept an incoming WebSocket and create a server peer. */
|
|
40
|
+
acceptConnection(ws) {
|
|
41
|
+
const ref = `server://${crypto.randomUUID()}`;
|
|
42
|
+
const conn = new RpcWebSocketConnection(ws);
|
|
43
|
+
const peer = this.getServerPeer(ref);
|
|
44
|
+
peer.accept(conn);
|
|
45
|
+
return peer;
|
|
46
|
+
}
|
|
47
|
+
/** Accept an RpcConnection and create a server peer. */
|
|
48
|
+
acceptRpcConnection(conn) {
|
|
49
|
+
const ref = `server://${crypto.randomUUID()}`;
|
|
50
|
+
const peer = this.getServerPeer(ref);
|
|
51
|
+
peer.accept(conn);
|
|
52
|
+
return peer;
|
|
53
|
+
}
|
|
54
|
+
/** Handle compute-specific system calls ($sys-c.Invalidate), then delegate to base. */
|
|
55
|
+
handleSystemCall(message, args, outbound, inbound) {
|
|
56
|
+
const method = message.Method;
|
|
57
|
+
const relatedId = message.RelatedId ?? 0;
|
|
58
|
+
if (method === FUSION_INVALIDATE_METHOD) {
|
|
59
|
+
const call = outbound.remove(relatedId);
|
|
60
|
+
if (call instanceof RpcOutboundComputeCall) {
|
|
61
|
+
call.whenInvalidated.resolve();
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
super.handleSystemCall(message, args, outbound, inbound);
|
|
66
|
+
}
|
|
67
|
+
/** Override to apply ctOffset=1 default and FUSION_CALL_TYPE_ID for compute methods. */
|
|
68
|
+
_buildServiceDef(cls) {
|
|
69
|
+
const svcMeta = getServiceMeta(cls);
|
|
70
|
+
if (svcMeta === void 0) throw new Error("Contract class missing @rpcService metadata");
|
|
71
|
+
const methodsMeta = getMethodsMeta(cls) ?? {};
|
|
72
|
+
const ctOffset = svcMeta.ctOffset ?? 1;
|
|
73
|
+
const methods = /* @__PURE__ */ new Map();
|
|
74
|
+
for (const [name, meta] of Object.entries(methodsMeta)) {
|
|
75
|
+
const wireArgCount = meta.argCount + ctOffset;
|
|
76
|
+
const mapKey = `${name}:${wireArgCount}`;
|
|
77
|
+
methods.set(mapKey, {
|
|
78
|
+
name,
|
|
79
|
+
serviceName: svcMeta.name,
|
|
80
|
+
argCount: meta.argCount,
|
|
81
|
+
wireArgCount,
|
|
82
|
+
callTypeId: meta.compute === true ? FUSION_CALL_TYPE_ID : 0,
|
|
83
|
+
stream: meta.stream ?? false,
|
|
84
|
+
noWait: meta.noWait ?? false
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return { name: svcMeta.name, methods };
|
|
88
|
+
}
|
|
89
|
+
_wrapServerMethod(methodDef, fn, impl) {
|
|
90
|
+
if (methodDef.callTypeId !== FUSION_CALL_TYPE_ID)
|
|
91
|
+
return fn;
|
|
92
|
+
const cf = new ComputeFunction(methodDef.name, fn);
|
|
93
|
+
return async (...args) => {
|
|
94
|
+
const context = extractDispatchContext(args);
|
|
95
|
+
const cleanArgs = context !== void 0 ? args.slice(0, -1) : args;
|
|
96
|
+
const computed = await cf.invoke(impl, cleanArgs);
|
|
97
|
+
if (context !== void 0) {
|
|
98
|
+
computed.onInvalidated.add(() => {
|
|
99
|
+
const msg = serializeMessage({
|
|
100
|
+
Method: FUSION_INVALIDATE_METHOD,
|
|
101
|
+
RelatedId: context.callId
|
|
102
|
+
});
|
|
103
|
+
context.connection.send(msg);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return computed.value;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
_createClientMethod(peer, methodDef) {
|
|
110
|
+
if (methodDef.callTypeId !== FUSION_CALL_TYPE_ID)
|
|
111
|
+
return super._createClientMethod(peer, methodDef);
|
|
112
|
+
const wireName = wireMethodName(methodDef);
|
|
113
|
+
const rpcImpl = async function(...args) {
|
|
114
|
+
const computeCtx = ComputeContext.from(AsyncContext.current);
|
|
115
|
+
const callOptions = {
|
|
116
|
+
callTypeId: FUSION_CALL_TYPE_ID,
|
|
117
|
+
outboundCallFactory: (id, m) => new RpcOutboundComputeCall(id, m)
|
|
118
|
+
};
|
|
119
|
+
const outboundCall = peer.call(wireName, args, callOptions);
|
|
120
|
+
const value = await outboundCall.result.promise;
|
|
121
|
+
if (computeCtx) {
|
|
122
|
+
outboundCall.whenInvalidated.promise.then(() => computeCtx.computed.invalidate());
|
|
123
|
+
}
|
|
124
|
+
return value;
|
|
125
|
+
};
|
|
126
|
+
const cf = new ComputeFunction(methodDef.name, rpcImpl);
|
|
127
|
+
const syntheticInstance = {};
|
|
128
|
+
return (...args) => {
|
|
129
|
+
const last = args[args.length - 1];
|
|
130
|
+
const sliced = args.slice(0, methodDef.argCount);
|
|
131
|
+
const invokeArgs = last instanceof AsyncContext ? [...sliced, last] : sliced;
|
|
132
|
+
return cf.invoke(syntheticInstance, invokeArgs).then((c) => c.value);
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
function extractDispatchContext(args) {
|
|
137
|
+
const last = args[args.length - 1];
|
|
138
|
+
if (last !== null && typeof last === "object" && "__rpcDispatch" in last) {
|
|
139
|
+
return last;
|
|
140
|
+
}
|
|
141
|
+
return void 0;
|
|
142
|
+
}
|
|
143
|
+
export {
|
|
144
|
+
FUSION_CALL_TYPE_ID,
|
|
145
|
+
FusionHub,
|
|
146
|
+
RpcOutboundComputeCall,
|
|
147
|
+
defineComputeService
|
|
148
|
+
};
|
|
149
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/fusion-hub.ts","../src/rpc-outbound-compute-call.ts"],"sourcesContent":["// .NET counterparts:\r\n// RpcComputeServiceDef — extends RpcServiceDef with ComputedOptionsProvider.\r\n// RpcComputeMethodDef — extends RpcMethodDef with ComputedOptions and sets\r\n// CallType = RpcComputeCallType (custom call type with its own\r\n// RpcOutboundComputeCall / RpcInboundComputeCall factories).\r\n// RpcInboundComputeCallHandler — an IRpcMiddleware that wraps inbound compute\r\n// calls in a ComputeContext, captures the Computed<T> instance, and stores\r\n// it on RpcInboundComputeCall.Computed for invalidation tracking.\r\n// RemoteComputeServiceInterceptor — the client-side interceptor that creates\r\n// compute-aware outbound calls, tracking server invalidation signals.\r\n//\r\n// In .NET this is split across ~10 files using DI, middleware, interceptors,\r\n// and the RpcComputeCallType custom call type. TS consolidates it into a\r\n// single FusionHub subclass with two method overrides.\r\n//\r\n// Omitted from .NET:\r\n// - RpcComputeCallType / typed RpcInboundComputeCall<T> / RpcOutboundComputeCall<T> —\r\n// .NET uses a separate call type with custom factories, result handling, and\r\n// stage tracking. TS extends the base RpcOutboundComputeCall with a\r\n// whenInvalidated promise (sufficient for the TS use case).\r\n// - ComputedOptionsProvider / ComputedOptions per method — .NET resolves\r\n// per-method caching/invalidation options via DI. TS uses default\r\n// ComputeFunction behavior for all methods.\r\n// - RpcInboundComputeCallHandler middleware — .NET wraps the inbound call in\r\n// ComputeContext using the middleware pipeline. TS wraps it directly in\r\n// _wrapServerMethod by calling cf.invoke() (which sets up\r\n// ComputeContext internally).\r\n// - LocalExecutionMode / ConstrainedEntry — .NET controls whether compute calls\r\n// can execute locally or must go remote (for Fusion's consistency guarantees).\r\n// TS always executes locally (server-side) or remotely (client-side).\r\n// - Invalidation via RpcInboundComputeCall.Computed.Invalidated event — .NET\r\n// attaches an invalidation callback to the captured Computed<T> that calls\r\n// SystemCallSender.Complete(peer, call, invalidatedResult). TS uses\r\n// $sys-c.Invalidate system call instead (equivalent wire effect).\r\n// - RpcOutboundComputeCall.SetError → SetInvalidatedUnsafe — .NET invalidates\r\n// the local computed when an error or reroute occurs. TS resolves\r\n// whenInvalidated on disconnect via rejectAll().\r\n\r\nimport {\r\n RpcHub,\r\n RpcServerPeer,\r\n RpcWebSocketConnection,\r\n serializeMessage,\r\n defineRpcService,\r\n wireMethodName,\r\n type RpcConnection,\r\n type WebSocketLike,\r\n type RpcServiceDef,\r\n type RpcMethodDef,\r\n type RpcMethodDefInput,\r\n type RpcServiceImpl,\r\n type RpcDispatchContext,\r\n type RpcPeer,\r\n type RpcMessage,\r\n type RpcCallOptions,\r\n RpcOutboundCallTracker,\r\n RpcInboundCallTracker,\r\n getServiceMeta,\r\n getMethodsMeta,\r\n} from \"@actuallab/rpc\";\r\nimport {\r\n ComputeFunction,\r\n ComputeContext,\r\n type ComputeFunctionImpl,\r\n} from \"@actuallab/fusion\";\r\nimport { AsyncContext } from \"@actuallab/core\";\r\nimport { RpcOutboundComputeCall } from \"./rpc-outbound-compute-call.js\";\r\n\r\n/** Call type ID for Fusion compute calls — matches .NET's RpcComputeCallType.Id. */\r\nexport const FUSION_CALL_TYPE_ID = 1;\r\n\r\n/** Wire method name for invalidation system call. */\r\nconst FUSION_INVALIDATE_METHOD = \"$sys-c.Invalidate:0\";\r\n\r\n/** Creates a compute service definition — all methods default to FUSION_CALL_TYPE_ID. */\r\nexport function defineComputeService(\r\n name: string,\r\n methods: Record<string, RpcMethodDefInput>,\r\n): RpcServiceDef {\r\n const withCallType: Record<string, RpcMethodDefInput> = {};\r\n for (const [methodName, input] of Object.entries(methods)) {\r\n withCallType[methodName] = { ...input, callTypeId: input.callTypeId ?? FUSION_CALL_TYPE_ID };\r\n }\r\n return defineRpcService(name, withCallType, { ctOffset: 1 });\r\n}\r\n\r\n/** Central coordinator for Fusion + RPC — manages compute services, invalidation wiring. */\r\nexport class FusionHub extends RpcHub {\r\n /** Accept an incoming WebSocket and create a server peer. */\r\n acceptConnection(ws: WebSocketLike): RpcServerPeer {\r\n const ref = `server://${crypto.randomUUID()}`;\r\n const conn = new RpcWebSocketConnection(ws);\r\n const peer = this.getServerPeer(ref);\r\n peer.accept(conn);\r\n return peer;\r\n }\r\n\r\n /** Accept an RpcConnection and create a server peer. */\r\n acceptRpcConnection(conn: RpcConnection): RpcServerPeer {\r\n const ref = `server://${crypto.randomUUID()}`;\r\n const peer = this.getServerPeer(ref);\r\n peer.accept(conn);\r\n return peer;\r\n }\r\n\r\n /** Handle compute-specific system calls ($sys-c.Invalidate), then delegate to base. */\r\n override handleSystemCall(\r\n message: RpcMessage,\r\n args: unknown[],\r\n outbound: RpcOutboundCallTracker,\r\n inbound: RpcInboundCallTracker,\r\n ): void {\r\n const method = message.Method;\r\n const relatedId = message.RelatedId ?? 0;\r\n\r\n if (method === FUSION_INVALIDATE_METHOD) {\r\n // Server invalidated a compute call — remove from tracker and resolve\r\n const call = outbound.remove(relatedId);\r\n if (call instanceof RpcOutboundComputeCall) {\r\n call.whenInvalidated.resolve();\r\n }\r\n return;\r\n }\r\n\r\n super.handleSystemCall(message, args, outbound, inbound);\r\n }\r\n\r\n /** Override to apply ctOffset=1 default and FUSION_CALL_TYPE_ID for compute methods. */\r\n protected override _buildServiceDef(cls: abstract new (...args: any[]) => any): RpcServiceDef {\r\n const svcMeta = getServiceMeta(cls);\r\n if (svcMeta === undefined) throw new Error(\"Contract class missing @rpcService metadata\");\r\n\r\n const methodsMeta = getMethodsMeta(cls) ?? {};\r\n // FusionHub defaults ctOffset to 1 (CancellationToken convention)\r\n const ctOffset = svcMeta.ctOffset ?? 1;\r\n const methods = new Map<string, RpcMethodDef>();\r\n\r\n for (const [name, meta] of Object.entries(methodsMeta)) {\r\n const wireArgCount = meta.argCount + ctOffset;\r\n const mapKey = `${name}:${wireArgCount}`;\r\n methods.set(mapKey, {\r\n name,\r\n serviceName: svcMeta.name,\r\n argCount: meta.argCount,\r\n wireArgCount,\r\n callTypeId: (meta as any).compute === true ? FUSION_CALL_TYPE_ID : 0,\r\n stream: meta.stream ?? false,\r\n noWait: meta.noWait ?? false,\r\n });\r\n }\r\n\r\n return { name: svcMeta.name, methods };\r\n }\r\n\r\n protected override _wrapServerMethod(\r\n methodDef: RpcMethodDef,\r\n fn: (...args: unknown[]) => unknown, impl: object,\r\n ): (...args: unknown[]) => unknown {\r\n if (methodDef.callTypeId !== FUSION_CALL_TYPE_ID)\r\n return fn;\r\n\r\n // Route through ComputeFunction + wire invalidation → $sys-c.Invalidate\r\n const cf = new ComputeFunction(methodDef.name, fn as ComputeFunctionImpl);\r\n return async (...args: unknown[]) => {\r\n const context = extractDispatchContext(args);\r\n const cleanArgs = context !== undefined ? args.slice(0, -1) : args;\r\n\r\n const computed = await cf.invoke(impl, cleanArgs);\r\n\r\n // Wire invalidation → send $sys-c.Invalidate to the client\r\n if (context !== undefined) {\r\n computed.onInvalidated.add(() => {\r\n const msg = serializeMessage({\r\n Method: FUSION_INVALIDATE_METHOD,\r\n RelatedId: context.callId,\r\n });\r\n context.connection.send(msg);\r\n });\r\n }\r\n\r\n return computed.value;\r\n };\r\n }\r\n\r\n protected override _createClientMethod(\r\n peer: RpcPeer, methodDef: RpcMethodDef,\r\n ): (...args: unknown[]) => unknown {\r\n if (methodDef.callTypeId !== FUSION_CALL_TYPE_ID)\r\n return super._createClientMethod(peer, methodDef);\r\n\r\n const wireName = wireMethodName(methodDef);\r\n // Local ComputeFunction whose impl makes an RPC call\r\n const rpcImpl: ComputeFunctionImpl = async function(...args: unknown[]) {\r\n // Capture ComputeContext synchronously — AsyncContext.current is the child\r\n // context set by ComputeFunction.invoke() and won't survive the await below.\r\n const computeCtx = ComputeContext.from(AsyncContext.current);\r\n const callOptions: RpcCallOptions = {\r\n callTypeId: FUSION_CALL_TYPE_ID,\r\n outboundCallFactory: (id, m) => new RpcOutboundComputeCall(id, m),\r\n };\r\n const outboundCall = peer.call(wireName, args, callOptions) as RpcOutboundComputeCall;\r\n const value = await outboundCall.result.promise;\r\n // Wire server invalidation → local computed invalidation\r\n if (computeCtx) {\r\n outboundCall.whenInvalidated.promise.then(() => computeCtx.computed.invalidate());\r\n }\r\n return value;\r\n };\r\n const cf = new ComputeFunction(methodDef.name, rpcImpl);\r\n const syntheticInstance = {};\r\n return (...args: unknown[]) => {\r\n const last = args[args.length - 1];\r\n const sliced = args.slice(0, methodDef.argCount);\r\n const invokeArgs = last instanceof AsyncContext ? [...sliced, last] : sliced;\r\n return cf.invoke(syntheticInstance, invokeArgs).then(c => c.value);\r\n };\r\n }\r\n}\r\n\r\nfunction extractDispatchContext(args: unknown[]): RpcDispatchContext | undefined {\r\n const last = args[args.length - 1];\r\n if (last !== null && typeof last === \"object\" && \"__rpcDispatch\" in last) {\r\n return last as RpcDispatchContext;\r\n }\r\n return undefined;\r\n}\r\n","import { PromiseSource } from \"@actuallab/core\";\r\nimport { RpcOutboundCall } from \"@actuallab/rpc\";\r\n\r\n/** Tracks a pending outbound compute RPC call — stays in tracker until invalidated. */\r\nexport class RpcOutboundComputeCall extends RpcOutboundCall {\r\n override readonly removeOnOk = false;\r\n readonly whenInvalidated = new PromiseSource<void>();\r\n\r\n override onDisconnect(): void {\r\n this.whenInvalidated.resolve();\r\n }\r\n}\r\n"],"mappings":";AAsCA;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAaA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,oBAAoB;;;ACjE7B,SAAS,qBAAqB;AAC9B,SAAS,uBAAuB;AAGzB,IAAM,yBAAN,cAAqC,gBAAgB;AAAA,EACxC,aAAa;AAAA,EACtB,kBAAkB,IAAI,cAAoB;AAAA,EAE1C,eAAqB;AAC5B,SAAK,gBAAgB,QAAQ;AAAA,EAC/B;AACF;;;AD0DO,IAAM,s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IAAI,uBAAuB,IAAI,CAAC;AAAA,MAClE;AACA,YAAM,eAAe,KAAK,KAAK,UAAU,MAAM,WAAW;AAC1D,YAAM,QAAQ,MAAM,aAAa,OAAO;AAExC,UAAI,YAAY;AACd,qBAAa,gBAAgB,QAAQ,KAAK,MAAM,WAAW,SAAS,WAAW,CAAC;AAAA,MAClF;AACA,aAAO;AAAA,IACT;AACA,UAAM,KAAK,IAAI,gBAAgB,UAAU,MAAM,OAAO;AACtD,UAAM,oBAAoB,CAAC;AAC3B,WAAO,IAAI,SAAoB;AAC7B,YAAM,OAAO,KAAK,KAAK,SAAS,CAAC;AACjC,YAAM,SAAS,KAAK,MAAM,GAAG,UAAU,QAAQ;AAC/C,YAAM,aAAa,gBAAgB,eAAe,CAAC,GAAG,QAAQ,IAAI,IAAI;AACtE,aAAO,GAAG,OAAO,mBAAmB,UAAU,EAAE,KAAK,OAAK,EAAE,KAAK;AAAA,IACnE;AAAA,EACF;AACF;AAEA,SAAS,uBAAuB,MAAiD;AAC/E,QAAM,OAAO,KAAK,KAAK,SAAS,CAAC;AACjC,MAAI,SAAS,QAAQ,OAAO,SAAS,YAAY,mBAAmB,MAAM;AACxE,WAAO;AAAA,EACT;AACA,SAAO;AACT;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@actuallab/fusion-rpc",
|
|
3
|
+
"version": "12.1.62",
|
|
4
|
+
"description": "Bridge between Fusion and RPC - FusionHub, compute service registration",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup --config ../../tsup.config.ts src/index.ts",
|
|
26
|
+
"clean": "rm -rf dist"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@actuallab/core": "12.1.62",
|
|
30
|
+
"@actuallab/fusion": "12.1.62",
|
|
31
|
+
"@actuallab/rpc": "12.1.62"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT"
|
|
37
|
+
}
|