@proteinjs/db-file 1.8.4 → 1.9.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 (97) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/dist/generated/index.d.ts.map +1 -1
  3. package/dist/generated/index.js +3 -1
  4. package/dist/generated/index.js.map +1 -1
  5. package/dist/index.d.ts +4 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +4 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/src/DbFileStorageDriver.d.ts +7 -0
  10. package/dist/src/DbFileStorageDriver.d.ts.map +1 -1
  11. package/dist/src/DbFileStorageDriver.js +69 -41
  12. package/dist/src/DbFileStorageDriver.js.map +1 -1
  13. package/dist/src/FileCopyForOthers.d.ts +52 -0
  14. package/dist/src/FileCopyForOthers.d.ts.map +1 -0
  15. package/dist/src/FileCopyForOthers.js +62 -0
  16. package/dist/src/FileCopyForOthers.js.map +1 -0
  17. package/dist/src/FileStorage.d.ts +145 -11
  18. package/dist/src/FileStorage.d.ts.map +1 -1
  19. package/dist/src/FileStorage.js +537 -41
  20. package/dist/src/FileStorage.js.map +1 -1
  21. package/dist/src/FileStorageDriver.d.ts +11 -0
  22. package/dist/src/FileStorageDriver.d.ts.map +1 -1
  23. package/dist/src/FileStorageError.d.ts +62 -0
  24. package/dist/src/FileStorageError.d.ts.map +1 -0
  25. package/dist/src/FileStorageError.js +118 -0
  26. package/dist/src/FileStorageError.js.map +1 -0
  27. package/dist/src/FileVariantMaker.d.ts +61 -0
  28. package/dist/src/FileVariantMaker.d.ts.map +1 -0
  29. package/dist/src/FileVariantMaker.js +62 -0
  30. package/dist/src/FileVariantMaker.js.map +1 -0
  31. package/dist/src/routes/FileResponder.d.ts +41 -0
  32. package/dist/src/routes/FileResponder.d.ts.map +1 -0
  33. package/dist/src/routes/FileResponder.js +135 -0
  34. package/dist/src/routes/FileResponder.js.map +1 -0
  35. package/dist/src/routes/getFile.d.ts +5 -13
  36. package/dist/src/routes/getFile.d.ts.map +1 -1
  37. package/dist/src/routes/getFile.js +20 -56
  38. package/dist/src/routes/getFile.js.map +1 -1
  39. package/dist/src/routes/getFileVariant.d.ts +20 -0
  40. package/dist/src/routes/getFileVariant.d.ts.map +1 -0
  41. package/dist/src/routes/getFileVariant.js +112 -0
  42. package/dist/src/routes/getFileVariant.js.map +1 -0
  43. package/dist/src/routes/getFileVariantRoute.d.ts +7 -0
  44. package/dist/src/routes/getFileVariantRoute.d.ts.map +1 -0
  45. package/dist/src/routes/getFileVariantRoute.js +9 -0
  46. package/dist/src/routes/getFileVariantRoute.js.map +1 -0
  47. package/dist/src/tables/FileTable.d.ts +55 -2
  48. package/dist/src/tables/FileTable.d.ts.map +1 -1
  49. package/dist/src/tables/FileTable.js +8 -0
  50. package/dist/src/tables/FileTable.js.map +1 -1
  51. package/dist/test/DbFileStorageDriverMissingBytes.test.d.ts +2 -0
  52. package/dist/test/DbFileStorageDriverMissingBytes.test.d.ts.map +1 -0
  53. package/dist/test/DbFileStorageDriverMissingBytes.test.js +184 -0
  54. package/dist/test/DbFileStorageDriverMissingBytes.test.js.map +1 -0
  55. package/dist/test/FileByteAccess.test.js +6 -3
  56. package/dist/test/FileByteAccess.test.js.map +1 -1
  57. package/dist/test/FileCopyForOthers.test.d.ts +2 -0
  58. package/dist/test/FileCopyForOthers.test.d.ts.map +1 -0
  59. package/dist/test/FileCopyForOthers.test.js +1143 -0
  60. package/dist/test/FileCopyForOthers.test.js.map +1 -0
  61. package/dist/test/FileMediaMetadata.test.js +60 -5
  62. package/dist/test/FileMediaMetadata.test.js.map +1 -1
  63. package/dist/test/FileStorageError.test.d.ts +2 -0
  64. package/dist/test/FileStorageError.test.d.ts.map +1 -0
  65. package/dist/test/FileStorageError.test.js +68 -0
  66. package/dist/test/FileStorageError.test.js.map +1 -0
  67. package/dist/test/FileTestEnvironment.d.ts.map +1 -1
  68. package/dist/test/FileTestEnvironment.js +3 -0
  69. package/dist/test/FileTestEnvironment.js.map +1 -1
  70. package/dist/test/FileVariants.test.d.ts +2 -0
  71. package/dist/test/FileVariants.test.d.ts.map +1 -0
  72. package/dist/test/FileVariants.test.js +1008 -0
  73. package/dist/test/FileVariants.test.js.map +1 -0
  74. package/dist/test/GetFileRoute.test.js +29 -0
  75. package/dist/test/GetFileRoute.test.js.map +1 -1
  76. package/generated/index.ts +3 -1
  77. package/index.ts +4 -0
  78. package/package.json +5 -5
  79. package/src/DbFileStorageDriver.ts +32 -21
  80. package/src/FileCopyForOthers.ts +71 -0
  81. package/src/FileStorage.ts +351 -18
  82. package/src/FileStorageDriver.ts +12 -0
  83. package/src/FileStorageError.ts +123 -0
  84. package/src/FileVariantMaker.ts +84 -0
  85. package/src/routes/FileResponder.ts +85 -0
  86. package/src/routes/getFile.ts +9 -44
  87. package/src/routes/getFileVariant.ts +53 -0
  88. package/src/routes/getFileVariantRoute.ts +7 -0
  89. package/src/tables/FileTable.ts +57 -1
  90. package/test/DbFileStorageDriverMissingBytes.test.ts +85 -0
  91. package/test/FileByteAccess.test.ts +7 -1
  92. package/test/FileCopyForOthers.test.ts +702 -0
  93. package/test/FileMediaMetadata.test.ts +44 -0
  94. package/test/FileStorageError.test.ts +79 -0
  95. package/test/FileTestEnvironment.ts +3 -0
  96. package/test/FileVariants.test.ts +604 -0
  97. package/test/GetFileRoute.test.ts +19 -0
