From 854c775c8e0bf86fd4bf29c38123eccef9dbd267 Mon Sep 17 00:00:00 2001 From: Matthew Wright Date: Sat, 18 Nov 2023 22:08:10 -0700 Subject: [PATCH] pico-engine now using latest pico-framework and leveldb --- packages/pico-engine/package.json | 9 ++------ .../src/RulesetRegistryLoaderFs.ts | 16 ++++++-------- packages/pico-engine/src/index.ts | 13 +++++++----- .../test/test-rulesets/persistent.krl.ts | 21 ++++++++----------- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/packages/pico-engine/package.json b/packages/pico-engine/package.json index afe603366..76ddf32f4 100644 --- a/packages/pico-engine/package.json +++ b/packages/pico-engine/package.json @@ -44,23 +44,20 @@ "dependencies": { "body-parser": "^1.18.3", "charwise": "^3.0.1", + "classic-level": "^1.3.0", "cross-fetch": "^3.0.5", "cuid": "^2.1.8", - "encoding-down": "^6.0.2", "express": "^4.16.4", "helmet": "^3.16.0", "home-dir": "^1.0.0", "krl-compiler": "^1.3.0", "krl-stdlib": "^1.3.0", "level-json-coerce-null": "^1.0.1", - "leveldown": "^5.0.2", - "levelup": "^4.0.1", "lodash": "^4.17.11", "make-dir": "^3.0.0", - "memdown": "^5.1.0", "minimist": "^1.2.5", "pico-engine-core": "^1.3.0", - "pico-framework": "^0.6.0", + "pico-framework": "^0.7.0", "rotating-file-stream": "^1.4.1", "split": "^1.0.1", "through2": "^3.0.1" @@ -69,8 +66,6 @@ "@types/body-parser": "^1.17.0", "@types/express": "^4.16.1", "@types/helmet": "0.0.43", - "@types/leveldown": "^4.0.0", - "@types/levelup": "^3.1.0", "@types/lodash": "^4.14.123", "@types/node": "^20.8.10", "ava": "^5.3.1", diff --git a/packages/pico-engine/src/RulesetRegistryLoaderFs.ts b/packages/pico-engine/src/RulesetRegistryLoaderFs.ts index 12b57bd32..23de1afed 100644 --- a/packages/pico-engine/src/RulesetRegistryLoaderFs.ts +++ b/packages/pico-engine/src/RulesetRegistryLoaderFs.ts @@ -1,16 +1,14 @@ import fetch from "cross-fetch"; import * as fs from "fs"; -import leveldown from "leveldown"; -import { default as level } from "levelup"; +import { ClassicLevel } from "classic-level"; import * as makeDir from "make-dir"; import * as path from "path"; import { RulesetRegistryLoader } from "pico-engine-core"; -import { Ruleset } from "pico-framework"; +import { PicoDbKey, Ruleset } from "pico-framework"; import * as urlLib from "url"; const krlCompiler = require("krl-compiler"); const krlCompilerVersion = require("krl-compiler/package.json").version; const charwise = require("charwise"); -const encode = require("encoding-down"); const safeJsonCodec = require("level-json-coerce-null"); function getUrlFilename(url: string): string | null { @@ -40,12 +38,10 @@ export function RulesetRegistryLoaderFs( ): RulesetRegistryLoader { const rulesetDir = path.resolve(engineHomeDir, "rulesets"); - const db = level( - encode(leveldown(path.resolve(engineHomeDir, "rulesets-db")), { - keyEncoding: charwise, - valueEncoding: safeJsonCodec, - }) - ); + const db = new ClassicLevel(path.resolve(engineHomeDir, "rulesets-db"), { + keyEncoding: charwise, + valueEncoding: safeJsonCodec, + }) async function compileAndLoad( url: string, diff --git a/packages/pico-engine/src/index.ts b/packages/pico-engine/src/index.ts index e238bf03f..c76464d09 100644 --- a/packages/pico-engine/src/index.ts +++ b/packages/pico-engine/src/index.ts @@ -1,16 +1,16 @@ import { krl, KrlLogger, makeKrlLogger } from "krl-stdlib"; -import leveldown from "leveldown"; +import { ClassicLevel } from "classic-level"; import * as _ from "lodash"; import * as makeDir from "make-dir"; import * as path from "path"; import { PicoEngineCore, RulesetRegistry } from "pico-engine-core"; -import { PicoFramework } from "pico-framework"; +import { PicoDbKey, PicoFramework } from "pico-framework"; import { getPicoLogs, makeRotatingFileLogWriter } from "./logging"; import { RulesetRegistryLoaderFs } from "./RulesetRegistryLoaderFs"; import { server } from "./server"; import { toFileUrl } from "./utils/toFileUrl"; -import * as fs from "fs"; -import { reject } from "lodash"; +const charwise = require("charwise"); +const safeJsonCodec = require("level-json-coerce-null"); const homeDir = require("home-dir"); const version = require("../package.json").version; @@ -85,7 +85,10 @@ export async function startEngine( : makeKrlLogger(makeRotatingFileLogWriter(logFilePath)); const core = new PicoEngineCore({ - leveldown: leveldown(path.resolve(home, "db")) as any, + db: new ClassicLevel(path.resolve(home, "db"), { + keyEncoding: charwise, + valueEncoding: safeJsonCodec, + }), rsRegLoader: RulesetRegistryLoaderFs(home), log, modules: configuration.modules, diff --git a/packages/pico-engine/test/test-rulesets/persistent.krl.ts b/packages/pico-engine/test/test-rulesets/persistent.krl.ts index fd03eff76..1a28ca976 100644 --- a/packages/pico-engine/test/test-rulesets/persistent.krl.ts +++ b/packages/pico-engine/test/test-rulesets/persistent.krl.ts @@ -77,19 +77,16 @@ test("persistent.krl", async t => { ///////////////////////////////////////////////////////////////////////////// // clear vars - function dumpEnts(picoId: string) { - return new Promise((resolve, reject) => { - const list: any = []; - const s = pe.pf.db.createReadStream({ - gte: ["entvar", picoId, "io.picolabs.persistent"], - lte: ["entvar", picoId, "io.picolabs.persistent", undefined] - }); - s.on("error", err => reject(err)); - s.on("end", () => resolve(list)); - s.on("data", function(data) { - list.push([data.key.slice(3), data.value]); - }); + async function dumpEnts(picoId: string) { + const list: any = []; + const iter = pe.pf.db.iterator({ + gte: ["entvar", picoId, "io.picolabs.persistent"], + lte: ["entvar", picoId, "io.picolabs.persistent", undefined] }); + for await (const [key, value] of iter) { + list.push([key.slice(3), value]); + } + return list } t.deepEqual(await dumpEnts(picoIdA), [ [["name"], "Alf"],