Skip to content

Commit

Permalink
pico-engine updates
Browse files Browse the repository at this point in the history
  • Loading branch information
farskipper committed Nov 25, 2023
1 parent 35c157a commit eeea079
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 83 deletions.
14 changes: 5 additions & 9 deletions packages/pico-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,29 @@
"body-parser": "^1.18.3",
"charwise": "^3.0.1",
"classic-level": "^1.3.0",
"cross-fetch": "^3.0.5",
"cross-fetch": "^4.0.0",
"cuid": "^2.1.8",
"express": "^4.16.4",
"helmet": "^3.16.0",
"helmet": "^7.1.0",
"home-dir": "^1.0.0",
"krl-compiler": "^1.3.0",
"krl-stdlib": "^1.3.0",
"level-json-coerce-null": "^1.0.1",
"lodash": "^4.17.11",
"make-dir": "^3.0.0",
"make-dir": "^4.0.0",
"minimist": "^1.2.5",
"pico-engine-core": "^1.3.0",
"pico-framework": "^0.7.0",
"rotating-file-stream": "^1.4.1",
"split": "^1.0.1",
"through2": "^3.0.1"
"rotating-file-stream": "^1.4.1"
},
"devDependencies": {
"@types/body-parser": "^1.17.0",
"@types/express": "^4.16.1",
"@types/helmet": "0.0.43",
"@types/lodash": "^4.14.123",
"@types/node": "^20.8.10",
"ava": "^5.3.1",
"onchange": "^6.0.0",
"onchange": "^7.1.0",
"scriptsp": "^1.1.1",
"temp-dir": "^2.0.0",
"ts-node": "^10.4.0",
"typescript": "^5.2.2"
},
Expand Down
12 changes: 6 additions & 6 deletions packages/pico-engine/public/pico-engine-ui.css

Large diffs are not rendered by default.

65 changes: 49 additions & 16 deletions packages/pico-engine/public/pico-engine-ui.js

Large diffs are not rendered by default.

83 changes: 36 additions & 47 deletions packages/pico-engine/src/logging.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as fs from "fs";
import * as readline from "readline";
import { krlLogLevelCodeToHuman, PicoLogEntry } from "krl-stdlib";
import * as path from "path";
const split = require("split");
const through2 = require("through2");

const rfs = require("rotating-file-stream");

Expand All @@ -21,14 +20,14 @@ function getRotatingFileStream(filePath: string): NodeJS.WritableStream {

size: "100M", // rotate every 10 MegaBytes written
maxFiles: 12,
}
},
) as NodeJS.WritableStream;
}
return logStreams[filePath];
}

export function makeRotatingFileLogWriter(
filePath: string
filePath: string,
): (line: string) => void {
const fileStream = getRotatingFileStream(filePath);
const isTest = process.env.NODE_ENV === "test";
Expand All @@ -45,50 +44,40 @@ export function makeRotatingFileLogWriter(

export async function getPicoLogs(
filePath: string,
picoId: string
picoId: string,
): Promise<PicoLogEntry[]> {
return new Promise((resolve, reject) => {
const output: PicoLogEntry[] = [];
fs.createReadStream(filePath)
.pipe(split())
.pipe(
through2((chunk: any, enc: any, next: any) => {
const line = chunk.toString();
if (line.indexOf(picoId) < 0) {
// not my pico
return next();
}
let entry;
try {
entry = JSON.parse(line);
} catch (err) {}
if (!entry || entry.picoId !== picoId) {
// not my pico
return next();
}
const time = new Date(entry.time);
if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
// too old
return next();
}

const out: PicoLogEntry = {
...entry,
level: krlLogLevelCodeToHuman[entry.level] || `${entry.level}`,
time: entry.time,
txnId: entry.txnId,
};
delete (out as any).picoId;
output.push(out);
const output: PicoLogEntry[] = [];

next();
})
)
.on("finish", () => {
resolve(output);
})
.on("error", (err: any) => {
reject(err);
});
const rl = readline.createInterface({
input: fs.createReadStream(filePath),
});

for await (const line of rl) {
// Each line in the readline input will be successively available here as
// `line`.
if (line.indexOf(picoId) < 0) {
continue; // not my pico
}
let entry;
try {
entry = JSON.parse(line);
} catch (err) {}
if (!entry || entry.picoId !== picoId) {
continue; // not my pico
}
const time = new Date(entry.time);
if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
continue; // too old
}

const out: PicoLogEntry = {
...entry,
level: krlLogLevelCodeToHuman[entry.level] || `${entry.level}`,
time: entry.time,
txnId: entry.txnId,
};
delete (out as any).picoId;
output.push(out);
}
return output;
}
2 changes: 1 addition & 1 deletion packages/pico-engine/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as bodyParser from "body-parser";
import * as express from "express";
import { Express, Request } from "express";
import * as helmet from "helmet";
import helmet from "helmet";
import * as _ from "lodash";
import * as path from "path";
import { PicoEngineCore } from "pico-engine-core";
Expand Down
4 changes: 2 additions & 2 deletions packages/pico-engine/test/RulesetRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import * as cuid from "cuid";
import * as fs from "fs";
import * as makeDir from "make-dir";
import * as path from "path";
import * as tempDir from "temp-dir";
import * as os from "os";
import { RulesetRegistry } from "pico-engine-core";
import { RulesetRegistryLoaderFs } from "../src/RulesetRegistryLoaderFs";
import { toFileUrl } from "../src/utils/toFileUrl";

test("RulesetRegistry", async (t) => {
const dir = path.resolve(tempDir, "pico-engine", cuid());
const dir = path.resolve(os.tmpdir(), "pico-engine", cuid());
await makeDir(dir);

await fs.promises.writeFile(
Expand Down
4 changes: 2 additions & 2 deletions packages/pico-engine/test/helpers/startTestEngine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cuid from "cuid";
import * as path from "path";
import { ChannelConfig, Pico, PicoFramework } from "pico-framework";
import * as tempDir from "temp-dir";
import * as os from "os";
import { PicoEngineConfiguration, startEngine } from "../../src/index";
import { cleanDirectives } from "./cleanDirectives";
import { toTestKrlURL } from "./toTestKrlURL";
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function startTestEngine(
) {
const pe = await startEngine({
...conf,
home: conf.home || path.resolve(tempDir, "pico-engine", cuid()),
home: conf.home || path.resolve(os.tmpdir(), "pico-engine", cuid()),
port: 0,
});

Expand Down

0 comments on commit eeea079

Please sign in to comment.