@@ -1,11 +1,21 @@
1
- import { ScopedRecord, getScopedDb, getScopedDbAsSystem } from '@proteinjs/user';
1
+ import { Reference, getDbAsSystem } from '@proteinjs/db';
2
+ import { ScopedRecord, UserRepo, getScopedDb, getScopedDbAsSystem } from '@proteinjs/user';
2
3
  import { File } from './tables/FileTable';
3
4
  import { tables } from './tables/tables';
4
5
  import { FileStorageService, getFileStorageService } from './services/FileStorageService';
5
6
  import { FileStorageDriver } from './FileStorageDriver';
6
7
  import { getFileReachabilityResolvers } from './FileReachabilityResolver';
8
+ import { FileCopyRefused, getFileCopyForOthers } from './FileCopyForOthers';
9
+ import {
10
+ FILE_VARIANT_KINDS,
11
+ FileVariant,
12
+ FileVariantKind,
13
+ FileVariantNotMade,
14
+ getFileVariantMaker,
15
+ } from './FileVariantMaker';
7
16
  import { Loadable, SourceRepository } from '@proteinjs/reflection';
8
17
  import { Logger } from '@proteinjs/logger';
18
+ import { ServiceRefusal } from '@proteinjs/service';
9
19
  import { DbFileStorageDriver } from './DbFileStorageDriver';
10
20
 
11
21
  /**
@@ -29,12 +39,15 @@ export interface DefaultFileStorageDriverFactory extends Loadable {
29
39
  */
