Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

test(credentials): guard the file store against simultaneous writers

Mikee Ai10a11d5f5259
diff --git a/packages/credential-store-file/test/concurrency.test.ts b/packages/credential-store-file/test/concurrency.test.ts
new file mode 100644
index 00000000..20766b80
--- /dev/null
+++ b/packages/credential-store-file/test/concurrency.test.ts
@@ -0,0 +1,72 @@
+import assert from "node:assert/strict";
+import { spawn } from "node:child_process";
+import { mkdtemp, readdir, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { fileURLToPath } from "node:url";
+import test from "node:test";
+import { credentialRef } from "@hypit/runtime";
+import { FileCredentialStore } from "../src/store.js";
+
+const distribution = fileURLToPath(new URL("../../../", import.meta.url));
+const fixture = fileURLToPath(new URL("./store-writer-fixture.ts", import.meta.url));
+const writers = 8;
+
+type Exit = { code: number; stderr: string };
+
+/**
+ * Place every credential from its own process, released at one shared instant. A Store that keeps
+ * every key in one document reads the value it is about to replace, so simultaneous replacements
+ * overwrite each other and the last writer's document wins; keeping the schedule spread out instead
+ * would let that layout pass, which is why the barrier is the assertion.
+ */
+async function placeSimultaneously(directory: string, entries: { key: string; secret: string }[]): Promise<Exit[]> {
+ const startAt = Date.now() + 3_000;
+ return Promise.all(entries.map(({ key, secret }) => new Promise<Exit>((settle) => {
+ const writer = spawn(process.execPath, ["--import", "tsx", fixture, directory, key, secret, String(startAt)], {
+ cwd: distribution, stdio: ["ignore", "ignore", "pipe"], windowsHide: true,
+ });
+ let stderr = "";
+ writer.stderr.on("data", (chunk) => { stderr += String(chunk); });
+ writer.on("close", (code) => settle({ code: code ?? 1, stderr }));
+ })));
+}
+
+test("processes placing different keys at one instant keep every credential", { timeout: 120_000 }, async () => {
+ const root = await mkdtemp(join(tmpdir(), "hypit-file-credentials-process-"));
+ try {
+ const directory = join(root, "private");
+ const entries = Array.from({ length: writers }, (_, index) => ({
+ key: `instance-${index}`, secret: `secret-${index}`,
+ }));
+ const exits = await placeSimultaneously(directory, entries);
+ for (const [index, exit] of exits.entries()) {
+ assert.equal(exit.code, 0, `writer ${index} failed: ${exit.stderr}`);
+ }
+ const store = new FileCredentialStore(directory);
+ for (const { key, secret } of entries) {
+ assert.deepEqual(await store.resolve(credentialRef("file", key)), { secret },
+ `${key} was placed by a writer that completed without error, so it must still be readable`);
+ }
+ assert.deepEqual((await readdir(directory)).sort(), entries.map(({ key }) =>
+ `key-${Buffer.from(key, "utf16le").toString("hex")}.json`).sort(),
+ "each key owns its own document, with no temporary file left behind");
+ } finally { await rm(root, { recursive: true, force: true }); }
+});
+
+test("two processes placing the same key leave one complete value", { timeout: 120_000 }, async () => {
+ const root = await mkdtemp(join(tmpdir(), "hypit-file-credentials-process-"));
+ try {
+ const directory = join(root, "private");
+ const entries = [{ key: "shared", secret: "first" }, { key: "shared", secret: "second" }];
+ const exits = await placeSimultaneously(directory, entries);
+ for (const [index, exit] of exits.entries()) {
+ assert.equal(exit.code, 0, `writer ${index} failed: ${exit.stderr}`);
+ }
+ // Which replacement lands last is the filesystem's business; that the value is one writer's
+ // whole value, and never a mixture of the two, is this Store's.
+ const resolved = await new FileCredentialStore(directory).resolve(credentialRef("file", "shared"));
+ assert.ok(["first", "second"].includes(resolved?.secret ?? ""), `expected one complete value, got ${JSON.stringify(resolved)}`);
+ assert.equal((await readdir(directory)).length, 1);
+ } finally { await rm(root, { recursive: true, force: true }); }
+});
diff --git a/packages/credential-store-file/test/store-writer-fixture.ts b/packages/credential-store-file/test/store-writer-fixture.ts
new file mode 100644
index 00000000..ee021996
--- /dev/null
+++ b/packages/credential-store-file/test/store-writer-fixture.ts
@@ -0,0 +1,18 @@
+import { credentialRef } from "@hypit/runtime";
+import { FileCredentialStore } from "../src/store.js";
+
+const [directory, key, secret, startAt] = process.argv.slice(2);
+
+// The parent releases every writer at the same wall-clock instant, because the failure this guards
+// against is a shared document read, modified and replaced: it survives sequential writes and a
+// spread-out schedule, and only loses a key when two replacements overlap.
+async function place(): Promise<void> {
+ const store = new FileCredentialStore(directory!);
+ while (Date.now() < Number(startAt)) await new Promise((done) => setTimeout(done, 0));
+ await store.put(credentialRef("file", key!), { secret: secret! });
+}
+
+place().catch((error: unknown) => {
+ process.stderr.write(`${String(error)}\n`);
+ process.exit(1);
+});
diff --git a/packages/credential-store-os/src/store.ts b/packages/credential-store-os/src/store.ts
index 82284b99..805eea41 100644
--- a/packages/credential-store-os/src/store.ts
+++ b/packages/credential-store-os/src/store.ts
@@ -141,7 +141,11 @@ function platformBackend(service: string) {
return { read: macosReader(service), write: macosWriter(service), remove: macosDeleter(service) };
}
if (process.platform === "win32") return windowsBackend();
- throw new Error("OS CredentialStore supports macOS and Windows only");
+ // A Profile that selects this Store cannot be repaired by anything the user does here, and the
+ // other two Stores are the answer, so name them where the failure is read.
+ throw new Error("OS CredentialStore supports macOS and Windows only; select "
+ + "@hypit/credential-store-file (owner-private local file) or @hypit/credential-store-env "
+ + "(externally supplied value) in this Profile's credentials instead");
}
/** One logical writable store backed by the current user's OS credential locker. */