refactor(core): migrate to Bun and converge acquisition policies and Agent boundaries
This commit is contained in:
1 parent
3f1c6d02f6
commit
27c0f27434
268 files changed
+7468
-7852
No files matched your search
@@ -0,0 +1,65 @@
|
||||
import { serve, upgradeWebSocket } from "@umt/runtime";
|
||||
import { Hono } from "hono";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
it("Bun HTTP 适配通过 Hono 中间件升级 WebSocket,保留协议、文本与二进制消息", async () => {
|
||||
const app = new Hono();
|
||||
app.use("*", async (c, next) => {
|
||||
await next();
|
||||
c.header("x-test", "middleware");
|
||||
});
|
||||
app.get("/ws", (c) => {
|
||||
const { socket, response } = upgradeWebSocket(c.req.raw, { protocol: "umt-test" });
|
||||
socket.onmessage = (event) => socket.send(event.data);
|
||||
return response;
|
||||
});
|
||||
const server = serve({ hostname: "127.0.0.1", port: 0 }, app.fetch);
|
||||
const client = new WebSocket(`ws://127.0.0.1:${server.addr.port}/ws`, ["umt-test"]);
|
||||
client.binaryType = "arraybuffer";
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
client.onopen = () => resolve();
|
||||
client.onerror = () => reject(new Error("upgrade failed"));
|
||||
});
|
||||
expect(client.protocol).toBe("umt-test");
|
||||
const echo = (data: string | ArrayBuffer) =>
|
||||
new Promise<unknown>((resolve) => {
|
||||
client.onmessage = (event) => resolve(event.data);
|
||||
client.send(data);
|
||||
});
|
||||
expect(await echo("hello")).toBe("hello");
|
||||
expect(new Uint8Array((await echo(new Uint8Array([1, 2, 3]).buffer)) as ArrayBuffer)).toEqual(
|
||||
new Uint8Array([1, 2, 3]),
|
||||
);
|
||||
} finally {
|
||||
client.close();
|
||||
await server.shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
it("Bun WebSocket 上游连接发送自定义认证头", async () => {
|
||||
let authorization: string | null = null;
|
||||
const server = Bun.serve({
|
||||
hostname: "127.0.0.1",
|
||||
port: 0,
|
||||
fetch(req, server) {
|
||||
authorization = req.headers.get("authorization");
|
||||
if (server.upgrade(req)) return undefined;
|
||||
return new Response(null, { status: 400 });
|
||||
},
|
||||
websocket: { message() {} },
|
||||
});
|
||||
const client = new WebSocket(`ws://127.0.0.1:${server.port}`, {
|
||||
headers: { authorization: "Basic test" },
|
||||
} as unknown as string[]);
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
client.onopen = () => resolve();
|
||||
client.onerror = () => reject(new Error("connect failed"));
|
||||
});
|
||||
expect(authorization).toBe("Basic test");
|
||||
} finally {
|
||||
client.close();
|
||||
await server.stop(true);
|
||||
}
|
||||
});
|
||||
@@ -1,7 +1,8 @@
|
||||
// 生产权限下读 /proc、/sys:Deno 2 不带 --allow-all 时 readTextFile 一律 NotCapable,
|
||||
// 编译产物的资源监控与沙箱内存看门狗曾因此静默取不到数(v0.3.34 运行状态页一直无数据)。
|
||||
// 这里用与 apps/server/deno.json compile 相同的权限起子进程,确认走 cat 兜底能读到。
|
||||
// 这里用与 apps/server/package.json compile 相同的权限起子进程,确认走 cat 兜底能读到。
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { Command } from "@umt/runtime";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const here = fileURLToPath(new URL(".", import.meta.url));
|
||||
@@ -13,20 +14,10 @@ const r = await readResources();
|
||||
console.log(JSON.stringify({ stat: !!stat && stat.startsWith("cpu"), host: r.host != null, mem: r.meminfo != null }));
|
||||
`;
|
||||
|
||||
describe.skipIf(Deno.build.os !== "linux")("readProcText 在生产权限下", () => {
|
||||
describe.skipIf(process.platform !== "linux")("readProcText 在生产权限下", () => {
|
||||
it("走 cat 兜底读到 /proc 与资源读数", async () => {
|
||||
const out = await new Deno.Command(Deno.execPath(), {
|
||||
args: [
|
||||
"run",
|
||||
"--no-prompt",
|
||||
"--allow-net",
|
||||
"--allow-env",
|
||||
"--allow-sys",
|
||||
"--allow-read",
|
||||
"--allow-write",
|
||||
"--allow-run=ffprobe,sha256sum,cat",
|
||||
"-",
|
||||
],
|
||||
const out = await new Command(process.execPath, {
|
||||
args: ["run", "-"],
|
||||
stdin: "piped",
|
||||
stdout: "piped",
|
||||
stderr: "piped",
|
||||
|
||||
@@ -4,6 +4,7 @@ import { join } from "node:path";
|
||||
import type { DiskUsage } from "@umt/adapters";
|
||||
import { SystemHealth, SystemHealthHistory, SystemResources } from "@umt/contracts";
|
||||
import { sql } from "@umt/db";
|
||||
import { runtimeMkdir, runtimeWriteTextFile } from "@umt/runtime";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { inferTriggeredBy, triggeredByFromActor } from "../../src/modules/auth-settings/engine-business.ts";
|
||||
import { scheduleRegistry } from "../../src/modules/schedules/registry.ts";
|
||||
@@ -233,8 +234,8 @@ describe.skipIf(!hasPg)("W9-I 进程与健康(真实 PG)", () => {
|
||||
ctx: h.ctx,
|
||||
}) as Harness["app"];
|
||||
admin = bearer(h.app, DEV_KEY);
|
||||
await Deno.mkdir(libOk, { recursive: true });
|
||||
await Deno.writeTextFile(join(libOk, ".umt-root"), "x");
|
||||
await runtimeMkdir(libOk, { recursive: true });
|
||||
await runtimeWriteTextFile(join(libOk, ".umt-root"), "x");
|
||||
}, 60_000);
|
||||
afterAll(async () => {
|
||||
await h?.close();
|
||||
|
||||
Reference in new issue
Block a user