30
40
  export class FileStorage implements FileStorageService {
31
41
  private static driver: FileStorageDriver;
42
+ /** The read path's derivations running in this process, by file and kind — see {@link deriveOnce}. */
43
+ private static derivations = new Map<string, Promise<string | undefined>>();
32
44
 
33
45
  public serviceMetadata = {
34
46
  auth: {
35
47
  allUsers: true,
36
48
  },
37
49
  };
50
+ private logger = new Logger({ name: this.constructor.name });
38
51
 
39
52
  /**
40
53
  * The `FileStorageDriver` for this process — provided by the `DefaultFileStorageDriverFactory`
@@ -77,11 +90,16 @@ export class FileStorage implements FileStorageService {
77
90
  * Retrieves the metadata of a given file — THE file-read access decision every serving path
78
91
  * derives from (`/file/:id`, signed-URL minting, the browser service).
79
92
  *
80
- * Two legs: the caller's own file resolves by the SCOPED read; when that misses, the
81
- * shared-content leg asks the registered {@link FileReachabilityResolver}s whether the caller
82
- * can read a row that REFERENCES the file (a shared thought's media node) — content access
83
- * confers file access, through the content's own grant-filtered read, never by widening file
84
- * scope. No resolver vouching means the miss stands. Reads only: writes/deletes stay scoped.
93
+ * Three legs, cheapest first: the caller's own file resolves by the SCOPED read. When that
94
+ * misses, the derived leg: a VARIANT the seam made (`File.variantOf`) is readable by whoever
95
+ * can read its original — this same decision, asked of the original — so a variant derived
96
+ * after the content was placed (no content row names it) is reachable by exactly the readers
97
+ * of the file it was made from; one system point read decides whether the row is a variant at
98
+ * all. Last, the shared-content leg asks the registered {@link FileReachabilityResolver}s
99
+ * whether the caller can read a row that REFERENCES the file (a shared thought's media node) —
100
+ * content access confers file access, through the content's own grant-filtered read, never by
101
+ * widening file scope. No leg vouching means the miss stands. Reads only: writes/deletes stay
102
+ * scoped.
85
103
  * @param fileId - The `id` of the file.
86
104
  * @returns The file metadata, or `undefined` when the caller can neither own nor reach it.
87
105
  */
@@ -92,15 +110,82 @@ export class FileStorage implements FileStorageService {
92
110
  return file;
93
111
  }
94
112
 
113
+ const row = await getScopedDbAsSystem().get(tables.File, { id: fileId });
114
+ if (!row) {
115
+ return undefined as unknown as File;
116
+ }
117
+ if (row.variantOf?._id) {
118
+ // A variant's reachability is its original's — the copy for others carries no `variantOf`,
119
+ // so its own id stays nobody's but the owner's.
120
+ return (await this.getFile(row.variantOf._id)) ? row : (undefined as unknown as File);
121
+ }
122
+
95
123
  for (const resolver of getFileReachabilityResolvers()) {
96
124
  if (await resolver.canReadViaReference(fileId)) {
97
125
  // Reachability established through the caller's grant-filtered content read — the row
98
126
  // itself lives in the owner's scope, so it is served via system read.
99
- return await getScopedDbAsSystem().get(tables.File, { id: fileId });
127
+ return row;
100
128
  }
101
129
  }
102
130
 
103
- return file;
131
+ return undefined as unknown as File;
132
+ }
133
+
134
+ /**
135
+ * The variant of this kind of a file — the derived File the row names, or one made now, ONCE,
136
+ * by the registered {@link FileVariantMaker} (the read path's door, behind
137
+ * `GET /file/:id/variant/:kind`: a file made before the seam existed, or by a producer that did
138
+ * not derive it, gets its variant on the first request from a surface that draws it; every
139
+ * later request is served the same row without the maker). Server-only, like `getSignedUrl` —
140
+ * the browser asks by URL, never by service call. Access is the original's ({@link getFile});
141
+ * the variant is stored as the owner's own File whoever's read made it, and is served like any
142
+ * File afterwards — with the same non-owner copy rule.
143
+ * @returns The variant's File row, or `undefined` when the caller cannot read the original, no
144
+ * maker is registered, the maker does not apply to this file for this kind, or the
145
+ * maker could not make it of these bytes (the consumer then draws the original).
146
+ */
147
+ async getVariant(fileId: string, kind: FileVariantKind): Promise<File | undefined> {
148
+ const file = await this.getFile(fileId);
149
+ if (!file) {
150
+ return undefined;
151
+ }
152
+ const named = file[kind]?._id;
153
+ if (named) {
154
+ return await getScopedDbAsSystem().get(tables.File, { id: named });
155
+ }
156
+ const maker = getFileVariantMaker();
157
+ if (!maker || !maker.appliesTo(file, kind)) {
158
+ return undefined;
159
+ }
160
+ const variantId = await this.deriveOnce(file, kind);
161
+ return variantId ? await getScopedDbAsSystem().get(tables.File, { id: variantId }) : undefined;
162
+ }
163
+
164
+ /**
165
+ * Make and store every variant the registered maker applies to, with the original's bytes in
166
+ * hand (the ingest's door — one decode of bytes just stored, never a re-read). Each is named on
167
+ * the row; a kind the maker does not apply to is left unset (the read path may derive it later
168
+ * if the maker's rule changes). With no maker registered nothing is made.
169
+ * @returns The caller's file row with each variant made here named on it (the row as the caller
170
+ * holds it, not a re-read — an unset column stays unset), and the variant Files made.
171
+ */
172
+ async deriveVariants(
173
+ file: File,
174
+ bytes: Buffer
175
+ ): Promise<{ file: File; variants: Partial<Record<FileVariantKind, File>> }> {
176
+ const maker = getFileVariantMaker();
177
+ const variants: Partial<Record<FileVariantKind, File>> = {};
178
+ const named: Partial<Pick<File, FileVariantKind>> = {};
179
+ if (maker) {
180
+ for (const kind of FILE_VARIANT_KINDS) {
181
+ if (!file[kind]?._id && maker.appliesTo(file, kind)) {
182
+ const variantId = await this.makeVariant(file, kind, bytes);
183
+ variants[kind] = await getScopedDbAsSystem().get(tables.File, { id: variantId });
184
+ named[kind] = new Reference<File>(tables.File.name, variantId);
185
+ }
186
+ }
187
+ }
188
+ return { file: { ...file, ...named }, variants };
104
189
  }
105
190
 
