@opensaas/stack-storage 0.42.3 → 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.
- package/CHANGELOG.md +447 -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 +135 -11
- 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,313 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
validateFile,
|
|
4
|
-
formatFileSize,
|
|
5
|
-
getMimeType,
|
|
6
|
-
fileToBuffer,
|
|
7
|
-
parseFileFromFormData,
|
|
8
|
-
type FileValidationOptions,
|
|
9
|
-
} from '../src/utils/upload.js'
|
|
10
|
-
|
|
11
|
-
describe('Upload Utilities', () => {
|
|
12
|
-
describe('validateFile', () => {
|
|
13
|
-
it('should return valid when no options provided', () => {
|
|
14
|
-
const file = { size: 1000, name: 'test.txt', type: 'text/plain' }
|
|
15
|
-
const result = validateFile(file)
|
|
16
|
-
expect(result.valid).toBe(true)
|
|
17
|
-
expect(result.error).toBeUndefined()
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
it('should return valid when options are empty', () => {
|
|
21
|
-
const file = { size: 1000, name: 'test.txt', type: 'text/plain' }
|
|
22
|
-
const result = validateFile(file, {})
|
|
23
|
-
expect(result.valid).toBe(true)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
describe('file size validation', () => {
|
|
27
|
-
it('should accept files within size limit', () => {
|
|
28
|
-
const file = { size: 1000, name: 'test.txt', type: 'text/plain' }
|
|
29
|
-
const options: FileValidationOptions = { maxFileSize: 2000 }
|
|
30
|
-
const result = validateFile(file, options)
|
|
31
|
-
expect(result.valid).toBe(true)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('should reject files exceeding size limit', () => {
|
|
35
|
-
const file = { size: 3000, name: 'test.txt', type: 'text/plain' }
|
|
36
|
-
const options: FileValidationOptions = { maxFileSize: 2000 }
|
|
37
|
-
const result = validateFile(file, options)
|
|
38
|
-
expect(result.valid).toBe(false)
|
|
39
|
-
expect(result.error).toContain('File size exceeds maximum')
|
|
40
|
-
expect(result.error).toContain('1.95 KB')
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('should accept files exactly at size limit', () => {
|
|
44
|
-
const file = { size: 2000, name: 'test.txt', type: 'text/plain' }
|
|
45
|
-
const options: FileValidationOptions = { maxFileSize: 2000 }
|
|
46
|
-
const result = validateFile(file, options)
|
|
47
|
-
expect(result.valid).toBe(true)
|
|
48
|
-
})
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
describe('MIME type validation', () => {
|
|
52
|
-
it('should accept files with allowed MIME type', () => {
|
|
53
|
-
const file = { size: 1000, name: 'test.jpg', type: 'image/jpeg' }
|
|
54
|
-
const options: FileValidationOptions = {
|
|
55
|
-
acceptedMimeTypes: ['image/jpeg', 'image/png'],
|
|
56
|
-
}
|
|
57
|
-
const result = validateFile(file, options)
|
|
58
|
-
expect(result.valid).toBe(true)
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
it('should reject files with disallowed MIME type', () => {
|
|
62
|
-
const file = { size: 1000, name: 'test.gif', type: 'image/gif' }
|
|
63
|
-
const options: FileValidationOptions = {
|
|
64
|
-
acceptedMimeTypes: ['image/jpeg', 'image/png'],
|
|
65
|
-
}
|
|
66
|
-
const result = validateFile(file, options)
|
|
67
|
-
expect(result.valid).toBe(false)
|
|
68
|
-
expect(result.error).toContain('File type')
|
|
69
|
-
expect(result.error).toContain('image/gif')
|
|
70
|
-
expect(result.error).toContain('image/jpeg')
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
it('should fall back to filename extension when type is empty', () => {
|
|
74
|
-
const file = { size: 1000, name: 'test.jpg', type: '' }
|
|
75
|
-
const options: FileValidationOptions = {
|
|
76
|
-
acceptedMimeTypes: ['image/jpeg'],
|
|
77
|
-
}
|
|
78
|
-
const result = validateFile(file, options)
|
|
79
|
-
expect(result.valid).toBe(true)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('should reject unknown MIME types when restrictions exist', () => {
|
|
83
|
-
const file = { size: 1000, name: 'test.xyz', type: '' }
|
|
84
|
-
const options: FileValidationOptions = {
|
|
85
|
-
acceptedMimeTypes: ['image/jpeg'],
|
|
86
|
-
}
|
|
87
|
-
const result = validateFile(file, options)
|
|
88
|
-
expect(result.valid).toBe(false)
|
|
89
|
-
})
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
describe('file extension validation', () => {
|
|
93
|
-
it('should accept files with allowed extension', () => {
|
|
94
|
-
const file = { size: 1000, name: 'test.jpg', type: 'image/jpeg' }
|
|
95
|
-
const options: FileValidationOptions = {
|
|
96
|
-
acceptedExtensions: ['.jpg', '.png'],
|
|
97
|
-
}
|
|
98
|
-
const result = validateFile(file, options)
|
|
99
|
-
expect(result.valid).toBe(true)
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
it('should reject files with disallowed extension', () => {
|
|
103
|
-
const file = { size: 1000, name: 'test.gif', type: 'image/gif' }
|
|
104
|
-
const options: FileValidationOptions = {
|
|
105
|
-
acceptedExtensions: ['.jpg', '.png'],
|
|
106
|
-
}
|
|
107
|
-
const result = validateFile(file, options)
|
|
108
|
-
expect(result.valid).toBe(false)
|
|
109
|
-
expect(result.error).toContain('File extension')
|
|
110
|
-
expect(result.error).toContain('.gif')
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
it('should handle case-insensitive extensions', () => {
|
|
114
|
-
const file = { size: 1000, name: 'test.JPG', type: 'image/jpeg' }
|
|
115
|
-
const options: FileValidationOptions = {
|
|
116
|
-
acceptedExtensions: ['.jpg'],
|
|
117
|
-
}
|
|
118
|
-
const result = validateFile(file, options)
|
|
119
|
-
expect(result.valid).toBe(true)
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
it('should handle files with multiple dots in name', () => {
|
|
123
|
-
const file = { size: 1000, name: 'my.test.file.jpg', type: 'image/jpeg' }
|
|
124
|
-
const options: FileValidationOptions = {
|
|
125
|
-
acceptedExtensions: ['.jpg'],
|
|
126
|
-
}
|
|
127
|
-
const result = validateFile(file, options)
|
|
128
|
-
expect(result.valid).toBe(true)
|
|
129
|
-
})
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
describe('combined validation', () => {
|
|
133
|
-
it('should validate all criteria when multiple options provided', () => {
|
|
134
|
-
const file = { size: 1000, name: 'test.jpg', type: 'image/jpeg' }
|
|
135
|
-
const options: FileValidationOptions = {
|
|
136
|
-
maxFileSize: 2000,
|
|
137
|
-
acceptedMimeTypes: ['image/jpeg'],
|
|
138
|
-
acceptedExtensions: ['.jpg'],
|
|
139
|
-
}
|
|
140
|
-
const result = validateFile(file, options)
|
|
141
|
-
expect(result.valid).toBe(true)
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
it('should fail if any criterion is not met', () => {
|
|
145
|
-
const file = { size: 3000, name: 'test.jpg', type: 'image/jpeg' }
|
|
146
|
-
const options: FileValidationOptions = {
|
|
147
|
-
maxFileSize: 2000,
|
|
148
|
-
acceptedMimeTypes: ['image/jpeg'],
|
|
149
|
-
acceptedExtensions: ['.jpg'],
|
|
150
|
-
}
|
|
151
|
-
const result = validateFile(file, options)
|
|
152
|
-
expect(result.valid).toBe(false)
|
|
153
|
-
})
|
|
154
|
-
})
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
describe('formatFileSize', () => {
|
|
158
|
-
it('should format 0 bytes', () => {
|
|
159
|
-
expect(formatFileSize(0)).toBe('0 Bytes')
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
it('should format bytes', () => {
|
|
163
|
-
expect(formatFileSize(500)).toBe('500 Bytes')
|
|
164
|
-
expect(formatFileSize(1023)).toBe('1023 Bytes')
|
|
165
|
-
})
|
|
166
|
-
|
|
167
|
-
it('should format kilobytes', () => {
|
|
168
|
-
expect(formatFileSize(1024)).toBe('1 KB')
|
|
169
|
-
expect(formatFileSize(2048)).toBe('2 KB')
|
|
170
|
-
expect(formatFileSize(1536)).toBe('1.5 KB')
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
it('should format megabytes', () => {
|
|
174
|
-
expect(formatFileSize(1024 * 1024)).toBe('1 MB')
|
|
175
|
-
expect(formatFileSize(5 * 1024 * 1024)).toBe('5 MB')
|
|
176
|
-
expect(formatFileSize(1.5 * 1024 * 1024)).toBe('1.5 MB')
|
|
177
|
-
})
|
|
178
|
-
|
|
179
|
-
it('should format gigabytes', () => {
|
|
180
|
-
expect(formatFileSize(1024 * 1024 * 1024)).toBe('1 GB')
|
|
181
|
-
expect(formatFileSize(2.5 * 1024 * 1024 * 1024)).toBe('2.5 GB')
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
it('should round to 2 decimal places', () => {
|
|
185
|
-
expect(formatFileSize(1536)).toBe('1.5 KB')
|
|
186
|
-
expect(formatFileSize(1555)).toBe('1.52 KB')
|
|
187
|
-
expect(formatFileSize(1666)).toBe('1.63 KB')
|
|
188
|
-
})
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
describe('getMimeType', () => {
|
|
192
|
-
it('should return correct MIME type for common extensions', () => {
|
|
193
|
-
expect(getMimeType('test.jpg')).toBe('image/jpeg')
|
|
194
|
-
expect(getMimeType('test.jpeg')).toBe('image/jpeg')
|
|
195
|
-
expect(getMimeType('test.png')).toBe('image/png')
|
|
196
|
-
expect(getMimeType('test.gif')).toBe('image/gif')
|
|
197
|
-
expect(getMimeType('test.pdf')).toBe('application/pdf')
|
|
198
|
-
expect(getMimeType('test.txt')).toBe('text/plain')
|
|
199
|
-
expect(getMimeType('test.html')).toBe('text/html')
|
|
200
|
-
expect(getMimeType('test.json')).toBe('application/json')
|
|
201
|
-
})
|
|
202
|
-
|
|
203
|
-
it('should handle uppercase extensions', () => {
|
|
204
|
-
expect(getMimeType('test.JPG')).toBe('image/jpeg')
|
|
205
|
-
expect(getMimeType('test.PNG')).toBe('image/png')
|
|
206
|
-
})
|
|
207
|
-
|
|
208
|
-
it('should return default MIME type for unknown extensions', () => {
|
|
209
|
-
// Note: .xyz is recognized by mime-types as 'chemical/x-xyz'
|
|
210
|
-
// Using a truly unknown extension instead
|
|
211
|
-
expect(getMimeType('test.unknownext')).toBe('application/octet-stream')
|
|
212
|
-
expect(getMimeType('noextension')).toBe('application/octet-stream')
|
|
213
|
-
})
|
|
214
|
-
|
|
215
|
-
it('should handle files with multiple dots', () => {
|
|
216
|
-
expect(getMimeType('my.test.file.jpg')).toBe('image/jpeg')
|
|
217
|
-
})
|
|
218
|
-
})
|
|
219
|
-
|
|
220
|
-
describe('fileToBuffer', () => {
|
|
221
|
-
it('should convert File to Buffer', async () => {
|
|
222
|
-
// Create a mock File object
|
|
223
|
-
const content = 'test file content'
|
|
224
|
-
const blob = new Blob([content], { type: 'text/plain' })
|
|
225
|
-
const file = new File([blob], 'test.txt', { type: 'text/plain' })
|
|
226
|
-
|
|
227
|
-
const buffer = await fileToBuffer(file)
|
|
228
|
-
|
|
229
|
-
expect(buffer).toBeInstanceOf(Buffer)
|
|
230
|
-
expect(buffer.toString()).toBe(content)
|
|
231
|
-
})
|
|
232
|
-
|
|
233
|
-
it('should convert Blob to Buffer', async () => {
|
|
234
|
-
const content = 'test blob content'
|
|
235
|
-
const blob = new Blob([content], { type: 'text/plain' })
|
|
236
|
-
|
|
237
|
-
const buffer = await fileToBuffer(blob)
|
|
238
|
-
|
|
239
|
-
expect(buffer).toBeInstanceOf(Buffer)
|
|
240
|
-
expect(buffer.toString()).toBe(content)
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
it('should handle binary data', async () => {
|
|
244
|
-
const binaryData = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]) // "Hello"
|
|
245
|
-
const blob = new Blob([binaryData])
|
|
246
|
-
|
|
247
|
-
const buffer = await fileToBuffer(blob)
|
|
248
|
-
|
|
249
|
-
expect(buffer).toBeInstanceOf(Buffer)
|
|
250
|
-
expect(buffer.toString()).toBe('Hello')
|
|
251
|
-
})
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
describe('parseFileFromFormData', () => {
|
|
255
|
-
it('should extract file from FormData with default field name', async () => {
|
|
256
|
-
const content = 'test file content'
|
|
257
|
-
const file = new File([content], 'test.txt', { type: 'text/plain' })
|
|
258
|
-
const formData = new FormData()
|
|
259
|
-
formData.append('file', file)
|
|
260
|
-
|
|
261
|
-
const result = await parseFileFromFormData(formData)
|
|
262
|
-
|
|
263
|
-
expect(result).not.toBeNull()
|
|
264
|
-
expect(result!.file).toBeInstanceOf(File)
|
|
265
|
-
expect(result!.file.name).toBe('test.txt')
|
|
266
|
-
expect(result!.buffer).toBeInstanceOf(Buffer)
|
|
267
|
-
expect(result!.buffer.toString()).toBe(content)
|
|
268
|
-
})
|
|
269
|
-
|
|
270
|
-
it('should extract file from FormData with custom field name', async () => {
|
|
271
|
-
const content = 'custom field content'
|
|
272
|
-
const file = new File([content], 'custom.txt', { type: 'text/plain' })
|
|
273
|
-
const formData = new FormData()
|
|
274
|
-
formData.append('customField', file)
|
|
275
|
-
|
|
276
|
-
const result = await parseFileFromFormData(formData, 'customField')
|
|
277
|
-
|
|
278
|
-
expect(result).not.toBeNull()
|
|
279
|
-
expect(result!.file.name).toBe('custom.txt')
|
|
280
|
-
expect(result!.buffer.toString()).toBe(content)
|
|
281
|
-
})
|
|
282
|
-
|
|
283
|
-
it('should return null when field does not exist', async () => {
|
|
284
|
-
const formData = new FormData()
|
|
285
|
-
|
|
286
|
-
const result = await parseFileFromFormData(formData)
|
|
287
|
-
|
|
288
|
-
expect(result).toBeNull()
|
|
289
|
-
})
|
|
290
|
-
|
|
291
|
-
it('should return null when field is not a File', async () => {
|
|
292
|
-
const formData = new FormData()
|
|
293
|
-
formData.append('file', 'not a file')
|
|
294
|
-
|
|
295
|
-
const result = await parseFileFromFormData(formData)
|
|
296
|
-
|
|
297
|
-
expect(result).toBeNull()
|
|
298
|
-
})
|
|
299
|
-
|
|
300
|
-
it('should handle multiple files but only return first', async () => {
|
|
301
|
-
const file1 = new File(['content1'], 'file1.txt')
|
|
302
|
-
const file2 = new File(['content2'], 'file2.txt')
|
|
303
|
-
const formData = new FormData()
|
|
304
|
-
formData.append('file', file1)
|
|
305
|
-
formData.append('file', file2)
|
|
306
|
-
|
|
307
|
-
const result = await parseFileFromFormData(formData)
|
|
308
|
-
|
|
309
|
-
expect(result).not.toBeNull()
|
|
310
|
-
expect(result!.file.name).toBe('file1.txt')
|
|
311
|
-
})
|
|
312
|
-
})
|
|
313
|
-
})
|
package/tsconfig.json
DELETED
package/vitest.config.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from 'vitest/config'
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
test: {
|
|
5
|
-
globals: true,
|
|
6
|
-
environment: 'node',
|
|
7
|
-
coverage: {
|
|
8
|
-
provider: 'v8',
|
|
9
|
-
reporter: ['text', 'json', 'html', 'json-summary'],
|
|
10
|
-
include: ['src/**/*.ts'],
|
|
11
|
-
exclude: ['src/**/*.test.ts', 'src/**/*.spec.ts', 'dist/**', 'node_modules/**'],
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
})
|