@syntropika/tembra-sdk 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +47 -2
- package/dist/authoring/declaration.d.ts +47 -0
- package/dist/authoring/hooks.d.ts +11 -0
- package/dist/authoring/orb.d.ts +3 -0
- package/dist/authoring/types.d.ts +166 -0
- package/dist/bridge/identifiers.d.ts +207 -0
- package/dist/bridge/kernel.d.ts +117 -0
- package/dist/bridge/protocol.d.ts +181 -0
- package/dist/browser.d.ts +21 -0
- package/dist/browser.js +3282 -0
- package/dist/codec.d.ts +1 -0
- package/dist/codecs/json.d.ts +13 -0
- package/dist/codecs/msgpack.d.ts +3 -0
- package/dist/context-key.d.ts +35 -0
- package/dist/coordinator.d.ts +1 -0
- package/dist/coordinator.js +543 -0
- package/dist/embedded.d.ts +1 -0
- package/dist/embedded.js +1 -0
- package/dist/error.d.ts +13 -0
- package/dist/errors.d.ts +122 -0
- package/dist/hooks.d.ts +1 -0
- package/dist/host.d.ts +96 -0
- package/dist/host.js +3225 -0
- package/dist/identifiers.d.ts +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +833 -0
- package/dist/infrastructure/coordinator.d.ts +13 -0
- package/dist/infrastructure/manager.d.ts +23 -0
- package/dist/infrastructure/rpc/channel.d.ts +52 -0
- package/dist/infrastructure/rpc/client.d.ts +20 -0
- package/dist/infrastructure/rpc/server.d.ts +10 -0
- package/dist/infrastructure/rpc/state.d.ts +16 -0
- package/dist/kernel.d.ts +1 -0
- package/dist/manager.d.ts +1 -0
- package/dist/manager.js +390 -0
- package/dist/msgpack.d.ts +1 -0
- package/dist/msgpack.js +635 -0
- package/dist/node.d.ts +14 -0
- package/dist/node.js +3283 -0
- package/dist/orb.d.ts +1 -0
- package/dist/protocol.d.ts +1 -0
- package/dist/rpc.d.ts +3 -0
- package/dist/rpc.js +1036 -0
- package/dist/runtime/activities.d.ts +30 -0
- package/dist/runtime/calls.d.ts +22 -0
- package/dist/runtime/context.d.ts +37 -0
- package/dist/runtime/declaration.d.ts +1 -0
- package/dist/runtime/lifecycle.d.ts +28 -0
- package/dist/runtime/orbs.d.ts +39 -0
- package/dist/runtime/programs.d.ts +31 -0
- package/dist/runtime/records.d.ts +107 -0
- package/dist/runtime/references.d.ts +38 -0
- package/dist/runtime/reload.d.ts +17 -0
- package/dist/runtime/resources.d.ts +24 -0
- package/dist/runtime/scopes.d.ts +35 -0
- package/dist/runtime/session.d.ts +69 -0
- package/dist/state/binding.d.ts +28 -0
- package/dist/state/selection.d.ts +21 -0
- package/dist/state/types.d.ts +35 -0
- package/dist/state.d.ts +2 -0
- package/dist/telemetry.d.ts +23 -0
- package/dist/telemetry.js +27 -0
- package/dist/tooling/build.d.ts +3 -0
- package/dist/tooling/index.d.ts +2 -0
- package/dist/tooling/index.js +181 -0
- package/dist/tooling/resolver.d.ts +5 -0
- package/dist/tooling/typecheck.d.ts +2 -0
- package/dist/tooling/types.d.ts +29 -0
- package/dist/types.d.ts +1 -0
- package/dist/values.d.ts +23 -0
- package/package.json +103 -4
- package/src/authoring/declaration.ts +220 -0
- package/src/authoring/hooks.ts +101 -0
- package/src/authoring/orb.ts +77 -0
- package/src/authoring/types.ts +212 -0
- package/src/bridge/identifiers.ts +212 -0
- package/src/bridge/kernel.ts +117 -0
- package/src/bridge/protocol.ts +193 -0
- package/src/browser.ts +59 -0
- package/src/codec.ts +1 -0
- package/src/codecs/json.ts +147 -0
- package/src/codecs/msgpack.ts +26 -0
- package/src/context-key.ts +94 -0
- package/src/coordinator.ts +1 -0
- package/src/embedded.ts +293 -0
- package/src/error.ts +50 -0
- package/src/errors.ts +237 -0
- package/src/hooks.ts +1 -0
- package/src/host.ts +142 -0
- package/src/identifiers.ts +1 -0
- package/src/index.ts +22 -0
- package/src/infrastructure/coordinator.ts +76 -0
- package/src/infrastructure/manager.ts +77 -0
- package/src/infrastructure/rpc/channel.ts +88 -0
- package/src/infrastructure/rpc/client.ts +161 -0
- package/src/infrastructure/rpc/server.ts +201 -0
- package/src/infrastructure/rpc/state.ts +70 -0
- package/src/kernel.ts +1 -0
- package/src/manager.ts +1 -0
- package/src/msgpack.ts +1 -0
- package/src/node.ts +53 -0
- package/src/orb.ts +1 -0
- package/src/protocol.ts +1 -0
- package/src/rpc.ts +3 -0
- package/src/runtime/activities.ts +197 -0
- package/src/runtime/calls.ts +165 -0
- package/src/runtime/context.ts +113 -0
- package/src/runtime/declaration.ts +1 -0
- package/src/runtime/lifecycle.ts +227 -0
- package/src/runtime/orbs.ts +292 -0
- package/src/runtime/programs.ts +274 -0
- package/src/runtime/records.ts +127 -0
- package/src/runtime/references.ts +398 -0
- package/src/runtime/reload.ts +161 -0
- package/src/runtime/resources.ts +157 -0
- package/src/runtime/scopes.ts +210 -0
- package/src/runtime/session.ts +181 -0
- package/src/state/binding.ts +157 -0
- package/src/state/selection.ts +122 -0
- package/src/state/types.ts +47 -0
- package/src/state.ts +2 -0
- package/src/telemetry.ts +45 -0
- package/src/tooling/build.ts +83 -0
- package/src/tooling/index.ts +8 -0
- package/src/tooling/resolver.ts +95 -0
- package/src/tooling/typecheck.ts +32 -0
- package/src/tooling/types.ts +32 -0
- package/src/types.ts +1 -0
- package/src/values.ts +85 -0
package/dist/rpc.js
ADDED
|
@@ -0,0 +1,1036 @@
|
|
|
1
|
+
// src/bridge/identifiers.ts
|
|
2
|
+
var Code = Object.freeze({
|
|
3
|
+
Invalid: "invalid",
|
|
4
|
+
Unavailable: "unavailable",
|
|
5
|
+
Released: "released",
|
|
6
|
+
Gone: "gone",
|
|
7
|
+
Expired: "expired",
|
|
8
|
+
Cancelled: "cancelled",
|
|
9
|
+
Forbidden: "forbidden",
|
|
10
|
+
Conflict: "conflict",
|
|
11
|
+
Unsupported: "unsupported",
|
|
12
|
+
Closed: "closed",
|
|
13
|
+
Exhausted: "exhausted"
|
|
14
|
+
});
|
|
15
|
+
var Reason = Object.freeze({
|
|
16
|
+
ActivationBusy: "activation_busy",
|
|
17
|
+
Address: "address",
|
|
18
|
+
AlreadyCommitted: "already_committed",
|
|
19
|
+
AmbiguousScope: "ambiguous_scope",
|
|
20
|
+
ApiVersion: "api_version",
|
|
21
|
+
Arguments: "arguments",
|
|
22
|
+
AsyncFactory: "async_factory",
|
|
23
|
+
AsyncState: "async_state",
|
|
24
|
+
Bigint: "bigint",
|
|
25
|
+
ByteResult: "byte_result",
|
|
26
|
+
Call: "call",
|
|
27
|
+
Callback: "callback",
|
|
28
|
+
CleanupBusy: "cleanup_busy",
|
|
29
|
+
Clock: "clock",
|
|
30
|
+
Codec: "codec",
|
|
31
|
+
Connection: "connection",
|
|
32
|
+
Context: "context",
|
|
33
|
+
Contribution: "contribution",
|
|
34
|
+
CoordinationLimits: "coordination_limits",
|
|
35
|
+
Deadline: "deadline",
|
|
36
|
+
Dependency: "dependency",
|
|
37
|
+
DependencyCycle: "dependency_cycle",
|
|
38
|
+
DependencyService: "dependency_service",
|
|
39
|
+
Disposing: "disposing",
|
|
40
|
+
Driver: "driver",
|
|
41
|
+
Duplicate: "duplicate",
|
|
42
|
+
DuplicateContribution: "duplicate_contribution",
|
|
43
|
+
DuplicateListener: "duplicate_listener",
|
|
44
|
+
DuplicateResource: "duplicate_resource",
|
|
45
|
+
DuplicateService: "duplicate_service",
|
|
46
|
+
EffectResults: "effect_results",
|
|
47
|
+
EffectsRunning: "effects_running",
|
|
48
|
+
EngineEffect: "engine_effect",
|
|
49
|
+
EventSource: "event_source",
|
|
50
|
+
ExclusiveDependent: "exclusive_dependent",
|
|
51
|
+
Executor: "executor",
|
|
52
|
+
Orb: "orb",
|
|
53
|
+
OrbGone: "orb_gone",
|
|
54
|
+
OrbInstalled: "orb_installed",
|
|
55
|
+
OrbScope: "orb_scope",
|
|
56
|
+
Factory: "factory",
|
|
57
|
+
FactoryExpired: "factory_expired",
|
|
58
|
+
FrameSize: "frame_size",
|
|
59
|
+
GateBusy: "gate_busy",
|
|
60
|
+
GateOrb: "gate_orb",
|
|
61
|
+
GateObject: "gate_object",
|
|
62
|
+
Hook: "hook",
|
|
63
|
+
HookDependency: "hook_dependency",
|
|
64
|
+
Hooks: "hooks",
|
|
65
|
+
HooksDisabled: "hooks_disabled",
|
|
66
|
+
Host: "host",
|
|
67
|
+
HostAddress: "host_address",
|
|
68
|
+
HostResource: "host_resource",
|
|
69
|
+
HostResourceReload: "host_resource_reload",
|
|
70
|
+
Identities: "identities",
|
|
71
|
+
Implementation: "implementation",
|
|
72
|
+
Invocation: "invocation",
|
|
73
|
+
Javascript: "javascript",
|
|
74
|
+
JsonAccessor: "json_accessor",
|
|
75
|
+
JsonCycle: "json_cycle",
|
|
76
|
+
JsonNumber: "json_number",
|
|
77
|
+
JsonSymbol: "json_symbol",
|
|
78
|
+
JsonTag: "json_tag",
|
|
79
|
+
JsonType: "json_type",
|
|
80
|
+
JsonValue: "json_value",
|
|
81
|
+
Lifetime: "lifetime",
|
|
82
|
+
Linger: "linger",
|
|
83
|
+
ListenerCallback: "listener_callback",
|
|
84
|
+
LiveConsumer: "live_consumer",
|
|
85
|
+
LiveReference: "live_reference",
|
|
86
|
+
LocalValue: "local_value",
|
|
87
|
+
Memory: "memory",
|
|
88
|
+
Metadata: "metadata",
|
|
89
|
+
MetadataSize: "metadata_size",
|
|
90
|
+
Method: "method",
|
|
91
|
+
MigrationGate: "migration_gate",
|
|
92
|
+
MigrationRequired: "migration_required",
|
|
93
|
+
MigrationValue: "migration_value",
|
|
94
|
+
MissingHook: "missing_hook",
|
|
95
|
+
MissingMethod: "missing_method",
|
|
96
|
+
NativeEffect: "native_effect",
|
|
97
|
+
NativeMethod: "native_method",
|
|
98
|
+
NativeObject: "native_object",
|
|
99
|
+
NativeService: "native_service",
|
|
100
|
+
Next: "next",
|
|
101
|
+
NextUsed: "next_used",
|
|
102
|
+
NotReady: "not_ready",
|
|
103
|
+
Object: "object",
|
|
104
|
+
ObjectState: "object_state",
|
|
105
|
+
ObservationSource: "observation_source",
|
|
106
|
+
ObservationAdmitted: "observation_admitted",
|
|
107
|
+
Opaque: "opaque",
|
|
108
|
+
Operation: "operation",
|
|
109
|
+
OperationOwner: "operation_owner",
|
|
110
|
+
Owner: "owner",
|
|
111
|
+
Payload: "payload",
|
|
112
|
+
PendingRequests: "pending_requests",
|
|
113
|
+
PrepareDeadline: "prepare_deadline",
|
|
114
|
+
Priority: "priority",
|
|
115
|
+
Program: "program",
|
|
116
|
+
ProgramPhase: "program_phase",
|
|
117
|
+
ProgramRunning: "program_running",
|
|
118
|
+
Reference: "reference",
|
|
119
|
+
ReloadAborted: "reload_aborted",
|
|
120
|
+
ReloadDeadline: "reload_deadline",
|
|
121
|
+
ReloadPreparing: "reload_preparing",
|
|
122
|
+
ReloadRunning: "reload_running",
|
|
123
|
+
ReloadTicket: "reload_ticket",
|
|
124
|
+
RemoteOpaque: "remote_opaque",
|
|
125
|
+
RemoteReferences: "remote_references",
|
|
126
|
+
Request: "request",
|
|
127
|
+
RequestIds: "request_ids",
|
|
128
|
+
Requires: "requires",
|
|
129
|
+
Resource: "resource",
|
|
130
|
+
ResourceDependencies: "resource_dependencies",
|
|
131
|
+
ResourceDependency: "resource_dependency",
|
|
132
|
+
ResourceHost: "resource_host",
|
|
133
|
+
ResourceInFactory: "resource_in_factory",
|
|
134
|
+
ResourceState: "resource_state",
|
|
135
|
+
ResourceVersion: "resource_version",
|
|
136
|
+
Result: "result",
|
|
137
|
+
RetainedVersions: "retained_versions",
|
|
138
|
+
RevisionExhausted: "revision_exhausted",
|
|
139
|
+
Root: "root",
|
|
140
|
+
Route: "route",
|
|
141
|
+
RpcFrame: "rpc_frame",
|
|
142
|
+
RpcOperation: "rpc_operation",
|
|
143
|
+
RpcRequest: "rpc_request",
|
|
144
|
+
RustType: "rust_type",
|
|
145
|
+
Scope: "scope",
|
|
146
|
+
ScopeClosed: "scope_closed",
|
|
147
|
+
ScopeOrb: "scope_orb",
|
|
148
|
+
ScopeOwner: "scope_owner",
|
|
149
|
+
ScopeResource: "scope_resource",
|
|
150
|
+
Serialization: "serialization",
|
|
151
|
+
Service: "service",
|
|
152
|
+
ServiceGone: "service_gone",
|
|
153
|
+
State: "state",
|
|
154
|
+
StateCycle: "state_cycle",
|
|
155
|
+
StateInitialized: "state_initialized",
|
|
156
|
+
StateOwner: "state_owner",
|
|
157
|
+
StateRevision: "state_revision",
|
|
158
|
+
StateValidation: "state_validation",
|
|
159
|
+
StateValue: "state_value",
|
|
160
|
+
StateVersion: "state_version",
|
|
161
|
+
Subscription: "subscription",
|
|
162
|
+
SubscriptionBusy: "subscription_busy",
|
|
163
|
+
SubscriptionIdle: "subscription_idle",
|
|
164
|
+
SubscriptionQueue: "subscription_queue",
|
|
165
|
+
SubscriptionSize: "subscription_size",
|
|
166
|
+
SubscriptionsDisabled: "subscriptions_disabled",
|
|
167
|
+
Superseded: "superseded",
|
|
168
|
+
Task: "task",
|
|
169
|
+
Telemetry: "telemetry",
|
|
170
|
+
UndeclaredDependency: "undeclared_dependency",
|
|
171
|
+
UndeclaredService: "undeclared_service",
|
|
172
|
+
UnknownService: "unknown_service",
|
|
173
|
+
UpgradeConflict: "upgrade_conflict",
|
|
174
|
+
Utf8: "utf8",
|
|
175
|
+
Value: "value",
|
|
176
|
+
ValueGone: "value_gone",
|
|
177
|
+
ValuesExhausted: "values_exhausted",
|
|
178
|
+
Views: "views"
|
|
179
|
+
});
|
|
180
|
+
var Lifetime = Object.freeze({
|
|
181
|
+
Referenced: "referenced",
|
|
182
|
+
Host: "host"
|
|
183
|
+
});
|
|
184
|
+
var Status = Object.freeze({
|
|
185
|
+
Loading: "loading",
|
|
186
|
+
Live: "live",
|
|
187
|
+
Released: "released",
|
|
188
|
+
Gone: "gone"
|
|
189
|
+
});
|
|
190
|
+
var ReloadStatus = Object.freeze({
|
|
191
|
+
Prepared: "prepared",
|
|
192
|
+
Committed: "committed",
|
|
193
|
+
Aborted: "aborted"
|
|
194
|
+
});
|
|
195
|
+
var HookCapability = Object.freeze({
|
|
196
|
+
Observe: "observe",
|
|
197
|
+
Transform: "transform",
|
|
198
|
+
Decide: "decide"
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// src/bridge/protocol.ts
|
|
202
|
+
var KernelOp = Object.freeze({
|
|
203
|
+
ContextDerive: "context-derive",
|
|
204
|
+
ContextValue: "context-value",
|
|
205
|
+
ContextStats: "context-stats",
|
|
206
|
+
Allocate: "allocate",
|
|
207
|
+
ScopeRegister: "scope-register",
|
|
208
|
+
ScopeExists: "scope-exists",
|
|
209
|
+
ScopesForOwner: "scopes-for-owner",
|
|
210
|
+
ScopesForOrb: "scopes-for-orb",
|
|
211
|
+
ScopeAddResource: "scope-add-resource",
|
|
212
|
+
ScopeActivate: "scope-activate",
|
|
213
|
+
ScopesCleanup: "scopes-cleanup",
|
|
214
|
+
ScopesReloadRegistered: "scopes-reload-registered",
|
|
215
|
+
ScopesReload: "scopes-reload",
|
|
216
|
+
ProgramRun: "program-run",
|
|
217
|
+
ProgramNext: "program-next",
|
|
218
|
+
ProgramCompleteStep: "program-complete-step",
|
|
219
|
+
ProgramCommit: "program-commit",
|
|
220
|
+
ProgramAbort: "program-abort",
|
|
221
|
+
ProgramForget: "program-forget",
|
|
222
|
+
Features: "features",
|
|
223
|
+
Prepare: "prepare",
|
|
224
|
+
Gate: "gate",
|
|
225
|
+
GateOrbs: "gate-orbs",
|
|
226
|
+
Commit: "commit",
|
|
227
|
+
Abort: "abort",
|
|
228
|
+
Orbs: "orbs",
|
|
229
|
+
Orb: "orb",
|
|
230
|
+
OrbNamed: "orb-named",
|
|
231
|
+
NextDeadline: "next-deadline",
|
|
232
|
+
Address: "address",
|
|
233
|
+
Acquire: "acquire",
|
|
234
|
+
Ready: "ready",
|
|
235
|
+
Dependency: "dependency",
|
|
236
|
+
Undepend: "undepend",
|
|
237
|
+
Fail: "fail",
|
|
238
|
+
Status: "status",
|
|
239
|
+
Release: "release",
|
|
240
|
+
Forget: "forget",
|
|
241
|
+
Root: "root",
|
|
242
|
+
CleanupRoot: "cleanup-root",
|
|
243
|
+
RootPrincipal: "root-principal",
|
|
244
|
+
CheckContext: "check-context",
|
|
245
|
+
CheckRoot: "check-root",
|
|
246
|
+
EndRoot: "end-root",
|
|
247
|
+
CancelRoot: "cancel-root",
|
|
248
|
+
BeginCall: "begin-call",
|
|
249
|
+
BeginOperation: "begin-operation",
|
|
250
|
+
BeginHookObservation: "begin-hook-observation",
|
|
251
|
+
FinishCall: "finish-call",
|
|
252
|
+
Invocation: "invocation",
|
|
253
|
+
StateInit: "state-init",
|
|
254
|
+
Snapshot: "snapshot",
|
|
255
|
+
StateAdopt: "state-adopt",
|
|
256
|
+
Task: "task",
|
|
257
|
+
TaskRoot: "task-root",
|
|
258
|
+
StopTask: "stop-task",
|
|
259
|
+
FinishTask: "finish-task",
|
|
260
|
+
Resource: "resource",
|
|
261
|
+
ResourceOpened: "resource-opened",
|
|
262
|
+
ResourceValue: "resource-value",
|
|
263
|
+
CloseResource: "close-resource",
|
|
264
|
+
ResourceClosed: "resource-closed",
|
|
265
|
+
Disposed: "disposed",
|
|
266
|
+
Tick: "tick",
|
|
267
|
+
NextWake: "next-wake",
|
|
268
|
+
NextIdle: "next-idle",
|
|
269
|
+
Close: "close",
|
|
270
|
+
FinishClose: "finish-close",
|
|
271
|
+
Diagnostics: "diagnostics",
|
|
272
|
+
Drain: "drain",
|
|
273
|
+
Restrict: "restrict",
|
|
274
|
+
Contribute: "contribute",
|
|
275
|
+
ContributionOrdinal: "contribution-ordinal",
|
|
276
|
+
HookPlan: "hook-plan",
|
|
277
|
+
Listen: "listen",
|
|
278
|
+
Subscribe: "subscribe",
|
|
279
|
+
Enqueue: "enqueue",
|
|
280
|
+
NextDelivery: "next-delivery",
|
|
281
|
+
FinishDelivery: "finish-delivery",
|
|
282
|
+
Unsubscribe: "unsubscribe"
|
|
283
|
+
});
|
|
284
|
+
var ActionType = Object.freeze({
|
|
285
|
+
Execute: "execute",
|
|
286
|
+
CancelEffects: "cancel-effects",
|
|
287
|
+
ProgramPrepared: "program-prepared",
|
|
288
|
+
ProgramComplete: "program-complete",
|
|
289
|
+
Dispose: "dispose",
|
|
290
|
+
CloseResource: "close-resource",
|
|
291
|
+
DropValue: "drop-value",
|
|
292
|
+
CancelRoot: "cancel-root",
|
|
293
|
+
CancelTask: "cancel-task",
|
|
294
|
+
RetireOrb: "retire-orb",
|
|
295
|
+
State: "state",
|
|
296
|
+
SubscriptionFailed: "subscription-failed",
|
|
297
|
+
DetachSubscription: "detach-subscription",
|
|
298
|
+
Deliver: "deliver"
|
|
299
|
+
});
|
|
300
|
+
var EffectType = Object.freeze({
|
|
301
|
+
Stop: "stop",
|
|
302
|
+
Open: "open",
|
|
303
|
+
Close: "close",
|
|
304
|
+
Restore: "restore",
|
|
305
|
+
Prepare: "prepare",
|
|
306
|
+
Validate: "validate",
|
|
307
|
+
Initialize: "initialize",
|
|
308
|
+
Migrate: "migrate",
|
|
309
|
+
Adopt: "adopt",
|
|
310
|
+
Start: "start",
|
|
311
|
+
Drop: "drop"
|
|
312
|
+
});
|
|
313
|
+
var StepType = Object.freeze({
|
|
314
|
+
Execute: "execute",
|
|
315
|
+
Wait: "wait",
|
|
316
|
+
Prepared: "prepared",
|
|
317
|
+
Complete: "complete",
|
|
318
|
+
Failed: "failed"
|
|
319
|
+
});
|
|
320
|
+
var NativeOp = Object.freeze({
|
|
321
|
+
Activate: "activate",
|
|
322
|
+
Dispose: "dispose",
|
|
323
|
+
CancelEffect: "cancel-effect",
|
|
324
|
+
Effect: "effect",
|
|
325
|
+
Invoke: "invoke",
|
|
326
|
+
Contribute: "contribute"
|
|
327
|
+
});
|
|
328
|
+
var RpcOp = Object.freeze({
|
|
329
|
+
Describe: "describe",
|
|
330
|
+
Get: "get",
|
|
331
|
+
Release: "release",
|
|
332
|
+
Call: "call",
|
|
333
|
+
Cancel: "cancel"
|
|
334
|
+
});
|
|
335
|
+
var RpcEvent = Object.freeze({
|
|
336
|
+
State: "state"
|
|
337
|
+
});
|
|
338
|
+
var OwnerKind = Object.freeze({
|
|
339
|
+
Host: "host",
|
|
340
|
+
Orb: "orb",
|
|
341
|
+
Object: "object"
|
|
342
|
+
});
|
|
343
|
+
var EnvironmentPhase = Object.freeze({
|
|
344
|
+
Declaring: "declaring",
|
|
345
|
+
Prepared: "prepared",
|
|
346
|
+
Live: "live",
|
|
347
|
+
Closed: "closed"
|
|
348
|
+
});
|
|
349
|
+
var CoordinationDecision = Object.freeze({
|
|
350
|
+
Preparing: "preparing",
|
|
351
|
+
Commit: "commit",
|
|
352
|
+
Abort: "abort"
|
|
353
|
+
});
|
|
354
|
+
var Outcome = Object.freeze({
|
|
355
|
+
Ok: "ok",
|
|
356
|
+
Error: "error"
|
|
357
|
+
});
|
|
358
|
+
var CodecTag = Object.freeze({
|
|
359
|
+
Bigint: "bigint",
|
|
360
|
+
Bytes: "bytes",
|
|
361
|
+
Object: "object"
|
|
362
|
+
});
|
|
363
|
+
var CodecId = Object.freeze({
|
|
364
|
+
Json: "tembra-json/1",
|
|
365
|
+
MessagePack: "tembra-msgpack/1"
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// src/errors.ts
|
|
369
|
+
function define(code, reason, message) {
|
|
370
|
+
return Object.freeze({ code, reason, message });
|
|
371
|
+
}
|
|
372
|
+
var Errors = Object.freeze({
|
|
373
|
+
Cancelled: Object.freeze({
|
|
374
|
+
EngineEffect: define(Code.Cancelled, Reason.EngineEffect, "The engine effect was cancelled"),
|
|
375
|
+
Request: define(Code.Cancelled, Reason.Request, "The request was cancelled"),
|
|
376
|
+
Resource: define(Code.Cancelled, Reason.Resource, "Resource opening was cancelled"),
|
|
377
|
+
Root: define(Code.Cancelled, Reason.Root, "The operation was cancelled"),
|
|
378
|
+
Task: define(Code.Cancelled, Reason.Task, "The task was cancelled")
|
|
379
|
+
}),
|
|
380
|
+
Closed: Object.freeze({
|
|
381
|
+
Host: define(Code.Closed, Reason.Host, "The host is closing")
|
|
382
|
+
}),
|
|
383
|
+
Conflict: Object.freeze({
|
|
384
|
+
AlreadyCommitted: define(Code.Conflict, Reason.AlreadyCommitted, "The replacement is already committed"),
|
|
385
|
+
Codec: define(Code.Conflict, Reason.Codec, "The Rust and JS host codecs must agree"),
|
|
386
|
+
PrepareDeadline: define(Code.Conflict, Reason.PrepareDeadline, "The preparation deadline expired"),
|
|
387
|
+
ReloadRunning: define(Code.Conflict, Reason.ReloadRunning, "A replacement is already running"),
|
|
388
|
+
ReloadTicket: define(Code.Conflict, Reason.ReloadTicket, "The replacement is no longer prepared")
|
|
389
|
+
}),
|
|
390
|
+
Exhausted: Object.freeze({
|
|
391
|
+
FrameSize: define(Code.Exhausted, Reason.FrameSize, "The frame exceeds the configured size limit"),
|
|
392
|
+
PendingRequests: define(Code.Exhausted, Reason.PendingRequests, "The pending request limit was reached"),
|
|
393
|
+
RemoteReferences: define(Code.Exhausted, Reason.RemoteReferences, "The remote reference limit was reached"),
|
|
394
|
+
RequestIds: define(Code.Exhausted, Reason.RequestIds, "Request identities are exhausted"),
|
|
395
|
+
SubscriptionQueue: define(Code.Exhausted, Reason.SubscriptionQueue, "The subscription queue is full")
|
|
396
|
+
}),
|
|
397
|
+
Expired: Object.freeze({
|
|
398
|
+
Next: define(Code.Expired, Reason.Next, "Continuation scope has ended")
|
|
399
|
+
}),
|
|
400
|
+
Forbidden: Object.freeze({
|
|
401
|
+
Context: define(Code.Forbidden, Reason.Context, "An issued context from this host is required"),
|
|
402
|
+
HostResource: define(Code.Forbidden, Reason.HostResource, "The host resource was not declared"),
|
|
403
|
+
Method: define(Code.Forbidden, Reason.Method, "The method is not allowed by this reference"),
|
|
404
|
+
ObservationSource: define(Code.Forbidden, Reason.ObservationSource, "Hook observation requires the original physical invocation"),
|
|
405
|
+
Opaque: define(Code.Forbidden, Reason.Opaque, "The resource is not available as an opaque local value"),
|
|
406
|
+
Service: define(Code.Forbidden, Reason.Service, "Access to this service is forbidden"),
|
|
407
|
+
StateOwner: define(Code.Forbidden, Reason.StateOwner, "Only the owning orb may change this state"),
|
|
408
|
+
UndeclaredDependency: define(Code.Forbidden, Reason.UndeclaredDependency, "The dependency was not declared"),
|
|
409
|
+
UndeclaredService: define(Code.Forbidden, Reason.UndeclaredService, "The service was not declared as a dependency")
|
|
410
|
+
}),
|
|
411
|
+
Gone: Object.freeze({
|
|
412
|
+
Connection: define(Code.Gone, Reason.Connection, "The connection is closed"),
|
|
413
|
+
Contribution: define(Code.Gone, Reason.Contribution, "The contribution is no longer available"),
|
|
414
|
+
Implementation: define(Code.Gone, Reason.Implementation, "The implementation is no longer available"),
|
|
415
|
+
ListenerCallback: define(Code.Gone, Reason.ListenerCallback, "The listener callback is no longer available"),
|
|
416
|
+
Reference: define(Code.Gone, Reason.Reference, "The reference is no longer available"),
|
|
417
|
+
Resource: define(Code.Gone, Reason.Resource, "The resource is no longer available"),
|
|
418
|
+
Scope: define(Code.Gone, Reason.Scope, "The declaration scope is no longer available"),
|
|
419
|
+
State: define(Code.Gone, Reason.State, "State owner is gone")
|
|
420
|
+
}),
|
|
421
|
+
Invalid: Object.freeze({
|
|
422
|
+
Address: define(Code.Invalid, Reason.Address, "Invalid address"),
|
|
423
|
+
AsyncFactory: define(Code.Invalid, Reason.AsyncFactory, "Factories must be synchronous"),
|
|
424
|
+
AsyncState: define(Code.Invalid, Reason.AsyncState, "State updates and migrations must be synchronous"),
|
|
425
|
+
CoordinationLimits: define(Code.Invalid, Reason.CoordinationLimits, "Invalid coordination limits"),
|
|
426
|
+
DependencyCycle: define(Code.Invalid, Reason.DependencyCycle, "Orb factories cannot acquire their own services"),
|
|
427
|
+
DependencyService: define(Code.Invalid, Reason.DependencyService, "Invalid dependency service"),
|
|
428
|
+
DuplicateListener: define(Code.Invalid, Reason.DuplicateListener, "Invalid duplicate listener"),
|
|
429
|
+
DuplicateResource: define(Code.Invalid, Reason.DuplicateResource, "Invalid duplicate resource"),
|
|
430
|
+
EventSource: define(Code.Invalid, Reason.EventSource, "Invalid event source"),
|
|
431
|
+
Orb: define(Code.Invalid, Reason.Orb, "An orb needs a name and positive API version"),
|
|
432
|
+
OrbGone: define(Code.Invalid, Reason.OrbGone, "Invalid orb gone"),
|
|
433
|
+
OrbInstalled: define(Code.Invalid, Reason.OrbInstalled, "Use reload to replace an installed orb"),
|
|
434
|
+
Factory: define(Code.Invalid, Reason.Factory, "Invalid factory"),
|
|
435
|
+
FactoryExpired: define(Code.Invalid, Reason.FactoryExpired, "Declarations must be synchronous"),
|
|
436
|
+
Hook: define(Code.Invalid, Reason.Hook, "Hooks need a run function"),
|
|
437
|
+
HookDependency: define(Code.Invalid, Reason.HookDependency, "Invalid hook dependency"),
|
|
438
|
+
HooksDisabled: define(Code.Invalid, Reason.HooksDisabled, "Invalid hooks disabled"),
|
|
439
|
+
JsonAccessor: define(Code.Invalid, Reason.JsonAccessor, "Invalid json accessor"),
|
|
440
|
+
JsonCycle: define(Code.Invalid, Reason.JsonCycle, "Invalid json cycle"),
|
|
441
|
+
JsonNumber: define(Code.Invalid, Reason.JsonNumber, "Use BigInt for integers outside the safe Number range"),
|
|
442
|
+
JsonSymbol: define(Code.Invalid, Reason.JsonSymbol, "Invalid json symbol"),
|
|
443
|
+
JsonTag: define(Code.Invalid, Reason.JsonTag, "Unknown or malformed portable type tag"),
|
|
444
|
+
JsonType: define(Code.Invalid, Reason.JsonType, "Supply a codec for custom object types"),
|
|
445
|
+
JsonValue: define(Code.Invalid, Reason.JsonValue, "This JSON codec requires explicit portable values"),
|
|
446
|
+
Method: define(Code.Invalid, Reason.Method, "Methods must be functions"),
|
|
447
|
+
NextUsed: define(Code.Invalid, Reason.NextUsed, "Invalid next used"),
|
|
448
|
+
Priority: define(Code.Invalid, Reason.Priority, "Invalid priority"),
|
|
449
|
+
Reference: define(Code.Invalid, Reason.Reference, "An issued local reference is required"),
|
|
450
|
+
Resource: define(Code.Invalid, Reason.Resource, "Invalid resource"),
|
|
451
|
+
ResourceHost: define(Code.Invalid, Reason.ResourceHost, "Resource dependencies must belong to the same host"),
|
|
452
|
+
ResourceInFactory: define(Code.Invalid, Reason.ResourceInFactory, "Resources open after declaration"),
|
|
453
|
+
RpcFrame: define(Code.Invalid, Reason.RpcFrame, "Invalid rpc frame"),
|
|
454
|
+
RpcOperation: define(Code.Invalid, Reason.RpcOperation, "Invalid rpc operation"),
|
|
455
|
+
RpcRequest: define(Code.Invalid, Reason.RpcRequest, "Invalid rpc request"),
|
|
456
|
+
Service: define(Code.Invalid, Reason.Service, "Invalid service"),
|
|
457
|
+
ServiceGone: define(Code.Invalid, Reason.ServiceGone, "Invalid service gone"),
|
|
458
|
+
State: define(Code.Invalid, Reason.State, "Service does not expose state"),
|
|
459
|
+
StateCycle: define(Code.Invalid, Reason.StateCycle, "State cannot contain cycles"),
|
|
460
|
+
StateValidation: define(Code.Invalid, Reason.StateValidation, "The orb rejected the state candidate"),
|
|
461
|
+
StateValue: define(Code.Invalid, Reason.StateValue, "State cannot contain accessors"),
|
|
462
|
+
StateVersion: define(Code.Invalid, Reason.StateVersion, "Invalid state version"),
|
|
463
|
+
SubscriptionsDisabled: define(Code.Invalid, Reason.SubscriptionsDisabled, "Invalid subscriptions disabled"),
|
|
464
|
+
SubscriptionSize: define(Code.Invalid, Reason.SubscriptionSize, "A local byte budget requires an explicit sizeOf function"),
|
|
465
|
+
Task: define(Code.Invalid, Reason.Task, "Invalid task"),
|
|
466
|
+
Utf8: define(Code.Invalid, Reason.Utf8, "Invalid utf8"),
|
|
467
|
+
ValueGone: define(Code.Invalid, Reason.ValueGone, "Invalid value gone"),
|
|
468
|
+
ValuesExhausted: define(Code.Invalid, Reason.ValuesExhausted, "Invalid values exhausted")
|
|
469
|
+
}),
|
|
470
|
+
Released: Object.freeze({
|
|
471
|
+
Reference: define(Code.Released, Reason.Reference, "The reference has been released")
|
|
472
|
+
}),
|
|
473
|
+
Unavailable: Object.freeze({
|
|
474
|
+
Callback: define(Code.Unavailable, Reason.Callback, "A callback failed"),
|
|
475
|
+
OrbScope: define(Code.Unavailable, Reason.OrbScope, "The orb scope is unavailable"),
|
|
476
|
+
NotReady: define(Code.Unavailable, Reason.NotReady, "The object is not ready"),
|
|
477
|
+
Route: define(Code.Unavailable, Reason.Route, "The route is unavailable")
|
|
478
|
+
}),
|
|
479
|
+
Unsupported: Object.freeze({
|
|
480
|
+
EngineEffect: define(Code.Unsupported, Reason.EngineEffect, "The engine effect is not supported by this adapter"),
|
|
481
|
+
RemoteOpaque: define(Code.Unsupported, Reason.RemoteOpaque, "Opaque resources are local to their language environment"),
|
|
482
|
+
Telemetry: define(Code.Unsupported, Reason.Telemetry, "Telemetry is disabled in this kernel"),
|
|
483
|
+
Views: define(Code.Unsupported, Reason.Views, "Reference views are disabled in this kernel")
|
|
484
|
+
})
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
// src/error.ts
|
|
488
|
+
var codes = new Set(Object.values(Code));
|
|
489
|
+
function isDescriptor(value) {
|
|
490
|
+
return typeof value === "object" && value !== null && "code" in value && codes.has(value.code) && "reason" in value && typeof value.reason === "string" && "message" in value && typeof value.message === "string";
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
class TembraError extends Error {
|
|
494
|
+
code;
|
|
495
|
+
reason;
|
|
496
|
+
constructor(error, message = error.message, options) {
|
|
497
|
+
super(message, options);
|
|
498
|
+
this.name = "TembraError";
|
|
499
|
+
this.code = error.code;
|
|
500
|
+
this.reason = error.reason;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
function failure(error) {
|
|
504
|
+
let message = Errors.Unavailable.Callback.message;
|
|
505
|
+
try {
|
|
506
|
+
if (error instanceof TembraError)
|
|
507
|
+
return error;
|
|
508
|
+
if (isDescriptor(error))
|
|
509
|
+
return new TembraError(error, error.message, { cause: error });
|
|
510
|
+
message = String(error instanceof Error ? error.message : error);
|
|
511
|
+
} catch {}
|
|
512
|
+
return new TembraError(Errors.Unavailable.Callback, message, { cause: error });
|
|
513
|
+
}
|
|
514
|
+
function refuse(error, message = error.message) {
|
|
515
|
+
throw new TembraError(error, message);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// src/codecs/json.ts
|
|
519
|
+
var marker = "$tembra";
|
|
520
|
+
function pack(value, visiting) {
|
|
521
|
+
if (typeof value === "bigint")
|
|
522
|
+
return { [marker]: [CodecTag.Bigint, value.toString()] };
|
|
523
|
+
if (value instanceof Uint8Array)
|
|
524
|
+
return { [marker]: [CodecTag.Bytes, [...value]] };
|
|
525
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
526
|
+
return value;
|
|
527
|
+
if (typeof value === "number") {
|
|
528
|
+
if (!Number.isFinite(value) || Number.isInteger(value) && !Number.isSafeInteger(value))
|
|
529
|
+
refuse(Errors.Invalid.JsonNumber, "Use BigInt for integers outside the safe Number range");
|
|
530
|
+
return value;
|
|
531
|
+
}
|
|
532
|
+
if (typeof value !== "object")
|
|
533
|
+
refuse(Errors.Invalid.JsonValue, "This JSON codec requires explicit portable values");
|
|
534
|
+
if (visiting.has(value))
|
|
535
|
+
refuse(Errors.Invalid.JsonCycle);
|
|
536
|
+
const prototype = Object.getPrototypeOf(value);
|
|
537
|
+
if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null)
|
|
538
|
+
refuse(Errors.Invalid.JsonType, "Supply a codec for custom object types");
|
|
539
|
+
visiting.add(value);
|
|
540
|
+
try {
|
|
541
|
+
if (Array.isArray(value))
|
|
542
|
+
return Array.from(value, (child) => pack(child, visiting));
|
|
543
|
+
const entries = Reflect.ownKeys(value).map((key) => {
|
|
544
|
+
if (typeof key !== "string")
|
|
545
|
+
refuse(Errors.Invalid.JsonSymbol);
|
|
546
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
547
|
+
if (!Object.hasOwn(descriptor, "value"))
|
|
548
|
+
refuse(Errors.Invalid.JsonAccessor);
|
|
549
|
+
return [key, pack(descriptor.value, visiting)];
|
|
550
|
+
});
|
|
551
|
+
return Object.hasOwn(value, marker) ? { [marker]: [CodecTag.Object, entries] } : Object.fromEntries(entries);
|
|
552
|
+
} finally {
|
|
553
|
+
visiting.delete(value);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
function unpack(value) {
|
|
557
|
+
if (value === null || typeof value !== "object") {
|
|
558
|
+
if (typeof value === "number" && Number.isInteger(value) && !Number.isSafeInteger(value))
|
|
559
|
+
refuse(Errors.Invalid.JsonNumber);
|
|
560
|
+
return value;
|
|
561
|
+
}
|
|
562
|
+
if (Array.isArray(value))
|
|
563
|
+
return value.map(unpack);
|
|
564
|
+
if (Object.hasOwn(value, marker)) {
|
|
565
|
+
const tag = value[marker];
|
|
566
|
+
if (Object.keys(value).length !== 1 || !Array.isArray(tag) || tag.length !== 2)
|
|
567
|
+
refuse(Errors.Invalid.JsonTag);
|
|
568
|
+
const [type, data] = tag;
|
|
569
|
+
if (type === CodecTag.Bigint && typeof data === "string" && /^-?(0|[1-9]\d*)$/.test(data))
|
|
570
|
+
return BigInt(data);
|
|
571
|
+
if (type === CodecTag.Bytes && Array.isArray(data) && data.every((v) => Number.isInteger(v) && v >= 0 && v <= 255))
|
|
572
|
+
return Uint8Array.from(data);
|
|
573
|
+
if (type === CodecTag.Object && Array.isArray(data)) {
|
|
574
|
+
if (!data.every((entry) => Array.isArray(entry) && entry.length === 2 && typeof entry[0] === "string"))
|
|
575
|
+
refuse(Errors.Invalid.JsonTag);
|
|
576
|
+
return Object.fromEntries(data.map(([key, child]) => [key, unpack(child)]));
|
|
577
|
+
}
|
|
578
|
+
refuse(Errors.Invalid.JsonTag, "Unknown or malformed portable type tag");
|
|
579
|
+
}
|
|
580
|
+
return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, unpack(child)]));
|
|
581
|
+
}
|
|
582
|
+
function utf8(text) {
|
|
583
|
+
const output = [];
|
|
584
|
+
for (const character of text) {
|
|
585
|
+
let point = character.codePointAt(0);
|
|
586
|
+
if (point >= 55296 && point <= 57343)
|
|
587
|
+
point = 65533;
|
|
588
|
+
if (point < 128)
|
|
589
|
+
output.push(point);
|
|
590
|
+
else if (point < 2048)
|
|
591
|
+
output.push(192 | point >> 6, 128 | point & 63);
|
|
592
|
+
else if (point < 65536)
|
|
593
|
+
output.push(224 | point >> 12, 128 | point >> 6 & 63, 128 | point & 63);
|
|
594
|
+
else
|
|
595
|
+
output.push(240 | point >> 18, 128 | point >> 12 & 63, 128 | point >> 6 & 63, 128 | point & 63);
|
|
596
|
+
}
|
|
597
|
+
return Uint8Array.from(output);
|
|
598
|
+
}
|
|
599
|
+
function text(bytes) {
|
|
600
|
+
const output = [];
|
|
601
|
+
for (let index = 0;index < bytes.length; ) {
|
|
602
|
+
const first = bytes[index++];
|
|
603
|
+
if (first < 128) {
|
|
604
|
+
output.push(String.fromCharCode(first));
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
const count = first >= 194 && first <= 223 ? 1 : first >= 224 && first <= 239 ? 2 : first >= 240 && first <= 244 ? 3 : -1;
|
|
608
|
+
if (count < 0 || index + count > bytes.length)
|
|
609
|
+
refuse(Errors.Invalid.Utf8);
|
|
610
|
+
let point = first & 127 >> count + 1;
|
|
611
|
+
for (let offset = 0;offset < count; offset++) {
|
|
612
|
+
const next = bytes[index++];
|
|
613
|
+
if ((next & 192) !== 128)
|
|
614
|
+
refuse(Errors.Invalid.Utf8);
|
|
615
|
+
point = point << 6 | next & 63;
|
|
616
|
+
}
|
|
617
|
+
if (point < [0, 128, 2048, 65536][count] || point > 1114111 || point >= 55296 && point <= 57343)
|
|
618
|
+
refuse(Errors.Invalid.Utf8);
|
|
619
|
+
output.push(String.fromCodePoint(point));
|
|
620
|
+
}
|
|
621
|
+
return output.join("");
|
|
622
|
+
}
|
|
623
|
+
var jsonCodec = Object.freeze({
|
|
624
|
+
id: CodecId.Json,
|
|
625
|
+
serialize: (value) => utf8(JSON.stringify(pack(value, new WeakSet))),
|
|
626
|
+
deserialize: (bytes) => unpack(JSON.parse(text(bytes)))
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
// src/infrastructure/rpc/channel.ts
|
|
630
|
+
function errorValue(error) {
|
|
631
|
+
const value = failure(error);
|
|
632
|
+
return { code: value.code, reason: value.reason, message: value.message };
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
class Channel {
|
|
636
|
+
transport;
|
|
637
|
+
options;
|
|
638
|
+
ended;
|
|
639
|
+
codec;
|
|
640
|
+
maxFrame;
|
|
641
|
+
maxPending;
|
|
642
|
+
closed = false;
|
|
643
|
+
#detach;
|
|
644
|
+
constructor(transport, options, receive, ended) {
|
|
645
|
+
this.transport = transport;
|
|
646
|
+
this.options = options;
|
|
647
|
+
this.ended = ended;
|
|
648
|
+
this.codec = options.codec ?? jsonCodec;
|
|
649
|
+
this.maxFrame = options.maxFrame ?? 1024 * 1024;
|
|
650
|
+
this.maxPending = options.maxPending ?? 128;
|
|
651
|
+
this.#detach = transport.listen((frame) => {
|
|
652
|
+
if (this.closed)
|
|
653
|
+
return;
|
|
654
|
+
try {
|
|
655
|
+
if (frame.length > this.maxFrame)
|
|
656
|
+
throw new TembraError(Errors.Exhausted.FrameSize);
|
|
657
|
+
const message = this.codec.deserialize(frame);
|
|
658
|
+
if (!message || typeof message !== "object" || Array.isArray(message))
|
|
659
|
+
throw new TembraError(Errors.Invalid.RpcFrame);
|
|
660
|
+
receive(message);
|
|
661
|
+
} catch (error) {
|
|
662
|
+
options.onError?.(error);
|
|
663
|
+
this.close(error);
|
|
664
|
+
}
|
|
665
|
+
}, (error) => this.close(error));
|
|
666
|
+
}
|
|
667
|
+
async send(message) {
|
|
668
|
+
if (this.closed)
|
|
669
|
+
throw new TembraError(Errors.Gone.Connection);
|
|
670
|
+
const frame = this.codec.serialize(message);
|
|
671
|
+
if (frame.length > this.maxFrame)
|
|
672
|
+
throw new TembraError(Errors.Exhausted.FrameSize);
|
|
673
|
+
await this.transport.send(frame);
|
|
674
|
+
}
|
|
675
|
+
close(error = new TembraError(Errors.Gone.Connection)) {
|
|
676
|
+
if (this.closed)
|
|
677
|
+
return;
|
|
678
|
+
this.closed = true;
|
|
679
|
+
this.#detach?.();
|
|
680
|
+
this.transport.close();
|
|
681
|
+
this.ended(error);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// src/values.ts
|
|
686
|
+
var protectedValues = new WeakSet;
|
|
687
|
+
function protect(value) {
|
|
688
|
+
const visiting = new WeakSet;
|
|
689
|
+
function visit(value) {
|
|
690
|
+
if (value === null || typeof value !== "object") {
|
|
691
|
+
if (["function", "symbol"].includes(typeof value))
|
|
692
|
+
refuse(Errors.Invalid.StateValue, "State must contain plain data");
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
if (protectedValues.has(value))
|
|
696
|
+
return;
|
|
697
|
+
if (visiting.has(value))
|
|
698
|
+
refuse(Errors.Invalid.StateCycle, "State cannot contain cycles");
|
|
699
|
+
const proto = Object.getPrototypeOf(value);
|
|
700
|
+
if (!Array.isArray(value) && proto !== Object.prototype && proto !== null)
|
|
701
|
+
refuse(Errors.Invalid.StateValue, "State requires plain objects and arrays");
|
|
702
|
+
visiting.add(value);
|
|
703
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
704
|
+
if (typeof key !== "string")
|
|
705
|
+
refuse(Errors.Invalid.StateValue, "State cannot contain symbol properties");
|
|
706
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
707
|
+
if (!Object.hasOwn(descriptor, "value"))
|
|
708
|
+
refuse(Errors.Invalid.StateValue, "State cannot contain accessors");
|
|
709
|
+
visit(descriptor.value);
|
|
710
|
+
}
|
|
711
|
+
visiting.delete(value);
|
|
712
|
+
Object.freeze(value);
|
|
713
|
+
protectedValues.add(value);
|
|
714
|
+
}
|
|
715
|
+
visit(value);
|
|
716
|
+
return value;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// src/infrastructure/rpc/state.ts
|
|
720
|
+
class RemoteState {
|
|
721
|
+
check;
|
|
722
|
+
report;
|
|
723
|
+
#value;
|
|
724
|
+
#listeners = new Set;
|
|
725
|
+
constructor(value, check, report) {
|
|
726
|
+
this.check = check;
|
|
727
|
+
this.report = report;
|
|
728
|
+
this.#value = protect(value);
|
|
729
|
+
}
|
|
730
|
+
get value() {
|
|
731
|
+
this.check();
|
|
732
|
+
return this.#value;
|
|
733
|
+
}
|
|
734
|
+
adopt(value) {
|
|
735
|
+
this.#value = protect(value);
|
|
736
|
+
for (const listener of this.#listeners) {
|
|
737
|
+
try {
|
|
738
|
+
listener(this.#value);
|
|
739
|
+
} catch (error) {
|
|
740
|
+
this.report(error);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
subscribe(listener) {
|
|
745
|
+
this.check();
|
|
746
|
+
this.#listeners.add(listener);
|
|
747
|
+
listener(this.#value);
|
|
748
|
+
return () => {
|
|
749
|
+
this.#listeners.delete(listener);
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
select(compute) {
|
|
753
|
+
const source = this;
|
|
754
|
+
return {
|
|
755
|
+
get value() {
|
|
756
|
+
return compute(source.value);
|
|
757
|
+
},
|
|
758
|
+
select: (next) => source.select((value) => next(compute(value))),
|
|
759
|
+
subscribe(listener) {
|
|
760
|
+
let initial = true;
|
|
761
|
+
let previous;
|
|
762
|
+
return source.subscribe((value) => {
|
|
763
|
+
const next = compute(value);
|
|
764
|
+
if (initial || !Object.is(previous, next)) {
|
|
765
|
+
initial = false;
|
|
766
|
+
previous = next;
|
|
767
|
+
listener(next);
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// src/infrastructure/rpc/client.ts
|
|
776
|
+
class RemoteHost {
|
|
777
|
+
channel;
|
|
778
|
+
#next = 0;
|
|
779
|
+
#pending = new Map;
|
|
780
|
+
#states = new Map;
|
|
781
|
+
constructor(transport, options = {}) {
|
|
782
|
+
this.channel = new Channel(transport, options, (message) => {
|
|
783
|
+
if (message.event === RpcEvent.State) {
|
|
784
|
+
this.#states.get(message.payload.reference)?.adopt(message.payload.value);
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
const pending = message.id && this.#pending.get(message.id);
|
|
788
|
+
if (!pending)
|
|
789
|
+
return;
|
|
790
|
+
this.#pending.delete(message.id);
|
|
791
|
+
if (message.error)
|
|
792
|
+
pending.reject(new TembraError(message.error));
|
|
793
|
+
else
|
|
794
|
+
pending.resolve(message.ok);
|
|
795
|
+
}, (error) => {
|
|
796
|
+
for (const pending of this.#pending.values())
|
|
797
|
+
pending.reject(error);
|
|
798
|
+
this.#pending.clear();
|
|
799
|
+
this.#states.clear();
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
async request(op, payload, signal) {
|
|
803
|
+
signal?.throwIfAborted();
|
|
804
|
+
if (this.#pending.size >= this.channel.maxPending)
|
|
805
|
+
throw new TembraError(Errors.Exhausted.PendingRequests);
|
|
806
|
+
const id = ++this.#next;
|
|
807
|
+
if (!Number.isSafeInteger(id))
|
|
808
|
+
throw new TembraError(Errors.Exhausted.RequestIds);
|
|
809
|
+
let abort;
|
|
810
|
+
const result = new Promise((resolve, reject) => {
|
|
811
|
+
this.#pending.set(id, { resolve, reject });
|
|
812
|
+
abort = () => {
|
|
813
|
+
this.#pending.delete(id);
|
|
814
|
+
reject(signal?.reason ?? new TembraError(Errors.Cancelled.Request));
|
|
815
|
+
this.channel.send({ op: RpcOp.Cancel, payload: { id } }).catch(() => {});
|
|
816
|
+
};
|
|
817
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
818
|
+
this.channel.send({ id, op, payload }).catch((error) => {
|
|
819
|
+
this.#pending.delete(id);
|
|
820
|
+
reject(error);
|
|
821
|
+
});
|
|
822
|
+
});
|
|
823
|
+
try {
|
|
824
|
+
return await result;
|
|
825
|
+
} finally {
|
|
826
|
+
if (abort)
|
|
827
|
+
signal?.removeEventListener("abort", abort);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
installedOrbs() {
|
|
831
|
+
return this.request(RpcOp.Describe, null);
|
|
832
|
+
}
|
|
833
|
+
async get(service, key, options = {}) {
|
|
834
|
+
const acquired = await this.request(RpcOp.Get, { service, key: typeof key === "string" ? [key] : key }, options.signal);
|
|
835
|
+
const id = acquired.reference;
|
|
836
|
+
let released = false;
|
|
837
|
+
const check = () => {
|
|
838
|
+
if (released)
|
|
839
|
+
throw new TembraError(Errors.Released.Reference);
|
|
840
|
+
if (this.channel.closed)
|
|
841
|
+
throw new TembraError(Errors.Gone.Connection);
|
|
842
|
+
};
|
|
843
|
+
const state = Object.hasOwn(acquired, "state") ? new RemoteState(acquired.state, check, (error) => this.channel.options.onError?.(error)) : undefined;
|
|
844
|
+
if (state)
|
|
845
|
+
this.#states.set(id, state);
|
|
846
|
+
const target = {
|
|
847
|
+
ready: Promise.resolve(),
|
|
848
|
+
get status() {
|
|
849
|
+
return released ? Status.Released : peer.channel.closed ? Status.Gone : Status.Live;
|
|
850
|
+
},
|
|
851
|
+
get state() {
|
|
852
|
+
check();
|
|
853
|
+
if (!state)
|
|
854
|
+
throw new TembraError(Errors.Invalid.State);
|
|
855
|
+
return state;
|
|
856
|
+
},
|
|
857
|
+
release: () => {
|
|
858
|
+
if (released)
|
|
859
|
+
return;
|
|
860
|
+
released = true;
|
|
861
|
+
this.#states.delete(id);
|
|
862
|
+
this.request(RpcOp.Release, { reference: id }).catch((error) => this.channel.options.onError?.(error));
|
|
863
|
+
},
|
|
864
|
+
opaque() {
|
|
865
|
+
throw new TembraError(Errors.Unsupported.RemoteOpaque, "Opaque resources are local to their language environment");
|
|
866
|
+
}
|
|
867
|
+
};
|
|
868
|
+
const peer = this;
|
|
869
|
+
return new Proxy(target, {
|
|
870
|
+
get: (target, property, receiver) => {
|
|
871
|
+
if (property === "then")
|
|
872
|
+
return;
|
|
873
|
+
if (Object.hasOwn(target, property))
|
|
874
|
+
return Reflect.get(target, property, receiver);
|
|
875
|
+
if (typeof property !== "string" || !acquired.methods.includes(property))
|
|
876
|
+
return;
|
|
877
|
+
return async (...arguments_) => {
|
|
878
|
+
check();
|
|
879
|
+
const last = arguments_.at(-1);
|
|
880
|
+
const signal = last && typeof last.check === "function" && "signal" in last ? arguments_.pop().signal : undefined;
|
|
881
|
+
return this.request(RpcOp.Call, { reference: id, method: property, arguments: arguments_ }, signal);
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
close() {
|
|
887
|
+
this.channel.close();
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
// src/infrastructure/rpc/server.ts
|
|
891
|
+
function serve(host, transport, options) {
|
|
892
|
+
const references = new Map;
|
|
893
|
+
const calls = new Map;
|
|
894
|
+
const requests = new Map;
|
|
895
|
+
let next = 0;
|
|
896
|
+
let pending = 0;
|
|
897
|
+
const pump = (reference, record) => {
|
|
898
|
+
if (record.sending || !record.active || record.closed)
|
|
899
|
+
return;
|
|
900
|
+
record.sending = true;
|
|
901
|
+
(async () => {
|
|
902
|
+
while (Object.hasOwn(record, "pending") && !record.closed) {
|
|
903
|
+
const value = record.pending;
|
|
904
|
+
delete record.pending;
|
|
905
|
+
await channel.send({ event: RpcEvent.State, payload: { reference, value } });
|
|
906
|
+
}
|
|
907
|
+
})().catch((error) => channel.close(error)).finally(() => {
|
|
908
|
+
record.sending = false;
|
|
909
|
+
});
|
|
910
|
+
};
|
|
911
|
+
const channel = new Channel(transport, options, (message) => {
|
|
912
|
+
if (message.op === RpcOp.Cancel) {
|
|
913
|
+
const request = requests.get(message.payload?.id);
|
|
914
|
+
if (request)
|
|
915
|
+
request.cancelled = true;
|
|
916
|
+
calls.get(message.payload?.id)?.cancel();
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
const id = message.id;
|
|
920
|
+
if (!Number.isSafeInteger(id) || !id || id <= 0 || !message.op) {
|
|
921
|
+
channel.close(new TembraError(Errors.Invalid.RpcRequest));
|
|
922
|
+
return;
|
|
923
|
+
}
|
|
924
|
+
if (pending >= channel.maxPending || requests.has(id)) {
|
|
925
|
+
channel.send({ id, error: errorValue(new TembraError(Errors.Exhausted.PendingRequests)) }).catch(() => channel.close());
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
pending++;
|
|
929
|
+
const request = { cancelled: false };
|
|
930
|
+
requests.set(id, request);
|
|
931
|
+
(async () => {
|
|
932
|
+
const payload = message.payload;
|
|
933
|
+
switch (message.op) {
|
|
934
|
+
case RpcOp.Describe:
|
|
935
|
+
return host.installedOrbs().map(({ describe, ...info }) => info);
|
|
936
|
+
case RpcOp.Get: {
|
|
937
|
+
if (typeof payload?.service !== "string" || !Array.isArray(payload.key) || !payload.key.every((key) => typeof key === "string"))
|
|
938
|
+
throw new TembraError(Errors.Invalid.Address);
|
|
939
|
+
if (options.allow && !options.allow(payload.service, payload.key))
|
|
940
|
+
throw new TembraError(Errors.Forbidden.Service);
|
|
941
|
+
if (references.size >= channel.maxPending)
|
|
942
|
+
throw new TembraError(Errors.Exhausted.RemoteReferences);
|
|
943
|
+
const reference = await host.get(payload.service, payload.key, {
|
|
944
|
+
principal: options.principal
|
|
945
|
+
});
|
|
946
|
+
if (channel.closed || request.cancelled) {
|
|
947
|
+
reference.release();
|
|
948
|
+
throw new TembraError(Errors.Gone.Connection);
|
|
949
|
+
}
|
|
950
|
+
const referenceId = ++next;
|
|
951
|
+
const methods = new Set(host.session.handles.reference(reference).methods);
|
|
952
|
+
const record = {
|
|
953
|
+
reference,
|
|
954
|
+
methods,
|
|
955
|
+
sending: false,
|
|
956
|
+
closed: false,
|
|
957
|
+
active: false
|
|
958
|
+
};
|
|
959
|
+
references.set(referenceId, record);
|
|
960
|
+
let snapshot = {};
|
|
961
|
+
try {
|
|
962
|
+
snapshot = { state: reference.state.value };
|
|
963
|
+
let initial = true;
|
|
964
|
+
record.detach = reference.state.subscribe((value) => {
|
|
965
|
+
if (initial) {
|
|
966
|
+
initial = false;
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
record.pending = value;
|
|
970
|
+
pump(referenceId, record);
|
|
971
|
+
});
|
|
972
|
+
} catch {}
|
|
973
|
+
return { reference: referenceId, methods: [...methods], ...snapshot };
|
|
974
|
+
}
|
|
975
|
+
case RpcOp.Release: {
|
|
976
|
+
const record = references.get(payload?.reference);
|
|
977
|
+
if (record) {
|
|
978
|
+
record.closed = true;
|
|
979
|
+
record.detach?.();
|
|
980
|
+
record.reference.release();
|
|
981
|
+
references.delete(payload.reference);
|
|
982
|
+
}
|
|
983
|
+
return null;
|
|
984
|
+
}
|
|
985
|
+
case RpcOp.Call: {
|
|
986
|
+
const record = references.get(payload?.reference);
|
|
987
|
+
if (!record || typeof payload.method !== "string" || !record.methods.has(payload.method) || !Array.isArray(payload.arguments) || typeof record.reference[payload.method] !== "function")
|
|
988
|
+
throw new TembraError(Errors.Forbidden.Method);
|
|
989
|
+
if (request.cancelled)
|
|
990
|
+
throw new TembraError(Errors.Cancelled.Request);
|
|
991
|
+
return host.root(async (ctx) => {
|
|
992
|
+
calls.set(id, ctx);
|
|
993
|
+
try {
|
|
994
|
+
return await record.reference[payload.method](...payload.arguments, ctx) ?? null;
|
|
995
|
+
} finally {
|
|
996
|
+
calls.delete(id);
|
|
997
|
+
}
|
|
998
|
+
}, { principal: options.principal });
|
|
999
|
+
}
|
|
1000
|
+
default:
|
|
1001
|
+
throw new TembraError(Errors.Invalid.RpcOperation);
|
|
1002
|
+
}
|
|
1003
|
+
})().then(async (ok) => {
|
|
1004
|
+
await channel.send({ id, ok });
|
|
1005
|
+
if (message.op === RpcOp.Get) {
|
|
1006
|
+
const record = references.get(ok.reference);
|
|
1007
|
+
if (record) {
|
|
1008
|
+
record.active = true;
|
|
1009
|
+
pump(ok.reference, record);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}, (error) => channel.send({ id, error: errorValue(error) })).catch((error) => channel.close(error)).finally(() => {
|
|
1013
|
+
pending--;
|
|
1014
|
+
requests.delete(id);
|
|
1015
|
+
});
|
|
1016
|
+
}, () => {
|
|
1017
|
+
for (const ctx of calls.values()) {
|
|
1018
|
+
try {
|
|
1019
|
+
ctx.cancel();
|
|
1020
|
+
} catch {}
|
|
1021
|
+
}
|
|
1022
|
+
for (const record of references.values()) {
|
|
1023
|
+
record.closed = true;
|
|
1024
|
+
record.detach?.();
|
|
1025
|
+
record.reference.release();
|
|
1026
|
+
}
|
|
1027
|
+
references.clear();
|
|
1028
|
+
});
|
|
1029
|
+
return {
|
|
1030
|
+
close: () => channel.close()
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
export {
|
|
1034
|
+
RemoteHost,
|
|
1035
|
+
serve
|
|
1036
|
+
};
|