106
191
  /**
@@ -111,37 +196,65 @@ export class FileStorage implements FileStorageService {
111
196
  * exactly as reachable as the row that names them. The gate lives here because this is the
112
197
  * browser-facing service boundary (`FileStorageService` is `allUsers`) and the driver byte ops
113
198
  * below are deliberately unscoped. Server-side doors that make their OWN documented access
114
- * decision (the avatar route, issue/ticket attachment doors) read bytes through
115
- * `FileStorage.getDriver()` instead.
199
+ * decision (an attachment door keyed by a record's linkage, say) serve through
200
+ * {@link getAuthorizedFileData} — the same owner-or-copy bytes, without this gate.
201
+ *
202
+ * WHICH bytes: the owner's read serves the original; anyone else's — a read that reached the
203
+ * file through the shared-content leg — serves the {@link FileCopyForOthers} copy
204
+ * ({@link servedFileId}): the same substitution `getSignedUrl` makes, so the proxy route, the
205
+ * signed-URL route and every server-side reader of this door agree.
116
206
  * @param fileId - The `id` of the file.
117
207
  * @returns The file data as a single string.
118
- * @throws When the file row does not exist or is not readable by the caller — the same named
119
- * miss either way, so existence is not leaked to unauthorized callers.
208
+ * @throws ServiceRefusal (404, `File not found`) when the file row does not exist or is not
209
+ * readable by the caller, and when the caller is not the owner and no copy can be made
210
+ * ({@link servedFileId}) — ONE answer for all three ({@link notFound}), so no door tells
211
+ * a missing file from one this caller may not have.
120
212
  */
121
213
  async getFileData(fileId: string): Promise<string> {
122
214
  const file = await this.getFile(fileId);
123
215
  if (!file) {
124
- throw new Error(`File not found: ${fileId}`);
216
+ throw FileStorage.notFound();
125
217
  }
126
218
 
127
- return await FileStorage.getDriver().getFileData(fileId);
219
+ return await FileStorage.getDriver().getFileData(await this.servedFileId(file));
220
+ }
221
+
222
+ /**
223
+ * The bytes THIS caller is served for a file a server-side door has ALREADY decided the caller
224
+ * may read, by its own documented rule (an attachment door keyed by a record's linkage, say —
225
+ * a read neither the caller's scope nor a {@link FileReachabilityResolver} would open). The
226
+ * door vouches for access; this decides WHICH bytes, exactly as {@link getFileData} does: the
227
+ * original for the file's owner, the {@link FileCopyForOthers} copy for anyone else — the same
228
+ * copy, made once and named on the row, that every other door serves them. Server-only (not
229
+ * part of the browser-facing `FileStorageService`), so the rule that opens the file stays the
230
+ * door's.
231
+ * @param file - The file's row, as the door read it (its `scope` names the owner).
232
+ * @returns The file data as a single string.
233
+ * @throws ServiceRefusal (404) when the caller is not the owner and no copy can be made — the same
234
+ * refusal every door gives ({@link servedFileId}).
235
+ */
236
+ async getAuthorizedFileData(file: File): Promise<string> {
237
+ return await FileStorage.getDriver().getFileData(await this.servedFileId(file));
128
238
  }
129
239
 
130
240
  /**
131
241
  * Mint a short-lived, read-only URL for the file's bytes (server-only; not part of the
132
242
  * browser-facing `FileStorageService`). A signed URL is a bearer capability, so minting
133
243
  * verifies the caller can read the file row (the two-legged {@link getFile} read — own scope
134
- * or shared-content reachability) before signing.
244
+ * or shared-content reachability) before signing — and signs the bytes THAT caller is served:
245
+ * the original for the owner, the {@link FileCopyForOthers} copy for anyone else.
135
246
  * @param fileId - The `id` of the file.
136
247
  * @param options.ttlMs - How long the URL stays valid; the driver applies its default when omitted.
137
248
  * @returns The signed URL, or `undefined` when the driver has no external URL space
138
249
  * (`DbFileStorageDriver`) — the caller then serves bytes through the proxy route.
139
- * @throws When the file row does not exist or is not readable by the caller.
250
+ * @throws ServiceRefusal (404, `File not found`) when the file row does not exist or is not
251
+ * readable by the caller, and when the caller is not the owner and no copy can be made —
252
+ * the one answer {@link getFileData} gives ({@link notFound}).
140
253
  */
141
254
  async getSignedUrl(fileId: string, options?: { ttlMs?: number }): Promise<string | undefined> {
142
255
  const file = await this.getFile(fileId);
143
256
  if (!file) {
144
- throw new Error(`File not found: ${fileId}`);
257
+ throw FileStorage.notFound();
145
258
  }
146
259
 
147
260
  const driver = FileStorage.getDriver();
@@ -149,7 +262,7 @@ export class FileStorage implements FileStorageService {
149
262
  return undefined;
150
263
  }
151
264
 
152
- return await driver.getSignedUrl(fileId, options);
265
+ return await driver.getSignedUrl(await this.servedFileId(file), options);
153
266
  }
