pico-engine 1.6.2 → 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.
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isLevelDbOpenError = isLevelDbOpenError;
4
+ exports.openClassicLevelWithRecovery = openClassicLevelWithRecovery;
5
+ const classic_level_1 = require("classic-level");
6
+ const fs = require("fs/promises");
7
+ const makeDir = require("make-dir");
8
+ const path = require("path");
9
+ function errCode(err) {
10
+ if (err && typeof err === "object" && "code" in err) {
11
+ const code = err.code;
12
+ return typeof code === "string" ? code : undefined;
13
+ }
14
+ return undefined;
15
+ }
16
+ function errMessage(err) {
17
+ if (err instanceof Error) {
18
+ return err.message;
19
+ }
20
+ return String(err);
21
+ }
22
+ function isLevelDbOpenError(err) {
23
+ const code = errCode(err);
24
+ if (code === "LEVEL_LOCKED") {
25
+ return true;
26
+ }
27
+ const message = errMessage(err);
28
+ return /LOCK|corrupt|IO error|database/i.test(message);
29
+ }
30
+ async function removeStaleLockFile(location, log) {
31
+ const lockPath = path.join(location, "LOCK");
32
+ try {
33
+ await fs.unlink(lockPath);
34
+ log === null || log === void 0 ? void 0 : log.info("Removed stale LevelDB LOCK file", { location });
35
+ }
36
+ catch (err) {
37
+ if ((err === null || err === void 0 ? void 0 : err.code) !== "ENOENT") {
38
+ throw err;
39
+ }
40
+ }
41
+ }
42
+ async function closeClassicLevelQuietly(db) {
43
+ try {
44
+ await db.close();
45
+ }
46
+ catch (_a) {
47
+ // May never have opened.
48
+ }
49
+ }
50
+ async function openOnce(location, options) {
51
+ const db = new classic_level_1.ClassicLevel(location, options);
52
+ try {
53
+ await db.open();
54
+ return db;
55
+ }
56
+ catch (err) {
57
+ // A failed open can still leave a handle; if it is GC'd later it closes
58
+ // the underlying store and breaks a subsequent successful open on the same path.
59
+ await closeClassicLevelQuietly(db);
60
+ throw err;
61
+ }
62
+ }
63
+ /**
64
+ * Open a ClassicLevel database, recovering from stale locks or minor corruption
65
+ * left by an unclean shutdown (e.g. container SIGKILL).
66
+ *
67
+ * Recovery order (non-destructive first):
68
+ * 1. Normal open
69
+ * 2. Remove stale LOCK and retry
70
+ * 3. LevelDB repair and retry
71
+ */
72
+ async function openClassicLevelWithRecovery(location, options, log) {
73
+ await makeDir(location);
74
+ try {
75
+ return await openOnce(location, options);
76
+ }
77
+ catch (firstErr) {
78
+ if (!isLevelDbOpenError(firstErr)) {
79
+ throw firstErr;
80
+ }
81
+ log === null || log === void 0 ? void 0 : log.warn("LevelDB open failed; attempting recovery", {
82
+ location,
83
+ error: firstErr,
84
+ });
85
+ }
86
+ await removeStaleLockFile(location, log);
87
+ try {
88
+ return await openOnce(location, options);
89
+ }
90
+ catch (secondErr) {
91
+ if (!isLevelDbOpenError(secondErr)) {
92
+ throw secondErr;
93
+ }
94
+ log === null || log === void 0 ? void 0 : log.warn("LevelDB open failed after lock cleanup; running repair", {
95
+ location,
96
+ error: secondErr,
97
+ });
98
+ }
99
+ await classic_level_1.ClassicLevel.repair(location);
100
+ log === null || log === void 0 ? void 0 : log.info("LevelDB repair finished", { location });
101
+ return openOnce(location, options);
102
+ }
package/dist/logging.js CHANGED
@@ -7,6 +7,8 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
7
7
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
8
8
  };
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.resolveEngineLogFilePath = resolveEngineLogFilePath;
11
+ exports.makeEngineLogWriter = makeEngineLogWriter;
10
12
  exports.makeRotatingFileLogWriter = makeRotatingFileLogWriter;
11
13
  exports.getPicoLogs = getPicoLogs;
12
14
  const fs = require("fs");
