@fluid-app/fluid-cli-theme-dev 0.1.310 → 0.1.312

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.
package/dist/index.mjs CHANGED
@@ -2678,6 +2678,66 @@ function assertMemberConfigurationRetained(deletedPaths) {
2678
2678
  if (deletedPaths.includes("members_sites.json")) throw new Error("Keep members_sites.json. To remove member configuration, upload an explicit empty versioned document instead of deleting or renaming the file.");
2679
2679
  }
2680
2680
  //#endregion
2681
+ //#region src/theme/binary-download.ts
2682
+ /**
2683
+ * Download one binary theme asset, surviving slow and flaky connections.
2684
+ *
2685
+ * A bare `fetch(url)` has no deadline: on a dropped Wi-Fi connection the
2686
+ * pull hangs forever on whichever asset was in flight. Here a download is
2687
+ * aborted after `idleTimeoutMs` without receiving any bytes (an idle
2688
+ * deadline rather than a total one, so a large video on a slow but working
2689
+ * link still completes), and a network failure or stall is retried
2690
+ * `retries` times. HTTP error statuses are answers, not transport failures,
2691
+ * and are not retried.
2692
+ */
2693
+ const BINARY_DOWNLOAD_IDLE_TIMEOUT_MS = 6e4;
2694
+ var AssetHttpError = class extends Error {
2695
+ constructor(status) {
2696
+ super(`Failed to download asset: ${status}`);
2697
+ this.status = status;
2698
+ this.name = "AssetHttpError";
2699
+ }
2700
+ };
2701
+ async function downloadOnce(url, fetchImpl, idleTimeoutMs) {
2702
+ const controller = new AbortController();
2703
+ let timer;
2704
+ const armIdleTimer = () => {
2705
+ if (timer) clearTimeout(timer);
2706
+ timer = setTimeout(() => {
2707
+ controller.abort(/* @__PURE__ */ new Error(`no data received for ${idleTimeoutMs / 1e3}s`));
2708
+ }, idleTimeoutMs);
2709
+ };
2710
+ armIdleTimer();
2711
+ try {
2712
+ const resp = await fetchImpl(url, { signal: controller.signal });
2713
+ if (!resp.ok) throw new AssetHttpError(resp.status);
2714
+ if (!resp.body) return Buffer.from(await resp.arrayBuffer());
2715
+ const chunks = [];
2716
+ const reader = resp.body.getReader();
2717
+ for (;;) {
2718
+ armIdleTimer();
2719
+ const { done, value } = await reader.read();
2720
+ if (done) break;
2721
+ chunks.push(Buffer.from(value));
2722
+ }
2723
+ return Buffer.concat(chunks);
2724
+ } finally {
2725
+ if (timer) clearTimeout(timer);
2726
+ }
2727
+ }
2728
+ async function downloadBinaryWithRetry(url, { fetchImpl = fetch, idleTimeoutMs = BINARY_DOWNLOAD_IDLE_TIMEOUT_MS, retries = 2, retryDelayMs = 1e3 } = {}) {
2729
+ let lastError;
2730
+ for (let attempt = 0; attempt <= retries; attempt++) try {
2731
+ return await downloadOnce(url, fetchImpl, idleTimeoutMs);
2732
+ } catch (error) {
2733
+ if (error instanceof AssetHttpError) throw error;
2734
+ lastError = error;
2735
+ if (attempt < retries && retryDelayMs > 0) await new Promise((resolve) => setTimeout(resolve, retryDelayMs * (attempt + 1)));
2736
+ }
2737
+ const reason = lastError instanceof Error ? lastError.message : String(lastError);
2738
+ throw new Error(`Failed to download asset after ${retries + 1} attempts: ${reason}`, { cause: lastError });
2739
+ }
2740
+ //#endregion
2681
2741
  //#region src/theme/syncer.ts
2682
2742
  const ASSET_REFERENCE_CONCURRENCY = 6;
