Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs";
import type { ServerResponse } from "node:http";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterAll, describe, expect, it } from "vitest";
import { serveStatic } from "./static-files.ts";
const dir = mkdtempSync(join(tmpdir(), "omb-static-"));
writeFileSync(join(dir, "index.html"), "app");
writeFileSync(join(dir, "rail.html"), "rail");
mkdirSync(join(dir, "assets"));
writeFileSync(join(dir, "assets", "rail-1a2b.js"), "js");
afterAll(() => rmSync(dir, { recursive: true, force: true }));
function get(path: string, staticDir: string = dir, headers: Record<string, string> = {}) {
const answer: { status?: number; headers: Record<string, string | number>; body?: string } = { headers: {} };
const res = {
writeHead(status: number, h: Record<string, string | number>) { answer.status = status; answer.headers = h; return res; },
end(data?: Buffer) { answer.body = data === undefined ? undefined : String(data); },
} as unknown as ServerResponse;
return { served: serveStatic(res, path, staticDir, { headers }), ...answer };
}
describe("serveStatic", () => {
it("answers the window's rail with its own small page, never cached", () => {
const rail = get("/rail");
expect(rail.body).toBe("rail");
expect(rail.headers).toMatchObject({ "content-type": "text/html", "cache-control": "no-cache", "x-frame-options": "DENY" });
expect(rail.headers.etag).toMatch(/^W\/"/);
expect(rail.headers["content-length"]).toBe(4);
});
it("answers the app's paths with the app, and keeps hashed assets for good", () => {
expect(get("/").body).toBe("app");
expect(get("/settings").body).toBe("app");
expect(get("/rail/more").body).toBe("app");
expect(get("/assets/rail-1a2b.js").headers["cache-control"]).toBe("public, max-age=31536000, immutable");
});
it("returns 304 Not Modified when If-None-Match matches the current ETag, and refreshes after file updates", () => {
const first = get("/");
expect(first.status).toBe(200);
const etag = String(first.headers.etag);
const notModified = get("/", dir, { "if-none-match": etag });
expect(notModified.served).toBe(true);
expect(notModified.status).toBe(304);
expect(notModified.body).toBeUndefined();
expect(notModified.headers["cache-control"]).toBe("no-cache");
const file = join(dir, "index.html");
writeFileSync(file, "app-v2");
const future = new Date(Date.now() + 5_000);
utimesSync(file, future, future);
const updated = get("/", dir, { "if-none-match": etag });
expect(updated.status).toBe(200);
expect(updated.body).toBe("app-v2");
expect(updated.headers.etag).not.toBe(etag);
});
});