@hyperframes/share 0.9.31 → 0.9.33

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 (61) hide show
  1. package/README.md +64 -3
  2. package/dist/index.d.mts +2 -0
  3. package/dist/projectLock.d.mts +2 -2
  4. package/dist/projectSnapshot.d.mts +3 -1
  5. package/dist/reviewClient.d.mts +8 -0
  6. package/dist/reviewExport.d.mts +20 -10
  7. package/dist/reviewShare.d.mts +3 -2
  8. package/dist/reviewUpload.d.mts +2 -0
  9. package/dist/reviewVersionSentences.d.mts +2 -2
  10. package/dist/shareSources.d.mts +24 -0
  11. package/dist/sourceIgnore.d.mts +6 -0
  12. package/dist/sources/adapterTypes.d.ts +39 -0
  13. package/dist/sources/api.d.mts +29 -0
  14. package/dist/sources/capture.d.mts +24 -0
  15. package/dist/sources/contract.d.mts +2 -0
  16. package/dist/sources/errors.d.mts +24 -0
  17. package/dist/sources/limits.d.mts +12 -0
  18. package/dist/sources/manifest.d.mts +23 -0
  19. package/dist/sources/pathPolicy.d.mts +25 -0
  20. package/dist/sources/projectState.d.mts +9 -0
  21. package/dist/sources/store.d.mts +3 -0
  22. package/dist/sources/transfer.d.mts +45 -0
  23. package/dist/sources/types.d.ts +353 -0
  24. package/dist/sources/unicode.d.mts +15 -0
  25. package/dist/sources/upload.d.mts +9 -0
  26. package/dist/sources/uploadFiles.d.mts +31 -0
  27. package/dist/sources/wire.d.mts +21 -0
  28. package/dist/types/reviewWire.d.ts +2 -0
  29. package/package.json +1 -1
  30. package/scripts/copy-types.mjs +3 -0
  31. package/src/index.mjs +8 -0
  32. package/src/projectLock.mjs +126 -38
  33. package/src/projectSnapshot.mjs +22 -6
  34. package/src/reviewClient.mjs +8 -1
  35. package/src/reviewExport.mjs +178 -53
  36. package/src/reviewShare.mjs +11 -2
  37. package/src/reviewUpload.mjs +18 -16
  38. package/src/reviewVersionSentences.mjs +10 -8
  39. package/src/shareSources.mjs +236 -0
  40. package/src/sourceIgnore.mjs +92 -0
  41. package/src/sources/adapterTypes.d.ts +39 -0
  42. package/src/sources/api.mjs +114 -0
  43. package/src/sources/capture.mjs +264 -0
  44. package/src/sources/contract/LICENSE-Unicode.txt +39 -0
  45. package/src/sources/contract/path-policy.json +311 -0
  46. package/src/sources/contract/unicode-15.1.0.json +6179 -0
  47. package/src/sources/contract.mjs +13 -0
  48. package/src/sources/errors.mjs +77 -0
  49. package/src/sources/limits.mjs +48 -0
  50. package/src/sources/manifest.mjs +154 -0
  51. package/src/sources/pathPolicy.mjs +197 -0
  52. package/src/sources/projectState.mjs +59 -0
  53. package/src/sources/store.mjs +82 -0
  54. package/src/sources/transfer.mjs +196 -0
  55. package/src/sources/types.d.ts +353 -0
  56. package/src/sources/unicode.mjs +163 -0
  57. package/src/sources/upload.mjs +201 -0
  58. package/src/sources/uploadFiles.mjs +263 -0
  59. package/src/sources/wire.mjs +205 -0
  60. package/src/types/reviewWire.d.ts +2 -0
  61. package/LICENSE +0 -190
@@ -39,9 +39,10 @@ import { versionSentence } from "./reviewVersionSentences.mjs";
39
39
  * definitive result; `reviewSent`: cleanup cutoff, with attribution only when the accepted attempt is
40
40
  * known; `last`: the newest version.