154
267
 
155
268
  /**
@@ -159,6 +272,10 @@ export class FileStorage implements FileStorageService {
159
272
  * (the same authorization a scoped row update enforces). Deliberately STRICTER than
160
273
  * {@link getFileData}: the {@link FileReachabilityResolver} leg widens READS only, so a
161
274
  * share recipient who can read the bytes still cannot write them.
275
+ *
276
+ * New bytes make every derived File stale — the copy others were served, the preview, the
277
+ * stage variant: each is dropped here — the row forgets it, then its row (and, through the
278
+ * delete watcher, its bytes) goes — and the next read that wants one makes a fresh one.
162
279
  * @param fileId - The `id` of the file.
163
280
  * @param data - The new data string to replace the existing data.
164
281
  * @throws When the file row does not exist or is not writable by the caller — the same named
@@ -172,6 +289,7 @@ export class FileStorage implements FileStorageService {
172
289
  }
173
290
 
174
291
  await FileStorage.getDriver().updateFileData(fileId, data);
292
+ await this.dropDerivedFiles(file);
175
293
  }
176
294
 
177
295
  /**
@@ -199,4 +317,219 @@ export class FileStorage implements FileStorageService {
199
317
  const db = getScopedDb();
200
318
  await db.delete(tables.File, { id: fileId });
201
319
  }
320
+
321
+ /**
322
+ * The id whose bytes this caller is served: the file's own for its owner, or for a file with
323
+ * no maker (or one the maker does not apply to); otherwise the {@link FileCopyForOthers} copy —
324
+ * the one already named on the row, or made now, once, and named for every later read.
325
+ *
326
+ * ONE REFUSAL, EVERY DOOR: when no copy can be made (the seam's {@link FileCopyRefused}), the file
327
+ * is served to no one but its owner, so to this caller it is not there — a
328
+ * `ServiceRefusal(404, 'File not found')`, thrown here so the browser service, `GET /file/:id` in
329
+ * both its shapes and the server-side door answer the same by construction: the service router
330
+ * answers 404 `{ error: 'File not found' }`, the executor logs one WARN (a refusal, never a
331
+ * failure), the route answers `404 File not found` — the same answer, at every door, as a row the
332
+ * caller cannot read ({@link notFound}). The refusal carries no reason, file id or owner: the
333
+ * maker's reason is the server's, logged here in one WARN, so nothing a caller reads tells a
334
+ * refused copy from a file that is not there.
335
+ */
336
+ private async servedFileId(file: File): Promise<string> {
337
+ if (this.ownedByCaller(file)) {
338
+ return file.id;
339
+ }
340
+ if (file.copyForOthers?._id) {
341
+ return file.copyForOthers._id;
342
+ }
343
+ const maker = getFileCopyForOthers();
344
+ if (!maker || !maker.appliesTo(file)) {
345
+ return file.id;
346
+ }
347
+ try {
348
+ return await this.makeCopyForOthers(file);
349
+ } catch (error) {
350
+ if (FileCopyRefused.is(error)) {
351
+ this.logger.warn({
352
+ message: 'No copy of a file can be made for anyone but its owner; refused as not found',
353
+ obj: { fileId: error.fileId, reason: error.reason },
354
+ });
355
+ throw FileStorage.notFound();
356
+ }
357
+ throw error;
358
+ }
359
+ }
360
+
361
+ /**
362
+ * Whether the caller is the file's owner — the row's `scope` is the owner (the scoped read
363
+ * {@link getFile} makes first is exactly `scope = the caller`), so a row {@link getFile} handed
364
+ * back whose scope is someone else's was reached through the shared-content leg. A row with no
365
+ * owner recorded serves as itself: there is no one to withhold it from.
366
+ */
367
+ private ownedByCaller(file: File): boolean {
368
+ return !file.scope || file.scope === new UserRepo().getUser().id;
369
+ }
370
+
371
+ /**
372
+ * Makes the copy (the maker's bytes, from the original's), stores it as a File in the owner's
373
+ * scope and names it on the original's row. Two non-owners reading at once may both get here:
374
+ * the row's word wins — decided in ONE transaction (the read of the row and the write of its
375
+ * word together, so racing writers are serialized and the loser's transaction, retried, finds
376
+ * the winner named) — and a copy that lost is deleted after the commit, the first is served.
377
+ */
378
+ private async makeCopyForOthers(file: File): Promise<string> {
379
+ const maker = getFileCopyForOthers()!;
380
+ const driver = FileStorage.getDriver();
381
+ const original = Buffer.from(await driver.getFileData(file.id), 'base64');
382
+ let copyBytes: Buffer;
383
+ try {
384
+ copyBytes = await maker.make(file, original);
385
+ } catch (cause) {
386
+ // The maker's contract: a throw means no copy can be made — the file is the owner's alone.
387
+ throw new FileCopyRefused(file.id, cause);
388
+ }
389
+ // As system, in the OWNER's scope: the copy is the owner's file (their storage, their purge).
390
+ const system = getDbAsSystem();
391
+ const copy = await system.insert(tables.File, {
392
+ name: file.name,
393
+ type: file.type,
394
+ size: copyBytes.length,
395
+ ...(file.width !== undefined && file.width !== null ? { width: file.width } : {}),
396
+ ...(file.height !== undefined && file.height !== null ? { height: file.height } : {}),
397
+ ...(file.durationMs !== undefined && file.durationMs !== null ? { durationMs: file.durationMs } : {}),
398
+ scope: file.scope,
399
+ });
400
+ await driver.createFile(copy, copyBytes.toString('base64'));
401
+
402
+ const named = await system.runTransaction(async () => {
403
+ const current = await system.get(tables.File, { id: file.id });
404
+ if (current?.copyForOthers?._id) {
405
+ return current.copyForOthers._id;
406
+ }
407
+ await system.update(tables.File, { id: file.id, copyForOthers: new Reference<File>(tables.File.name, copy.id) });
408
+ return copy.id;
409
+ });
410
+ if (named !== copy.id) {
411
+ // Outside the transaction: the row's delete takes the bytes with it (the delete watcher), a
412
+ // side effect that must not ride a transaction the runner may retry.
413
+ await system.delete(tables.File, { id: copy.id });
414
+ }
415
+ return named;
416
+ }
417
+
418
+ /**
419
+ * The read path's derivation, ONCE per file and kind at a time in this process: the readers
420
+ * that ask for the same absent variant while one derives (a shared document opened by many on
421
+ * one tick — every face asks the same URL) wait for that derivation's answer instead of each
422
+ * reading the original, encoding it and storing an object the row's word would then discard.
423
+ * Across processes the row's word in one transaction ({@link makeVariant}) still decides: this
424
+ * bounds the cost, that bounds the outcome. A maker that cannot make the variant of these
425
+ * bytes (a picture its decoder cannot read) answers `undefined` — nothing is stored, the reader
426
+ * draws the file itself, the next reader asks again — said in the log, never as a failed read;
427
+ * any other failure (the store, the database) is the reader's as it would be for any File.
428
+ */
429
+ private deriveOnce(file: File, kind: FileVariantKind): Promise<string | undefined> {
430
+ const key = `${file.id}:${kind}`;
431
+ const running = FileStorage.derivations.get(key);
432
+ if (running) {
433
+ return running;
434
+ }
435
+ const derivation = (async () => {
436
+ const original = Buffer.from(await FileStorage.getDriver().getFileData(file.id), 'base64');
437
+ try {
438
+ return await this.makeVariant(file, kind, original);
439
+ } catch (error) {
440
+ if (!FileVariantNotMade.is(error)) {
441
+ throw error;
442
+ }
443
+ new Logger({ name: 'FileStorage' }).warn({
444
+ message: `No ${kind} variant could be made of file ${file.id}; the file itself is served`,
445
+ obj: { reason: error.reason },
446
+ });
447
+ return undefined;
448
+ }
449
+ })().finally(() => FileStorage.derivations.delete(key));
450
+ FileStorage.derivations.set(key, derivation);
451
+ return derivation;
452
+ }
453
+
454
+ /**
455
+ * Makes the variant (the maker's answer from the original's bytes), stores it as a File in the
456
+ * OWNER's scope naming its original, and names it on the original's row. Two readers may derive
457
+ * the same kind at once: the row's word wins — decided in ONE transaction, exactly as the copy
458
+ * for others is named — and the variant that lost is deleted after the commit. The maker's own
459
+ * failure (it could not make the variant of these bytes) is thrown as {@link FileVariantNotMade}
460
+ * with the maker's reason, so the doors can tell it from a failure of the store or the database;
461
+ * nothing is stored on it.
462
+ * @returns The id of the variant the row names.
463
+ */
464
+ private async makeVariant(file: File, kind: FileVariantKind, bytes: Buffer): Promise<string> {
465
+ const maker = getFileVariantMaker()!;
466
+ const driver = FileStorage.getDriver();
467
+ let made: FileVariant;
468
+ try {
469
+ made = await maker.make(file, bytes, kind);
470
+ } catch (cause) {
471
+ // The maker's contract: a throw means no variant can be made of these bytes.
472
+ throw new FileVariantNotMade(file.id, kind, cause);
473
+ }
474
+ // As system, in the OWNER's scope: the variant is the owner's file (their storage, their purge).
475
+ const system = getDbAsSystem();
476
+ const variant = await system.insert(tables.File, {
477
+ name: `(${kind}) ${file.name}`,
478
+ type: made.type,
479
+ size: made.bytes.length,
480
+ ...(made.width !== undefined ? { width: made.width } : {}),
481
+ ...(made.height !== undefined ? { height: made.height } : {}),
482
+ variantOf: new Reference<File>(tables.File.name, file.id),
483
+ scope: file.scope,
484
+ });
485
+ await driver.createFile(variant, made.bytes.toString('base64'));
486
+
487
+ const named = await system.runTransaction(async () => {
488
+ const current = await system.get(tables.File, { id: file.id });
489
+ if (current?.[kind]?._id) {
490
+ return current[kind]!._id!;
491
+ }
492
+ await system.update(tables.File, { id: file.id, [kind]: new Reference<File>(tables.File.name, variant.id) });
493
+ return variant.id;
494
+ });
495
+ if (named !== variant.id) {
496
+ // Outside the transaction: the row's delete takes the bytes with it (the delete watcher), a
497
+ // side effect that must not ride a transaction the runner may retry.
498
+ await system.delete(tables.File, { id: variant.id });
499
+ }
500
+ return named;
501
+ }
502
+
503
+ /**
504
+ * The row forgets each derived File first (nothing serves a copy or a variant the row no longer
505
+ * names), then their rows go — the bytes with them, through the delete watcher: the copy for
506
+ * others and every variant kind, one rule.
507
+ */
508
+ private async dropDerivedFiles(file: File): Promise<void> {
509
+ const seats: Array<'copyForOthers' | FileVariantKind> = ['copyForOthers', ...FILE_VARIANT_KINDS];
510
+ const stale = seats.filter((seat) => !!file[seat]?._id);
511
+ if (stale.length === 0) {
512
+ return;
513
+ }
514
+ const system = getDbAsSystem();
515
+ const forgotten: Partial<File> & { id: string } = { id: file.id };
516
+ for (const seat of stale) {
517
+ forgotten[seat] = null;
518
+ }
519
+ await system.update(tables.File, forgotten);
520
+ for (const seat of stale) {
521
+ await system.delete(tables.File, { id: file[seat]!._id! });
522
+ }
523
+ }
524
+
525
+ /**
526
+ * THE answer a byte door gives a caller who does not get the file's bytes — a row that is not
527
+ * there, one this caller cannot read, or a copy that cannot be made for them: a 404 refusal in
528
+ * the same words every time, never the id, so the answer reveals nothing about existence (the
529
+ * service router sends `{ error: 'File not found' }`, the executor logs it at WARN, the routes
530
+ * send `404 File not found`).
531
+ */
532
+ private static notFound(): ServiceRefusal {
533
+ return new ServiceRefusal(404, 'File not found');
534
+ }
202
535
  }
