kernel-sandbox/src/index.ts

46 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-07-20 20:13:36 +00:00
import * as path from "path";
import * as kernel from "@lumeweb/libkernel/kernel";
2023-07-02 06:42:43 +00:00
2022-06-30 20:12:43 +00:00
// @ts-ignore
2023-07-20 20:13:36 +00:00
import StaticServer from "static-server";
import { Page } from "puppeteer";
import { bufToHex, ed25519, ErrTuple } from "@lumeweb/libkernel";
2023-07-02 06:42:43 +00:00
2023-07-20 20:13:36 +00:00
import * as url from "url";
2023-07-02 06:42:43 +00:00
2023-07-20 20:13:36 +00:00
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
2022-07-20 18:01:54 +00:00
2022-06-30 20:12:43 +00:00
export function generateSeedPhrase() {
2023-07-02 06:42:43 +00:00
return ed25519.utils.randomPrivateKey();
2022-06-30 20:12:43 +00:00
}
export async function login(page: Page, seed = generateSeedPhrase()) {
2023-07-20 20:13:36 +00:00
await page.goto("http://kernel.lumeweb.com");
2022-06-30 20:12:43 +00:00
2023-07-02 06:42:43 +00:00
let seedHex = bufToHex(seed);
2022-06-30 20:12:43 +00:00
await page.evaluate((seed: string) => {
2023-07-20 20:13:36 +00:00
window.localStorage.setItem("v1-key", seed);
2022-06-30 20:12:43 +00:00
}, seedHex);
}
export async function loadTester(page: Page, port = 8080) {
const server = new StaticServer({
2023-07-20 20:13:36 +00:00
rootPath: path.resolve(__dirname, "..", "public"),
2022-06-30 20:12:43 +00:00
port,
2023-07-20 20:13:36 +00:00
host: "localhost",
2022-06-30 20:12:43 +00:00
});
await new Promise((resolve) => {
server.start(resolve);
});
const stop = () => server.stop();
2023-07-20 20:13:36 +00:00
process.on("SIGTERM", stop);
page.browser().on("disconnected", stop);
2022-06-30 20:12:43 +00:00
await page.goto(`http://localhost:${port}/`);
await page.evaluate(() => {
return kernel.init();
});
}