@opensaas/stack-storage 0.30.0 → 0.31.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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +8 -0
- package/dist/config/types.d.ts +5 -1
- package/dist/config/types.d.ts.map +1 -1
- package/dist/providers/local.d.ts.map +1 -1
- package/dist/providers/local.js +8 -1
- package/dist/providers/local.js.map +1 -1
- package/package.json +2 -2
- package/src/config/types.ts +5 -1
- package/src/providers/local.ts +7 -1
- package/tests/local-provider.test.ts +15 -0
- package/tests/opensaas-image-file-write-validation.test.ts +294 -0
package/.turbo/turbo-build.log
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @opensaas/stack-storage
|
|
2
2
|
|
|
3
|
+
## 0.31.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#795](https://github.com/OpenSaasAU/stack/pull/795) [`0e603b9`](https://github.com/OpenSaasAU/stack/commit/0e603b92fd93611c3e7e614c4bc213d1eae1f926) Thanks [@borisno2](https://github.com/borisno2)! - Add integration-style tests proving image()/file() reject unrecognised write value shapes end-to-end in both `db.columns: 'keystone'` (multi-column) and default single-column (JSON) modes, pinning the [#789](https://github.com/OpenSaasAU/stack/issues/789) fix as a regression guard.
|
|
8
|
+
|
|
9
|
+
- [#776](https://github.com/OpenSaasAU/stack/pull/776) [`030d540`](https://github.com/OpenSaasAU/stack/commit/030d540cfb1fc4495da5177cda0bc1915c0cb354) Thanks [@borisno2](https://github.com/borisno2)! - `StorageProvider.delete()` is now documented as idempotent; the local provider swallows `ENOENT` on delete instead of throwing.
|
|
10
|
+
|
|
3
11
|
## 0.30.0
|
|
4
12
|
|
|
5
13
|
## 0.29.0
|
package/dist/config/types.d.ts
CHANGED
|
@@ -18,7 +18,11 @@ export interface StorageProvider {
|
|
|
18
18
|
*/
|
|
19
19
|
download(filename: string): Promise<Buffer>;
|
|
20
20
|
/**
|
|
21
|
-
* Deletes a file from the storage provider
|
|
21
|
+
* Deletes a file from the storage provider.
|
|
22
|
+
*
|
|
23
|
+
* Idempotent: deleting a filename that doesn't exist in the backing store
|
|
24
|
+
* resolves without error. Only non-not-found errors (permissions, network,
|
|
25
|
+
* etc.) should reject.
|
|
22
26
|
* @param filename - Filename to delete
|
|
23
27
|
*/
|
|
24
28
|
delete(filename: string): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/config/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;;OAMG;IACH,MAAM,CACJ,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,YAAY,CAAC,CAAA;IAExB;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAE3C
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/config/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;;OAMG;IACH,MAAM,CACJ,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,YAAY,CAAC,CAAA;IAExB;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IAE3C;;;;;;;OAOG;IACH,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAEvC;;;;OAIG;IACH,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAA;IAEhC;;;;;OAKG;IACH,YAAY,CAAC,C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,yBAAyB,GAC1B,MAAM,+BAA+B,CAAA;AAEtC;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC,6BAA6B;IAC7B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,8BAA8B;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,kEAAkE;IAClE,GAAG,CAAC,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAA;IACzD,+CAA+C;IAC/C,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,CAAA;IACzC,kCAAkC;IAClC,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local.d.ts","sourceRoot":"","sources":["../../src/providers/local.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACb,YAAY,EACZ,kBAAkB,EACnB,MAAM,oBAAoB,CAAA;AAE3B;;;GAGG;AACH,qBAAa,oBAAqB,YAAW,eAAe;IAC1D,OAAO,CAAC,MAAM,CAAoB;IAElC,YAAY,MAAM,EAAE,kBAAkB,EAErC;IAED;;OAEG;YACW,eAAe;IAQ7B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAWlB,MAAM,CACV,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,YAAY,CAAC,CAmBvB;IAEK,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAGhD;IAEK,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"local.d.ts","sourceRoot":"","sources":["../../src/providers/local.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,eAAe,EACf,aAAa,EACb,YAAY,EACZ,kBAAkB,EACnB,MAAM,oBAAoB,CAAA;AAE3B;;;GAGG;AACH,qBAAa,oBAAqB,YAAW,eAAe;IAC1D,OAAO,CAAC,MAAM,CAAoB;IAElC,YAAY,MAAM,EAAE,kBAAkB,EAErC;IAED;;OAEG;YACW,eAAe;IAQ7B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAWlB,MAAM,CACV,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,QAAQ,EAAE,MAAM,EAChB,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,YAAY,CAAC,CAmBvB;IAEK,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAGhD;IAEK,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAS5C;IAED,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAE/B;CACF"}
|
package/dist/providers/local.js
CHANGED
|
@@ -55,7 +55,14 @@ export class LocalStorageProvider {
|
|
|
55
55
|
}
|
|
56
56
|
async delete(filename) {
|
|
57
57
|
const filePath = path.join(this.config.uploadDir, filename);
|
|
58
|
-
|
|
58
|
+
try {
|
|
59
|
+
await fs.unlink(filePath);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error.code !== 'ENOENT') {
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
59
66
|
}
|
|
60
67
|
getUrl(filename) {
|
|
61
68
|
return `${this.config.serveUrl}/${filename}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local.js","sourceRoot":"","sources":["../../src/providers/local.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAA;AACjC,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAQzC;;;GAGG;AACH,MAAM,OAAO,oBAAoB;IACvB,MAAM,CAAoB;IAElC,YA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AC5B,CAAA;IACH,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,QAAgB;QAC7B,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;QAC3D,OAAO,MAAM,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;IACpC,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,QAAgB;QAC3B,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;QAC3D,IAAI,CAAC;YACH,MAAM,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;QAC3B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAK,KAA2B,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBACnD,MAAM,KAAK,CAAA;YACb,CAAC;QACH,CAAC;IACH,CAAC;IAED,MAAM,CAAC,QAAgB;QACrB,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,IAAI,QAAQ,EAAE,CAAA;IAC9C,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opensaas/stack-storage",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "File and image upload field types with pluggable storage providers for OpenSaas Stack",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"@vitest/coverage-v8": "^4.1.10",
|
|
37
37
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
38
38
|
"vitest": "^4.1.10",
|
|
39
|
-
"@opensaas/stack-core": "0.
|
|
39
|
+
"@opensaas/stack-core": "0.31.0"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@opensaas/stack-core": "^0"
|
package/src/config/types.ts
CHANGED
|
@@ -24,7 +24,11 @@ export interface StorageProvider {
|
|
|
24
24
|
download(filename: string): Promise<Buffer>
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* Deletes a file from the storage provider
|
|
27
|
+
* Deletes a file from the storage provider.
|
|
28
|
+
*
|
|
29
|
+
* Idempotent: deleting a filename that doesn't exist in the backing store
|
|
30
|
+
* resolves without error. Only non-not-found errors (permissions, network,
|
|
31
|
+
* etc.) should reject.
|
|
28
32
|
* @param filename - Filename to delete
|
|
29
33
|
*/
|
|
30
34
|
delete(filename: string): Promise<void>
|
package/src/providers/local.ts
CHANGED
|
@@ -76,7 +76,13 @@ export class LocalStorageProvider implements StorageProvider {
|
|
|
76
76
|
|
|
77
77
|
async delete(filename: string): Promise<void> {
|
|
78
78
|
const filePath = path.join(this.config.uploadDir, filename)
|
|
79
|
-
|
|
79
|
+
try {
|
|
80
|
+
await fs.unlink(filePath)
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if ((error as { code?: string }).code !== 'ENOENT') {
|
|
83
|
+
throw error
|
|
84
|
+
}
|
|
85
|
+
}
|
|
80
86
|
}
|
|
81
87
|
|
|
82
88
|
getUrl(filename: string): string {
|
|
@@ -304,6 +304,21 @@ describe('LocalStorageProvider', () => {
|
|
|
304
304
|
|
|
305
305
|
await expect(provider.delete('protected.txt')).rejects.toThrow('Permission denied')
|
|
306
306
|
})
|
|
307
|
+
|
|
308
|
+
it('should resolve without error when deleting a nonexistent file (idempotent)', async () => {
|
|
309
|
+
const config: LocalStorageConfig = {
|
|
310
|
+
type: 'local',
|
|
311
|
+
uploadDir: './uploads',
|
|
312
|
+
serveUrl: '/uploads',
|
|
313
|
+
}
|
|
314
|
+
const provider = new LocalStorageProvider(config)
|
|
315
|
+
const enoent = Object.assign(new Error('ENOENT: no such file or directory'), {
|
|
316
|
+
code: 'ENOENT',
|
|
317
|
+
})
|
|
318
|
+
fs.unlink.mockRejectedValueOnce(enoent)
|
|
319
|
+
|
|
320
|
+
await expect(provider.delete('nonexistent.txt')).resolves.toBeUndefined()
|
|
321
|
+
})
|
|
307
322
|
})
|
|
308
323
|
|
|
309
324
|
describe('getUrl', () => {
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
+
import { ValidationError, type FieldConfig } from '@opensaas/stack-core'
|
|
3
|
+
import { validateWithZod } from '@opensaas/stack-core/internal'
|
|
4
|
+
import { image, file } from '../src/fields/index.js'
|
|
5
|
+
import type { ImageMetadata, FileMetadata } from '../src/config/types.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* End-to-end regression coverage for #789/#790: a wrong-shape write value for
|
|
9
|
+
* an `image()`/`file()` field must be rejected by validation instead of being
|
|
10
|
+
* silently split into null/undefined physical columns.
|
|
11
|
+
*
|
|
12
|
+
* This exercises the REAL field pipeline pieces in the order the Hook
|
|
13
|
+
* Pipeline runs them (see `packages/core/src/context/hook-pipeline.ts`):
|
|
14
|
+
*
|
|
15
|
+
* field `resolveInput` → built-in field rules → `splitColumns`
|
|
16
|
+
*
|
|
17
|
+
* `runValidateFieldRules` below mirrors core's `validateFieldRules`
|
|
18
|
+
* (`packages/core/src/hooks/index.ts`) — itself a thin formatting wrapper
|
|
19
|
+
* over the public `validateWithZod` export — so this test validates against
|
|
20
|
+
* the exact Zod schema (`field.getZodSchema`) Phase 3 of the real pipeline
|
|
21
|
+
* runs, and throws the real `ValidationError` class on failure. `splitColumns`
|
|
22
|
+
* is only invoked when that validation passes, matching the #789 phase order
|
|
23
|
+
* (validate the LOGICAL value before splitting into physical columns).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** A File-like stub with an arrayBuffer() method (triggers an upload). */
|
|
27
|
+
function fakeFile(bytes = [1, 2, 3]): File {
|
|
28
|
+
return {
|
|
29
|
+
name: 'photo.png',
|
|
30
|
+
type: 'image/png',
|
|
31
|
+
arrayBuffer: async () => new Uint8Array(bytes).buffer,
|
|
32
|
+
} as unknown as File
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The legacy Keystone GraphQL upload wrapper shape (`{ upload: File }`). It
|
|
37
|
+
* has neither `.arrayBuffer()` (so it isn't File-like) nor `filename`/`url`
|
|
38
|
+
* keys (so it doesn't look like already-shaped metadata either) — the
|
|
39
|
+
* unrecognised shape that reproduces the original silent-corruption bug.
|
|
40
|
+
*/
|
|
41
|
+
function legacyUploadWrapper(): unknown {
|
|
42
|
+
return { upload: fakeFile() }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function makeContext() {
|
|
46
|
+
const uploadImage = vi.fn(async (): Promise<ImageMetadata> => ({
|
|
47
|
+
filename: 'new.png',
|
|
48
|
+
originalFilename: 'photo.png',
|
|
49
|
+
url: '/uploads/new.png',
|
|
50
|
+
mimeType: 'image/png',
|
|
51
|
+
size: 3,
|
|
52
|
+
width: 10,
|
|
53
|
+
height: 10,
|
|
54
|
+
uploadedAt: new Date().toISOString(),
|
|
55
|
+
storageProvider: 'images',
|
|
56
|
+
}))
|
|
57
|
+
const uploadFile = vi.fn(async (): Promise<FileMetadata> => ({
|
|
58
|
+
filename: 'new.pdf',
|
|
59
|
+
originalFilename: 'doc.pdf',
|
|
60
|
+
url: '/uploads/new.pdf',
|
|
61
|
+
mimeType: 'application/pdf',
|
|
62
|
+
size: 3,
|
|
63
|
+
uploadedAt: new Date().toISOString(),
|
|
64
|
+
storageProvider: 'documents',
|
|
65
|
+
}))
|
|
66
|
+
return {
|
|
67
|
+
context: { storage: { uploadImage, uploadFile, deleteImage: vi.fn(), deleteFile: vi.fn() } },
|
|
68
|
+
uploadImage,
|
|
69
|
+
uploadFile,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Mirrors core's `validateFieldRules` — throws the real `ValidationError` on failure. */
|
|
74
|
+
function runValidateFieldRules(
|
|
75
|
+
data: Record<string, unknown>,
|
|
76
|
+
fieldConfigs: Record<string, FieldConfig>,
|
|
77
|
+
operation: 'create' | 'update',
|
|
78
|
+
): void {
|
|
79
|
+
const result = validateWithZod(data, fieldConfigs, operation)
|
|
80
|
+
if (!result.success) {
|
|
81
|
+
throw new ValidationError(Object.values(result.errors), result.errors)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Runs the real resolveInput → validate → splitColumns sequence for one field
|
|
87
|
+
* and one input value, in that order. `splitColumns` is only reached if
|
|
88
|
+
* validation does not throw — exactly the #789 phase order.
|
|
89
|
+
*/
|
|
90
|
+
async function resolveValidateAndSplit(
|
|
91
|
+
field: FieldConfig,
|
|
92
|
+
fieldKey: string,
|
|
93
|
+
operation: 'create' | 'update',
|
|
94
|
+
inputValue: unknown,
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- test helper threads through a minimal context stub
|
|
96
|
+
context: any,
|
|
97
|
+
item?: Record<string, unknown>,
|
|
98
|
+
): Promise<Record<string, unknown>> {
|
|
99
|
+
const resolved = await field.hooks!.resolveInput!({
|
|
100
|
+
listKey: 'Post',
|
|
101
|
+
fieldKey,
|
|
102
|
+
operation,
|
|
103
|
+
inputData: { [fieldKey]: inputValue },
|
|
104
|
+
item,
|
|
105
|
+
resolvedData: { [fieldKey]: inputValue },
|
|
106
|
+
context,
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
108
|
+
} as any)
|
|
109
|
+
|
|
110
|
+
runValidateFieldRules({ [fieldKey]: resolved }, { [fieldKey]: field }, operation)
|
|
111
|
+
|
|
112
|
+
return field.splitColumns!(fieldKey, resolved)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
describe('image()/file() write validation matrix (#790)', () => {
|
|
116
|
+
describe('db.columns: "keystone" (multi-column) mode', () => {
|
|
117
|
+
describe('image()', () => {
|
|
118
|
+
const field = image({ storage: 'images', db: { columns: 'keystone' } })
|
|
119
|
+
|
|
120
|
+
it('rejects the legacy Keystone upload-wrapper shape with a thrown ValidationError', async () => {
|
|
121
|
+
const { context } = makeContext()
|
|
122
|
+
const splitSpy = vi.spyOn(field, 'splitColumns')
|
|
123
|
+
|
|
124
|
+
await expect(
|
|
125
|
+
resolveValidateAndSplit(field, 'image', 'create', legacyUploadWrapper(), context),
|
|
126
|
+
).rejects.toThrow(ValidationError)
|
|
127
|
+
|
|
128
|
+
// splitColumns must never run once validation has rejected the value —
|
|
129
|
+
// this is the exact mechanism that used to silently corrupt data.
|
|
130
|
+
expect(splitSpy).not.toHaveBeenCalled()
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('clears every column for a null value', async () => {
|
|
134
|
+
const { context } = makeContext()
|
|
135
|
+
const columns = await resolveValidateAndSplit(field, 'image', 'update', null, context)
|
|
136
|
+
expect(columns).toEqual({
|
|
137
|
+
image_url: null,
|
|
138
|
+
image_width: null,
|
|
139
|
+
image_height: null,
|
|
140
|
+
image_filesize: null,
|
|
141
|
+
image_contentType: null,
|
|
142
|
+
image_contentDisposition: null,
|
|
143
|
+
image_pathname: null,
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('keeps an already-shaped ImageMetadata unchanged (no re-upload) and splits it', async () => {
|
|
148
|
+
const { context, uploadImage } = makeContext()
|
|
149
|
+
const existing: ImageMetadata = {
|
|
150
|
+
filename: 'a.jpg',
|
|
151
|
+
originalFilename: 'a.jpg',
|
|
152
|
+
url: '/u/a.jpg',
|
|
153
|
+
mimeType: 'image/jpeg',
|
|
154
|
+
size: 1024,
|
|
155
|
+
width: 100,
|
|
156
|
+
height: 50,
|
|
157
|
+
uploadedAt: '',
|
|
158
|
+
storageProvider: 'images',
|
|
159
|
+
}
|
|
160
|
+
const columns = await resolveValidateAndSplit(field, 'image', 'update', existing, context, {
|
|
161
|
+
image: existing,
|
|
162
|
+
})
|
|
163
|
+
expect(uploadImage).not.toHaveBeenCalled()
|
|
164
|
+
expect(columns).toEqual({
|
|
165
|
+
image_url: '/u/a.jpg',
|
|
166
|
+
image_width: 100,
|
|
167
|
+
image_height: 50,
|
|
168
|
+
image_filesize: 1024,
|
|
169
|
+
image_contentType: 'image/jpeg',
|
|
170
|
+
image_contentDisposition: null,
|
|
171
|
+
image_pathname: 'a.jpg',
|
|
172
|
+
})
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('uploads a File-like value and splits the returned metadata', async () => {
|
|
176
|
+
const { context, uploadImage } = makeContext()
|
|
177
|
+
const columns = await resolveValidateAndSplit(field, 'image', 'create', fakeFile(), context)
|
|
178
|
+
expect(uploadImage).toHaveBeenCalledTimes(1)
|
|
179
|
+
expect(columns).toMatchObject({
|
|
180
|
+
image_url: '/uploads/new.png',
|
|
181
|
+
image_width: 10,
|
|
182
|
+
image_height: 10,
|
|
183
|
+
image_filesize: 3,
|
|
184
|
+
image_contentType: 'image/png',
|
|
185
|
+
})
|
|
186
|
+
})
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
describe('file()', () => {
|
|
190
|
+
const field = file({ storage: 'documents', db: { columns: 'keystone' } })
|
|
191
|
+
|
|
192
|
+
it('rejects the legacy Keystone upload-wrapper shape with a thrown ValidationError', async () => {
|
|
193
|
+
const { context } = makeContext()
|
|
194
|
+
const splitSpy = vi.spyOn(field, 'splitColumns')
|
|
195
|
+
|
|
196
|
+
await expect(
|
|
197
|
+
resolveValidateAndSplit(field, 'doc', 'create', legacyUploadWrapper(), context),
|
|
198
|
+
).rejects.toThrow(ValidationError)
|
|
199
|
+
expect(splitSpy).not.toHaveBeenCalled()
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
it('clears every column for a null value', async () => {
|
|
203
|
+
const { context } = makeContext()
|
|
204
|
+
const columns = await resolveValidateAndSplit(field, 'doc', 'update', null, context)
|
|
205
|
+
expect(columns).toEqual({ doc_filename: null, doc_filesize: null, doc_url: null })
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
it('keeps an already-shaped FileMetadata unchanged (no re-upload) and splits it', async () => {
|
|
209
|
+
const { context, uploadFile } = makeContext()
|
|
210
|
+
const existing: FileMetadata = {
|
|
211
|
+
filename: 'report.pdf',
|
|
212
|
+
originalFilename: 'report.pdf',
|
|
213
|
+
url: 'https://cdn/report.pdf',
|
|
214
|
+
mimeType: 'application/pdf',
|
|
215
|
+
size: 4096,
|
|
216
|
+
uploadedAt: '',
|
|
217
|
+
storageProvider: 'documents',
|
|
218
|
+
}
|
|
219
|
+
const columns = await resolveValidateAndSplit(field, 'doc', 'update', existing, context, {
|
|
220
|
+
doc: existing,
|
|
221
|
+
})
|
|
222
|
+
expect(uploadFile).not.toHaveBeenCalled()
|
|
223
|
+
expect(columns).toEqual({
|
|
224
|
+
doc_filename: 'report.pdf',
|
|
225
|
+
doc_filesize: 4096,
|
|
226
|
+
doc_url: 'https://cdn/report.pdf',
|
|
227
|
+
})
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
it('uploads a File-like value and splits the returned metadata', async () => {
|
|
231
|
+
const { context, uploadFile } = makeContext()
|
|
232
|
+
const columns = await resolveValidateAndSplit(field, 'doc', 'create', fakeFile(), context)
|
|
233
|
+
expect(uploadFile).toHaveBeenCalledTimes(1)
|
|
234
|
+
expect(columns).toEqual({
|
|
235
|
+
doc_filename: 'new.pdf',
|
|
236
|
+
doc_filesize: 3,
|
|
237
|
+
doc_url: '/uploads/new.pdf',
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
})
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
describe('default single-column (JSON) mode — comparison baseline', () => {
|
|
244
|
+
// No `db.columns: 'keystone'` — these fields have no `splitColumns`, so the
|
|
245
|
+
// resolved logical value goes straight to the database as `Json?`. The
|
|
246
|
+
// downstream report's own caveat was that this path was never explicitly
|
|
247
|
+
// verified; these assertions close that gap (it already throws today).
|
|
248
|
+
describe('image()', () => {
|
|
249
|
+
const field = image({ storage: 'images' })
|
|
250
|
+
|
|
251
|
+
it('already rejects the legacy upload-wrapper shape (baseline, unaffected by #789)', async () => {
|
|
252
|
+
expect(field.splitColumns).toBeUndefined()
|
|
253
|
+
const { context } = makeContext()
|
|
254
|
+
const resolved = await field.hooks!.resolveInput!({
|
|
255
|
+
listKey: 'Post',
|
|
256
|
+
fieldKey: 'image',
|
|
257
|
+
operation: 'create',
|
|
258
|
+
inputData: { image: legacyUploadWrapper() },
|
|
259
|
+
item: undefined,
|
|
260
|
+
resolvedData: { image: legacyUploadWrapper() },
|
|
261
|
+
context,
|
|
262
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
263
|
+
} as any)
|
|
264
|
+
|
|
265
|
+
expect(() =>
|
|
266
|
+
runValidateFieldRules({ image: resolved }, { image: field }, 'create'),
|
|
267
|
+
).toThrow(ValidationError)
|
|
268
|
+
})
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
describe('file()', () => {
|
|
272
|
+
const field = file({ storage: 'documents' })
|
|
273
|
+
|
|
274
|
+
it('already rejects the legacy upload-wrapper shape (baseline, unaffected by #789)', async () => {
|
|
275
|
+
expect(field.splitColumns).toBeUndefined()
|
|
276
|
+
const { context } = makeContext()
|
|
277
|
+
const resolved = await field.hooks!.resolveInput!({
|
|
278
|
+
listKey: 'Post',
|
|
279
|
+
fieldKey: 'doc',
|
|
280
|
+
operation: 'create',
|
|
281
|
+
inputData: { doc: legacyUploadWrapper() },
|
|
282
|
+
item: undefined,
|
|
283
|
+
resolvedData: { doc: legacyUploadWrapper() },
|
|
284
|
+
context,
|
|
285
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
286
|
+
} as any)
|
|
287
|
+
|
|
288
|
+
expect(() => runValidateFieldRules({ doc: resolved }, { doc: field }, 'create')).toThrow(
|
|
289
|
+
ValidationError,
|
|
290
|
+
)
|
|
291
|
+
})
|
|
292
|
+
})
|
|
293
|
+
})
|
|
294
|
+
})
|