@alpha2-sh/editor 0.4.5 → 0.5.0

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 (81) hide show
  1. package/dist/browser/bridge.d.ts +15 -16
  2. package/dist/browser/bridge.js +533 -387
  3. package/dist/browser/extensions-worker.d.ts +12 -16
  4. package/dist/browser/extensions-worker.js +17 -24
  5. package/dist/browser/hash-worker.d.ts +4 -5
  6. package/dist/browser/hash-worker.js +7 -8
  7. package/dist/browser/jobs.d.ts +17 -19
  8. package/dist/browser/jobs.js +41 -28
  9. package/dist/browser/resources.d.ts +6 -11
  10. package/dist/browser/resources.js +5 -9
  11. package/dist/browser/runtime/assets.d.ts +52 -31
  12. package/dist/browser/runtime/assets.js +206 -47
  13. package/dist/browser/runtime/audio.d.ts +16 -11
  14. package/dist/browser/runtime/audio.js +58 -31
  15. package/dist/browser/runtime/budget.d.ts +4 -5
  16. package/dist/browser/runtime/budget.js +4 -5
  17. package/dist/browser/runtime/caption-fonts.d.ts +3 -7
  18. package/dist/browser/runtime/caption-fonts.js +4 -8
  19. package/dist/browser/runtime/engine.d.ts +40 -43
  20. package/dist/browser/runtime/engine.js +82 -63
  21. package/dist/browser/runtime/export-audio.d.ts +6 -6
  22. package/dist/browser/runtime/export-audio.js +8 -9
  23. package/dist/browser/runtime/exporter.d.ts +33 -66
  24. package/dist/browser/runtime/exporter.js +111 -137
  25. package/dist/browser/runtime/extension-import.d.ts +28 -35
  26. package/dist/browser/runtime/extension-import.js +36 -46
  27. package/dist/browser/runtime/fingerprint.d.ts +11 -14
  28. package/dist/browser/runtime/fingerprint.js +20 -24
  29. package/dist/browser/runtime/generators.d.ts +2 -3
  30. package/dist/browser/runtime/generators.js +5 -7
  31. package/dist/browser/runtime/locks.d.ts +5 -7
  32. package/dist/browser/runtime/locks.js +5 -7
  33. package/dist/browser/runtime/output.d.ts +7 -9
  34. package/dist/browser/runtime/output.js +17 -21
  35. package/dist/browser/runtime/preview-utils.js +10 -6
  36. package/dist/browser/runtime/proxies.d.ts +10 -17
  37. package/dist/browser/runtime/proxies.js +33 -45
  38. package/dist/browser/runtime/proxy-storage.d.ts +15 -19
  39. package/dist/browser/runtime/proxy-storage.js +20 -24
  40. package/dist/browser/runtime/sources.d.ts +23 -31
  41. package/dist/browser/runtime/sources.js +98 -105
  42. package/dist/browser/runtime/staging.d.ts +2 -6
  43. package/dist/browser/runtime/staging.js +7 -11
  44. package/dist/browser/runtime/still.d.ts +2 -6
  45. package/dist/browser/runtime/still.js +6 -12
  46. package/dist/browser/runtime/text.d.ts +14 -0
  47. package/dist/browser/runtime/text.js +27 -0
  48. package/dist/browser/runtime/wasm.d.ts +4 -5
  49. package/dist/browser/runtime/wasm.js +4 -5
  50. package/dist/browser/types.d.ts +243 -221
  51. package/dist/browser/worker.d.ts +8 -9
  52. package/dist/browser/worker.js +55 -22
  53. package/dist/core/captions.d.ts +8 -10
  54. package/dist/core/captions.js +10 -18
  55. package/dist/core/clip-time.d.ts +8 -10
  56. package/dist/core/clip-time.js +12 -15
  57. package/dist/core/commands.d.ts +46 -46
  58. package/dist/core/commands.js +38 -36
  59. package/dist/core/compositions.d.ts +78 -112
  60. package/dist/core/compositions.js +66 -94
  61. package/dist/core/document.d.ts +59 -92
  62. package/dist/core/document.js +65 -90
  63. package/dist/core/keyframes.d.ts +11 -12
  64. package/dist/core/keyframes.js +11 -13
  65. package/dist/core/presets.d.ts +14 -20
  66. package/dist/core/presets.js +12 -18
  67. package/dist/core/session.d.ts +26 -38
  68. package/dist/core/session.js +38 -53
  69. package/dist/core/text.d.ts +2 -3
  70. package/dist/core/text.js +2 -3
  71. package/dist/core/timeline-index.d.ts +2 -3
  72. package/dist/core/timeline-index.js +11 -7
  73. package/dist/core/transitions.js +4 -5
  74. package/dist/core/validated.d.ts +6 -7
  75. package/dist/core/validated.js +8 -11
  76. package/dist/native-client/index.d.ts +31 -32
  77. package/dist/native-client/index.js +40 -41
  78. package/dist/ports/index.d.ts +19 -19
  79. package/dist/react/index.d.ts +5 -6
  80. package/dist/react/index.js +5 -6
  81. package/package.json +4 -4
