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

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 +445 -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 +135 -11
  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,393 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest'
2
- import { image, file } from '../src/fields/index.js'
3
- import type { ImageMetadata, FileMetadata } from '../src/config/types.js'
4
-
5
- /**
6
- * Field-level behaviour of image()/file() in multi-column (Keystone-parity)
7
- * mode, plus the no-re-upload guarantee in BOTH modes. See ADR-0006 / issue #477.
8
- */
9
-
10
- /** A File-like stub with an arrayBuffer() method (triggers an upload). */
11
- function fakeFile(bytes = [1, 2, 3]): File {
12
- return {
13
- name: 'photo.png',
14
- type: 'image/png',
15
- arrayBuffer: async () => new Uint8Array(bytes).buffer,
16
- } as unknown as File
17
- }
18
-
19
- /** A context whose storage records every upload call so we can assert on it. */
20
- function makeContext() {
21
- const uploadImage = vi.fn(async (): Promise<ImageMetadata> => ({
22
- filename: 'new.png',
23
- originalFilename: 'photo.png',
24
- url: '/uploads/new.png',
25
- mimeType: 'image/png',
26
- size: 3,
27
- width: 10,
28
- height: 10,
29
- uploadedAt: new Date().toISOString(),
30
- storageProvider: 'images',
31
- }))
32
- const uploadFile = vi.fn(async (): Promise<FileMetadata> => ({
33
- filename: 'new.pdf',
34
- originalFilename: 'doc.pdf',
35
- url: '/uploads/new.pdf',
36
- mimeType: 'application/pdf',
37
- size: 3,
38
- uploadedAt: new Date().toISOString(),
39
- storageProvider: 'documents',
40
- }))
41
- return {
42
- context: { storage: { uploadImage, uploadFile, deleteImage: vi.fn(), deleteFile: vi.fn() } },
43
- uploadImage,
44
- uploadFile,
45
- }
46
- }
47
-
48
- describe('image() / file() multi-column mode', () => {
49
- describe('single-Json? default is unchanged', () => {
50
- it('image() default has no multi-column methods and emits Json?', () => {
51
- const field = image({ storage: 'images' })
52
- expect(field.getPrismaColumns).toBeUndefined()
53
- expect(field.getColumnNames).toBeUndefined()
54
- expect(field.assembleColumns).toBeUndefined()
55
- expect(field.splitColumns).toBeUndefined()
56
- expect(field.getPrismaType?.('image')).toEqual({ type: 'Json', modifiers: '?' })
57
- })
58
-
59
- it('file() default has no multi-column methods and emits Json?', () => {
60
- const field = file({ storage: 'documents' })
61
- expect(field.getPrismaColumns).toBeUndefined()
62
- expect(field.getPrismaType?.('doc')).toEqual({ type: 'Json', modifiers: '?' })
63
- })
64
- })
65
-
66
- describe('multi-column emission', () => {
67
- it('image() in keystone mode emits seven @map-ped nullable columns', () => {
68
- const field = image({ storage: 'images', db: { columns: 'keystone' } })
69
- const columns = field.getPrismaColumns?.('image')
70
- expect(columns).toHaveLength(7)
71
- expect(columns?.every((c) => c.modifiers === '?')).toBe(true)
72
- expect(columns?.map((c) => c.map)).toEqual([
73
- 'image_url',
74
- 'image_width',
75
- 'image_height',
76
- 'image_filesize',
77
- 'image_contentType',
78
- 'image_contentDisposition',
79
- 'image_pathname',
80
- ])
81
- expect(field.getColumnNames?.('image')).toHaveLength(7)
82
- })
83
-
84
- it('file() in keystone mode emits three @map-ped nullable columns', () => {
85
- const field = file({ storage: 'documents', db: { columns: 'keystone' } })
86
- const columns = field.getPrismaColumns?.('doc')
87
- expect(columns).toHaveLength(3)
88
- expect(columns?.every((c) => c.modifiers === '?')).toBe(true)
89
- expect(columns?.map((c) => c.map)).toEqual(['doc_filename', 'doc_filesize', 'doc_url'])
90
- // filesize is Int; filename/url are String.
91
- const byMap = Object.fromEntries((columns ?? []).map((c) => [c.map, c.type]))
92
- expect(byMap.doc_filesize).toBe('Int')
93
- expect(byMap.doc_filename).toBe('String')
94
- expect(byMap.doc_url).toBe('String')
95
- expect(field.getColumnNames?.('doc')).toEqual(['doc_filename', 'doc_filesize', 'doc_url'])
96
- })
97
-
98
- it('file() per-part @map names are configurable', () => {
99
- const field = file({
100
- storage: 'documents',
101
- db: { columns: { mode: 'keystone', map: { url: 'doc_href' } } },
102
- })
103
- const maps = field.getPrismaColumns?.('doc')?.map((c) => c.map)
104
- expect(maps).toContain('doc_href')
105
- // Un-overridden parts keep Keystone defaults.
106
- expect(maps).toContain('doc_filename')
107
- expect(maps).not.toContain('doc_url')
108
- expect(field.getColumnNames?.('doc')).toContain('doc_href')
109
- })
110
-
111
- it('per-part @map names are configurable', () => {
112
- const field = image({
113
- storage: 'images',
114
- db: { columns: { mode: 'keystone', map: { url: 'image_link', pathname: 'image_key' } } },
115
- })
116
- const columns = field.getPrismaColumns?.('image')
117
- const maps = columns?.map((c) => c.map)
118
- expect(maps).toContain('image_link')
119
- expect(maps).toContain('image_key')
120
- // Un-overridden parts keep Keystone defaults.
121
- expect(maps).toContain('image_width')
122
- expect(field.getColumnNames?.('image')).toContain('image_link')
123
- })
124
- })
125
-
126
- describe('assemble (read) and split (write)', () => {
127
- it('image() assembles its columns and a url-only row yields a valid value', () => {
128
- const field = image({ storage: 'images', db: { columns: 'keystone' } })
129
- const full = field.assembleColumns?.('image', {
130
- image_url: '/u/a.jpg',
131
- image_width: 100,
132
- image_height: 50,
133
- image_filesize: 1024,
134
- image_contentType: 'image/jpeg',
135
- image_pathname: 'a.jpg',
136
- }) as ImageMetadata
137
- expect(full.url).toBe('/u/a.jpg')
138
- expect(full.width).toBe(100)
139
- expect(full.storageProvider).toBe('images')
140
-
141
- const partial = field.assembleColumns?.('image', {
142
- image_url: '/u/only.jpg',
143
- }) as ImageMetadata
144
- expect(partial.url).toBe('/u/only.jpg')
145
- expect(partial.width).toBe(0)
146
- })
147
-
148
- it('image() split writes back into the per-part columns', () => {
149
- const field = image({ storage: 'images', db: { columns: 'keystone' } })
150
- const meta: ImageMetadata = {
151
- filename: 'a.jpg',
152
- originalFilename: 'a.jpg',
153
- url: '/u/a.jpg',
154
- mimeType: 'image/jpeg',
155
- size: 1024,
156
- width: 100,
157
- height: 50,
158
- uploadedAt: '',
159
- storageProvider: 'images',
160
- }
161
- const columns = field.splitColumns?.('image', meta)
162
- expect(columns).toMatchObject({
163
- image_url: '/u/a.jpg',
164
- image_width: 100,
165
- image_height: 50,
166
- image_filesize: 1024,
167
- image_contentType: 'image/jpeg',
168
- image_pathname: 'a.jpg',
169
- })
170
- })
171
-
172
- it('file() assembles a fully-populated row into FileMetadata', () => {
173
- const field = file({ storage: 'documents', db: { columns: 'keystone' } })
174
- const meta = field.assembleColumns?.('doc', {
175
- doc_filename: 'report.pdf',
176
- doc_filesize: 4096,
177
- doc_url: 'https://cdn/report.pdf',
178
- }) as FileMetadata
179
- expect(meta.url).toBe('https://cdn/report.pdf')
180
- expect(meta.filename).toBe('report.pdf')
181
- expect(meta.size).toBe(4096)
182
- expect(meta.storageProvider).toBe('documents')
183
- })
184
-
185
- it('file() assembles a partial row (only doc_url) into a valid value', () => {
186
- const field = file({ storage: 'documents', db: { columns: 'keystone' } })
187
- const meta = field.assembleColumns?.('doc', {
188
- doc_url: 'https://cdn/only.pdf',
189
- }) as FileMetadata
190
- expect(meta.url).toBe('https://cdn/only.pdf')
191
- // Missing filename falls back to the URL; missing filesize defaults to 0.
192
- expect(meta.filename).toBe('https://cdn/only.pdf')
193
- expect(meta.size).toBe(0)
194
- })
195
-
196
- it('file() assembles an empty (all-NULL) row to null', () => {
197
- const field = file({ storage: 'documents', db: { columns: 'keystone' } })
198
- expect(
199
- field.assembleColumns?.('doc', { doc_url: null, doc_filename: null, doc_filesize: null }),
200
- ).toBeNull()
201
- })
202
-
203
- it('file() split writes back into the per-part columns', () => {
204
- const field = file({ storage: 'documents', db: { columns: 'keystone' } })
205
- const meta: FileMetadata = {
206
- filename: 'report.pdf',
207
- originalFilename: 'report.pdf',
208
- url: 'https://cdn/report.pdf',
209
- mimeType: 'application/pdf',
210
- size: 4096,
211
- uploadedAt: '',
212
- storageProvider: 'documents',
213
- }
214
- expect(field.splitColumns?.('doc', meta)).toEqual({
215
- doc_filename: 'report.pdf',
216
- doc_filesize: 4096,
217
- doc_url: 'https://cdn/report.pdf',
218
- })
219
- })
220
-
221
- it('file() round-trips a row through assemble → split with custom @map names', () => {
222
- const field = file({
223
- storage: 'documents',
224
- db: { columns: { mode: 'keystone', map: { url: 'doc_href' } } },
225
- })
226
- const row = { doc_filename: 'a.bin', doc_filesize: 12, doc_href: 'https://cdn/a.bin' }
227
- const meta = field.assembleColumns?.('doc', row)
228
- expect((meta as FileMetadata).url).toBe('https://cdn/a.bin')
229
- // Split back lands on the overridden physical column, not doc_url.
230
- expect(field.splitColumns?.('doc', meta)).toEqual(row)
231
- })
232
-
233
- it('file() split of null clears all columns', () => {
234
- const field = file({ storage: 'documents', db: { columns: 'keystone' } })
235
- expect(field.splitColumns?.('doc', null)).toEqual({
236
- doc_filename: null,
237
- doc_filesize: null,
238
- doc_url: null,
239
- })
240
- })
241
- })
242
-
243
- describe('field-level write access is preserved alongside the column split', () => {
244
- // The core write pipeline gates the multi-column split on the field's own
245
- // write access (so a denied multi-column field writes NONE of its per-part
246
- // columns — see packages/core/.../multi-column-read-write.test.ts). For that
247
- // gate to fire, the field builder must surface the user-provided `access` on
248
- // the config (the same object key the single-column path reads). These tests
249
- // lock that wiring for the real image()/file() fields. See ADR-0006 / #477.
250
- it('image() in multi-column mode carries user access AND splitColumns together', () => {
251
- const denyUpdate = () => false
252
- const field = image({
253
- storage: 'images',
254
- db: { columns: 'keystone' },
255
- access: { update: denyUpdate },
256
- })
257
- expect(field.access?.update).toBe(denyUpdate)
258
- // The gate has both inputs it needs: the access object and the splitter.
259
- expect(typeof field.splitColumns).toBe('function')
260
- })
261
-
262
- it('file() in multi-column mode carries user access AND splitColumns together', () => {
263
- const denyCreate = () => false
264
- const field = file({
265
- storage: 'documents',
266
- db: { columns: 'keystone' },
267
- access: { create: denyCreate },
268
- })
269
- expect(field.access?.create).toBe(denyCreate)
270
- expect(typeof field.splitColumns).toBe('function')
271
- })
272
-
273
- it('single-column image() still carries access and has no splitColumns', () => {
274
- const denyUpdate = () => false
275
- const field = image({ storage: 'images', access: { update: denyUpdate } })
276
- expect(field.access?.update).toBe(denyUpdate)
277
- expect(field.splitColumns).toBeUndefined()
278
- })
279
- })
280
-
281
- describe('no-re-upload guarantee', () => {
282
- it('image() does NOT upload when given an existing ImageMetadata (single-Json mode)', async () => {
283
- const field = image({ storage: 'images' })
284
- const { context, uploadImage } = makeContext()
285
- const existing: ImageMetadata = {
286
- filename: 'a.jpg',
287
- originalFilename: 'a.jpg',
288
- url: '/u/a.jpg',
289
- mimeType: 'image/jpeg',
290
- size: 1,
291
- width: 1,
292
- height: 1,
293
- uploadedAt: '',
294
- storageProvider: 'images',
295
- }
296
- const result = await field.hooks?.resolveInput?.({
297
- listKey: 'Post',
298
- fieldKey: 'image',
299
- operation: 'update',
300
- inputData: { image: existing },
301
- item: { image: existing },
302
- resolvedData: { image: existing },
303
- context,
304
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
305
- } as any)
306
- expect(uploadImage).not.toHaveBeenCalled()
307
- expect(result).toBe(existing)
308
- })
309
-
310
- it('image() does NOT upload when given an existing ImageMetadata (multi-column mode)', async () => {
311
- const field = image({ storage: 'images', db: { columns: 'keystone' } })
312
- const { context, uploadImage } = makeContext()
313
- const existing: ImageMetadata = {
314
- filename: 'a.jpg',
315
- originalFilename: 'a.jpg',
316
- url: '/u/a.jpg',
317
- mimeType: 'image/jpeg',
318
- size: 1,
319
- width: 1,
320
- height: 1,
321
- uploadedAt: '',
322
- storageProvider: 'images',
323
- }
324
- const result = await field.hooks?.resolveInput?.({
325
- listKey: 'Post',
326
- fieldKey: 'image',
327
- operation: 'create',
328
- inputData: { image: existing },
329
- item: undefined,
330
- resolvedData: { image: existing },
331
- context,
332
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
333
- } as any)
334
- expect(uploadImage).not.toHaveBeenCalled()
335
- expect(result).toBe(existing)
336
- })
337
-
338
- it('image() DOES upload when given a File-like input', async () => {
339
- const field = image({ storage: 'images', db: { columns: 'keystone' } })
340
- const { context, uploadImage } = makeContext()
341
- const result = await field.hooks?.resolveInput?.({
342
- listKey: 'Post',
343
- fieldKey: 'image',
344
- operation: 'create',
345
- inputData: { image: fakeFile() },
346
- item: undefined,
347
- resolvedData: { image: fakeFile() },
348
- context,
349
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
350
- } as any)
351
- expect(uploadImage).toHaveBeenCalledTimes(1)
352
- expect((result as ImageMetadata).url).toBe('/uploads/new.png')
353
- })
354
-
355
- it('file() does NOT upload existing metadata but DOES upload a File-like input', async () => {
356
- const field = file({ storage: 'documents', db: { columns: 'keystone' } })
357
- const { context, uploadFile } = makeContext()
358
- const existing: FileMetadata = {
359
- filename: 'd.pdf',
360
- originalFilename: 'd.pdf',
361
- url: '/u/d.pdf',
362
- mimeType: 'application/pdf',
363
- size: 1,
364
- uploadedAt: '',
365
- storageProvider: 'documents',
366
- }
367
- const kept = await field.hooks?.resolveInput?.({
368
- listKey: 'Post',
369
- fieldKey: 'doc',
370
- operation: 'update',
371
- inputData: { doc: existing },
372
- item: { doc: existing },
373
- resolvedData: { doc: existing },
374
- context,
375
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
376
- } as any)
377
- expect(uploadFile).not.toHaveBeenCalled()
378
- expect(kept).toBe(existing)
379
-
380
- await field.hooks?.resolveInput?.({
381
- listKey: 'Post',
382
- fieldKey: 'doc',
383
- operation: 'create',
384
- inputData: { doc: fakeFile() },
385
- item: undefined,
386
- resolvedData: { doc: fakeFile() },
387
- context,
388
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
389
- } as any)
390
- expect(uploadFile).toHaveBeenCalledTimes(1)
391
- })
392
- })
393
- })