@@ -1,4 +1,5 @@
1
1
  import { File } from './tables/FileTable';
2
+ import type { FileStorageError } from './FileStorageError';
2
3
 
3
4
  /**
4
5
  * Byte store behind `FileStorage`.
@@ -7,6 +8,17 @@ import { File } from './tables/FileTable';
7
8
  * file's true bytes — for text and binary files alike. Drivers own their at-rest representation,
8
9
  * with one hard requirement: a driver that serves blobs directly to clients (`getSignedUrl`)
9
10
  * MUST store the true bytes at rest, because clients read its objects without any decode step.
11
+ *
12
+ * Error contract: a failed byte operation throws a {@link FileStorageError} — a code, a plain
13
+ * message and the store's HTTP status where it has one — and NEVER the store client's own error
14
+ * object or anything hanging off one (a request config, a request, a response, headers, a signed
15
+ * URL): a client's error holds the request it made, and that request holds the driver's
16
+ * credentials, so an application logging what it caught would write a live credential. Bytes that
17
+ * are not there are reported as the `not-found` code on every read and overwrite, so callers
18
+ * branch on one thing whichever driver is behind them (`deleteFile` alone treats missing bytes as
19
+ * success — see its contract). A driver whose store IS the application's database
20
+ * (`DbFileStorageDriver`) lets the database layer's own errors through unchanged: they are
21
+ * already that layer's owned errors, and a transaction runner must see them to retry.
10
22
  */
