Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkgs/pkarr-worker/deploy-probe.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare function isNotFoundProbeFailure(output: string | null | undefined): boolean;

export declare function probeFailureOutput(err: unknown): string;

export declare function probeWorkerExists(runGet: () => unknown, worker: string): boolean;
59 changes: 59 additions & 0 deletions pkgs/pkarr-worker/deploy-probe.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Existence-probe classification for deploy.mjs, kept in its own module so
// the rules can be tested without importing the deploy script (which runs
// cf/bws at import time). The rule is fail-closed (#4865): the bootstrap
// `cf deploy` path deploys at 100% immediately, bypassing the versions
// flow, so it may be chosen ONLY when the Worker genuinely does not exist.
// Any other probe failure — an expired or under-scoped token, a
// Cloudflare API blip, a network error, a broken cf install — is a stop,
// never a silent change of deploy mechanism.

/**
* The not-found shapes of `cf workers get <name>` for a Worker that does
* not exist: the Cloudflare API error code 10007
* (workers.api.error.not_found) or cf's rendering of it. Deliberately
* narrow: a bare "not found" for something else (an account, a zone)
* does not match, so it aborts like every other unrecognized failure.
*/
const NOT_FOUND_PATTERNS = [
/10007/,
/workers\.api\.error\.not_found/,
/script not found/i,
/could not find (the )?worker/i,
/worker [^\n]* not found/i,
];

export function isNotFoundProbeFailure(output) {
if (!output) return false;
return NOT_FOUND_PATTERNS.some((pattern) => pattern.test(output));
}

/** Everything a failed execFileSync can tell us, in one string. */
export function probeFailureOutput(err) {
return [err?.stderr, err?.stdout, err?.message]
.filter((part) => typeof part === "string" && part.length > 0)
.join("\n");
}

/**
* Run the existence probe.
*
* @param {() => unknown} runGet performs `cf workers get <worker>` and
* throws on a non-zero exit or spawn failure
* @param {string} worker worker name, for the abort message
* @returns {boolean} true when the Worker exists; false only when the
* probe failed with the not-found response
* @throws {Error} on any other probe failure, with the cf error visible
*/
export function probeWorkerExists(runGet, worker) {
try {
runGet();
return true;
} catch (err) {
const output = probeFailureOutput(err);
if (isNotFoundProbeFailure(output)) return false;
throw new Error(
`could not determine whether worker ${worker} exists; aborting before any upload. ` +
`The bootstrap path is only for a genuinely new Worker. cf said:\n${output || String(err)}`,
);
}
}
27 changes: 16 additions & 11 deletions pkgs/pkarr-worker/deploy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";

import { parseArgs } from "./deploy-args.mjs";
import { probeWorkerExists } from "./deploy-probe.mjs";

const here = dirname(fileURLToPath(import.meta.url));
const WORKER = "pkarr";
Expand Down Expand Up @@ -132,18 +133,22 @@ if (bootstrapping) {
reportLive();
console.log(`live: ${WORKER_URL} at version ${version.id} (${tag})`);

// Only the genuine not-found response means "does not exist" (#4865):
// any other probe failure (auth, network, broken cf) aborts the deploy
// with the cf error visible, instead of silently rerouting a standard
// deploy into the bootstrap `cf deploy` path. Classification lives in
// deploy-probe.mjs so it is tested without running this script.
function workerExists() {
try {
execFileSync("cf", ["-q", "workers", "get", WORKER], {
cwd: here,
env,
encoding: "utf8",
stdio: ["inherit", "pipe", "pipe"],
});
return true;
} catch {
return false;
}
return probeWorkerExists(
() =>
execFileSync("cf", ["-q", "workers", "get", WORKER], {
cwd: here,
env,
encoding: "utf8",
stdio: ["inherit", "pipe", "pipe"],
}),
WORKER,
);
}

function createDeployment(versionId) {
Expand Down
46 changes: 46 additions & 0 deletions pkgs/pkarr-worker/test/deploy-probe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, expect, it } from "vitest";

import { isNotFoundProbeFailure, probeWorkerExists } from "../deploy-probe.mjs";

// Simulated execFileSync failures: with stdio piped, the thrown error
// carries the cf output on .stderr and a generic line on .message.
const cfFailure = (stderr: string) =>
Object.assign(new Error("Command failed: cf -q workers get pkarr"), { status: 1, stderr });

describe("worker existence probe (#4865)", () => {
it("reports exists when the probe succeeds", () => {
expect(probeWorkerExists(() => "{}", "pkarr")).toBe(true);
});

it("reports does-not-exist only for the not-found response (API code 10007)", () => {
const err = cfFailure('{"errors":[{"code":10007,"message":"workers.api.error.not_found"}]}');
expect(probeWorkerExists(() => { throw err; }, "pkarr")).toBe(false);
});

it("reports does-not-exist for cf's rendered not-found message", () => {
const err = cfFailure("error: script not found: pkarr");
expect(probeWorkerExists(() => { throw err; }, "pkarr")).toBe(false);
});

it("aborts on an authentication failure, with the cf error visible", () => {
const err = cfFailure('{"errors":[{"code":10000,"message":"Authentication error"}]}');
expect(() => probeWorkerExists(() => { throw err; }, "pkarr")).toThrow(/aborting before any upload/);
expect(() => probeWorkerExists(() => { throw err; }, "pkarr")).toThrow(/Authentication error/);
});

it("aborts on a network failure instead of bootstrapping", () => {
const err = cfFailure("error: request to https://api.cloudflare.com failed: connection reset");
expect(() => probeWorkerExists(() => { throw err; }, "pkarr")).toThrow(/aborting before any upload/);
});

it("aborts on a spawn failure (broken cf install)", () => {
const err = Object.assign(new Error("spawnSync cf ENOENT"), { code: "ENOENT" });
expect(() => probeWorkerExists(() => { throw err; }, "pkarr")).toThrow(/ENOENT/);
});

it("does not treat an unrelated not-found as the Worker missing", () => {
const err = cfFailure('{"errors":[{"code":10003,"message":"account not found"}]}');
expect(isNotFoundProbeFailure(err.stderr)).toBe(false);
expect(() => probeWorkerExists(() => { throw err; }, "pkarr")).toThrow(/aborting before any upload/);
});
});
Loading