@opensaas/stack-storage 0.43.0 → 0.44.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/CHANGELOG.md +451 -0
  2. package/README.md +62 -30
  3. package/dist/.build-hash +1 -0
  4. package/dist/fields/index.d.ts +9 -8
  5. package/dist/fields/index.d.ts.map +1 -1
  6. package/dist/fields/index.js +376 -153
  7. package/dist/fields/index.js.map +1 -1
  8. package/dist/providers/local.d.ts +8 -0
  9. package/dist/providers/local.d.ts.map +1 -1
  10. package/dist/providers/local.js +56 -5
  11. package/dist/providers/local.js.map +1 -1
  12. package/dist/runtime/index.d.ts +29 -0
  13. package/dist/runtime/index.d.ts.map +1 -1
  14. package/dist/runtime/index.js +244 -29
  15. package/dist/runtime/index.js.map +1 -1
  16. package/dist/utils/asset-references.d.ts +13 -0
  17. package/dist/utils/asset-references.d.ts.map +1 -0
  18. package/dist/utils/asset-references.js +40 -0
  19. package/dist/utils/asset-references.js.map +1 -0
  20. package/dist/utils/image.d.ts +17 -0
  21. package/dist/utils/image.d.ts.map +1 -1
  22. package/dist/utils/image.js +69 -22
  23. package/dist/utils/image.js.map +1 -1
  24. package/dist/utils/metadata-trust.d.ts +30 -0
  25. package/dist/utils/metadata-trust.d.ts.map +1 -0
  26. package/dist/utils/metadata-trust.js +57 -0
  27. package/dist/utils/metadata-trust.js.map +1 -0
  28. package/dist/utils/multi-column.d.ts +7 -7
  29. package/dist/utils/multi-column.d.ts.map +1 -1
  30. package/dist/utils/multi-column.js +17 -34
  31. package/dist/utils/multi-column.js.map +1 -1
  32. package/dist/utils/upload.d.ts +58 -0
  33. package/dist/utils/upload.d.ts.map +1 -1
  34. package/dist/utils/upload.js +144 -14
  35. package/dist/utils/upload.js.map +1 -1
  36. package/package.json +20 -9
  37. package/.turbo/turbo-build.log +0 -4
  38. package/CLAUDE.md +0 -528
  39. package/src/config/index.ts +0 -30
  40. package/src/config/types.ts +0 -92
  41. package/src/fields/index.ts +0 -531
  42. package/src/index.ts +0 -14
  43. package/src/providers/index.ts +0 -1
  44. package/src/providers/local.ts +0 -79
  45. package/src/runtime/index.ts +0 -244
  46. package/src/runtime/registry.ts +0 -106
  47. package/src/utils/image.ts +0 -95
  48. package/src/utils/index.ts +0 -3
  49. package/src/utils/multi-column.ts +0 -425
  50. package/src/utils/upload.ts +0 -95
  51. package/tests/field-zod-schema.test.ts +0 -133
  52. package/tests/image-utils.test.ts +0 -498
  53. package/tests/local-provider.test.ts +0 -364
  54. package/tests/multi-column-fields.test.ts +0 -393
  55. package/tests/multi-column.test.ts +0 -453
  56. package/tests/opensaas-image-file-write-validation.test.ts +0 -294
  57. package/tests/provider-registry.test.ts +0 -201
  58. package/tests/upload-utils.test.ts +0 -313
  59. package/tsconfig.json +0 -9
  60. package/vitest.config.ts +0 -14
