run402 1.34.0 → 1.34.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/deploy.mjs +81 -1
  2. package/package.json +2 -1
package/lib/deploy.mjs CHANGED
@@ -1,8 +1,82 @@
1
1
  import { readFileSync } from "fs";
2
2
  import { dirname, resolve } from "path";
3
+ import { Agent, fetch as undiciFetch } from "undici";
3
4
  import { API, allowanceAuthHeaders, findProject } from "./config.mjs";
4
5
  import { resolveFilePathsInManifest, resolveMigrationsFile } from "./manifest.mjs";
5
6
 
7
+ // Custom undici dispatcher with longer timeouts for large-batch deploys.
8
+ // Default Node undici headersTimeout is ~5 min; large image uploads can exceed it.
9
+ // We MUST pair this Agent (from the installed undici major) with undici.fetch
10
+ // — not globalThis.fetch — because Node's built-in fetch ships its own bundled
11
+ // undici whose Dispatcher interface may differ by major version, which would
12
+ // cause UND_ERR_INVALID_ARG ("invalid onRequestStart method") at dispatch time.
13
+ const deployDispatcher = new Agent({
14
+ headersTimeout: 600_000, // 10 min
15
+ bodyTimeout: 600_000,
16
+ connectTimeout: 30_000,
17
+ });
18
+
19
+ // Retry policy for transient network errors and 5xx gateway errors.
20
+ // We retry on these because they tend to be load-shedding/blip-related; we do
21
+ // NOT retry on other 4xx/5xx (402, 400, etc.) — those are deterministic.
22
+ const RETRY_CAUSE_CODES = new Set([
23
+ "UND_ERR_HEADERS_TIMEOUT",
24
+ "UND_ERR_BODY_TIMEOUT",
25
+ "UND_ERR_SOCKET",
26
+ "ECONNRESET",
27
+ "ECONNREFUSED",
28
+ "ETIMEDOUT",
29
+ ]);
30
+ const RETRY_HTTP_STATUSES = new Set([502, 503, 504]);
31
+
32
+ // Test-only injection seam. Tests can replace the fetch implementation used by
33
+ // run() without monkey-patching globalThis.fetch (which would not intercept
34
+ // undici.fetch anyway). Pass null/undefined to reset.
35
+ let _runFetchImpl = undiciFetch;
36
+ export function _setFetchImpl(fn) { _runFetchImpl = fn ?? undiciFetch; }
37
+
38
+ function isRetriableError(err) {
39
+ if (!err) return false;
40
+ const code = err.code || (err.cause && err.cause.code);
41
+ return typeof code === "string" && RETRY_CAUSE_CODES.has(code);
42
+ }
43
+
44
+ function sleep(ms) {
45
+ return new Promise((r) => setTimeout(r, ms));
46
+ }
47
+
48
+ /**
49
+ * Fetch with bounded retries on transient errors. Exported for testability.
50
+ * - 2 retries (3 total attempts)
51
+ * - Backoff ~1s then ~4s with small jitter
52
+ * - Retries on RETRY_CAUSE_CODES network errors and RETRY_HTTP_STATUSES
53
+ * - Silent: no stdout noise on retry (CLI is agent-first)
54
+ */
55
+ export async function fetchWithRetry(url, init, { attempts = 3, fetchImpl = undiciFetch } = {}) {
56
+ for (let attempt = 1; attempt <= attempts; attempt++) {
57
+ try {
58
+ const res = await fetchImpl(url, init);
59
+ if (attempt < attempts && RETRY_HTTP_STATUSES.has(res.status)) {
60
+ // Drain body so the connection can be reused, then retry.
61
+ try { await res.arrayBuffer(); } catch { /* noop */ }
62
+ const delay = (attempt === 1 ? 1000 : 4000) + Math.floor(Math.random() * 250);
63
+ await sleep(delay);
64
+ continue;
65
+ }
66
+ return res;
67
+ } catch (err) {
68
+ if (attempt < attempts && isRetriableError(err)) {
69
+ const delay = (attempt === 1 ? 1000 : 4000) + Math.floor(Math.random() * 250);
70
+ await sleep(delay);
71
+ continue;
72
+ }
73
+ throw err;
74
+ }
75
+ }
76
+ // Unreachable: the loop above either returns a response or throws.
77
+ throw new Error("fetchWithRetry: exhausted attempts without returning");
78
+ }
79
+
6
80
  const HELP = `run402 deploy — Deploy to an existing project on Run402
7
81
 
8
82
  Usage:
@@ -113,7 +187,13 @@ export async function run(args) {
113
187
  delete manifest.name;
114
188
 
115
189
  const authHeaders = allowanceAuthHeaders("/deploy/v1");
116
- const res = await fetch(`${API}/deploy/v1`, { method: "POST", headers: { "Content-Type": "application/json", ...authHeaders }, body: JSON.stringify(manifest) });
190
+ const body = JSON.stringify(manifest);
191
+ const res = await fetchWithRetry(`${API}/deploy/v1`, {
192
+ method: "POST",
193
+ headers: { "Content-Type": "application/json", ...authHeaders },
194
+ body,
195
+ dispatcher: deployDispatcher,
196
+ }, { fetchImpl: _runFetchImpl });
117
197
 
118
198
  // Content-type aware parsing: gateways (ALB, CloudFront, etc.) return HTML on
119
199
  // 504/413/etc., which would otherwise crash res.json() with SyntaxError.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "run402",
3
- "version": "1.34.0",
3
+ "version": "1.34.2",
4
4
  "description": "CLI for Run402 — provision Postgres databases, deploy static sites, generate images, and manage wallets via x402 and MPP micropayments.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -20,6 +20,7 @@
20
20
  "@x402/evm": "^2.6.0",
21
21
  "@x402/fetch": "^2.6.0",
22
22
  "mppx": "^0.4.7",
23
+ "undici": "^8.0.2",
23
24
  "viem": "^2.47.1"
24
25
  },
25
26
  "engines": {