pico-engine 1.1.1 → 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.
@@ -108,7 +108,7 @@ export function RulesetRegistryLoaderFs(
108
108
  let fromDb: DbUrlRuleset;
109
109
  try {
110
110
  fromDb = (await db.get(["url", url])) as DbUrlRuleset;
111
- } catch (err) {
111
+ } catch (err: any) {
112
112
  if (err.notFound) {
113
113
  return null;
114
114
  }
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
+ }