pico-engine 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,78 @@
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pico-engine",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "An implementation of the pico-engine targeting node.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -50,8 +50,8 @@
50
50
  "express": "^4.16.4",
51
51
  "helmet": "^3.16.0",
52
52
  "home-dir": "^1.0.0",
53
- "krl-compiler": "^1.2.0",
54
- "krl-stdlib": "^1.2.0",
53
+ "krl-compiler": "^1.3.0",
54
+ "krl-stdlib": "^1.3.0",
55
55
  "level-json-coerce-null": "^1.0.1",
56
56
  "leveldown": "^5.0.2",
57
57
  "levelup": "^4.0.1",
@@ -59,7 +59,7 @@
59
59
  "make-dir": "^3.0.0",
60
60
  "memdown": "^5.1.0",
61
61
  "minimist": "^1.2.5",
62
- "pico-engine-core": "^1.2.0",
62
+ "pico-engine-core": "^1.3.0",
63
63
  "pico-framework": "^0.6.0",
64
64
  "rotating-file-stream": "^1.4.1",
65
65
  "split": "^1.0.1",
@@ -89,5 +89,5 @@
89
89
  "ts-node/register"
90
90
  ]
91
91
  },
92
- "gitHead": "7052049b68ebf82ac0042dfcfbd8c71470e95652"
92
+ "gitHead": "7eee32777be87b2a65398ae7054e16dca4f2ccdc"
93
93
  }
package/src/cli.ts CHANGED
@@ -23,7 +23,7 @@ USAGE
23
23
 
24
24
  Environment Variables:
25
25
 
26
- PORT - The port the http server should listen on. By default it's 8080
26
+ PORT - The port the http server should listen on. By default it's 3000
27
27
  PICO_ENGINE_HOME - Where the database and other files should be stored. By default it's ~/.pico-engine/
28
28
  PICO_ENGINE_BASE_URL - The public url prefix to reach this engine. By default it's http://localhost:$PORT