@@ -15,21 +17,55 @@ const krl_stdlib_1 = require("krl-stdlib");
15
17
  const path = require("path");
16
18
  const rfs = require("rotating-file-stream");
17
19
  const logStreams = {};
20
+ function stdoutOnlyLogWriter() {
21
+ const isTest = process.env.NODE_ENV === "test";
22
+ return (line) => {
23
+ if (!isTest) {
24
+ process.stdout.write(line);
25
+ }
26
+ };
27
+ }
28
+ function attachSafeLogStreamHandlers(fileStream, filePath) {
29
+ fileStream.on("error", (err) => {
30
+ process.stderr.write(`pico-engine log file write failed (${filePath}): ${err.message}\n`);
31
+ });
32
+ }
18
33
  function getRotatingFileStream(filePath) {
19
34
  if (!logStreams[filePath]) {
20
35
  const filename = path.basename(filePath);
21
- logStreams[filePath] = rfs((time, index) => {
36
+ const fileStream = rfs((time, index) => {
22
37
  if (!time)
23
38
  return filename;
24
39
  return filename + "." + index;
25
40
  }, {
26
41
  path: path.dirname(filePath),
27
- size: "100M", // rotate every 10 MegaBytes written
42
+ size: "100M", // rotate every 100 MegaBytes written
28
43
  maxFiles: 12,
29
44
  });
45
+ attachSafeLogStreamHandlers(fileStream, filePath);
46
+ logStreams[filePath] = fileStream;
30
47
  }
31
48
  return logStreams[filePath];
32
49
  }
50
+ function resolveEngineLogFilePath(home) {
51
+ const configured = process.env.PICO_ENGINE_LOG_FILE;
52
+ if (configured === "0" ||
53
+ configured === "-" ||
54
+ configured === "stdout" ||
55
+ configured === "") {
56
+ return null;
57
+ }
58
+ if (typeof configured === "string" && configured.length > 0) {
59
+ return path.resolve(configured);
60
+ }
61
+ return path.resolve(home, "pico-engine.log");
62
+ }
63
+ function makeEngineLogWriter(filePath) {
64
+ if (!filePath) {
65
+ return stdoutOnlyLogWriter();
66
+ }
67
+ return makeRotatingFileLogWriter(filePath);
68
+ }
33
69
  function makeRotatingFileLogWriter(filePath) {
34
70
  const fileStream = getRotatingFileStream(filePath);
35
71
  const isTest = process.env.NODE_ENV === "test";
@@ -37,13 +73,20 @@ function makeRotatingFileLogWriter(filePath) {
37
73
  if (!isTest) {
38
74
  process.stdout.write(line);
39
75
  }
40
- fileStream.write(line);
76
+ fileStream.write(line, (err) => {
77
+ if (err) {
78
+ process.stderr.write(`pico-engine log file write failed (${filePath}): ${err.message}\n`);
79
+ }
80
+ });
41
81
  }
42
82
  return write;
43
83
  }
44
84
  async function getPicoLogs(filePath, picoId) {
45
85
  var _a, e_1, _b, _c;
46
86
  const output = [];
87
+ if (!(await fs.promises.stat(filePath).catch(() => null))) {
88
+ return output;
89
+ }
47
90
  const rl = readline.createInterface({
48
91
  input: fs.createReadStream(filePath),
49
92
  });
@@ -174,13 +174,16 @@ class OAuthService {
174
174
  channel.tags.includes("ingress")) {
175
175
  return false;
176
176
  }
177
+ if ((0, channelEligibility_1.isOAuthEligibleChannel)(channel)) {
178
+ return true;
179
+ }
180
+ if ((0, channelEligibility_1.isMeshOAuthExemptChannel)(channel)) {
181
+ return false;
182
+ }
177
183
  }
178
184
  catch (_e) {
179
185
  // channel not found — fall through
180
186
  }
181
- if (await this.channelRequiresBearer(eci)) {
182
- return true;
183
- }
184
187
  return this.meshRequiresOAuth(eci);
185
188
  }
186
189
  async validateBearerToken(token, channelEci) {
@@ -1,9 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OAUTH_WEBHOOK_TAG = void 0;
3
+ exports.MESH_OAUTH_EXEMPT_TAG = exports.OAUTH_WEBHOOK_TAG = void 0;
4
4
  exports.isSubscriptionChannel = isSubscriptionChannel;
5
5
  exports.isOAuthEligibleChannel = isOAuthEligibleChannel;
6
+ exports.isMeshOAuthExemptChannel = isMeshOAuthExemptChannel;
6
7
  exports.OAUTH_WEBHOOK_TAG = "oauth-webhook";
8
+ /** Opt out of mesh-wide /sky/* OAuth when the root has io.picolabs.oauth installed. */
9
+ exports.MESH_OAUTH_EXEMPT_TAG = "mesh-oauth-exempt";
7
10
  const SUBSCRIPTION_CHANNEL_TAGS = new Set([
8
11
  "wellknown_rx",
9
12
  "tx_rx",
@@ -24,3 +27,6 @@ function isOAuthEligibleChannel(channel) {
24
27
  }
25
28
  return channel.tags.includes(exports.OAUTH_WEBHOOK_TAG);
26
29
  }
30
+ function isMeshOAuthExemptChannel(channel) {
31
+ return channel.tags.some((tag) => tag.toLowerCase() === exports.MESH_OAUTH_EXEMPT_TAG);
32
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initOAuthModule = exports.isChannelUnderRoot = exports.rootPicoIdForChannelEci = exports.rootHasOAuthMeshRuleset = exports.meshRequiresOAuth = exports.OAUTH_MESH_RULESET_RID = exports.isSubscriptionChannel = exports.isOAuthEligibleChannel = exports.OAUTH_WEBHOOK_TAG = exports.renderConsentHtml = exports.parseAuthorizeQuery = exports.parseApproveBody = exports.OAuthError = exports.OAuthService = void 0;
3
+ exports.initOAuthModule = exports.isChannelUnderRoot = exports.rootPicoIdForChannelEci = exports.rootHasOAuthMeshRuleset = exports.meshRequiresOAuth = exports.OAUTH_MESH_RULESET_RID = exports.isSubscriptionChannel = exports.isMeshOAuthExemptChannel = exports.isOAuthEligibleChannel = exports.MESH_OAUTH_EXEMPT_TAG = exports.OAUTH_WEBHOOK_TAG = exports.renderConsentHtml = exports.parseAuthorizeQuery = exports.parseApproveBody = exports.OAuthError = exports.OAuthService = void 0;
4
4
  var OAuthService_1 = require("./OAuthService");
5
5
  Object.defineProperty(exports, "OAuthService", { enumerable: true, get: function () { return OAuthService_1.OAuthService; } });
6
6
  Object.defineProperty(exports, "OAuthError", { enumerable: true, get: function () { return OAuthService_1.OAuthError; } });
@@ -9,7 +9,9 @@ Object.defineProperty(exports, "parseAuthorizeQuery", { enumerable: true, get: f
9
9
  Object.defineProperty(exports, "renderConsentHtml", { enumerable: true, get: function () { return OAuthService_1.renderConsentHtml; } });
10
10
  var channelEligibility_1 = require("./channelEligibility");
11
11
  Object.defineProperty(exports, "OAUTH_WEBHOOK_TAG", { enumerable: true, get: function () { return channelEligibility_1.OAUTH_WEBHOOK_TAG; } });
12
+ Object.defineProperty(exports, "MESH_OAUTH_EXEMPT_TAG", { enumerable: true, get: function () { return channelEligibility_1.MESH_OAUTH_EXEMPT_TAG; } });
12
13
  Object.defineProperty(exports, "isOAuthEligibleChannel", { enumerable: true, get: function () { return channelEligibility_1.isOAuthEligibleChannel; } });
14
+ Object.defineProperty(exports, "isMeshOAuthExemptChannel", { enumerable: true, get: function () { return channelEligibility_1.isMeshOAuthExemptChannel; } });
13
15
  Object.defineProperty(exports, "isSubscriptionChannel", { enumerable: true, get: function () { return channelEligibility_1.isSubscriptionChannel; } });
14
16
  var meshOAuth_1 = require("./meshOAuth");
15
17
  Object.defineProperty(exports, "OAUTH_MESH_RULESET_RID", { enumerable: true, get: function () { return meshOAuth_1.OAUTH_MESH_RULESET_RID; } });
package/dist/server.js CHANGED
@@ -3,9 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.server = server;
4
4
  const bodyParser = require("body-parser");
5
5
  const express = require("express");
6
+ const fs = require("fs");
6
7
  const helmet_1 = require("helmet");
7
8
  const _ = require("lodash");
8
9
  const path = require("path");
10
+ const bundledKrl_1 = require("./bundledKrl");
9
11
  const auth_1 = require("./auth");
10
12
  const oauth_1 = require("./oauth");
11
13
  const provisionRoot_1 = require("./provisionRoot");
@@ -212,6 +214,12 @@ function server(core, uiECI, auth, oauth, identity) {
212
214
  if (oauth) {
213
215
  registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookies);
214
216
  }
217
+ app.get("/api/oauth/mesh-enabled", requireAuthSession, function (_req, res) {
218
+ const roots = pf.rootPicos();
219
+ const meshOAuthEnabled = roots.length > 0 &&
220
+ roots.some((root) => (0, oauth_1.rootHasOAuthMeshRuleset)(pf, root.id));
221
+ res.json({ meshOAuthEnabled });
222
+ });
215
223
  app.get("/api/mesh-context", function (req, res, next) {
216
224
  if (!oauth) {
217
225
  res.status(503).json({ error: "OAuth not enabled" });
@@ -252,6 +260,37 @@ function server(core, uiECI, auth, oauth, identity) {
252
260
  })
253
261
  .catch(next);
254
262
  });
263
+ app.get("/api/krl-sources", function (req, res, next) {
264
+ try {
265
+ let baseUrl = core.base_url;
266
+ if (!baseUrl) {
267
+ baseUrl = `${req.protocol}://${req.get("host") || "localhost"}`;
268
+ }
269
+ res.json({ sources: (0, bundledKrl_1.listEngineKrlSources)(baseUrl.replace(/\/$/, "")) });
270
+ }
271
+ catch (err) {
272
+ next(err);
273
+ }
274
+ });
275
+ app.get(/^\/krl\/(.+\.krl)$/, function (req, res, next) {
276
+ const suffix = req.params[0];
277
+ if (typeof suffix !== "string" || !suffix.endsWith(".krl")) {
278
+ res.status(404).end();
279
+ return;
280
+ }
281
+ const filePath = (0, bundledKrl_1.resolveEngineKrlFile)(suffix);
282
+ if (!filePath) {
283
+ res.status(404).end();
284
+ return;
285
+ }
286
+ fs.readFile(filePath, { encoding: "utf8" }, function (err, data) {
287
+ if (err) {
288
+ next(err);
289
+ return;
290
+ }
291
+ res.type("text/plain").send(data);
292
+ });
293
+ });
255
294
  app.get("/api/ui-context", function (req, res, next) {
256
295
  const hasRoots = pf.rootPicos().length > 0;
257
296
  const base = {
@@ -28,6 +28,7 @@ exports.UI_CHANNEL_EVENT_POLICY = {
28
28
  { domain: "wrangler", name: "relationship_cancellation" },
29
29
  { domain: "wrangler", name: "set_public_intro" },
30
30
  { domain: "wrangler", name: "send_event_on_subs" },
31
+ { domain: "wrangler", name: "send_event_on_relationships" },
31
32
  ],
32
33
  deny: [],
33
34
  };
@@ -3,9 +3,10 @@ ruleset io.picolabs.oauth {
3
3
  name "OAuth Mesh"
4
4
  description <<
5
5
  Optional mesh-level OAuth. Install on the root pico to require Bearer tokens on external /sky/*
6
- for this mesh, and to hold per-mesh OAuth configuration (apps, consent policy, defaults).
7
- Webhook Client Credentials (per channel) and app Authorization Code grants coexist here.
8
- Not installed by default.
6
+ for this mesh (except channels tagged mesh-oauth-exempt or didcomm+ingress), and to hold
7
+ per-mesh OAuth configuration (apps, consent policy, defaults).
8
+ Webhook Client Credentials (per channel, oauth-webhook tag) and app Authorization Code grants
9
+ coexist here. Not installed by default.
9
10
 
10
11
  use module io.picolabs.oauth alias oauth
11
12
  >>
@@ -24,7 +25,7 @@ ruleset io.picolabs.oauth {
24
25
  true
25
26
  }
26
27
 
27
- /** True when external /sky/* for eci requires a Bearer token (mesh lock or oauth-webhook tag). */
28
+ /** True when the mesh root has io.picolabs.oauth (mesh lock may still be skipped via mesh-oauth-exempt on a channel). */
28
29
  meshRequiresOAuth = function(eci) {
29
30
  oauth:meshRequiresOAuth(eci)
30
31
  }
@@ -45,4 +46,10 @@ ruleset io.picolabs.oauth {
45
46
  oauth:revokeTokens(eci)
46
47
  }
47
48
  }
49
+
50
+ // Mesh OAuth is keyed off the root pico; installing here has no effect.
51
+ rule reject_non_root_install {
52
+ select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
53
+ if ctx:parent then ctx:uninstall(ctx:rid)
54
+ }
48
55
  }
@@ -153,7 +153,8 @@ ruleset io.picolabs.pico-engine-ui {
153
153
  { "domain": "wrangler", "name": "subscription_cancellation" },
154
154
  { "domain": "wrangler", "name": "relationship_cancellation" },
155
155
  { "domain": "wrangler", "name": "set_public_intro" },
156
- { "domain": "wrangler", "name": "send_event_on_subs" }
156
+ { "domain": "wrangler", "name": "send_event_on_subs" },
157
+ { "domain": "wrangler", "name": "send_event_on_relationships" }
157
158
  ],
158
159
  "deny": []
159
160
  }
@@ -314,7 +315,15 @@ ruleset io.picolabs.pico-engine-ui {
314
315
  }
315
316
  rule install {
316
317
  select when engine_ui install
317
- fired {
318
+ pre {
319
+ url = event:attrs{"url"} || ""
320
+ oauth_url = url.match(re#io\.picolabs\.oauth#)
321
+ // OAuth mesh ruleset is root-only; allow other URLs on any pico.
322
+ block_oauth = oauth_url && ctx:parent
323
+ }
324
+ // Action-block condition sets $fired; use notfired (or always) for the install path.
325
+ if block_oauth then noop()
326
+ notfired {
318
327
  raise wrangler event "install_ruleset_request" attributes event:attrs
319
328
  }
320
329
  }
@@ -2,7 +2,8 @@ ruleset io.picolabs.subscription {
2
2
  meta {
3
3
  name "subscription "
4
4
  description <<
5
- Tx/Rx ruleset.
5
+ Pico-to-pico relationships (Tx/Rx). User-facing term: relationship.
6
+ Ruleset RID io.picolabs.subscription is stable; prefer module alias relationship.
6
7
  >>
7
8
  author "Tedrub Modulus"
8
9
  use module io.picolabs.wrangler alias wrangler
@@ -242,6 +243,7 @@ ent:established [
242
243
 
243
244
  rule cleanup_subscriptions {
244
245
  select when wrangler rulesets_need_to_cleanup
246
+ or wrangler cleanup_relationships
245
247
  always {
246
248
  raise wrangler event "cancel_subscriptions" attributes event:attrs
247
249
  }
@@ -328,7 +330,7 @@ ent:established [
328
330
  channel_type = event:attr("channel_type").defaultsTo("Tx_Rx","Tx_Rx channel_type used.")
329
331
  pending_entry = pending_entry().put(["wellKnown_Tx"],event:attr("wellKnown_Tx"))
330
332
  }
331
- if not layer2 && pending_entry{"wellKnown_Tx"} then // check if we have someone to send a request to
333
+ if not layer2 && pending_entry{"wellKnown_Tx"} && pending_entry{"wellKnown_Tx"} != wellKnown_Rx(){"id"} then // check if we have someone to send a request to
332
334
  ctx:newChannel([channel_name,channel_type],allow_all_eventPolicy,allow_all_queryPolicy) setting(channel); // create Rx
333
335
  fired {
334
336
  newBus = pending_entry.put({ "Rx" : channel{"id"} });
@@ -342,9 +344,20 @@ ent:established [
342
344
  raise wrangler event "subscription_request_needed"
343
345
  attributes event:attrs.put(fullNewBus);
344
346
  raise wrangler event "outbound_pending_subscription_added" attributes event:attrs.put(fullNewBus)// API event
347
+ raise wrangler event "outbound_pending_relationship_added" attributes event:attrs.put(fullNewBus)// API event
345
348
  }
346
349
  }//end createMySubscription rule
347
350
 
351
+ rule createRxBusLegacySelfFailure {
352
+ select when wrangler subscription
353
+ or wrangler relationship
354
+ where not event:attr("layer2") == true
355
+ && event:attr("wellKnown_Tx") == wellKnown_Rx(){"id"}
356
+ fired {
357
+ raise wrangler event "self_relationship_failure" attributes event:attrs
358
+ }
359
+ }
360
+
348
361
  rule createRxBusLegacyFailure {
349
362
  select when wrangler subscription
350
363
  or wrangler relationship
@@ -370,7 +383,7 @@ ent:established [
370
383
  .put(["target_did"], target_did)
371
384
  .put(["layer2"], true)
372
385
  }
373
- if layer2 && target_did then
386
+ if layer2 && target_did && target_did != wrangler:myDid() then
374
387
  ctx:newChannel([channel_name,channel_type],allow_all_eventPolicy,allow_all_queryPolicy) setting(channel)
375
388
  fired {
376
389
  newBus = pending_entry.put({ "Rx" : channel{"id"} })
@@ -382,6 +395,17 @@ ent:established [
382
395
  ent:outbound := outbound().append(fullNewBus)
383
396
  raise wrangler event "subscription_request_needed" attributes event:attrs.put(fullNewBus)
384
397
  raise wrangler event "outbound_pending_subscription_added" attributes event:attrs.put(fullNewBus)
398
+ raise wrangler event "outbound_pending_relationship_added" attributes event:attrs.put(fullNewBus)
399
+ }
400
+ }
401
+
402
+ rule createRxBusLayer2SelfFailure {
403
+ select when wrangler subscription
404
+ or wrangler relationship
405
+ where event:attr("layer2") == true
406
+ && event:attr("target_did") == wrangler:myDid()
407
+ fired {
408
+ raise wrangler event "self_relationship_failure" attributes event:attrs
385
409
  }
386
410
  }
387
411
 
@@ -425,7 +449,7 @@ ent:established [
425
449
  pre {
426
450
  layer2 = event:attr("layer2") == true
427
451
  target = event:attr("target_did")
428
- sent = layer2 && target => dido:sendSkyIntro({
452
+ sent = layer2 && target && target != wrangler:myDid() => dido:sendSkyIntro({
429
453
  "subscriptionId": event:attr("Id"),
430
454
  "targetDid": target,
431
455
  "name": event:attr("name").defaultsTo(event:attr("channel_name")),
@@ -452,6 +476,7 @@ ent:established [
452
476
  fired {
453
477
  ent:inbound := inbound().append(pending_entry)
454
478
  raise wrangler event "inbound_pending_subscription_added" attributes event:attrs
479
+ raise wrangler event "inbound_pending_relationship_added" attributes event:attrs
455
480
  }
456
481
  else {
457
482
  raise wrangler event "no_Tx_did_failure" attributes event:attrs
@@ -473,6 +498,7 @@ ent:established [
473
498
  });
474
499
  ent:inbound := inbound().append( newBus );
475
500
  raise wrangler event "inbound_pending_subscription_added" attributes event:attrs.put(["Rx"], Rx); // API event
501
+ raise wrangler event "inbound_pending_relationship_added" attributes event:attrs.put(["Rx"], Rx); // API event
476
502
  }
477
503
  else {
478
504
  raise wrangler event "no_Tx_failure" attributes event:attrs // API event
@@ -489,6 +515,7 @@ ent:established [
489
515
  if bus{"layer2"} == true then noop()
490
516
  fired {
491
517
  raise wrangler event "inbound_pending_subscription_approved" attributes event:attrs.put("Id", bus{"Id"}).put(["bus"],bus)
518
+ raise wrangler event "inbound_pending_relationship_approved" attributes event:attrs.put("Id", bus{"Id"}).put(["bus"],bus)
492
519
  }
493
520
  }
494
521
 
@@ -509,6 +536,7 @@ ent:established [
509
536
  }, bus{"Tx_host"})
510
537
  fired {
511
538
  raise wrangler event "inbound_pending_subscription_approved" attributes event:attrs.put("Id", bus{"Id"}).put(["bus"],bus)
539
+ raise wrangler event "inbound_pending_relationship_approved" attributes event:attrs.put("Id", bus{"Id"}).put(["bus"],bus)
512
540
  }
513
541
  }
514
542
 
@@ -528,11 +556,13 @@ ent:established [
528
556
  ent:established := established().append(updated)
529
557
  ent:outbound := buses.splice(index, 1)
530
558
  raise wrangler event "subscription_added" attributes event:attrs.put(["bus"], updated)
559
+ raise wrangler event "relationship_added" attributes event:attrs.put(["bus"], updated)
531
560
  }
532
561
  }
533
562
 
534
563
  rule addOutboundSubscription {
535
564
  select when wrangler outbound_pending_subscription_approved
565
+ or wrangler outbound_pending_relationship_approved
536
566
  pre{
537
567
  outbound = outbound()
538
568
  bus = findBus(outbound).put({"Tx" : event:attr("Tx"),
@@ -547,11 +577,13 @@ ent:established [
547
577
  ent:established := established().append(bus);
548
578
  ent:outbound := outbound.splice(index,1);
549
579
  raise wrangler event "subscription_added" attributes event:attrs.put(["bus"], bus) // API event
580
+ raise wrangler event "relationship_added" attributes event:attrs.put(["bus"], bus) // API event
550
581
  }
551
582
  }
552
583
 
553
584
  rule addInboundSubscription {
554
585
  select when wrangler inbound_pending_subscription_approved
586
+ or wrangler inbound_pending_relationship_approved
555
587
  pre{
556
588
  inbound = inbound()
557
589
  index = indexOfId(inbound,event:attr("Id"))
@@ -563,12 +595,14 @@ ent:established [
563
595
  ent:established := established().append( event:attr("bus") );
564
596
  ent:inbound := inbound.splice(index,1);
565
597
  raise wrangler event "subscription_added" attributes event:attrs // API event
598
+ raise wrangler event "relationship_added" attributes event:attrs // API event
566
599
  }
567
600
  }
568
601
 
569
602
  // Layer 2 (1.6+): provision did:peer + internal Rx identity when bus.layer2 is set.
570
603
  rule establish_layer2_subscription_identity {
571
604
  select when wrangler subscription_added
605
+ or wrangler relationship_added
572
606
  pre {
573
607
  bus = event:attr("bus").defaultsTo({})
574
608
  layer2_identity = bus{"layer2"} == true => dido:establishSubscription(bus) | null
@@ -636,6 +670,7 @@ ent:established [
636
670
  fired {
637
671
  ent:established := buses.splice(index,1);
638
672
  raise wrangler event "subscription_removed" attributes event:attrs.put({ "bus" : bus }) // API event
673
+ raise wrangler event "relationship_removed" attributes event:attrs.put({ "bus" : bus }) // API event
639
674
  }
640
675
  }
641
676
 
@@ -689,6 +724,7 @@ ent:established [
689
724
  fired {
690
725
  ent:inbound := buses.splice(index,1);
691
726
  raise wrangler event "inbound_subscription_cancelled" attributes event:attrs.put({ "bus" : bus }) // API event
727
+ raise wrangler event "inbound_relationship_cancelled" attributes event:attrs.put({ "bus" : bus }) // API event
692
728
  }
693
729
  }
694
730
 
@@ -743,11 +779,13 @@ ent:established [
743
779
  fired {
744
780
  ent:outbound := buses.splice(index,1);
745
781
  raise wrangler event "outbound_subscription_cancelled" attributes event:attrs.put({ "bus" : bus }) // API event
782
+ raise wrangler event "outbound_relationship_cancelled" attributes event:attrs.put({ "bus" : bus }) // API event
746
783
  }
747
784
  }
748
785
 
749
786
  rule sendEventToSubCheck {
750
787
  select when wrangler send_event_on_subs
788
+ or wrangler send_event_on_relationships
751
789
  pre {
752
790
  subID = event:attr("subID")
753
791
  Tx_role = event:attr("Tx_role")
@@ -767,6 +805,11 @@ ent:established [
767
805
  "domainGiven":event:attr("domain").as("Boolean"),
768
806
  "typeGiven":event:attr("type").as("Boolean"),
769
807
  })
808
+ raise wrangler event "failed_to_send_event_to_relationship" attributes event:attrs.put({
809
+ "foundSubsToSendTo":subs.length() > 0,
810
+ "domainGiven":event:attr("domain").as("Boolean"),
811
+ "typeGiven":event:attr("type").as("Boolean"),
812
+ })
770
813
  }
771
814
  }
772
815
 
@@ -789,6 +832,7 @@ ent:established [
789
832
 
790
833
  rule autoAccept {
791
834
  select when wrangler inbound_pending_subscription_added
835
+ or wrangler inbound_pending_relationship_added
792
836
  pre{
793
837
  /*
794
838
  autoAcceptConfig{
@@ -811,7 +855,9 @@ ent:established [
811
855
  if matches then noop()
812
856
  fired {
813
857
  raise wrangler event "pending_subscription_approval" attributes event:attrs;
858
+ raise wrangler event "pending_relationship_approval" attributes event:attrs;
814
859
  raise wrangler event "auto_accepted_subscription_request" attributes event:attrs; //API event
860
+ raise wrangler event "auto_accepted_relationship_request" attributes event:attrs; //API event
815
861
  }// else ...
816
862
  }
817
863
 
@@ -18,13 +18,13 @@ ruleset io.picolabs.wrangler {
18
18
  channels, createChannel, updateChannel, deleteChannel, //channel
19
19
  discoveryChannel, filterBindingsForCaller, //discovery
20
20
  rulesetConfig, rulesetMeta, installedRIDs, //ruleset
21
- children, parent_eci, name, myself, myDid, publicIntro, establishSubscription, callerDid //pico
21
+ children, parent_eci, name, myself, myDid, publicIntro, establishSubscription, establishRelationship, callerDid //pico
22
22
 
23
23
  shares skyQuery, picoQuery,
24
24
  channels, //channel
25
25
  discoveryChannel, filterBindingsForCaller, //discovery
26
26
  rulesetConfig, rulesetMeta,installedRIDs, //ruleset
27
- children, parent_eci, name, myself, myDid, publicIntro, establishSubscription, callerDid, id //pico
27
+ children, parent_eci, name, myself, myDid, publicIntro, establishSubscription, establishRelationship, callerDid, id //pico
28
28
  }
29
29
  global {
30
30
  __testing = { "queries": [ {"name": "name"},
@@ -340,11 +340,14 @@ ruleset io.picolabs.wrangler {
340
340
  dido:setPublicIntro(enabled)
341
341
  }
342
342
 
343
- /** Layer 2: create did:peer + internal Rx for a subscription bus map. */
343
+ /** Layer 2: create did:peer + internal Rx for a relationship bus map. */
344
344
  establishSubscription = function(bus) {
345
345
  dido:establishSubscription(bus)
346
346
  }
347
347
 
348
+ /** Alias for establishSubscription (preferred name in new rulesets). */
349
+ establishRelationship = establishSubscription
350
+
348
351
  /** did:webvh of the pico that sent the current event (layer2 cross-pico delivery). */
349
352
  callerDid = function() {
350
353
  event:attr("callerDid")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pico-engine",
3
- "version": "1.6.2",
3
+ "version": "1.6.4",
4
4
  "description": "An implementation of the pico-engine targeting node.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -55,6 +55,8 @@
55
55
  "test:layer2-policy": "ava reset-cache && ava --serial ./test/layer2ChannelPolicy.ts --timeout=2m",
56
56
  "test:cross-engine": "ava reset-cache && ava --serial ./test/crossEngineSubscription.ts --timeout=3m",
57
57
  "test:relationship-alias": "ava reset-cache && ava --serial ./test/relationshipAlias.ts --timeout=2m",
58
+ "test:relationship-self": "ava reset-cache && ava --serial ./test/relationshipSelf.ts --timeout=2m",
59
+ "test:relationship-lifecycle": "ava reset-cache && ava --serial ./test/relationshipLifecycle.ts --timeout=2m",
58
60
  "test:epic9": "ava reset-cache && ava --serial ./test/epic9Matrix.ts --timeout=3m"
59
61
  },
60
62
  "dependencies": {
@@ -113,5 +115,5 @@
113
115
  "!test/helpers/**/*"
114
116
  ]
115
117
  },
116
- "gitHead": "e5cafec5fad5e22df1ddd30eb227ad41d55e67b3"
118
+ "gitHead": "b15b28de39f537f722e98d2e14d3cf3c857bb6cb"
117
119
  }