pico-engine 1.6.1 → 1.6.4
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 +105 -7
- package/dist/RulesetRegistryLoaderFs.js +10 -6
- package/dist/bundledKrl.js +129 -11
- package/dist/cli.js +25 -1
- package/dist/identity/crossPico.js +82 -22
- package/dist/identity/skyIntro.js +4 -1
- package/dist/index.js +45 -12
- package/dist/levelDb.js +102 -0
- package/dist/logging.js +46 -3
- package/dist/oauth/OAuthService.js +6 -3
- package/dist/oauth/channelEligibility.js +7 -1
- package/dist/oauth/index.js +3 -1
- package/dist/server.js +39 -0
- package/dist/uiChannelPolicies.js +6 -0
- package/krl/io.picolabs.oauth.krl +11 -4
- package/krl/io.picolabs.pico-engine-ui.krl +16 -2
- package/krl/io.picolabs.subscription.krl +73 -4
- package/krl/io.picolabs.wrangler.krl +6 -3
- package/package.json +5 -2
- package/public/docs/krl/discovery.md +1 -1
- package/public/docs/krl/modules/event.md +6 -4
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +19 -19
- package/src/RulesetRegistryLoaderFs.ts +25 -8
- package/src/bundledKrl.ts +150 -12
- package/src/cli.ts +33 -6
- package/src/identity/crossPico.ts +104 -33
- package/src/identity/skyIntro.ts +5 -1
- package/src/index.ts +60 -14
- package/src/levelDb.ts +122 -0
- package/src/logging.ts +64 -6
- package/src/oauth/OAuthService.ts +10 -4
- package/src/oauth/channelEligibility.ts +9 -0
- package/src/oauth/index.ts +7 -1
- package/src/server.ts +54 -1
- package/src/uiChannelPolicies.ts +6 -0
- package/krl/test/didtestdid.krl +0 -78
package/README.md
CHANGED
|
@@ -122,11 +122,95 @@ The server is configured via some environment variables.
|
|
|
122
122
|
- `PICO_ENGINE_BASE_URL` - The public url prefix to reach this engine. By default it's `"http://localhost:3000"`
|
|
123
123
|
- `PICO_ENGINE_ALLOW_SELF_SIGNUP` - Set to `"true"` or `"1"` to allow open registration after bootstrap (default: off; bootstrap and invite still work)
|
|
124
124
|
- `PICO_ENGINE_ALLOW_LOCALHOST_C` - Set to `"0"` to require passkey session on `/c/*` even from localhost (default: allow localhost without session for in-engine HTTP loops). Full registry: repo `MEMORY.md` § Engine environment variables.
|
|
125
|
+
- `PICO_ENGINE_LOG_FILE` - Log file path. Set to `stdout`, `-`, or `0` for **stdout only** (recommended on Fargate + EFS; use CloudWatch for retention). Default: `$PICO_ENGINE_HOME/pico-engine.log`.
|
|
125
126
|
|
|
126
127
|
The `PORT` is the only value used in setting up the engine’s [nodejs http server](https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback). We only specify the `port` so it listens listens to all traffic on that port, it will not filter by host.
|
|
127
128
|
|
|
128
129
|
For example, say you want to have your engine running with SSL on a custom domain i.e. `https://example.com` Starting the engine like this `PICO_ENGINE_BASE_URL=https://example.com pico-engine` is not enough. You will need to use a reverse proxy server like nginx to handle the SSL termination, and then forward the traffic to your private port that is running the engine.
|
|
129
130
|
|
|
131
|
+
### Containers
|
|
132
|
+
|
|
133
|
+
The engine stores mesh state in **LevelDB** under `PICO_ENGINE_HOME`:
|
|
134
|
+
|
|
135
|
+
| Path | Purpose |
|
|
136
|
+
|------|---------|
|
|
137
|
+
| `db/` | Main mesh state (picos, channels, auth, identity, …) |
|
|
138
|
+
| `rulesets-db/` | Cached flushed ruleset metadata (rebuildable from URLs) |
|
|
139
|
+
| `rulesets/` | Compiled ruleset JS |
|
|
140
|
+
| `pico-engine.log` | Rotating log file |
|
|
141
|
+
|
|
142
|
+
On startup the engine opens both LevelDB directories. If a previous process exited uncleanly (e.g. container SIGKILL), startup removes a stale `LOCK` file and may run LevelDB repair before failing.
|
|
143
|
+
|
|
144
|
+
The CLI handles **SIGTERM** and **SIGINT** by closing the HTTP server and both databases cleanly.
|
|
145
|
+
|
|
146
|
+
**Docker Compose (local or any host):**
|
|
147
|
+
|
|
148
|
+
```yaml
|
|
149
|
+
services:
|
|
150
|
+
pico-engine:
|
|
151
|
+
image: picolabs/pico-engine:1.6.4
|
|
152
|
+
ports:
|
|
153
|
+
- "3001:3000"
|
|
154
|
+
environment:
|
|
155
|
+
PICO_ENGINE_HOME: /var/pico-image
|
|
156
|
+
PICO_ENGINE_BASE_URL: http://localhost:3001
|
|
157
|
+
volumes:
|
|
158
|
+
- pico-engine-data:/var/pico-image
|
|
159
|
+
|
|
160
|
+
volumes:
|
|
161
|
+
pico-engine-data:
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Use a **named volume** or bind mount for `PICO_ENGINE_HOME`. Do not run two containers against the same volume.
|
|
165
|
+
|
|
166
|
+
#### Running on AWS ECS / Fargate
|
|
167
|
+
|
|
168
|
+
Fargate has no persistent disk of its own. For a mesh that **survives redeploys and image updates**, mount **Amazon EFS** at `/var/pico-image` and set `PICO_ENGINE_HOME=/var/pico-image`.
|
|
169
|
+
|
|
170
|
+
EFS + LevelDB is a common deployment pattern. LevelDB expects a **single writer**; it is not safe for two tasks to share one database directory. With one Fargate task and graceful shutdown, EFS persistence works well in practice.
|
|
171
|
+
|
|
172
|
+
**Do not use Fargate managed EBS** for durable mesh storage on a service: those volumes are deleted when the task stops, so each deploy would start with an empty mesh unless you build a snapshot workflow.
|
|
173
|
+
|
|
174
|
+
**Task definition (essentials):**
|
|
175
|
+
|
|
176
|
+
| Setting | Value |
|
|
177
|
+
|---------|--------|
|
|
178
|
+
| Container port | `3000` |
|
|
179
|
+
| `PICO_ENGINE_HOME` | `/var/pico-image` |
|
|
180
|
+
| `PICO_ENGINE_BASE_URL` | Public **HTTPS** origin users hit (e.g. `https://picos.example.com`) — required for passkeys and did:webvh |
|
|
181
|
+
| `PICO_ENGINE_LOG_FILE` | `stdout` — log to CloudWatch only; avoid writing `pico-engine.log` on EFS (NFS stale-handle errors can crash the process) |
|
|
182
|
+
| EFS mount | `containerPath`: `/var/pico-image` |
|
|
183
|
+
| Container `stopTimeout` | `120` (seconds) |
|
|
184
|
+
|
|
185
|
+
`PICO_ENGINE_BASE_URL` must match the URL in the browser (scheme, host, no trailing slash). Put TLS at Cloudflare, an ALB, or another reverse proxy in front of the task.
|
|
186
|
+
|
|
187
|
+
**Service deployment:**
|
|
188
|
+
|
|
189
|
+
| Setting | Recommended | Why |
|
|
190
|
+
|---------|-------------|-----|
|
|
191
|
+
| `desiredCount` | `1` | One writer per EFS mount |
|
|
192
|
+
| `minimumHealthyPercent` | `0` | Stop old task before starting new one |
|
|
193
|
+
| `maximumPercent` | `100` | Avoid two tasks on the same volume during deploy |
|
|
194
|
+
| `healthCheckGracePeriodSeconds` | `60`–`120` | Allow startup before health checks fail |
|
|
195
|
+
|
|
196
|
+
**TLS / public URL:** Terminate HTTPS in front of the container (ALB, Cloudflare Tunnel, nginx, etc.). The engine listens on HTTP port 3000 inside the task.
|
|
197
|
+
|
|
198
|
+
**If the engine fails to start** with LevelDB errors after an unclean shutdown:
|
|
199
|
+
|
|
200
|
+
1. Stop the service (ensure no task is using the volume).
|
|
201
|
+
2. Remove only the database directories on the EFS mount: `db/` and `rulesets-db/`.
|
|
202
|
+
3. Start the service again and register a new passkey (fresh mesh).
|
|
203
|
+
|
|
204
|
+
That wipes mesh state but preserves the volume mount and deployment layout. Export/backup of mesh state is a separate operational concern (see repo design docs on pico move/export).
|
|
205
|
+
|
|
206
|
+
**Checklist for operators:**
|
|
207
|
+
|
|
208
|
+
- [ ] EFS mounted at `/var/pico-image`
|
|
209
|
+
- [ ] `PICO_ENGINE_BASE_URL` set to the public HTTPS URL
|
|
210
|
+
- [ ] Single task per EFS volume
|
|
211
|
+
- [ ] `stopTimeout` ≥ 60s; deploy stops old task before starting new one
|
|
212
|
+
- [ ] Image includes graceful shutdown (1.6.x+)
|
|
213
|
+
|
|
130
214
|
## Authentication
|
|
131
215
|
|
|
132
216
|
The developer UI uses **passkeys** (WebAuthn). On first visit you register a passkey, which creates your account and root pico. Sign in again with the same passkey; add more passkeys or invite others from **Settings** (gear icon).
|
|
@@ -149,12 +233,26 @@ Two grant types can coexist in the same mesh:
|
|
|
149
233
|
|
|
150
234
|
### Mesh lock
|
|
151
235
|
|
|
152
|
-
Install the optional ruleset **`io.picolabs.oauth`** on the **root pico** to require a Bearer token on
|
|
236
|
+
Install the optional ruleset **`io.picolabs.oauth`** on the **root pico** to require a Bearer token on most **`/sky/*`** requests for that mesh. Without it, open channels work as before (channel policy only); channels tagged **`oauth-webhook`** always require Bearer.
|
|
153
237
|
|
|
154
238
|
From the Rulesets tab, install `io.picolabs.oauth` (KRL source: `packages/pico-engine/krl/io.picolabs.oauth.krl`). Removing the ruleset unlocks the mesh again.
|
|
155
239
|
|
|
156
240
|
Ruleset queries (install `io.picolabs.oauth` on the root): `meshEnabled()`, `meshRequiresOAuth(eci)`, `channelStatus(eci)`, `createChannelSecret(eci)`, … — see `krl/io.picolabs.oauth.krl`.
|
|
157
241
|
|
|
242
|
+
**Bearer evaluation order** (engine `skyRequiresBearer`):
|
|
243
|
+
|
|
244
|
+
| Condition | `/sky/*` Bearer required? |
|
|
245
|
+
|-----------|---------------------------|
|
|
246
|
+
| Channel tags `didcomm` + `ingress` | No (DIDComm ingress) |
|
|
247
|
+
| Channel tag `oauth-webhook` | **Yes** (Client Credentials) |
|
|
248
|
+
| Channel tag **`mesh-oauth-exempt`** | **No** (opt out of mesh lock) |
|
|
249
|
+
| Root has `io.picolabs.oauth` | **Yes** (mesh lock) |
|
|
250
|
+
| Otherwise | No |
|
|
251
|
+
|
|
252
|
+
Use **`mesh-oauth-exempt`** on inbound webhook channels that cannot send `Authorization` (e.g. LoRa/Helium POST to `/sky/event/...`) while mesh OAuth stays enabled for integrators (Home Assistant ACG). Tight **event policy** on those channels still applies. If a channel has both `oauth-webhook` and `mesh-oauth-exempt`, **`oauth-webhook` wins** (Bearer required).
|
|
253
|
+
|
|
254
|
+
When mesh OAuth is enabled, the **Channels → New Channel** form offers a one-click **`mesh-oauth-exempt`** tag. Existing channels: add the tag under **Edit channel** → Tags.
|
|
255
|
+
|
|
158
256
|
### Webhook credentials (Client Credentials)
|
|
159
257
|
|
|
160
258
|
For channels tagged **`oauth-webhook`** at creation time:
|
|
@@ -274,18 +372,18 @@ npm run test:auth # passkeys
|
|
|
274
372
|
|
|
275
373
|
Tests use isolated temp homes and ephemeral ports; they do not require stopping a running engine.
|
|
276
374
|
|
|
277
|
-
## Layer 2 identity and
|
|
375
|
+
## Layer 2 identity and relationships (1.6)
|
|
278
376
|
|
|
279
|
-
Every pico has a portable **did:webvh** identity. **Layer 2
|
|
377
|
+
Every pico has a portable **did:webvh** identity. **DID-based relationships** (Layer 2) use **`layer2: true`** and a **`target_did`** (did:webvh) instead of the legacy **`wellKnown_Tx`** ECI handshake. After intro, remote traffic uses **did:peer** DIDs and SKY over **DIDComm** when picos are on different meshes or engines. Prefer **`wrangler:relationship`** and **`use module io.picolabs.subscription alias relationship`** in new rulesets; legacy *subscription* event names remain supported.
|
|
280
378
|
|
|
281
379
|
- **Same engine:** intra-mesh queries/events stay local when roots share a mesh.
|
|
282
380
|
- **Cross-engine:** set a peer-reachable **`PICO_ENGINE_BASE_URL`** on each engine (for local demos, two processes on `localhost:3001` and `:3002` — see below).
|
|
283
|
-
- **Developer UI:** **
|
|
284
|
-
- **Legacy ECI
|
|
381
|
+
- **Developer UI:** **Relationships** tab (DID-based create, inbound approval, established relationships with `Tx_did` / `Rx_did`). A pico cannot target itself (`target_did` / `wellKnown_Tx` checks).
|
|
382
|
+
- **Legacy ECI relationships** are unchanged.
|
|
285
383
|
|
|
286
384
|
**Deprecated:** `io.picolabs.did-o` and `dido:prepareQuery` — use **`wrangler:picoQuery`** and **`event:send({ did: … })`**.
|
|
287
385
|
|
|
288
|
-
Full guide: [docs/guides/layer2-subscriptions.md](../../docs/guides/layer2-subscriptions.md) · Release notes: [docs/release/1.6.md](../../docs/release/1.6.md)
|
|
386
|
+
Full guide: [docs/guides/layer2-subscriptions.md](../../docs/guides/layer2-subscriptions.md) · Release notes: [docs/release/1.6.md](../../docs/release/1.6.md) · [CHANGELOG.md](../../CHANGELOG.md) (1.6.4 mesh-oauth-exempt + KRL directory UI; 1.6.3 fixes)
|
|
289
387
|
|
|
290
388
|
**Dependency:** pico-engine 1.6 requires **pico-framework `^0.8.1`** (npm). When developing engine + framework together, use `npm run link-framework` from the repo root.
|
|
291
389
|
|
|
@@ -311,7 +409,7 @@ PICO_ENGINE_HOME=/tmp/pico-engine-a PORT=3001 npm start
|
|
|
311
409
|
PICO_ENGINE_HOME=/tmp/pico-engine-b PORT=3002 npm start
|
|
312
410
|
```
|
|
313
411
|
|
|
314
|
-
Open http://localhost:3001 and http://localhost:3002 — register on both, then Layer 2
|
|
412
|
+
Open http://localhost:3001 and http://localhost:3002 — register on both, then form a Layer 2 relationship A→B. See [docs/guides/layer2-subscriptions.md](../../docs/guides/layer2-subscriptions.md). Test/demo homes live under **`/tmp`** (not `~/.pico-engine`).
|
|
315
413
|
|
|
316
414
|
## Contributing
|
|
317
415
|
|
|
@@ -11,14 +11,14 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
11
11
|
return t;
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.
|
|
14
|
+
exports.createRulesetRegistryLoaderFs = createRulesetRegistryLoaderFs;
|
|
15
15
|
const cross_fetch_1 = require("cross-fetch");
|
|
16
16
|
const fs = require("fs");
|
|
17
|
-
const classic_level_1 = require("classic-level");
|
|
18
17
|
const makeDir = require("make-dir");
|
|
19
18
|
const path = require("path");
|
|
20
19
|
const urlLib = require("url");
|
|
21
20
|
const bundledKrl_1 = require("./bundledKrl");
|
|
21
|
+
const levelDb_1 = require("./levelDb");
|
|
22
22
|
const krlCompiler = require("krl-compiler");
|
|
23
23
|
const krlCompilerVersion = require("krl-compiler/package.json").version;
|
|
24
24
|
const charwise = require("charwise");
|
|
@@ -33,12 +33,12 @@ function getUrlFilename(url) {
|
|
|
33
33
|
catch (err) { }
|
|
34
34
|
return null;
|
|
35
35
|
}
|
|
36
|
-
function
|
|
36
|
+
async function createRulesetRegistryLoaderFs(engineHomeDir, log) {
|
|
37
37
|
const rulesetDir = path.resolve(engineHomeDir, "rulesets");
|
|
38
|
-
const db =
|
|
38
|
+
const db = await (0, levelDb_1.openClassicLevelWithRecovery)(path.resolve(engineHomeDir, "rulesets-db"), {
|
|
39
39
|
keyEncoding: charwise,
|
|
40
40
|
valueEncoding: safeJsonCodec,
|
|
41
|
-
});
|
|
41
|
+
}, log);
|
|
42
42
|
async function compileAndLoad(url, krl, hash) {
|
|
43
43
|
const jsFile = path.resolve(rulesetDir, krlCompilerVersion, hash.substr(0, 2), hash.substr(2, 2), hash + ".js");
|
|
44
44
|
let compileWarnings = [];
|
|
@@ -67,7 +67,7 @@ function RulesetRegistryLoaderFs(engineHomeDir) {
|
|
|
67
67
|
compiler: { version: krlCompilerVersion, warnings: compileWarnings },
|
|
68
68
|
};
|
|
69
69
|
}
|
|
70
|
-
|
|
70
|
+
const loader = {
|
|
71
71
|
fetchKrl,
|
|
72
72
|
async save(_a) {
|
|
73
73
|
var { ruleset } = _a, rest = __rest(_a, ["ruleset"]);
|
|
@@ -91,6 +91,10 @@ function RulesetRegistryLoaderFs(engineHomeDir) {
|
|
|
91
91
|
compiler });
|
|
92
92
|
},
|
|
93
93
|
};
|
|
94
|
+
return {
|
|
95
|
+
loader,
|
|
96
|
+
close: () => db.close(),
|
|
97
|
+
};
|
|
94
98
|
}
|
|
95
99
|
async function fetchKrl(url) {
|
|
96
100
|
url = (0, bundledKrl_1.resolveBundledKrlUrl)(url);
|
package/dist/bundledKrl.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BUNDLED_KRL_FILES = void 0;
|
|
3
|
+
exports.ROOT_ONLY_RULESET_RIDS = exports.BUNDLED_KRL_FILES = void 0;
|
|
4
4
|
exports.bundledKrlDir = bundledKrlDir;
|
|
5
|
+
exports.listEngineKrlRelativePaths = listEngineKrlRelativePaths;
|
|
6
|
+
exports.engineKrlHttpPath = engineKrlHttpPath;
|
|
7
|
+
exports.engineKrlFileUrl = engineKrlFileUrl;
|
|
8
|
+
exports.listEngineKrlSources = listEngineKrlSources;
|
|
9
|
+
exports.engineKrlRelativeFromUrl = engineKrlRelativeFromUrl;
|
|
10
|
+
exports.resolveEngineKrlFile = resolveEngineKrlFile;
|
|
5
11
|
exports.resolveBundledKrlUrl = resolveBundledKrlUrl;
|
|
6
12
|
const fs = require("fs");
|
|
7
13
|
const path = require("path");
|
|
@@ -17,25 +23,137 @@ exports.BUNDLED_KRL_FILES = [
|
|
|
17
23
|
"io.picolabs.subscription.krl",
|
|
18
24
|
"io.picolabs.pds.krl",
|
|
19
25
|
];
|
|
26
|
+
/** Rulesets that only function when installed on the mesh root pico. */
|
|
27
|
+
exports.ROOT_ONLY_RULESET_RIDS = new Set(["io.picolabs.oauth"]);
|
|
20
28
|
function bundledKrlDir() {
|
|
21
29
|
return path.resolve(__dirname, "..", "krl");
|
|
22
30
|
}
|
|
23
|
-
function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
function walkKrlFiles(dir, baseDir, out) {
|
|
32
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
33
|
+
const fullPath = path.join(dir, entry.name);
|
|
34
|
+
if (entry.isDirectory()) {
|
|
35
|
+
walkKrlFiles(fullPath, baseDir, out);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (entry.isFile() && entry.name.endsWith(".krl")) {
|
|
39
|
+
out.push(path.relative(baseDir, fullPath).split(path.sep).join("/"));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** All `.krl` files under the engine's shipped `krl/` directory. */
|
|
44
|
+
function listEngineKrlRelativePaths() {
|
|
45
|
+
const root = bundledKrlDir();
|
|
46
|
+
if (!fs.existsSync(root)) {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
const paths = [];
|
|
50
|
+
walkKrlFiles(root, root, paths);
|
|
51
|
+
return paths.sort();
|
|
52
|
+
}
|
|
53
|
+
function ridFromRelativePath(relativePath) {
|
|
54
|
+
const basename = path.basename(relativePath, ".krl");
|
|
55
|
+
return basename;
|
|
56
|
+
}
|
|
57
|
+
function readKrlMeta(filePath) {
|
|
58
|
+
const text = fs.readFileSync(filePath, { encoding: "utf8", flag: "r" }).slice(0, 4000);
|
|
59
|
+
const ridMatch = text.match(/^ruleset\s+(\S+)/m);
|
|
60
|
+
const nameMatch = text.match(/\bname\s+"([^"]+)"/);
|
|
61
|
+
let description;
|
|
62
|
+
const descQuoted = text.match(/\bdescription\s+"([^"]+)"/);
|
|
63
|
+
if (descQuoted) {
|
|
64
|
+
description = descQuoted[1];
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const descHere = text.match(/\bdescription\s+<<\s*([\s\S]*?)\s*>>/);
|
|
68
|
+
if (descHere) {
|
|
69
|
+
description = descHere[1].replace(/\s+/g, " ").trim();
|
|
29
70
|
}
|
|
30
71
|
}
|
|
72
|
+
return {
|
|
73
|
+
rid: ridMatch ? ridMatch[1] : ridFromRelativePath(path.basename(filePath)),
|
|
74
|
+
name: nameMatch ? nameMatch[1] : undefined,
|
|
75
|
+
description,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function engineKrlHttpPath(relativePath) {
|
|
79
|
+
return `/krl/${relativePath.split(path.sep).join("/")}`;
|
|
80
|
+
}
|
|
81
|
+
function engineKrlFileUrl(relativePath, baseUrl) {
|
|
82
|
+
if (baseUrl) {
|
|
83
|
+
return `${baseUrl.replace(/\/$/, "")}${engineKrlHttpPath(relativePath)}`;
|
|
84
|
+
}
|
|
85
|
+
return (0, toFileUrl_1.toFileUrl)(path.join(bundledKrlDir(), relativePath));
|
|
86
|
+
}
|
|
87
|
+
function listEngineKrlSources(baseUrl) {
|
|
88
|
+
const root = bundledKrlDir();
|
|
89
|
+
return listEngineKrlRelativePaths().map((relativePath) => {
|
|
90
|
+
const filePath = path.join(root, relativePath);
|
|
91
|
+
const meta = readKrlMeta(filePath);
|
|
92
|
+
const filename = path.basename(relativePath);
|
|
93
|
+
return {
|
|
94
|
+
rid: meta.rid,
|
|
95
|
+
filename,
|
|
96
|
+
relativePath,
|
|
97
|
+
url: engineKrlFileUrl(relativePath, baseUrl),
|
|
98
|
+
name: meta.name,
|
|
99
|
+
description: meta.description,
|
|
100
|
+
rootOnly: exports.ROOT_ONLY_RULESET_RIDS.has(meta.rid),
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/** Map a URL to a relative path under `krl/`, if it refers to an engine-shipped ruleset. */
|
|
105
|
+
function engineKrlRelativeFromUrl(url) {
|
|
106
|
+
let parsed;
|
|
107
|
+
try {
|
|
108
|
+
parsed = new URL(url);
|
|
109
|
+
}
|
|
31
110
|
catch (_a) {
|
|
32
|
-
return
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
if (parsed.protocol === "file:") {
|
|
114
|
+
const filePath = decodeURI(parsed.pathname).replace(/^\/([a-z]:\/)/i, "$1");
|
|
115
|
+
const normalized = path.normalize(filePath);
|
|
116
|
+
const root = bundledKrlDir();
|
|
117
|
+
if (normalized.startsWith(root + path.sep) || normalized === root) {
|
|
118
|
+
return path.relative(root, normalized).split(path.sep).join("/");
|
|
119
|
+
}
|
|
120
|
+
const basename = path.basename(normalized);
|
|
121
|
+
const candidate = path.join(root, basename);
|
|
122
|
+
if (basename.endsWith(".krl") && fs.existsSync(candidate)) {
|
|
123
|
+
return basename;
|
|
124
|
+
}
|
|
125
|
+
return null;
|
|
33
126
|
}
|
|
34
|
-
if (
|
|
127
|
+
if (parsed.protocol === "http:" || parsed.protocol === "https:") {
|
|
128
|
+
const match = parsed.pathname.match(/^\/krl\/(.+\.krl)$/);
|
|
129
|
+
if (match) {
|
|
130
|
+
const relativePath = match[1].split("/").join(path.sep);
|
|
131
|
+
const candidate = path.join(bundledKrlDir(), relativePath);
|
|
132
|
+
if (fs.existsSync(candidate)) {
|
|
133
|
+
return match[1];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
function resolveEngineKrlFile(relativePath) {
|
|
140
|
+
const normalized = relativePath.split("/").join(path.sep);
|
|
141
|
+
const candidate = path.resolve(bundledKrlDir(), normalized);
|
|
142
|
+
const root = bundledKrlDir();
|
|
143
|
+
if (candidate.startsWith(root + path.sep) &&
|
|
144
|
+
fs.existsSync(candidate) &&
|
|
145
|
+
candidate.endsWith(".krl")) {
|
|
146
|
+
return candidate;
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
function resolveBundledKrlUrl(url) {
|
|
151
|
+
const relativePath = engineKrlRelativeFromUrl(url);
|
|
152
|
+
if (!relativePath) {
|
|
35
153
|
return url;
|
|
36
154
|
}
|
|
37
|
-
const localPath =
|
|
38
|
-
if (
|
|
155
|
+
const localPath = resolveEngineKrlFile(relativePath);
|
|
156
|
+
if (localPath) {
|
|
39
157
|
return (0, toFileUrl_1.toFileUrl)(localPath);
|
|
40
158
|
}
|
|
41
159
|
return url;
|
package/dist/cli.js
CHANGED
|
@@ -52,7 +52,31 @@ if (allowSelfSignupEnv === "true" || allowSelfSignupEnv === "1") {
|
|
|
52
52
|
else if (allowSelfSignupEnv === "false" || allowSelfSignupEnv === "0") {
|
|
53
53
|
env.allowSelfSignup = false;
|
|
54
54
|
}
|
|
55
|
-
|
|
55
|
+
function installGracefulShutdown(engine) {
|
|
56
|
+
let shuttingDown = false;
|
|
57
|
+
const onSignal = (signal) => {
|
|
58
|
+
if (shuttingDown) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
shuttingDown = true;
|
|
62
|
+
console.log(`Received ${signal}; shutting down`);
|
|
63
|
+
engine
|
|
64
|
+
.shutdown()
|
|
65
|
+
.then(() => process.exit(0))
|
|
66
|
+
.catch((err) => {
|
|
67
|
+
console.error("Shutdown failed.");
|
|
68
|
+
console.error(err);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
process.on("SIGTERM", onSignal);
|
|
73
|
+
process.on("SIGINT", onSignal);
|
|
74
|
+
}
|
|
75
|
+
(0, index_1.startEngine)(env)
|
|
76
|
+
.then((engine) => {
|
|
77
|
+
installGracefulShutdown(engine);
|
|
78
|
+
})
|
|
79
|
+
.catch((err) => {
|
|
56
80
|
console.error("Failed to start engine.");
|
|
57
81
|
console.error(err);
|
|
58
82
|
process.exit(1);
|
|
@@ -13,7 +13,7 @@ const skyProtocol_1 = require("./skyProtocol");
|
|
|
13
13
|
const veramoDidComm_1 = require("./veramoDidComm");
|
|
14
14
|
const errors_1 = require("./errors");
|
|
15
15
|
function remotePeerDid(sub) {
|
|
16
|
-
const did = sub.remotePeerDid || sub.remoteDid;
|
|
16
|
+
const did = sub.remotePeerDid || sub.remoteDid || sub.remoteWebvhDid;
|
|
17
17
|
if (!did) {
|
|
18
18
|
throw new errors_1.IdentityError("Subscription missing remote peer DID", 400);
|
|
19
19
|
}
|
|
@@ -65,16 +65,31 @@ async function crossPicoEvent(deps, fromPicoId, toPicoId, subscriptionId, event)
|
|
|
65
65
|
(await resolveRecipientPicoId(deps.store, deps.pf, fromPicoId, subscriptionId));
|
|
66
66
|
if (to &&
|
|
67
67
|
(await usesLocalDispatch(deps.store, deps.pf, fromPicoId, to, sub))) {
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
68
|
+
const rxEci = await resolveRecipientRxEci(deps, to, subscriptionId);
|
|
69
|
+
if (rxEci) {
|
|
70
|
+
return deps.pf.eventWait({
|
|
71
|
+
eci: rxEci,
|
|
72
|
+
domain: event.domain,
|
|
73
|
+
name: event.name,
|
|
74
|
+
data: { attrs },
|
|
75
|
+
time: 0,
|
|
76
|
+
}, fromPicoId);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
return await sendSkyEvent(deps, fromPicoId, sub, event, attrs);
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
if (isRemovalNotification(event.name)) {
|
|
84
|
+
return { eid: "none", responses: [] };
|
|
85
|
+
}
|
|
86
|
+
throw err;
|
|
76
87
|
}
|
|
77
|
-
|
|
88
|
+
}
|
|
89
|
+
function isRemovalNotification(name) {
|
|
90
|
+
return (name === "inbound_removal" ||
|
|
91
|
+
name === "outbound_removal" ||
|
|
92
|
+
name === "established_removal");
|
|
78
93
|
}
|
|
79
94
|
async function crossPicoQuery(deps, fromPicoId, toPicoId, subscriptionId, query) {
|
|
80
95
|
const sub = await deps.store.getPeerSubscription(fromPicoId, subscriptionId);
|
|
@@ -85,22 +100,67 @@ async function crossPicoQuery(deps, fromPicoId, toPicoId, subscriptionId, query)
|
|
|
85
100
|
(await resolveRecipientPicoId(deps.store, deps.pf, fromPicoId, subscriptionId));
|
|
86
101
|
if (to &&
|
|
87
102
|
(await usesLocalDispatch(deps.store, deps.pf, fromPicoId, to, sub))) {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
103
|
+
const rxEci = await resolveRecipientRxEci(deps, to, subscriptionId);
|
|
104
|
+
if (rxEci) {
|
|
105
|
+
return deps.pf.query({
|
|
106
|
+
eci: rxEci,
|
|
107
|
+
rid: query.rid,
|
|
108
|
+
name: query.name,
|
|
109
|
+
args: query.args || {},
|
|
110
|
+
}, fromPicoId);
|
|
111
|
+
}
|
|
95
112
|
}
|
|
96
113
|
return sendSkyQuery(deps, fromPicoId, sub, query);
|
|
97
114
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
115
|
+
function deliveryEci(pf, picoId, preferred) {
|
|
116
|
+
if (preferred) {
|
|
117
|
+
return preferred;
|
|
118
|
+
}
|
|
119
|
+
const pico = pf.loadedPicos().find((p) => p.id === picoId);
|
|
120
|
+
if (!pico) {
|
|
121
|
+
throw new errors_1.IdentityError(`Pico ${picoId} not loaded`, 404);
|
|
102
122
|
}
|
|
103
|
-
|
|
123
|
+
for (const ch of Object.values(pico.channels)) {
|
|
124
|
+
const ro = ch.toReadOnly();
|
|
125
|
+
if (ro.tags.includes("wellKnown_Rx")) {
|
|
126
|
+
return ro.id;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
for (const ch of Object.values(pico.channels)) {
|
|
130
|
+
const ro = ch.toReadOnly();
|
|
131
|
+
if (ro.tags.includes("allow-all")) {
|
|
132
|
+
return ro.id;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const first = Object.keys(pico.channels)[0];
|
|
136
|
+
if (!first) {
|
|
137
|
+
throw new errors_1.IdentityError("Pico has no channel for cross-pico delivery", 404);
|
|
138
|
+
}
|
|
139
|
+
return first;
|
|
140
|
+
}
|
|
141
|
+
/** Resolve Rx ECI on the recipient for local dispatch (identity store, then inbound bus). */
|
|
142
|
+
async function resolveRecipientRxEci(deps, toPicoId, subscriptionId) {
|
|
143
|
+
const recipientSub = await deps.store.getPeerSubscription(toPicoId, subscriptionId);
|
|
144
|
+
if (recipientSub === null || recipientSub === void 0 ? void 0 : recipientSub.rxEci) {
|
|
145
|
+
return recipientSub.rxEci;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const queryEci = deliveryEci(deps.pf, toPicoId, recipientSub === null || recipientSub === void 0 ? void 0 : recipientSub.rxEci);
|
|
149
|
+
const inbound = (await deps.pf.query({
|
|
150
|
+
eci: queryEci,
|
|
151
|
+
rid: "io.picolabs.subscription",
|
|
152
|
+
name: "inbound",
|
|
153
|
+
args: {},
|
|
154
|
+
}, toPicoId));
|
|
155
|
+
const bus = inbound.find((b) => b.Id === subscriptionId);
|
|
156
|
+
if (typeof (bus === null || bus === void 0 ? void 0 : bus.Rx) === "string" && bus.Rx.length > 0) {
|
|
157
|
+
return bus.Rx;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch (_a) {
|
|
161
|
+
// fall through to DIDComm or no-op
|
|
162
|
+
}
|
|
163
|
+
return undefined;
|
|
104
164
|
}
|
|
105
165
|
async function sendSkyEvent(deps, fromPicoId, sub, event, attrs) {
|
|
106
166
|
const message = deps.identity.generateMessage({
|
|
@@ -59,6 +59,10 @@ async function postJwe(endpoint, jwe) {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
async function sendSkyIntro(deps, picoId, input) {
|
|
62
|
+
const senderWebvh = await deps.store.getWebvhDid(picoId);
|
|
63
|
+
if (senderWebvh && input.targetDid === senderWebvh) {
|
|
64
|
+
throw new errors_1.IdentityError("Cannot form a relationship with self", 400);
|
|
65
|
+
}
|
|
62
66
|
const established = await (0, establishSubscription_1.establishSubscriptionIdentity)(deps.store, deps.pf, picoId, {
|
|
63
67
|
subscriptionId: input.subscriptionId,
|
|
64
68
|
channelName: input.name,
|
|
@@ -77,7 +81,6 @@ async function sendSkyIntro(deps, picoId, input) {
|
|
|
77
81
|
Tx_host: input.Tx_host || deps.getBaseUrl(),
|
|
78
82
|
subscription_id: input.subscriptionId,
|
|
79
83
|
};
|
|
80
|
-
const senderWebvh = await deps.store.getWebvhDid(picoId);
|
|
81
84
|
if (senderWebvh) {
|
|
82
85
|
body.sender_webvh = senderWebvh;
|
|
83
86
|
try {
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.startEngine = startEngine;
|
|
4
4
|
const krl_stdlib_1 = require("krl-stdlib");
|
|
5
|
-
const classic_level_1 = require("classic-level");
|
|
6
5
|
const _ = require("lodash");
|
|
7
6
|
const makeDir = require("make-dir");
|
|
8
7
|
const path = require("path");
|
|
@@ -11,6 +10,7 @@ const auth_1 = require("./auth");
|
|
|
11
10
|
const identity_1 = require("./identity");
|
|
12
11
|
const oauth_1 = require("./oauth");
|
|
13
12
|
const logging_1 = require("./logging");
|
|
13
|
+
const levelDb_1 = require("./levelDb");
|
|
14
14
|
const provisionRoot_1 = require("./provisionRoot");
|
|
15
15
|
const RulesetRegistryLoaderFs_1 = require("./RulesetRegistryLoaderFs");
|
|
16
16
|
const server_1 = require("./server");
|
|
@@ -35,21 +35,26 @@ async function startEngine(configuration = {}) {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
await makeDir(home);
|
|
38
|
-
const logFilePath =
|
|
38
|
+
const logFilePath = (0, logging_1.resolveEngineLogFilePath)(home);
|
|
39
39
|
const log = configuration.log
|
|
40
40
|
? configuration.log
|
|
41
|
-
: (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.
|
|
41
|
+
: (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.makeEngineLogWriter)(logFilePath));
|
|
42
|
+
const db = await (0, levelDb_1.openClassicLevelWithRecovery)(path.resolve(home, "db"), {
|
|
43
|
+
keyEncoding: charwise,
|
|
44
|
+
valueEncoding: safeJsonCodec,
|
|
45
|
+
}, log);
|
|
46
|
+
const rulesetLoaderHandle = await (0, RulesetRegistryLoaderFs_1.createRulesetRegistryLoaderFs)(home, log);
|
|
42
47
|
const core = new pico_engine_core_1.PicoEngineCore({
|
|
43
|
-
db
|
|
44
|
-
|
|
45
|
-
valueEncoding: safeJsonCodec,
|
|
46
|
-
}),
|
|
47
|
-
rsRegLoader: (0, RulesetRegistryLoaderFs_1.RulesetRegistryLoaderFs)(home),
|
|
48
|
+
db,
|
|
49
|
+
rsRegLoader: rulesetLoaderHandle.loader,
|
|
48
50
|
log,
|
|
49
51
|
modules: configuration.modules,
|
|
50
52
|
useEventInputTime: configuration.useEventInputTime,
|
|
51
53
|
autoCreateRootPico: (_a = configuration.autoCreateRootPico) !== null && _a !== void 0 ? _a : false,
|
|
52
54
|
getPicoLogs(picoId) {
|
|
55
|
+
if (!logFilePath) {
|
|
56
|
+
return Promise.resolve([]);
|
|
57
|
+
}
|
|
53
58
|
return (0, logging_1.getPicoLogs)(logFilePath, picoId);
|
|
54
59
|
},
|
|
55
60
|
});
|
|
@@ -67,7 +72,15 @@ async function startEngine(configuration = {}) {
|
|
|
67
72
|
}
|
|
68
73
|
};
|
|
69
74
|
core.modules["dido"] = (0, identity_1.initDidoModule)(identityService);
|
|
70
|
-
|
|
75
|
+
try {
|
|
76
|
+
await core.start();
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
log.error("Engine startup failed", { error: err });
|
|
80
|
+
await rulesetLoaderHandle.close().catch(() => undefined);
|
|
81
|
+
await db.close().catch(() => undefined);
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
71
84
|
const rsRegistry = core.rsRegistry;
|
|
72
85
|
// Fresh engines boot with zero roots. Adopt + (re)provision only when roots
|
|
73
86
|
// already exist (test harness with autoCreateRootPico, or a migrated DB).
|
|
@@ -128,10 +141,11 @@ async function startEngine(configuration = {}) {
|
|
|
128
141
|
if ((!port || !_.isInteger(port) || port < 1) && port !== 0) {
|
|
129
142
|
port = process.env.NODE_ENV === "test" ? 0 : 3000;
|
|
130
143
|
}
|
|
144
|
+
let httpServer;
|
|
131
145
|
await new Promise((resolve) => {
|
|
132
|
-
|
|
133
|
-
if (
|
|
134
|
-
const addr =
|
|
146
|
+
httpServer = app.listen(port, () => {
|
|
147
|
+
if (httpServer) {
|
|
148
|
+
const addr = httpServer.address();
|
|
135
149
|
if (addr && typeof addr !== "string" && _.isInteger(addr.port)) {
|
|
136
150
|
// Get the actual port i.e. if they set port to 0 nodejs will assign you an available port
|
|
137
151
|
port = addr.port;
|
|
@@ -157,6 +171,24 @@ async function startEngine(configuration = {}) {
|
|
|
157
171
|
log.error("Error signaling engine:started event", { error });
|
|
158
172
|
});
|
|
159
173
|
}
|
|
174
|
+
let shuttingDown = false;
|
|
175
|
+
async function shutdown() {
|
|
176
|
+
if (shuttingDown) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
shuttingDown = true;
|
|
180
|
+
log.info("Shutting down pico-engine");
|
|
181
|
+
await new Promise((resolve, reject) => {
|
|
182
|
+
if (!httpServer) {
|
|
183
|
+
resolve(undefined);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
httpServer.close((err) => (err ? reject(err) : resolve(undefined)));
|
|
187
|
+
});
|
|
188
|
+
await rulesetLoaderHandle.close();
|
|
189
|
+
await db.close();
|
|
190
|
+
log.info("Shutdown complete");
|
|
191
|
+
}
|
|
160
192
|
return {
|
|
161
193
|
version,
|
|
162
194
|
home,
|
|
@@ -169,5 +201,6 @@ async function startEngine(configuration = {}) {
|
|
|
169
201
|
oauth,
|
|
170
202
|
identity,
|
|
171
203
|
identityService,
|
|
204
|
+
shutdown,
|
|
172
205
|
};
|
|
173
206
|
}
|