Work on plug:run

This commit is contained in:
Zef Hemel
2023-08-11 20:37:13 +02:00
parent d4f7833f0d
commit 4dbbc31cb9
24 changed files with 174 additions and 104 deletions
+31 -37
View File
@@ -1,48 +1,42 @@
import { createSandbox } from "../environments/deno_sandbox.ts";
import { Manifest } from "../types.ts";
import { EndpointHook, EndpointHookT } from "./endpoint.ts";
import { System } from "../system.ts";
import { Application } from "../../server/deps.ts";
import { assertEquals } from "../../test_deps.ts";
import { compileManifest } from "../compile.ts";
import { esbuild } from "../deps.ts";
// Deno.test("Run a plugos endpoint server", async () => {
// const system = new System<EndpointHookT>("server");
// await system.load(
// {
// name: "test",
// functions: {
// testhandler: {
// http: {
// path: "/",
// },
// code: `(() => {
// return {
// default: (req) => {
// console.log("Req", req);
// return {status: 200, body: [1, 2, 3], headers: {"Content-type": "application/json"}};
// }
// };
// })()`,
// },
// },
// } as Manifest<EndpointHookT>,
// createSandbox,
// );
Deno.test("Run a plugos endpoint server", async () => {
const tempDir = await Deno.makeTempDir();
const system = new System<EndpointHookT>("server");
// const app = new Application();
// const port = 3123;
const workerPath = await compileManifest(
new URL("../test.plug.yaml", import.meta.url).pathname,
tempDir,
);
// system.addHook(new EndpointHook(app, "/_"));
await system.load(
new URL(`file://${workerPath}`),
createSandbox,
);
// const controller = new AbortController();
// app.listen({ port: port, signal: controller.signal });
const app = new Application();
const port = 3123;
// const res = await fetch(`http://localhost:${port}/_/test/?name=Pete`);
// assertEquals(res.status, 200);
// assertEquals(res.headers.get("Content-type"), "application/json");
// assertEquals(await res.json(), [1, 2, 3]);
// console.log("Aborting");
// controller.abort();
// await system.unloadAll();
// });
system.addHook(new EndpointHook(app, "/_"));
const controller = new AbortController();
app.listen({ port: port, signal: controller.signal });
const res = await fetch(`http://localhost:${port}/_/test/?name=Pete`);
assertEquals(res.status, 200);
assertEquals(res.headers.get("Content-type"), "application/json");
assertEquals(await res.json(), [1, 2, 3]);
console.log("Aborting");
controller.abort();
await system.unloadAll();
await Deno.remove(tempDir, { recursive: true });
esbuild.stop();
});
+1
View File
@@ -58,6 +58,7 @@ export class EndpointHook implements Hook<EndpointHookT> {
if (!functionDef.http) {
continue;
}
console.log("Got config", functionDef);
const endpoints = Array.isArray(functionDef.http)
? functionDef.http
: [functionDef.http];
+1 -1
View File
@@ -2,7 +2,7 @@ import { Hook, Manifest } from "../types.ts";
import { System } from "../system.ts";
import { DexieMQ } from "../lib/mq.dexie.ts";
import { fullQueueName } from "../lib/mq_util.ts";
import { Message } from "$sb/mq.ts";
import { Message } from "$sb/types.ts";
type MQSubscription = {
queue: string;
+2
View File
@@ -8,9 +8,11 @@ export class DexieKVStore implements KVStore {
dbName: string,
tableName: string,
indexedDB?: any,
IDBKeyRange?: any,
) {
this.db = new Dexie(dbName, {
indexedDB,
IDBKeyRange,
});
this.db.version(1).stores({
[tableName]: "key",
+1 -7
View File
@@ -1,5 +1,5 @@
import Dexie, { Table } from "dexie";
import { Message } from "$sb/mq.ts";
import { Message, QueueStats } from "$sb/types.ts";
export type ProcessingMessage = Message & {
ts: number;
@@ -10,12 +10,6 @@ export type SubscribeOptions = {
pollInterval?: number;
};
export type QueueStats = {
queued: number;
processing: number;
dlq: number;
};
export class DexieMQ {
db: Dexie;
queued: Table<Message, [string, string]>;
+3
View File
@@ -18,5 +18,8 @@ export function mqSyscalls(
"mq.batchAck": (ctx, queue: string, ids: string[]) => {
return mq.batchAck(fullQueueName(ctx.plug.name!, queue), ids);
},
"mq.getQueueStats": (ctx, queue: string) => {
return mq.getQueueStats(fullQueueName(ctx.plug.name!, queue));
},
};
}
+4
View File
@@ -2,3 +2,7 @@ name: test
functions:
boot:
path: "./test_func.test.ts:hello"
endpoint:
path: "./test_func.test.ts:endpoint"
http:
path: "/"
+10
View File
@@ -1,7 +1,17 @@
import * as YAML from "https://deno.land/std@0.184.0/yaml/mod.ts";
import { EndpointRequest, EndpointResponse } from "./hooks/endpoint.ts";
export function hello() {
console.log(YAML.stringify({ hello: "world" }));
return "hello";
}
export function endpoint(req: EndpointRequest): EndpointResponse {
console.log("Req", req);
return {
status: 200,
body: [1, 2, 3],
headers: { "Content-type": "application/json" },
};
}