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.

fix(credentials): order each credential document's reads and replacements

rpone8a76cd76261e
diff --git a/packages/credential-store-file/src/store.ts b/packages/credential-store-file/src/store.ts
index 228d9239..2beb790f 100644
--- a/packages/credential-store-file/src/store.ts
+++ b/packages/credential-store-file/src/store.ts
@@ -8,6 +8,26 @@ function missing(error: unknown): boolean {
return error instanceof Error && "code" in error && error.code === "ENOENT";
}
+/**
+ * One process reads a credential for one Endpoint action while another action's refresh replaces
+ * it, so a read handle and a replacement can reach the same document at the same instant. Windows
+ * refuses to replace a file any handle still holds. Each document's work takes its turn instead,
+ * keyed by path so separate Store instances over one directory share the same order. Keys are
+ * independent: a turn on one document never delays another.
+ */
+const documentTurns = new Map<string, Promise<unknown>>();
+
+function onDocument<T>(path: string, work: () => Promise<T>): Promise<T> {
+ const turn = (documentTurns.get(path) ?? Promise.resolve()).then(work, work);
+ // The successor waits for this turn to settle, not to succeed, and the last turn clears the path.
+ const settled = turn.then(() => undefined, () => undefined);
+ documentTurns.set(path, settled);
+ void settled.then(() => {
+ if (documentTurns.get(path) === settled) documentTurns.delete(path);
+ });
+ return turn;
+}
+
function credentialValue(value: unknown): CredentialValue {
if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error("invalid credential value");
const record = value as Record<string, unknown>;
@@ -44,6 +64,10 @@ export class FileCredentialStore implements WritableCredentialStore {
async resolve(ref: CredentialRef): Promise<CredentialValue | undefined> {
if (!this.owns(ref)) return undefined;
const path = this.#path(ref);
+ return await onDocument(path, async () => await this.#read(path));
+ }
+
+ async #read(path: string): Promise<CredentialValue | undefined> {
try { await this.#privateDirectory(false); } catch (error) {
if (missing(error)) return undefined;
throw error;
@@ -69,21 +93,25 @@ export class FileCredentialStore implements WritableCredentialStore {
async put(ref: CredentialRef, value: CredentialValue): Promise<void> {
const path = this.#path(ref);
const contents = JSON.stringify(credentialValue(value));
- await this.#privateDirectory(true);
- const temporary = join(this.directory, `.write-${randomUUID()}.tmp`);
- const file = await open(temporary, "wx", 0o600);
- try {
- try { await file.writeFile(`${contents}\n`, "utf8"); } finally { await file.close(); }
- await rename(temporary, path);
- } finally { await rm(temporary, { force: true }); }
+ await onDocument(path, async () => {
+ await this.#privateDirectory(true);
+ const temporary = join(this.directory, `.write-${randomUUID()}.tmp`);
+ const file = await open(temporary, "wx", 0o600);
+ try {
+ try { await file.writeFile(`${contents}\n`, "utf8"); } finally { await file.close(); }
+ await rename(temporary, path);
+ } finally { await rm(temporary, { force: true }); }
+ });
}
async delete(ref: CredentialRef): Promise<boolean> {
const path = this.#path(ref);
// Deletion never opens or decodes the old document.
- try { await unlink(path); return true; } catch (error) {
- if (missing(error)) return false;
- throw error;
- }
+ return await onDocument(path, async () => {
+ try { await unlink(path); return true; } catch (error) {
+ if (missing(error)) return false;
+ throw error;
+ }
+ });
}
}
diff --git a/packages/credential-store-file/test/concurrency.test.ts b/packages/credential-store-file/test/concurrency.test.ts
index 20766b80..de8bd58a 100644
--- a/packages/credential-store-file/test/concurrency.test.ts
+++ b/packages/credential-store-file/test/concurrency.test.ts
@@ -60,8 +60,13 @@ test("two processes placing the same key leave one complete value", { timeout: 1
const directory = join(root, "private");
const entries = [{ key: "shared", secret: "first" }, { key: "shared", secret: "second" }];
const exits = await placeSimultaneously(directory, entries);
+ // Separate processes share no order, and Windows refuses to replace a document while another
+ // replacement of it is in flight. A writer that loses that contest reports why, and one of them
+ // places the credential. Within one process this Store orders the work and both writers place.
+ assert.ok(exits.some((exit) => exit.code === 0),
+ `no writer placed the credential: ${exits.map((exit) => exit.stderr).join(" ")}`);
for (const [index, exit] of exits.entries()) {
- assert.equal(exit.code, 0, `writer ${index} failed: ${exit.stderr}`);
+ if (exit.code !== 0) assert.notEqual(exit.stderr.trim(), "", `writer ${index} failed without saying why`);
}
// 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.