@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.
Files changed (130) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +47 -2
  3. package/dist/authoring/declaration.d.ts +47 -0
  4. package/dist/authoring/hooks.d.ts +11 -0
  5. package/dist/authoring/orb.d.ts +3 -0
  6. package/dist/authoring/types.d.ts +166 -0
  7. package/dist/bridge/identifiers.d.ts +207 -0
  8. package/dist/bridge/kernel.d.ts +117 -0
  9. package/dist/bridge/protocol.d.ts +181 -0
  10. package/dist/browser.d.ts +21 -0
  11. package/dist/browser.js +3282 -0
  12. package/dist/codec.d.ts +1 -0
  13. package/dist/codecs/json.d.ts +13 -0
  14. package/dist/codecs/msgpack.d.ts +3 -0
  15. package/dist/context-key.d.ts +35 -0
  16. package/dist/coordinator.d.ts +1 -0
  17. package/dist/coordinator.js +543 -0
  18. package/dist/embedded.d.ts +1 -0
  19. package/dist/embedded.js +1 -0
  20. package/dist/error.d.ts +13 -0
  21. package/dist/errors.d.ts +122 -0
  22. package/dist/hooks.d.ts +1 -0
  23. package/dist/host.d.ts +96 -0
  24. package/dist/host.js +3225 -0
  25. package/dist/identifiers.d.ts +1 -0
  26. package/dist/index.d.ts +12 -0
  27. package/dist/index.js +833 -0
  28. package/dist/infrastructure/coordinator.d.ts +13 -0
  29. package/dist/infrastructure/manager.d.ts +23 -0
  30. package/dist/infrastructure/rpc/channel.d.ts +52 -0
  31. package/dist/infrastructure/rpc/client.d.ts +20 -0
  32. package/dist/infrastructure/rpc/server.d.ts +10 -0
  33. package/dist/infrastructure/rpc/state.d.ts +16 -0
  34. package/dist/kernel.d.ts +1 -0
  35. package/dist/manager.d.ts +1 -0
  36. package/dist/manager.js +390 -0
  37. package/dist/msgpack.d.ts +1 -0
  38. package/dist/msgpack.js +635 -0
  39. package/dist/node.d.ts +14 -0
  40. package/dist/node.js +3283 -0
  41. package/dist/orb.d.ts +1 -0
  42. package/dist/protocol.d.ts +1 -0
  43. package/dist/rpc.d.ts +3 -0
  44. package/dist/rpc.js +1036 -0
  45. package/dist/runtime/activities.d.ts +30 -0
  46. package/dist/runtime/calls.d.ts +22 -0
  47. package/dist/runtime/context.d.ts +37 -0
  48. package/dist/runtime/declaration.d.ts +1 -0
  49. package/dist/runtime/lifecycle.d.ts +28 -0
  50. package/dist/runtime/orbs.d.ts +39 -0
  51. package/dist/runtime/programs.d.ts +31 -0
  52. package/dist/runtime/records.d.ts +107 -0
  53. package/dist/runtime/references.d.ts +38 -0
  54. package/dist/runtime/reload.d.ts +17 -0
  55. package/dist/runtime/resources.d.ts +24 -0
  56. package/dist/runtime/scopes.d.ts +35 -0
  57. package/dist/runtime/session.d.ts +69 -0
  58. package/dist/state/binding.d.ts +28 -0
  59. package/dist/state/selection.d.ts +21 -0
  60. package/dist/state/types.d.ts +35 -0
  61. package/dist/state.d.ts +2 -0
  62. package/dist/telemetry.d.ts +23 -0
  63. package/dist/telemetry.js +27 -0
  64. package/dist/tooling/build.d.ts +3 -0
  65. package/dist/tooling/index.d.ts +2 -0
  66. package/dist/tooling/index.js +181 -0
  67. package/dist/tooling/resolver.d.ts +5 -0
  68. package/dist/tooling/typecheck.d.ts +2 -0
  69. package/dist/tooling/types.d.ts +29 -0
  70. package/dist/types.d.ts +1 -0
  71. package/dist/values.d.ts +23 -0
  72. package/package.json +103 -4
  73. package/src/authoring/declaration.ts +220 -0
  74. package/src/authoring/hooks.ts +101 -0
  75. package/src/authoring/orb.ts +77 -0
  76. package/src/authoring/types.ts +212 -0
  77. package/src/bridge/identifiers.ts +212 -0
  78. package/src/bridge/kernel.ts +117 -0
  79. package/src/bridge/protocol.ts +193 -0
  80. package/src/browser.ts +59 -0
  81. package/src/codec.ts +1 -0
  82. package/src/codecs/json.ts +147 -0
  83. package/src/codecs/msgpack.ts +26 -0
  84. package/src/context-key.ts +94 -0
  85. package/src/coordinator.ts +1 -0
  86. package/src/embedded.ts +293 -0
  87. package/src/error.ts +50 -0
  88. package/src/errors.ts +237 -0
  89. package/src/hooks.ts +1 -0
  90. package/src/host.ts +142 -0
  91. package/src/identifiers.ts +1 -0
  92. package/src/index.ts +22 -0
  93. package/src/infrastructure/coordinator.ts +76 -0
  94. package/src/infrastructure/manager.ts +77 -0
  95. package/src/infrastructure/rpc/channel.ts +88 -0
  96. package/src/infrastructure/rpc/client.ts +161 -0
  97. package/src/infrastructure/rpc/server.ts +201 -0
  98. package/src/infrastructure/rpc/state.ts +70 -0
  99. package/src/kernel.ts +1 -0
  100. package/src/manager.ts +1 -0
  101. package/src/msgpack.ts +1 -0
  102. package/src/node.ts +53 -0
  103. package/src/orb.ts +1 -0
  104. package/src/protocol.ts +1 -0
  105. package/src/rpc.ts +3 -0
  106. package/src/runtime/activities.ts +197 -0
  107. package/src/runtime/calls.ts +165 -0
  108. package/src/runtime/context.ts +113 -0
  109. package/src/runtime/declaration.ts +1 -0
  110. package/src/runtime/lifecycle.ts +227 -0
  111. package/src/runtime/orbs.ts +292 -0
  112. package/src/runtime/programs.ts +274 -0
  113. package/src/runtime/records.ts +127 -0
  114. package/src/runtime/references.ts +398 -0
  115. package/src/runtime/reload.ts +161 -0
  116. package/src/runtime/resources.ts +157 -0
  117. package/src/runtime/scopes.ts +210 -0
  118. package/src/runtime/session.ts +181 -0
  119. package/src/state/binding.ts +157 -0
  120. package/src/state/selection.ts +122 -0
  121. package/src/state/types.ts +47 -0
  122. package/src/state.ts +2 -0
  123. package/src/telemetry.ts +45 -0
  124. package/src/tooling/build.ts +83 -0
  125. package/src/tooling/index.ts +8 -0
  126. package/src/tooling/resolver.ts +95 -0
  127. package/src/tooling/typecheck.ts +32 -0
  128. package/src/tooling/types.ts +32 -0
  129. package/src/types.ts +1 -0
  130. package/src/values.ts +85 -0