29
29
  `);
package/src/index.ts CHANGED
@@ -1,187 +1,188 @@
1
- import { krl, KrlLogger, makeKrlLogger } from "krl-stdlib";
2
- import leveldown from "leveldown";
3
- import * as _ from "lodash";
4
- import * as makeDir from "make-dir";
5
- import * as path from "path";
6
- import { PicoEngineCore, RulesetRegistry } from "pico-engine-core";
7
- import { PicoFramework } from "pico-framework";
8
- import { getPicoLogs, makeRotatingFileLogWriter } from "./logging";
9
- import { RulesetRegistryLoaderFs } from "./RulesetRegistryLoaderFs";
10
- import { server } from "./server";
11
- import { toFileUrl } from "./utils/toFileUrl";
12
- import * as fs from "fs";
13
- import { reject } from "lodash";
14
-
15
- const homeDir = require("home-dir");
16
- const version = require("../package.json").version;
17
-
18
- /**
19
- * Configuration options that may be set by the user
20
- */
21
- export interface PicoEngineConfiguration {
22
- /**
23
- * The absolute path to the folder where the engine should store the database and logs.
24
- *
25
- * Default: "~/.pico-engine/"
26
- */
27
- home?: string;
28
-
29
- /**
30
- * The port number the http server should listen on.
31
- *
32
- * If you want an available port assigned to you, set it to 0
33
- *
34
- * Default: 3000
35
- */
36
- port?: number;
37
-
38
- /**
39
- * The base url others should use when addressing your engine.
40
- *
41
- * Default: "http://localhost:3000"
42
- */
43
- base_url?: string;
44
-
45
- /**
46
- * Provide any custom krl modules
47
- */
48
- modules?: { [domain: string]: krl.Module };
49
-
50
- /**
51
- * Trust event.time input. Used for testing
52
- */
53
- useEventInputTime?: boolean;
54
-
55
- log?: KrlLogger;
56
- }
57
-
58
- export interface PicoEngine {
59
- version: string;
60
-
61
- home: string;
62
- port: number;
63
- base_url: string;
64
-
65
- pf: PicoFramework;
66
- uiECI: string;
67
- rsRegistry: RulesetRegistry;
68
- }
69
-
70
- export async function startEngine(
71
- configuration: PicoEngineConfiguration = {}
72
- ): Promise<PicoEngine> {
73
- let home = configuration.home;
74
- let port = configuration.port;
75
- let base_url = configuration.base_url;
76
-
77
- if (typeof home !== "string") {
78
- home = homeDir(".pico-engine") as string;
79
- }
80
- await makeDir(home);
81
-
82
- const logFilePath = path.resolve(home, "pico-engine.log");
83
- const log = configuration.log
84
- ? configuration.log
85
- : makeKrlLogger(makeRotatingFileLogWriter(logFilePath));
86
-
87
- const core = new PicoEngineCore({
88
- leveldown: leveldown(path.resolve(home, "db")) as any,
89
- rsRegLoader: RulesetRegistryLoaderFs(home),
90
- log,
91
- modules: configuration.modules,
92
- useEventInputTime: configuration.useEventInputTime,
93
- getPicoLogs(picoId) {
94
- return getPicoLogs(logFilePath, picoId);
95
- },
96
- });
97
- await core.start();
98
- const rsRegistry = core.rsRegistry;
99
- const pf = core.picoFramework;
100
-
101
- const krl_urls: string[] = [
102
- toFileUrl(
103
- path.resolve(__dirname, "..", "krl", "io.picolabs.pico-engine-ui.krl")
104
- ),
105
- toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.wrangler.krl")),
106
- toFileUrl(
107
- path.resolve(__dirname, "..", "krl", "io.picolabs.subscription.krl")
108
- ),
109
- ];
110
- for (const url of krl_urls) {
111
- const { ruleset } = await rsRegistry.flush(url);
112
- await pf.rootPico.install(ruleset, { url, config: {} });
113
- }
114
-
115
- let uiChannel = pf.rootPico
116
- .toReadOnly()
117
- .channels.find(
118
- (chann) => "engine,ui" === chann.tags.slice(0).sort().join(",")
119
- );
120
- if (!uiChannel) {
121
- uiChannel = (
122
- await pf.rootPico.newChannel({
123
- tags: ["engine", "ui"],
124
- eventPolicy: {
125
- allow: [{ domain: "engine_ui", name: "setup" }],
126
- deny: [],
127
- },
128
- })
129
- ).toReadOnly();
130
- }
131
- await pf.eventWait({
132
- eci: uiChannel.id,
133
- domain: "engine_ui",
134
- name: "setup",
135
- data: { attrs: {} },
136
- time: 0,
137
- });
138
-
139
- const uiECI = uiChannel.id;
140
-
141
- const app = server(core, uiECI);
142
-
143
- if ((!port || !_.isInteger(port) || port < 1) && port !== 0) {
144
- port = 3000;
145
- }
146
- await new Promise((resolve) => {
147
- const listener = app.listen(port, () => {
148
- if (listener) {
149
- const addr = listener.address();
150
- if (addr && typeof addr !== "string" && _.isInteger(addr.port)) {
151
- // Get the actual port i.e. if they set port to 0 nodejs will assign you an available port
152
- port = addr.port;
153
- }
154
- }
155
- resolve(undefined);
156
- });
157
- });
158
- if (typeof base_url !== "string") {
159
- base_url = `http://localhost:${port}`;
160
- }
161
- core.base_url = base_url;
162
-
163
- log.info(`Listening at ${base_url}`);
164
-
165
- pf.event({
166
- eci: uiECI,
167
- domain: "engine",
168
- name: "started",
169
- data: { attrs: {} },
170
- time: 0, // TODO remove this typescript requirement
171
- }).catch((error) => {
172
- log.error("Error signaling engine:started event", { error });
173
- // TODO signal all errors engine:error
174
- });
175
-
176
- return {
177
- version,
178
-
179
- home,
180
- port,
181
- base_url,
182
-
183
- pf,
184
- uiECI,
185
- rsRegistry,
186
- };
187
- }
1
+ import { krl, KrlLogger, makeKrlLogger } from "krl-stdlib";
2
+ import leveldown from "leveldown";
3
+ import * as _ from "lodash";
4
+ import * as makeDir from "make-dir";
5
+ import * as path from "path";
6
+ import { PicoEngineCore, RulesetRegistry } from "pico-engine-core";
7
+ import { PicoFramework } from "pico-framework";
8
+ import { getPicoLogs, makeRotatingFileLogWriter } from "./logging";
9
+ import { RulesetRegistryLoaderFs } from "./RulesetRegistryLoaderFs";
10
+ import { server } from "./server";
11
+ import { toFileUrl } from "./utils/toFileUrl";
12
+ import * as fs from "fs";
13
+ import { reject } from "lodash";
14
+
15
+ const homeDir = require("home-dir");
16
+ const version = require("../package.json").version;
17
+
18
+ /**
19
+ * Configuration options that may be set by the user
20
+ */
21
+ export interface PicoEngineConfiguration {
22
+ /**
23
+ * The absolute path to the folder where the engine should store the database and logs.
24
+ *
25
+ * Default: "~/.pico-engine/"
26
+ */
27
+ home?: string;
28
+
29
+ /**
30
+ * The port number the http server should listen on.
31
+ *
32
+ * If you want an available port assigned to you, set it to 0
33
+ *
34
+ * Default: 3000
35
+ */
36
+ port?: number;
37
+
38
+ /**
39
+ * The base url others should use when addressing your engine.
40
+ *
41
+ * Default: "http://localhost:3000"
42
+ */
43
+ base_url?: string;
44
+
45
+ /**
46
+ * Provide any custom krl modules
47
+ */
48
+ modules?: { [domain: string]: krl.Module };
49
+
50
+ /**
51
+ * Trust event.time input. Used for testing
52
+ */
53
+ useEventInputTime?: boolean;
54
+
55
+ log?: KrlLogger;
56
+ }
57
+
58
+ export interface PicoEngine {
59
+ version: string;
60
+
61
+ home: string;
62
+ port: number;
63
+ base_url: string;
64
+
65
+ pf: PicoFramework;
66
+ uiECI: string;
67
+ rsRegistry: RulesetRegistry;
68
+ }
69
+
70
+ export async function startEngine(
71
+ configuration: PicoEngineConfiguration = {}
72
+ ): Promise<PicoEngine> {
73
+ let home = configuration.home;
74
+ let port = configuration.port;
75
+ let base_url = configuration.base_url;
76
+
77
+ if (typeof home !== "string") {
78
+ home = homeDir(".pico-engine") as string;
79
+ }
80
+ await makeDir(home);
81
+
82
+ const logFilePath = path.resolve(home, "pico-engine.log");
83
+ const log = configuration.log
84
+ ? configuration.log
85
+ : makeKrlLogger(makeRotatingFileLogWriter(logFilePath));
86
+
87
+ const core = new PicoEngineCore({
88
+ leveldown: leveldown(path.resolve(home, "db")) as any,
89
+ rsRegLoader: RulesetRegistryLoaderFs(home),
90
+ log,
91
+ modules: configuration.modules,
92
+ useEventInputTime: configuration.useEventInputTime,
93
+ getPicoLogs(picoId) {
94
+ return getPicoLogs(logFilePath, picoId);
95
+ },
96
+ });
97
+ await core.start();
98
+ const rsRegistry = core.rsRegistry;
99
+ const pf = core.picoFramework;
100
+
101
+ const krl_urls: string[] = [
102
+ toFileUrl(
103
+ path.resolve(__dirname, "..", "krl", "io.picolabs.pico-engine-ui.krl")
104
+ ),
105
+ toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.wrangler.krl")),
106
+ toFileUrl(
107
+ path.resolve(__dirname, "..", "krl", "io.picolabs.subscription.krl")
108
+ ),
109
+ toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.did-o.krl")),
110
+ ];
111
+ for (const url of krl_urls) {
112
+ const { ruleset } = await rsRegistry.flush(url);
113
+ await pf.rootPico.install(ruleset, { url, config: {} });
114
+ }
115
+
116
+ let uiChannel = pf.rootPico
117
+ .toReadOnly()
118
+ .channels.find(
119
+ (chann) => "engine,ui" === chann.tags.slice(0).sort().join(",")
120
+ );
121
+ if (!uiChannel) {
122
+ uiChannel = (
123
+ await pf.rootPico.newChannel({
124
+ tags: ["engine", "ui"],
125
+ eventPolicy: {
126
+ allow: [{ domain: "engine_ui", name: "setup" }],
127
+ deny: [],
128
+ },
129
+ })
130
+ ).toReadOnly();
131
+ }
132
+ await pf.eventWait({
133
+ eci: uiChannel.id,
134
+ domain: "engine_ui",
135
+ name: "setup",
136
+ data: { attrs: {} },
137
+ time: 0,
138
+ });
139
+
140
+ const uiECI = uiChannel.id;
141
+
142
+ const app = server(core, uiECI);
143
+
144
+ if ((!port || !_.isInteger(port) || port < 1) && port !== 0) {
145
+ port = 3000;
146
+ }
147
+ await new Promise((resolve) => {
148
+ const listener = app.listen(port, () => {
149
+ if (listener) {
150
+ const addr = listener.address();
151
+ if (addr && typeof addr !== "string" && _.isInteger(addr.port)) {
152
+ // Get the actual port i.e. if they set port to 0 nodejs will assign you an available port
153
+ port = addr.port;
154
+ }
155
+ }
156
+ resolve(undefined);
157
+ });
158
+ });
159
+ if (typeof base_url !== "string") {
160
+ base_url = `http://localhost:${port}`;
161
+ }
162
+ core.base_url = base_url;
163
+
164
+ log.info(`Listening at ${base_url}`);
165
+
166
+ pf.event({
167
+ eci: uiECI,
168
+ domain: "engine",
169
+ name: "started",
170
+ data: { attrs: {} },
171
+ time: 0, // TODO remove this typescript requirement
172
+ }).catch((error) => {
173
+ log.error("Error signaling engine:started event", { error });
174
+ // TODO signal all errors engine:error
175
+ });
176
+
177
+ return {
178
+ version,
179
+
180
+ home,
181
+ port,
182
+ base_url,
183
+
184
+ pf,
185
+ uiECI,
186
+ rsRegistry,
187
+ };
188
+ }