41
41
  * @typedef {{ pending: { body: ExportRequest, videoId: string | null, projectId?: string | null,
42
- * checkingSince?: number, snapshotRefused?: boolean, unanswered?: boolean, reviewSent?: ReviewSent } | null,
43
- * last: { fingerprint: string, versionId: string, exportedAt: string, projectId: string, files?: boolean, checksum?: string }
44
- * | null }} VersionRecord */
42
+ * renderKey?: string, checkingSince?: number, snapshotRefused?: boolean, unanswered?: boolean, reviewSent?: ReviewSent } | null,
43
+ * last: { fingerprint: string, versionId: string, exportedAt: string, projectId: string, files?: boolean,
44
+ * snapshot?: ExportRequest["snapshot"], checksum?: string }
45
+ * | null, failure?: (Failure & { projectId: string }) | null }} VersionRecord */
45
46
 
46
47
  /** `onAdded`: a version went ready, with whether it carries files and whether it is the page's first from here.
47
48
  * @typedef {{ onProgress?: (progress: ReviewExportProgress | null) => void, onUploaded?: () => Promise<void>,
@@ -49,10 +50,11 @@ import { versionSentence } from "./reviewVersionSentences.mjs";
49
50
 
50
51
  /** `projectId` overrides the recorded review project; `open`: a held video is watched only while the project is open.
51
52
  * `filesForExport`: the synced snapshot a new version carries, null for
52
- * none.
53
+ * none. `allowPending` lets a host treat accepted processing or screening as a successful return.
53
54
  * @typedef {VersionHooks & { projectPath: string, title: string, client: ReviewClient, put?: typeof putFile,
54
55
  * sleep?: (ms: number) => Promise<void>, probe?: (file: string) => Promise<VideoFacts | null>, now?: () => number,
55
- * projectId?: string | null, filesForExport?: FilesForRender, fromReview?: FromReview | null }} VersionRequest */
56
+ * projectId?: string | null, filesForExport?: FilesForRender, fromReview?: FromReview | null,
57
+ * allowPending?: boolean, requireSource?: boolean, omitSource?: boolean }} VersionRequest */
56
58
 
57
59
  /** The snapshot of `rendered`, the project as the export was rendered; null when the folder is not that render.
58
60
  * @typedef {(projectPath: string, rendered: string | null) => Promise<import("./types/reviewWire.js").SnapshotRef | null>}
@@ -69,6 +71,8 @@ const SLOW_POLL_MS = 30_000;
69
71
  const FINAL = new Set(["export_blocked", "content_rejected", "feature_unavailable"]);
70
72
  /** Too big or too long: the same file is refused again, but a smaller or shorter export can go up. */
71
73
  const TOO_BIG = new Set(["export_too_large", "export_too_long"]);
74
+ const UNCONFIRMED_VERSION =
75
+ "Couldn't confirm whether the previous version was created. Check the review page. To add a new version, change the export settings and export again.";
72
76
  /** Longer than the backend's own limiter ever asks, and short enough that a laptop waking up checks again soon. */
73
77
  const MAX_WAIT_MS = 5 * 60_000;
74
78
  /** A run stops watching after this long; the video stays recorded, so the next press reads it back. */
@@ -177,18 +181,36 @@ function isLast(value) {
177
181
  isSavedId(value.versionId) &&
178
182
  isSavedId(value.exportedAt) &&
179
183
  isSavedId(value.projectId) &&
184
+ isLastMetadata(value)
185
+ );
186
+ }
187
+
188
+ /** @param {Record<string, unknown>} value */
189
+ function isLastMetadata(value) {
190
+ return (
180
191
  isOptionalBoolean(value.files) &&
192
+ isOptionalSnapshot(value.snapshot) &&
181
193
  (value.checksum === undefined || isSavedId(value.checksum))
182
194
  );
183
195
  }
184
196
 
185
197
  /** @param {Record<string, unknown>} saved
186
- * @returns {saved is {pending?: VersionRecord["pending"], last?: SavedLast | null}} */
198
+ * @returns {saved is {pending?: VersionRecord["pending"], last?: SavedLast | null, failure?: unknown}} */
187
199
  const isExportState = (saved) =>
188
200
  ("pending" in saved || "last" in saved) &&
189
201
  (saved.pending == null || isPending(saved.pending)) &&
190
202
  (saved.last == null || isLast(saved.last));
191
203
 
