@sorbetai/plugins 0.0.0-stage → 0.1.0-alpha.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1179 -2
- package/dist/LICENSE.worker.acorn +21 -0
- package/dist/LICENSE.worker.acorn-walk +21 -0
- package/dist/LICENSE.worker.effect +21 -0
- package/dist/LICENSE.worker.ensure-error +9 -0
- package/dist/LICENSE.worker.es-toolkit +21 -0
- package/dist/LICENSE.worker.pi-schema.typebox +23 -0
- package/dist/authority-Ci--C1Np.js +4667 -0
- package/dist/authority-DGNilDTs.d.ts +547 -0
- package/dist/context-VD8Q9ax-.d.ts +1326 -0
- package/dist/data-schema-CHZWAtoX.js +185 -0
- package/dist/facet-DnC_pqrd.d.ts +178 -0
- package/dist/index-S_uGJe46.d.ts +610 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +1400 -0
- package/dist/isolate-worker.d.mts +1 -0
- package/dist/isolate-worker.mjs +180791 -0
- package/dist/model.d.ts +27 -0
- package/dist/model.js +274 -0
- package/dist/node.d.ts +273 -0
- package/dist/node.js +2844 -0
- package/dist/object-borrow-C81Gj5tq.js +488 -0
- package/dist/object-invocation-DC6PCiFY.js +15 -0
- package/dist/object-invocation-DMnRagqW.d.ts +13 -0
- package/dist/object-method-Bs7hYSjS.js +69 -0
- package/dist/objects-DdioT9PR.js +113 -0
- package/dist/remote.d.ts +39 -0
- package/dist/remote.js +899 -0
- package/dist/run-yield-B1AE7-PN.js +16 -0
- package/dist/source-Dlv8HG5K.d.ts +214 -0
- package/dist/view-Cn9jz2Wx.js +73 -0
- package/package.json +53 -3
package/dist/remote.js
ADDED
|
@@ -0,0 +1,899 @@
|
|
|
1
|
+
import { H as JsonValueSchema, U as captureJsonValue, t as bindObjectRefPort } from "./object-borrow-C81Gj5tq.js";
|
|
2
|
+
import { n as makeObjectMethodBinding } from "./object-method-Bs7hYSjS.js";
|
|
3
|
+
import { t as captureObjectAddress } from "./objects-DdioT9PR.js";
|
|
4
|
+
import { n as ObjectInvocationUnknownSchema, t as ObjectInvocationUnknownError } from "./object-invocation-DC6PCiFY.js";
|
|
5
|
+
import { a as FacetStateSchema, c as decodeObjectFacetReply, i as FacetIdentitySchema, l as decodeObjectFacetRequest, n as FacetAddressSchema, o as ObjectFacetUnavailableError, r as FacetCatalogSchema, s as UnavailableFacetSchema, t as validateDataSchema } from "./data-schema-CHZWAtoX.js";
|
|
6
|
+
import * as s from "@earendil-works/pi-schema";
|
|
7
|
+
import { check, compile, decode, decodeWith } from "@sorbetai/schema";
|
|
8
|
+
import * as Effect from "effect/Effect";
|
|
9
|
+
import ensureError from "ensure-error";
|
|
10
|
+
import { isEqual } from "es-toolkit";
|
|
11
|
+
import * as Exit from "effect/Exit";
|
|
12
|
+
import * as Scope from "effect/Scope";
|
|
13
|
+
import * as Cause from "effect/Cause";
|
|
14
|
+
import * as Deferred from "effect/Deferred";
|
|
15
|
+
import * as Fiber from "effect/Fiber";
|
|
16
|
+
import * as Option from "effect/Option";
|
|
17
|
+
import * as Semaphore from "effect/Semaphore";
|
|
18
|
+
import * as Stream from "effect/Stream";
|
|
19
|
+
|
|
20
|
+
//#region src/remote/client.ts
|
|
21
|
+
const AcquiredSchema = s.obj({
|
|
22
|
+
handle: s.str({ minLength: 1 }),
|
|
23
|
+
address: FacetAddressSchema
|
|
24
|
+
}, { open: false });
|
|
25
|
+
const UncertainActions = new Set(["call", "acquire"]);
|
|
26
|
+
const CleanupActions$1 = new Set(["release"]);
|
|
27
|
+
const IdentityDecoder$1 = compile(FacetIdentitySchema);
|
|
28
|
+
const AcquiredDecoder = compile(AcquiredSchema);
|
|
29
|
+
const CatalogDecoder = compile(FacetCatalogSchema);
|
|
30
|
+
const StateDecoder = compile(FacetStateSchema);
|
|
31
|
+
function decodeStateFrame(input) {
|
|
32
|
+
const frame = decodeWith(StateDecoder, input);
|
|
33
|
+
validateDataSchema(decode(s.map(JsonValueSchema), frame.schema));
|
|
34
|
+
return frame;
|
|
35
|
+
}
|
|
36
|
+
/** Shared typed boundary for connection-owned and explicitly resumable transports. */
|
|
37
|
+
const makeObjectClient = Effect.fn("Object.remote.client")(function* (transport, acquisitionOwner) {
|
|
38
|
+
const stopped = yield* Deferred.make();
|
|
39
|
+
const cancellation = new AbortController();
|
|
40
|
+
const cleanupCancellation = new AbortController();
|
|
41
|
+
const holds = /* @__PURE__ */ new Set();
|
|
42
|
+
let transportClose;
|
|
43
|
+
const closeTransport = () => transportClose ??= Promise.resolve().then(() => transport.close());
|
|
44
|
+
let open = true;
|
|
45
|
+
const assertAvailable = () => {
|
|
46
|
+
if (!open || transport.signal.aborted) throw new ObjectFacetUnavailableError("Remote Object connection is unavailable");
|
|
47
|
+
};
|
|
48
|
+
const request = (input) => Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
49
|
+
const controller = new AbortController();
|
|
50
|
+
const pending = (async () => {
|
|
51
|
+
const signal = controller.signal;
|
|
52
|
+
if (!CleanupActions$1.has(input.action)) assertAvailable();
|
|
53
|
+
const combined = CleanupActions$1.has(input.action) ? AbortSignal.any([
|
|
54
|
+
signal,
|
|
55
|
+
transport.signal,
|
|
56
|
+
cleanupCancellation.signal
|
|
57
|
+
]) : AbortSignal.any([
|
|
58
|
+
signal,
|
|
59
|
+
transport.signal,
|
|
60
|
+
cancellation.signal
|
|
61
|
+
]);
|
|
62
|
+
let reply;
|
|
63
|
+
try {
|
|
64
|
+
reply = await transport.request(input, combined);
|
|
65
|
+
} catch (cause) {
|
|
66
|
+
const error = ensureError(cause);
|
|
67
|
+
if (!UncertainActions.has(input.action)) throw error;
|
|
68
|
+
if (check(UnavailableFacetSchema, error)) throw error;
|
|
69
|
+
throw new ObjectInvocationUnknownError("Remote Object request lost its terminal reply; the effect may have occurred", { cause: error });
|
|
70
|
+
}
|
|
71
|
+
if (!reply.ok) {
|
|
72
|
+
if (reply.code === "outcome-unknown") throw new ObjectInvocationUnknownError(reply.message);
|
|
73
|
+
if (reply.code === "unavailable") throw new ObjectFacetUnavailableError(reply.message);
|
|
74
|
+
throw new Error(reply.message);
|
|
75
|
+
}
|
|
76
|
+
return reply.value;
|
|
77
|
+
})();
|
|
78
|
+
const settled = Effect.tryPromise({
|
|
79
|
+
try: () => pending,
|
|
80
|
+
catch: ensureError
|
|
81
|
+
});
|
|
82
|
+
return yield* restore(settled).pipe(Effect.onInterrupt(() => Effect.sync(() => controller.abort()).pipe(Effect.andThen(settled), Effect.catch((error) => check(ObjectInvocationUnknownSchema, error) ? Effect.die(error) : Effect.void), Effect.asVoid)));
|
|
83
|
+
}));
|
|
84
|
+
const retire = (hold) => {
|
|
85
|
+
hold.retirement ??= Effect.runPromise(request({
|
|
86
|
+
action: "release",
|
|
87
|
+
handle: hold.handle,
|
|
88
|
+
acquisitionId: hold.acquisitionId
|
|
89
|
+
}).pipe(Effect.tap(() => Effect.sync(() => holds.delete(hold))), Effect.asVoid));
|
|
90
|
+
return hold.retirement;
|
|
91
|
+
};
|
|
92
|
+
yield* Effect.addFinalizer(() => Effect.tryPromise({
|
|
93
|
+
try: async () => {
|
|
94
|
+
open = false;
|
|
95
|
+
cancellation.abort();
|
|
96
|
+
cleanupCancellation.abort();
|
|
97
|
+
await Effect.runPromise(Deferred.succeed(stopped, void 0));
|
|
98
|
+
const failures = [];
|
|
99
|
+
try {
|
|
100
|
+
await closeTransport();
|
|
101
|
+
} catch (cause) {
|
|
102
|
+
failures.push(ensureError(cause));
|
|
103
|
+
}
|
|
104
|
+
if (failures.length === 0) {
|
|
105
|
+
const results = await Promise.allSettled([...holds].map(retire));
|
|
106
|
+
failures.push(...results.flatMap((result) => result.status === "rejected" ? [ensureError(result.reason)] : []));
|
|
107
|
+
}
|
|
108
|
+
if (failures.length > 0) throw new AggregateError(failures, "Remote Object release failed");
|
|
109
|
+
},
|
|
110
|
+
catch: ensureError
|
|
111
|
+
}).pipe(Effect.orDie));
|
|
112
|
+
const catalog = () => request({ action: "catalog" }).pipe(Effect.flatMap((value) => Effect.try({
|
|
113
|
+
try: () => {
|
|
114
|
+
const captured = decodeWith(CatalogDecoder, value);
|
|
115
|
+
for (const { contract } of captured.types) {
|
|
116
|
+
validateDataSchema(contract.key);
|
|
117
|
+
for (const method of Object.values(contract.methods)) {
|
|
118
|
+
validateDataSchema(method.input);
|
|
119
|
+
validateDataSchema(method.output);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return captured;
|
|
123
|
+
},
|
|
124
|
+
catch: ensureError
|
|
125
|
+
})));
|
|
126
|
+
const acquire = (type, key, namespace$1) => Effect.gen(function* () {
|
|
127
|
+
const captured = yield* Effect.try({
|
|
128
|
+
try: () => captureObjectAddress(type, key, namespace$1),
|
|
129
|
+
catch: ensureError
|
|
130
|
+
});
|
|
131
|
+
const closed = yield* Deferred.make();
|
|
132
|
+
const acquisitionId = globalThis.crypto.randomUUID();
|
|
133
|
+
const settleUnknown = () => acquisitionOwner === void 0 ? closeTransport() : acquisitionOwner.reconcile(acquisitionId);
|
|
134
|
+
let referenceOpen = true;
|
|
135
|
+
const hold = yield* Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
136
|
+
const held = yield* restore(request({
|
|
137
|
+
action: "acquire",
|
|
138
|
+
acquisitionId,
|
|
139
|
+
address: captured.address,
|
|
140
|
+
contract: captured.descriptor
|
|
141
|
+
}).pipe(Effect.flatMap((value) => Effect.try({
|
|
142
|
+
try: () => {
|
|
143
|
+
const result = decodeWith(AcquiredDecoder, value);
|
|
144
|
+
if (!isEqual(result.address, captured.address)) throw new TypeError("Remote Object returned another target");
|
|
145
|
+
if (!open) throw new Error("Remote Object client closed during acquisition");
|
|
146
|
+
const created = {
|
|
147
|
+
handle: result.handle,
|
|
148
|
+
acquisitionId,
|
|
149
|
+
address: captured.address,
|
|
150
|
+
references: 1
|
|
151
|
+
};
|
|
152
|
+
holds.add(created);
|
|
153
|
+
return created;
|
|
154
|
+
},
|
|
155
|
+
catch: (cause) => new ObjectInvocationUnknownError("Remote acquisition returned an invalid hold", { cause })
|
|
156
|
+
})), Effect.tapError((error) => check(ObjectInvocationUnknownSchema, error) ? Effect.tryPromise({
|
|
157
|
+
try: settleUnknown,
|
|
158
|
+
catch: ensureError
|
|
159
|
+
}).pipe(Effect.ignore) : Effect.void))).pipe(Effect.onInterrupt(() => Effect.tryPromise({
|
|
160
|
+
try: settleUnknown,
|
|
161
|
+
catch: ensureError
|
|
162
|
+
}).pipe(Effect.ignore)));
|
|
163
|
+
yield* Effect.addFinalizer(() => Effect.gen(function* () {
|
|
164
|
+
referenceOpen = false;
|
|
165
|
+
yield* Deferred.succeed(closed, void 0);
|
|
166
|
+
held.references -= 1;
|
|
167
|
+
if (held.references === 0) yield* Effect.tryPromise({
|
|
168
|
+
try: () => retire(held),
|
|
169
|
+
catch: ensureError
|
|
170
|
+
}).pipe(Effect.orDie);
|
|
171
|
+
}));
|
|
172
|
+
return held;
|
|
173
|
+
}));
|
|
174
|
+
const assertHold = () => {
|
|
175
|
+
assertAvailable();
|
|
176
|
+
if (hold.retirement !== void 0) throw new ObjectFacetUnavailableError("Remote Object hold has closed");
|
|
177
|
+
};
|
|
178
|
+
const assertReference = () => {
|
|
179
|
+
assertHold();
|
|
180
|
+
if (!referenceOpen) throw new Error("Object Ref has been released");
|
|
181
|
+
};
|
|
182
|
+
const invoke = (name, input) => Effect.try({
|
|
183
|
+
try: assertHold,
|
|
184
|
+
catch: ensureError
|
|
185
|
+
}).pipe(Effect.andThen(request({
|
|
186
|
+
action: "call",
|
|
187
|
+
handle: hold.handle,
|
|
188
|
+
method: name,
|
|
189
|
+
input
|
|
190
|
+
})), Effect.flatMap((value) => Effect.try({
|
|
191
|
+
try: () => {
|
|
192
|
+
const method = captured.methods[name];
|
|
193
|
+
if (method === void 0) throw new TypeError("Unknown Object method");
|
|
194
|
+
return decode(method.output, value);
|
|
195
|
+
},
|
|
196
|
+
catch: (cause) => new ObjectInvocationUnknownError("Remote Object result violated its captured contract", { cause })
|
|
197
|
+
})));
|
|
198
|
+
return bindObjectRefPort(captured.createRef({ call: (name, input) => Effect.try({
|
|
199
|
+
try: assertReference,
|
|
200
|
+
catch: ensureError
|
|
201
|
+
}).pipe(Effect.andThen(invoke(name, input))) }, {
|
|
202
|
+
states: () => Effect.try({
|
|
203
|
+
try: assertReference,
|
|
204
|
+
catch: ensureError
|
|
205
|
+
}).pipe(Effect.andThen(request({
|
|
206
|
+
action: "states",
|
|
207
|
+
handle: hold.handle
|
|
208
|
+
})), Effect.flatMap((value) => Effect.try({
|
|
209
|
+
try: () => decode(s.arr(JsonValueSchema), value).map(decodeStateFrame),
|
|
210
|
+
catch: ensureError
|
|
211
|
+
}))),
|
|
212
|
+
watchState: (name) => Stream.unwrap(Effect.try({
|
|
213
|
+
try: assertReference,
|
|
214
|
+
catch: ensureError
|
|
215
|
+
}).pipe(Effect.as(Stream.unfold(void 0, (after) => request({
|
|
216
|
+
action: "watch",
|
|
217
|
+
handle: hold.handle,
|
|
218
|
+
name,
|
|
219
|
+
after
|
|
220
|
+
}).pipe(Effect.flatMap((value) => Effect.try({
|
|
221
|
+
try: () => {
|
|
222
|
+
if (!referenceOpen || !open || value === null) return void 0;
|
|
223
|
+
const frame = decodeStateFrame(value);
|
|
224
|
+
if (frame.name !== name || after !== void 0 && frame.revision <= after) throw new TypeError("Remote Object state feed changed its name or regressed");
|
|
225
|
+
return [frame, frame.revision];
|
|
226
|
+
},
|
|
227
|
+
catch: ensureError
|
|
228
|
+
}))))))).pipe(Stream.interruptWhen(Effect.race(Deferred.await(closed), Deferred.await(stopped))))
|
|
229
|
+
}), { capture: (name) => {
|
|
230
|
+
assertReference();
|
|
231
|
+
const method = captured.methods[name];
|
|
232
|
+
if (method === void 0) throw new TypeError("Unknown Object method");
|
|
233
|
+
let pins = 0;
|
|
234
|
+
let expired = false;
|
|
235
|
+
const assertProjection = () => {
|
|
236
|
+
if (expired) throw new Error("Object method binding is released");
|
|
237
|
+
assertHold();
|
|
238
|
+
};
|
|
239
|
+
return {
|
|
240
|
+
address: captured.address,
|
|
241
|
+
input: method.input,
|
|
242
|
+
output: method.output,
|
|
243
|
+
assertAvailable: assertProjection,
|
|
244
|
+
invoke: (input) => Effect.try({
|
|
245
|
+
try: () => {
|
|
246
|
+
assertProjection();
|
|
247
|
+
if (pins === 0) throw new Error("Object method binding is not retained");
|
|
248
|
+
},
|
|
249
|
+
catch: ensureError
|
|
250
|
+
}).pipe(Effect.andThen(invoke(name, input))),
|
|
251
|
+
retain: () => {
|
|
252
|
+
assertProjection();
|
|
253
|
+
if (pins === 0) assertReference();
|
|
254
|
+
pins += 1;
|
|
255
|
+
hold.references += 1;
|
|
256
|
+
let retained = true;
|
|
257
|
+
return () => {
|
|
258
|
+
if (!retained) return;
|
|
259
|
+
retained = false;
|
|
260
|
+
pins -= 1;
|
|
261
|
+
if (pins === 0) expired = true;
|
|
262
|
+
hold.references -= 1;
|
|
263
|
+
if (hold.references === 0) retire(hold).catch(() => {});
|
|
264
|
+
};
|
|
265
|
+
},
|
|
266
|
+
settle: Effect.suspend(() => hold.retirement === void 0 ? Effect.void : Effect.tryPromise({
|
|
267
|
+
try: () => hold.retirement ?? Promise.resolve(),
|
|
268
|
+
catch: ensureError
|
|
269
|
+
}).pipe(Effect.orDie))
|
|
270
|
+
};
|
|
271
|
+
} });
|
|
272
|
+
});
|
|
273
|
+
let originNamespace;
|
|
274
|
+
const namespace = () => Effect.tryPromise({
|
|
275
|
+
try: () => {
|
|
276
|
+
assertAvailable();
|
|
277
|
+
if (originNamespace === void 0) {
|
|
278
|
+
const pending = Effect.runPromise(request({ action: "hello" }).pipe(Effect.map((value) => decodeWith(IdentityDecoder$1, value).namespace)));
|
|
279
|
+
originNamespace = pending;
|
|
280
|
+
pending.catch(() => {
|
|
281
|
+
if (originNamespace === pending) originNamespace = void 0;
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
return originNamespace;
|
|
285
|
+
},
|
|
286
|
+
catch: ensureError
|
|
287
|
+
});
|
|
288
|
+
const objects = {
|
|
289
|
+
catalog,
|
|
290
|
+
acquire: (type, key) => namespace().pipe(Effect.flatMap((current) => acquire(type, key, current)))
|
|
291
|
+
};
|
|
292
|
+
const objectCapabilities = {
|
|
293
|
+
catalog,
|
|
294
|
+
bind: (address, method, metadata) => Effect.gen(function* () {
|
|
295
|
+
const current = yield* catalog();
|
|
296
|
+
const contract = current.types.find((entry) => entry.availability === "available" && entry.contract.id === address.typeId && entry.contract.version === address.version)?.contract;
|
|
297
|
+
if (contract === void 0 || address.namespace !== current.namespace) return yield* Effect.fail(/* @__PURE__ */ new Error("Remote Object contract is unavailable or ambiguous"));
|
|
298
|
+
return yield* makeObjectMethodBinding(yield* acquire({
|
|
299
|
+
id: contract.id,
|
|
300
|
+
version: contract.version,
|
|
301
|
+
key: contract.key,
|
|
302
|
+
methods: Object.fromEntries(Object.entries(contract.methods).map(([name, schemas]) => [name, {
|
|
303
|
+
input: schemas.input,
|
|
304
|
+
output: schemas.output
|
|
305
|
+
}]))
|
|
306
|
+
}, address.key, current.namespace), method, metadata);
|
|
307
|
+
})
|
|
308
|
+
};
|
|
309
|
+
return Object.freeze({
|
|
310
|
+
objects: Object.freeze(objects),
|
|
311
|
+
objectCapabilities: Object.freeze(objectCapabilities)
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
/** Owns one transport and its holds. Lost acquisition replies close that connection so its
|
|
315
|
+
* owner releases provisional holds; this entry point never reconnects or replays a request. */
|
|
316
|
+
const makeRemoteObjectClient = (transport) => makeObjectClient(transport, void 0);
|
|
317
|
+
|
|
318
|
+
//#endregion
|
|
319
|
+
//#region src/remote/connector.ts
|
|
320
|
+
/** The client stopped locally, but its Host-owned facet still needs these identities reconciled.
|
|
321
|
+
* Reconciliation may release the original hold; it must never repeat its constructor. */
|
|
322
|
+
var ObjectFacetRecoveryPendingError = class extends Error {
|
|
323
|
+
code = "object-facet-recovery-pending";
|
|
324
|
+
acquisitionIds;
|
|
325
|
+
constructor(facetId, acquisitionIds) {
|
|
326
|
+
super("Remote Object acquisitions remain unreconciled after client closure");
|
|
327
|
+
this.facetId = facetId;
|
|
328
|
+
this.name = "ObjectFacetRecoveryPendingError";
|
|
329
|
+
this.acquisitionIds = Object.freeze([...acquisitionIds]);
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
const HeldProperties = {
|
|
333
|
+
handle: s.str({ minLength: 1 }),
|
|
334
|
+
address: FacetAddressSchema
|
|
335
|
+
};
|
|
336
|
+
const HeldSchema = s.obj(HeldProperties, { open: false });
|
|
337
|
+
const LookupSchema = s.union([
|
|
338
|
+
s.obj({
|
|
339
|
+
...HeldProperties,
|
|
340
|
+
released: s.bool()
|
|
341
|
+
}, { open: false }),
|
|
342
|
+
s.obj({
|
|
343
|
+
handle: s.str({ minLength: 1 }),
|
|
344
|
+
released: s.lit(true)
|
|
345
|
+
}, { open: false }),
|
|
346
|
+
s.obj({
|
|
347
|
+
released: s.lit(true),
|
|
348
|
+
abandoned: s.lit(true)
|
|
349
|
+
}, { open: false })
|
|
350
|
+
]);
|
|
351
|
+
const IdentityDecoder = compile(FacetIdentitySchema);
|
|
352
|
+
const HeldDecoder = compile(HeldSchema);
|
|
353
|
+
const LookupDecoder = compile(LookupSchema);
|
|
354
|
+
const ResumableActions = new Set([
|
|
355
|
+
"watch",
|
|
356
|
+
"lookup",
|
|
357
|
+
"abandon",
|
|
358
|
+
"release"
|
|
359
|
+
]);
|
|
360
|
+
const CleanupActions = new Set([
|
|
361
|
+
"lookup",
|
|
362
|
+
"abandon",
|
|
363
|
+
"release"
|
|
364
|
+
]);
|
|
365
|
+
/** Re-establishes capability access to the same resumable facet and original handles.
|
|
366
|
+
* Calls are never replayed. An uncertain acquisition is reconciled and released by its
|
|
367
|
+
* original identity, and still reports Unknown to its caller. */
|
|
368
|
+
const makeResumableRemoteObjectClient = Effect.fn("Object.remote.resumable")(function* (connector) {
|
|
369
|
+
const lifetime = new AbortController();
|
|
370
|
+
const connections = /* @__PURE__ */ new Set();
|
|
371
|
+
const failures = [];
|
|
372
|
+
const reconciliations = /* @__PURE__ */ new Map();
|
|
373
|
+
const acquisitions = /* @__PURE__ */ new Map();
|
|
374
|
+
const connect = connector.connect.bind(connector);
|
|
375
|
+
let current;
|
|
376
|
+
let opening;
|
|
377
|
+
let openingCancellation;
|
|
378
|
+
let facetId;
|
|
379
|
+
let namespace;
|
|
380
|
+
let closing;
|
|
381
|
+
let admissionClosed = false;
|
|
382
|
+
const beginClose = () => {
|
|
383
|
+
admissionClosed = true;
|
|
384
|
+
openingCancellation?.abort();
|
|
385
|
+
lifetime.abort();
|
|
386
|
+
};
|
|
387
|
+
const retire = (connection$1) => {
|
|
388
|
+
connection$1.closing ??= Promise.resolve().then(() => connection$1.transport.close()).then(() => {
|
|
389
|
+
connections.delete(connection$1);
|
|
390
|
+
});
|
|
391
|
+
return connection$1.closing;
|
|
392
|
+
};
|
|
393
|
+
const replyValue = (reply) => {
|
|
394
|
+
if (!reply.ok) throw new Error(reply.message);
|
|
395
|
+
return reply.value;
|
|
396
|
+
};
|
|
397
|
+
const connection = async (signal) => {
|
|
398
|
+
if (lifetime.signal.aborted || signal.aborted) throw new ObjectFacetUnavailableError("Object facet connection was not admitted");
|
|
399
|
+
if (current !== void 0 && !current.transport.signal.aborted) return current;
|
|
400
|
+
if (admissionClosed) throw new ObjectFacetUnavailableError("Object facet client no longer reconnects");
|
|
401
|
+
if (current !== void 0) {
|
|
402
|
+
retire(current).catch((cause) => failures.push(ensureError(cause)));
|
|
403
|
+
current = void 0;
|
|
404
|
+
}
|
|
405
|
+
opening ??= (async () => {
|
|
406
|
+
const controller = new AbortController();
|
|
407
|
+
openingCancellation = controller;
|
|
408
|
+
const connecting = AbortSignal.any([lifetime.signal, controller.signal]);
|
|
409
|
+
const transport$1 = await connect(connecting);
|
|
410
|
+
const created = {
|
|
411
|
+
transport: transport$1,
|
|
412
|
+
held: /* @__PURE__ */ new Set()
|
|
413
|
+
};
|
|
414
|
+
connections.add(created);
|
|
415
|
+
try {
|
|
416
|
+
const identity$1 = decodeWith(IdentityDecoder, replyValue(await transport$1.request({ action: "hello" }, connecting)));
|
|
417
|
+
if (!identity$1.resumable) throw new ObjectFacetUnavailableError("The original resumable Object facet is unavailable");
|
|
418
|
+
if (facetId !== void 0) {
|
|
419
|
+
if (facetId !== identity$1.facetId || namespace !== identity$1.namespace) throw new ObjectFacetUnavailableError("The original resumable Object facet changed");
|
|
420
|
+
}
|
|
421
|
+
if (lifetime.signal.aborted || admissionClosed) throw new ObjectFacetUnavailableError("Object facet client is closed");
|
|
422
|
+
facetId = identity$1.facetId;
|
|
423
|
+
namespace = identity$1.namespace;
|
|
424
|
+
current = created;
|
|
425
|
+
return created;
|
|
426
|
+
} catch (cause) {
|
|
427
|
+
await retire(created).catch((error) => failures.push(ensureError(error)));
|
|
428
|
+
throw cause;
|
|
429
|
+
}
|
|
430
|
+
})().finally(() => {
|
|
431
|
+
opening = void 0;
|
|
432
|
+
openingCancellation = void 0;
|
|
433
|
+
});
|
|
434
|
+
const pending = opening;
|
|
435
|
+
try {
|
|
436
|
+
return await Effect.runPromise(Effect.tryPromise({
|
|
437
|
+
try: () => pending,
|
|
438
|
+
catch: ensureError
|
|
439
|
+
}), { signal });
|
|
440
|
+
} catch (cause) {
|
|
441
|
+
throw new ObjectFacetUnavailableError("Object facet connection was not established", { cause });
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
const identity = () => {
|
|
445
|
+
if (facetId === void 0) throw new ObjectFacetUnavailableError("Object facet identity is not established");
|
|
446
|
+
return facetId;
|
|
447
|
+
};
|
|
448
|
+
const request = async (input, signal) => {
|
|
449
|
+
if (admissionClosed && !CleanupActions.has(input.action)) throw new ObjectFacetUnavailableError("Object facet client is closed");
|
|
450
|
+
const selected = await connection(signal);
|
|
451
|
+
let heldHandle;
|
|
452
|
+
switch (input.action) {
|
|
453
|
+
case "call":
|
|
454
|
+
case "states":
|
|
455
|
+
case "watch":
|
|
456
|
+
heldHandle = input.handle;
|
|
457
|
+
break;
|
|
458
|
+
}
|
|
459
|
+
if (heldHandle !== void 0 && !selected.held.has(heldHandle)) try {
|
|
460
|
+
if (decodeWith(HeldDecoder, replyValue(await selected.transport.request({
|
|
461
|
+
action: "rehold",
|
|
462
|
+
facetId: identity(),
|
|
463
|
+
handle: heldHandle
|
|
464
|
+
}, signal))).handle !== heldHandle) throw new Error("Remote Object re-hold returned another handle");
|
|
465
|
+
selected.held.add(heldHandle);
|
|
466
|
+
} catch (cause) {
|
|
467
|
+
if ((selected.transport.signal.aborted || check(UnavailableFacetSchema, cause)) && !signal.aborted && !admissionClosed) {
|
|
468
|
+
if (current === selected) current = void 0;
|
|
469
|
+
retire(selected).catch((error) => failures.push(ensureError(error)));
|
|
470
|
+
return request(input, signal);
|
|
471
|
+
}
|
|
472
|
+
throw new ObjectFacetUnavailableError(ensureError(cause).message, { cause });
|
|
473
|
+
}
|
|
474
|
+
const newAcquisition = input.action === "acquire" && !acquisitions.has(input.acquisitionId);
|
|
475
|
+
try {
|
|
476
|
+
if (input.action === "acquire" && !acquisitions.has(input.acquisitionId)) acquisitions.set(input.acquisitionId, {});
|
|
477
|
+
const result = await selected.transport.request(input, signal);
|
|
478
|
+
const reply = result;
|
|
479
|
+
if (input.action === "acquire") if (reply.ok) {
|
|
480
|
+
const { handle } = decodeWith(HeldDecoder, reply.value);
|
|
481
|
+
selected.held.add(handle);
|
|
482
|
+
const acquisition = acquisitions.get(input.acquisitionId);
|
|
483
|
+
if (acquisition !== void 0) acquisition.handle = handle;
|
|
484
|
+
} else if (reply.code === "outcome-unknown") await reconcile(input.acquisitionId).catch(() => void 0);
|
|
485
|
+
else acquisitions.delete(input.acquisitionId);
|
|
486
|
+
if (reply.ok && input.action === "release") {
|
|
487
|
+
for (const connected of connections) connected.held.delete(input.handle);
|
|
488
|
+
acquisitions.delete(input.acquisitionId);
|
|
489
|
+
}
|
|
490
|
+
if (reply.ok && input.action === "abandon") {
|
|
491
|
+
const acquisition = acquisitions.get(input.acquisitionId);
|
|
492
|
+
if (acquisition?.handle !== void 0) for (const connected of connections) connected.held.delete(acquisition.handle);
|
|
493
|
+
acquisitions.delete(input.acquisitionId);
|
|
494
|
+
}
|
|
495
|
+
return result;
|
|
496
|
+
} catch (cause) {
|
|
497
|
+
if (newAcquisition && input.action === "acquire" && check(UnavailableFacetSchema, cause)) acquisitions.delete(input.acquisitionId);
|
|
498
|
+
if (check(UnavailableFacetSchema, cause) && !signal.aborted && !lifetime.signal.aborted && !admissionClosed) {
|
|
499
|
+
if (current === selected) current = void 0;
|
|
500
|
+
retire(selected).catch((error) => failures.push(ensureError(error)));
|
|
501
|
+
return request(input, signal);
|
|
502
|
+
}
|
|
503
|
+
if (ResumableActions.has(input.action) && selected.transport.signal.aborted && !signal.aborted && !lifetime.signal.aborted && !admissionClosed) return request(input, signal);
|
|
504
|
+
throw cause;
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
function reconcile(acquisitionId) {
|
|
508
|
+
if (facetId === void 0 || !acquisitions.has(acquisitionId)) return Promise.resolve();
|
|
509
|
+
const existing = reconciliations.get(acquisitionId);
|
|
510
|
+
if (existing !== void 0) return existing;
|
|
511
|
+
const settled = (async () => {
|
|
512
|
+
try {
|
|
513
|
+
const lookup = await request({
|
|
514
|
+
action: "lookup",
|
|
515
|
+
facetId: identity(),
|
|
516
|
+
acquisitionId
|
|
517
|
+
}, lifetime.signal);
|
|
518
|
+
if (lookup.ok && lookup.value !== null) decodeWith(LookupDecoder, lookup.value);
|
|
519
|
+
} finally {
|
|
520
|
+
replyValue(await request({
|
|
521
|
+
action: "abandon",
|
|
522
|
+
facetId: identity(),
|
|
523
|
+
acquisitionId
|
|
524
|
+
}, lifetime.signal));
|
|
525
|
+
}
|
|
526
|
+
})().finally(() => reconciliations.delete(acquisitionId));
|
|
527
|
+
reconciliations.set(acquisitionId, settled);
|
|
528
|
+
settled.catch((cause) => {
|
|
529
|
+
const acquisition = acquisitions.get(acquisitionId);
|
|
530
|
+
if (acquisition !== void 0) acquisition.error = ensureError(cause);
|
|
531
|
+
});
|
|
532
|
+
return settled;
|
|
533
|
+
}
|
|
534
|
+
const transport = {
|
|
535
|
+
signal: lifetime.signal,
|
|
536
|
+
request,
|
|
537
|
+
close: () => {
|
|
538
|
+
beginClose();
|
|
539
|
+
closing ??= (async () => {
|
|
540
|
+
const results = await Promise.allSettled([...connections].map(retire));
|
|
541
|
+
if (opening !== void 0) await opening.catch(() => void 0);
|
|
542
|
+
if (results.every((result) => result.status === "fulfilled")) await Promise.allSettled(reconciliations.values());
|
|
543
|
+
failures.push(...results.flatMap((result) => result.status === "rejected" ? [ensureError(result.reason)] : []));
|
|
544
|
+
if (acquisitions.size > 0) failures.push(...[...acquisitions.values()].flatMap((value) => value.error === void 0 ? [] : [value.error]), new ObjectFacetRecoveryPendingError(facetId, [...acquisitions.keys()]));
|
|
545
|
+
current = void 0;
|
|
546
|
+
acquisitions.clear();
|
|
547
|
+
if (failures.length > 0) throw new AggregateError([...new Set(failures)], "Object facet recovery cleanup failed");
|
|
548
|
+
})();
|
|
549
|
+
return closing;
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
const scope = yield* Effect.acquireRelease(Scope.make("sequential"), (owned) => Scope.close(owned, Exit.void));
|
|
553
|
+
return yield* Scope.provide(Effect.uninterruptible(Effect.gen(function* () {
|
|
554
|
+
const client = yield* makeObjectClient(transport, { reconcile });
|
|
555
|
+
yield* Effect.addFinalizer(() => Effect.sync(beginClose));
|
|
556
|
+
return client;
|
|
557
|
+
})), scope);
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
//#endregion
|
|
561
|
+
//#region src/remote/facet.ts
|
|
562
|
+
const DeniedSchema = s.obj({ code: s.lit("object-facet-denied") }, { open: true });
|
|
563
|
+
var FacetDeniedError = class extends Error {
|
|
564
|
+
code = "object-facet-denied";
|
|
565
|
+
};
|
|
566
|
+
/** Scoped opaque holds borrow the origin's existing instances and generations. */
|
|
567
|
+
const makeObjectFacet = Effect.fn("Object.remote.facet")(function* (objects, options) {
|
|
568
|
+
const owner = yield* Scope.make();
|
|
569
|
+
const holds = /* @__PURE__ */ new Map();
|
|
570
|
+
const acquisitions = /* @__PURE__ */ new Map();
|
|
571
|
+
const facetId = globalThis.crypto.randomUUID();
|
|
572
|
+
const resumable = options.lifetime === "resumable";
|
|
573
|
+
let open = true;
|
|
574
|
+
let closing = false;
|
|
575
|
+
const closed = yield* Deferred.make();
|
|
576
|
+
const retireScope = (hold) => Effect.suspend(() => {
|
|
577
|
+
const retirement = hold.retirement ??= Effect.runPromise(Scope.close(hold.scope, Exit.void)).finally(() => {
|
|
578
|
+
if (holds.get(hold.handle) === hold) {
|
|
579
|
+
const receipt = {
|
|
580
|
+
kind: "receipt",
|
|
581
|
+
handle: hold.handle,
|
|
582
|
+
acquisitionId: hold.acquisitionId,
|
|
583
|
+
retirement,
|
|
584
|
+
abandoned: hold.abandoned
|
|
585
|
+
};
|
|
586
|
+
holds.set(hold.handle, receipt);
|
|
587
|
+
acquisitions.set(hold.acquisitionId, receipt);
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
return Effect.tryPromise({
|
|
591
|
+
try: () => retirement,
|
|
592
|
+
catch: ensureError
|
|
593
|
+
});
|
|
594
|
+
});
|
|
595
|
+
const release = (hold) => Effect.uninterruptible(Effect.gen(function* () {
|
|
596
|
+
if (hold.kind === "receipt") return yield* Effect.tryPromise({
|
|
597
|
+
try: () => hold.retirement,
|
|
598
|
+
catch: ensureError
|
|
599
|
+
});
|
|
600
|
+
hold.closed = true;
|
|
601
|
+
if (hold.ref === void 0) yield* Deferred.fail(hold.ready, /* @__PURE__ */ new Error("Object acquisition has been released"));
|
|
602
|
+
return yield* retireScope(hold);
|
|
603
|
+
}));
|
|
604
|
+
const close = Effect.uninterruptible(Effect.gen(function* () {
|
|
605
|
+
if (closing) return yield* Deferred.await(closed);
|
|
606
|
+
closing = true;
|
|
607
|
+
open = false;
|
|
608
|
+
const result = yield* Effect.exit(Effect.gen(function* () {
|
|
609
|
+
const outcomes = yield* Effect.forEach([...holds.values()], (hold) => Effect.exit(release(hold)), { concurrency: "unbounded" });
|
|
610
|
+
outcomes.push(yield* Effect.exit(Scope.close(owner, Exit.void)));
|
|
611
|
+
const errors = [...new Set(outcomes.flatMap((outcome) => Exit.isFailure(outcome) ? [ensureError(Cause.squash(outcome.cause))] : []))];
|
|
612
|
+
const first = errors[0];
|
|
613
|
+
if (errors.length === 1 && first !== void 0) return yield* Effect.fail(first);
|
|
614
|
+
if (errors.length > 0) return yield* Effect.fail(new AggregateError(errors, "Object facet cleanup failed"));
|
|
615
|
+
}));
|
|
616
|
+
holds.clear();
|
|
617
|
+
acquisitions.clear();
|
|
618
|
+
yield* Deferred.done(closed, result);
|
|
619
|
+
return yield* result;
|
|
620
|
+
}));
|
|
621
|
+
yield* Effect.addFinalizer(() => close.pipe(Effect.orDie));
|
|
622
|
+
const permit = (permission) => options.authorize(permission).pipe(Effect.flatMap((allowed) => allowed ? Effect.void : Effect.fail(new FacetDeniedError("Object facet access denied"))));
|
|
623
|
+
const getHold = (handle) => Effect.try({
|
|
624
|
+
try: () => {
|
|
625
|
+
const hold = holds.get(handle);
|
|
626
|
+
if (hold?.kind !== "hold" || hold.ref === void 0 || hold.closed) throw new Error("Object facet handle is unavailable");
|
|
627
|
+
return {
|
|
628
|
+
...hold,
|
|
629
|
+
ref: hold.ref
|
|
630
|
+
};
|
|
631
|
+
},
|
|
632
|
+
catch: ensureError
|
|
633
|
+
});
|
|
634
|
+
const reserve = (request) => {
|
|
635
|
+
if (!open) throw new Error("Object facet is closed");
|
|
636
|
+
const existing = acquisitions.get(request.acquisitionId);
|
|
637
|
+
if (existing !== void 0) {
|
|
638
|
+
if (existing.kind === "abandoned") throw new Error("Object acquisition has been abandoned");
|
|
639
|
+
if (existing.kind === "failed") throw new Error("Object acquisition has failed");
|
|
640
|
+
if (existing.kind === "receipt") throw new Error("Object acquisition has been released");
|
|
641
|
+
if (!isEqual(existing.address, request.address) || !isEqual(existing.contract, request.contract)) throw new Error("Object acquisition identity conflicts");
|
|
642
|
+
return existing;
|
|
643
|
+
}
|
|
644
|
+
const handle = options.createId();
|
|
645
|
+
if (handle.length === 0 || holds.has(handle)) throw new TypeError("Object facet requires unique handle identities");
|
|
646
|
+
const hold = {
|
|
647
|
+
kind: "hold",
|
|
648
|
+
handle,
|
|
649
|
+
acquisitionId: request.acquisitionId,
|
|
650
|
+
contract: request.contract,
|
|
651
|
+
scope: Scope.makeUnsafe(),
|
|
652
|
+
address: request.address,
|
|
653
|
+
ready: Deferred.makeUnsafe(),
|
|
654
|
+
closed: false,
|
|
655
|
+
started: false,
|
|
656
|
+
abandoned: false
|
|
657
|
+
};
|
|
658
|
+
holds.set(handle, hold);
|
|
659
|
+
acquisitions.set(request.acquisitionId, hold);
|
|
660
|
+
return hold;
|
|
661
|
+
};
|
|
662
|
+
const dispatch = (request, admission, reservation) => Effect.gen(function* () {
|
|
663
|
+
if (!open) return yield* Effect.fail(/* @__PURE__ */ new Error("Object facet is closed"));
|
|
664
|
+
switch (request.action) {
|
|
665
|
+
case "hello": return {
|
|
666
|
+
facetId,
|
|
667
|
+
namespace: (yield* objects.catalog()).namespace,
|
|
668
|
+
resumable
|
|
669
|
+
};
|
|
670
|
+
case "catalog": {
|
|
671
|
+
const catalog = yield* objects.catalog();
|
|
672
|
+
const types = yield* Effect.filter(catalog.types, ({ contract }) => options.authorize({
|
|
673
|
+
action: "discover",
|
|
674
|
+
contract
|
|
675
|
+
}));
|
|
676
|
+
const instances = yield* Effect.filter(catalog.instances, (address) => options.authorize({
|
|
677
|
+
action: "inspect",
|
|
678
|
+
address
|
|
679
|
+
}));
|
|
680
|
+
return captureJsonValue({
|
|
681
|
+
...catalog,
|
|
682
|
+
types,
|
|
683
|
+
instances
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
case "acquire": return yield* Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
687
|
+
const hold = reservation;
|
|
688
|
+
if (hold === void 0) return yield* Effect.fail(/* @__PURE__ */ new Error("Object acquisition has no reservation"));
|
|
689
|
+
if (hold.closed) return yield* Effect.fail(/* @__PURE__ */ new Error("Object acquisition has been released"));
|
|
690
|
+
if (hold.started) {
|
|
691
|
+
yield* restore(permit({
|
|
692
|
+
action: "acquire",
|
|
693
|
+
address: hold.address
|
|
694
|
+
}));
|
|
695
|
+
return yield* restore(Deferred.await(hold.ready));
|
|
696
|
+
}
|
|
697
|
+
hold.started = true;
|
|
698
|
+
const preparation = Effect.gen(function* () {
|
|
699
|
+
if (!open || hold.closed) return yield* Effect.fail(/* @__PURE__ */ new Error("Object acquisition is closed"));
|
|
700
|
+
yield* permit({
|
|
701
|
+
action: "acquire",
|
|
702
|
+
address: request.address
|
|
703
|
+
});
|
|
704
|
+
const catalog = yield* objects.catalog();
|
|
705
|
+
if (!open || hold.closed) return yield* Effect.fail(/* @__PURE__ */ new Error("Object acquisition is closed"));
|
|
706
|
+
if (request.address.namespace !== catalog.namespace || request.address.typeId !== request.contract.id || request.address.version !== request.contract.version || !catalog.types.some(({ contract }) => isEqual(contract, request.contract))) return yield* Effect.fail(/* @__PURE__ */ new TypeError("Remote Object contract or namespace changed"));
|
|
707
|
+
const type = {
|
|
708
|
+
id: request.contract.id,
|
|
709
|
+
version: request.contract.version,
|
|
710
|
+
key: request.contract.key,
|
|
711
|
+
methods: Object.fromEntries(Object.entries(request.contract.methods).map(([name, method]) => [name, {
|
|
712
|
+
input: method.input,
|
|
713
|
+
output: method.output
|
|
714
|
+
}]))
|
|
715
|
+
};
|
|
716
|
+
hold.ref = yield* Scope.provide(objects.acquire(type, request.address.key), hold.scope);
|
|
717
|
+
if (!open || hold.closed) return yield* Effect.fail(/* @__PURE__ */ new Error("Object facet closed during acquisition"));
|
|
718
|
+
return {
|
|
719
|
+
handle: hold.handle,
|
|
720
|
+
address: { ...hold.ref.address }
|
|
721
|
+
};
|
|
722
|
+
});
|
|
723
|
+
const settled = (admission === void 0 ? preparation : Semaphore.withPermit(admission, preparation)).pipe(Effect.onExit((result) => Effect.gen(function* () {
|
|
724
|
+
if (Exit.isFailure(result)) {
|
|
725
|
+
hold.closed = true;
|
|
726
|
+
const cleanup = yield* Effect.exit(retireScope(hold));
|
|
727
|
+
if (Exit.isFailure(cleanup)) {
|
|
728
|
+
yield* Deferred.fail(hold.ready, ensureError(Cause.squash(cleanup.cause)));
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
const receipt = acquisitions.get(hold.acquisitionId);
|
|
732
|
+
if (receipt?.kind === "receipt" && receipt.handle === hold.handle && !hold.abandoned && !receipt.abandoned) {
|
|
733
|
+
acquisitions.set(hold.acquisitionId, {
|
|
734
|
+
kind: "failed",
|
|
735
|
+
acquisitionId: hold.acquisitionId,
|
|
736
|
+
handle: hold.handle
|
|
737
|
+
});
|
|
738
|
+
if (holds.get(hold.handle) === receipt) holds.delete(hold.handle);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
yield* Deferred.done(hold.ready, result);
|
|
742
|
+
})));
|
|
743
|
+
yield* Effect.forkIn(settled, owner);
|
|
744
|
+
return yield* restore(Deferred.await(hold.ready)).pipe(Effect.onInterrupt(() => resumable ? Effect.void : release(hold)));
|
|
745
|
+
}));
|
|
746
|
+
case "lookup": {
|
|
747
|
+
if (request.facetId !== facetId) return yield* Effect.fail(/* @__PURE__ */ new Error("Object facet identity changed"));
|
|
748
|
+
const hold = acquisitions.get(request.acquisitionId);
|
|
749
|
+
if (hold === void 0) return null;
|
|
750
|
+
if (hold.kind === "abandoned") return {
|
|
751
|
+
released: true,
|
|
752
|
+
abandoned: true
|
|
753
|
+
};
|
|
754
|
+
if (hold.kind === "failed") return {
|
|
755
|
+
handle: hold.handle,
|
|
756
|
+
released: true
|
|
757
|
+
};
|
|
758
|
+
if (hold.kind === "receipt") return {
|
|
759
|
+
handle: hold.handle,
|
|
760
|
+
released: true
|
|
761
|
+
};
|
|
762
|
+
yield* permit({
|
|
763
|
+
action: "rehold",
|
|
764
|
+
address: hold.address
|
|
765
|
+
});
|
|
766
|
+
return {
|
|
767
|
+
handle: hold.handle,
|
|
768
|
+
address: { ...hold.address },
|
|
769
|
+
released: hold.closed
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
case "abandon": {
|
|
773
|
+
if (request.facetId !== facetId) return yield* Effect.fail(/* @__PURE__ */ new Error("Object facet identity changed"));
|
|
774
|
+
const hold = acquisitions.get(request.acquisitionId);
|
|
775
|
+
if (hold?.kind === "abandoned") return null;
|
|
776
|
+
if (hold?.kind === "failed") {
|
|
777
|
+
acquisitions.set(request.acquisitionId, {
|
|
778
|
+
kind: "abandoned",
|
|
779
|
+
acquisitionId: request.acquisitionId
|
|
780
|
+
});
|
|
781
|
+
return null;
|
|
782
|
+
}
|
|
783
|
+
if (hold !== void 0) {
|
|
784
|
+
hold.abandoned = true;
|
|
785
|
+
yield* release(hold);
|
|
786
|
+
}
|
|
787
|
+
if (hold === void 0 || acquisitions.has(request.acquisitionId)) acquisitions.set(request.acquisitionId, {
|
|
788
|
+
kind: "abandoned",
|
|
789
|
+
acquisitionId: request.acquisitionId
|
|
790
|
+
});
|
|
791
|
+
if (hold !== void 0) holds.delete(hold.handle);
|
|
792
|
+
return null;
|
|
793
|
+
}
|
|
794
|
+
case "rehold": {
|
|
795
|
+
if (request.facetId !== facetId) return yield* Effect.fail(/* @__PURE__ */ new Error("Object facet identity changed"));
|
|
796
|
+
const hold = yield* getHold(request.handle);
|
|
797
|
+
yield* permit({
|
|
798
|
+
action: "rehold",
|
|
799
|
+
address: hold.address
|
|
800
|
+
});
|
|
801
|
+
return {
|
|
802
|
+
handle: hold.handle,
|
|
803
|
+
address: { ...hold.address }
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
case "release": {
|
|
807
|
+
const hold = holds.get(request.handle);
|
|
808
|
+
if (hold === void 0) return null;
|
|
809
|
+
if (hold.acquisitionId !== request.acquisitionId) return yield* Effect.fail(/* @__PURE__ */ new Error("Object release identity conflicts"));
|
|
810
|
+
yield* release(hold);
|
|
811
|
+
const receipt = acquisitions.get(request.acquisitionId);
|
|
812
|
+
if (receipt?.kind === "receipt" && receipt.handle === request.handle && !hold.abandoned && !receipt.abandoned) {
|
|
813
|
+
acquisitions.delete(request.acquisitionId);
|
|
814
|
+
if (holds.get(request.handle) === receipt) holds.delete(request.handle);
|
|
815
|
+
}
|
|
816
|
+
return null;
|
|
817
|
+
}
|
|
818
|
+
case "call": {
|
|
819
|
+
const hold = yield* getHold(request.handle);
|
|
820
|
+
yield* permit({
|
|
821
|
+
action: "call",
|
|
822
|
+
address: hold.address,
|
|
823
|
+
method: request.method
|
|
824
|
+
});
|
|
825
|
+
return yield* hold.ref.call(request.method, request.input);
|
|
826
|
+
}
|
|
827
|
+
case "states": {
|
|
828
|
+
const hold = yield* getHold(request.handle);
|
|
829
|
+
yield* permit({
|
|
830
|
+
action: "observe",
|
|
831
|
+
address: hold.address
|
|
832
|
+
});
|
|
833
|
+
return captureJsonValue(yield* hold.ref.states());
|
|
834
|
+
}
|
|
835
|
+
case "watch": {
|
|
836
|
+
const hold = yield* getHold(request.handle);
|
|
837
|
+
yield* permit({
|
|
838
|
+
action: "observe",
|
|
839
|
+
address: hold.address,
|
|
840
|
+
name: request.name
|
|
841
|
+
});
|
|
842
|
+
const frame = yield* hold.ref.watchState(request.name).pipe(Stream.filter((snapshot) => request.after === void 0 || snapshot.revision > request.after), Stream.runHead);
|
|
843
|
+
yield* permit({
|
|
844
|
+
action: "observe",
|
|
845
|
+
address: hold.address,
|
|
846
|
+
name: request.name
|
|
847
|
+
});
|
|
848
|
+
return Option.isSome(frame) ? captureJsonValue(frame.value) : null;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
});
|
|
852
|
+
return Object.freeze({
|
|
853
|
+
resumable,
|
|
854
|
+
close,
|
|
855
|
+
request: (input, admission) => Effect.gen(function* () {
|
|
856
|
+
const decoded = yield* Effect.exit(Effect.try({
|
|
857
|
+
try: () => {
|
|
858
|
+
return {
|
|
859
|
+
request: input,
|
|
860
|
+
reservation: input.action === "acquire" ? reserve(input) : void 0
|
|
861
|
+
};
|
|
862
|
+
},
|
|
863
|
+
catch: ensureError
|
|
864
|
+
}));
|
|
865
|
+
if (Exit.isFailure(decoded)) return {
|
|
866
|
+
ok: false,
|
|
867
|
+
code: "invalid-request",
|
|
868
|
+
message: "Invalid Object facet request"
|
|
869
|
+
};
|
|
870
|
+
const dispatched = dispatch(decoded.value.request, admission, decoded.value.reservation);
|
|
871
|
+
const admitted = decoded.value.request.action === "acquire" || admission === void 0 ? dispatched : Semaphore.withPermit(admission, dispatched);
|
|
872
|
+
const work = yield* Effect.forkIn(admitted, owner);
|
|
873
|
+
const result = yield* Effect.exit(Fiber.join(work).pipe(Effect.onInterrupt(() => Fiber.interrupt(work))));
|
|
874
|
+
if (Exit.isSuccess(result)) return {
|
|
875
|
+
ok: true,
|
|
876
|
+
value: result.value
|
|
877
|
+
};
|
|
878
|
+
const error = ensureError(Cause.squash(result.cause));
|
|
879
|
+
if (check(ObjectInvocationUnknownSchema, error)) return {
|
|
880
|
+
ok: false,
|
|
881
|
+
code: "outcome-unknown",
|
|
882
|
+
message: error.message
|
|
883
|
+
};
|
|
884
|
+
if (check(DeniedSchema, error)) return {
|
|
885
|
+
ok: false,
|
|
886
|
+
code: "denied",
|
|
887
|
+
message: error.message
|
|
888
|
+
};
|
|
889
|
+
return {
|
|
890
|
+
ok: false,
|
|
891
|
+
code: "execution-failed",
|
|
892
|
+
message: error.message
|
|
893
|
+
};
|
|
894
|
+
})
|
|
895
|
+
});
|
|
896
|
+
});
|
|
897
|
+
|
|
898
|
+
//#endregion
|
|
899
|
+
export { ObjectFacetRecoveryPendingError, ObjectFacetUnavailableError, ObjectInvocationUnknownError, decodeObjectFacetReply, decodeObjectFacetRequest, makeObjectFacet, makeRemoteObjectClient, makeResumableRemoteObjectClient };
|