11
23
  export interface FileStorageDriver {
12
24
  createFile(file: File, fileData: string): Promise<void>;
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Why a byte operation failed — the closed set a caller branches on.
3
+ *
4
+ * - `not-found`: the stored bytes for that file id are not there.
5
+ * - `precondition-failed`: the store refused a conditional write (the object changed underneath it).
6
+ * - `forbidden`: the store refused the driver's credentials or permissions.
7
+ * - `unavailable`: the store could not be reached or asked to be retried later.
8
+ * - `unknown`: anything else.
9
+ */
10
+ export type FileStorageErrorCode = 'not-found' | 'precondition-failed' | 'forbidden' | 'unavailable' | 'unknown';
11
+
12
+ /**
13
+ * THE error a {@link FileStorageDriver} throws — the only thing that leaves a driver when a byte
14
+ * operation fails.
15
+ *
16
+ * It carries three plain facts and nothing else: a {@link FileStorageErrorCode}, a one-line
17
+ * message, and the HTTP status where the store speaks HTTP. It NEVER carries the store client's own
18
+ * error object, nor anything hanging off one — a request config, a request, a response, headers, a
19
+ * signed URL. A store client's error routinely holds the request it made, and that request holds
20
+ * the driver's credentials (an `Authorization` header); an application that logs the error it
21
+ * caught would write a live credential into its logs. The driver is the only party that talks to
22
+ * the store, so the driver owns what its errors carry, and no caller has to know to strip one.
23
+ *
24
+ * Callers branch on `code` — through {@link FileStorageError.is} / {@link FileStorageError.isNotFound},
25
+ * which read the error's shape rather than its prototype, so they hold across duplicate copies of
26
+ * this package in one process.
27
+ */
28
+ export class FileStorageError extends Error {
29
+ private static readonly NAME = 'FileStorageError';
30
+ private static readonly CODES: FileStorageErrorCode[] = [
31
+ 'not-found',
32
+ 'precondition-failed',
33
+ 'forbidden',
34
+ 'unavailable',
35
+ 'unknown',
36
+ ];
37
+ /** Free text that reaches a message is one line and bounded. */
38
+ private static readonly MAX_DETAIL_CHARS = 300;
39
+
40
+ readonly code: FileStorageErrorCode;
41
+ /** The store's HTTP status, where it has one. */
42
+ readonly status?: number;
43
+
44
+ /**
45
+ * @param code what went wrong, for callers to branch on
46
+ * @param summary the driver's own words for the failed operation (e.g. `getFileData failed for file <id>`)
47
+ * @param options.status the store's HTTP status, where it has one
48
+ * @param options.detail the store's own message text — made plain (see {@link plain}) before it is kept
49
+ */
50
+ constructor(code: FileStorageErrorCode, summary: string, options?: { status?: number; detail?: unknown }) {
51
+ super(FileStorageError.describe(code, summary, options));
52
+ this.name = FileStorageError.NAME;
53
+ Object.setPrototypeOf(this, FileStorageError.prototype);
54
+ this.code = code;
55
+ if (options?.status !== undefined) {
56
+ this.status = options.status;
57
+ }
58
+ }
59
+
60
+ /** Is this a driver's error? Read by shape, so it holds across duplicate copies of this package. */
61
+ static is(error: unknown): error is FileStorageError {
62
+ const candidate = error as { name?: unknown; code?: unknown } | null | undefined;
63
+ return (
64
+ candidate?.name === FileStorageError.NAME &&
65
+ FileStorageError.CODES.includes(candidate.code as FileStorageErrorCode)
66
+ );
67
+ }
68
+
69
+ /** The stored bytes for that file id are not there. */
70
+ static isNotFound(error: unknown): boolean {
71
+ return FileStorageError.is(error) && error.code === 'not-found';
72
+ }
73
+
74
+ /**
75
+ * The code an HTTP status means. Drivers whose store speaks HTTP share this table so one status
76
+ * never reads two ways.
77
+ */
78
+ static codeForStatus(status: number | undefined): FileStorageErrorCode {
79
+ if (status === 404) {
80
+ return 'not-found';
81
+ }
82
+ if (status === 412) {
83
+ return 'precondition-failed';
84
+ }
85
+ if (status === 401 || status === 403) {
86
+ return 'forbidden';
87
+ }
88
+ if (status === 408 || status === 429 || (status !== undefined && status >= 500 && status <= 599)) {
89
+ return 'unavailable';
90
+ }
91
+ return 'unknown';
92
+ }
93
+
94
+ private static describe(
95
+ code: FileStorageErrorCode,
96
+ summary: string,
97
+ options?: { status?: number; detail?: unknown }
98
+ ): string {
99
+ const status = options?.status !== undefined ? ` (HTTP ${options.status})` : '';
100
+ const detail = FileStorageError.plain(options?.detail);
101
+ return `${summary}: ${code}${status}${detail ? ` — ${detail}` : ''}`;
102
+ }
103
+
104
+ /**
105
+ * A store's message text made safe to keep: text only (an object is never read into it), one
106
+ * line, bounded, with every URL's query string dropped (a signed URL's signature lives there)
107
+ * and any bearer credential masked.
108
+ */
109
+ private static plain(detail: unknown): string {
110
+ if (typeof detail !== 'string') {
111
+ return '';
112
+ }
113
+
114
+ const oneLine = detail
115
+ .replace(/\s+/g, ' ')
116
+ .replace(/(https?:\/\/[^\s?#]+)[?#]\S*/gi, '$1')
117
+ .replace(/\b(bearer|basic)\s+\S+/gi, '$1 [masked]')
118
+ .trim();
119
+ return oneLine.length > FileStorageError.MAX_DETAIL_CHARS
120
+ ? `${oneLine.slice(0, FileStorageError.MAX_DETAIL_CHARS)}…`
121
+ : oneLine;
122
+ }
123
+ }