204
+ /** @param {unknown} value @returns {value is Failure & { projectId: string }} */
205
+ const isSavedFailure = (value) =>
206
+ isJsonObject(value) &&
207
+ isSavedId(value.projectId) &&
208
+ typeof value.exportKey === "string" &&
209
+ typeof value.sentence === "string" &&
210
+ typeof value.final === "boolean" &&
211
+ typeof value.exportHelps === "boolean" &&
212
+ isOptionalBoolean(value.held);
213
+
192
214
  /** @param {string} dir @returns {Promise<VersionRecord>} */
193
215
  async function readState(dir) {
194
216
  const file = statePath(dir);
@@ -198,6 +220,7 @@ async function readState(dir) {
198
220
  const { last, pending } = saved;
199
221
  return {
200
222
  pending: pending ?? null,
223
+ failure: isSavedFailure(saved.failure) ? saved.failure : null,
201
224
  last: typeof last?.fingerprint === "string" ? { ...last, fingerprint: last.fingerprint } : null,
202
225
  };
203
226
  }
@@ -210,13 +233,19 @@ async function writeState(dir, state) {
210
233
 
211
234
  /** @typedef {{ sentence: string, final: boolean, exportHelps: boolean, held?: boolean }} Said
212
235
  * @typedef {Said & { exportKey: string }} Failure */
213
- /** @param {unknown} error @returns {Said} */
214
- const said = (error) => ({
215
- sentence: versionSentence(error),
216
- final: error instanceof ReviewFailed && (FINAL.has(error.reason) || TOO_BIG.has(error.reason)),
217
- exportHelps: error instanceof ReviewFailed && TOO_BIG.has(error.reason),
218
- ...(error instanceof ReviewFailed && error.reason === "held_for_review" && { held: true }),
219
- });
236
+ /** @param {unknown} error @param {boolean} [refusedReplay] @returns {Said} */
237
+ function said(error, refusedReplay = false) {
238
+ const sentence = versionSentence(error, refusedReplay ? UNCONFIRMED_VERSION : undefined);
239
+ const exportHelps =
240
+ sentence === UNCONFIRMED_VERSION ||
241
+ (error instanceof ReviewFailed && TOO_BIG.has(error.reason));
242
+ return {
243
+ sentence,
244
+ final: exportHelps || (error instanceof ReviewFailed && FINAL.has(error.reason)),
245
+ exportHelps,
246
+ ...(error instanceof ReviewFailed && error.reason === "held_for_review" && { held: true }),
247
+ };
248
+ }
220
249
 
221
250
  /** A failure found here, before the server: fixing the export fixes it. @param {string} sentence @returns {Said} */
222
251
  const local = (sentence) => ({ sentence, final: false, exportHelps: false });
@@ -234,7 +263,7 @@ const failures = new Map();
234
263
  /** Which export a failure was about: the newest export file and the project as it was rendered. @param {string} dir */
235
264
  const exportKeyOf = (dir) => {
236
265
  const latest = latestExportFile(dir);
237
- return latest ? `${latest.path}\n${latest.fingerprint ?? ""}` : "";
266
+ return latest ? `${latest.id}\n${latest.path}\n${latest.fingerprint ?? ""}` : "";
238
267
  };
239
268
 
240
269
  /** Whether the newest export can go up now: there is one, it is MP4, and the project has not moved past it.
@@ -259,7 +288,8 @@ export async function reviewVersion(projectPath, server = {}) {
259
288
  const last = state.last?.projectId === projectId ? state.last : null;
260
289
  const onServer =
261
290
  server.latestReadyVersionId === undefined || server.latestReadyVersionId === last?.versionId;
262
- const failed = failures.get(key);
291
+ const savedFailure = state.failure?.projectId === projectId ? state.failure : null;
292
+ const failed = savedFailure ?? failures.get(key);
263
293
  return {
264
294
  last: last
265
295
  ? { exportedAt: last.exportedAt, current: last.fingerprint === now && onServer }
@@ -279,8 +309,8 @@ export async function reviewVersion(projectPath, server = {}) {
279
309
 
280
310
  /** The newest MP4 and the body that adds it; a stale, non-MP4, too big or too long export never sends anything.
281
311
  * @param {string} dir @param {string} title @param {(file: string) => Promise<VideoFacts | null>} probe
282
- * @param {FilesForRender} filesForExport */
283
- async function exportSource(dir, title, probe, filesForExport) {
312
+ * @param {FilesForRender} filesForExport @param {boolean} requireSource */
313
+ async function exportSource(dir, title, probe, filesForExport, requireSource) {
284
314
  const latest = latestExportFile(dir);
285
315
  if (!latest || sourceOf(dir, await projectFingerprint(dir)) !== "ready")
286
316
  return { failure: local("Export an MP4 of the project as it is now first.") };
@@ -307,18 +337,34 @@ async function exportSource(dir, title, probe, filesForExport) {
307
337
  content_type: "video/mp4",
308
338
  },
309
339
  };
310
- const snapshot = await filesForExport(dir, body.source_fingerprint).catch((error) => {
311
- // expected: the version still goes up, and Share's files line says it has no files and offers Sync now.
312
- console.error(
313
- "[review files] couldn't read the synced snapshot; the version goes up without files:",
314
- error,
315
- );
316
- return null;
317
- });
340
+ const snapshot = await sourceSnapshot(
341
+ dir,
342
+ body.source_fingerprint,
343
+ filesForExport,
344
+ requireSource,
345
+ );
346
+ if (missingRequiredSource(requireSource, snapshot))
347
+ return {
348
+ failure: local(
349
+ "Source changed after preparation. Retry sharing, or choose video-only explicitly.",
350
+ ),
351
+ };
318
352
  if (snapshot) body.snapshot = snapshot;
319
353
  return { file: latest.path, body };
320
354
  }
321
355
 
356
+ /** @param {boolean} required @param {import("./types/reviewWire.js").SnapshotRef | null} snapshot */
357
+ const missingRequiredSource = (required, snapshot) => required && !snapshot;
358
+
359
+ /** @param {string} dir @param {string | null} rendered @param {FilesForRender} filesForExport @param {boolean} requireSource */
360
+ async function sourceSnapshot(dir, rendered, filesForExport, requireSource) {
361
+ return filesForExport(dir, rendered).catch((error) => {
362
+ if (requireSource) throw error;
363
+ console.error("[review files] could not read source; adding video only:", error);
364
+ return null;
365
+ });
366
+ }
367
+
322
368
  /** @param {ExportRequest["snapshot"]} a @param {ExportRequest["snapshot"]} b */
323
369
  const sameSnapshot = (a, b) =>
324
370
  a?.space_id === b?.space_id &&
@@ -368,7 +414,7 @@ async function createdExport(r, id, pending, kept) {
368
414
  return await sendCreate(r, id, pending, kept);
369
415
  } catch (error) {
370
416
  const refused = error instanceof ReviewFailed && error.reason === "snapshot_mismatch";
371
- if (!refused || !pending.body.snapshot || pending.unanswered) throw error;
417
+ if (!refused || !pending.body.snapshot || pending.unanswered || r.requireSource) throw error;
372
418
  console.error("[review] snapshot refused; the version goes up without files:", error);
373
419
  const body = { ...pending.body, client_operation_id: `export-${randomUUID()}` };
374
420
  delete body.snapshot;
@@ -549,11 +595,8 @@ async function poll(r, id, current, until, every) {
549
595
  }
550
596
  }
551
597
 
552
- /** A refusal the same body would get again, or a spent upload, needs a new key next time.
553
- * A refusal of a replay cannot settle an earlier unanswered create.
554
- * @param {unknown} error @param {VersionRecord["pending"]} pending */
555
- const spent = (error, pending) =>
556
- (!pending?.unanswered || pending.videoId !== null) &&
598
+ /** @param {unknown} error */
599
+ const permanentRefusal = (error) =>
557
600
  error instanceof ReviewFailed &&
558
601
  error.status >= 400 &&
559
602
  error.status < 500 &&
@@ -564,17 +607,31 @@ const spent = (error, pending) =>
564
607
  * @param {string} id @param {ExportDTO} done @param {FromReview | null} fromReview @returns {Promise<Said | null>} */
565
608
  async function finish(dir, state, pending, id, done, fromReview) {
566
609
  const ready = done.status === "ready";
567
- const last = {
568
- fingerprint: pending.body.source_fingerprint ?? "",
569
- versionId: done.version_id,
570
- exportedAt: done.exported_at,
571
- projectId: id,
572
- files: Boolean(pending.body.snapshot),
573
- checksum: pending.body.local_upload.checksum_sha256,
574
- };
575
- await writeState(dir, { pending: null, last: ready ? last : state.last });
610
+ const last = readyRecord(pending, id, done);
611
+ const failure = ready ? null : said(new ReviewFailed(200, done.error?.reason ?? ""));
612
+ await writeState(dir, {
613
+ pending: null,
614
+ last: ready ? last : state.last,
615
+ ...(failure && {
616
+ failure: { ...failure, projectId: id, exportKey: await failedExportKey(dir, pending) },
617
+ }),
618
+ });
576
619
  if (ready) await sharedFromReview(fromReview, dir, pending.reviewSent?.since);
577
- return ready ? null : said(new ReviewFailed(200, done.error?.reason ?? ""));
620
+ return failure;
621
+ }
622
+
623
+ /** @param {string} dir @param {NonNullable<VersionRecord["pending"]>} pending */
624
+ async function failedExportKey(dir, pending) {
625
+ if (typeof pending.renderKey === "string") return pending.renderKey;
626
+ const latest = latestExportFile(dir);
627
+ if (!latest) return "";
628
+ const checksum =
629
+ latest.fingerprint === pending.body.source_fingerprint
630
+ ? await fileSha256Base64(latest.path).catch(() => null)
631
+ : null;
632
+ return checksum === pending.body.local_upload.checksum_sha256
633
+ ? exportKeyOf(dir)
634
+ : `pending:${pending.body.client_operation_id}`;
578
635
  }
579
636
 
580
637
  /** @param {Required<VersionRequest>} r @param {(progress: ReviewExportProgress) => void} report
@@ -582,17 +639,37 @@ async function finish(dir, state, pending, id, done, fromReview) {
582
639
  async function run(r, report) {
583
640
  report({ step: "preparing", sent: 0, total: 0 });
584
641
  const state = await readState(r.projectPath);
585
- const source = await exportSource(r.projectPath, r.title, r.probe, r.filesForExport);
642
+ const source = await exportSource(
643
+ r.projectPath,
644
+ r.title,
645
+ r.probe,
646
+ r.omitSource ? noSource : r.filesForExport,
647
+ r.requireSource,
648
+ );
586
649
  if ("failure" in source) return source.failure ?? null;
587
650
  const id = r.projectId ?? (await reviewProjectId(r.projectPath));
588
651
  if (!id) return local("Share the project first, then add a version.");
589
- if (await alreadyUploaded(r.client, id, state, source.body)) return null;
652
+ if (
653
+ sameSourceMode(r, state.last, source.body.snapshot) &&
654
+ (await alreadyUploaded(r.client, id, state, source.body))
655
+ )
656
+ return null;
590
657
  return sendVersion(r, report, source, id, state);
591
658
  }
659
+ const noSource = async () => null;
660
+
661
+ /** @param {Required<VersionRequest>} r @param {VersionRecord["last"]} last @param {ExportRequest["snapshot"]} snapshot */
662
+ function sameSourceMode(r, last, snapshot) {
663
+ if (r.requireSource) return sameSnapshot(last?.snapshot, snapshot);
664
+ if (r.omitSource) return last?.files !== true;
665
+ return true;
666
+ }
667
+
592
668
  /** @param {ReviewClient} client @param {string} id @param {VersionRecord} state @param {ExportRequest} body */
593
669
  async function alreadyUploaded(client, id, state, body) {
594
670
  const last = state.last;
595
- if (state.pending || !last || last.projectId !== id) return false;
671
+ if (state.pending || state.failure?.projectId === id || !last || last.projectId !== id)
672
+ return false;
596
673
  if (
597
674
  last.fingerprint !== body.source_fingerprint ||
598
675
  last.checksum !== body.local_upload.checksum_sha256
@@ -604,8 +681,10 @@ async function alreadyUploaded(client, id, state, body) {
604
681
  * @param {{file: string, body: ExportRequest}} source @param {string} id @param {VersionRecord} state */
605
682
  async function sendVersion(r, report, source, id, state) {
606
683
  const first = state.last?.projectId !== id;
607
- let pending = bodyToSend(state, source.body, id);
608
- await writeState(r.projectPath, (state = { ...state, pending }));
684
+ let pending = bindRenderKey(bodyToSend(state, source.body, id), r.projectPath);
685
+ const sourceConflict = pendingSourceConflict(r, pending, source.body);
686
+ if (sourceConflict) return local(sourceConflict);
687
+ await writeState(r.projectPath, (state = { ...state, pending, failure: null }));
609
688
  try {
610
689
  const answer = await createdExport(r, id, pending, (next) =>
611
690
  writeState(r.projectPath, (state = { ...state, pending: next })),
@@ -631,16 +710,40 @@ async function sendVersion(r, report, source, id, state) {
631
710
  const done = await watch(r, id, uploaded, size, report, clock);
632
711
  return finishRun(r, state, pending, id, done, first);
633
712
  } catch (error) {
634
- if (spent(error, state.pending)) await writeState(r.projectPath, { ...state, pending: null });
635
- console.error("[review] version failed:", error);
636
- return said(error);
713
+ return failedSend(r.projectPath, state, pending, id, error);
637
714
  }
638
715
  }
639
716
 
717
+ /** @param {string} dir @param {VersionRecord} state
718
+ * @param {NonNullable<VersionRecord["pending"]>} pending @param {string} id @param {unknown} error */
719
+ async function failedSend(dir, state, pending, id, error) {
720
+ const refused = permanentRefusal(error);
721
+ // An earlier create can still be in flight when the replay is refused.
722
+ const unansweredCreate = state.pending?.unanswered === true && state.pending.videoId === null;
723
+ const failure = said(error, refused && unansweredCreate);
724
+ await writeState(dir, {
725
+ ...state,
726
+ pending: refused && !unansweredCreate ? null : state.pending,
727
+ failure: { ...failure, projectId: id, exportKey: await failedExportKey(dir, pending) },
728
+ });
729
+ return failure;
730
+ }
731
+
732
+ /** @param {Required<VersionRequest>} r @param {NonNullable<VersionRecord["pending"]>} pending @param {ExportRequest} fresh */
733
+ function pendingSourceConflict(r, pending, fresh) {
734
+ if (r.requireSource && !pending.body.snapshot)
735
+ return "A pending video-only upload must finish before attaching source. Retry with --video-only, then share source again.";
736
+ if (r.omitSource && pending.body.snapshot)
737
+ return "A pending upload includes source. Check its status or retry with source before creating a video-only version.";
738
+ if (r.requireSource && !sameSnapshot(pending.body.snapshot, fresh.snapshot))
739
+ return "A pending upload includes a different source snapshot. Check its status before sharing again.";
740
+ return null;
741
+ }
742
+
640
743
  /** @param {Required<VersionRequest>} r @param {VersionRecord} state
641
744
  * @param {NonNullable<VersionRecord["pending"]>} pending @param {string} id @param {ExportDTO} done @param {boolean} first */
642
745
  async function finishRun(r, state, pending, id, done, first) {
643
- if (going(done)) return stillGoing(done);
746
+ if (going(done)) return r.allowPending ? null : stillGoing(done);
644
747
  const failed = await finish(r.projectPath, state, pending, id, done, r.fromReview);
645
748
  if (!failed) {
646
749
  const fromReview = pending.reviewSent?.value;
@@ -663,8 +766,11 @@ export async function addVersion(request) {
663
766
  now: () => Date.now(),
664
767
  onProgress: () => {},
665
768
  open: () => true,
666
- filesForExport: async () => null,
769
+ allowPending: false,
770
+ filesForExport: noSource,
667
771
  fromReview: null,
772
+ requireSource: false,
773
+ omitSource: false,
668
774
  onAdded: () => {},
669
775
  ...request,
670
776
  projectId: request.projectId ?? null,
@@ -685,7 +791,6 @@ export async function addVersion(request) {
685
791
  if (kept) failures.set(key, { ...failure, exportKey: exportKeyOf(r.projectPath) });
686
792
  return failure?.sentence ?? null;
687
793
  } catch (error) {
688
- console.error("[review] version failed:", error);
689
794
  const sentence =
690
795
  error instanceof SharingStateError ? error.message : "Couldn't add this version. Try again.";
691
796
  failures.set(key, {
@@ -755,7 +860,7 @@ async function settleSavedExport(dir, client, fromReview) {
755
860
  const id = await reviewProjectId(dir);
756
861
  if (!pending?.videoId || !id || pending.projectId !== id) return;
757
862
  const done = await client.readExport(id, pending.videoId);
758
- if (going(done)) return;
863
+ if (done.status === "upload_pending" || going(done)) return;
759
864
  const failure = await finish(dir, state, pending, id, done, fromReview);
760
865
  const key = normalize(dir);
761
866
  if (failure) failures.set(key, { ...failure, exportKey: exportKeyOf(dir) });
@@ -825,3 +930,23 @@ export async function pendingReviewExport(projectPath) {
825
930
  checksum: pending.body.local_upload.checksum_sha256,
826
931
  };
827
932
  }
933
+
934
+ /** @param {NonNullable<VersionRecord["pending"]>} pending @param {string} id @param {ExportDTO} done */
935
+ function readyRecord(pending, id, done) {
936
+ return {
937
+ fingerprint: pending.body.source_fingerprint ?? "",
938
+ versionId: done.version_id,
939
+ exportedAt: done.exported_at,
940
+ projectId: id,
941
+ files: Boolean(pending.body.snapshot),
942
+ ...(pending.body.snapshot && { snapshot: pending.body.snapshot }),
943
+ checksum: pending.body.local_upload.checksum_sha256,
944
+ };
945
+ }
946
+
947
+ /** @param {NonNullable<VersionRecord["pending"]>} pending @param {string} dir */
948
+ function bindRenderKey(pending, dir) {
949
+ return typeof pending.renderKey === "string"
950
+ ? pending
951
+ : { ...pending, renderKey: exportKeyOf(dir) };
952
+ }
@@ -446,9 +446,17 @@ async function spaceRefusal(client, refused, error) {
446
446
 
447
447
  /** Share's press: the first registers the project into the space given (none: the signed-in one) and opens its link
448
448
  * to that space; a later one reads the link, turning it back on when it was off. A registered project keeps its space.
449
+ * A host can prepare review content after registration is saved and before the link or its audience changes.
449
450
  * @param {{ projectPath: string, title: string, client: ReviewClient, spaceId?: string | null, audience?:
450
- * import("./types/share.js").AccessMode | undefined }} request */
451
- export const shareForReview = ({ projectPath, title, client, spaceId = null, audience }) =>
451
+ * import("./types/share.js").AccessMode | undefined, beforeOpen?: (projectId: string) => Promise<void> }} request */
452
+ export const shareForReview = ({
453
+ projectPath,
454
+ title,
455
+ client,
456
+ spaceId = null,
457
+ audience,
458
+ beforeOpen,
459
+ }) =>
452
460
  oneAtATime(projectPath, async () => {
453
461
  const read = await recordToShare(client, projectPath, spaceId);
454
462
  const id = read.projectId ?? (await registerOnce(client, projectPath, read, title));
@@ -458,6 +466,7 @@ export const shareForReview = ({ projectPath, title, client, spaceId = null, aud
458
466
  if (!read.projectId) await writeJsonAtomic(recordPath(projectPath), record);
459
467
  const share = await readShare(client, id, projectPath);
460
468
  if (takenDown(share)) return shown(client, share);
469
+ if (!record.opened || share.revoked) await beforeOpen?.(id);
461
470
  return openLink(client, projectPath, record, id, share, audience).then(
462
471
  (opened) => shown(client, opened),
463
472
  heldInstead(client, id),
@@ -18,6 +18,22 @@ export function fileSha256Base64(file) {
18
18
 
19
19
  /** @typedef {"sent" | "expired" | "refused" | "offline"} PutOutcome */
20
20
 
21
+ /** @param {string} url */
22
+ export function trustedUploadUrl(url) {
23
+ if (!URL.canParse(url)) return null;
24
+ const target = new URL(url);
25
+ const loopback = target.protocol === "http:" && target.hostname === "127.0.0.1";
26
+ if (
27
+ (target.protocol !== "https:" && !loopback) ||
28
+ target.username ||
29
+ target.password ||
30
+ target.hash ||
31
+ !reviewUploadOrigins().includes(target.origin)
32
+ )
33
+ return null;
34
+ return target;
35
+ }
36
+
21
37
  /** Streams the file to the signed URL through main's fetch (the app's proxy), the server's headers as given plus
22
38
  * the length, and no bearer. Nothing sent or answered for `stallMs` ends it as offline, never "Uploading" forever.
23
39
  * @param {{ put_url: string, headers: Record<string, string> }} upload @param {string} file @param {number} size
@@ -29,22 +45,8 @@ export async function putFile(
29
45
  onSent,
30
46
  { stallMs = 60_000, fetchImpl = fetch } = {},
31
47
  ) {
32
- let target;
33
- try {
34
- target = new URL(upload.put_url);
35
- } catch {
36
- return "refused";
37
- }
38
- const loopback = target.protocol === "http:" && target.hostname === "127.0.0.1";
39
- if (
40
- (target.protocol !== "https:" && !loopback) ||
41
- target.username ||
42
- target.password ||
43
- target.hash ||
44
- !reviewUploadOrigins().includes(target.origin)
45
- ) {
46
- return "refused";
47
- }
48
+ const target = trustedUploadUrl(upload.put_url);
49
+ if (!target) return "refused";
48
50
  const stall = new AbortController();
49
51
  /** @type {ReturnType<typeof setTimeout> | undefined} */
50
52
  let timer;
@@ -11,8 +11,10 @@ const SAID = {
11
11
  export_too_large: "This video is over 2 GB, the most a review page takes. Export a smaller one.",
12
12
  export_too_long:
13
13
  "This video is over 30 minutes, the most a review page takes. Export a shorter one.",
14
+ export_hourly_limit: (error) =>
15
+ `You've added the most versions for this hour. Try again ${laterIn(error.retryAfterMs, "in an hour")}.`,
14
16
  export_daily_limit: (error) =>
15
- `You've added the most versions for today. Try again ${laterIn(error.retryAfterMs)}.`,
17
+ `You've added the most versions for today. Try again ${laterIn(error.retryAfterMs, "tomorrow")}.`,
16
18
  content_rejected: "This video can't be shared.",
17
19
  export_blocked: "Adding videos to review pages isn't available for this account.",
18
20
  feature_unavailable: "Review pages aren't available for your account yet.",
@@ -24,9 +26,9 @@ const SAID = {
24
26
  held_for_review: HELD_FOR_REVIEW,
25
27
  };
26
28
 
27
- /** @param {unknown} error */
28
- export function versionSentence(error) {
29
- if (!(error instanceof ReviewFailed)) return "Couldn't add this version. Try again.";
29
+ /** @param {unknown} error @param {string} [fallback] */
30
+ export function versionSentence(error, fallback = "Couldn't add this version. Try again.") {
31
+ if (!(error instanceof ReviewFailed)) return fallback;
30
32
  const said = SAID[error.reason];
31
33
  if (said) return typeof said === "function" ? said(error) : said;
32
34
  if (error.status === 0) return "Couldn't reach HeyGen. Check your connection and try again.";
@@ -35,12 +37,12 @@ export function versionSentence(error) {
35
37
  // written for developers, so it is never shown.
36
38
  if (error.status === 200)
37
39
  return "HyperFrames couldn't process this video. Export it again, then try again.";
38
- return "Couldn't add this version. Try again.";
40
+ return fallback;
39
41
  }
40
42
 
41
- /** "in about 3 hours", from the server's Retry-After. @param {number | undefined} ms */
42
- function laterIn(ms) {
43
- if (!ms) return "tomorrow";
43
+ /** "in about 3 hours", from the server's Retry-After; `unknown` when it sent none. @param {number | undefined} ms @param {string} unknown */
44
+ function laterIn(ms, unknown) {
45
+ if (!ms) return unknown;
44
46
  const minutes = Math.ceil(ms / 60_000);
45
47
  if (minutes < 60) return `in ${minutes} minute${minutes === 1 ? "" : "s"}`;
46
48
  const hours = Math.round(minutes / 60);