mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
* fix(cli): the browser the CLI launches exits with it, even on kill -9 * fix(build): the root build runs on Windows * perf(cli): a repeat check runs in a process that already has the CLI and Chrome up * fix(cli): a warm check delivers all its output before it exits * fix(cli): the warm check only talks to a server that proves it is the user's The warm socket now lives in a private, owner-verified directory (XDG_RUNTIME_DIR, ~/.cache or LOCALAPPDATA) next to a 0600 token. Client and server prove the token to each other over fresh nonces before any request is sent, the request carries an HMAC of the environment instead of the environment itself, and frames are capped. * fix(cli): a warm check prints its stdout even after something went to stderr The client stopped writing frames once any frame had been written, so a check that printed a notice to stderr first lost its JSON report. * test(cli): list the warm-check client fixture as a fallow entry instead of an inline ignore * fix(cli): a check started while a warm one is busy runs cold at once on Windows The warm server stopped listening when it accepted a client. On Windows the pipe name stays alive through the served connection, so a second client's connect saw a busy pipe and waited 30 s for a free instance before running cold. It now keeps listening while serving (accept already declines anyone else) and stops when the served check finishes. * test(cli): the orphan test reads the check's own Chrome and watchdog instead of racing a poll On Windows each process listing is a PowerShell call of about a second, so a check that finishes quickly could start and close Chrome between two polls, and the test failed its own 'Chrome was seen' precondition. A fixture now records the Chrome tree and the watchdog from inside the check when its first page opens.
115 lines
4.3 KiB
TypeScript
115 lines
4.3 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,
|
|
warmCheckDirectory,
|
|
warmCheckEndpoint,
|
|
} 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"]);
|
|
});
|
|
});
|