kernel-sandbox/dist/index.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-03-31 19:42:33 +00:00
import { bufToHex } from "libskynet";
2022-07-20 18:06:18 +00:00
import * as path from "path";
import * as kernel from "libkernel";
2023-03-31 19:42:33 +00:00
import * as ed from "@noble/ed25519";
2022-07-20 17:51:16 +00:00
// @ts-ignore
2022-07-20 18:06:18 +00:00
import StaticServer from "static-server";
import * as url from "url";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
export function generateSeedPhrase() {
2023-03-31 19:42:33 +00:00
return ed.utils.randomPrivateKey();
2022-06-30 20:13:15 +00:00
}
2022-07-20 18:06:18 +00:00
export async function login(page, seed = generateSeedPhrase()) {
2023-03-31 19:42:33 +00:00
await page.goto("http://kernel.lumeweb.com");
let userSeed = seed;
let seedHex = bufToHex(userSeed);
await page.evaluate((seed) => {
window.localStorage.setItem("v1-key", seed);
}, seedHex);
2022-07-20 17:51:16 +00:00
}
2023-03-31 19:42:33 +00:00
export async function loadTester(page, port = 8081) {
const server = new StaticServer({
rootPath: path.resolve(__dirname, "..", "public"),
port,
host: "localhost",
});
await new Promise((resolve) => {
server.start(resolve);
});
const stop = () => server.stop();
process.on("SIGTERM", stop);
page.browser().on("disconnected", stop);
await page.goto(`http://localhost:${port}/`);
await page.evaluate(() => {
return kernel.init();
});
2022-07-20 17:51:16 +00:00
}
class Tester {
2023-03-31 19:42:33 +00:00
page;
constructor(page) {
this.page = page;
}
async callModule(id, method, data = {}) {
return this.page.evaluate(
async (id, method, data) => {
return kernel.callModule(id, method, data);
},
id,
method,
data
);
}
2022-07-20 17:51:16 +00:00
}
2022-07-20 18:06:18 +00:00
export const tester = (page) => new Tester(page);