Switch from Oak to Hono

This commit is contained in:
Zef Hemel
2024-01-13 18:07:02 +01:00
parent bf1eb03129
commit 291280b709
9 changed files with 372 additions and 458 deletions
+4 -5
View File
@@ -2,10 +2,10 @@ import { createSandbox } from "../environments/deno_sandbox.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";
import { Hono } from "../../server/deps.ts";
Deno.test("Run a plugos endpoint server", async () => {
const tempDir = await Deno.makeTempDir();
@@ -23,21 +23,20 @@ Deno.test("Run a plugos endpoint server", async () => {
createSandbox,
);
const app = new Application();
const app = new Hono();
const port = 3123;
const endpointHook = new EndpointHook("/_/");
app.use((context, next) => {
app.all("*", (context, next) => {
return endpointHook.handleRequest(system, context, next);
});
const controller = new AbortController();
app.listen({ port: port, signal: controller.signal });
Deno.serve({ port: port, signal: controller.signal }, app.fetch);
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();
+19 -15
View File
@@ -1,6 +1,6 @@
import { Hook, Manifest } from "../types.ts";
import { System } from "../system.ts";
import { Application, Context, Next } from "../../server/deps.ts";
import { Context, Next } from "../../server/deps.ts";
export type EndpointRequest = {
method: string;
@@ -37,8 +37,9 @@ export class EndpointHook implements Hook<EndpointHookT> {
ctx: Context,
next: Next,
) {
const req = ctx.request;
const requestPath = ctx.request.url.pathname;
const req = ctx.req;
const url = new URL(req.url);
const requestPath = url.pathname;
if (!requestPath.startsWith(this.prefix)) {
return next();
}
@@ -73,13 +74,13 @@ export class EndpointHook implements Hook<EndpointHookT> {
try {
const response: EndpointResponse = await plug.invoke(name, [
{
path: req.url.pathname,
path: url.pathname,
method: req.method,
body: req.body(),
body: await req.text(),
query: Object.fromEntries(
req.url.searchParams.entries(),
url.searchParams.entries(),
),
headers: Object.fromEntries(req.headers.entries()),
headers: req.header(),
} as EndpointRequest,
]);
if (response.headers) {
@@ -88,18 +89,21 @@ export class EndpointHook implements Hook<EndpointHookT> {
response.headers,
)
) {
ctx.response.headers.set(key, value);
ctx.header(key, value);
}
}
ctx.response.status = response.status;
ctx.response.body = response.body;
// console.log("Sent result");
return;
ctx.status(response.status);
console.log("Going to return", response.body);
if (typeof response.body === "string") {
return ctx.text(response.body);
} else if (response.body instanceof Uint8Array) {
return ctx.body(response.body);
} else {
return ctx.json(response.body);
}
} catch (e: any) {
console.error("Error executing function", e);
ctx.response.status = 500;
ctx.response.body = e.message;
return;
return ctx.body(e.message, 500);
}
}
}