@forgeax/engine-host 0.1.27
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 +32 -0
- package/dist/__tests__/host.test.d.ts +2 -0
- package/dist/__tests__/host.test.d.ts.map +1 -0
- package/dist/backend.d.ts +46 -0
- package/dist/backend.d.ts.map +1 -0
- package/dist/backend.mjs +797 -0
- package/dist/backend.mjs.map +1 -0
- package/dist/frontend.d.ts +50 -0
- package/dist/frontend.d.ts.map +1 -0
- package/dist/frontend.mjs +715 -0
- package/dist/frontend.mjs.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +1473 -0
- package/dist/index.mjs.map +1 -0
- package/dist/protocol.d.ts +153 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.mjs +351 -0
- package/dist/protocol.mjs.map +1 -0
- package/dist/transport.d.ts +64 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.mjs +575 -0
- package/dist/transport.mjs.map +1 -0
- package/package.json +88 -0
- package/src/__tests__/host.test.ts +510 -0
- package/src/backend.ts +342 -0
- package/src/frontend.ts +508 -0
- package/src/index.ts +54 -0
- package/src/protocol.ts +586 -0
- package/src/transport.ts +717 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1473 @@
|
|
|
1
|
+
import { Context, inspectCatalogPlugins } from '@forgeax/engine-plugin';
|
|
2
|
+
import { bootstrapCatalogLoader, projectPluginEntries, installCatalogLoader } from '@forgeax/engine-plugin/loader';
|
|
3
|
+
|
|
4
|
+
// src/backend.ts
|
|
5
|
+
|
|
6
|
+
// src/protocol.ts
|
|
7
|
+
var HOST_ASSEMBLY_SCHEMA_VERSION = 1;
|
|
8
|
+
var HostAssemblyError = class extends Error {
|
|
9
|
+
code;
|
|
10
|
+
expected;
|
|
11
|
+
hint;
|
|
12
|
+
detail;
|
|
13
|
+
constructor(code, expected, hint, detail) {
|
|
14
|
+
super(`${code}: ${expected}`);
|
|
15
|
+
this.name = "HostAssemblyError";
|
|
16
|
+
this.code = code;
|
|
17
|
+
this.expected = expected;
|
|
18
|
+
this.hint = hint;
|
|
19
|
+
this.detail = detail;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
function assertHostModuleCatalogIdentity(module, record) {
|
|
23
|
+
if (record.version !== void 0 && record.version !== module.version) {
|
|
24
|
+
throw new HostAssemblyError(
|
|
25
|
+
"host-assembly-module-version-mismatch",
|
|
26
|
+
`module ${module.name} to load catalog version ${module.version}`,
|
|
27
|
+
"Regenerate the static Catalog from the same backend package revision.",
|
|
28
|
+
{ name: module.name, actual: record.version, expected: module.version }
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
if (record.digest !== void 0 && module.digest !== void 0 && record.digest !== module.digest) {
|
|
32
|
+
throw new HostAssemblyError(
|
|
33
|
+
"host-assembly-module-version-mismatch",
|
|
34
|
+
`module ${module.name} to load catalog digest ${module.digest ?? "none"}`,
|
|
35
|
+
"Regenerate the static Catalog from the same backend package bytes.",
|
|
36
|
+
{ name: module.name, actual: record.digest, expected: module.digest ?? "none" }
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const versionMatches = record.version !== void 0 && record.version === module.version;
|
|
40
|
+
const digestMatches = module.digest !== void 0 && record.digest !== void 0 && record.digest === module.digest;
|
|
41
|
+
const staticWithoutIdentity = module.version === "static" && module.digest === void 0 && record.version === void 0 && record.digest === void 0;
|
|
42
|
+
if (!versionMatches && !digestMatches && !staticWithoutIdentity) {
|
|
43
|
+
throw new HostAssemblyError(
|
|
44
|
+
"host-assembly-module-version-mismatch",
|
|
45
|
+
`module ${module.name} to have a matching catalog code identity for ${module.version}`,
|
|
46
|
+
"Add the generated module version or digest to the static Catalog.",
|
|
47
|
+
{
|
|
48
|
+
name: module.name,
|
|
49
|
+
actual: record.version ?? record.digest ?? "unknown",
|
|
50
|
+
expected: module.version
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function canonicalHostJson(value) {
|
|
56
|
+
if (value === void 0) return "undefined";
|
|
57
|
+
if (value === null || typeof value !== "object") return JSON.stringify(value);
|
|
58
|
+
if (Array.isArray(value)) return `[${value.map(canonicalHostJson).join(",")}]`;
|
|
59
|
+
return `{${Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, item]) => `${JSON.stringify(key)}:${canonicalHostJson(item)}`).join(",")}}`;
|
|
60
|
+
}
|
|
61
|
+
function hostRevision(value) {
|
|
62
|
+
let hash = 2166136261;
|
|
63
|
+
for (const char of canonicalHostJson(value)) {
|
|
64
|
+
hash ^= char.codePointAt(0) ?? 0;
|
|
65
|
+
hash = Math.imul(hash, 16777619);
|
|
66
|
+
}
|
|
67
|
+
return `fnv1a:${(hash >>> 0).toString(16).padStart(8, "0")}`;
|
|
68
|
+
}
|
|
69
|
+
function cloneEntry(entry) {
|
|
70
|
+
const config = entry.group ? entry.config?.map(cloneEntry) : entry.config;
|
|
71
|
+
return {
|
|
72
|
+
id: entry.id,
|
|
73
|
+
name: entry.name,
|
|
74
|
+
...config === void 0 ? {} : { config },
|
|
75
|
+
...entry.group === void 0 ? {} : { group: entry.group },
|
|
76
|
+
...entry.disabled === void 0 ? {} : { disabled: entry.disabled },
|
|
77
|
+
...entry.inject === void 0 ? {} : { inject: entry.inject },
|
|
78
|
+
...entry.realm === void 0 ? {} : { realm: entry.realm }
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function projectPairs(pairs) {
|
|
82
|
+
const entries = [];
|
|
83
|
+
const modules = [];
|
|
84
|
+
const projections = [];
|
|
85
|
+
for (const pair of pairs) {
|
|
86
|
+
if (pair.backend !== void 0 && pair.frontend !== void 0 && pair.backend.module.version !== pair.frontend.module.version) {
|
|
87
|
+
throw new HostAssemblyError(
|
|
88
|
+
"host-assembly-module-version-mismatch",
|
|
89
|
+
`paired module ${pair.id} to use one code version on both hosts`,
|
|
90
|
+
"Resolve backend and frontend package entries from the same locked package revision.",
|
|
91
|
+
{
|
|
92
|
+
name: pair.id,
|
|
93
|
+
actual: pair.backend.module.version,
|
|
94
|
+
expected: pair.frontend.module.version
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
if (pair.frontend === void 0) continue;
|
|
99
|
+
entries.push(pair.frontend.entry);
|
|
100
|
+
modules.push(pair.frontend.module);
|
|
101
|
+
projections.push({
|
|
102
|
+
id: pair.id,
|
|
103
|
+
entryId: pair.frontend.entry.id,
|
|
104
|
+
module: pair.frontend.module
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return { entries, modules, projections };
|
|
108
|
+
}
|
|
109
|
+
function assemblyPairsFromInput(input) {
|
|
110
|
+
if (input.pairs !== void 0) {
|
|
111
|
+
const projected = projectPairs(input.pairs);
|
|
112
|
+
return {
|
|
113
|
+
entries: input.entries ?? projected.entries,
|
|
114
|
+
modules: input.modules ?? projected.modules,
|
|
115
|
+
pairs: projected.projections
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const entries = input.entries ?? [];
|
|
119
|
+
const modules = [...input.modules ?? []];
|
|
120
|
+
for (const [index, entry] of entries.entries()) {
|
|
121
|
+
if (modules[index] !== void 0 || modules.some((module) => module.name === entry.name))
|
|
122
|
+
continue;
|
|
123
|
+
modules.push({
|
|
124
|
+
name: entry.name,
|
|
125
|
+
realm: entry.realm ?? "engine",
|
|
126
|
+
version: "unknown"
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
entries,
|
|
131
|
+
modules,
|
|
132
|
+
pairs: entries.map((entry, index) => ({
|
|
133
|
+
id: entry.id,
|
|
134
|
+
entryId: entry.id,
|
|
135
|
+
module: modules[index]?.name === entry.name ? modules[index] : modules.find((module) => module.name === entry.name) ?? {
|
|
136
|
+
name: entry.name,
|
|
137
|
+
realm: entry.realm ?? "engine",
|
|
138
|
+
version: "unknown"
|
|
139
|
+
}
|
|
140
|
+
}))
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function createHostAssembly(input) {
|
|
144
|
+
const projected = assemblyPairsFromInput(input);
|
|
145
|
+
const entries = projected.entries.map(cloneEntry);
|
|
146
|
+
const modules = projected.modules.map((module) => ({ ...module }));
|
|
147
|
+
const pairs = projected.pairs.map((pair) => ({ ...pair, module: { ...pair.module } }));
|
|
148
|
+
const identity = {
|
|
149
|
+
schemaVersion: HOST_ASSEMBLY_SCHEMA_VERSION,
|
|
150
|
+
entries,
|
|
151
|
+
modules,
|
|
152
|
+
pairs,
|
|
153
|
+
...input.config === void 0 ? {} : { config: input.config }
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
...identity,
|
|
157
|
+
revision: hostRevision(identity)
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
function validateEntry(entry, path) {
|
|
161
|
+
if (entry === null || typeof entry !== "object") return `${path} must be an Entry object`;
|
|
162
|
+
if (typeof entry.id !== "string" || entry.id.length === 0) return `${path}.id must be non-empty`;
|
|
163
|
+
if (typeof entry.name !== "string" || entry.name.length === 0)
|
|
164
|
+
return `${path}.name must be non-empty`;
|
|
165
|
+
if (entry.group === true) {
|
|
166
|
+
if (!Array.isArray(entry.config)) return `${path}.config must be an Entry array for a Group`;
|
|
167
|
+
for (const [index, child] of entry.config.entries()) {
|
|
168
|
+
const reason = validateEntry(child, `${path}.config[${index}]`);
|
|
169
|
+
if (reason !== void 0) return reason;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return void 0;
|
|
173
|
+
}
|
|
174
|
+
function validateHostAssembly(assembly) {
|
|
175
|
+
if (assembly === null || typeof assembly !== "object") {
|
|
176
|
+
return {
|
|
177
|
+
ok: false,
|
|
178
|
+
error: new HostAssemblyError(
|
|
179
|
+
"host-assembly-invalid",
|
|
180
|
+
"assembly to be an object",
|
|
181
|
+
"Regenerate the frontend assembly from the active backend authority.",
|
|
182
|
+
{ reason: "assembly is not an object" }
|
|
183
|
+
)
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
if (assembly.schemaVersion !== HOST_ASSEMBLY_SCHEMA_VERSION) {
|
|
187
|
+
return {
|
|
188
|
+
ok: false,
|
|
189
|
+
error: new HostAssemblyError(
|
|
190
|
+
"host-assembly-invalid",
|
|
191
|
+
`assembly schema ${HOST_ASSEMBLY_SCHEMA_VERSION}`,
|
|
192
|
+
"Regenerate the frontend assembly with the matching Engine host package.",
|
|
193
|
+
{ reason: `unsupported schema ${String(assembly.schemaVersion)}` }
|
|
194
|
+
)
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
if (!Array.isArray(assembly.entries) || !Array.isArray(assembly.modules)) {
|
|
198
|
+
return {
|
|
199
|
+
ok: false,
|
|
200
|
+
error: new HostAssemblyError(
|
|
201
|
+
"host-assembly-invalid",
|
|
202
|
+
"assembly entries and modules to be arrays",
|
|
203
|
+
"Regenerate the frontend assembly from the active backend authority.",
|
|
204
|
+
{ reason: "entries or modules is not an array" }
|
|
205
|
+
)
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (!Array.isArray(assembly.pairs)) {
|
|
209
|
+
return {
|
|
210
|
+
ok: false,
|
|
211
|
+
error: new HostAssemblyError(
|
|
212
|
+
"host-assembly-invalid",
|
|
213
|
+
"assembly pairs to be an array",
|
|
214
|
+
"Regenerate the assembly from the backend host using the matching host package.",
|
|
215
|
+
{ reason: "pairs is not an array" }
|
|
216
|
+
)
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
const seen = /* @__PURE__ */ new Set();
|
|
220
|
+
for (const [index, entry] of assembly.entries.entries()) {
|
|
221
|
+
const reason = validateEntry(entry, `entries[${index}]`);
|
|
222
|
+
if (reason !== void 0) {
|
|
223
|
+
return {
|
|
224
|
+
ok: false,
|
|
225
|
+
error: new HostAssemblyError(
|
|
226
|
+
"host-assembly-invalid",
|
|
227
|
+
"all assembly entries to be valid native EntryOptions",
|
|
228
|
+
"Repair the backend Entry projection before publishing it to a browser.",
|
|
229
|
+
{ reason }
|
|
230
|
+
)
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
if (seen.has(entry.id)) {
|
|
234
|
+
return {
|
|
235
|
+
ok: false,
|
|
236
|
+
error: new HostAssemblyError(
|
|
237
|
+
"host-assembly-invalid",
|
|
238
|
+
"assembly Entry ids to be unique",
|
|
239
|
+
"Give repeated plugin instances independent stable ids.",
|
|
240
|
+
{ reason: `duplicate Entry id ${entry.id}` }
|
|
241
|
+
)
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
seen.add(entry.id);
|
|
245
|
+
}
|
|
246
|
+
const seenModules = /* @__PURE__ */ new Map();
|
|
247
|
+
for (const [index, module] of assembly.modules.entries()) {
|
|
248
|
+
if (module === null || typeof module !== "object" || typeof module.name !== "string" || typeof module.realm !== "string" || typeof module.version !== "string" || module.name.length === 0 || module.version.length === 0 || module.url !== void 0 && typeof module.url !== "string" || module.digest !== void 0 && (typeof module.digest !== "string" || module.digest.length === 0)) {
|
|
249
|
+
return {
|
|
250
|
+
ok: false,
|
|
251
|
+
error: new HostAssemblyError(
|
|
252
|
+
"host-assembly-invalid",
|
|
253
|
+
"module names and versions to be non-empty",
|
|
254
|
+
"Regenerate the module projection from the resolved package metadata.",
|
|
255
|
+
{ reason: `invalid module at index ${index}` }
|
|
256
|
+
)
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
const previousModule = seenModules.get(module.name);
|
|
260
|
+
if (previousModule !== void 0 && (previousModule.realm !== module.realm || previousModule.version !== module.version || previousModule.url !== module.url || previousModule.digest !== module.digest)) {
|
|
261
|
+
return {
|
|
262
|
+
ok: false,
|
|
263
|
+
error: new HostAssemblyError(
|
|
264
|
+
"host-assembly-invalid",
|
|
265
|
+
"assembly module identity to be consistent for repeated Entries",
|
|
266
|
+
"Reuse one resolved module identity when a package has multiple Entry instances.",
|
|
267
|
+
{ reason: `module ${module.name} has conflicting identities` }
|
|
268
|
+
)
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
seenModules.set(module.name, module);
|
|
272
|
+
}
|
|
273
|
+
const seenPairIds = /* @__PURE__ */ new Set();
|
|
274
|
+
for (const [index, pair] of assembly.pairs.entries()) {
|
|
275
|
+
const pairModule = pair?.module;
|
|
276
|
+
if (pair === null || typeof pair !== "object" || typeof pair.id !== "string" || pair.id.length === 0 || typeof pair.entryId !== "string" || pair.entryId.length === 0 || pairModule === null || pairModule === void 0 || typeof pairModule !== "object" || typeof pairModule.name !== "string" || typeof pairModule.realm !== "string" || typeof pairModule.version !== "string" || pairModule.name.length === 0 || pairModule.version.length === 0 || pairModule.url !== void 0 && typeof pairModule.url !== "string" || pairModule.digest !== void 0 && (typeof pairModule.digest !== "string" || pairModule.digest.length === 0)) {
|
|
277
|
+
return {
|
|
278
|
+
ok: false,
|
|
279
|
+
error: new HostAssemblyError(
|
|
280
|
+
"host-assembly-invalid",
|
|
281
|
+
"assembly pair identities and modules to be valid",
|
|
282
|
+
"Regenerate paired frontend entries from the backend package manifest.",
|
|
283
|
+
{ reason: `invalid pair at index ${index}` }
|
|
284
|
+
)
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
if (seenPairIds.has(pair.id)) {
|
|
288
|
+
return {
|
|
289
|
+
ok: false,
|
|
290
|
+
error: new HostAssemblyError(
|
|
291
|
+
"host-assembly-invalid",
|
|
292
|
+
"assembly pair ids to be unique",
|
|
293
|
+
"Give each paired package instance an independent stable id.",
|
|
294
|
+
{ reason: `duplicate pair ${pair.id}` }
|
|
295
|
+
)
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
seenPairIds.add(pair.id);
|
|
299
|
+
if (!assembly.entries.some((entry) => entry.id === pair.entryId)) {
|
|
300
|
+
return {
|
|
301
|
+
ok: false,
|
|
302
|
+
error: new HostAssemblyError(
|
|
303
|
+
"host-assembly-invalid",
|
|
304
|
+
`pair ${pair.id} to reference an assembly Entry`,
|
|
305
|
+
"Keep paired identity and frontend Entry projection under one backend authority.",
|
|
306
|
+
{ reason: `missing frontend Entry ${pair.entryId}` }
|
|
307
|
+
)
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
const module = assembly.modules.find(
|
|
311
|
+
(candidate) => candidate.name === pairModule.name && candidate.realm === pairModule.realm && candidate.version === pairModule.version && candidate.url === pairModule.url && candidate.digest === pairModule.digest
|
|
312
|
+
);
|
|
313
|
+
if (module === void 0) {
|
|
314
|
+
return {
|
|
315
|
+
ok: false,
|
|
316
|
+
error: new HostAssemblyError(
|
|
317
|
+
"host-assembly-invalid",
|
|
318
|
+
`pair ${pair.id} to reference a resolved frontend module`,
|
|
319
|
+
"Keep module/code identity in the backend-derived assembly projection.",
|
|
320
|
+
{ reason: `missing frontend module ${pairModule.name}` }
|
|
321
|
+
)
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
const expected = hostRevision({
|
|
326
|
+
schemaVersion: assembly.schemaVersion,
|
|
327
|
+
entries: assembly.entries,
|
|
328
|
+
modules: assembly.modules,
|
|
329
|
+
pairs: assembly.pairs,
|
|
330
|
+
...assembly.config === void 0 ? {} : { config: assembly.config }
|
|
331
|
+
});
|
|
332
|
+
if (expected !== assembly.revision) {
|
|
333
|
+
return {
|
|
334
|
+
ok: false,
|
|
335
|
+
error: new HostAssemblyError(
|
|
336
|
+
"host-assembly-revision-mismatch",
|
|
337
|
+
"assembly revision to match its entries and modules",
|
|
338
|
+
"Discard the stale response and request the current backend assembly again.",
|
|
339
|
+
{ actual: assembly.revision, expected }
|
|
340
|
+
)
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
return { ok: true, value: assembly };
|
|
344
|
+
}
|
|
345
|
+
function modulesFromCatalog(catalog, realm, version = "static") {
|
|
346
|
+
return [...catalog.entries()].filter(([, record]) => record.realm === realm).map(([name, record]) => ({
|
|
347
|
+
name,
|
|
348
|
+
realm,
|
|
349
|
+
version: record.version ?? version,
|
|
350
|
+
...record.digest === void 0 ? {} : { digest: record.digest }
|
|
351
|
+
}));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// src/transport.ts
|
|
355
|
+
var HOST_ASSEMBLY_SERVICE = "host/assembly.get";
|
|
356
|
+
var HOST_ACTIVATION_REPORT_SERVICE = "host/assembly.report";
|
|
357
|
+
var HOST_ASSEMBLY_CHANGED_TOPIC = "host/assembly.changed";
|
|
358
|
+
function createHostTransport() {
|
|
359
|
+
const services = /* @__PURE__ */ new Map();
|
|
360
|
+
const generations = /* @__PURE__ */ new Map();
|
|
361
|
+
const clients = /* @__PURE__ */ new Set();
|
|
362
|
+
let closed = false;
|
|
363
|
+
const closeClient = (state, reason) => {
|
|
364
|
+
if (!state.connected) return;
|
|
365
|
+
state.connected = false;
|
|
366
|
+
for (const controller of state.pending) controller.abort();
|
|
367
|
+
state.pending.clear();
|
|
368
|
+
for (const callbacks of state.subscriptions.values()) callbacks.clear();
|
|
369
|
+
state.subscriptions.clear();
|
|
370
|
+
clients.delete(state);
|
|
371
|
+
const error = new HostAssemblyError(
|
|
372
|
+
"host-transport-failure",
|
|
373
|
+
"the host connection to remain available",
|
|
374
|
+
"Reconnect the frontend host before issuing another request.",
|
|
375
|
+
{ service: "host/socket", reason: String(reason ?? "host connection closed") }
|
|
376
|
+
);
|
|
377
|
+
for (const listener of state.disconnectListeners) listener(error);
|
|
378
|
+
state.disconnectListeners.clear();
|
|
379
|
+
};
|
|
380
|
+
const server = {
|
|
381
|
+
get connectedClients() {
|
|
382
|
+
return clients.size;
|
|
383
|
+
},
|
|
384
|
+
register(service, handler) {
|
|
385
|
+
if (closed) throw new Error("host transport is closed");
|
|
386
|
+
const previous = services.get(service);
|
|
387
|
+
const generation = (generations.get(service) ?? 0) + 1;
|
|
388
|
+
generations.set(service, generation);
|
|
389
|
+
for (const controller of previous?.pending ?? []) controller.abort();
|
|
390
|
+
const identity = Symbol(service);
|
|
391
|
+
services.set(service, {
|
|
392
|
+
identity,
|
|
393
|
+
generation,
|
|
394
|
+
handler,
|
|
395
|
+
pending: /* @__PURE__ */ new Set()
|
|
396
|
+
});
|
|
397
|
+
return () => {
|
|
398
|
+
const current = services.get(service);
|
|
399
|
+
if (current?.identity !== identity) return;
|
|
400
|
+
for (const controller of current.pending) controller.abort();
|
|
401
|
+
generations.set(service, current.generation + 1);
|
|
402
|
+
services.delete(service);
|
|
403
|
+
};
|
|
404
|
+
},
|
|
405
|
+
invalidate(service) {
|
|
406
|
+
const current = services.get(service);
|
|
407
|
+
if (current === void 0) return;
|
|
408
|
+
for (const controller of current.pending) controller.abort();
|
|
409
|
+
generations.set(service, current.generation + 1);
|
|
410
|
+
services.delete(service);
|
|
411
|
+
},
|
|
412
|
+
snapshot(service) {
|
|
413
|
+
const current = services.get(service);
|
|
414
|
+
return current === void 0 ? void 0 : { service, generation: current.generation };
|
|
415
|
+
},
|
|
416
|
+
publish(topic, payload) {
|
|
417
|
+
for (const state of clients) {
|
|
418
|
+
for (const listener of state.subscriptions.get(topic) ?? []) listener(payload);
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
connect() {
|
|
422
|
+
if (closed) throw new Error("host transport is closed");
|
|
423
|
+
const state = {
|
|
424
|
+
connected: true,
|
|
425
|
+
subscriptions: /* @__PURE__ */ new Map(),
|
|
426
|
+
pending: /* @__PURE__ */ new Set(),
|
|
427
|
+
disconnectListeners: /* @__PURE__ */ new Set()
|
|
428
|
+
};
|
|
429
|
+
clients.add(state);
|
|
430
|
+
const client = {
|
|
431
|
+
get connected() {
|
|
432
|
+
return state.connected;
|
|
433
|
+
},
|
|
434
|
+
get generation() {
|
|
435
|
+
let current = 0;
|
|
436
|
+
for (const service of services.values()) current = Math.max(current, service.generation);
|
|
437
|
+
return current;
|
|
438
|
+
},
|
|
439
|
+
async request(service, payload, options = {}) {
|
|
440
|
+
if (!state.connected) {
|
|
441
|
+
throw new HostAssemblyError(
|
|
442
|
+
"host-assembly-service-unavailable",
|
|
443
|
+
`service ${service} to be available on the current connection`,
|
|
444
|
+
"Reconnect the frontend host before issuing another request.",
|
|
445
|
+
{ service }
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
if (options.signal?.aborted) {
|
|
449
|
+
throw new HostAssemblyError(
|
|
450
|
+
"host-assembly-request-aborted",
|
|
451
|
+
`request ${service} to start with a live AbortSignal`,
|
|
452
|
+
"Start a fresh request with a live AbortSignal.",
|
|
453
|
+
{ service }
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
const record = services.get(service);
|
|
457
|
+
if (record === void 0) {
|
|
458
|
+
const invalidatedGeneration = generations.get(service);
|
|
459
|
+
if (options.generation !== void 0 && invalidatedGeneration !== void 0 && options.generation < invalidatedGeneration) {
|
|
460
|
+
throw new HostAssemblyError(
|
|
461
|
+
"host-assembly-stale-request",
|
|
462
|
+
`request generation ${options.generation} to match the invalidated service ${service}`,
|
|
463
|
+
"Refresh the client capability after the backend service is enabled again.",
|
|
464
|
+
{ service, generation: options.generation }
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
throw new HostAssemblyError(
|
|
468
|
+
"host-assembly-service-unavailable",
|
|
469
|
+
`service ${service} to be registered`,
|
|
470
|
+
"Wait for the backend plugin to activate before using this capability.",
|
|
471
|
+
{ service }
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
const generation = options.generation ?? record.generation;
|
|
475
|
+
if (generation !== record.generation) {
|
|
476
|
+
throw new HostAssemblyError(
|
|
477
|
+
"host-assembly-stale-request",
|
|
478
|
+
`request generation ${generation} to match service ${service}`,
|
|
479
|
+
"Refresh the client capability and retry only when the owning business contract permits it.",
|
|
480
|
+
{ service, generation }
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
const controller = new AbortController();
|
|
484
|
+
const abortFromCaller = () => controller.abort();
|
|
485
|
+
options.signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
486
|
+
record.pending.add(controller);
|
|
487
|
+
state.pending.add(controller);
|
|
488
|
+
let removeAbortRequest;
|
|
489
|
+
try {
|
|
490
|
+
if (controller.signal.aborted) {
|
|
491
|
+
throw new HostAssemblyError(
|
|
492
|
+
"host-assembly-request-aborted",
|
|
493
|
+
`request ${service} not to be aborted before execution`,
|
|
494
|
+
"Start a fresh request with a live AbortSignal.",
|
|
495
|
+
{ service }
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
let abortReject;
|
|
499
|
+
const aborted = new Promise((_, reject) => {
|
|
500
|
+
abortReject = reject;
|
|
501
|
+
});
|
|
502
|
+
const abortRequest = () => {
|
|
503
|
+
abortReject?.(
|
|
504
|
+
new HostAssemblyError(
|
|
505
|
+
"host-assembly-request-aborted",
|
|
506
|
+
`request ${service} to finish before its service is closed or invalidated`,
|
|
507
|
+
"Treat the capability as withdrawn and refresh the service before retrying.",
|
|
508
|
+
{ service }
|
|
509
|
+
)
|
|
510
|
+
);
|
|
511
|
+
};
|
|
512
|
+
controller.signal.addEventListener("abort", abortRequest, { once: true });
|
|
513
|
+
removeAbortRequest = () => controller.signal.removeEventListener("abort", abortRequest);
|
|
514
|
+
const result = await Promise.race([
|
|
515
|
+
Promise.resolve(
|
|
516
|
+
record.handler({
|
|
517
|
+
service,
|
|
518
|
+
payload,
|
|
519
|
+
generation,
|
|
520
|
+
signal: controller.signal
|
|
521
|
+
})
|
|
522
|
+
),
|
|
523
|
+
aborted
|
|
524
|
+
]);
|
|
525
|
+
if (controller.signal.aborted) {
|
|
526
|
+
throw new HostAssemblyError(
|
|
527
|
+
"host-assembly-request-aborted",
|
|
528
|
+
`request ${service} to finish before its service is invalidated`,
|
|
529
|
+
"Treat the capability as withdrawn and refresh the service before retrying.",
|
|
530
|
+
{ service }
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
return result;
|
|
534
|
+
} finally {
|
|
535
|
+
removeAbortRequest?.();
|
|
536
|
+
record.pending.delete(controller);
|
|
537
|
+
state.pending.delete(controller);
|
|
538
|
+
options.signal?.removeEventListener("abort", abortFromCaller);
|
|
539
|
+
}
|
|
540
|
+
},
|
|
541
|
+
subscribe(topic, listener) {
|
|
542
|
+
if (!state.connected) return () => {
|
|
543
|
+
};
|
|
544
|
+
const callbacks = state.subscriptions.get(topic) ?? /* @__PURE__ */ new Set();
|
|
545
|
+
callbacks.add(listener);
|
|
546
|
+
state.subscriptions.set(topic, callbacks);
|
|
547
|
+
return () => {
|
|
548
|
+
callbacks.delete(listener);
|
|
549
|
+
if (callbacks.size === 0) state.subscriptions.delete(topic);
|
|
550
|
+
};
|
|
551
|
+
},
|
|
552
|
+
onDisconnect(listener) {
|
|
553
|
+
if (!state.connected) {
|
|
554
|
+
listener(
|
|
555
|
+
new HostAssemblyError(
|
|
556
|
+
"host-transport-failure",
|
|
557
|
+
"the host connection to remain available",
|
|
558
|
+
"Reconnect the frontend host before issuing another request.",
|
|
559
|
+
{ service: "host/socket", reason: "host connection is already closed" }
|
|
560
|
+
)
|
|
561
|
+
);
|
|
562
|
+
return () => {
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
state.disconnectListeners.add(listener);
|
|
566
|
+
return () => state.disconnectListeners.delete(listener);
|
|
567
|
+
},
|
|
568
|
+
close(reason) {
|
|
569
|
+
closeClient(state, reason);
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
return client;
|
|
573
|
+
},
|
|
574
|
+
close(reason) {
|
|
575
|
+
if (closed) return;
|
|
576
|
+
closed = true;
|
|
577
|
+
for (const service of services.values())
|
|
578
|
+
for (const controller of service.pending) controller.abort();
|
|
579
|
+
services.clear();
|
|
580
|
+
for (const client of [...clients]) closeClient(client, reason);
|
|
581
|
+
generations.clear();
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
return server;
|
|
585
|
+
}
|
|
586
|
+
function addSocketListener(socket, type, listener) {
|
|
587
|
+
if (socket.addEventListener !== void 0) {
|
|
588
|
+
socket.addEventListener(type, listener);
|
|
589
|
+
return () => socket.removeEventListener?.(type, listener);
|
|
590
|
+
}
|
|
591
|
+
socket.on?.(type, listener);
|
|
592
|
+
return () => socket.off?.(type, listener);
|
|
593
|
+
}
|
|
594
|
+
function socketPayload(value) {
|
|
595
|
+
const data = Array.isArray(value) ? value[0] : value;
|
|
596
|
+
if (typeof data === "string") return data;
|
|
597
|
+
if (data instanceof ArrayBuffer) return new TextDecoder().decode(data);
|
|
598
|
+
if (data instanceof Uint8Array) return new TextDecoder().decode(data);
|
|
599
|
+
if (data !== null && typeof data === "object" && "data" in data)
|
|
600
|
+
return socketPayload(data.data);
|
|
601
|
+
return void 0;
|
|
602
|
+
}
|
|
603
|
+
function parseWire(value) {
|
|
604
|
+
const source = socketPayload(value);
|
|
605
|
+
if (source === void 0) return void 0;
|
|
606
|
+
try {
|
|
607
|
+
const parsed = JSON.parse(source);
|
|
608
|
+
return parsed !== null && typeof parsed === "object" && typeof parsed.kind === "string" ? parsed : void 0;
|
|
609
|
+
} catch {
|
|
610
|
+
return void 0;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
function serializeError(service, error) {
|
|
614
|
+
if (error !== null && typeof error === "object" && typeof error.code === "string" && typeof error.expected === "string" && typeof error.hint === "string") {
|
|
615
|
+
const detail = error.detail;
|
|
616
|
+
return {
|
|
617
|
+
code: error.code,
|
|
618
|
+
expected: error.expected,
|
|
619
|
+
hint: error.hint,
|
|
620
|
+
detail: detail !== null && typeof detail === "object" ? detail : { reason: String(detail ?? "unknown failure") }
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
return {
|
|
624
|
+
code: "host-transport-failure",
|
|
625
|
+
expected: `service ${service} to complete without an exception`,
|
|
626
|
+
hint: "Inspect the backend host process and reconnect before retrying.",
|
|
627
|
+
detail: { service, reason: error instanceof Error ? error.message : String(error) }
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
function errorFromSummary(service, summary) {
|
|
631
|
+
const detail = summary.detail;
|
|
632
|
+
const supported = /* @__PURE__ */ new Set([
|
|
633
|
+
"host-assembly-invalid",
|
|
634
|
+
"host-assembly-revision-mismatch",
|
|
635
|
+
"host-assembly-module-missing",
|
|
636
|
+
"host-assembly-module-version-mismatch",
|
|
637
|
+
"host-assembly-reload-required",
|
|
638
|
+
"host-assembly-service-unavailable",
|
|
639
|
+
"host-assembly-stale-request",
|
|
640
|
+
"host-assembly-request-aborted",
|
|
641
|
+
"host-assembly-not-ready",
|
|
642
|
+
"host-transport-failure"
|
|
643
|
+
]);
|
|
644
|
+
if (supported.has(summary.code)) {
|
|
645
|
+
return new HostAssemblyError(
|
|
646
|
+
summary.code,
|
|
647
|
+
summary.expected,
|
|
648
|
+
summary.hint,
|
|
649
|
+
detail
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
return new HostAssemblyError("host-transport-failure", summary.expected, summary.hint, {
|
|
653
|
+
service,
|
|
654
|
+
reason: summary.detail.reason ? String(summary.detail.reason) : summary.code
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
function transportFailure(service, reason) {
|
|
658
|
+
return new HostAssemblyError(
|
|
659
|
+
"host-transport-failure",
|
|
660
|
+
`service ${service} to remain connected`,
|
|
661
|
+
"Reconnect the frontend host and inspect the backend transport diagnostics.",
|
|
662
|
+
{ service, reason }
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
function sendWire(socket, message) {
|
|
666
|
+
socket.send(JSON.stringify(message));
|
|
667
|
+
}
|
|
668
|
+
async function createHostWebSocketClient(socket) {
|
|
669
|
+
if (socket.readyState !== void 0 && socket.readyState !== 1) {
|
|
670
|
+
await new Promise((resolve, reject) => {
|
|
671
|
+
const removeOpen = addSocketListener(socket, "open", () => {
|
|
672
|
+
removeOpen();
|
|
673
|
+
removeError2();
|
|
674
|
+
resolve();
|
|
675
|
+
});
|
|
676
|
+
const removeError2 = addSocketListener(socket, "error", (cause) => {
|
|
677
|
+
removeOpen();
|
|
678
|
+
removeError2();
|
|
679
|
+
reject(transportFailure("host/connect", String(cause ?? "socket error")));
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
const pending = /* @__PURE__ */ new Map();
|
|
684
|
+
const subscriptions = /* @__PURE__ */ new Map();
|
|
685
|
+
const disconnectListeners = /* @__PURE__ */ new Set();
|
|
686
|
+
let connected = true;
|
|
687
|
+
let generation = 0;
|
|
688
|
+
let sequence = 0;
|
|
689
|
+
const disconnectError = (reason) => transportFailure("host/socket", reason instanceof Error ? reason.message : String(reason));
|
|
690
|
+
const failPending = (reason) => {
|
|
691
|
+
if (!connected) return;
|
|
692
|
+
connected = false;
|
|
693
|
+
for (const request of pending.values()) {
|
|
694
|
+
request.cleanup();
|
|
695
|
+
request.reject(reason);
|
|
696
|
+
}
|
|
697
|
+
pending.clear();
|
|
698
|
+
subscriptions.clear();
|
|
699
|
+
const error = disconnectError(reason);
|
|
700
|
+
for (const listener of disconnectListeners) listener(error);
|
|
701
|
+
disconnectListeners.clear();
|
|
702
|
+
};
|
|
703
|
+
const removeMessage = addSocketListener(socket, "message", (event) => {
|
|
704
|
+
const message = parseWire(event);
|
|
705
|
+
if (message === void 0) return;
|
|
706
|
+
if (message.kind === "event") {
|
|
707
|
+
for (const listener of subscriptions.get(message.topic) ?? []) listener(message.payload);
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
if (message.kind !== "response") return;
|
|
711
|
+
if (message.generation !== void 0) generation = Math.max(generation, message.generation);
|
|
712
|
+
const request = pending.get(message.id);
|
|
713
|
+
if (request === void 0) return;
|
|
714
|
+
pending.delete(message.id);
|
|
715
|
+
request.cleanup();
|
|
716
|
+
if (message.ok) request.resolve(message.value);
|
|
717
|
+
else
|
|
718
|
+
request.reject(
|
|
719
|
+
errorFromSummary(
|
|
720
|
+
"host/socket",
|
|
721
|
+
message.error ?? serializeError("host/socket", "unknown response failure")
|
|
722
|
+
)
|
|
723
|
+
);
|
|
724
|
+
});
|
|
725
|
+
const removeClose = addSocketListener(socket, "close", (reason) => {
|
|
726
|
+
removeMessage();
|
|
727
|
+
removeClose();
|
|
728
|
+
removeError();
|
|
729
|
+
failPending(disconnectError(reason));
|
|
730
|
+
});
|
|
731
|
+
const removeError = addSocketListener(socket, "error", (reason) => {
|
|
732
|
+
failPending(disconnectError(reason));
|
|
733
|
+
});
|
|
734
|
+
const client = {
|
|
735
|
+
get connected() {
|
|
736
|
+
return connected;
|
|
737
|
+
},
|
|
738
|
+
get generation() {
|
|
739
|
+
return generation;
|
|
740
|
+
},
|
|
741
|
+
request(service, payload, options = {}) {
|
|
742
|
+
if (!connected) return Promise.reject(disconnectError("socket is closed"));
|
|
743
|
+
if (options.signal?.aborted)
|
|
744
|
+
return Promise.reject(
|
|
745
|
+
new HostAssemblyError(
|
|
746
|
+
"host-assembly-request-aborted",
|
|
747
|
+
`request ${service} to start with a live AbortSignal`,
|
|
748
|
+
"Start a fresh request with a live AbortSignal.",
|
|
749
|
+
{ service }
|
|
750
|
+
)
|
|
751
|
+
);
|
|
752
|
+
sequence += 1;
|
|
753
|
+
const id = `${Date.now().toString(36)}-${sequence.toString(36)}`;
|
|
754
|
+
return new Promise((resolve, reject) => {
|
|
755
|
+
const abort = () => {
|
|
756
|
+
const request = pending.get(id);
|
|
757
|
+
if (request === void 0) return;
|
|
758
|
+
pending.delete(id);
|
|
759
|
+
request.cleanup();
|
|
760
|
+
try {
|
|
761
|
+
sendWire(socket, { kind: "cancel", id });
|
|
762
|
+
} catch {
|
|
763
|
+
}
|
|
764
|
+
reject(
|
|
765
|
+
new HostAssemblyError(
|
|
766
|
+
"host-assembly-request-aborted",
|
|
767
|
+
`request ${service} to finish before cancellation`,
|
|
768
|
+
"Start a fresh request with a live AbortSignal.",
|
|
769
|
+
{ service }
|
|
770
|
+
)
|
|
771
|
+
);
|
|
772
|
+
};
|
|
773
|
+
const cleanup = () => options.signal?.removeEventListener("abort", abort);
|
|
774
|
+
pending.set(id, {
|
|
775
|
+
resolve,
|
|
776
|
+
reject,
|
|
777
|
+
cleanup
|
|
778
|
+
});
|
|
779
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
780
|
+
if (options.signal?.aborted) {
|
|
781
|
+
abort();
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
try {
|
|
785
|
+
sendWire(socket, {
|
|
786
|
+
kind: "request",
|
|
787
|
+
id,
|
|
788
|
+
service,
|
|
789
|
+
payload,
|
|
790
|
+
...options.generation === void 0 ? {} : { generation: options.generation }
|
|
791
|
+
});
|
|
792
|
+
} catch (cause) {
|
|
793
|
+
const request = pending.get(id);
|
|
794
|
+
request?.cleanup();
|
|
795
|
+
pending.delete(id);
|
|
796
|
+
reject(disconnectError(cause));
|
|
797
|
+
}
|
|
798
|
+
});
|
|
799
|
+
},
|
|
800
|
+
subscribe(topic, listener) {
|
|
801
|
+
if (!connected) return () => {
|
|
802
|
+
};
|
|
803
|
+
const listeners = subscriptions.get(topic) ?? /* @__PURE__ */ new Set();
|
|
804
|
+
listeners.add(listener);
|
|
805
|
+
subscriptions.set(topic, listeners);
|
|
806
|
+
sendWire(socket, { kind: "subscribe", topic });
|
|
807
|
+
return () => {
|
|
808
|
+
listeners.delete(listener);
|
|
809
|
+
if (listeners.size !== 0) return;
|
|
810
|
+
subscriptions.delete(topic);
|
|
811
|
+
sendWire(socket, { kind: "unsubscribe", topic });
|
|
812
|
+
};
|
|
813
|
+
},
|
|
814
|
+
onDisconnect(listener) {
|
|
815
|
+
if (!connected) {
|
|
816
|
+
listener(disconnectError("socket is already closed"));
|
|
817
|
+
return () => {
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
disconnectListeners.add(listener);
|
|
821
|
+
return () => disconnectListeners.delete(listener);
|
|
822
|
+
},
|
|
823
|
+
close(reason) {
|
|
824
|
+
if (!connected) return;
|
|
825
|
+
failPending(disconnectError(reason ?? "client closed the host connection"));
|
|
826
|
+
removeMessage();
|
|
827
|
+
removeClose();
|
|
828
|
+
removeError();
|
|
829
|
+
socket.close();
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
return client;
|
|
833
|
+
}
|
|
834
|
+
async function connectHostWebSocket(url) {
|
|
835
|
+
const Constructor = globalThis.WebSocket;
|
|
836
|
+
if (typeof Constructor !== "function")
|
|
837
|
+
throw transportFailure("host/connect", "the current runtime does not provide WebSocket");
|
|
838
|
+
return createHostWebSocketClient(new Constructor(url));
|
|
839
|
+
}
|
|
840
|
+
function attachHostWebSocketServer(socket, server) {
|
|
841
|
+
const client = server.connect();
|
|
842
|
+
const pending = /* @__PURE__ */ new Map();
|
|
843
|
+
const subscriptions = /* @__PURE__ */ new Map();
|
|
844
|
+
let disposed = false;
|
|
845
|
+
const removeMessage = addSocketListener(socket, "message", (event) => {
|
|
846
|
+
const message = parseWire(event);
|
|
847
|
+
if (message === void 0 || disposed) return;
|
|
848
|
+
if (message.kind === "cancel") {
|
|
849
|
+
pending.get(message.id)?.abort();
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
if (message.kind === "subscribe") {
|
|
853
|
+
subscriptions.get(message.topic)?.();
|
|
854
|
+
subscriptions.set(
|
|
855
|
+
message.topic,
|
|
856
|
+
client.subscribe(message.topic, (payload) => {
|
|
857
|
+
if (!disposed) sendWire(socket, { kind: "event", topic: message.topic, payload });
|
|
858
|
+
})
|
|
859
|
+
);
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
if (message.kind === "unsubscribe") {
|
|
863
|
+
subscriptions.get(message.topic)?.();
|
|
864
|
+
subscriptions.delete(message.topic);
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
if (message.kind !== "request") return;
|
|
868
|
+
const controller = new AbortController();
|
|
869
|
+
pending.set(message.id, controller);
|
|
870
|
+
void client.request(message.service, message.payload, {
|
|
871
|
+
signal: controller.signal,
|
|
872
|
+
...message.generation === void 0 ? {} : { generation: message.generation }
|
|
873
|
+
}).then((value) => {
|
|
874
|
+
if (!disposed)
|
|
875
|
+
sendWire(socket, {
|
|
876
|
+
kind: "response",
|
|
877
|
+
id: message.id,
|
|
878
|
+
ok: true,
|
|
879
|
+
value,
|
|
880
|
+
generation: client.generation
|
|
881
|
+
});
|
|
882
|
+
}).catch((error) => {
|
|
883
|
+
if (!disposed)
|
|
884
|
+
sendWire(socket, {
|
|
885
|
+
kind: "response",
|
|
886
|
+
id: message.id,
|
|
887
|
+
ok: false,
|
|
888
|
+
error: serializeError(message.service, error),
|
|
889
|
+
generation: client.generation
|
|
890
|
+
});
|
|
891
|
+
}).finally(() => pending.delete(message.id));
|
|
892
|
+
});
|
|
893
|
+
const removeClose = addSocketListener(socket, "close", () => dispose());
|
|
894
|
+
const removeError = addSocketListener(socket, "error", () => dispose());
|
|
895
|
+
function dispose() {
|
|
896
|
+
if (disposed) return;
|
|
897
|
+
disposed = true;
|
|
898
|
+
removeMessage();
|
|
899
|
+
removeClose();
|
|
900
|
+
removeError();
|
|
901
|
+
for (const controller of pending.values()) controller.abort();
|
|
902
|
+
pending.clear();
|
|
903
|
+
for (const unsubscribe of subscriptions.values()) unsubscribe();
|
|
904
|
+
subscriptions.clear();
|
|
905
|
+
client.close("host socket disconnected");
|
|
906
|
+
}
|
|
907
|
+
return dispose;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// src/backend.ts
|
|
911
|
+
function effectiveAssemblyInput(current, input) {
|
|
912
|
+
const preservePairs = input.pairs === void 0 && input.entries === void 0 && input.modules === void 0;
|
|
913
|
+
const entriesById = new Map(current.entries.map((entry) => [entry.id, entry]));
|
|
914
|
+
const modulesByName = new Map(current.modules.map((module) => [module.name, module]));
|
|
915
|
+
const pairs = current.pairs.flatMap((pair) => {
|
|
916
|
+
const entry = entriesById.get(pair.entryId);
|
|
917
|
+
const module = modulesByName.get(pair.module.name);
|
|
918
|
+
return entry === void 0 || module === void 0 ? [] : [{ id: pair.id, frontend: { entry, module } }];
|
|
919
|
+
});
|
|
920
|
+
return {
|
|
921
|
+
entries: input.entries ?? current.entries,
|
|
922
|
+
modules: input.modules ?? current.modules,
|
|
923
|
+
...input.pairs !== void 0 ? { pairs: input.pairs } : preservePairs ? { pairs } : {},
|
|
924
|
+
...input.config === void 0 ? current.config === void 0 ? {} : { config: current.config } : { config: input.config },
|
|
925
|
+
...input.backendEntries === void 0 ? {} : { backendEntries: input.backendEntries }
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
function authorityOf(initial) {
|
|
929
|
+
let current = initial;
|
|
930
|
+
let generation = 1;
|
|
931
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
932
|
+
const authority = {
|
|
933
|
+
get current() {
|
|
934
|
+
return current;
|
|
935
|
+
},
|
|
936
|
+
get generation() {
|
|
937
|
+
return generation;
|
|
938
|
+
},
|
|
939
|
+
subscribe(listener) {
|
|
940
|
+
listeners.add(listener);
|
|
941
|
+
return () => listeners.delete(listener);
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
return {
|
|
945
|
+
authority,
|
|
946
|
+
publish(input) {
|
|
947
|
+
const next = createHostAssembly(input);
|
|
948
|
+
const checked = validateHostAssembly(next);
|
|
949
|
+
if (!checked.ok) throw checked.error;
|
|
950
|
+
current = checked.value;
|
|
951
|
+
generation += 1;
|
|
952
|
+
for (const listener of listeners) listener(current);
|
|
953
|
+
return current;
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
}
|
|
957
|
+
function hostFoundationPlugin(assembly, transport) {
|
|
958
|
+
return {
|
|
959
|
+
name: "forgeax:backend-host-foundation",
|
|
960
|
+
provide: ["hostAssembly", "hostTransport"],
|
|
961
|
+
apply(ctx) {
|
|
962
|
+
ctx.provide("hostAssembly", assembly);
|
|
963
|
+
ctx.provide("hostTransport", transport);
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
function assertBackendCatalogIdentity(catalog, modules) {
|
|
968
|
+
if (catalog === void 0) return;
|
|
969
|
+
for (const module of modules) {
|
|
970
|
+
const record = catalog.get(module.name);
|
|
971
|
+
if (record !== void 0) assertHostModuleCatalogIdentity(module, record);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
async function createBackendHost(options = {}) {
|
|
975
|
+
const context = options.context ?? new Context();
|
|
976
|
+
const ownedContext = options.context === void 0;
|
|
977
|
+
const realm = options.realm ?? "engine";
|
|
978
|
+
const catalog = options.catalog;
|
|
979
|
+
const pairedBackendEntries = options.pairs?.flatMap((pair) => pair.backend === void 0 ? [] : [pair.backend.entry]) ?? [];
|
|
980
|
+
const entries = options.entries ?? options.assembly?.entries ?? options.pairs?.flatMap((pair) => pair.frontend === void 0 ? [] : [pair.frontend.entry]) ?? [];
|
|
981
|
+
const modules = options.modules ?? options.assembly?.modules ?? (options.pairs === void 0 ? catalog === void 0 ? [] : modulesFromCatalog(catalog, realm) : options.pairs.flatMap(
|
|
982
|
+
(pair) => pair.frontend === void 0 ? [] : [pair.frontend.module]
|
|
983
|
+
));
|
|
984
|
+
const initialAssembly = options.assembly ?? createHostAssembly({
|
|
985
|
+
entries,
|
|
986
|
+
modules,
|
|
987
|
+
...options.pairs === void 0 ? {} : { pairs: options.pairs },
|
|
988
|
+
...options.config === void 0 ? {} : { config: options.config }
|
|
989
|
+
});
|
|
990
|
+
const checkedInitial = validateHostAssembly(initialAssembly);
|
|
991
|
+
if (!checkedInitial.ok) {
|
|
992
|
+
if (ownedContext) await context.fiber.dispose();
|
|
993
|
+
throw checkedInitial.error;
|
|
994
|
+
}
|
|
995
|
+
const backendModules = options.pairs === void 0 ? modules : options.pairs.flatMap((pair) => pair.backend === void 0 ? [] : [pair.backend.module]);
|
|
996
|
+
assertBackendCatalogIdentity(catalog, backendModules);
|
|
997
|
+
const authority = authorityOf(checkedInitial.value);
|
|
998
|
+
const assembly = authority.authority;
|
|
999
|
+
const bootstrapRevision = checkedInitial.value.revision;
|
|
1000
|
+
const transport = options.transport ?? createHostTransport();
|
|
1001
|
+
let foundationFiber;
|
|
1002
|
+
let loaderFiber;
|
|
1003
|
+
let loader;
|
|
1004
|
+
let backendEntries = options.pairs === void 0 ? options.entries ?? options.assembly?.entries ?? [] : pairedBackendEntries;
|
|
1005
|
+
const startupFibers = [];
|
|
1006
|
+
let unregisterAssemblyService;
|
|
1007
|
+
let unregisterActivationService;
|
|
1008
|
+
let unsubscribeAssembly;
|
|
1009
|
+
try {
|
|
1010
|
+
foundationFiber = await context.plugin(hostFoundationPlugin(assembly, transport));
|
|
1011
|
+
for (const plugin of options.startupPlugins ?? [])
|
|
1012
|
+
startupFibers.push(await context.plugin(plugin));
|
|
1013
|
+
if (catalog !== void 0) {
|
|
1014
|
+
const bootstrapped = await bootstrapCatalogLoader(context, catalog, realm, {
|
|
1015
|
+
catalogDigest: checkedInitial.value.revision,
|
|
1016
|
+
supportedRealms: [realm]
|
|
1017
|
+
});
|
|
1018
|
+
if (!bootstrapped.ok) throw bootstrapped.error;
|
|
1019
|
+
loaderFiber = bootstrapped.value.fiber;
|
|
1020
|
+
loader = bootstrapped.value.loader;
|
|
1021
|
+
if (backendEntries.length > 0) {
|
|
1022
|
+
await loader.root.update(projectPluginEntries(backendEntries, realm, realm));
|
|
1023
|
+
await loader.await();
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
unregisterAssemblyService = transport.register(HOST_ASSEMBLY_SERVICE, () => assembly.current);
|
|
1027
|
+
unregisterActivationService = transport.register(
|
|
1028
|
+
HOST_ACTIVATION_REPORT_SERVICE,
|
|
1029
|
+
async ({ payload }) => {
|
|
1030
|
+
const report = payload;
|
|
1031
|
+
if (report.revision !== assembly.current.revision && report.revision !== bootstrapRevision) {
|
|
1032
|
+
throw new HostAssemblyError(
|
|
1033
|
+
"host-assembly-revision-mismatch",
|
|
1034
|
+
`activation report revision ${report.revision} to match ${assembly.current.revision}`,
|
|
1035
|
+
"Discard the stale frontend report and fetch the current backend assembly.",
|
|
1036
|
+
{ actual: report.revision, expected: assembly.current.revision }
|
|
1037
|
+
);
|
|
1038
|
+
}
|
|
1039
|
+
await options.onActivationReport?.(report);
|
|
1040
|
+
return { accepted: true };
|
|
1041
|
+
}
|
|
1042
|
+
);
|
|
1043
|
+
unsubscribeAssembly = assembly.subscribe((next) => {
|
|
1044
|
+
transport.publish(HOST_ASSEMBLY_CHANGED_TOPIC, next);
|
|
1045
|
+
});
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
unregisterActivationService?.();
|
|
1048
|
+
unregisterAssemblyService?.();
|
|
1049
|
+
unsubscribeAssembly?.();
|
|
1050
|
+
await loaderFiber?.dispose();
|
|
1051
|
+
for (const fiber of startupFibers.reverse()) await fiber.dispose();
|
|
1052
|
+
await foundationFiber?.dispose();
|
|
1053
|
+
if (ownedContext) await context.fiber.dispose();
|
|
1054
|
+
throw error;
|
|
1055
|
+
}
|
|
1056
|
+
let disposed = false;
|
|
1057
|
+
const host = {
|
|
1058
|
+
context,
|
|
1059
|
+
...loader === void 0 ? {} : { loader },
|
|
1060
|
+
assembly,
|
|
1061
|
+
transport,
|
|
1062
|
+
ownedContext,
|
|
1063
|
+
async update(nextInput) {
|
|
1064
|
+
if (disposed) {
|
|
1065
|
+
throw new HostAssemblyError(
|
|
1066
|
+
"host-assembly-service-unavailable",
|
|
1067
|
+
"backend host to remain active while updating Entries",
|
|
1068
|
+
"Create a new host instance before updating the disposed host.",
|
|
1069
|
+
{ service: "host-assembly" }
|
|
1070
|
+
);
|
|
1071
|
+
}
|
|
1072
|
+
const input = Array.isArray(nextInput) ? {
|
|
1073
|
+
entries: nextInput,
|
|
1074
|
+
backendEntries: nextInput
|
|
1075
|
+
} : nextInput;
|
|
1076
|
+
const effectiveInput = effectiveAssemblyInput(assembly.current, input);
|
|
1077
|
+
const candidate = createHostAssembly(effectiveInput);
|
|
1078
|
+
const checkedCandidate = validateHostAssembly(candidate);
|
|
1079
|
+
if (!checkedCandidate.ok) throw checkedCandidate.error;
|
|
1080
|
+
const candidateBackendModules = input.pairs === void 0 ? checkedCandidate.value.modules : input.pairs.flatMap(
|
|
1081
|
+
(pair) => pair.backend === void 0 ? [] : [pair.backend.module]
|
|
1082
|
+
);
|
|
1083
|
+
assertBackendCatalogIdentity(catalog, candidateBackendModules);
|
|
1084
|
+
const nextBackendEntries = input.backendEntries ?? (input.pairs === void 0 ? input.entries ?? backendEntries : input.pairs.flatMap(
|
|
1085
|
+
(pair) => pair.backend === void 0 ? [] : [pair.backend.entry]
|
|
1086
|
+
));
|
|
1087
|
+
if (loader === void 0 && nextBackendEntries.length > 0) {
|
|
1088
|
+
throw new HostAssemblyError(
|
|
1089
|
+
"host-assembly-service-unavailable",
|
|
1090
|
+
"a CatalogLoader to be installed before updating Entries",
|
|
1091
|
+
"Provide a backend Catalog when the host owns plugin Entry activation.",
|
|
1092
|
+
{ service: "loader" }
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
if (loader !== void 0) {
|
|
1096
|
+
await loader.root.update(projectPluginEntries(nextBackendEntries, realm, realm));
|
|
1097
|
+
await loader.await();
|
|
1098
|
+
}
|
|
1099
|
+
backendEntries = nextBackendEntries;
|
|
1100
|
+
return authority.publish(effectiveInput);
|
|
1101
|
+
},
|
|
1102
|
+
async dispose() {
|
|
1103
|
+
if (disposed) return;
|
|
1104
|
+
disposed = true;
|
|
1105
|
+
unsubscribeAssembly?.();
|
|
1106
|
+
unregisterActivationService?.();
|
|
1107
|
+
unregisterAssemblyService?.();
|
|
1108
|
+
transport.close();
|
|
1109
|
+
await loaderFiber?.dispose();
|
|
1110
|
+
for (const fiber of startupFibers.reverse()) await fiber.dispose();
|
|
1111
|
+
await foundationFiber?.dispose();
|
|
1112
|
+
if (ownedContext) await context.fiber.dispose();
|
|
1113
|
+
}
|
|
1114
|
+
};
|
|
1115
|
+
return host;
|
|
1116
|
+
}
|
|
1117
|
+
function hostFoundationPlugin2(assembly, transport) {
|
|
1118
|
+
return {
|
|
1119
|
+
name: "forgeax:frontend-host-foundation",
|
|
1120
|
+
provide: ["hostAssembly", "hostTransport"],
|
|
1121
|
+
apply(ctx) {
|
|
1122
|
+
ctx.provide("hostAssembly", assembly);
|
|
1123
|
+
if (transport !== void 0) ctx.provide("hostTransport", transport);
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
function dynamicModuleRecord(name, realm, version, url, digest) {
|
|
1128
|
+
if (url === void 0) {
|
|
1129
|
+
return {
|
|
1130
|
+
realm,
|
|
1131
|
+
version,
|
|
1132
|
+
...digest === void 0 ? {} : { digest },
|
|
1133
|
+
load: async () => {
|
|
1134
|
+
throw new HostAssemblyError(
|
|
1135
|
+
"host-assembly-module-missing",
|
|
1136
|
+
`module ${name} to have a browser URL or static Catalog record`,
|
|
1137
|
+
"Add the module to the frozen Catalog or publish its browser entry from the backend.",
|
|
1138
|
+
{ name }
|
|
1139
|
+
);
|
|
1140
|
+
}
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
return {
|
|
1144
|
+
realm,
|
|
1145
|
+
version,
|
|
1146
|
+
...digest === void 0 ? {} : { digest },
|
|
1147
|
+
load: () => import(
|
|
1148
|
+
/* @vite-ignore */
|
|
1149
|
+
url
|
|
1150
|
+
)
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
function catalogForAssembly(assembly, staticCatalog) {
|
|
1154
|
+
const catalog = /* @__PURE__ */ new Map();
|
|
1155
|
+
for (const module of assembly.modules) {
|
|
1156
|
+
const existing = staticCatalog?.get(module.name);
|
|
1157
|
+
if (existing !== void 0) {
|
|
1158
|
+
assertHostModuleCatalogIdentity(module, existing);
|
|
1159
|
+
catalog.set(module.name, existing);
|
|
1160
|
+
continue;
|
|
1161
|
+
}
|
|
1162
|
+
catalog.set(
|
|
1163
|
+
module.name,
|
|
1164
|
+
dynamicModuleRecord(module.name, module.realm, module.version, module.url, module.digest)
|
|
1165
|
+
);
|
|
1166
|
+
}
|
|
1167
|
+
for (const [name, record] of staticCatalog ?? []) {
|
|
1168
|
+
if (!catalog.has(name)) catalog.set(name, record);
|
|
1169
|
+
}
|
|
1170
|
+
return catalog;
|
|
1171
|
+
}
|
|
1172
|
+
function assertModuleVersions(assembly, versions) {
|
|
1173
|
+
if (versions === void 0) return;
|
|
1174
|
+
for (const module of assembly.modules) {
|
|
1175
|
+
const actual = versions.get(module.name);
|
|
1176
|
+
if (actual === void 0 || actual === module.version) continue;
|
|
1177
|
+
throw new HostAssemblyError(
|
|
1178
|
+
"host-assembly-module-version-mismatch",
|
|
1179
|
+
`module ${module.name} to use version ${module.version}`,
|
|
1180
|
+
"Refresh the browser module graph from the same backend assembly revision.",
|
|
1181
|
+
{ name: module.name, actual, expected: module.version }
|
|
1182
|
+
);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
function moduleIdentity(module) {
|
|
1186
|
+
return `${module.version}@${module.digest ?? module.url ?? "<catalog>"}`;
|
|
1187
|
+
}
|
|
1188
|
+
function assertModuleReloadBoundary(previous, next) {
|
|
1189
|
+
const previousModules = new Map(previous.modules.map((module) => [module.name, module]));
|
|
1190
|
+
const nextModules = new Map(next.modules.map((module) => [module.name, module]));
|
|
1191
|
+
const names = /* @__PURE__ */ new Set([...previousModules.keys(), ...nextModules.keys()]);
|
|
1192
|
+
for (const name of names) {
|
|
1193
|
+
const before = previousModules.get(name);
|
|
1194
|
+
const after = nextModules.get(name);
|
|
1195
|
+
const beforeIdentity = before === void 0 ? "<absent>" : moduleIdentity(before);
|
|
1196
|
+
const afterIdentity = after === void 0 ? "<absent>" : moduleIdentity(after);
|
|
1197
|
+
if (before === void 0 || after === void 0 || before.realm !== after.realm || beforeIdentity !== afterIdentity) {
|
|
1198
|
+
throw new HostAssemblyError(
|
|
1199
|
+
"host-assembly-reload-required",
|
|
1200
|
+
`module ${name} to keep its loaded code identity ${beforeIdentity}`,
|
|
1201
|
+
"Reload the frontend host to install the new module graph before activating this assembly.",
|
|
1202
|
+
{ module: name, actual: beforeIdentity, expected: afterIdentity }
|
|
1203
|
+
);
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
function staticAssembly(options, realm) {
|
|
1208
|
+
const entries = options.entries ?? [];
|
|
1209
|
+
const modules = options.catalog === void 0 ? [] : modulesFromCatalog(options.catalog, realm, "static");
|
|
1210
|
+
return createHostAssembly({
|
|
1211
|
+
entries,
|
|
1212
|
+
modules,
|
|
1213
|
+
...options.config === void 0 ? {} : { config: options.config }
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1216
|
+
function errorSummary(error) {
|
|
1217
|
+
if (error === null || typeof error !== "object" || typeof error.code !== "string" || typeof error.expected !== "string" || typeof error.hint !== "string")
|
|
1218
|
+
return void 0;
|
|
1219
|
+
const detail = error.detail;
|
|
1220
|
+
return {
|
|
1221
|
+
code: error.code,
|
|
1222
|
+
expected: error.expected,
|
|
1223
|
+
hint: error.hint,
|
|
1224
|
+
detail: detail !== null && typeof detail === "object" ? detail : { reason: String(detail ?? "unknown failure") }
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
async function fetchInitialAssembly(options, realm) {
|
|
1228
|
+
if (options.assembly !== void 0) return options.assembly;
|
|
1229
|
+
if (options.transport !== void 0)
|
|
1230
|
+
return options.transport.request(HOST_ASSEMBLY_SERVICE, void 0);
|
|
1231
|
+
if (options.entries !== void 0 || options.catalog !== void 0 || options.config !== void 0)
|
|
1232
|
+
return staticAssembly(options, realm);
|
|
1233
|
+
throw new HostAssemblyError(
|
|
1234
|
+
"host-assembly-service-unavailable",
|
|
1235
|
+
"a static assembly or backend transport to be provided",
|
|
1236
|
+
"Pass the frozen assembly for a static player or connect the frontend host to a backend host.",
|
|
1237
|
+
{ service: HOST_ASSEMBLY_SERVICE }
|
|
1238
|
+
);
|
|
1239
|
+
}
|
|
1240
|
+
function readiness(loader) {
|
|
1241
|
+
return inspectCatalogPlugins(loader).live.map((entry) => ({
|
|
1242
|
+
entryId: entry.entryId,
|
|
1243
|
+
fiberState: entry.fiberState,
|
|
1244
|
+
...entry.failure === void 0 ? {} : {
|
|
1245
|
+
failure: {
|
|
1246
|
+
code: entry.failure.code,
|
|
1247
|
+
expected: entry.failure.expected,
|
|
1248
|
+
hint: entry.failure.hint,
|
|
1249
|
+
detail: entry.failure.detail
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
}));
|
|
1253
|
+
}
|
|
1254
|
+
function assertReady(entries) {
|
|
1255
|
+
const failed = entries.find(
|
|
1256
|
+
(entry) => entry.fiberState !== "active" && entry.fiberState !== "disabled"
|
|
1257
|
+
);
|
|
1258
|
+
if (failed === void 0) return;
|
|
1259
|
+
throw new HostAssemblyError(
|
|
1260
|
+
"host-assembly-not-ready",
|
|
1261
|
+
`Entry ${failed.entryId} to reach active or disabled Fiber state`,
|
|
1262
|
+
"Inspect the Entry failure or waiting dependency and repair the frontend package graph.",
|
|
1263
|
+
{
|
|
1264
|
+
entryId: failed.entryId,
|
|
1265
|
+
fiberState: failed.fiberState,
|
|
1266
|
+
...failed.failure === void 0 ? {} : { failure: failed.failure }
|
|
1267
|
+
}
|
|
1268
|
+
);
|
|
1269
|
+
}
|
|
1270
|
+
async function createFrontendHost(options = {}) {
|
|
1271
|
+
const realm = options.realm ?? "engine";
|
|
1272
|
+
const initial = await fetchInitialAssembly(options, realm);
|
|
1273
|
+
const context = options.context ?? new Context();
|
|
1274
|
+
const ownedContext = options.context === void 0;
|
|
1275
|
+
const checked = validateHostAssembly(initial);
|
|
1276
|
+
if (!checked.ok) throw checked.error;
|
|
1277
|
+
let current = checked.value;
|
|
1278
|
+
let status = {
|
|
1279
|
+
state: "created",
|
|
1280
|
+
revision: current.revision
|
|
1281
|
+
};
|
|
1282
|
+
const state = {
|
|
1283
|
+
get current() {
|
|
1284
|
+
return current;
|
|
1285
|
+
},
|
|
1286
|
+
get status() {
|
|
1287
|
+
return status;
|
|
1288
|
+
}
|
|
1289
|
+
};
|
|
1290
|
+
const startupFibers = [];
|
|
1291
|
+
let foundationFiber;
|
|
1292
|
+
let loaderFiber;
|
|
1293
|
+
let loader;
|
|
1294
|
+
let disposed = false;
|
|
1295
|
+
let removeTransportDisconnect;
|
|
1296
|
+
const report = async (next) => {
|
|
1297
|
+
status = next;
|
|
1298
|
+
await options.reportStatus?.(next);
|
|
1299
|
+
if (options.transport !== void 0) {
|
|
1300
|
+
const report2 = {
|
|
1301
|
+
state: next.state,
|
|
1302
|
+
revision: next.revision
|
|
1303
|
+
};
|
|
1304
|
+
if (next.entries !== void 0) report2.entries = next.entries;
|
|
1305
|
+
const failure = errorSummary(next.error);
|
|
1306
|
+
if (failure !== void 0) report2.error = failure;
|
|
1307
|
+
try {
|
|
1308
|
+
await options.transport.request(HOST_ACTIVATION_REPORT_SERVICE, report2);
|
|
1309
|
+
} catch (error) {
|
|
1310
|
+
const failed = {
|
|
1311
|
+
...next,
|
|
1312
|
+
state: next.state === "active" ? "failed" : next.state,
|
|
1313
|
+
error
|
|
1314
|
+
};
|
|
1315
|
+
status = failed;
|
|
1316
|
+
await options.reportStatus?.(failed);
|
|
1317
|
+
throw error;
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1321
|
+
if (options.transport !== void 0) {
|
|
1322
|
+
removeTransportDisconnect = options.transport.onDisconnect((error) => {
|
|
1323
|
+
if (disposed) return;
|
|
1324
|
+
const failed = {
|
|
1325
|
+
state: "failed",
|
|
1326
|
+
revision: current.revision,
|
|
1327
|
+
error
|
|
1328
|
+
};
|
|
1329
|
+
status = failed;
|
|
1330
|
+
void Promise.resolve(options.reportStatus?.(failed)).catch(() => {
|
|
1331
|
+
});
|
|
1332
|
+
});
|
|
1333
|
+
}
|
|
1334
|
+
try {
|
|
1335
|
+
foundationFiber = await context.plugin(hostFoundationPlugin2(state, options.transport));
|
|
1336
|
+
for (const plugin of options.startupPlugins ?? [])
|
|
1337
|
+
startupFibers.push(await context.plugin(plugin));
|
|
1338
|
+
if (options.autoActivate !== false) {
|
|
1339
|
+
const host2 = {
|
|
1340
|
+
context,
|
|
1341
|
+
...options.transport === void 0 ? {} : { transport: options.transport },
|
|
1342
|
+
assembly: state,
|
|
1343
|
+
...ownedContext ? { ownedContext: true } : { ownedContext: false },
|
|
1344
|
+
get status() {
|
|
1345
|
+
return status;
|
|
1346
|
+
},
|
|
1347
|
+
activate: async (_next) => {
|
|
1348
|
+
},
|
|
1349
|
+
update: async (_next) => {
|
|
1350
|
+
},
|
|
1351
|
+
dispose: async () => {
|
|
1352
|
+
}
|
|
1353
|
+
};
|
|
1354
|
+
await activateFrontendHost(
|
|
1355
|
+
host2,
|
|
1356
|
+
initial,
|
|
1357
|
+
options,
|
|
1358
|
+
realm,
|
|
1359
|
+
() => loader,
|
|
1360
|
+
(value) => {
|
|
1361
|
+
loader = value.loader;
|
|
1362
|
+
loaderFiber = value.fiber;
|
|
1363
|
+
},
|
|
1364
|
+
report,
|
|
1365
|
+
() => {
|
|
1366
|
+
current = initial;
|
|
1367
|
+
}
|
|
1368
|
+
);
|
|
1369
|
+
}
|
|
1370
|
+
} catch (error) {
|
|
1371
|
+
removeTransportDisconnect?.();
|
|
1372
|
+
await loaderFiber?.dispose();
|
|
1373
|
+
for (const fiber of startupFibers.reverse()) await fiber.dispose();
|
|
1374
|
+
await foundationFiber?.dispose();
|
|
1375
|
+
if (ownedContext) await context.fiber.dispose();
|
|
1376
|
+
throw error;
|
|
1377
|
+
}
|
|
1378
|
+
const host = {
|
|
1379
|
+
context,
|
|
1380
|
+
...loader === void 0 ? {} : { loader },
|
|
1381
|
+
...options.transport === void 0 ? {} : { transport: options.transport },
|
|
1382
|
+
assembly: state,
|
|
1383
|
+
ownedContext,
|
|
1384
|
+
get status() {
|
|
1385
|
+
return status;
|
|
1386
|
+
},
|
|
1387
|
+
async activate(next = current) {
|
|
1388
|
+
await activateFrontendHost(
|
|
1389
|
+
host,
|
|
1390
|
+
next,
|
|
1391
|
+
options,
|
|
1392
|
+
realm,
|
|
1393
|
+
() => loader,
|
|
1394
|
+
(value) => {
|
|
1395
|
+
loader = value.loader;
|
|
1396
|
+
loaderFiber = value.fiber;
|
|
1397
|
+
host.loader = value.loader;
|
|
1398
|
+
},
|
|
1399
|
+
report,
|
|
1400
|
+
() => {
|
|
1401
|
+
current = next;
|
|
1402
|
+
}
|
|
1403
|
+
);
|
|
1404
|
+
},
|
|
1405
|
+
async update(next) {
|
|
1406
|
+
await host.activate(next);
|
|
1407
|
+
},
|
|
1408
|
+
async dispose() {
|
|
1409
|
+
if (disposed) return;
|
|
1410
|
+
disposed = true;
|
|
1411
|
+
removeTransportDisconnect?.();
|
|
1412
|
+
await loaderFiber?.dispose();
|
|
1413
|
+
for (const fiber of startupFibers.reverse()) await fiber.dispose();
|
|
1414
|
+
await foundationFiber?.dispose();
|
|
1415
|
+
if (ownedContext) await context.fiber.dispose();
|
|
1416
|
+
status = { state: "disposed", revision: current.revision };
|
|
1417
|
+
}
|
|
1418
|
+
};
|
|
1419
|
+
return host;
|
|
1420
|
+
}
|
|
1421
|
+
async function activateFrontendHost(host, next, options, realm, getLoader, setLoader, report, commit) {
|
|
1422
|
+
if (host.status.state === "disposed") {
|
|
1423
|
+
throw new HostAssemblyError(
|
|
1424
|
+
"host-assembly-service-unavailable",
|
|
1425
|
+
"frontend host to remain active while activating an assembly",
|
|
1426
|
+
"Create a new frontend host for the next browser connection.",
|
|
1427
|
+
{ service: "host-assembly" }
|
|
1428
|
+
);
|
|
1429
|
+
}
|
|
1430
|
+
const checked = validateHostAssembly(next);
|
|
1431
|
+
if (!checked.ok) {
|
|
1432
|
+
await report({ state: "failed", revision: next.revision, error: checked.error });
|
|
1433
|
+
throw checked.error;
|
|
1434
|
+
}
|
|
1435
|
+
const activeLoader = getLoader();
|
|
1436
|
+
try {
|
|
1437
|
+
if (activeLoader !== void 0)
|
|
1438
|
+
assertModuleReloadBoundary(host.assembly.current, checked.value);
|
|
1439
|
+
await report({ state: "loading", revision: next.revision });
|
|
1440
|
+
assertModuleVersions(checked.value, options.moduleVersions);
|
|
1441
|
+
let loader = activeLoader;
|
|
1442
|
+
if (loader === void 0) {
|
|
1443
|
+
const catalog = catalogForAssembly(checked.value, options.catalog);
|
|
1444
|
+
const installed = await installCatalogLoader(host.context, catalog, realm);
|
|
1445
|
+
loader = installed.loader;
|
|
1446
|
+
setLoader(installed);
|
|
1447
|
+
}
|
|
1448
|
+
const entries = projectPluginEntries(checked.value.entries, realm, realm);
|
|
1449
|
+
await loader.root.update(entries);
|
|
1450
|
+
await loader.await();
|
|
1451
|
+
const actualEntries = readiness(loader);
|
|
1452
|
+
assertReady(actualEntries);
|
|
1453
|
+
commit();
|
|
1454
|
+
await report({ state: "active", revision: checked.value.revision, entries: actualEntries });
|
|
1455
|
+
} catch (error) {
|
|
1456
|
+
if (error instanceof HostAssemblyError && error.code === "host-assembly-reload-required") {
|
|
1457
|
+
throw error;
|
|
1458
|
+
}
|
|
1459
|
+
const activeLoader2 = getLoader();
|
|
1460
|
+
const actualEntries = activeLoader2 === void 0 ? void 0 : readiness(activeLoader2);
|
|
1461
|
+
await report({
|
|
1462
|
+
state: "failed",
|
|
1463
|
+
revision: next.revision,
|
|
1464
|
+
...actualEntries === void 0 ? {} : { entries: actualEntries },
|
|
1465
|
+
error
|
|
1466
|
+
});
|
|
1467
|
+
throw error;
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
export { HOST_ACTIVATION_REPORT_SERVICE, HOST_ASSEMBLY_CHANGED_TOPIC, HOST_ASSEMBLY_SCHEMA_VERSION, HOST_ASSEMBLY_SERVICE, HostAssemblyError, attachHostWebSocketServer, canonicalHostJson, connectHostWebSocket, createBackendHost, createFrontendHost, createHostAssembly, createHostTransport, createHostWebSocketClient, hostRevision, modulesFromCatalog, validateHostAssembly };
|
|
1472
|
+
//# sourceMappingURL=index.mjs.map
|
|
1473
|
+
//# sourceMappingURL=index.mjs.map
|