@@ -0,0 +1,13 @@
1
+ export interface Participant<T> {
2
+ /** Prepare this participant without publishing its changes. */
3
+ prepare(signal: AbortSignal): Promise<T>;
4
+ /** Publish this participant's prepared changes. */
5
+ commit(ticket: T): Promise<void>;
6
+ /** Release this participant's uncommitted candidate. */
7
+ abort(ticket: T): Promise<void>;
8
+ }
9
+ /** Bounded direct participants. A commit decision is not a durable distributed transaction. */
10
+ export declare function coordinate<T>(participants: readonly Participant<T>[], options?: {
11
+ deadline?: number;
12
+ maxParticipants?: number;
13
+ }): Promise<void>;
@@ -0,0 +1,23 @@
1
+ import type { Host } from "../host.ts";
2
+ /** Explicit infrastructure lifecycle. Replacing a host creates new memory and references. */
3
+ export declare class HostManager {
4
+ #private;
5
+ /** Read an existing host without starting it. */
6
+ get(name: string): Host | undefined;
7
+ /** Share one pending creation for each host name. */
8
+ start(name: string, create: () => Promise<Host> | Host): Promise<Host>;
9
+ /** Remove the named host and wait for its cleanup. */
10
+ stop(name: string): Promise<void>;
11
+ /** Close the old host before creating its replacement. */
12
+ replace(name: string, create: () => Promise<Host> | Host): Promise<Host>;
13
+ /** Wait for every managed host and report cleanup failures together. */
14
+ close(): Promise<void>;
15
+ }
16
+ /** Route acquisitions to an application-selected host. */
17
+ export declare function router<H extends {
18
+ /** Acquire a reference from the selected host. */
19
+ get(service: string, key: string | readonly string[], options?: any): any;
20
+ }>(hosts: ReadonlyMap<string, H>, route: (service: string, key: readonly string[]) => string): {
21
+ /** Choose the destination host before acquiring the reference. */
22
+ get(service: string, key: string | readonly string[], options?: any): ReturnType<H["get"]>;
23
+ };
@@ -0,0 +1,52 @@
1
+ import { Code } from "../../bridge/identifiers.ts";
2
+ import { RpcEvent, RpcOp } from "../../bridge/protocol.ts";
3
+ import type { Codec } from "../../codecs/json.ts";
4
+ /** Adapters for WebSocket, MessagePort, or an application channel implement this byte boundary. */
5
+ export interface Transport {
6
+ /** Send one encoded frame through the application transport. */
7
+ send(frame: Uint8Array): void | Promise<void>;
8
+ /** Register frame and connection callbacks until detached. */
9
+ listen(receive: (frame: Uint8Array) => void, closed: (error?: unknown) => void): () => void;
10
+ /** Close the underlying connection. */
11
+ close(): void;
12
+ }
13
+ export interface Limits {
14
+ codec?: Codec;
15
+ maxFrame?: number;
16
+ maxPending?: number;
17
+ onError?: (error: unknown) => void;
18
+ }
19
+ export interface Wire {
20
+ id?: number;
21
+ op?: RpcOp;
22
+ payload?: any;
23
+ ok?: any;
24
+ error?: {
25
+ code: Code;
26
+ reason: string;
27
+ message: string;
28
+ };
29
+ event?: RpcEvent;
30
+ }
31
+ /** Produce the portable fields of a structured error. */
32
+ export declare function errorValue(error: unknown): {
33
+ code: Code;
34
+ reason: string;
35
+ message: string;
36
+ };
37
+ export declare class Channel {
38
+ #private;
39
+ readonly transport: Transport;
40
+ readonly options: Limits;
41
+ readonly ended: (error: unknown) => void;
42
+ readonly codec: Codec;
43
+ readonly maxFrame: number;
44
+ readonly maxPending: number;
45
+ closed: boolean;
46
+ /** Attach the transport with frame limits and the chosen codec. */
47
+ constructor(transport: Transport, options: Limits, receive: (message: Wire) => void, ended: (error: unknown) => void);
48
+ /** Encode and size-check a message before transport delivery. */
49
+ send(message: Wire): Promise<void>;
50
+ /** Detach the transport and reject connection work once. */
51
+ close(error?: unknown): void;
52
+ }
@@ -0,0 +1,20 @@
1
+ import type { DynamicReference } from "../../authoring/types.ts";
2
+ import { RpcOp } from "../../bridge/protocol.ts";
3
+ import { Channel, type Limits, type Transport } from "./channel.ts";
4
+ /** A lost connection revokes its references. Reconnect by creating a peer and acquiring current state. */
5
+ export declare class RemoteHost {
6
+ #private;
7
+ readonly channel: Channel;
8
+ /** Attach response routing and revoke state on disconnect. */
9
+ constructor(transport: Transport, options?: Limits);
10
+ /** Correlate one bounded remote request with optional cancellation. */
11
+ request(op: RpcOp, payload: unknown, signal?: AbortSignal): Promise<any>;
12
+ /** Request remote orb metadata without service activation. */
13
+ installedOrbs(): Promise<unknown>;
14
+ /** Acquire a remote reference and its current state snapshot. */
15
+ get(service: string, key: string | readonly string[], options?: {
16
+ signal?: AbortSignal;
17
+ }): Promise<DynamicReference>;
18
+ /** Close the connection and reject pending requests. */
19
+ close(): void;
20
+ }
@@ -0,0 +1,10 @@
1
+ import type { Host } from "../../host.ts";
2
+ import { type Limits, type Transport } from "./channel.ts";
3
+ /** Trusted infrastructure supplies the principal and authorization policy; peers cannot choose it. */
4
+ export declare function serve(host: Host, transport: Transport, options: Limits & {
5
+ principal: string;
6
+ allow?: (service: string, key: readonly string[]) => boolean;
7
+ }): {
8
+ /** Stop the connection and release its remote claims. */
9
+ close(): void;
10
+ };
@@ -0,0 +1,16 @@
1
+ import type { Readable } from "../../state.ts";
2
+ export declare class RemoteState implements Readable<any> {
3
+ #private;
4
+ readonly check: () => void;
5
+ readonly report: (error: unknown) => void;
6
+ /** Protect the initial remote snapshot and retain its validity check. */
7
+ constructor(value: unknown, check: () => void, report: (error: unknown) => void);
8
+ /** Read the snapshot while the remote reference remains valid. */
9
+ get value(): any;
10
+ /** Publish a protected remote snapshot to local observers. */
11
+ adopt(value: unknown): void;
12
+ /** Deliver the current snapshot, then subsequent updates. */
13
+ subscribe(listener: (value: any) => void): () => void;
14
+ /** Derive a readable value from the remote state. */
15
+ select<R>(compute: (value: any) => R): Readable<R>;
16
+ }
@@ -0,0 +1 @@
1
+ export * from "./bridge/kernel.ts";
@@ -0,0 +1 @@
1
+ export * from "./infrastructure/manager.ts";
@@ -0,0 +1,390 @@
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/errors.ts
202
+ function define(code, reason, message) {
203
+ return Object.freeze({ code, reason, message });
204
+ }
205
+ var Errors = Object.freeze({
206
+ Cancelled: Object.freeze({
207
+ EngineEffect: define(Code.Cancelled, Reason.EngineEffect, "The engine effect was cancelled"),
208
+ Request: define(Code.Cancelled, Reason.Request, "The request was cancelled"),
209
+ Resource: define(Code.Cancelled, Reason.Resource, "Resource opening was cancelled"),
210
+ Root: define(Code.Cancelled, Reason.Root, "The operation was cancelled"),
211
+ Task: define(Code.Cancelled, Reason.Task, "The task was cancelled")
212
+ }),
213
+ Closed: Object.freeze({
214
+ Host: define(Code.Closed, Reason.Host, "The host is closing")
215
+ }),
216
+ Conflict: Object.freeze({
217
+ AlreadyCommitted: define(Code.Conflict, Reason.AlreadyCommitted, "The replacement is already committed"),
218
+ Codec: define(Code.Conflict, Reason.Codec, "The Rust and JS host codecs must agree"),
219
+ PrepareDeadline: define(Code.Conflict, Reason.PrepareDeadline, "The preparation deadline expired"),
220
+ ReloadRunning: define(Code.Conflict, Reason.ReloadRunning, "A replacement is already running"),
221
+ ReloadTicket: define(Code.Conflict, Reason.ReloadTicket, "The replacement is no longer prepared")
222
+ }),
223
+ Exhausted: Object.freeze({
224
+ FrameSize: define(Code.Exhausted, Reason.FrameSize, "The frame exceeds the configured size limit"),
225
+ PendingRequests: define(Code.Exhausted, Reason.PendingRequests, "The pending request limit was reached"),
226
+ RemoteReferences: define(Code.Exhausted, Reason.RemoteReferences, "The remote reference limit was reached"),
227
+ RequestIds: define(Code.Exhausted, Reason.RequestIds, "Request identities are exhausted"),
228
+ SubscriptionQueue: define(Code.Exhausted, Reason.SubscriptionQueue, "The subscription queue is full")
229
+ }),
230
+ Expired: Object.freeze({
231
+ Next: define(Code.Expired, Reason.Next, "Continuation scope has ended")
232
+ }),
233
+ Forbidden: Object.freeze({
234
+ Context: define(Code.Forbidden, Reason.Context, "An issued context from this host is required"),
235
+ HostResource: define(Code.Forbidden, Reason.HostResource, "The host resource was not declared"),
236
+ Method: define(Code.Forbidden, Reason.Method, "The method is not allowed by this reference"),
237
+ ObservationSource: define(Code.Forbidden, Reason.ObservationSource, "Hook observation requires the original physical invocation"),
238
+ Opaque: define(Code.Forbidden, Reason.Opaque, "The resource is not available as an opaque local value"),
239
+ Service: define(Code.Forbidden, Reason.Service, "Access to this service is forbidden"),
240
+ StateOwner: define(Code.Forbidden, Reason.StateOwner, "Only the owning orb may change this state"),
241
+ UndeclaredDependency: define(Code.Forbidden, Reason.UndeclaredDependency, "The dependency was not declared"),
242
+ UndeclaredService: define(Code.Forbidden, Reason.UndeclaredService, "The service was not declared as a dependency")
243
+ }),
244
+ Gone: Object.freeze({
245
+ Connection: define(Code.Gone, Reason.Connection, "The connection is closed"),
246
+ Contribution: define(Code.Gone, Reason.Contribution, "The contribution is no longer available"),
247
+ Implementation: define(Code.Gone, Reason.Implementation, "The implementation is no longer available"),
248
+ ListenerCallback: define(Code.Gone, Reason.ListenerCallback, "The listener callback is no longer available"),
249
+ Reference: define(Code.Gone, Reason.Reference, "The reference is no longer available"),
250
+ Resource: define(Code.Gone, Reason.Resource, "The resource is no longer available"),
251
+ Scope: define(Code.Gone, Reason.Scope, "The declaration scope is no longer available"),
252
+ State: define(Code.Gone, Reason.State, "State owner is gone")
253
+ }),
254
+ Invalid: Object.freeze({
255
+ Address: define(Code.Invalid, Reason.Address, "Invalid address"),
256
+ AsyncFactory: define(Code.Invalid, Reason.AsyncFactory, "Factories must be synchronous"),
257
+ AsyncState: define(Code.Invalid, Reason.AsyncState, "State updates and migrations must be synchronous"),
258
+ CoordinationLimits: define(Code.Invalid, Reason.CoordinationLimits, "Invalid coordination limits"),
259
+ DependencyCycle: define(Code.Invalid, Reason.DependencyCycle, "Orb factories cannot acquire their own services"),
260
+ DependencyService: define(Code.Invalid, Reason.DependencyService, "Invalid dependency service"),
261
+ DuplicateListener: define(Code.Invalid, Reason.DuplicateListener, "Invalid duplicate listener"),
262
+ DuplicateResource: define(Code.Invalid, Reason.DuplicateResource, "Invalid duplicate resource"),
263
+ EventSource: define(Code.Invalid, Reason.EventSource, "Invalid event source"),
264
+ Orb: define(Code.Invalid, Reason.Orb, "An orb needs a name and positive API version"),
265
+ OrbGone: define(Code.Invalid, Reason.OrbGone, "Invalid orb gone"),
266
+ OrbInstalled: define(Code.Invalid, Reason.OrbInstalled, "Use reload to replace an installed orb"),
267
+ Factory: define(Code.Invalid, Reason.Factory, "Invalid factory"),
268
+ FactoryExpired: define(Code.Invalid, Reason.FactoryExpired, "Declarations must be synchronous"),
269
+ Hook: define(Code.Invalid, Reason.Hook, "Hooks need a run function"),
270
+ HookDependency: define(Code.Invalid, Reason.HookDependency, "Invalid hook dependency"),
271
+ HooksDisabled: define(Code.Invalid, Reason.HooksDisabled, "Invalid hooks disabled"),
272
+ JsonAccessor: define(Code.Invalid, Reason.JsonAccessor, "Invalid json accessor"),
273
+ JsonCycle: define(Code.Invalid, Reason.JsonCycle, "Invalid json cycle"),
274
+ JsonNumber: define(Code.Invalid, Reason.JsonNumber, "Use BigInt for integers outside the safe Number range"),
275
+ JsonSymbol: define(Code.Invalid, Reason.JsonSymbol, "Invalid json symbol"),
276
+ JsonTag: define(Code.Invalid, Reason.JsonTag, "Unknown or malformed portable type tag"),
277
+ JsonType: define(Code.Invalid, Reason.JsonType, "Supply a codec for custom object types"),
278
+ JsonValue: define(Code.Invalid, Reason.JsonValue, "This JSON codec requires explicit portable values"),
279
+ Method: define(Code.Invalid, Reason.Method, "Methods must be functions"),
280
+ NextUsed: define(Code.Invalid, Reason.NextUsed, "Invalid next used"),
281
+ Priority: define(Code.Invalid, Reason.Priority, "Invalid priority"),
282
+ Reference: define(Code.Invalid, Reason.Reference, "An issued local reference is required"),
283
+ Resource: define(Code.Invalid, Reason.Resource, "Invalid resource"),
284
+ ResourceHost: define(Code.Invalid, Reason.ResourceHost, "Resource dependencies must belong to the same host"),
285
+ ResourceInFactory: define(Code.Invalid, Reason.ResourceInFactory, "Resources open after declaration"),
286
+ RpcFrame: define(Code.Invalid, Reason.RpcFrame, "Invalid rpc frame"),
287
+ RpcOperation: define(Code.Invalid, Reason.RpcOperation, "Invalid rpc operation"),
288
+ RpcRequest: define(Code.Invalid, Reason.RpcRequest, "Invalid rpc request"),
289
+ Service: define(Code.Invalid, Reason.Service, "Invalid service"),
290
+ ServiceGone: define(Code.Invalid, Reason.ServiceGone, "Invalid service gone"),
291
+ State: define(Code.Invalid, Reason.State, "Service does not expose state"),
292
+ StateCycle: define(Code.Invalid, Reason.StateCycle, "State cannot contain cycles"),
293
+ StateValidation: define(Code.Invalid, Reason.StateValidation, "The orb rejected the state candidate"),
294
+ StateValue: define(Code.Invalid, Reason.StateValue, "State cannot contain accessors"),
295
+ StateVersion: define(Code.Invalid, Reason.StateVersion, "Invalid state version"),
296
+ SubscriptionsDisabled: define(Code.Invalid, Reason.SubscriptionsDisabled, "Invalid subscriptions disabled"),
297
+ SubscriptionSize: define(Code.Invalid, Reason.SubscriptionSize, "A local byte budget requires an explicit sizeOf function"),
298
+ Task: define(Code.Invalid, Reason.Task, "Invalid task"),
299
+ Utf8: define(Code.Invalid, Reason.Utf8, "Invalid utf8"),
300
+ ValueGone: define(Code.Invalid, Reason.ValueGone, "Invalid value gone"),
301
+ ValuesExhausted: define(Code.Invalid, Reason.ValuesExhausted, "Invalid values exhausted")
302
+ }),
303
+ Released: Object.freeze({
304
+ Reference: define(Code.Released, Reason.Reference, "The reference has been released")
305
+ }),
306
+ Unavailable: Object.freeze({
307
+ Callback: define(Code.Unavailable, Reason.Callback, "A callback failed"),
308
+ OrbScope: define(Code.Unavailable, Reason.OrbScope, "The orb scope is unavailable"),
309
+ NotReady: define(Code.Unavailable, Reason.NotReady, "The object is not ready"),
310
+ Route: define(Code.Unavailable, Reason.Route, "The route is unavailable")
311
+ }),
312
+ Unsupported: Object.freeze({
313
+ EngineEffect: define(Code.Unsupported, Reason.EngineEffect, "The engine effect is not supported by this adapter"),
314
+ RemoteOpaque: define(Code.Unsupported, Reason.RemoteOpaque, "Opaque resources are local to their language environment"),
315
+ Telemetry: define(Code.Unsupported, Reason.Telemetry, "Telemetry is disabled in this kernel"),
316
+ Views: define(Code.Unsupported, Reason.Views, "Reference views are disabled in this kernel")
317
+ })
318
+ });
319
+
320
+ // src/error.ts
321
+ var codes = new Set(Object.values(Code));
322
+ class TembraError extends Error {
323
+ code;
324
+ reason;
325
+ constructor(error, message = error.message, options) {
326
+ super(message, options);
327
+ this.name = "TembraError";
328
+ this.code = error.code;
329
+ this.reason = error.reason;
330
+ }
331
+ }
332
+
333
+ // src/infrastructure/manager.ts
334
+ class HostManager {
335
+ #hosts = new Map;
336
+ #pending = new Map;
337
+ get(name) {
338
+ return this.#hosts.get(name);
339
+ }
340
+ start(name, create) {
341
+ const existing = this.#hosts.get(name);
342
+ if (existing)
343
+ return Promise.resolve(existing);
344
+ const pending = this.#pending.get(name);
345
+ if (pending)
346
+ return pending;
347
+ const operation = Promise.resolve().then(create).then((host) => {
348
+ this.#hosts.set(name, host);
349
+ return host;
350
+ }).finally(() => {
351
+ this.#pending.delete(name);
352
+ });
353
+ this.#pending.set(name, operation);
354
+ return operation;
355
+ }
356
+ async stop(name) {
357
+ await this.#pending.get(name);
358
+ const host = this.#hosts.get(name);
359
+ if (!host)
360
+ return;
361
+ this.#hosts.delete(name);
362
+ await host.close();
363
+ }
364
+ async replace(name, create) {
365
+ await this.stop(name);
366
+ return this.start(name, create);
367
+ }
368
+ async close() {
369
+ const names = new Set([...this.#hosts.keys(), ...this.#pending.keys()]);
370
+ const results = await Promise.allSettled([...names].map((name) => this.stop(name)));
371
+ const errors = results.filter((result) => result.status === "rejected").map((result) => result.reason);
372
+ if (errors.length)
373
+ throw new AggregateError(errors, "Host cleanup failed");
374
+ }
375
+ }
376
+ function router(hosts, route) {
377
+ return {
378
+ get(service, key, options) {
379
+ const name = route(service, typeof key === "string" ? [key] : key);
380
+ const host = hosts.get(name);
381
+ if (!host)
382
+ throw new TembraError(Errors.Unavailable.Route, name);
383
+ return host.get(service, key, options);
384
+ }
385
+ };
386
+ }
387
+ export {
388
+ HostManager,
389
+ router
390
+ };
@@ -0,0 +1 @@
1
+ export * from "./codecs/msgpack.ts";