pico-engine 1.3.0 → 1.4.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.
@@ -11,18 +11,16 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  return t;
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.RulesetRegistryLoaderFs = void 0;
14
+ exports.RulesetRegistryLoaderFs = RulesetRegistryLoaderFs;
15
15
  const cross_fetch_1 = require("cross-fetch");
16
16
  const fs = require("fs");
17
- const leveldown_1 = require("leveldown");
18
- const levelup_1 = require("levelup");
17
+ const classic_level_1 = require("classic-level");
19
18
  const makeDir = require("make-dir");
20
19
  const path = require("path");
21
20
  const urlLib = require("url");
22
21
  const krlCompiler = require("krl-compiler");
23
22
  const krlCompilerVersion = require("krl-compiler/package.json").version;
24
23
  const charwise = require("charwise");
25
- const encode = require("encoding-down");
26
24
  const safeJsonCodec = require("level-json-coerce-null");
27
25
  function getUrlFilename(url) {
28
26
  try {
@@ -36,10 +34,10 @@ function getUrlFilename(url) {
36
34
  }
37
35
  function RulesetRegistryLoaderFs(engineHomeDir) {
38
36
  const rulesetDir = path.resolve(engineHomeDir, "rulesets");
39
- const db = (0, levelup_1.default)(encode((0, leveldown_1.default)(path.resolve(engineHomeDir, "rulesets-db")), {
37
+ const db = new classic_level_1.ClassicLevel(path.resolve(engineHomeDir, "rulesets-db"), {
40
38
  keyEncoding: charwise,
41
39
  valueEncoding: safeJsonCodec,
42
- }));
40
+ });
43
41
  async function compileAndLoad(url, krl, hash) {
44
42
  const jsFile = path.resolve(rulesetDir, krlCompilerVersion, hash.substr(0, 2), hash.substr(2, 2), hash + ".js");
45
43
  let compileWarnings = [];
@@ -93,7 +91,6 @@ function RulesetRegistryLoaderFs(engineHomeDir) {
93
91
  },
94
92
  };
95
93
  }
96
- exports.RulesetRegistryLoaderFs = RulesetRegistryLoaderFs;
97
94
  async function fetchKrl(url) {
98
95
  const urlParsed = urlLib.parse(url);
99
96
  if (urlParsed.protocol === "file:") {
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.startEngine = void 0;
3
+ exports.startEngine = startEngine;
4
4
  const krl_stdlib_1 = require("krl-stdlib");
5
- const leveldown_1 = require("leveldown");
5
+ const classic_level_1 = require("classic-level");
6
6
  const _ = require("lodash");
7
7
  const makeDir = require("make-dir");
8
8
  const path = require("path");
@@ -11,6 +11,8 @@ const logging_1 = require("./logging");
11
11
  const RulesetRegistryLoaderFs_1 = require("./RulesetRegistryLoaderFs");
12
12
  const server_1 = require("./server");
13
13
  const toFileUrl_1 = require("./utils/toFileUrl");
14
+ const charwise = require("charwise");
15
+ const safeJsonCodec = require("level-json-coerce-null");
14
16
  const homeDir = require("home-dir");
15
17
  const version = require("../package.json").version;
16
18
  async function startEngine(configuration = {}) {
@@ -26,7 +28,10 @@ async function startEngine(configuration = {}) {
26
28
  ? configuration.log
27
29
  : (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.makeRotatingFileLogWriter)(logFilePath));
28
30
  const core = new pico_engine_core_1.PicoEngineCore({
29
- leveldown: (0, leveldown_1.default)(path.resolve(home, "db")),
31
+ db: new classic_level_1.ClassicLevel(path.resolve(home, "db"), {
32
+ keyEncoding: charwise,
33
+ valueEncoding: safeJsonCodec,
34
+ }),
30
35
  rsRegLoader: (0, RulesetRegistryLoaderFs_1.RulesetRegistryLoaderFs)(home),
31
36
  log,
32
37
  modules: configuration.modules,
@@ -43,6 +48,7 @@ async function startEngine(configuration = {}) {
43
48
  (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.wrangler.krl")),
44
49
  (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.subscription.krl")),
45
50
  (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.did-o.krl")),
51
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.pds.krl")),
46
52
  ];
47
53
  for (const url of krl_urls) {
48
54
  const { ruleset } = await rsRegistry.flush(url);
@@ -109,4 +115,3 @@ async function startEngine(configuration = {}) {
109
115
  rsRegistry,
110
116
  };
111
117
  }
112
- exports.startEngine = startEngine;
package/dist/logging.js CHANGED
@@ -1,11 +1,18 @@
1
1
  "use strict";
2
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
3
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
4
+ var m = o[Symbol.asyncIterator], i;
5
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
6
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
7
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
8
+ };
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getPicoLogs = exports.makeRotatingFileLogWriter = void 0;
10
+ exports.makeRotatingFileLogWriter = makeRotatingFileLogWriter;
11
+ exports.getPicoLogs = getPicoLogs;
4
12
  const fs = require("fs");
13
+ const readline = require("readline");
5
14
  const krl_stdlib_1 = require("krl-stdlib");
6
15
  const path = require("path");
7
- const split = require("split");
8
- const through2 = require("through2");
9
16
  const rfs = require("rotating-file-stream");
10
17
  const logStreams = {};
11
18
  function getRotatingFileStream(filePath) {
@@ -17,7 +24,7 @@ function getRotatingFileStream(filePath) {
17
24
  return filename + "." + index;
18
25
  }, {
19
26
  path: path.dirname(filePath),
20
- size: "100M",
27
+ size: "100M", // rotate every 10 MegaBytes written
21
28
  maxFiles: 12,
22
29
  });
23
30
  }
@@ -34,17 +41,21 @@ function makeRotatingFileLogWriter(filePath) {
34
41
  }
35
42
  return write;
36
43
  }
37
- exports.makeRotatingFileLogWriter = makeRotatingFileLogWriter;
38
44
  async function getPicoLogs(filePath, picoId) {
39
- return new Promise((resolve, reject) => {
40
- const output = [];
41
- fs.createReadStream(filePath)
42
- .pipe(split())
43
- .pipe(through2((chunk, enc, next) => {
44
- const line = chunk.toString();
45
+ var _a, e_1, _b, _c;
46
+ const output = [];
47
+ const rl = readline.createInterface({
48
+ input: fs.createReadStream(filePath),
49
+ });
50
+ try {
51
+ for (var _d = true, rl_1 = __asyncValues(rl), rl_1_1; rl_1_1 = await rl_1.next(), _a = rl_1_1.done, !_a; _d = true) {
52
+ _c = rl_1_1.value;
53
+ _d = false;
54
+ const line = _c;
55
+ // Each line in the readline input will be successively available here as
56
+ // `line`.
45
57
  if (line.indexOf(picoId) < 0) {
46
- // not my pico
47
- return next();
58
+ continue; // not my pico
48
59
  }
49
60
  let entry;
50
61
  try {
@@ -52,25 +63,23 @@ async function getPicoLogs(filePath, picoId) {
52
63
  }
53
64
  catch (err) { }
54
65
  if (!entry || entry.picoId !== picoId) {
55
- // not my pico
56
- return next();
66
+ continue; // not my pico
57
67
  }
58
68
  const time = new Date(entry.time);
59
69
  if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
60
- // too old
61
- return next();
70
+ continue; // too old
62
71
  }
63
72
  const out = Object.assign(Object.assign({}, entry), { level: krl_stdlib_1.krlLogLevelCodeToHuman[entry.level] || `${entry.level}`, time: entry.time, txnId: entry.txnId });
64
73
  delete out.picoId;
65
74
  output.push(out);
66
- next();
67
- }))
68
- .on("finish", () => {
69
- resolve(output);
70
- })
71
- .on("error", (err) => {
72
- reject(err);
73
- });
74
- });
75
+ }
76
+ }
77
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
78
+ finally {
79
+ try {
80
+ if (!_d && !_a && (_b = rl_1.return)) await _b.call(rl_1);
81
+ }
82
+ finally { if (e_1) throw e_1.error; }
83
+ }
84
+ return output;
75
85
  }
76
- exports.getPicoLogs = getPicoLogs;
package/dist/server.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.server = void 0;
3
+ exports.server = server;
4
4
  const bodyParser = require("body-parser");
5
5
  const express = require("express");
6
- const helmet = require("helmet");
6
+ const helmet_1 = require("helmet");
7
7
  const _ = require("lodash");
8
8
  const path = require("path");
9
9
  const engineVersion = require("../package.json").version;
@@ -29,7 +29,13 @@ function server(core, uiECI) {
29
29
  next();
30
30
  }
31
31
  });
32
- app.use(helmet());
32
+ // Helmet 7+ CSP defaults include upgrade-insecure-requests, which breaks the
33
+ // UI on plain HTTP (browser upgrades /api/* to https and TLS fails).
34
+ app.use((0, helmet_1.default)({
35
+ contentSecurityPolicy: {
36
+ directives: Object.assign(Object.assign({}, helmet_1.default.contentSecurityPolicy.getDefaultDirectives()), { "upgrade-insecure-requests": null }),
37
+ },
38
+ }));
33
39
  app.use(express.static(path.resolve(__dirname, "..", "public")));
34
40
  app.use(bodyParser.json({
35
41
  type: [
@@ -226,4 +232,3 @@ function server(core, uiECI) {
226
232
  });
227
233
  return app;
228
234
  }
229
- exports.server = server;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toFileUrl = void 0;
3
+ exports.toFileUrl = toFileUrl;
4
4
  const path = require("path");
5
5
  function toFileUrl(str) {
6
6
  var pathName = path.resolve(str).replace(/\\/g, "/");
@@ -9,4 +9,3 @@ function toFileUrl(str) {
9
9
  }
10
10
  return encodeURI("file://" + pathName);
11
11
  }
12
- exports.toFileUrl = toFileUrl;
@@ -24,9 +24,9 @@ DID-O V 1.0.0
24
24
  >>
25
25
  author "Rembrand Paul Pardo, Kekoapoaono Montalbo, Josh Mann"
26
26
 
27
- provides addRoute, routes, send, sendEvent, sendQuery, didMap, clearDidMap, didDocs, clearDidDocs, pendingRotations, clearPendingRotations, generate_invitation
27
+ provides addRoute, routes, send, sendEvent, sendQuery, didMap, clearDidMap, didDocs, clearDidDocs, pendingRotations, clearPendingRotations, generate_invitation, generate_message
28
28
 
29
- shares routes, addRoute, didDocs, clearDidDocs, didMap, clearDidMap, pendingRotations, clearPendingRotations, generate_invitation
29
+ shares routes, addRoute, didDocs, clearDidDocs, didMap, clearDidMap, pendingRotations, clearPendingRotations, generate_invitation, generate_message
30
30
 
31
31
  use module io.picolabs.wrangler alias wrangler
32
32
  }
@@ -139,11 +139,31 @@ DID-O V 1.0.0
139
139
 
140
140
  ///////////////////////////////////////////// MESSAGE CREATORS //////////////////////////////////////////////
141
141
 
142
+ /**
143
+ * Generates and returns a DIDComm v2 message with the given message.
144
+ * message is formatted as follows:
145
+ * {
146
+ * type: string,
147
+ * body: any,
148
+ * from?: string,
149
+ * to?: [string],
150
+ * thid?: string,
151
+ * pthid?: string,
152
+ * expires_time?: number,
153
+ * attachments?: [Attachment]
154
+ * }
155
+ *
156
+ * Refer to didcomm-node package for Attachment object specifics.
157
+ */
158
+ generate_message = function(message) {
159
+ dido:generateMessage(message)
160
+ }
161
+
142
162
  /**
143
163
  * Creates an invitation OOB URL that can be used to establish DID based relationships.
144
164
  * This automates the DID, channel, and message creation and encodes it into a base64 Out-of-Band URL.
145
165
  */
146
- generate_invitation = function() {
166
+ generate_invitation = function(label) {
147
167
  DIDdoc = dido:generateDID(true)
148
168
  new_did = DIDdoc{"id"}
149
169
  invitation = dido:generateMessage({
@@ -152,7 +172,7 @@ DID-O V 1.0.0
152
172
  "body": {
153
173
  "goal_code": "exchange-did",
154
174
  "goal": "ExchangeDid",
155
- "label": "PicoInvite",
175
+ "label": label || "PicoInvite",
156
176
  "accept": [
157
177
  "didcomm/v2"
158
178
  ]
@@ -322,4 +342,4 @@ DID-O - V 1.0.0
322
342
  | ,.' `---'
323
343
  '_ ,--`
324
344
  `---'
325
- */
345
+ */
@@ -0,0 +1,344 @@
1
+ ruleset io.picolabs.pds {
2
+ meta {
3
+ name "PDS"
4
+ description <<
5
+ Pico Data Services (PDS), a common data base structure for all rulesets to use.
6
+ >>
7
+ author "PICOLABS"
8
+
9
+ use module io.picolabs.wrangler alias wrangler
10
+
11
+ logging on
12
+
13
+ provides items, get_keys,
14
+ profile,
15
+ settings, get_config_value, get_setting_data_value, config_value, settings_names,
16
+ getChannelEci
17
+
18
+ shares items, get_keys,
19
+ profile,
20
+ settings, get_config_value, get_setting_data_value, config_value, settings_names,
21
+ getChannelEci
22
+
23
+ }
24
+
25
+
26
+ // --------------------------------------------
27
+ // PDS entity variables (io.picolabs.pds RS scope only)
28
+ //
29
+ // ent:profile — this pico's display identity
30
+ // {
31
+ // "name": "",
32
+ // "description": "",
33
+ // "photo": "<url or data: URI>",
34
+ // "_created": "20260609T120000+0000", // set on first update
35
+ // "_modified": "20260609T153000+0000", // set on every update
36
+ // "email": "", // optional
37
+ // "phone": "" // optional
38
+ // }
39
+ //
40
+ // ent:general — namespaced app/domain data
41
+ // "<namespace>": {
42
+ // "<key>": <value>,
43
+ // ...
44
+ // }
45
+ // Path: ent:general{[namespace, key]} or ent:general{namespace} for whole namespace map
46
+ //
47
+ // ent:settings — per-ruleset configuration, keyed by RID
48
+ // "<rulesetRID>": {
49
+ // "name": "<display name>",
50
+ // "rid": "<rulesetRID>",
51
+ // "data": { "<configKey>": <value>, ... },
52
+ // "schema": []
53
+ // }
54
+ // Path: ent:settings{[rulesetRID, "data", configKey]} for config_value()
55
+ //
56
+ // --------------------------------------------
57
+
58
+
59
+ global {
60
+
61
+ // Default avatar: embedded SVG (no external URL dependency)
62
+ defaultUnknownPhoto = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NCA2NCI+PHJlY3Qgd2lkdGg9IjY0IiBoZWlnaHQ9IjY0IiBmaWxsPSIjZWNlZmYxIi8+PGNpcmNsZSBjeD0iMzIiIGN5PSIyMiIgcj0iMTEiIGZpbGw9IiM3ODkwOWMiLz48ZWxsaXBzZSBjeD0iMzIiIGN5PSI1NCIgcng9IjE4IiByeT0iMTQiIGZpbGw9IiM3ODkwOWMiLz48L3N2Zz4="
63
+
64
+ items = function (namespace, key){
65
+ item = function(namespace, keyvalue) {
66
+ ent:general{[namespace, keyvalue]}
67
+ };
68
+
69
+ multipleItems = function(namespace) {
70
+ ent:general{namespace}
71
+ };
72
+ payload = (key.isnull()) => (namespace.isnull() => ent:general | multipleItems(namespace)) | item(namespace, key);
73
+ status = namespace.isnull() => "failed" | "success";
74
+ {
75
+ "status" : status,
76
+ "general" : payload
77
+ };
78
+ }
79
+
80
+ get_keys = function(namespace) {
81
+ ent:general{namespace}.keys()
82
+ }
83
+
84
+ profile = function(key){
85
+ get_profile = function(k) {
86
+ ent:profile{k};
87
+ };
88
+ get_all_profile = function() {
89
+ ent:profile;
90
+ };
91
+ payload = (key.isnull()) => get_all_profile() | get_profile(key);
92
+ {
93
+ "status" : "success",
94
+ "profile" : payload
95
+ };
96
+ };
97
+
98
+ settings_names = function() {
99
+ ent:settings.keys().map(function(setRID) {
100
+ setName = ent:settings{[setRID, "name"]};
101
+ {
102
+ "setRID": setRID,
103
+ "setName": setName
104
+ }
105
+ })
106
+ };
107
+
108
+ settings = function(Rid, Key, detail){
109
+ get_setting_all = function() {
110
+ ent:settings
111
+ };
112
+ get_setting = function(rid) {
113
+ ent:settings{rid}
114
+ };
115
+ get_setting_value = function(rid, varible) {
116
+ ent:settings{[rid, varible]}
117
+ };
118
+ get_data_value = function(rid, dataKey) {
119
+ ent:settings{[rid, "data", dataKey]}
120
+ };
121
+ pick = function() {
122
+ Rid.isnull() => get_setting_all()
123
+ | Key.isnull() => get_setting(Rid)
124
+ | detail.isnull() => get_setting_value(Rid, Key)
125
+ | get_data_value(Rid, detail)
126
+ };
127
+ {
128
+ "status" : "success",
129
+ "settings" : pick()
130
+ };
131
+ }
132
+
133
+ setting_data = function(setRID) {
134
+ ent:settings{[setRID, "data"]}
135
+ };
136
+
137
+ setting_schema = function(setRID) {
138
+ ent:settings{[setRID, "schema"]}
139
+ };
140
+
141
+ setting_data_value = function(setRID, setKey) {
142
+ ent:settings{[setRID, "data", setKey]}
143
+ };
144
+
145
+ get_setting_data_value = function(setRID, setKey) {
146
+ setting_data_value(setRID, setKey)
147
+ }
148
+
149
+ config_value = function(setKey) {
150
+ setRID = meta:callingRID();
151
+ ent:settings{[setRID, "data", setKey]}
152
+ };
153
+
154
+ get_config_value = function(setKey) {
155
+ config_value(setKey)
156
+ }
157
+
158
+ getChannelEci = function() {
159
+ ent:pds_channel_eci
160
+ }
161
+
162
+ defaultProfile = {
163
+ "name": "",
164
+ "description": "",
165
+ "photo": defaultUnknownPhoto
166
+ };
167
+
168
+ }//End Global
169
+ // Rules
170
+
171
+ //------------------------------- ent: general
172
+
173
+ rule add_item {
174
+ select when pds new_data_available
175
+ pre {
176
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
177
+ keyvalue = event:attr("key").defaultsTo("", "no key");
178
+ value = event:attr("value").defaultsTo("", "no value");
179
+ }
180
+ noop()
181
+ always {
182
+ ent:general{[namespace, keyvalue]} := value;
183
+ raise pds event "data_added"
184
+ attributes {"namespace": namespace, "keyvalue": keyvalue}
185
+ }
186
+ }
187
+
188
+ rule update_item {
189
+ select when pds updated_data_available
190
+ foreach(event:attr("value") || {}) setting(avalue, akey)
191
+ pre {
192
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
193
+ keyvalue = event:attr("key").defaultsTo("", "no key");
194
+ }
195
+ noop()
196
+ always {
197
+ ent:general{[namespace, keyvalue, akey]} := avalue;
198
+ raise pds event "data_updated"
199
+ attributes {"namespace": namespace, "keyvalue": keyvalue};
200
+ }
201
+ }
202
+
203
+ rule remove_item {
204
+ select when pds remove_old_data
205
+ pre{
206
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
207
+ keyvalue = event:attr("key").defaultsTo("", "no key");
208
+ }
209
+ noop()
210
+ always {
211
+ clear ent:general{[namespace, keyvalue]};
212
+ raise pds event "data_deleted"
213
+ attributes {"namespace": namespace, "keyvalue": keyvalue}
214
+ }
215
+ }
216
+
217
+ rule remove_namespace {
218
+ select when pds remove_namespace
219
+ pre{
220
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
221
+ }
222
+ noop()
223
+ always {
224
+ clear ent:general{namespace};
225
+ raise pds event "namespace_deleted"
226
+ attributes {"namespace": namespace}
227
+ }
228
+ }
229
+
230
+ rule itemed_mapped {
231
+ select when pds map_item
232
+ pre{
233
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
234
+ mapvalues = event:attr("mapvalues").defaultsTo("", "no mapvalues").decode();
235
+ }
236
+ noop()
237
+ always {
238
+ ent:general{namespace} := mapvalues;
239
+ raise pds event "new_map_added"
240
+ attributes {"namespace": namespace, "mapvalues": mapvalues};
241
+ }
242
+ }
243
+
244
+ rule update_profile {
245
+ select when pds updated_profile
246
+ pre {
247
+ newProfile = event:attrs.defaultsTo({}, "no attrs");
248
+ created = function(){
249
+ time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"});
250
+ };
251
+
252
+ buildProfile = function(newProfile){
253
+ profile = ent:profile || defaultProfile;
254
+ keepOrSet = function(key) {
255
+ newProfile{key}.isnull() => profile{key} | newProfile{key}
256
+ };
257
+ newProfile
258
+ .put(["name"], keepOrSet("name"))
259
+ .put(["description"], keepOrSet("description"))
260
+ .put(["photo"], keepOrSet("photo"))
261
+ .put(["email"], keepOrSet("email"))
262
+ .put(["phone"], keepOrSet("phone"))
263
+ .put(["_created"], (profile{"_created"} || created()))
264
+ .put(["_modified"], time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"}))
265
+ };
266
+ newly_constructed_profile = (newProfile.keys().length() > 0) =>
267
+ buildProfile(newProfile) |
268
+ "nothing to update";
269
+
270
+ }
271
+ if newly_constructed_profile != "nothing to update" then
272
+ noop()
273
+ fired {
274
+ ent:profile := newly_constructed_profile;
275
+ raise pds event "profile_updated" attributes newProfile.put(["_status"], "success");
276
+ }
277
+ else {
278
+ raise pds event "profile_updated" attributes newProfile.put(["_status"], "failure");
279
+ }
280
+ }
281
+
282
+ rule settings_added {
283
+ select when pds add_settings
284
+ pre {
285
+ set_name = event:attr("name").defaultsTo(0, "no Name");
286
+ set_rid = event:attr("keyed_rid").defaultsTo(0, "no keyed_rid");
287
+ set_schema = event:attr("schema").defaultsTo(0, "no Schema");
288
+ set_attr = event:attr("data_key").defaultsTo(0, "no setAttr");
289
+ set_value = event:attr("value").defaultsTo(0, "no Value");
290
+
291
+ }
292
+ noop()
293
+ always {
294
+ ent:settings{[set_rid, "name"]} := set_name;
295
+ ent:settings{[set_rid, "rid"]} := set_rid;
296
+ ent:settings{[set_rid, "schema"]} := set_schema if set_schema;
297
+ ent:settings{[set_rid, "data", set_attr]} := set_value if set_attr;
298
+ raise pds event "settings_added" attributes event:attrs;
299
+ }
300
+ }
301
+
302
+ rule create_pds_channel {
303
+ select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
304
+ pre {
305
+ channelName = "pds"
306
+ eventPolicy = {"allow": [{"domain": "pds", "name": "*"}], "deny": []}
307
+ queryPolicy = {"allow": [{"rid": "io.picolabs.pds", "name": "*"}], "deny": []}
308
+ existing_channels = wrangler:channels()
309
+ pds_channel = existing_channels.filter(function(chan) {
310
+ chan{"name"} == channelName
311
+ })
312
+ channel_exists = pds_channel.length() > 0
313
+ }
314
+ if not channel_exists then
315
+ wrangler:createChannel([channelName], eventPolicy, queryPolicy) setting(channel)
316
+ fired {
317
+ ent:pds_channel_eci := channel_exists => pds_channel.head(){"id"} | channel{"id"}
318
+ }
319
+ }
320
+
321
+ rule seed_profile_name {
322
+ select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
323
+ pre {
324
+ wrangler_name = wrangler:myself(){"name"}
325
+ existing_name = ent:profile{"name"}
326
+ needs_seed = wrangler_name && (existing_name.isnull() || existing_name == "")
327
+ }
328
+ if needs_seed then noop()
329
+ fired {
330
+ raise pds event "updated_profile" attributes {"name": wrangler_name}
331
+ }
332
+ }
333
+
334
+ rule clearPDS {
335
+ select when pds clear_all_data
336
+ noop()
337
+ always {
338
+ clear ent:general;
339
+ clear ent:profile;
340
+ clear ent:settings;
341
+ }
342
+ }
343
+
344
+ }