|
| 1 | +/** |
| 2 | + * run402 cdn — CloudFront CDN diagnostics for public blob URLs. |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * run402 cdn wait-fresh <url> --sha <sha256> [--timeout <seconds>] [--project <id>] |
| 6 | + * |
| 7 | + * Wraps the SDK's `client.blobs.waitFresh(...)`. Polls the gateway diagnose |
| 8 | + * endpoint until the URL serves the expected SHA, then exits 0. On timeout, |
| 9 | + * exits 1 (agent shell loops can chain a fallback action). |
| 10 | + * |
| 11 | + * **Mutable URLs only.** For immutable URLs (the `immutableUrl` field |
| 12 | + * returned by `run402 blob put --immutable`), no waiting is needed — they |
| 13 | + * are bound to a SHA at upload time and never previously cached. |
| 14 | + */ |
| 15 | + |
| 16 | +import { resolveProjectId } from "./config.mjs"; |
| 17 | +import { getSdk } from "./sdk.mjs"; |
| 18 | +import { reportSdkError } from "./sdk-errors.mjs"; |
| 19 | + |
| 20 | +const HELP = `run402 cdn — CloudFront CDN diagnostics for public blob URLs |
| 21 | +
|
| 22 | +Usage: |
| 23 | + run402 cdn wait-fresh <url> --sha <sha256> [options] |
| 24 | +
|
| 25 | +Subcommands: |
| 26 | + wait-fresh Poll the CDN until a mutable URL serves the expected SHA-256 |
| 27 | +
|
| 28 | +Examples: |
| 29 | + run402 cdn wait-fresh https://app.run402.com/_blob/avatar.png --sha abc... |
| 30 | +
|
| 31 | +For details: run402 cdn wait-fresh --help |
| 32 | +`; |
| 33 | + |
| 34 | +const SUB_HELP = { |
| 35 | + "wait-fresh": `run402 cdn wait-fresh — Poll the CDN for an expected SHA on a mutable URL |
| 36 | +
|
| 37 | +Usage: |
| 38 | + run402 cdn wait-fresh <url> --sha <sha256> [options] |
| 39 | +
|
| 40 | +Arguments: |
| 41 | + <url> Full mutable blob URL to poll (e.g. |
| 42 | + https://app.run402.com/_blob/avatar.png) |
| 43 | +
|
| 44 | +Options: |
| 45 | + --sha <hex> Expected hex SHA-256. Required. |
| 46 | + --timeout <secs> Max wait in seconds. Default 60. |
| 47 | + --project <id> Project ID (defaults to active project) |
| 48 | +
|
| 49 | +Output: |
| 50 | + Prints a JSON result on stdout when polling ends: |
| 51 | + { "fresh": true|false, "observedSha256": "...", "attempts": N, |
| 52 | + "elapsedMs": ..., "vantage": "gateway-us-east-1" } |
| 53 | +
|
| 54 | +Exit codes: |
| 55 | + 0 the URL served the expected SHA before the timeout |
| 56 | + 1 the URL did NOT match within the timeout (or the project resolution failed) |
| 57 | +
|
| 58 | +Notes: |
| 59 | + - For IMMUTABLE URLs (returned as 'immutableUrl' from 'run402 blob put |
| 60 | + --immutable'), no waiting is needed. Use this command only for the |
| 61 | + mutable 'url' field, after a re-upload to an existing public key. |
| 62 | + - The probe is single-vantage (us-east-1). Other CloudFront PoPs may |
| 63 | + serve different cached states until invalidation propagates. |
| 64 | +
|
| 65 | +Examples: |
| 66 | + run402 cdn wait-fresh https://app.run402.com/_blob/avatar.png --sha ba78... |
| 67 | + run402 cdn wait-fresh https://app.run402.com/_blob/avatar.png --sha ba78... --timeout 120 |
| 68 | +`, |
| 69 | +}; |
| 70 | + |
| 71 | +function die(msg, code = 1) { |
| 72 | + console.error(JSON.stringify({ status: "error", message: msg })); |
| 73 | + process.exit(code); |
| 74 | +} |
| 75 | + |
| 76 | +function parseArgs(args) { |
| 77 | + const opts = { positional: [] }; |
| 78 | + for (let i = 0; i < args.length; i++) { |
| 79 | + const a = args[i]; |
| 80 | + if (a === "--sha") opts.sha = args[++i]; |
| 81 | + else if (a === "--timeout") opts.timeout = Number(args[++i]); |
| 82 | + else if (a === "--project") opts.project = args[++i]; |
| 83 | + else if (a.startsWith("--")) die(`Unknown option: ${a}`); |
| 84 | + else opts.positional.push(a); |
| 85 | + } |
| 86 | + return opts; |
| 87 | +} |
| 88 | + |
| 89 | +async function waitFresh(projectId, argv) { |
| 90 | + const opts = parseArgs(argv); |
| 91 | + opts.project = opts.project || projectId; |
| 92 | + const resolvedId = resolveProjectId(opts.project); |
| 93 | + if (opts.positional.length === 0) die("URL required"); |
| 94 | + const url = opts.positional[0]; |
| 95 | + if (!opts.sha) die("--sha is required"); |
| 96 | + |
| 97 | + const timeoutMs = (opts.timeout ?? 60) * 1000; |
| 98 | + try { |
| 99 | + const result = await getSdk().blobs.waitFresh(resolvedId, { |
| 100 | + url, |
| 101 | + sha256: opts.sha.toLowerCase(), |
| 102 | + timeoutMs, |
| 103 | + }); |
| 104 | + console.log(JSON.stringify(result, null, 2)); |
| 105 | + process.exit(result.fresh ? 0 : 1); |
| 106 | + } catch (err) { |
| 107 | + reportSdkError(err); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +export async function run(sub, args) { |
| 112 | + if (!sub || sub === "--help" || sub === "-h") { |
| 113 | + console.log(HELP); |
| 114 | + process.exit(0); |
| 115 | + } |
| 116 | + if (Array.isArray(args) && (args.includes("--help") || args.includes("-h"))) { |
| 117 | + console.log(SUB_HELP[sub] || HELP); |
| 118 | + process.exit(0); |
| 119 | + } |
| 120 | + const defaultProject = process.env.RUN402_PROJECT ?? null; |
| 121 | + switch (sub) { |
| 122 | + case "wait-fresh": |
| 123 | + await waitFresh(defaultProject, args); |
| 124 | + break; |
| 125 | + default: |
| 126 | + console.error(`Unknown subcommand: ${sub}`); |
| 127 | + console.log(HELP); |
| 128 | + process.exit(1); |
| 129 | + } |
| 130 | +} |
0 commit comments