pico-engine 1.4.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uiECIForRoot = uiECIForRoot;
4
+ exports.provisionRoot = provisionRoot;
5
+ const path = require("path");
6
+ const toFileUrl_1 = require("./utils/toFileUrl");
7
+ /**
8
+ * Base rulesets installed onto every root pico. These make up the engine's
9
+ * "OS" layer (wrangler + friends) plus the built-in UI ruleset.
10
+ */
11
+ const BASE_KRL_FILES = [
12
+ "io.picolabs.pico-engine-ui.krl",
13
+ "io.picolabs.wrangler.krl",
14
+ "io.picolabs.subscription.krl",
15
+ "io.picolabs.did-o.krl",
16
+ "io.picolabs.pds.krl",
17
+ ];
18
+ const UI_CHANNEL_TAGS = ["engine", "ui"];
19
+ function uiChannelKey(tags) {
20
+ return tags.slice(0).sort().join(",");
21
+ }
22
+ /**
23
+ * Find the ["engine","ui"] channel on a pico, if it exists.
24
+ */
25
+ function uiECIForRoot(pf, rootPicoId) {
26
+ const pico = pf.rootPicos().find((p) => p.id === rootPicoId);
27
+ if (!pico) {
28
+ return null;
29
+ }
30
+ const chann = pico
31
+ .toReadOnly()
32
+ .channels.find((c) => uiChannelKey(UI_CHANNEL_TAGS) === uiChannelKey(c.tags));
33
+ return chann ? chann.id : null;
34
+ }
35
+ /**
36
+ * Provision a root pico: install the base rulesets, ensure the ["engine","ui"]
37
+ * channel exists, fire `engine_ui setup`, and return the root + its UI ECI.
38
+ *
39
+ * Pass an existing `root` to (re)provision it — this is how the engine migrates
40
+ * a pre-existing single-root install. Omit it to mint a brand new root, which is
41
+ * what the passkey new-account registration flow does.
42
+ */
43
+ async function provisionRoot(pf, core, opts = {}) {
44
+ const root = opts.root || (await pf.createRootPico());
45
+ for (const file of BASE_KRL_FILES) {
46
+ const url = (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", file));
47
+ const { ruleset } = await core.rsRegistry.flush(url);
48
+ await root.install(ruleset, { url, config: {} });
49
+ }
50
+ let uiChannel = root
51
+ .toReadOnly()
52
+ .channels.find((c) => uiChannelKey(UI_CHANNEL_TAGS) === uiChannelKey(c.tags));
53
+ if (!uiChannel) {
54
+ uiChannel = (await root.newChannel({
55
+ tags: UI_CHANNEL_TAGS,
56
+ eventPolicy: {
57
+ allow: [{ domain: "engine_ui", name: "setup" }],
58
+ deny: [],
59
+ },
60
+ })).toReadOnly();
61
+ }
62
+ await pf.eventWait({
63
+ eci: uiChannel.id,
64
+ domain: "engine_ui",
65
+ name: "setup",
66
+ data: { attrs: {} },
67
+ time: 0,
68
+ });
69
+ const name = (opts.name || "").trim();
70
+ if (name) {
71
+ await pf.eventWait({
72
+ eci: uiChannel.id,
73
+ domain: "engine_ui",
74
+ name: "box",
75
+ data: { attrs: { name } },
76
+ time: 0,
77
+ });
78
+ }
79
+ return { root, uiECI: uiChannel.id };
80
+ }