@@ -1,45 +1,42 @@
1
1
  // Where a browser export's file goes: streamed into a temporary OPFS file (or a user-picked file)
2
- // so encoded video never accumulates in memory. In-memory output is for small outputs sized up
3
- // front, never a silent fallback for a large one.
2
+ // so encoded video never accumulates in memory. In-memory output is only for small outputs sized
3
+ // up front, never a silent fallback for large ones.
4
4
  import { estimatedOutputBytes, memoryOutputPeakBytes, MEMORY_OUTPUT_COPIES, MEMORY_OUTPUT_MARGIN, WritableFileDestination, } from "alpha2/export-destination";
5
5
  import { containerPolicy } from "alpha2/containers";
6
6
  import { ExportFailureError } from "alpha2/export-failure";
7
7
  import { holdShared, whenUnheld } from "./locks.js";
8
- /** The most an in-memory output may hold at its peak unless asked otherwise. */
8
+ /** Default peak size of an in-memory output. */
9
9
  export const DEFAULT_MEMORY_OUTPUT_LIMIT = 512 * 1024 * 1024;
10
- /** The origin-private directory temporary export files live in. */
11
10
  const EXPORT_DIRECTORY = "alpha2-exports";
12
11
  /**
13
- * A temporary export older than this, that no page holds, belongs to a page that is gone. Where the
14
- * browser has no Web Locks, age alone decides.
12
+ * Unheld temporary exports older than this belong to closed pages. Without Web Locks, age alone
13
+ * decides.
15
14
  */
16
15
  const STALE_EXPORT_MS = 24 * 60 * 60 * 1000;
17
16
  /** The Web Lock a page holds on a temporary export file while it writes or keeps it. */
18
17
  const exportLock = (fileName) => `alpha2-export:${fileName}`;
19
- /** Free storage an export is reported to want beyond its estimate. */
18
+ /** Free storage reported as wanted beyond the estimate. */
20
19
  const STORAGE_MARGIN = MEMORY_OUTPUT_MARGIN;
21
- /** Which storage APIs this page has, as an export's result reports them. */
20
+ /** Which storage APIs this page has, as an export result reports them. */
22
21
  export function storageSupport() {
23
22
  const storage = globalThis.navigator?.storage;
24
23
  return {
25
- // Handles gained `createWritable` later than the directory, so that is checked at open.
24
+ // Handles gained `createWritable` later than the directory API, so that is checked at open.
26
25
  opfs: typeof storage?.getDirectory === "function",
27
26
  savePicker: typeof globalThis.showSaveFilePicker === "function",
28
27
  estimate: typeof storage?.estimate === "function",
29
28
  };
30
29
  }
31
30
  /**
32
- * Opens where an export writes, refusing before anything renders only an in-memory output past its
33
- * limit.
31
+ * Opens where an export writes, refusing up front only an in-memory output past its limit.
34
32
  *
35
33
  * Storage is never refused ahead: `navigator.storage.estimate()` is a guess, and WebKit's usage
36
34
  * counts every write since its last disk scan, so `quota - usage` can be near zero on an empty
37
35
  * disk. A full disk fails at the write (`STORAGE_QUOTA_EXCEEDED`); the estimate is only reported.
38
36
  *
39
- * `container` (default `mp4`) gives the temporary file its extension and the artifact its MIME
40
- * type.
37
+ * `container` (default `mp4`) sets the temporary file's extension and the artifact's MIME type.
41
38
  * Returns `{ mode, destination, storageEstimate, abort, finish }`: `destination` is the sink's,
42
- * `abort` discards the file, and `finish(sinkOutput)` turns the finished output into the artifact's
39
+ * `abort` discards the file, and `finish(sinkOutput)` turns the finished output into artifact
43
40
  * fields.
44
41
  */
