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.
@@ -1,16 +1,14 @@
1
1
  import fetch from "cross-fetch";
2
2
  import * as fs from "fs";
3
- import leveldown from "leveldown";
4
- import { default as level } from "levelup";
3
+ import { ClassicLevel } from "classic-level";
5
4
  import * as makeDir from "make-dir";
6
5
  import * as path from "path";
7
6
  import { RulesetRegistryLoader } from "pico-engine-core";
8
- import { Ruleset } from "pico-framework";
7
+ import { PicoDbKey, Ruleset } from "pico-framework";
9
8
  import * as urlLib from "url";
10
9
  const krlCompiler = require("krl-compiler");
11
10
  const krlCompilerVersion = require("krl-compiler/package.json").version;
12
11
  const charwise = require("charwise");
13
- const encode = require("encoding-down");
14
12
  const safeJsonCodec = require("level-json-coerce-null");
15
13
 
16
14
  function getUrlFilename(url: string): string | null {
@@ -40,12 +38,10 @@ export function RulesetRegistryLoaderFs(
40
38
  ): RulesetRegistryLoader {
41
39
  const rulesetDir = path.resolve(engineHomeDir, "rulesets");
42
40
 
43
- const db = level(
44
- encode(leveldown(path.resolve(engineHomeDir, "rulesets-db")), {
45
- keyEncoding: charwise,
46
- valueEncoding: safeJsonCodec,
47
- })
48
- );
41
+ const db = new ClassicLevel<PicoDbKey, any>(path.resolve(engineHomeDir, "rulesets-db"), {
42
+ keyEncoding: charwise,
43
+ valueEncoding: safeJsonCodec,
44
+ })
49
45
 
50
46
  async function compileAndLoad(
51
47
  url: string,
package/src/index.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  import { krl, KrlLogger, makeKrlLogger } from "krl-stdlib";
2
- import leveldown from "leveldown";
2
+ import { ClassicLevel } from "classic-level";
3
3
  import * as _ from "lodash";
4
4
  import * as makeDir from "make-dir";
5
5
  import * as path from "path";
6
6
  import { PicoEngineCore, RulesetRegistry } from "pico-engine-core";
7
- import { PicoFramework } from "pico-framework";
7
+ import { PicoDbKey, PicoFramework } from "pico-framework";
8
8
  import { getPicoLogs, makeRotatingFileLogWriter } from "./logging";
9
9
  import { RulesetRegistryLoaderFs } from "./RulesetRegistryLoaderFs";
10
10
  import { server } from "./server";
11
11
  import { toFileUrl } from "./utils/toFileUrl";
12
- import * as fs from "fs";
13
- import { reject } from "lodash";
12
+ const charwise = require("charwise");
13
+ const safeJsonCodec = require("level-json-coerce-null");
14
14
 
15
15
  const homeDir = require("home-dir");
16
16
  const version = require("../package.json").version;
@@ -85,7 +85,10 @@ export async function startEngine(
85
85
  : makeKrlLogger(makeRotatingFileLogWriter(logFilePath));
86
86
 
87
87
  const core = new PicoEngineCore({
88
- leveldown: leveldown(path.resolve(home, "db")) as any,
88
+ db: new ClassicLevel<PicoDbKey, any>(path.resolve(home, "db"), {
89
+ keyEncoding: charwise,
90
+ valueEncoding: safeJsonCodec,
91
+ }),
89
92
  rsRegLoader: RulesetRegistryLoaderFs(home),
90
93
  log,
91
94
  modules: configuration.modules,
@@ -107,6 +110,7 @@ export async function startEngine(
107
110
  path.resolve(__dirname, "..", "krl", "io.picolabs.subscription.krl")
108
111
  ),
109
112
  toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.did-o.krl")),
113
+ toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.pds.krl")),
110
114
  ];
111
115
  for (const url of krl_urls) {
112
116
  const { ruleset } = await rsRegistry.flush(url);
package/src/logging.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  import * as fs from "fs";
2
+ import * as readline from "readline";
2
3
  import { krlLogLevelCodeToHuman, PicoLogEntry } from "krl-stdlib";
3
4
  import * as path from "path";
4
- const split = require("split");
5
- const through2 = require("through2");
6
5
 
7
6
  const rfs = require("rotating-file-stream");
8
7
 
@@ -21,14 +20,14 @@ function getRotatingFileStream(filePath: string): NodeJS.WritableStream {
21
20
 
22
21
  size: "100M", // rotate every 10 MegaBytes written
23
22
  maxFiles: 12,
24
- }
23
+ },
25
24
  ) as NodeJS.WritableStream;
26
25
  }
27
26
  return logStreams[filePath];
28
27
  }
29
28
 
30
29
  export function makeRotatingFileLogWriter(
31
- filePath: string
30
+ filePath: string,
32
31
  ): (line: string) => void {
33
32
  const fileStream = getRotatingFileStream(filePath);
34
33
  const isTest = process.env.NODE_ENV === "test";
@@ -45,50 +44,40 @@ export function makeRotatingFileLogWriter(
45
44
 
46
45
  export async function getPicoLogs(
47
46
  filePath: string,
48
- picoId: string
47
+ picoId: string,
49
48
  ): Promise<PicoLogEntry[]> {
50
- return new Promise((resolve, reject) => {
51
- const output: PicoLogEntry[] = [];
52
- fs.createReadStream(filePath)
53
- .pipe(split())
54
- .pipe(
55
- through2((chunk: any, enc: any, next: any) => {
56
- const line = chunk.toString();
57
- if (line.indexOf(picoId) < 0) {
58
- // not my pico
59
- return next();
60
- }
61
- let entry;
62
- try {
63
- entry = JSON.parse(line);
64
- } catch (err) {}
65
- if (!entry || entry.picoId !== picoId) {
66
- // not my pico
67
- return next();
68
- }
69
- const time = new Date(entry.time);
70
- if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
71
- // too old
72
- return next();
73
- }
74
-
75
- const out: PicoLogEntry = {
76
- ...entry,
77
- level: krlLogLevelCodeToHuman[entry.level] || `${entry.level}`,
78
- time: entry.time,
79
- txnId: entry.txnId,
80
- };
81
- delete (out as any).picoId;
82
- output.push(out);
49
+ const output: PicoLogEntry[] = [];
83
50
 
84
- next();
85
- })
86
- )
87
- .on("finish", () => {
88
- resolve(output);
89
- })
90
- .on("error", (err: any) => {
91
- reject(err);
92
- });
51
+ const rl = readline.createInterface({
52
+ input: fs.createReadStream(filePath),
93
53
  });
54
+
55
+ for await (const line of rl) {
56
+ // Each line in the readline input will be successively available here as
57
+ // `line`.
58
+ if (line.indexOf(picoId) < 0) {
59
+ continue; // not my pico
60
+ }
61
+ let entry;
62
+ try {
63
+ entry = JSON.parse(line);
64
+ } catch (err) {}
65
+ if (!entry || entry.picoId !== picoId) {
66
+ continue; // not my pico
67
+ }
68
+ const time = new Date(entry.time);
69
+ if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
70
+ continue; // too old
71
+ }
72
+
73
+ const out: PicoLogEntry = {
74
+ ...entry,
75
+ level: krlLogLevelCodeToHuman[entry.level] || `${entry.level}`,
76
+ time: entry.time,
77
+ txnId: entry.txnId,
78
+ };
79
+ delete (out as any).picoId;
80
+ output.push(out);
81
+ }
82
+ return output;
94
83
  }
package/src/server.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as bodyParser from "body-parser";
2
2
  import * as express from "express";
3
3
  import { Express, Request } from "express";
4
- import * as helmet from "helmet";
4
+ import helmet from "helmet";
5
5
  import * as _ from "lodash";
6
6
  import * as path from "path";
7
7
  import { PicoEngineCore } from "pico-engine-core";
@@ -31,7 +31,18 @@ export function server(core: PicoEngineCore, uiECI: string): Express {
31
31
  next();
32
32
  }
33
33
  });
34
- app.use(helmet());
34
+ // Helmet 7+ CSP defaults include upgrade-insecure-requests, which breaks the
35
+ // UI on plain HTTP (browser upgrades /api/* to https and TLS fails).
36
+ app.use(
37
+ helmet({
38
+ contentSecurityPolicy: {
39
+ directives: {
40
+ ...helmet.contentSecurityPolicy.getDefaultDirectives(),
41
+ "upgrade-insecure-requests": null,
42
+ },
43
+ },
44
+ })
45
+ );
35
46
  app.use(express.static(path.resolve(__dirname, "..", "public")));
36
47
  app.use(
37
48
  bodyParser.json({