@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.
- package/CHANGELOG.md +451 -0
- package/README.md +62 -30
- package/dist/.build-hash +1 -0
- package/dist/fields/index.d.ts +9 -8
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/fields/index.js +376 -153
- package/dist/fields/index.js.map +1 -1
- package/dist/providers/local.d.ts +8 -0
- package/dist/providers/local.d.ts.map +1 -1
- package/dist/providers/local.js +56 -5
- package/dist/providers/local.js.map +1 -1
- package/dist/runtime/index.d.ts +29 -0
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +244 -29
- package/dist/runtime/index.js.map +1 -1
- package/dist/utils/asset-references.d.ts +13 -0
- package/dist/utils/asset-references.d.ts.map +1 -0
- package/dist/utils/asset-references.js +40 -0
- package/dist/utils/asset-references.js.map +1 -0
- package/dist/utils/image.d.ts +17 -0
- package/dist/utils/image.d.ts.map +1 -1
- package/dist/utils/image.js +69 -22
- package/dist/utils/image.js.map +1 -1
- package/dist/utils/metadata-trust.d.ts +30 -0
- package/dist/utils/metadata-trust.d.ts.map +1 -0
- package/dist/utils/metadata-trust.js +57 -0
- package/dist/utils/metadata-trust.js.map +1 -0
- package/dist/utils/multi-column.d.ts +7 -7
- package/dist/utils/multi-column.d.ts.map +1 -1
- package/dist/utils/multi-column.js +17 -34
- package/dist/utils/multi-column.js.map +1 -1
- package/dist/utils/upload.d.ts +58 -0
- package/dist/utils/upload.d.ts.map +1 -1
- package/dist/utils/upload.js +144 -14
- package/dist/utils/upload.js.map +1 -1
- package/package.json +20 -9
- package/.turbo/turbo-build.log +0 -4
- package/CLAUDE.md +0 -528
- package/src/config/index.ts +0 -30
- package/src/config/types.ts +0 -92
- package/src/fields/index.ts +0 -531
- package/src/index.ts +0 -14
- package/src/providers/index.ts +0 -1
- package/src/providers/local.ts +0 -79
- package/src/runtime/index.ts +0 -244
- package/src/runtime/registry.ts +0 -106
- package/src/utils/image.ts +0 -95
- package/src/utils/index.ts +0 -3
- package/src/utils/multi-column.ts +0 -425
- package/src/utils/upload.ts +0 -95
- package/tests/field-zod-schema.test.ts +0 -133
- package/tests/image-utils.test.ts +0 -498
- package/tests/local-provider.test.ts +0 -364
- package/tests/multi-column-fields.test.ts +0 -393
- package/tests/multi-column.test.ts +0 -453
- package/tests/opensaas-image-file-write-validation.test.ts +0 -294
- package/tests/provider-registry.test.ts +0 -201
- package/tests/upload-utils.test.ts +0 -313
- package/tsconfig.json +0 -9
- package/vitest.config.ts +0 -14
|
@@ -1,425 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure `metadata ↔ columns` mapper for the multi-column (Keystone-parity) mode
|
|
3
|
-
* of the `image()` / `file()` fields.
|
|
4
|
-
*
|
|
5
|
-
* Keystone stores an image across seven per-part columns and a file across
|
|
6
|
-
* three. To adopt a live Keystone database without a destructive migration (see
|
|
7
|
-
* ADR-0006), the storage fields can map onto those existing columns in place,
|
|
8
|
-
* assembling them into an {@link ImageMetadata} / {@link FileMetadata} on read
|
|
9
|
-
* and splitting a metadata value back into columns on write.
|
|
10
|
-
*
|
|
11
|
-
* This module is intentionally pure: no I/O, no storage-provider calls, no
|
|
12
|
-
* dependency on the field builders. It is unit-tested in isolation
|
|
13
|
-
* (`multi-column.test.ts`) and the read/write hooks call it.
|
|
14
|
-
*/
|
|
15
|
-
import type { FileMetadata, ImageMetadata } from '../config/types.js'
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The logical "parts" of a Keystone image field. Each maps to one physical
|
|
19
|
-
* column (whose physical name is configurable via `@map`).
|
|
20
|
-
*/
|
|
21
|
-
export type ImageColumnPart =
|
|
22
|
-
'url' | 'width' | 'height' | 'filesize' | 'contentType' | 'contentDisposition' | 'pathname'
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The logical "parts" of a Keystone file field. `pathname` and `contentType`
|
|
26
|
-
* are optional Keystone-parity extras (mirroring the equivalent `image()`
|
|
27
|
-
* parts) for projects whose legacy `file` field emits extra columns — e.g. a
|
|
28
|
-
* content-sniffed MIME type distinct from the public `url`, or a storage
|
|
29
|
-
* provider's blob pathname. They are not emitted unless explicitly opted into
|
|
30
|
-
* (see {@link DEFAULT_FILE_COLUMN_PARTS}).
|
|
31
|
-
*/
|
|
32
|
-
export type FileColumnPart = 'filename' | 'filesize' | 'url' | 'pathname' | 'contentType'
|
|
33
|
-
|
|
34
|
-
/** Ordered list of image parts, matching Keystone's column layout. */
|
|
35
|
-
export const IMAGE_COLUMN_PARTS: readonly ImageColumnPart[] = [
|
|
36
|
-
'url',
|
|
37
|
-
'width',
|
|
38
|
-
'height',
|
|
39
|
-
'filesize',
|
|
40
|
-
'contentType',
|
|
41
|
-
'contentDisposition',
|
|
42
|
-
'pathname',
|
|
43
|
-
] as const
|
|
44
|
-
|
|
45
|
-
/** Ordered list of every file part multi-column mode can emit. */
|
|
46
|
-
export const FILE_COLUMN_PARTS: readonly FileColumnPart[] = [
|
|
47
|
-
'filename',
|
|
48
|
-
'filesize',
|
|
49
|
-
'url',
|
|
50
|
-
'pathname',
|
|
51
|
-
'contentType',
|
|
52
|
-
] as const
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The file parts emitted by default (unchanged since multi-column mode was
|
|
56
|
-
* introduced). `pathname` and `contentType` are additive-only extras — a
|
|
57
|
-
* field must explicitly opt into them via `db.columns.parts` to emit those
|
|
58
|
-
* columns, so existing multi-column `file()` configs keep their exact
|
|
59
|
-
* three-column shape.
|
|
60
|
-
*/
|
|
61
|
-
export const DEFAULT_FILE_COLUMN_PARTS: readonly FileColumnPart[] = [
|
|
62
|
-
'filename',
|
|
63
|
-
'filesize',
|
|
64
|
-
'url',
|
|
65
|
-
] as const
|
|
66
|
-
|
|
67
|
-
/** The Prisma scalar type each image part is stored as. */
|
|
68
|
-
const IMAGE_PART_PRISMA_TYPE: Record<ImageColumnPart, 'String' | 'Int'> = {
|
|
69
|
-
url: 'String',
|
|
70
|
-
width: 'Int',
|
|
71
|
-
height: 'Int',
|
|
72
|
-
filesize: 'Int',
|
|
73
|
-
contentType: 'String',
|
|
74
|
-
contentDisposition: 'String',
|
|
75
|
-
pathname: 'String',
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** The Prisma scalar type each file part is stored as. */
|
|
79
|
-
const FILE_PART_PRISMA_TYPE: Record<FileColumnPart, 'String' | 'Int'> = {
|
|
80
|
-
filename: 'String',
|
|
81
|
-
filesize: 'Int',
|
|
82
|
-
url: 'String',
|
|
83
|
-
pathname: 'String',
|
|
84
|
-
contentType: 'String',
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Map of image part → physical column name (the value used in `@map`).
|
|
89
|
-
* Defaults follow Keystone's `<field>_<part>` naming.
|
|
90
|
-
*/
|
|
91
|
-
export type ImageColumnMap = Record<ImageColumnPart, string>
|
|
92
|
-
|
|
93
|
-
export type FileColumnMap = Record<FileColumnPart, string>
|
|
94
|
-
|
|
95
|
-
/** A single physical column to emit for a multi-column field. */
|
|
96
|
-
export interface MultiColumnDescriptor {
|
|
97
|
-
/** The Prisma model field name (the property carrying `@map`). */
|
|
98
|
-
name: string
|
|
99
|
-
/** The Prisma scalar type. */
|
|
100
|
-
type: 'String' | 'Int'
|
|
101
|
-
/** The physical column name used in `@map`. */
|
|
102
|
-
map: string
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Build the default per-part column-name map for an image field, following
|
|
107
|
-
* Keystone's `<field>_<part>` convention.
|
|
108
|
-
*
|
|
109
|
-
* @example
|
|
110
|
-
* keystoneImageColumnMap('image') // { url: 'image_url', width: 'image_width', ... }
|
|
111
|
-
*/
|
|
112
|
-
export function keystoneImageColumnMap(fieldName: string): ImageColumnMap {
|
|
113
|
-
return {
|
|
114
|
-
url: `${fieldName}_url`,
|
|
115
|
-
width: `${fieldName}_width`,
|
|
116
|
-
height: `${fieldName}_height`,
|
|
117
|
-
filesize: `${fieldName}_filesize`,
|
|
118
|
-
contentType: `${fieldName}_contentType`,
|
|
119
|
-
contentDisposition: `${fieldName}_contentDisposition`,
|
|
120
|
-
pathname: `${fieldName}_pathname`,
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Build the default per-part column-name map for a file field.
|
|
126
|
-
*
|
|
127
|
-
* @example
|
|
128
|
-
* keystoneFileColumnMap('file') // { filename: 'file_filename', filesize: 'file_filesize', url: 'file_url' }
|
|
129
|
-
*/
|
|
130
|
-
export function keystoneFileColumnMap(fieldName: string): FileColumnMap {
|
|
131
|
-
return {
|
|
132
|
-
filename: `${fieldName}_filename`,
|
|
133
|
-
filesize: `${fieldName}_filesize`,
|
|
134
|
-
url: `${fieldName}_url`,
|
|
135
|
-
pathname: `${fieldName}_pathname`,
|
|
136
|
-
contentType: `${fieldName}_contentType`,
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Resolve the effective image column map: defaults from {@link keystoneImageColumnMap},
|
|
142
|
-
* with any caller-supplied per-part overrides applied on top.
|
|
143
|
-
*/
|
|
144
|
-
export function resolveImageColumnMap(
|
|
145
|
-
fieldName: string,
|
|
146
|
-
overrides?: Partial<ImageColumnMap>,
|
|
147
|
-
): ImageColumnMap {
|
|
148
|
-
return { ...keystoneImageColumnMap(fieldName), ...overrides }
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** Resolve the effective file column map; see {@link resolveImageColumnMap}. */
|
|
152
|
-
export function resolveFileColumnMap(
|
|
153
|
-
fieldName: string,
|
|
154
|
-
overrides?: Partial<FileColumnMap>,
|
|
155
|
-
): FileColumnMap {
|
|
156
|
-
return { ...keystoneFileColumnMap(fieldName), ...overrides }
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* The Prisma model field name carrying a given image part's column. We name the
|
|
161
|
-
* model field after the physical column itself (i.e. `<field>_url`) so the
|
|
162
|
-
* generated property and its `@map` line up with the live Keystone column.
|
|
163
|
-
*/
|
|
164
|
-
function imagePartFieldName(map: ImageColumnMap, part: ImageColumnPart): string {
|
|
165
|
-
return map[part]
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function filePartFieldName(map: FileColumnMap, part: FileColumnPart): string {
|
|
169
|
-
return map[part]
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Describe the physical columns an image field emits in multi-column mode.
|
|
174
|
-
* Each descriptor's `name` is the Prisma model field name and `map` is the
|
|
175
|
-
* physical column it maps to (here they are identical so the schema reads
|
|
176
|
-
* naturally over the live columns).
|
|
177
|
-
*/
|
|
178
|
-
export function imageColumnDescriptors(map: ImageColumnMap): MultiColumnDescriptor[] {
|
|
179
|
-
return IMAGE_COLUMN_PARTS.map((part) => ({
|
|
180
|
-
name: imagePartFieldName(map, part),
|
|
181
|
-
type: IMAGE_PART_PRISMA_TYPE[part],
|
|
182
|
-
map: map[part],
|
|
183
|
-
}))
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Describe the physical columns a file field emits in multi-column mode.
|
|
188
|
-
* `parts` defaults to {@link DEFAULT_FILE_COLUMN_PARTS} — pass
|
|
189
|
-
* {@link FILE_COLUMN_PARTS} (or a custom subset) to opt into the
|
|
190
|
-
* `pathname`/`contentType` Keystone-parity extras.
|
|
191
|
-
*/
|
|
192
|
-
export function fileColumnDescriptors(
|
|
193
|
-
map: FileColumnMap,
|
|
194
|
-
parts: readonly FileColumnPart[] = DEFAULT_FILE_COLUMN_PARTS,
|
|
195
|
-
): MultiColumnDescriptor[] {
|
|
196
|
-
return parts.map((part) => ({
|
|
197
|
-
name: filePartFieldName(map, part),
|
|
198
|
-
type: FILE_PART_PRISMA_TYPE[part],
|
|
199
|
-
map: map[part],
|
|
200
|
-
}))
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/** The physical column field names an image field owns (for read stripping). */
|
|
204
|
-
export function imageColumnNames(map: ImageColumnMap): string[] {
|
|
205
|
-
return IMAGE_COLUMN_PARTS.map((part) => imagePartFieldName(map, part))
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* The physical column field names a file field owns (for read stripping).
|
|
210
|
-
* `parts` defaults to {@link DEFAULT_FILE_COLUMN_PARTS}, matching
|
|
211
|
-
* {@link fileColumnDescriptors}.
|
|
212
|
-
*/
|
|
213
|
-
export function fileColumnNames(
|
|
214
|
-
map: FileColumnMap,
|
|
215
|
-
parts: readonly FileColumnPart[] = DEFAULT_FILE_COLUMN_PARTS,
|
|
216
|
-
): string[] {
|
|
217
|
-
return parts.map((part) => filePartFieldName(map, part))
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/** Coerce an unknown column value to a string, or `null` when absent/empty. */
|
|
221
|
-
function asString(value: unknown): string | null {
|
|
222
|
-
if (value === null || value === undefined) return null
|
|
223
|
-
if (typeof value === 'string') return value
|
|
224
|
-
return String(value)
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/** Coerce an unknown column value to a number, or `null` when absent/invalid. */
|
|
228
|
-
function asNumber(value: unknown): number | null {
|
|
229
|
-
if (value === null || value === undefined) return null
|
|
230
|
-
if (typeof value === 'number' && Number.isFinite(value)) return value
|
|
231
|
-
if (typeof value === 'string' && value.trim() !== '') {
|
|
232
|
-
const n = Number(value)
|
|
233
|
-
return Number.isFinite(n) ? n : null
|
|
234
|
-
}
|
|
235
|
-
if (typeof value === 'bigint') return Number(value)
|
|
236
|
-
return null
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Assemble the per-part image columns from a database row into an
|
|
241
|
-
* {@link ImageMetadata}.
|
|
242
|
-
*
|
|
243
|
-
* Returns `null` when the row carries no image at all (the `url` column is the
|
|
244
|
-
* authoritative presence signal — Keystone leaves every part NULL for an empty
|
|
245
|
-
* image). Partially-populated rows (e.g. only `image_url`) still assemble into a
|
|
246
|
-
* valid object, with missing scalar parts defaulted (width/height/size → 0).
|
|
247
|
-
*
|
|
248
|
-
* `storageProvider` is supplied by the caller (the field knows which provider it
|
|
249
|
-
* is bound to); legacy columns do not carry it.
|
|
250
|
-
*/
|
|
251
|
-
export function assembleImageMetadata(
|
|
252
|
-
row: Record<string, unknown>,
|
|
253
|
-
map: ImageColumnMap,
|
|
254
|
-
storageProvider: string,
|
|
255
|
-
): ImageMetadata | null {
|
|
256
|
-
const url = asString(row[map.url])
|
|
257
|
-
if (url === null || url === '') {
|
|
258
|
-
return null
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const pathname = asString(row[map.pathname])
|
|
262
|
-
const contentType = asString(row[map.contentType])
|
|
263
|
-
const contentDisposition = asString(row[map.contentDisposition])
|
|
264
|
-
|
|
265
|
-
// Keystone stores the storage key in `pathname`; fall back to the URL when
|
|
266
|
-
// absent so `filename` is always populated.
|
|
267
|
-
const filename = pathname ?? url
|
|
268
|
-
|
|
269
|
-
const metadata: ImageMetadata = {
|
|
270
|
-
filename,
|
|
271
|
-
originalFilename: filename,
|
|
272
|
-
url,
|
|
273
|
-
mimeType: contentType ?? 'application/octet-stream',
|
|
274
|
-
size: asNumber(row[map.filesize]) ?? 0,
|
|
275
|
-
width: asNumber(row[map.width]) ?? 0,
|
|
276
|
-
height: asNumber(row[map.height]) ?? 0,
|
|
277
|
-
uploadedAt: '',
|
|
278
|
-
storageProvider,
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Preserve the Keystone-only content disposition (no first-class metadata
|
|
282
|
-
// field) so a round-trip back to columns does not lose it.
|
|
283
|
-
if (contentDisposition !== null) {
|
|
284
|
-
metadata.metadata = { contentDisposition }
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return metadata
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Split an {@link ImageMetadata} (or `null`) back into the per-part image
|
|
292
|
-
* columns for writing. A `null`/`undefined` metadata clears every column.
|
|
293
|
-
*/
|
|
294
|
-
export function splitImageMetadata(
|
|
295
|
-
metadata: ImageMetadata | null | undefined,
|
|
296
|
-
map: ImageColumnMap,
|
|
297
|
-
): Record<string, string | number | null> {
|
|
298
|
-
if (metadata === null || metadata === undefined) {
|
|
299
|
-
return {
|
|
300
|
-
[map.url]: null,
|
|
301
|
-
[map.width]: null,
|
|
302
|
-
[map.height]: null,
|
|
303
|
-
[map.filesize]: null,
|
|
304
|
-
[map.contentType]: null,
|
|
305
|
-
[map.contentDisposition]: null,
|
|
306
|
-
[map.pathname]: null,
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
const contentDisposition =
|
|
311
|
-
metadata.metadata && typeof metadata.metadata.contentDisposition === 'string'
|
|
312
|
-
? metadata.metadata.contentDisposition
|
|
313
|
-
: null
|
|
314
|
-
|
|
315
|
-
return {
|
|
316
|
-
[map.url]: metadata.url,
|
|
317
|
-
[map.width]: metadata.width ?? null,
|
|
318
|
-
[map.height]: metadata.height ?? null,
|
|
319
|
-
[map.filesize]: metadata.size ?? null,
|
|
320
|
-
[map.contentType]: metadata.mimeType ?? null,
|
|
321
|
-
[map.contentDisposition]: contentDisposition,
|
|
322
|
-
// Keystone's pathname holds the storage key (our `filename`).
|
|
323
|
-
[map.pathname]: metadata.filename ?? null,
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Assemble the per-part file columns from a database row into a
|
|
329
|
-
* {@link FileMetadata}. Returns `null` when no file is present.
|
|
330
|
-
*
|
|
331
|
-
* `parts` defaults to {@link DEFAULT_FILE_COLUMN_PARTS}. When the optional
|
|
332
|
-
* `pathname`/`contentType` extras are included, their column values are
|
|
333
|
-
* preserved via `metadata.metadata.pathname` / `metadata.metadata.contentType`
|
|
334
|
-
* — mirroring how {@link assembleImageMetadata} round-trips
|
|
335
|
-
* `contentDisposition` — since neither is a native top-level `FileMetadata`
|
|
336
|
-
* field (unlike `image()`, `file()`'s `mimeType` is not sourced from a
|
|
337
|
-
* multi-column part).
|
|
338
|
-
*/
|
|
339
|
-
export function assembleFileMetadata(
|
|
340
|
-
row: Record<string, unknown>,
|
|
341
|
-
map: FileColumnMap,
|
|
342
|
-
storageProvider: string,
|
|
343
|
-
parts: readonly FileColumnPart[] = DEFAULT_FILE_COLUMN_PARTS,
|
|
344
|
-
): FileMetadata | null {
|
|
345
|
-
const url = asString(row[map.url])
|
|
346
|
-
const filename = asString(row[map.filename])
|
|
347
|
-
|
|
348
|
-
// Either a URL or a filename signals a present file.
|
|
349
|
-
if ((url === null || url === '') && (filename === null || filename === '')) {
|
|
350
|
-
return null
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
const resolvedFilename = filename ?? url ?? ''
|
|
354
|
-
|
|
355
|
-
const metadata: FileMetadata = {
|
|
356
|
-
filename: resolvedFilename,
|
|
357
|
-
originalFilename: resolvedFilename,
|
|
358
|
-
url: url ?? '',
|
|
359
|
-
mimeType: 'application/octet-stream',
|
|
360
|
-
size: asNumber(row[map.filesize]) ?? 0,
|
|
361
|
-
uploadedAt: '',
|
|
362
|
-
storageProvider,
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
const extra: Record<string, string> = {}
|
|
366
|
-
if (parts.includes('pathname')) {
|
|
367
|
-
const pathname = asString(row[map.pathname])
|
|
368
|
-
if (pathname !== null) extra.pathname = pathname
|
|
369
|
-
}
|
|
370
|
-
if (parts.includes('contentType')) {
|
|
371
|
-
const contentType = asString(row[map.contentType])
|
|
372
|
-
if (contentType !== null) extra.contentType = contentType
|
|
373
|
-
}
|
|
374
|
-
if (Object.keys(extra).length > 0) {
|
|
375
|
-
metadata.metadata = extra
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
return metadata
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* Split a {@link FileMetadata} (or `null`) back into the per-part file columns
|
|
383
|
-
* for writing. A `null`/`undefined` metadata clears every column.
|
|
384
|
-
*
|
|
385
|
-
* `parts` defaults to {@link DEFAULT_FILE_COLUMN_PARTS}. When the optional
|
|
386
|
-
* `pathname`/`contentType` extras are included, their values are read from
|
|
387
|
-
* `metadata.metadata.pathname` / `metadata.metadata.contentType` (falling
|
|
388
|
-
* back to `null`, clearing the column, when absent).
|
|
389
|
-
*/
|
|
390
|
-
export function splitFileMetadata(
|
|
391
|
-
metadata: FileMetadata | null | undefined,
|
|
392
|
-
map: FileColumnMap,
|
|
393
|
-
parts: readonly FileColumnPart[] = DEFAULT_FILE_COLUMN_PARTS,
|
|
394
|
-
): Record<string, string | number | null> {
|
|
395
|
-
const columns: Record<string, string | number | null> = {
|
|
396
|
-
[map.filename]: null,
|
|
397
|
-
[map.filesize]: null,
|
|
398
|
-
[map.url]: null,
|
|
399
|
-
}
|
|
400
|
-
if (parts.includes('pathname')) columns[map.pathname] = null
|
|
401
|
-
if (parts.includes('contentType')) columns[map.contentType] = null
|
|
402
|
-
|
|
403
|
-
if (metadata === null || metadata === undefined) {
|
|
404
|
-
return columns
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
columns[map.filename] = metadata.filename ?? null
|
|
408
|
-
columns[map.filesize] = metadata.size ?? null
|
|
409
|
-
columns[map.url] = metadata.url ?? null
|
|
410
|
-
|
|
411
|
-
if (parts.includes('pathname')) {
|
|
412
|
-
columns[map.pathname] =
|
|
413
|
-
metadata.metadata && typeof metadata.metadata.pathname === 'string'
|
|
414
|
-
? metadata.metadata.pathname
|
|
415
|
-
: null
|
|
416
|
-
}
|
|
417
|
-
if (parts.includes('contentType')) {
|
|
418
|
-
columns[map.contentType] =
|
|
419
|
-
metadata.metadata && typeof metadata.metadata.contentType === 'string'
|
|
420
|
-
? metadata.metadata.contentType
|
|
421
|
-
: null
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
return columns
|
|
425
|
-
}
|
package/src/utils/upload.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import mime from 'mime-types'
|
|
2
|
-
|
|
3
|
-
export interface FileValidationOptions {
|
|
4
|
-
/** Maximum file size in bytes */
|
|
5
|
-
maxFileSize?: number
|
|
6
|
-
/** Accepted MIME types (e.g., ['image/jpeg', 'image/png']) */
|
|
7
|
-
acceptedMimeTypes?: string[]
|
|
8
|
-
/** Accepted file extensions (e.g., ['.jpg', '.png']) */
|
|
9
|
-
acceptedExtensions?: string[]
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface FileValidationResult {
|
|
13
|
-
valid: boolean
|
|
14
|
-
error?: string
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function validateFile(
|
|
18
|
-
file: { size: number; name: string; type: string },
|
|
19
|
-
options?: FileValidationOptions,
|
|
20
|
-
): FileValidationResult {
|
|
21
|
-
if (!options) {
|
|
22
|
-
return { valid: true }
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (options.maxFileSize && file.size > options.maxFileSize) {
|
|
26
|
-
return {
|
|
27
|
-
valid: false,
|
|
28
|
-
error: `File size exceeds maximum allowed size of ${formatFileSize(options.maxFileSize)}`,
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (options.acceptedMimeTypes && options.acceptedMimeTypes.length > 0) {
|
|
33
|
-
const fileMimeType = file.type || mime.lookup(file.name) || ''
|
|
34
|
-
if (!options.acceptedMimeTypes.includes(fileMimeType)) {
|
|
35
|
-
return {
|
|
36
|
-
valid: false,
|
|
37
|
-
error: `File type '${fileMimeType}' is not allowed. Accepted types: ${options.acceptedMimeTypes.join(', ')}`,
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (options.acceptedExtensions && options.acceptedExtensions.length > 0) {
|
|
43
|
-
const ext = file.name.substring(file.name.lastIndexOf('.')).toLowerCase()
|
|
44
|
-
if (!options.acceptedExtensions.includes(ext)) {
|
|
45
|
-
return {
|
|
46
|
-
valid: false,
|
|
47
|
-
error: `File extension '${ext}' is not allowed. Accepted extensions: ${options.acceptedExtensions.join(', ')}`,
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return { valid: true }
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function formatFileSize(bytes: number): string {
|
|
56
|
-
if (bytes === 0) return '0 Bytes'
|
|
57
|
-
|
|
58
|
-
const k = 1024
|
|
59
|
-
const sizes = ['Bytes', 'KB', 'MB', 'GB']
|
|
60
|
-
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
61
|
-
|
|
62
|
-
return `${Math.round((bytes / Math.pow(k, i)) * 100) / 100} ${sizes[i]}`
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function getMimeType(filename: string): string {
|
|
66
|
-
return mime.lookup(filename) || 'application/octet-stream'
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export interface FileInfo {
|
|
70
|
-
name: string
|
|
71
|
-
size: number
|
|
72
|
-
type: string
|
|
73
|
-
lastModified?: number
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export async function fileToBuffer(file: Blob | File): Promise<Buffer> {
|
|
77
|
-
const arrayBuffer = await file.arrayBuffer()
|
|
78
|
-
return Buffer.from(arrayBuffer)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Utility for developers to use in their own upload routes. */
|
|
82
|
-
export async function parseFileFromFormData(
|
|
83
|
-
formData: FormData,
|
|
84
|
-
fieldName: string = 'file',
|
|
85
|
-
): Promise<{ file: File; buffer: Buffer } | null> {
|
|
86
|
-
const file = formData.get(fieldName)
|
|
87
|
-
|
|
88
|
-
if (!file || !(file instanceof File)) {
|
|
89
|
-
return null
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const buffer = await fileToBuffer(file)
|
|
93
|
-
|
|
94
|
-
return { file, buffer }
|
|
95
|
-
}
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { z } from 'zod'
|
|
3
|
-
import { image, file } from '../src/fields/index.js'
|
|
4
|
-
import type { ImageMetadata, FileMetadata } from '../src/config/types.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Regression tests for issue #618: file()/image() fields were erroneously
|
|
8
|
-
* required on create/update because getZodSchema() returned
|
|
9
|
-
* `z.union([metadataSchema, z.null(), z.undefined()])`. In Zod 4, a union that
|
|
10
|
-
* merely accepts an `undefined` *value* does NOT make the object KEY optional —
|
|
11
|
-
* an omitted key fails with "expected nonoptional, received undefined". The fix
|
|
12
|
-
* returns `metadataSchema.nullish()` so the key is genuinely optional.
|
|
13
|
-
*
|
|
14
|
-
* Core composes create/update input via `z.object({ <field>: getZodSchema(...) })`
|
|
15
|
-
* with no extra wrapping (see packages/core/src/validation/schema.ts), so these
|
|
16
|
-
* tests reproduce that composition exactly.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/** A correctly-shaped ImageMetadata value. */
|
|
20
|
-
const validImageMetadata: ImageMetadata = {
|
|
21
|
-
filename: 'photo.png',
|
|
22
|
-
originalFilename: 'photo.png',
|
|
23
|
-
url: '/uploads/photo.png',
|
|
24
|
-
mimeType: 'image/png',
|
|
25
|
-
size: 1234,
|
|
26
|
-
width: 100,
|
|
27
|
-
height: 100,
|
|
28
|
-
uploadedAt: new Date().toISOString(),
|
|
29
|
-
storageProvider: 'images',
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** A correctly-shaped FileMetadata value. */
|
|
33
|
-
const validFileMetadata: FileMetadata = {
|
|
34
|
-
filename: 'doc.pdf',
|
|
35
|
-
originalFilename: 'doc.pdf',
|
|
36
|
-
url: '/uploads/doc.pdf',
|
|
37
|
-
mimeType: 'application/pdf',
|
|
38
|
-
size: 4096,
|
|
39
|
-
uploadedAt: new Date().toISOString(),
|
|
40
|
-
storageProvider: 'documents',
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** Wrap a single field schema the way core's generateZodSchema does. */
|
|
44
|
-
function objectFor(
|
|
45
|
-
field: { getZodSchema?: (name: string, op: 'create' | 'update') => z.ZodTypeAny },
|
|
46
|
-
fieldName: string,
|
|
47
|
-
operation: 'create' | 'update',
|
|
48
|
-
): z.ZodObject<Record<string, z.ZodTypeAny>> {
|
|
49
|
-
const schema = field.getZodSchema?.(fieldName, operation)
|
|
50
|
-
if (!schema) throw new Error('field has no getZodSchema')
|
|
51
|
-
return z.object({ [fieldName]: schema })
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
describe('image()/file() getZodSchema key-optionality (issue #618)', () => {
|
|
55
|
-
for (const operation of ['create', 'update'] as const) {
|
|
56
|
-
describe(`${operation}`, () => {
|
|
57
|
-
it(`image(): an OMITTED field validates (no key present)`, () => {
|
|
58
|
-
const obj = objectFor(image({ storage: 'images' }), 'avatar', operation)
|
|
59
|
-
const result = obj.safeParse({})
|
|
60
|
-
expect(result.success).toBe(true)
|
|
61
|
-
if (result.success) {
|
|
62
|
-
// Omitted optional key reads back as undefined.
|
|
63
|
-
expect(result.data.avatar).toBeUndefined()
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
it(`file(): an OMITTED field validates (no key present)`, () => {
|
|
68
|
-
const obj = objectFor(file({ storage: 'documents' }), 'resume', operation)
|
|
69
|
-
const result = obj.safeParse({})
|
|
70
|
-
expect(result.success).toBe(true)
|
|
71
|
-
if (result.success) {
|
|
72
|
-
expect(result.data.resume).toBeUndefined()
|
|
73
|
-
}
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
it(`image(): explicit null validates`, () => {
|
|
77
|
-
const obj = objectFor(image({ storage: 'images' }), 'avatar', operation)
|
|
78
|
-
expect(obj.safeParse({ avatar: null }).success).toBe(true)
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
it(`file(): explicit null validates`, () => {
|
|
82
|
-
const obj = objectFor(file({ storage: 'documents' }), 'resume', operation)
|
|
83
|
-
expect(obj.safeParse({ resume: null }).success).toBe(true)
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
it(`image(): explicit undefined validates`, () => {
|
|
87
|
-
const obj = objectFor(image({ storage: 'images' }), 'avatar', operation)
|
|
88
|
-
expect(obj.safeParse({ avatar: undefined }).success).toBe(true)
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
it(`file(): explicit undefined validates`, () => {
|
|
92
|
-
const obj = objectFor(file({ storage: 'documents' }), 'resume', operation)
|
|
93
|
-
expect(obj.safeParse({ resume: undefined }).success).toBe(true)
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
it(`image(): null/omitted validates in multi-column (keystone) mode`, () => {
|
|
97
|
-
const field = image({ storage: 'images', db: { columns: 'keystone' } })
|
|
98
|
-
const obj = objectFor(field, 'avatar', operation)
|
|
99
|
-
expect(obj.safeParse({}).success).toBe(true)
|
|
100
|
-
expect(obj.safeParse({ avatar: null }).success).toBe(true)
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
it(`file(): null/omitted validates in multi-column (keystone) mode`, () => {
|
|
104
|
-
const field = file({ storage: 'documents', db: { columns: 'keystone' } })
|
|
105
|
-
const obj = objectFor(field, 'resume', operation)
|
|
106
|
-
expect(obj.safeParse({}).success).toBe(true)
|
|
107
|
-
expect(obj.safeParse({ resume: null }).success).toBe(true)
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
it(`image(): a correctly-shaped metadata object validates`, () => {
|
|
111
|
-
const obj = objectFor(image({ storage: 'images' }), 'avatar', operation)
|
|
112
|
-
expect(obj.safeParse({ avatar: validImageMetadata }).success).toBe(true)
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
it(`file(): a correctly-shaped metadata object validates`, () => {
|
|
116
|
-
const obj = objectFor(file({ storage: 'documents' }), 'resume', operation)
|
|
117
|
-
expect(obj.safeParse({ resume: validFileMetadata }).success).toBe(true)
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
it(`image(): a malformed metadata object still fails`, () => {
|
|
121
|
-
const obj = objectFor(image({ storage: 'images' }), 'avatar', operation)
|
|
122
|
-
// Missing required sub-fields (width/height/size/...).
|
|
123
|
-
expect(obj.safeParse({ avatar: { filename: 'x.png' } }).success).toBe(false)
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
it(`file(): a malformed metadata object still fails`, () => {
|
|
127
|
-
const obj = objectFor(file({ storage: 'documents' }), 'resume', operation)
|
|
128
|
-
// size should be a number, not a string.
|
|
129
|
-
expect(obj.safeParse({ resume: { ...validFileMetadata, size: 'big' } }).success).toBe(false)
|
|
130
|
-
})
|
|
131
|
-
})
|
|
132
|
-
}
|
|
133
|
-
})
|