@@ -1,453 +0,0 @@
1
- import { describe, it, expect } from 'vitest'
2
- import {
3
- IMAGE_COLUMN_PARTS,
4
- FILE_COLUMN_PARTS,
5
- DEFAULT_FILE_COLUMN_PARTS,
6
- keystoneImageColumnMap,
7
- keystoneFileColumnMap,
8
- resolveImageColumnMap,
9
- resolveFileColumnMap,
10
- imageColumnDescriptors,
11
- fileColumnDescriptors,
12
- imageColumnNames,
13
- fileColumnNames,
14
- assembleImageMetadata,
15
- splitImageMetadata,
16
- assembleFileMetadata,
17
- splitFileMetadata,
18
- } from '../src/utils/multi-column.js'
19
- import type { ImageMetadata, FileMetadata } from '../src/config/types.js'
20
-
21
- describe('multi-column mapper', () => {
22
- describe('column-name maps', () => {
23
- it('builds Keystone default image column names', () => {
24
- expect(keystoneImageColumnMap('image')).toEqual({
25
- url: 'image_url',
26
- width: 'image_width',
27
- height: 'image_height',
28
- filesize: 'image_filesize',
29
- contentType: 'image_contentType',
30
- contentDisposition: 'image_contentDisposition',
31
- pathname: 'image_pathname',
32
- })
33
- })
34
-
35
- it('builds Keystone default file column names', () => {
36
- expect(keystoneFileColumnMap('doc')).toEqual({
37
- filename: 'doc_filename',
38
- filesize: 'doc_filesize',
39
- url: 'doc_url',
40
- pathname: 'doc_pathname',
41
- contentType: 'doc_contentType',
42
- })
43
- })
44
-
45
- it('applies per-part image overrides on top of defaults', () => {
46
- const map = resolveImageColumnMap('avatar', { url: 'avatar_link', width: 'avatar_w' })
47
- expect(map.url).toBe('avatar_link')
48
- expect(map.width).toBe('avatar_w')
49
- // Un-overridden parts keep the Keystone default.
50
- expect(map.height).toBe('avatar_height')
51
- expect(map.pathname).toBe('avatar_pathname')
52
- })
53
-
54
- it('applies per-part file overrides on top of defaults', () => {
55
- const map = resolveFileColumnMap('resume', { url: 'resume_href' })
56
- expect(map.url).toBe('resume_href')
57
- expect(map.filename).toBe('resume_filename')
58
- })
59
- })
60
-
61
- describe('descriptors / column names', () => {
62
- it('emits seven image columns with @map names', () => {
63
- const map = keystoneImageColumnMap('image')
64
- const descriptors = imageColumnDescriptors(map)
65
- expect(descriptors).toHaveLength(7)
66
- expect(descriptors.map((d) => d.map)).toEqual([
67
- 'image_url',
68
- 'image_width',
69
- 'image_height',
70
- 'image_filesize',
71
- 'image_contentType',
72
- 'image_contentDisposition',
73
- 'image_pathname',
74
- ])
75
- // width/height/filesize are Int, the rest String.
76
- const byName = Object.fromEntries(descriptors.map((d) => [d.map, d.type]))
77
- expect(byName.image_width).toBe('Int')
78
- expect(byName.image_height).toBe('Int')
79
- expect(byName.image_filesize).toBe('Int')
80
- expect(byName.image_url).toBe('String')
81
- expect(byName.image_contentType).toBe('String')
82
- })
83
-
84
- it('emits three file columns by default (no regression)', () => {
85
- const descriptors = fileColumnDescriptors(keystoneFileColumnMap('doc'))
86
- expect(descriptors.map((d) => d.map)).toEqual(['doc_filename', 'doc_filesize', 'doc_url'])
87
- })
88
-
89
- it('opts into pathname/contentType extras via an explicit parts list', () => {
90
- const map = keystoneFileColumnMap('doc')
91
- const descriptors = fileColumnDescriptors(map, FILE_COLUMN_PARTS)
92
- expect(descriptors.map((d) => d.map)).toEqual([
93
- 'doc_filename',
94
- 'doc_filesize',
95
- 'doc_url',
96
- 'doc_pathname',
97
- 'doc_contentType',
98
- ])
99
- const byName = Object.fromEntries(descriptors.map((d) => [d.map, d.type]))
100
- expect(byName.doc_pathname).toBe('String')
101
- expect(byName.doc_contentType).toBe('String')
102
- })
103
-
104
- it('lists column names for read stripping', () => {
105
- expect(imageColumnNames(keystoneImageColumnMap('image'))).toHaveLength(7)
106
- expect(fileColumnNames(keystoneFileColumnMap('doc'))).toEqual([
107
- 'doc_filename',
108
- 'doc_filesize',
109
- 'doc_url',
110
- ])
111
- expect(fileColumnNames(keystoneFileColumnMap('doc'), ['pathname', 'contentType'])).toEqual([
112
- 'doc_pathname',
113
- 'doc_contentType',
114
- ])
115
- expect(IMAGE_COLUMN_PARTS).toHaveLength(7)
116
- expect(FILE_COLUMN_PARTS).toHaveLength(5)
117
- expect(DEFAULT_FILE_COLUMN_PARTS).toEqual(['filename', 'filesize', 'url'])
118
- })
119
- })
120
-
121
- describe('image assemble/split round-trip', () => {
122
- const map = keystoneImageColumnMap('image')
123
-
124
- it('assembles a fully-populated row into ImageMetadata', () => {
125
- const row = {
126
- image_url: 'https://cdn.example.com/photo.jpg',
127
- image_width: 800,
128
- image_height: 600,
129
- image_filesize: 12345,
130
- image_contentType: 'image/jpeg',
131
- image_contentDisposition: 'inline',
132
- image_pathname: 'photo.jpg',
133
- }
134
- const meta = assembleImageMetadata(row, map, 'images')
135
- expect(meta).toEqual({
136
- filename: 'photo.jpg',
137
- originalFilename: 'photo.jpg',
138
- url: 'https://cdn.example.com/photo.jpg',
139
- mimeType: 'image/jpeg',
140
- size: 12345,
141
- width: 800,
142
- height: 600,
143
- uploadedAt: '',
144
- storageProvider: 'images',
145
- metadata: { contentDisposition: 'inline' },
146
- })
147
- })
148
-
149
- it('round-trips columns → metadata → columns', () => {
150
- const row = {
151
- image_url: 'https://cdn.example.com/photo.jpg',
152
- image_width: 800,
153
- image_height: 600,
154
- image_filesize: 12345,
155
- image_contentType: 'image/jpeg',
156
- image_contentDisposition: 'inline',
157
- image_pathname: 'photo.jpg',
158
- }
159
- const meta = assembleImageMetadata(row, map, 'images')
160
- const columns = splitImageMetadata(meta, map)
161
- expect(columns).toEqual(row)
162
- })
163
-
164
- it('assembles a partially-populated row (only image_url) into a valid value', () => {
165
- const meta = assembleImageMetadata(
166
- { image_url: 'https://cdn.example.com/only.png' },
167
- map,
168
- 'images',
169
- )
170
- expect(meta).not.toBeNull()
171
- expect(meta?.url).toBe('https://cdn.example.com/only.png')
172
- // Missing pathname falls back to the URL for filename.
173
- expect(meta?.filename).toBe('https://cdn.example.com/only.png')
174
- // Missing scalar parts default to 0.
175
- expect(meta?.width).toBe(0)
176
- expect(meta?.height).toBe(0)
177
- expect(meta?.size).toBe(0)
178
- // Missing contentType defaults to octet-stream; no contentDisposition metadata.
179
- expect(meta?.mimeType).toBe('application/octet-stream')
180
- expect(meta?.metadata).toBeUndefined()
181
- })
182
-
183
- it('returns null for a row with no url (empty image)', () => {
184
- expect(assembleImageMetadata({}, map, 'images')).toBeNull()
185
- expect(
186
- assembleImageMetadata(
187
- { image_url: null, image_width: null, image_pathname: null },
188
- map,
189
- 'images',
190
- ),
191
- ).toBeNull()
192
- })
193
-
194
- it('coerces string-typed numeric columns (e.g. from raw SQL) to numbers', () => {
195
- const meta = assembleImageMetadata(
196
- { image_url: 'u', image_width: '640', image_height: '480', image_filesize: '999' },
197
- map,
198
- 'images',
199
- )
200
- expect(meta?.width).toBe(640)
201
- expect(meta?.height).toBe(480)
202
- expect(meta?.size).toBe(999)
203
- })
204
-
205
- it('splits null metadata into all-null columns (clears the field)', () => {
206
- expect(splitImageMetadata(null, map)).toEqual({
207
- image_url: null,
208
- image_width: null,
209
- image_height: null,
210
- image_filesize: null,
211
- image_contentType: null,
212
- image_contentDisposition: null,
213
- image_pathname: null,
214
- })
215
- })
216
-
217
- it('honours per-part @map overrides on round-trip', () => {
218
- const custom = resolveImageColumnMap('image', { url: 'image_link' })
219
- const row = { image_link: 'https://x/y.jpg', image_pathname: 'y.jpg' }
220
- const meta = assembleImageMetadata(row, custom, 'images')
221
- expect(meta?.url).toBe('https://x/y.jpg')
222
- const split = splitImageMetadata(meta, custom)
223
- expect(split.image_link).toBe('https://x/y.jpg')
224
- })
225
- })
226
-
227
- describe('file assemble/split round-trip', () => {
228
- const map = keystoneFileColumnMap('doc')
229
-
230
- it('assembles a populated row into FileMetadata', () => {
231
- const meta = assembleFileMetadata(
232
- { doc_filename: 'report.pdf', doc_filesize: 4096, doc_url: 'https://cdn/report.pdf' },
233
- map,
234
- 'documents',
235
- )
236
- expect(meta).toEqual({
237
- filename: 'report.pdf',
238
- originalFilename: 'report.pdf',
239
- url: 'https://cdn/report.pdf',
240
- mimeType: 'application/octet-stream',
241
- size: 4096,
242
- uploadedAt: '',
243
- storageProvider: 'documents',
244
- })
245
- })
246
-
247
- it('round-trips file columns → metadata → columns', () => {
248
- const row = {
249
- doc_filename: 'report.pdf',
250
- doc_filesize: 4096,
251
- doc_url: 'https://cdn/report.pdf',
252
- }
253
- const meta = assembleFileMetadata(row, map, 'documents')
254
- expect(splitFileMetadata(meta, map)).toEqual(row)
255
- })
256
-
257
- it('assembles a partial row (only url) into a valid value', () => {
258
- const meta = assembleFileMetadata({ doc_url: 'https://cdn/only.pdf' }, map, 'documents')
259
- expect(meta).not.toBeNull()
260
- expect(meta?.url).toBe('https://cdn/only.pdf')
261
- expect(meta?.filename).toBe('https://cdn/only.pdf')
262
- expect(meta?.size).toBe(0)
263
- })
264
-
265
- it('assembles a partial row (only filename) into a valid value', () => {
266
- const meta = assembleFileMetadata({ doc_filename: 'legacy.bin' }, map, 'documents')
267
- expect(meta).not.toBeNull()
268
- expect(meta?.filename).toBe('legacy.bin')
269
- expect(meta?.url).toBe('')
270
- })
271
-
272
- it('returns null for an empty file row', () => {
273
- expect(assembleFileMetadata({}, map, 'documents')).toBeNull()
274
- expect(
275
- assembleFileMetadata({ doc_url: null, doc_filename: null }, map, 'documents'),
276
- ).toBeNull()
277
- })
278
-
279
- it('splits null metadata into all-null columns', () => {
280
- expect(splitFileMetadata(null, map)).toEqual({
281
- doc_filename: null,
282
- doc_filesize: null,
283
- doc_url: null,
284
- })
285
- })
286
-
287
- it('does not read/write pathname/contentType columns when not opted in', () => {
288
- const row = {
289
- doc_filename: 'report.pdf',
290
- doc_filesize: 4096,
291
- doc_url: 'https://cdn/report.pdf',
292
- doc_pathname: 'blobs/report.pdf',
293
- doc_contentType: 'application/pdf',
294
- }
295
- const meta = assembleFileMetadata(row, map, 'documents')
296
- expect(meta?.metadata).toBeUndefined()
297
- expect(splitFileMetadata(meta, map)).toEqual({
298
- doc_filename: 'report.pdf',
299
- doc_filesize: 4096,
300
- doc_url: 'https://cdn/report.pdf',
301
- })
302
- })
303
- })
304
-
305
- describe('file pathname/contentType extras (opt-in)', () => {
306
- const map = keystoneFileColumnMap('doc')
307
- const parts = FILE_COLUMN_PARTS
308
-
309
- it('assembles pathname/contentType columns into metadata.metadata', () => {
310
- const row = {
311
- doc_filename: 'report.pdf',
312
- doc_filesize: 4096,
313
- doc_url: 'https://cdn/report.pdf',
314
- doc_pathname: 'blobs/report.pdf',
315
- doc_contentType: 'application/pdf',
316
- }
317
- const meta = assembleFileMetadata(row, map, 'documents', parts)
318
- expect(meta).toEqual({
319
- filename: 'report.pdf',
320
- originalFilename: 'report.pdf',
321
- url: 'https://cdn/report.pdf',
322
- mimeType: 'application/octet-stream',
323
- size: 4096,
324
- uploadedAt: '',
325
- storageProvider: 'documents',
326
- metadata: { pathname: 'blobs/report.pdf', contentType: 'application/pdf' },
327
- })
328
- })
329
-
330
- it('round-trips columns → metadata → columns with the extras included', () => {
331
- const row = {
332
- doc_filename: 'report.pdf',
333
- doc_filesize: 4096,
334
- doc_url: 'https://cdn/report.pdf',
335
- doc_pathname: 'blobs/report.pdf',
336
- doc_contentType: 'application/pdf',
337
- }
338
- const meta = assembleFileMetadata(row, map, 'documents', parts)
339
- expect(splitFileMetadata(meta, map, parts)).toEqual(row)
340
- })
341
-
342
- it('assembles with only one extra present (contentType absent from metadata)', () => {
343
- const meta = assembleFileMetadata(
344
- {
345
- doc_filename: 'report.pdf',
346
- doc_filesize: 4096,
347
- doc_url: 'https://cdn/report.pdf',
348
- doc_pathname: 'blobs/report.pdf',
349
- },
350
- map,
351
- 'documents',
352
- parts,
353
- )
354
- expect(meta?.metadata).toEqual({ pathname: 'blobs/report.pdf' })
355
- })
356
-
357
- it('assembles with neither extra present — no metadata bag at all', () => {
358
- const meta = assembleFileMetadata(
359
- { doc_filename: 'report.pdf', doc_filesize: 4096, doc_url: 'https://cdn/report.pdf' },
360
- map,
361
- 'documents',
362
- parts,
363
- )
364
- expect(meta?.metadata).toBeUndefined()
365
- })
366
-
367
- it('splits a FileMetadata without the extras into null columns (clearing them)', () => {
368
- const meta = assembleFileMetadata(
369
- { doc_filename: 'report.pdf', doc_filesize: 4096, doc_url: 'https://cdn/report.pdf' },
370
- map,
371
- 'documents',
372
- )
373
- expect(splitFileMetadata(meta, map, parts)).toEqual({
374
- doc_filename: 'report.pdf',
375
- doc_filesize: 4096,
376
- doc_url: 'https://cdn/report.pdf',
377
- doc_pathname: null,
378
- doc_contentType: null,
379
- })
380
- })
381
-
382
- it('splits null metadata into all-null columns including the extras', () => {
383
- expect(splitFileMetadata(null, map, parts)).toEqual({
384
- doc_filename: null,
385
- doc_filesize: null,
386
- doc_url: null,
387
- doc_pathname: null,
388
- doc_contentType: null,
389
- })
390
- })
391
-
392
- it('honours per-part @map overrides for the extras', () => {
393
- const custom = resolveFileColumnMap('doc', { pathname: 'doc_blob_path' })
394
- const row = {
395
- doc_filename: 'report.pdf',
396
- doc_filesize: 4096,
397
- doc_url: 'https://cdn/report.pdf',
398
- doc_blob_path: 'blobs/report.pdf',
399
- doc_contentType: 'application/pdf',
400
- }
401
- const meta = assembleFileMetadata(row, custom, 'documents', parts)
402
- expect(meta?.metadata).toEqual({
403
- pathname: 'blobs/report.pdf',
404
- contentType: 'application/pdf',
405
- })
406
- expect(splitFileMetadata(meta, custom, parts)).toEqual(row)
407
- })
408
- })
409
-
410
- describe('typed round-trips with real metadata objects', () => {
411
- it('image: a freshly-uploaded ImageMetadata splits and re-assembles consistently', () => {
412
- const map = keystoneImageColumnMap('hero')
413
- const uploaded: ImageMetadata = {
414
- filename: 'abc123.webp',
415
- originalFilename: 'hero.webp',
416
- url: '/uploads/abc123.webp',
417
- mimeType: 'image/webp',
418
- size: 2048,
419
- width: 1200,
420
- height: 630,
421
- uploadedAt: new Date().toISOString(),
422
- storageProvider: 'images',
423
- }
424
- const cols = splitImageMetadata(uploaded, map)
425
- const back = assembleImageMetadata(cols, map, 'images')
426
- expect(back?.url).toBe(uploaded.url)
427
- expect(back?.width).toBe(uploaded.width)
428
- expect(back?.height).toBe(uploaded.height)
429
- expect(back?.size).toBe(uploaded.size)
430
- expect(back?.mimeType).toBe(uploaded.mimeType)
431
- // pathname stores the storage key (our filename).
432
- expect(back?.filename).toBe(uploaded.filename)
433
- })
434
-
435
- it('file: a freshly-uploaded FileMetadata splits and re-assembles consistently', () => {
436
- const map = keystoneFileColumnMap('attachment')
437
- const uploaded: FileMetadata = {
438
- filename: 'xyz.pdf',
439
- originalFilename: 'invoice.pdf',
440
- url: '/uploads/xyz.pdf',
441
- mimeType: 'application/pdf',
442
- size: 9999,
443
- uploadedAt: new Date().toISOString(),
444
- storageProvider: 'documents',
445
- }
446
- const cols = splitFileMetadata(uploaded, map)
447
- const back = assembleFileMetadata(cols, map, 'documents')
448
- expect(back?.url).toBe(uploaded.url)
449
- expect(back?.size).toBe(uploaded.size)
450
- expect(back?.filename).toBe(uploaded.filename)
451
- })
452
- })
453
- })