2683
2743
  /**
@@ -2696,6 +2756,39 @@ var PushConflictError = class extends Error {
2696
2756
  this.name = "PushConflictError";
2697
2757
  }
2698
2758
  };
2759
+ /**
2760
+ * Printed when Fluid could not be reached to finalize a push. Mist's publish
2761
+ * classifier matches the first sentence (`SYNC_NOT_FINALIZED_PATTERN` in
2762
+ * apps/mist-desktop/src/shared/publish-failure-signatures.ts); change both
2763
+ * together.
2764
+ */
2765
+ const SYNC_NOT_FINALIZED_MESSAGE = "Push not finalized: Fluid didn't record this push as a version. Your files are on Fluid, and a background job will commit them within a few minutes; run the push again to record it now.";
2766
+ /** Waits before each retry of a sync request that failed in transit. */
2767
+ const SYNC_REQUEST_RETRY_DELAYS_MS = [
2768
+ 1e3,
2769
+ 3e3,
2770
+ 9e3
2771
+ ];
2772
+ function defaultSleep(ms) {
2773
+ return new Promise((resolve) => setTimeout(resolve, ms));
2774
+ }
2775
+ function statusOf(error) {
2776
+ const candidate = error;
2777
+ return candidate?.status ?? candidate?.response?.status;
2778
+ }
2779
+ function isRetryableSyncError(error) {
2780
+ const status = statusOf(error);
2781
+ return !status || status >= 500 || status === 408 || status === 429;
2782
+ }
2783
+ function syncFailureFor(error, retryable) {
2784
+ return retryable ? {
2785
+ retryable,
2786
+ message: `${SYNC_NOT_FINALIZED_MESSAGE} (${formatError(error)})`
2787
+ } : {
2788
+ retryable,
2789
+ message: `Fluid refused to finalize this push: ${formatError(error)}`
2790
+ };
2791
+ }
2699
2792
  var Syncer = class {
2700
2793
  checksumIndex = /* @__PURE__ */ new Map();
2701
2794
  memberAcknowledgments = /* @__PURE__ */ new Map();
@@ -2705,6 +2798,7 @@ var Syncer = class {
2705
2798
  remoteIndexesDirty = false;
2706
2799
  remoteResourcesLoaded = false;
2707
2800
  lastKnownRemoteSha = null;
2801
+ lastSyncFailure = null;
2708
2802
  assetManifestInstance;
2709
2803
  constructor(api, themeId, themeRoot, assetManifest) {
2710
2804
  this.api = api;
@@ -3156,17 +3250,35 @@ var Syncer = class {
3156
3250
  * boundaries needs to know. A failed ask leaves the previous edit
3157
3251
  * uncommitted, and uploading the next one over it merges the two into
3158
3252
  * whichever commit eventually lands.
3253
+ *
3254
+ * A network failure or 5xx is retried after each of `delaysMs`: the files
3255
+ * are already on Fluid, so the only thing at stake is whether this push
3256
+ * becomes its own version now (408 and 429 count as in transit). A 4xx is
3257
+ * otherwise Fluid's answer (a 422 is an
3258
+ * incomplete member configuration) and is not retried. `syncFailure()`
3259
+ * then says which of the two happened.
3159
3260
  */
3160
- async requestSync() {
3161
- try {
3261
+ async requestSync({ delaysMs = SYNC_REQUEST_RETRY_DELAYS_MS, sleep = defaultSleep, warn = true } = {}) {
3262
+ this.lastSyncFailure = null;
3263
+ for (let attempt = 0;; attempt++) try {
3162
3264
  const response = await this.api.post(`/api/application_themes/${this.themeId}/resources/sync`, {});
3163
3265
  if (response.content_version_sha) this.lastKnownRemoteSha = response.content_version_sha;
3164
3266
  return true;
3165
3267
  } catch (e) {
3166
- console.warn(` ⚠ couldn't ask Fluid to commit this push (${formatError(e)}). Retry the push to finalize it.`);
3268
+ const retryable = isRetryableSyncError(e);
3269
+ if (retryable && attempt < delaysMs.length) {
3270
+ await sleep(delaysMs[attempt]);
3271
+ continue;
3272
+ }
3273
+ this.lastSyncFailure = syncFailureFor(e, retryable);
3274
+ if (warn) console.warn(` ⚠ ${this.lastSyncFailure.message}`);
3167
3275
  return false;
3168
3276
  }
3169
3277
  }
3278
+ /** Why the last `requestSync` failed, or null when it succeeded. */
3279
+ syncFailure() {
3280
+ return this.lastSyncFailure;
3281
+ }
3170
3282
  async preflightPush(baseSha) {
3171
3283
  if (!baseSha) return;
3172
3284
  try {
@@ -3291,9 +3403,7 @@ var Syncer = class {
3291
3403
  return resources;
3292
3404
  }
3293
3405
  async downloadBinaryAsset(url) {
3294
- const resp = await fetch(url);
3295
- if (!resp.ok) throw new Error(`Failed to download asset: ${resp.status}`);
3296
- return Buffer.from(await resp.arrayBuffer());
3406
+ return downloadBinaryWithRetry(url);
3297
3407
  }
3298
3408
  /**
3299
3409
  * Move directly-addressable binary `assets/*` resources into the local
@@ -4946,7 +5056,7 @@ async function pushToDevelopmentTheme(api, themeId, themeRoot, opts) {
4946
5056
  }
4947
5057
  const errors = [...result.errors];
4948
5058
  if (result.validationFailed || errors.length > 0) removeDevRemoteBaseline(themeRoot.root);
4949
- else if (!await syncer.requestSync()) errors.push("Fluid did not accept sync finalization. Retry the push to commit it.");
5059
+ else if (!await syncer.requestSync({ warn: false })) errors.push(syncer.syncFailure()?.message ?? "Push not finalized: Fluid didn't record this push as a version. Your files are on Fluid, and a background job will commit them within a few minutes; run the push again to record it now.");
4950
5060
  else {
4951
5061
  const state = syncer.devRemoteState(fingerprint());
4952
5062
  if (state) writeDevRemoteBaseline(themeRoot.root, state);
@@ -5497,7 +5607,7 @@ function createPushCommand() {
5497
5607
  errors.push(`Save asset provenance: ${formatError(error)}`);
5498
5608
  }
5499
5609
  if (errors.length === 0 && (uploaded > 0 || deletedCount > 0 || linked > 0)) {
5500
- if (!await syncer.requestSync()) errors.push("Fluid did not accept sync finalization. Retry the push before publishing.");
5610
+ if (!await syncer.requestSync({ warn: false })) errors.push(syncer.syncFailure()?.message ?? "Push not finalized: Fluid didn't record this push as a version. Your files are on Fluid, and a background job will commit them within a few minutes; run the push again to record it now.");
5501
5611
  }
5502
5612
  if (errors.length) {
5503
5613
  spinner.warn(`Pushed with ${errors.length} error(s).`);
@@ -5883,6 +5993,21 @@ function errMsg(e) {
5883
5993
  return e instanceof Error ? e.message : String(e);
5884
5994
  }
5885
5995
  //#endregion
5996
+ //#region src/theme/download-progress.ts
5997
+ const DOWNLOAD_PROGRESS_INTERVAL_MS = 2e3;
5998
+ function createPlainDownloadProgress({ isTTY, write, intervalMs = DOWNLOAD_PROGRESS_INTERVAL_MS, now = Date.now }) {
5999
+ if (isTTY) return () => {};
6000
+ let lastPrinted = null;
6001
+ return (done, total) => {
6002
+ const at = now();
6003
+ const finished = done >= total;
6004
+ if (!finished && lastPrinted !== null && at - lastPrinted < intervalMs) return;
6005
+ if (finished && lastPrinted === null && total === 0) return;
6006
+ lastPrinted = at;
6007
+ write(`Downloading ${done}/${total} files…\n`);
6008
+ };
6009
+ }
6010
+ //#endregion
5886
6011
  //#region src/commands/pull.ts
5887
6012
  async function fetchCompanySubdomain(api) {
5888
6013
  const subdomain = (await api.get("/api/company/v1/companies/me")).data?.company?.subdomain;
@@ -5968,6 +6093,10 @@ function createPullCommand() {
5968
6093
  delete: !opts.nodelete,
5969
6094
  backgroundGuard
5970
6095
  });
6096
+ const plainProgress = createPlainDownloadProgress({
6097
+ isTTY: process.stdout.isTTY === true,
6098
+ write: (line) => process.stdout.write(line)
6099
+ });
5971
6100
  const result = await mergePull({
5972
6101
  themeRoot,
5973
6102
  shadow,
@@ -5981,6 +6110,7 @@ function createPullCommand() {
5981
6110
  actor: await actorPromise,
5982
6111
  onProgress: (done, total) => {
5983
6112
  if (spinner) spinner.text = `Downloading ${done}/${total} files…`;
6113
+ plainProgress(done, total);
5984
6114
  }
5985
6115
  });
5986
6116
  result.errors.push(...externalizedAssets.errors);