@nolto/cli 0.22.1 → 0.22.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/dist/index.js +39 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import path2 from "path";
7
7
 
8
8
  // src/errors.ts
9
9
  var CliError = class extends Error {
10
- constructor(message, exitCode, hint, status, serverError, body, detail) {
10
+ constructor(message, exitCode, hint, status, serverError, body, detail, retryAfter) {
11
11
  super(message);
12
12
  this.exitCode = exitCode;
13
13
  this.hint = hint;
@@ -15,6 +15,7 @@ var CliError = class extends Error {
15
15
  this.serverError = serverError;
16
16
  this.body = body;
17
17
  this.detail = detail;
18
+ this.retryAfter = retryAfter;
18
19
  this.name = "CliError";
19
20
  }
20
21
  exitCode;
@@ -23,6 +24,7 @@ var CliError = class extends Error {
23
24
  serverError;
24
25
  body;
25
26
  detail;
27
+ retryAfter;
26
28
  };
27
29
  function mapHttpStatusToCliError(status, opts = {}) {
28
30
  if (opts.serverError?.startsWith("memory_") || status === 400 && opts.serverError === "invalid_input") {
@@ -149,7 +151,7 @@ function mapHttpStatusToCliError(status, opts = {}) {
149
151
  return new CliError(`${opts.serverMessage ?? "Rate limit exceeded."}${waitMsg}`, 4, void 0, status, opts.serverError, opts.body);
150
152
  }
151
153
  const reason = opts.serverMessage ?? opts.serverError;
152
- return new CliError(reason ? `Server returned HTTP ${status}: ${reason}` : `Server returned HTTP ${status}`, 5, void 0, status, opts.serverError, opts.body);
154
+ return new CliError(reason ? `Server returned HTTP ${status}: ${reason}` : `Server returned HTTP ${status}`, 5, void 0, status, opts.serverError, opts.body, void 0, opts.retryAfter);
153
155
  }
154
156
  var NETWORK_ERROR_CODES = [
155
157
  "ECONNREFUSED",
@@ -1366,6 +1368,33 @@ import path6 from "path";
1366
1368
  // src/sync-core.ts
1367
1369
  import { createHash as createHash2 } from "crypto";
1368
1370
  import path5 from "path";
1371
+
1372
+ // src/busy-retry.ts
1373
+ var MAX_RETRIES = 3;
1374
+ function busyWaitSeconds(retryAfter) {
1375
+ const seconds = Number(retryAfter.trim());
1376
+ if (retryAfter.trim() === "" || !Number.isFinite(seconds)) return void 0;
1377
+ return Math.min(30, Math.max(1, Math.ceil(seconds)));
1378
+ }
1379
+ async function retryWhenBusy(send, hooks = {}) {
1380
+ const sleep2 = hooks.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
1381
+ const random = hooks.random ?? Math.random;
1382
+ const notify = hooks.notify ?? ((line2) => {
1383
+ process.stderr.write(line2 + "\n");
1384
+ });
1385
+ for (let attempt = 1; ; attempt++) {
1386
+ try {
1387
+ return await send();
1388
+ } catch (err) {
1389
+ const wait = err instanceof CliError && err.status === 503 && err.retryAfter !== void 0 ? busyWaitSeconds(err.retryAfter) : void 0;
1390
+ if (wait === void 0 || attempt > MAX_RETRIES) throw err;
1391
+ notify(`Server busy; retrying in ${wait} s (attempt ${attempt}/${MAX_RETRIES})`);
1392
+ await sleep2(wait * 1e3 + Math.floor(random() * 1e3));
1393
+ }
1394
+ }
1395
+ }
1396
+
1397
+ // src/sync-core.ts
1369
1398
  function sha256Hex(content) {
1370
1399
  return "sha256:" + createHash2("sha256").update(content, "utf8").digest("hex");
1371
1400
  }
@@ -1469,10 +1498,9 @@ async function runSync(args, deps) {
1469
1498
  repoIdentity: args.repoIdentity,
1470
1499
  deps
1471
1500
  });
1472
- const response = await deps.http.put(
1473
- `/api/projects/${args.projectId}/roadmaps/${encodeURIComponent(args.slug)}`,
1474
- { ...body, baseRevision: args.baseRevision }
1475
- );
1501
+ const syncPath = `/api/projects/${args.projectId}/roadmaps/${encodeURIComponent(args.slug)}`;
1502
+ const syncBody = { ...body, baseRevision: args.baseRevision };
1503
+ const response = await retryWhenBusy(() => deps.http.put(syncPath, syncBody), deps.retry);
1476
1504
  for (const warning of response.warnings ?? []) deps.warn(warning);
1477
1505
  const stats = deriveTaskStats(response.roadmap ?? args.roadmap);
1478
1506
  deps.log(
@@ -2451,7 +2479,7 @@ async function syncRepo(args, io) {
2451
2479
  try {
2452
2480
  const response = await runSync(
2453
2481
  { repoRoot: args.root, projectId, slug, roadmap, repoIdentity, baseRevision: meta?.revision ?? 0 },
2454
- { http: io.http, readFile: io.readFile, fileExists: io.fileExists, realpath: io.realpath, ...isIgnored ? { isIgnored } : {}, log: io.log, warn: io.warn }
2482
+ { http: io.http, readFile: io.readFile, fileExists: io.fileExists, realpath: io.realpath, ...isIgnored ? { isIgnored } : {}, log: io.log, warn: io.warn, ...io.retry ? { retry: io.retry } : {} }
2455
2483
  );
2456
2484
  await writeCache(io.env, projectId, slug, response.roadmap ?? roadmap, {
2457
2485
  repoIdentity: identity,
@@ -2488,10 +2516,11 @@ async function syncRepo(args, io) {
2488
2516
  if (body && body.links.length > 0) {
2489
2517
  gitLinks = { linked: 0, existing: 0, skipped: 0 };
2490
2518
  for (let offset = 0; offset < body.links.length; offset += 500) {
2491
- const response = await io.http.post(
2519
+ const chunk = { links: body.links.slice(offset, offset + 500), repoIdentity };
2520
+ const response = await retryWhenBusy(() => io.http.post(
2492
2521
  `/api/projects/${projectId}/git-links`,
2493
- { links: body.links.slice(offset, offset + 500), repoIdentity }
2494
- );
2522
+ chunk
2523
+ ), io.retry);
2495
2524
  if (!Number.isSafeInteger(response.linked) || response.linked < 0 || !Number.isSafeInteger(response.existing) || response.existing < 0 || !Number.isSafeInteger(response.skipped) || response.skipped < 0 || !Array.isArray(response.warnings) || response.warnings.some((warning) => typeof warning !== "string")) throw new Error("Invalid git links response");
2496
2525
  gitLinks = { linked: gitLinks.linked + response.linked, existing: gitLinks.existing + response.existing, skipped: gitLinks.skipped + response.skipped };
2497
2526
  for (const warning of response.warnings) io.warn(`git links: ${warning}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolto/cli",
3
- "version": "0.22.1",
3
+ "version": "0.22.2",
4
4
  "description": "CLI for syncing repository roadmaps with Nolto.",
5
5
  "license": "MIT",
6
6
  "type": "module",