45
42
  export async function openExportOutput({ jobId, name, container = "mp4", target, durationSeconds, videoBitrate, audioBitrate, }) {
@@ -92,7 +89,7 @@ async function openOriginPrivateFile(jobId, name, policy, estimate) {
92
89
  let handle;
93
90
  let writable;
94
91
  const fileName = `${Date.now()}-${jobId}.${policy.extensions[0]}`;
95
- // Held from before the file exists until it is removed, so another page's sweep leaves it alone.
92
+ // Held from before the file exists until it is removed, so other pages' sweeps skip it.
96
93
  const lease = await holdShared(exportLock(fileName));
97
94
  try {
98
95
  directory = await (await storage.getDirectory()).getDirectoryHandle(EXPORT_DIRECTORY, { create: true });
@@ -152,9 +149,8 @@ async function openOriginPrivateFile(jobId, name, policy, estimate) {
152
149
  };
153
150
  }
154
151
  /**
155
- * What the browser says about the origin's storage, beside what this export is expected to need:
156
- * `belowEstimate` when its `quota - usage` is less. A report, never a refusal (see
157
- * `openExportOutput`); `null` where the browser says nothing.
152
+ * The origin's storage report beside this export's estimate, with `belowEstimate` when
153
+ * `quota - usage` is less. Informational only; `null` where the browser reports nothing.
158
154
  */
159
155
  async function reportedStorage(storage, estimate) {
160
156
  if (typeof storage.estimate !== "function")
@@ -193,8 +189,8 @@ function openMemory(limit, estimate, { name, mimeType, fellBack }) {
193
189
  };
194
190
  }
195
191
  /**
196
- * Deletes temporary export files left by pages that are gone: started over a day ago and held by
197
- * no page. Resolves to the names removed.
192
+ * Deletes temporary export files left by closed pages (started over a day ago, held by no page).
193
+ * Resolves to the names removed.
198
194
  * @returns {Promise<string[]>}
199
195
  */
200
196
  export async function sweepStaleExports(now = Date.now()) {
@@ -212,7 +208,7 @@ export async function sweepStaleExports(now = Date.now()) {
212
208
  removed.push(entry);
213
209
  }
214
210
  catch {
215
- // No directory yet, or no OPFS: nothing to sweep.
211
+ // No directory yet, or no OPFS, so nothing to sweep.
216
212
  }
217
213
  return removed;
218
214
  }
@@ -1,5 +1,6 @@
1
- // Keep the source's display aspect ratio; transparent padding belongs to the clip and participates
2
- // in its transform and transition.
1
+ import { activeTimeout } from "alpha2/active-clock";
2
+ // Keep the source's display aspect ratio; transparent padding belongs to the clip and takes part in
3
+ // its transform and transition.
3
4
  export function containRect(sourceWidth, sourceHeight, width, height) {
4
5
  const scale = Math.min(width / sourceWidth, height / sourceHeight);
5
6
  const drawWidth = sourceWidth * scale;
@@ -21,8 +22,9 @@ export async function seekVideoFrame(video, seconds, tolerance = 0.001) {
21
22
  video.readyState >= 2)
22
23
  return;
23
24
  await new Promise((resolve, reject) => {
25
+ const timeout = new AbortController();
24
26
  const cleanup = () => {
25
- clearTimeout(timeout);
27
+ timeout.abort();
26
28
  video.removeEventListener("seeked", done);
27
29
  video.removeEventListener("error", failed);
28
30
  };
@@ -36,13 +38,15 @@ export async function seekVideoFrame(video, seconds, tolerance = 0.001) {
36
38
  cleanup();
37
39
  reject(video.error ?? new Error("video seek failed"));
38
40
  };
39
- const timeout = setTimeout(() => {
41
+ // Active time, so a page frozen mid-seek does not time out.
42
+ void activeTimeout(10_000, timeout.signal).then(() => {
40
43
  cleanup();
41
44
  reject(new Error("video seek timed out"));
42
- }, 10_000);
45
+ });
43
46
  video.addEventListener("seeked", done);
44
47
  video.addEventListener("error", failed);
45
- // currentTime changes before the new frame is available; wait on a pending seek to this target.
48
+ // currentTime changes before the new frame is available, so wait on a pending seek to the
49
+ // target.
46
50
  if (!video.seeking || Math.abs(video.currentTime - seconds) > tolerance)
47
51
  video.currentTime = seconds;
48
52
  });
@@ -11,18 +11,14 @@ export const PROXY_LONG_EDGE: 960;
11
11
  * }} ProxyRecord
12
12
  */
13
13
  /**
14
- * Keeps proxies by their original's SHA-256: in OPFS where the browser can write files there, and
15
- * otherwise in this page's memory, within {@link DEFAULT_MEMORY_OUTPUT_LIMIT} unless asked
16
- * otherwise.
14
+ * Keeps proxies by their original's SHA-256, in OPFS where writable, else in this page's memory
15
+ * within {@link DEFAULT_MEMORY_OUTPUT_LIMIT} by default.
17
16
  */
18
17
  export class ProxyStore {
19
18
  constructor({ memoryLimit }?: {
20
19
  memoryLimit?: number | undefined;
21
20
  });
22
- /**
23
- * Holds the proxy of `hash` in use until `release()`, so no page removes it meanwhile (see
24
- * `holdProxy`).
25
- */
21
+ /** Holds the proxy of `hash` in use until `release()` so no page removes it (see `holdProxy`). */
26
22
  hold(hash: any): Promise<{
27
23
  release(): Promise<void>;
28
24
  }>;
@@ -31,24 +27,21 @@ export class ProxyStore {
31
27
  record: any;
32
28
  file: File;
33
29
  } | null>;
34
- /**
35
- * The proxies kept: each original's hash and the bytes its proxy takes, on disk or in this page's
36
- * memory.
37
- */
30
+ /** Each kept proxy's original hash and size in bytes, on disk or in this page's memory. */
38
31
  list(): Promise<{
39
32
  hash: string;
40
33
  bytes: number;
41
34
  }[]>;
42
35
  /**
43
- * Removes the proxies of `hashes`, except those a page holds in use. Resolves to the hashes
44
- * removed and those left in use.
36
+ * Removes the proxies of `hashes` except those held in use; resolves to removed and in-use
37
+ * hashes.
45
38
  */
46
39
  remove(hashes: any): Promise<import("../types.js").ProxyRemoval>;
47
40
  /**
48
- * Makes a proxy of `file`, whose fingerprint is `fingerprint`, and keeps it. The video is scaled
49
- * to fit {@link PROXY_LONG_EDGE} with a key frame every second; timestamps and the audio sample
50
- * rate are the original's. A proxy already kept for the same bytes is returned instead. Pages of
51
- * the origin make one hash's proxy in turn (`whileMakingProxy`).
41
+ * Makes and keeps a proxy of `file` (with `fingerprint`): video scaled to fit
42
+ * {@link PROXY_LONG_EDGE} with a key frame every second, original timestamps and audio sample
43
+ * rate. Returns an existing proxy for the same bytes instead. Pages of the origin take turns per
44
+ * hash (`whileMakingProxy`).
52
45
  *
53
46
  * Refuses before encoding when the conversion would drop a track the fingerprint records. Rejects
54
47
  * with `signal.reason` if `signal` aborts before the proxy is kept.
@@ -1,7 +1,7 @@
1
1
  // Proxies: lighter copies of a source's video, kept in OPFS under the original's content hash so a
2
- // reopened project previews before its files are relinked. Whether a proxy may stand in for its
3
- // source is the engine's decision. A proxy is streamed to its file as it is encoded; only a browser
4
- // without writable OPFS keeps it in memory, for this page and within a size limit.
2
+ // reopened project previews before its files are relinked. The engine decides whether a proxy may
3
+ // stand in for its source. Proxies stream to their file while encoding; only a browser without
4
+ // writable OPFS keeps them in memory, for this page and within a size limit.
5
5
  import { ALL_FORMATS, BlobSource, BufferTarget, Conversion, Input, Mp4OutputFormat, Output, StreamTarget, canEncodeAudio, } from "mediabunny";
6
6
  import { MEMORY_OUTPUT_COPIES, WritableFileDestination, } from "alpha2/export-destination";
7
7
  import { EditorError } from "../../core/document.js";
@@ -21,24 +21,17 @@ export const PROXY_LONG_EDGE = 960;
21
21
  * }} ProxyRecord
22
22
  */
23
23
  /**
24
- * Keeps proxies by their original's SHA-256: in OPFS where the browser can write files there, and
25
- * otherwise in this page's memory, within {@link DEFAULT_MEMORY_OUTPUT_LIMIT} unless asked
26
- * otherwise.
24
+ * Keeps proxies by their original's SHA-256, in OPFS where writable, else in this page's memory
25
+ * within {@link DEFAULT_MEMORY_OUTPUT_LIMIT} by default.
27
26
  */
28
27
  export class ProxyStore {
29
- /**
30
- * Proxies this page keeps in memory, where the browser cannot keep them on disk.
31
- * @type {Map<string, { record: ProxyRecord, file: File }>}
32
- */
28
+ /** @type {Map<string, { record: ProxyRecord, file: File }>} */
33
29
  #memory = new Map();
34
30
  #memoryLimit;
35
31
  constructor({ memoryLimit = DEFAULT_MEMORY_OUTPUT_LIMIT } = {}) {
36
32
  this.#memoryLimit = memoryLimit;
37
33
  }
38
- /**
39
- * Holds the proxy of `hash` in use until `release()`, so no page removes it meanwhile (see
40
- * `holdProxy`).
41
- */
34
+ /** Holds the proxy of `hash` in use until `release()` so no page removes it (see `holdProxy`). */
42
35
  hold(hash) {
43
36
  return holdProxy(hash);
44
37
  }
@@ -53,17 +46,14 @@ export class ProxyStore {
53
46
  try {
54
47
  const record = JSON.parse(await (await (await directory.getFileHandle(`${hash}.json`)).getFile()).text());
55
48
  const file = await (await directory.getFileHandle(`${hash}.mp4`)).getFile();
56
- // Read each time: another page may have removed it since.
49
+ // Read each time, since another page may have removed it.
57
50
  return { record, file };
58
51
  }
59
52
  catch {
60
53
  return null;
61
54
  }
62
55
  }
63
- /**
64
- * The proxies kept: each original's hash and the bytes its proxy takes, on disk or in this page's
65
- * memory.
66
- */
56
+ /** Each kept proxy's original hash and size in bytes, on disk or in this page's memory. */
67
57
  async list() {
68
58
  const kept = new Map((await keptProxies()).map(({ hash, bytes }) => [hash, bytes]));
69
59
  for (const [hash, { file }] of this.#memory)
@@ -71,8 +61,8 @@ export class ProxyStore {
71
61
  return [...kept].map(([hash, bytes]) => ({ hash, bytes }));
72
62
  }
73
63
  /**
74
- * Removes the proxies of `hashes`, except those a page holds in use. Resolves to the hashes
75
- * removed and those left in use.
64
+ * Removes the proxies of `hashes` except those held in use; resolves to removed and in-use
65
+ * hashes.
76
66
  */
77
67
  async remove(hashes) {
78
68
  const wanted = [...new Set(hashes)];
@@ -81,10 +71,10 @@ export class ProxyStore {
81
71
  return removeKeptProxies(wanted);
82
72
  }
83
73
  /**
84
- * Makes a proxy of `file`, whose fingerprint is `fingerprint`, and keeps it. The video is scaled
85
- * to fit {@link PROXY_LONG_EDGE} with a key frame every second; timestamps and the audio sample
86
- * rate are the original's. A proxy already kept for the same bytes is returned instead. Pages of
87
- * the origin make one hash's proxy in turn (`whileMakingProxy`).
74
+ * Makes and keeps a proxy of `file` (with `fingerprint`): video scaled to fit
75
+ * {@link PROXY_LONG_EDGE} with a key frame every second, original timestamps and audio sample
76
+ * rate. Returns an existing proxy for the same bytes instead. Pages of the origin take turns per
77
+ * hash (`whileMakingProxy`).
88
78
  *
89
79
  * Refuses before encoding when the conversion would drop a track the fingerprint records. Rejects
90
80
  * with `signal.reason` if `signal` aborts before the proxy is kept.
@@ -104,7 +94,7 @@ export class ProxyStore {
104
94
  const directory = held ? await proxyDirectory() : null;
105
95
  if (directory)
106
96
  await removeUnfinishedProxies(directory, hash);
107
- // Held while it is made, so no page removes it before it is returned.
97
+ // Held while made so no page removes it before it is returned.
108
98
  const hold = await holdProxy(hash);
109
99
  try {
110
100
  return await this.#make(file, fingerprint, hash, {
@@ -117,15 +107,14 @@ export class ProxyStore {
117
107
  }
118
108
  });
119
109
  }
120
- /** Makes the proxy of `file`, as {@link generate} says, holding the turn to make it. */
110
+ /** Makes the proxy of `file` while holding the turn (see {@link generate}). */
121
111
  async #make(file, fingerprint, hash, { signal, onProgress }) {
122
112
  signal?.throwIfAborted();
123
113
  const input = new Input({
124
114
  source: new BlobSource(file),
125
115
  formats: ALL_FORMATS,
126
116
  });
127
- // Installed before the setup's awaits, so an abort that comes first cancels the conversion once
128
- // it exists.
117
+ // Installed before setup awaits, so an earlier abort cancels the conversion once it exists.
129
118
  let cancel = () => { };
130
119
  const onAbort = () => cancel();
131
120
  signal?.addEventListener("abort", onAbort, { once: true });
@@ -137,13 +126,13 @@ export class ProxyStore {
137
126
  // The browser's scaler tone-maps HDR its own way, so the proxy would not match the original.
138
127
  if (space?.transfer === "pq" || space?.transfer === "hlg")
139
128
  throw new EditorError("unsupported_media", `${file.name} is HDR; the browser cannot make a proxy that keeps its color`, "proxy");
140
- // A proxy has square pixels, so it takes the original's displayed size.
129
+ // Proxies have square pixels, so use the original's displayed size.
141
130
  const shown = await displaySize(video);
142
131
  const scale = Math.min(1, PROXY_LONG_EDGE / Math.max(shown.width, shown.height));
143
132
  const even = (value) => Math.max(2, 2 * Math.round((value * scale) / 2));
144
133
  const audio = await input.getPrimaryAudioTrack();
145
- // AAC keeps any sample rate, else PCM; Opus would resample and the engine
146
- // would refuse the proxy.
134
+ // AAC keeps any sample rate, else PCM; Opus would resample and the engine would refuse the
135
+ // proxy.
147
136
  const audioCodec = audio &&
148
137
  (await canEncodeAudio("aac", {
149
138
  numberOfChannels: audio.numberOfChannels,
@@ -155,7 +144,7 @@ export class ProxyStore {
155
144
  const sink = await this.#open(hash);
156
145
  try {
157
146
  const output = new Output({
158
- // No fast start: samples stream out as they are muxed and nothing rewrites the payload.
147
+ // No fast start: samples stream out as muxed and nothing rewrites the payload.
159
148
  format: new Mp4OutputFormat({ fastStart: false }),
160
149
  target: sink.target,
161
150
  });
@@ -163,8 +152,8 @@ export class ProxyStore {
163
152
  input,
164
153
  output,
165
154
  tracks: "primary",
166
- // Without it a conversion starts at the first frame, so an original with a late one
167
- // would show that much early.
155
+ // Without it a conversion starts at the first frame, shifting an original with a late
156
+ // one.
168
157
  trim: { start: 0 },
169
158
  video: {
170
159
  width: even(shown.width),
@@ -183,7 +172,7 @@ export class ProxyStore {
183
172
  const left = conversion.discardedTracks.find(({ track }) => track === video || (track === audio && fingerprint.streams?.audio));
184
173
  if (left)
185
174
  throw new EditorError("unsupported_media", `Cannot make a proxy of ${file.name}: this browser cannot convert its ${left.track === video ? "video" : "audio"} (${left.reason})`, "proxy");
186
- // Discarded before the output closes it, so a cancelled proxy is never committed.
175
+ // Discard before the output closes it so a cancelled proxy is never committed.
187
176
  cancel = () => {
188
177
  void sink.discard();
189
178
  void conversion.cancel();
@@ -202,7 +191,7 @@ export class ProxyStore {
202
191
  signal?.throwIfAborted();
203
192
  if (sink.failure)
204
193
  throw sink.failure;
205
- // From here a failure discards the file; an abort after the proxy is kept leaves it kept.
194
+ // From here a failure discards the file; an abort after keeping leaves it kept.
206
195
  cancel = () => { };
207
196
  const proxy = await sink.file();
208
197
  const evidence = await probeEvidence(proxy);
@@ -238,7 +227,7 @@ export class ProxyStore {
238
227
  }
239
228
  /**
240
229
  * Where the proxy of `hash` is written: `target` takes the muxer's writes, `keep(record)` keeps
241
- * it, `discard()` leaves nothing behind. The file is written under its own name and moved to
230
+ * it, `discard()` leaves nothing. The file is written under its own name and moved to
242
231
  * `<hash>.mp4` by `keep`, so `discard` removes only what this sink wrote.
243
232
  */
244
233
  async #open(hash) {
@@ -276,7 +265,7 @@ export class ProxyStore {
276
265
  onOverflow: () => { },
277
266
  file: () => handle.getFile(),
278
267
  keep: async (record) => {
279
- // A video without a record is a stopped page's leftover and this page holds the turn.
268
+ // A video without a record is a stopped page's leftover, and this page holds the turn.
280
269
  await directory.removeEntry(kept).catch(() => { });
281
270
  moved = true;
282
271
  await moveFile(directory, handle, name, kept);
@@ -288,8 +277,8 @@ export class ProxyStore {
288
277
  },
289
278
  discard: () => (discarding ??= (async () => {
290
279
  await destination.abort();
291
- // Nothing finds an unrecorded file, and it is this sink's own: no other page's proxy is
292
- // in place while this page holds the turn.
280
+ // An unrecorded file is this sink's own; no other page's proxy is in place while this
281
+ // page holds the turn.
293
282
  if (recorded)
294
283
  await directory.removeEntry(`${hash}.json`).catch(() => { });
295
284
  if (moved)
@@ -326,14 +315,13 @@ export class ProxyStore {
326
315
  return sink;
327
316
  }
328
317
  }
329
- /** An id no other page's unfinished proxy has. */
330
318
  function uniqueId() {
331
319
  return (globalThis.crypto?.randomUUID?.() ??
332
320
  `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`);
333
321
  }
334
322
  /**
335
- * Moves the finished file `handle` (named `from`) to `to` in `directory`: with `move` where the
336
- * browser has it, else by copying it and removing the original.
323
+ * Moves finished file `handle` (named `from`) to `to`, with `move` where available, else by copy
324
+ * and remove.
337
325
  * @param {FileSystemDirectoryHandle} directory
338
326
  * @param {FileSystemFileHandle} handle
339
327
  * @param {string} from
@@ -348,7 +336,7 @@ async function moveFile(directory, handle, from, to) {
348
336
  return;
349
337
  }
350
338
  catch {
351
- // A browser whose move refuses here: copied instead.
339
+ // This browser's move refuses here, so copy instead.
352
340
  }
353
341
  const writable = await (await directory.getFileHandle(to, { create: true })).createWritable();
354
342
  try {
@@ -1,17 +1,15 @@
1
1
  /**
2
- * The proxy directory, or `null` where the browser has no origin-private file system (or, with
3
- * `create: false`, where no proxy was ever kept).
2
+ * The proxy directory, or `null` without an origin-private file system (or, with `create: false`,
3
+ * when no proxy was ever kept).
4
4
  * @returns {Promise<FileSystemDirectoryHandle | null>}
5
5
  */
6
6
  export function proxyDirectory({ create }?: {
7
7
  create?: boolean | undefined;
8
8
  }): Promise<FileSystemDirectoryHandle | null>;
9
9
  /**
10
- * Holds the proxy of `hash` in use, for this page, until `release()`: a removal from any page of
11
- * the origin leaves it alone meanwhile. Resolves once held; where the browser has no Web Locks, at
12
- * once and holding nothing.
13
- *
14
- * `release()` returns a promise that settles when the lock is let go.
10
+ * Holds the proxy of `hash` in use for this page until `release()`, so removals from any page of
11
+ * the origin skip it. Resolves once held; without Web Locks, at once and holding nothing.
12
+ * `release()` settles when the lock is let go.
15
13
  * @param {string} hash
16
14
  * @returns {Promise<{ release(): Promise<void> }>}
17
15
  */
@@ -19,10 +17,10 @@ export function holdProxy(hash: string): Promise<{
19
17
  release(): Promise<void>;
20
18
  }>;
21
19
  /**
22
- * Runs `make` as the only page of the origin making the proxy of `hash`: it waits for a page that
23
- * is making it to finish, so it can find that page's proxy instead of making it again. `make` is
24
- * told whether it holds the turn; where the browser has no Web Locks it runs at once, holding
25
- * nothing. Rejects with `signal.reason` once `signal` aborts while it waits.
20
+ * Runs `make` as the only page of the origin making the proxy of `hash`, waiting for a page already
21
+ * making it so the caller can find that proxy instead. `make` is told whether it holds the turn;
22
+ * without Web Locks it runs at once, holding nothing. Rejects with `signal.reason` if `signal`
23
+ * aborts while waiting.
26
24
  * @template T
27
25
  * @param {string} hash
28
26
  * @param {{ signal?: AbortSignal }} options
@@ -33,23 +31,21 @@ export function whileMakingProxy<T>(hash: string, options: {
33
31
  signal?: AbortSignal;
34
32
  }, make: (held: boolean) => Promise<T>): Promise<T>;
35
33
  /**
36
- * Removes what a page that stopped while making the proxy of `hash` left: the unfinished videos.
37
- * Only for the page holding the turn to make it ({@link whileMakingProxy}), when no other page can
38
- * be writing one.
34
+ * Removes unfinished videos a stopped page left while making the proxy of `hash`. Only for the page
35
+ * holding the turn ({@link whileMakingProxy}), when no other page can be writing one.
39
36
  * @param {FileSystemDirectoryHandle} directory
40
37
  * @param {string} hash
41
38
  */
42
39
  export function removeUnfinishedProxies(directory: FileSystemDirectoryHandle, hash: string): Promise<void>;
43
40
  /**
44
- * The proxies kept in this origin's storage: each original's hash and the bytes its proxy takes.
41
+ * Each proxy kept in this origin's storage: the original's hash and the proxy's size in bytes.
45
42
  * @returns {Promise<import("../types.js").KeptProxy[]>}
46
43
  */
47
44
  export function keptProxies(): Promise<import("../types.js").KeptProxy[]>;
48
45
  /**
49
- * Removes the kept proxies of `hashes`, except those a page of this origin holds in use ({@link
50
- * holdProxy}). Resolves to the hashes removed and those left because they are in use; a hash with
51
- * nothing kept counts as removed. A string that is not a lowercase hex SHA-256 is ignored and
52
- * listed in neither.
46
+ * Removes the kept proxies of `hashes` except those a page holds in use ({@link holdProxy}).
47
+ * Resolves to the removed hashes and those left in use; a hash with nothing kept counts as removed.
48
+ * Strings that are not lowercase hex SHA-256 are ignored and listed in neither.
53
49
  * @param {Iterable<string>} hashes
54
50
  * @returns {Promise<import("../types.js").ProxyRemoval>}
55
51
  */
@@ -1,8 +1,8 @@
1
- // Where kept proxies live: OPFS {@link PROXY_DIRECTORY}, `<hash>.mp4` plus `<hash>.json` per
2
- // original SHA-256. A proxy is shared by every project of the origin, so it lives while something
3
- // refers to it. Readers hold a shared Web Lock on the hash and removal takes it exclusively; makers
4
- // also take turns on a second lock ({@link whileMakingProxy}) and write `<hash>.<id>.part` until
5
- // finished. No media libraries here, so a storage-only page can load this module alone.
1
+ // Kept proxies live in OPFS {@link PROXY_DIRECTORY} as `<hash>.mp4` plus `<hash>.json` per original
2
+ // SHA-256, shared by every project of the origin. Readers hold a shared Web Lock on the hash and
3
+ // removal takes it exclusively; makers also take turns on a second lock ({@link whileMakingProxy})
4
+ // and write `<hash>.<id>.part` until finished. No media libraries here, so storage-only pages can
5
+ // load this module alone.
6
6
  import { holdShared, whenUnheld, withExclusive } from "./locks.js";
7
7
  /** The origin-private directory proxies are kept in. */
8
8
  export const PROXY_DIRECTORY = "alpha2-proxies";
@@ -13,8 +13,8 @@ const makeLockName = (hash) => `alpha2-proxy-make:${hash}`;
13
13
  export const unfinishedProxyName = (hash, id) => `${hash}.${id}.part`;
14
14
  const UNFINISHED = /^([0-9a-f]{64})\.[0-9A-Za-z-]+\.part$/;
15
15
  /**
16
- * The proxy directory, or `null` where the browser has no origin-private file system (or, with
17
- * `create: false`, where no proxy was ever kept).
16
+ * The proxy directory, or `null` without an origin-private file system (or, with `create: false`,
17
+ * when no proxy was ever kept).
18
18
  * @returns {Promise<FileSystemDirectoryHandle | null>}
19
19
  */
20
20
  export async function proxyDirectory({ create = true } = {}) {
@@ -27,11 +27,9 @@ export async function proxyDirectory({ create = true } = {}) {
27
27
  }
28
28
  }
29
29
  /**
30
- * Holds the proxy of `hash` in use, for this page, until `release()`: a removal from any page of
31
- * the origin leaves it alone meanwhile. Resolves once held; where the browser has no Web Locks, at
32
- * once and holding nothing.
33
- *
34
- * `release()` returns a promise that settles when the lock is let go.
30
+ * Holds the proxy of `hash` in use for this page until `release()`, so removals from any page of
31
+ * the origin skip it. Resolves once held; without Web Locks, at once and holding nothing.
32
+ * `release()` settles when the lock is let go.
35
33
  * @param {string} hash
36
34
  * @returns {Promise<{ release(): Promise<void> }>}
37
35
  */
@@ -39,10 +37,10 @@ export async function holdProxy(hash) {
39
37
  return holdShared(lockName(hash));
40
38
  }
41
39
  /**
42
- * Runs `make` as the only page of the origin making the proxy of `hash`: it waits for a page that
43
- * is making it to finish, so it can find that page's proxy instead of making it again. `make` is
44
- * told whether it holds the turn; where the browser has no Web Locks it runs at once, holding
45
- * nothing. Rejects with `signal.reason` once `signal` aborts while it waits.
40
+ * Runs `make` as the only page of the origin making the proxy of `hash`, waiting for a page already
41
+ * making it so the caller can find that proxy instead. `make` is told whether it holds the turn;
42
+ * without Web Locks it runs at once, holding nothing. Rejects with `signal.reason` if `signal`
43
+ * aborts while waiting.
46
44
  * @template T
47
45
  * @param {string} hash
48
46
  * @param {{ signal?: AbortSignal }} options
@@ -53,9 +51,8 @@ export function whileMakingProxy(hash, options, make) {
53
51
  return withExclusive(makeLockName(hash), options, make);
54
52
  }
55
53
  /**
56
- * Removes what a page that stopped while making the proxy of `hash` left: the unfinished videos.
57
- * Only for the page holding the turn to make it ({@link whileMakingProxy}), when no other page can
58
- * be writing one.
54
+ * Removes unfinished videos a stopped page left while making the proxy of `hash`. Only for the page
55
+ * holding the turn ({@link whileMakingProxy}), when no other page can be writing one.
59
56
  * @param {FileSystemDirectoryHandle} directory
60
57
  * @param {string} hash
61
58
  */
@@ -68,7 +65,7 @@ export async function removeUnfinishedProxies(directory, hash) {
68
65
  await directory.removeEntry(name).catch(() => { });
69
66
  }
70
67
  /**
71
- * The proxies kept in this origin's storage: each original's hash and the bytes its proxy takes.
68
+ * Each proxy kept in this origin's storage: the original's hash and the proxy's size in bytes.
72
69
  * @returns {Promise<import("../types.js").KeptProxy[]>}
73
70
  */
74
71
  export async function keptProxies() {
@@ -90,10 +87,9 @@ export async function keptProxies() {
90
87
  return [...bytes].map(([hash, size]) => ({ hash, bytes: size }));
91
88
  }
92
89
  /**
93
- * Removes the kept proxies of `hashes`, except those a page of this origin holds in use ({@link
94
- * holdProxy}). Resolves to the hashes removed and those left because they are in use; a hash with
95
- * nothing kept counts as removed. A string that is not a lowercase hex SHA-256 is ignored and
96
- * listed in neither.
90
+ * Removes the kept proxies of `hashes` except those a page holds in use ({@link holdProxy}).
91
+ * Resolves to the removed hashes and those left in use; a hash with nothing kept counts as removed.
92
+ * Strings that are not lowercase hex SHA-256 are ignored and listed in neither.
97
93
  * @param {Iterable<string>} hashes
98
94
  * @returns {Promise<import("../types.js").ProxyRemoval>}
99
95
  */