Monorepo with yarn workspaces requires yarn 3.2

This commit is contained in:
Zef Hemel
2022-04-21 13:57:45 +02:00
parent 32f3501773
commit 1f842ec1d6
167 changed files with 10424 additions and 8263 deletions
-10
View File
@@ -1,10 +0,0 @@
import { SysCallMapping } from "../system";
import { EventHook } from "../hooks/event";
export function eventSyscalls(eventHook: EventHook): SysCallMapping {
return {
"event.dispatch": async (ctx, eventName: string, data: any) => {
return eventHook.dispatchEvent(eventName, data);
},
};
}
-15
View File
@@ -1,15 +0,0 @@
import fetch, { RequestInfo, RequestInit } from "node-fetch";
import { SysCallMapping } from "../system";
export function fetchSyscalls(): SysCallMapping {
return {
"fetch.json": async (ctx, url: RequestInfo, init: RequestInit) => {
let resp = await fetch(url, init);
return resp.json();
},
"fetch.text": async (ctx, url: RequestInfo, init: RequestInit) => {
let resp = await fetch(url, init);
return resp.text();
},
};
}
-22
View File
@@ -1,22 +0,0 @@
import jwt, { Algorithm } from "jsonwebtoken";
import { SysCallMapping } from "../system";
export function jwtSyscalls(): SysCallMapping {
return {
"jwt.jwt": (
ctx,
hexSecret: string,
id: string,
algorithm: Algorithm,
expiry: string,
audience: string
): string => {
return jwt.sign({}, Buffer.from(hexSecret, "hex"), {
keyid: id,
algorithm: algorithm,
expiresIn: expiry,
audience: audience,
});
},
};
}
-20
View File
@@ -1,20 +0,0 @@
import { promisify } from "util";
import { execFile } from "child_process";
import type { SysCallMapping } from "../system";
const execFilePromise = promisify(execFile);
export default function (cwd: string): SysCallMapping {
return {
"shell.run": async (
ctx,
cmd: string,
args: string[]
): Promise<{ stdout: string; stderr: string }> => {
let { stdout, stderr } = await execFilePromise(cmd, args, {
cwd: cwd,
});
return { stdout, stderr };
},
};
}
@@ -1,49 +0,0 @@
import { createSandbox } from "../environments/node_sandbox";
import { expect, test } from "@jest/globals";
import { System } from "../system";
import { storeSyscalls } from "./store.dexie_browser";
// For testing in node.js
require("fake-indexeddb/auto");
test("Test store", async () => {
let system = new System("server");
system.registerSyscalls([], storeSyscalls("test", "test"));
let plug = await system.load(
"test",
{
functions: {
test1: {
code: `(() => {
return {
default: async () => {
await self.syscall("store.set", "name", "Pete");
return await self.syscall("store.get", "name");
}
};
})()`,
},
test2: {
code: `(() => {
return {
default: async () => {
await self.syscall("store.set", "page1:bl:page2:10", {title: "Something", meta: 20});
await self.syscall("store.batchSet", [
{key: "page2:bl:page3", value: {title: "Something2", meta: 10}},
{key: "page2:bl:page4", value: {title: "Something3", meta: 10}},
]);
return await self.syscall("store.queryPrefix", "page2:");
}
};
})()`,
},
},
},
createSandbox
);
expect(await plug.invoke("test1", [])).toBe("Pete");
let queryResults = await plug.invoke("test2", []);
expect(queryResults.length).toBe(2);
expect(queryResults[0].value.meta).toBe(10);
await system.unloadAll();
});
-66
View File
@@ -1,66 +0,0 @@
import Dexie from "dexie";
import { SysCallMapping } from "../system";
export type KV = {
key: string;
value: any;
};
export function storeSyscalls(
dbName: string,
tableName: string
): SysCallMapping {
const db = new Dexie(dbName);
db.version(1).stores({
[tableName]: "key",
});
const items = db.table(tableName);
return {
"store.delete": async (ctx, key: string) => {
await items.delete(key);
},
"store.deletePrefix": async (ctx, prefix: string) => {
await items.where("key").startsWith(prefix).delete();
},
"store.deleteAll": async () => {
await items.clear();
},
"store.set": async (ctx, key: string, value: any) => {
await items.put({
key,
value,
});
},
"store.batchSet": async (ctx, kvs: KV[]) => {
await items.bulkPut(
kvs.map(({ key, value }) => ({
key,
value,
}))
);
},
"store.get": async (ctx, key: string): Promise<any | null> => {
let result = await items.get({
key,
});
return result ? result.value : null;
},
"store.queryPrefix": async (
ctx,
keyPrefix: string
): Promise<{ key: string; value: any }[]> => {
let results = await items.where("key").startsWith(keyPrefix).toArray();
return results.map((result) => ({
key: result.key,
value: result.value,
}));
},
};
}
-40
View File
@@ -1,40 +0,0 @@
import { createSandbox } from "../environments/node_sandbox";
import { expect, test } from "@jest/globals";
import { System } from "../system";
import { ensureTable, storeSyscalls } from "./store.knex_node";
import knex from "knex";
import fs from "fs/promises";
test("Test store", async () => {
const db = knex({
client: "better-sqlite3",
connection: {
filename: "test.db",
},
useNullAsDefault: true,
});
await ensureTable(db, "test_table");
let system = new System("server");
system.registerSyscalls([], storeSyscalls(db, "test_table"));
let plug = await system.load(
"test",
{
functions: {
test1: {
code: `(() => {
return {
default: async () => {
await self.syscall("store.set", "name", "Pete");
return await self.syscall("store.get", "name");
}
};
})()`,
},
},
},
createSandbox
);
expect(await plug.invoke("test1", [])).toBe("Pete");
await system.unloadAll();
await fs.unlink("test.db");
});
-82
View File
@@ -1,82 +0,0 @@
import { Knex } from "knex";
import { SysCallMapping } from "../system";
type Item = {
page: string;
key: string;
value: any;
};
export type KV = {
key: string;
value: any;
};
export async function ensureTable(db: Knex<any, unknown>, tableName: string) {
if (!(await db.schema.hasTable(tableName))) {
await db.schema.createTable(tableName, (table) => {
table.string("key");
table.text("value");
table.primary(["key"]);
});
console.log(`Created table ${tableName}`);
}
}
export function storeSyscalls(
db: Knex<any, unknown>,
tableName: string
): SysCallMapping {
const apiObj: SysCallMapping = {
"store.delete": async (ctx, key: string) => {
await db<Item>(tableName).where({ key }).del();
},
"store.deletePrefix": async (ctx, prefix: string) => {
return db<Item>(tableName).andWhereLike("key", `${prefix}%`).del();
},
"store.deleteAll": async (ctx) => {
await db<Item>(tableName).del();
},
"store.set": async (ctx, key: string, value: any) => {
let changed = await db<Item>(tableName)
.where({ key })
.update("value", JSON.stringify(value));
if (changed === 0) {
await db<Item>(tableName).insert({
key,
value: JSON.stringify(value),
});
}
},
// TODO: Optimize
"store.batchSet": async (ctx, kvs: KV[]) => {
for (let { key, value } of kvs) {
await apiObj["store.set"](ctx, key, value);
}
},
"store.batchDelete": async (ctx, keys: string[]) => {
for (let key of keys) {
await apiObj["store.delete"](ctx, key);
}
},
"store.get": async (ctx, key: string): Promise<any | null> => {
let result = await db<Item>(tableName).where({ key }).select("value");
if (result.length) {
return JSON.parse(result[0].value);
} else {
return null;
}
},
"store.queryPrefix": async (ctx, prefix: string) => {
return (
await db<Item>(tableName)
.andWhereLike("key", `${prefix}%`)
.select("key", "value")
).map(({ key, value }) => ({
key,
value: JSON.parse(value),
}));
},
};
return apiObj;
}
-20
View File
@@ -1,20 +0,0 @@
import { SyscallContext, SysCallMapping } from "../system";
export function proxySyscalls(
names: string[],
transportCall: (
ctx: SyscallContext,
name: string,
...args: any[]
) => Promise<any>
): SysCallMapping {
let syscalls: SysCallMapping = {};
for (let name of names) {
syscalls[name] = (ctx, ...args: any[]) => {
return transportCall(ctx, name, ...args);
};
}
return syscalls;
}