mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
A caller that runs several renders at once sets HYPERFRAMES_OWNER to a per-run id; every browser the CLI or engine launches and the warm check server carry --hyperframes-owner=<id>, so the caller can tell one run's leftovers from another's.
131 lines
5.0 KiB
TypeScript
131 lines
5.0 KiB
TypeScript
import { chmodSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { describe, expect, it, onTestFinished } from "vitest";
|
|
import {
|
|
Frame,
|
|
encodeFrame,
|
|
envDigest,
|
|
frameForwarder,
|
|
frameReader,
|
|
parseWarmServerArgs,
|
|
warmCheckDirectory,
|
|
warmCheckEndpoint,
|
|
warmServerArgs,
|
|
} from "./warmCheckProtocol.js";
|
|
|
|
const TOKEN = "a".repeat(64);
|
|
|
|
function base(): string {
|
|
const dir = mkdtempSync("/tmp/hfw-proto-");
|
|
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
|
|
return dir;
|
|
}
|
|
|
|
describe.skipIf(process.platform === "win32")("the warm check endpoint", () => {
|
|
it("lives in a private directory it creates, with a private token", () => {
|
|
const runtime = base();
|
|
const endpoint = warmCheckEndpoint({ XDG_RUNTIME_DIR: runtime });
|
|
expect(endpoint?.address.startsWith(join(runtime, "hyperframes-warm"))).toBe(true);
|
|
expect(endpoint?.token).toMatch(/^[0-9a-f]{64}$/);
|
|
expect(warmCheckEndpoint({ XDG_RUNTIME_DIR: runtime })).toEqual(endpoint);
|
|
});
|
|
|
|
it("refuses a directory others can write to, or one that is a symlink", () => {
|
|
const open = base();
|
|
mkdirSync(join(open, "hyperframes-warm"));
|
|
chmodSync(join(open, "hyperframes-warm"), 0o777);
|
|
expect(warmCheckDirectory({ XDG_RUNTIME_DIR: open })).toBeUndefined();
|
|
expect(warmCheckEndpoint({ XDG_RUNTIME_DIR: open })).toBeUndefined();
|
|
|
|
const linked = base();
|
|
symlinkSync(base(), join(linked, "hyperframes-warm"));
|
|
expect(warmCheckEndpoint({ XDG_RUNTIME_DIR: linked })).toBeUndefined();
|
|
});
|
|
|
|
it("refuses a token file others can read, or one that is a symlink", () => {
|
|
const readable = base();
|
|
mkdirSync(join(readable, "hyperframes-warm"), { mode: 0o700 });
|
|
writeFileSync(join(readable, "hyperframes-warm", "token"), TOKEN, { mode: 0o644 });
|
|
expect(warmCheckEndpoint({ XDG_RUNTIME_DIR: readable })).toBeUndefined();
|
|
|
|
const linked = base();
|
|
mkdirSync(join(linked, "hyperframes-warm"), { mode: 0o700 });
|
|
const elsewhere = join(base(), "token");
|
|
writeFileSync(elsewhere, TOKEN, { mode: 0o600 });
|
|
symlinkSync(elsewhere, join(linked, "hyperframes-warm", "token"));
|
|
expect(warmCheckEndpoint({ XDG_RUNTIME_DIR: linked })).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe("envDigest", () => {
|
|
it("ignores the shell's per-command variables and unset values", () => {
|
|
expect(envDigest(TOKEN, { A: "1", _: "/x", PWD: "/a", B: undefined })).toBe(
|
|
envDigest(TOKEN, { A: "1", _: "/y", OLDPWD: "/b" }),
|
|
);
|
|
});
|
|
|
|
it("tells apart any added, removed or changed variable, and other tokens", () => {
|
|
const digest = envDigest(TOKEN, { A: "1" });
|
|
expect(envDigest(TOKEN, { A: "2" })).not.toBe(digest);
|
|
expect(envDigest(TOKEN, { A: "1", B: "1" })).not.toBe(digest);
|
|
expect(envDigest(TOKEN, {})).not.toBe(digest);
|
|
expect(envDigest("b".repeat(64), { A: "1" })).not.toBe(digest);
|
|
});
|
|
});
|
|
|
|
describe("frames", () => {
|
|
it("decodes frames however the bytes are split", () => {
|
|
const bytes = Buffer.concat([
|
|
encodeFrame(Frame.Stdout, "héllo"),
|
|
encodeFrame(Frame.Exit, "0"),
|
|
encodeFrame(Frame.Stderr, Buffer.alloc(0)),
|
|
]);
|
|
const seen: [number, string][] = [];
|
|
const read = frameReader((type, body) => seen.push([type, body.toString()]));
|
|
for (const byte of bytes) expect(read(Buffer.from([byte]))).toBe(true);
|
|
expect(seen).toEqual([
|
|
[Frame.Stdout, "héllo"],
|
|
[Frame.Exit, "0"],
|
|
[Frame.Stderr, ""],
|
|
]);
|
|
});
|
|
|
|
it("stops reading at a frame that claims more than the cap", () => {
|
|
const head = Buffer.alloc(5);
|
|
head.writeUInt8(Frame.Stdout, 0);
|
|
head.writeUInt32BE(0xffffffff, 1);
|
|
const read = frameReader(() => {
|
|
throw new Error("no frame expected");
|
|
});
|
|
expect(read(head)).toBe(false);
|
|
expect(read(encodeFrame(Frame.Exit, "0"))).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("frameForwarder", () => {
|
|
it("hands every write to the socket before returning, so ending the socket loses none", () => {
|
|
const sent: Buffer[] = [];
|
|
const write = frameForwarder({ write: (chunk: Buffer) => sent.push(chunk) > 0 }, Frame.Stdout);
|
|
write("one ");
|
|
write(Buffer.from("two "));
|
|
write("thrée", "utf8");
|
|
const seen: string[] = [];
|
|
frameReader((_type, body) => seen.push(body.toString()))(Buffer.concat(sent));
|
|
expect(seen).toEqual(["one ", "two ", "thrée"]);
|
|
});
|
|
});
|
|
|
|
// fails without the fix: a caller running several renders at once could not tell whose warm check server was whose.
|
|
describe("the warm server's command line", () => {
|
|
it("names the caller's owner and hands the check its own args back", () => {
|
|
const argv = warmServerArgs(["--json", "./film"], { HYPERFRAMES_OWNER: "run-7" });
|
|
expect(argv).toContain("--hyperframes-owner=run-7");
|
|
expect(parseWarmServerArgs(argv)).toEqual({ idleMs: 300_000, checkArgs: ["--json", "./film"] });
|
|
});
|
|
|
|
it("names no owner when the caller sets none or one that is not a plain id", () => {
|
|
for (const owner of [undefined, "", "a b", "x".repeat(65)])
|
|
expect(warmServerArgs(["."], { HYPERFRAMES_OWNER: owner })).toEqual(["300000", "."]);
|
|
});
|
|
});
|