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/src/levelDb.ts ADDED
@@ -0,0 +1,122 @@
1
+ import { ClassicLevel } from "classic-level";
2
+ import * as fs from "fs/promises";
3
+ import * as makeDir from "make-dir";
4
+ import * as path from "path";
5
+ import type { KrlLogger } from "krl-stdlib";
6
+
7
+ export type ClassicLevelOptions = ConstructorParameters<typeof ClassicLevel>[1];
8
+
9
+ function errCode(err: unknown): string | undefined {
10
+ if (err && typeof err === "object" && "code" in err) {
11
+ const code = (err as { code?: unknown }).code;
12
+ return typeof code === "string" ? code : undefined;
13
+ }
14
+ return undefined;
15
+ }
16
+
17
+ function errMessage(err: unknown): string {
18
+ if (err instanceof Error) {
19
+ return err.message;
20
+ }
21
+ return String(err);
22
+ }
23
+
24
+ export function isLevelDbOpenError(err: unknown): boolean {
25
+ const code = errCode(err);
26
+ if (code === "LEVEL_LOCKED") {
27
+ return true;
28
+ }
29
+ const message = errMessage(err);
30
+ return /LOCK|corrupt|IO error|database/i.test(message);
31
+ }
32
+
33
+ async function removeStaleLockFile(
34
+ location: string,
35
+ log?: KrlLogger
36
+ ): Promise<void> {
37
+ const lockPath = path.join(location, "LOCK");
38
+ try {
39
+ await fs.unlink(lockPath);
40
+ log?.info("Removed stale LevelDB LOCK file", { location });
41
+ } catch (err: any) {
42
+ if (err?.code !== "ENOENT") {
43
+ throw err;
44
+ }
45
+ }
46
+ }
47
+
48
+ async function closeClassicLevelQuietly(
49
+ db: ClassicLevel<any, any>
50
+ ): Promise<void> {
51
+ try {
52
+ await db.close();
53
+ } catch {
54
+ // May never have opened.
55
+ }
56
+ }
57
+
58
+ async function openOnce<K = any, V = any>(
59
+ location: string,
60
+ options?: ClassicLevelOptions
61
+ ): Promise<ClassicLevel<K, V>> {
62
+ const db = new ClassicLevel<K, V>(
63
+ location,
64
+ options as ConstructorParameters<typeof ClassicLevel<K, V>>[1]
65
+ );
66
+ try {
67
+ await db.open();
68
+ return db;
69
+ } catch (err) {
70
+ // A failed open can still leave a handle; if it is GC'd later it closes
71
+ // the underlying store and breaks a subsequent successful open on the same path.
72
+ await closeClassicLevelQuietly(db);
73
+ throw err;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Open a ClassicLevel database, recovering from stale locks or minor corruption
79
+ * left by an unclean shutdown (e.g. container SIGKILL).
80
+ *
81
+ * Recovery order (non-destructive first):
82
+ * 1. Normal open
83
+ * 2. Remove stale LOCK and retry
84
+ * 3. LevelDB repair and retry
85
+ */
86
+ export async function openClassicLevelWithRecovery<K = any, V = any>(
87
+ location: string,
88
+ options?: ClassicLevelOptions,
89
+ log?: KrlLogger
90
+ ): Promise<ClassicLevel<K, V>> {
91
+ await makeDir(location);
92
+
93
+ try {
94
+ return await openOnce(location, options);
95
+ } catch (firstErr) {
96
+ if (!isLevelDbOpenError(firstErr)) {
97
+ throw firstErr;
98
+ }
99
+ log?.warn("LevelDB open failed; attempting recovery", {
100
+ location,
101
+ error: firstErr,
102
+ });
103
+ }
104
+
105
+ await removeStaleLockFile(location, log);
106
+ try {
107
+ return await openOnce(location, options);
108
+ } catch (secondErr) {
109
+ if (!isLevelDbOpenError(secondErr)) {
110
+ throw secondErr;
111
+ }
112
+ log?.warn("LevelDB open failed after lock cleanup; running repair", {
113
+ location,
114
+ error: secondErr,
115
+ });
116
+ }
117
+
118
+ await ClassicLevel.repair(location);
119
+ log?.info("LevelDB repair finished", { location });
120
+
121
+ return openOnce(location, options);
122
+ }
package/src/logging.ts CHANGED
@@ -6,10 +6,31 @@ import * as path from "path";
6
6
  const rfs = require("rotating-file-stream");
7
7
 
8
8
  const logStreams: { [filePath: string]: NodeJS.WritableStream } = {};
9
+
10
+ function stdoutOnlyLogWriter(): (line: string) => void {
11
+ const isTest = process.env.NODE_ENV === "test";
12
+ return (line: string) => {
13
+ if (!isTest) {
14
+ process.stdout.write(line);
15
+ }
16
+ };
17
+ }
18
+
19
+ function attachSafeLogStreamHandlers(
20
+ fileStream: NodeJS.WritableStream,
21
+ filePath: string
22
+ ): void {
23
+ fileStream.on("error", (err: Error) => {
24
+ process.stderr.write(
25
+ `pico-engine log file write failed (${filePath}): ${err.message}\n`
26
+ );
27
+ });
28
+ }
29
+
9
30
  function getRotatingFileStream(filePath: string): NodeJS.WritableStream {
10
31
  if (!logStreams[filePath]) {
11
32
  const filename = path.basename(filePath);
12
- logStreams[filePath] = rfs(
33
+ const fileStream = rfs(
13
34
  (time: Date, index: number) => {
14
35
  if (!time) return filename;
15
36
 
@@ -18,16 +39,43 @@ function getRotatingFileStream(filePath: string): NodeJS.WritableStream {
18
39
  {
19
40
  path: path.dirname(filePath),
20
41
 
21
- size: "100M", // rotate every 10 MegaBytes written
42
+ size: "100M", // rotate every 100 MegaBytes written
22
43
  maxFiles: 12,
23
- },
44
+ }
24
45
  ) as NodeJS.WritableStream;
46
+ attachSafeLogStreamHandlers(fileStream, filePath);
47
+ logStreams[filePath] = fileStream;
25
48
  }
26
49
  return logStreams[filePath];
27
50
  }
28
51
 
52
+ export function resolveEngineLogFilePath(home: string): string | null {
53
+ const configured = process.env.PICO_ENGINE_LOG_FILE;
54
+ if (
55
+ configured === "0" ||
56
+ configured === "-" ||
57
+ configured === "stdout" ||
58
+ configured === ""
59
+ ) {
60
+ return null;
61
+ }
62
+ if (typeof configured === "string" && configured.length > 0) {
63
+ return path.resolve(configured);
64
+ }
65
+ return path.resolve(home, "pico-engine.log");
66
+ }
67
+
68
+ export function makeEngineLogWriter(
69
+ filePath: string | null
70
+ ): (line: string) => void {
71
+ if (!filePath) {
72
+ return stdoutOnlyLogWriter();
73
+ }
74
+ return makeRotatingFileLogWriter(filePath);
75
+ }
76
+
29
77
  export function makeRotatingFileLogWriter(
30
- filePath: string,
78
+ filePath: string
31
79
  ): (line: string) => void {
32
80
  const fileStream = getRotatingFileStream(filePath);
33
81
  const isTest = process.env.NODE_ENV === "test";
@@ -36,7 +84,13 @@ export function makeRotatingFileLogWriter(
36
84
  if (!isTest) {
37
85
  process.stdout.write(line);
38
86
  }
39
- fileStream.write(line);
87
+ fileStream.write(line, (err?: Error | null) => {
88
+ if (err) {
89
+ process.stderr.write(
90
+ `pico-engine log file write failed (${filePath}): ${err.message}\n`
91
+ );
92
+ }
93
+ });
40
94
  }
41
95
 
42
96
  return write;
@@ -44,10 +98,14 @@ export function makeRotatingFileLogWriter(
44
98
 
45
99
  export async function getPicoLogs(
46
100
  filePath: string,
47
- picoId: string,
101
+ picoId: string
48
102
  ): Promise<PicoLogEntry[]> {
49
103
  const output: PicoLogEntry[] = [];
50
104
 
105
+ if (!(await fs.promises.stat(filePath).catch(() => null))) {
106
+ return output;
107
+ }
108
+
51
109
  const rl = readline.createInterface({
52
110
  input: fs.createReadStream(filePath),
53
111
  });
@@ -1,7 +1,10 @@
1
1
  import * as crypto from "crypto";
2
2
  import { ChannelReadOnly, PicoDb, PicoDbKey, PicoFramework } from "pico-framework";
3
3
  import * as acg from "./acg";
4
- import { isOAuthEligibleChannel } from "./channelEligibility";
4
+ import {
5
+ isMeshOAuthExemptChannel,
6
+ isOAuthEligibleChannel,
7
+ } from "./channelEligibility";
5
8
  import { OAuthError } from "./errors";
6
9
  import { isChannelUnderRoot, meshRequiresOAuth as meshRequiresOAuthForEci } from "./meshOAuth";
7
10
 
@@ -240,12 +243,15 @@ export class OAuthService {
240
243
  ) {
241
244
  return false;
242
245
  }
246
+ if (isOAuthEligibleChannel(channel)) {
247
+ return true;
248
+ }
249
+ if (isMeshOAuthExemptChannel(channel)) {
250
+ return false;
251
+ }
243
252
  } catch (_e) {
244
253
  // channel not found — fall through
245
254
  }
246
- if (await this.channelRequiresBearer(eci)) {
247
- return true;
248
- }
249
255
  return this.meshRequiresOAuth(eci);
250
256
  }
251
257
 
@@ -2,6 +2,9 @@ import { ChannelReadOnly } from "pico-framework";
2
2
 
3
3
  export const OAUTH_WEBHOOK_TAG = "oauth-webhook";
4
4
 
5
+ /** Opt out of mesh-wide /sky/* OAuth when the root has io.picolabs.oauth installed. */
6
+ export const MESH_OAUTH_EXEMPT_TAG = "mesh-oauth-exempt";
7
+
5
8
  const SUBSCRIPTION_CHANNEL_TAGS = new Set([
6
9
  "wellknown_rx",
7
10
  "tx_rx",
@@ -24,3 +27,9 @@ export function isOAuthEligibleChannel(channel: ChannelReadOnly): boolean {
24
27
  }
25
28
  return channel.tags.includes(OAUTH_WEBHOOK_TAG);
26
29
  }
30
+
31
+ export function isMeshOAuthExemptChannel(channel: ChannelReadOnly): boolean {
32
+ return channel.tags.some(
33
+ (tag) => tag.toLowerCase() === MESH_OAUTH_EXEMPT_TAG
34
+ );
35
+ }
@@ -9,6 +9,12 @@ export {
9
9
  parseAuthorizeQuery,
10
10
  renderConsentHtml,
11
11
  } from "./OAuthService";
12
- export { OAUTH_WEBHOOK_TAG, isOAuthEligibleChannel, isSubscriptionChannel } from "./channelEligibility";
12
+ export {
13
+ OAUTH_WEBHOOK_TAG,
14
+ MESH_OAUTH_EXEMPT_TAG,
15
+ isOAuthEligibleChannel,
16
+ isMeshOAuthExemptChannel,
17
+ isSubscriptionChannel,
18
+ } from "./channelEligibility";
13
19
  export { OAUTH_MESH_RULESET_RID, meshRequiresOAuth, rootHasOAuthMeshRuleset, rootPicoIdForChannelEci, isChannelUnderRoot } from "./meshOAuth";
14
20
  export { default as initOAuthModule } from "./krlModule";
package/src/server.ts CHANGED
@@ -1,13 +1,26 @@
1
1
  import * as bodyParser from "body-parser";
2
2
  import * as express from "express";
3
3
  import { Express, Request, Response } from "express";
4
+ import * as fs from "fs";
4
5
  import helmet from "helmet";
5
6
  import * as _ from "lodash";
6
7
  import * as path from "path";
8
+ import {
9
+ bundledKrlDir,
10
+ listEngineKrlSources,
11
+ resolveEngineKrlFile,
12
+ } from "./bundledKrl";
7
13
  import { PicoEngineCore } from "pico-engine-core";
8
14
  import { PicoFramework } from "pico-framework";
9
15
  import { AuthError, AuthService } from "./auth";
10
- import { OAuthError, OAuthService, parseApproveBody, parseAuthorizeQuery, renderConsentHtml } from "./oauth";
16
+ import {
17
+ OAuthError,
18
+ OAuthService,
19
+ parseApproveBody,
20
+ parseAuthorizeQuery,
21
+ renderConsentHtml,
22
+ rootHasOAuthMeshRuleset,
23
+ } from "./oauth";
11
24
  import { uiECIForRoot } from "./provisionRoot";
12
25
  import { IdentityService } from "./identity/IdentityService";
13
26
  import { registerWebvhRoutes } from "./identity/webvhRoutes";
@@ -254,6 +267,14 @@ export function server(
254
267
  registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookies);
255
268
  }
256
269
 
270
+ app.get("/api/oauth/mesh-enabled", requireAuthSession, function (_req, res) {
271
+ const roots = pf.rootPicos();
272
+ const meshOAuthEnabled =
273
+ roots.length > 0 &&
274
+ roots.some((root) => rootHasOAuthMeshRuleset(pf, root.id));
275
+ res.json({ meshOAuthEnabled });
276
+ });
277
+
257
278
  app.get("/api/mesh-context", function (req, res, next) {
258
279
  if (!oauth) {
259
280
  res.status(503).json({ error: "OAuth not enabled" });
@@ -295,6 +316,38 @@ export function server(
295
316
  .catch(next);
296
317
  });
297
318
 
319
+ app.get("/api/krl-sources", function (req, res, next) {
320
+ try {
321
+ let baseUrl = core.base_url;
322
+ if (!baseUrl) {
323
+ baseUrl = `${req.protocol}://${req.get("host") || "localhost"}`;
324
+ }
325
+ res.json({ sources: listEngineKrlSources(baseUrl.replace(/\/$/, "")) });
326
+ } catch (err) {
327
+ next(err);
328
+ }
329
+ });
330
+
331
+ app.get(/^\/krl\/(.+\.krl)$/, function (req, res, next) {
332
+ const suffix = req.params[0];
333
+ if (typeof suffix !== "string" || !suffix.endsWith(".krl")) {
334
+ res.status(404).end();
335
+ return;
336
+ }
337
+ const filePath = resolveEngineKrlFile(suffix);
338
+ if (!filePath) {
339
+ res.status(404).end();
340
+ return;
341
+ }
342
+ fs.readFile(filePath, { encoding: "utf8" }, function (err, data) {
343
+ if (err) {
344
+ next(err);
345
+ return;
346
+ }
347
+ res.type("text/plain").send(data);
348
+ });
349
+ });
350
+
298
351
  app.get("/api/ui-context", function (req, res, next) {
299
352
  const hasRoots = pf.rootPicos().length > 0;
300
353
  const base: any = {
@@ -16,12 +16,18 @@ export const UI_CHANNEL_EVENT_POLICY: EventPolicy = {
16
16
  { domain: "engine_ui", name: "testing_eci" },
17
17
  { domain: "engine", name: "started" },
18
18
  { domain: "wrangler", name: "subscription" },
19
+ { domain: "wrangler", name: "relationship" },
19
20
  { domain: "wrangler", name: "pending_subscription_approval" },
21
+ { domain: "wrangler", name: "pending_relationship_approval" },
20
22
  { domain: "wrangler", name: "inbound_rejection" },
23
+ { domain: "wrangler", name: "inbound_relationship_rejection" },
21
24
  { domain: "wrangler", name: "outbound_cancellation" },
25
+ { domain: "wrangler", name: "outbound_relationship_cancellation" },
22
26
  { domain: "wrangler", name: "subscription_cancellation" },
27
+ { domain: "wrangler", name: "relationship_cancellation" },
23
28
  { domain: "wrangler", name: "set_public_intro" },
24
29
  { domain: "wrangler", name: "send_event_on_subs" },
30
+ { domain: "wrangler", name: "send_event_on_relationships" },
25
31
  ],
26
32
  deny: [],
27
33
  };
@@ -1,78 +0,0 @@
1
- ruleset didtestdid {
2
- meta {
3
- name "didtestdid"
4
- description
5
- <<
6
- A Test Ruleset Orchestrator for using DIDs with event:send() and wrangler:picoQuery()
7
- >>
8
- author "Josh Mann"
9
- shares child1DidMap, child2DidMap
10
- use module io.picolabs.wrangler alias wrangler
11
- use module io.picolabs.did-o alias didx
12
- }
13
-
14
- global {
15
- child1DidMap = function() {
16
- wrangler:picoQuery(ent:child1, "io.picolabs.did-o", "didMap", {})
17
- }
18
-
19
- child2DidMap = function() {
20
- wrangler:picoQuery(ent:child2, "io.picolabs.did-o", "didMap", {})
21
- }
22
- }
23
-
24
- rule createChildren {
25
- select when didtestdid createChildren
26
- always {
27
- raise wrangler event "new_child_request" attributes {"name": "child1", "backgroundColor": "#ae85fa"}
28
- raise wrangler event "new_child_request" attributes {"name": "child2", "backgroundColor": "#fef1b4"}
29
- }
30
- }
31
-
32
- rule createDIDRelationshipWithChildren {
33
- select when didtestdid createDIDRelationshipWithChildren
34
- foreach ctx:children setting(child)
35
- pre {
36
- invitation = didx:generate_invitation()
37
- }
38
- event:send({
39
- "eci": child,
40
- "domain": "dido",
41
- "name": "receive_invite",
42
- "attrs": {"invite": invitation}
43
- })
44
- }
45
-
46
- rule saveChildDids {
47
- select when didtestdid saveChildDids
48
- always {
49
- ent:child1 := didx:didMap().keys()[0]
50
- ent:child2 := didx:didMap().keys()[1]
51
- }
52
- }
53
-
54
- rule createDIDRelationship {
55
- select when didtestdid createDIDRelationship
56
- pre {
57
- child1 = ent:child1.klog("Child1: ")
58
- child2 = ent:child2.klog("Child2: ")
59
- invitation = wrangler:picoQuery(child1, "io.picolabs.did-o", "generate_invitation", {}).klog("Invitation: ")
60
- }
61
- event:send({
62
- "did": child2,
63
- "domain": "dido",
64
- "name": "receive_invite",
65
- "attrs": {
66
- "invite": invitation
67
- }
68
- })
69
- }
70
-
71
- rule deleteChildren {
72
- select when didtestdid deleteChildren
73
- foreach ctx:children setting(child)
74
- always {
75
- raise wrangler event "child_deletion_request" attributes {"eci": child}
76
- }
77
- }
78
- }