@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,364 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
2
|
-
import { LocalStorageProvider } from '../src/providers/local.js'
|
|
3
|
-
import type { LocalStorageConfig } from '../src/config/types.js'
|
|
4
|
-
|
|
5
|
-
// Mock Node.js filesystem modules
|
|
6
|
-
vi.mock('node:fs/promises', () => ({
|
|
7
|
-
default: {
|
|
8
|
-
access: vi.fn(),
|
|
9
|
-
mkdir: vi.fn(),
|
|
10
|
-
writeFile: vi.fn(),
|
|
11
|
-
readFile: vi.fn(),
|
|
12
|
-
unlink: vi.fn(),
|
|
13
|
-
stat: vi.fn(),
|
|
14
|
-
},
|
|
15
|
-
}))
|
|
16
|
-
|
|
17
|
-
vi.mock('node:crypto', () => ({
|
|
18
|
-
randomBytes: vi.fn((size: number) => ({
|
|
19
|
-
toString: () => 'a'.repeat(size * 2), // Hex string is 2x the byte length
|
|
20
|
-
})),
|
|
21
|
-
}))
|
|
22
|
-
|
|
23
|
-
describe('LocalStorageProvider', () => {
|
|
24
|
-
let fs: typeof import('node:fs/promises').default
|
|
25
|
-
|
|
26
|
-
beforeEach(async () => {
|
|
27
|
-
vi.clearAllMocks()
|
|
28
|
-
fs = (await import('node:fs/promises')).default
|
|
29
|
-
|
|
30
|
-
// Default mock implementations
|
|
31
|
-
fs.access.mockResolvedValue(undefined)
|
|
32
|
-
fs.mkdir.mockResolvedValue(undefined)
|
|
33
|
-
fs.writeFile.mockResolvedValue(undefined)
|
|
34
|
-
fs.readFile.mockResolvedValue(Buffer.from('file-contents'))
|
|
35
|
-
fs.unlink.mockResolvedValue(undefined)
|
|
36
|
-
fs.stat.mockResolvedValue({ size: 12345 })
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
describe('constructor', () => {
|
|
40
|
-
it('should create instance with config', () => {
|
|
41
|
-
const config: LocalStorageConfig = {
|
|
42
|
-
type: 'local',
|
|
43
|
-
uploadDir: './uploads',
|
|
44
|
-
serveUrl: '/uploads',
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const provider = new LocalStorageProvider(config)
|
|
48
|
-
|
|
49
|
-
expect(provider).toBeInstanceOf(LocalStorageProvider)
|
|
50
|
-
})
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
describe('upload', () => {
|
|
54
|
-
it('should upload file successfully', async () => {
|
|
55
|
-
const config: LocalStorageConfig = {
|
|
56
|
-
type: 'local',
|
|
57
|
-
uploadDir: './uploads',
|
|
58
|
-
serveUrl: '/uploads',
|
|
59
|
-
}
|
|
60
|
-
const provider = new LocalStorageProvider(config)
|
|
61
|
-
const fileBuffer = Buffer.from('test file content')
|
|
62
|
-
const filename = 'test.txt'
|
|
63
|
-
|
|
64
|
-
const result = await provider.upload(fileBuffer, filename)
|
|
65
|
-
|
|
66
|
-
expect(fs.writeFile).toHaveBeenCalledWith(
|
|
67
|
-
expect.stringMatching(/uploads\/\d+-.*\.txt$/),
|
|
68
|
-
fileBuffer,
|
|
69
|
-
)
|
|
70
|
-
expect(result.url).toMatch(/\/uploads\/\d+-.*\.txt/)
|
|
71
|
-
expect(result.size).toBe(12345)
|
|
72
|
-
expect(result.contentType).toBe('application/octet-stream')
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
it('should create upload directory if it does not exist', async () => {
|
|
76
|
-
fs.access.mockRejectedValueOnce(new Error('Directory does not exist'))
|
|
77
|
-
|
|
78
|
-
const config: LocalStorageConfig = {
|
|
79
|
-
type: 'local',
|
|
80
|
-
uploadDir: './new-uploads',
|
|
81
|
-
serveUrl: '/uploads',
|
|
82
|
-
}
|
|
83
|
-
const provider = new LocalStorageProvider(config)
|
|
84
|
-
|
|
85
|
-
await provider.upload(Buffer.from('test'), 'test.txt')
|
|
86
|
-
|
|
87
|
-
expect(fs.mkdir).toHaveBeenCalledWith('./new-uploads', { recursive: true })
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
it('should not create directory if it already exists', async () => {
|
|
91
|
-
fs.access.mockResolvedValueOnce(undefined)
|
|
92
|
-
|
|
93
|
-
const config: LocalStorageConfig = {
|
|
94
|
-
type: 'local',
|
|
95
|
-
uploadDir: './existing-uploads',
|
|
96
|
-
serveUrl: '/uploads',
|
|
97
|
-
}
|
|
98
|
-
const provider = new LocalStorageProvider(config)
|
|
99
|
-
|
|
100
|
-
await provider.upload(Buffer.from('test'), 'test.txt')
|
|
101
|
-
|
|
102
|
-
expect(fs.mkdir).not.toHaveBeenCalled()
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
it('should generate unique filenames by default', async () => {
|
|
106
|
-
const config: LocalStorageConfig = {
|
|
107
|
-
type: 'local',
|
|
108
|
-
uploadDir: './uploads',
|
|
109
|
-
serveUrl: '/uploads',
|
|
110
|
-
}
|
|
111
|
-
const provider = new LocalStorageProvider(config)
|
|
112
|
-
|
|
113
|
-
const result1 = await provider.upload(Buffer.from('test1'), 'test.txt')
|
|
114
|
-
const result2 = await provider.upload(Buffer.from('test2'), 'test.txt')
|
|
115
|
-
|
|
116
|
-
expect(result1.filename).not.toBe('test.txt')
|
|
117
|
-
expect(result2.filename).not.toBe('test.txt')
|
|
118
|
-
expect(result1.filename).toMatch(/\d+-[a-f0-9]+\.txt/)
|
|
119
|
-
expect(result2.filename).toMatch(/\d+-[a-f0-9]+\.txt/)
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
it('should preserve original filename when generateUniqueFilenames is false', async () => {
|
|
123
|
-
const config: LocalStorageConfig = {
|
|
124
|
-
type: 'local',
|
|
125
|
-
uploadDir: './uploads',
|
|
126
|
-
serveUrl: '/uploads',
|
|
127
|
-
generateUniqueFilenames: false,
|
|
128
|
-
}
|
|
129
|
-
const provider = new LocalStorageProvider(config)
|
|
130
|
-
|
|
131
|
-
const result = await provider.upload(Buffer.from('test'), 'original.txt')
|
|
132
|
-
|
|
133
|
-
expect(result.filename).toBe('original.txt')
|
|
134
|
-
expect(result.url).toBe('/uploads/original.txt')
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
it('should preserve file extension when generating unique names', async () => {
|
|
138
|
-
const config: LocalStorageConfig = {
|
|
139
|
-
type: 'local',
|
|
140
|
-
uploadDir: './uploads',
|
|
141
|
-
serveUrl: '/uploads',
|
|
142
|
-
}
|
|
143
|
-
const provider = new LocalStorageProvider(config)
|
|
144
|
-
|
|
145
|
-
const resultTxt = await provider.upload(Buffer.from('test'), 'file.txt')
|
|
146
|
-
const resultJpg = await provider.upload(Buffer.from('test'), 'photo.jpg')
|
|
147
|
-
const resultNoExt = await provider.upload(Buffer.from('test'), 'noext')
|
|
148
|
-
|
|
149
|
-
expect(resultTxt.filename).toMatch(/\.txt$/)
|
|
150
|
-
expect(resultJpg.filename).toMatch(/\.jpg$/)
|
|
151
|
-
expect(resultNoExt.filename).not.toMatch(/\./)
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
it('should use provided content type', async () => {
|
|
155
|
-
const config: LocalStorageConfig = {
|
|
156
|
-
type: 'local',
|
|
157
|
-
uploadDir: './uploads',
|
|
158
|
-
serveUrl: '/uploads',
|
|
159
|
-
}
|
|
160
|
-
const provider = new LocalStorageProvider(config)
|
|
161
|
-
|
|
162
|
-
const result = await provider.upload(Buffer.from('test'), 'test.txt', {
|
|
163
|
-
contentType: 'text/plain',
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
expect(result.contentType).toBe('text/plain')
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
it('should store custom metadata', async () => {
|
|
170
|
-
const config: LocalStorageConfig = {
|
|
171
|
-
type: 'local',
|
|
172
|
-
uploadDir: './uploads',
|
|
173
|
-
serveUrl: '/uploads',
|
|
174
|
-
}
|
|
175
|
-
const provider = new LocalStorageProvider(config)
|
|
176
|
-
const metadata = { uploadedBy: 'user123', category: 'documents' }
|
|
177
|
-
|
|
178
|
-
const result = await provider.upload(Buffer.from('test'), 'test.txt', { metadata })
|
|
179
|
-
|
|
180
|
-
expect(result.metadata).toEqual(metadata)
|
|
181
|
-
})
|
|
182
|
-
|
|
183
|
-
it('should handle Uint8Array input', async () => {
|
|
184
|
-
const config: LocalStorageConfig = {
|
|
185
|
-
type: 'local',
|
|
186
|
-
uploadDir: './uploads',
|
|
187
|
-
serveUrl: '/uploads',
|
|
188
|
-
}
|
|
189
|
-
const provider = new LocalStorageProvider(config)
|
|
190
|
-
const uint8Array = new Uint8Array([1, 2, 3, 4, 5])
|
|
191
|
-
|
|
192
|
-
await provider.upload(uint8Array, 'binary.dat')
|
|
193
|
-
|
|
194
|
-
expect(fs.writeFile).toHaveBeenCalledWith(expect.any(String), uint8Array)
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
it('should construct correct file path', async () => {
|
|
198
|
-
const config: LocalStorageConfig = {
|
|
199
|
-
type: 'local',
|
|
200
|
-
uploadDir: './my-uploads',
|
|
201
|
-
serveUrl: '/files',
|
|
202
|
-
generateUniqueFilenames: false,
|
|
203
|
-
}
|
|
204
|
-
const provider = new LocalStorageProvider(config)
|
|
205
|
-
|
|
206
|
-
await provider.upload(Buffer.from('test'), 'test.txt')
|
|
207
|
-
|
|
208
|
-
expect(fs.writeFile).toHaveBeenCalledWith('my-uploads/test.txt', expect.any(Buffer))
|
|
209
|
-
})
|
|
210
|
-
|
|
211
|
-
it('should return correct URL', async () => {
|
|
212
|
-
const config: LocalStorageConfig = {
|
|
213
|
-
type: 'local',
|
|
214
|
-
uploadDir: './uploads',
|
|
215
|
-
serveUrl: 'https://example.com/files',
|
|
216
|
-
generateUniqueFilenames: false,
|
|
217
|
-
}
|
|
218
|
-
const provider = new LocalStorageProvider(config)
|
|
219
|
-
|
|
220
|
-
const result = await provider.upload(Buffer.from('test'), 'photo.jpg')
|
|
221
|
-
|
|
222
|
-
expect(result.url).toBe('https://example.com/files/photo.jpg')
|
|
223
|
-
})
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
describe('download', () => {
|
|
227
|
-
it('should download file successfully', async () => {
|
|
228
|
-
const config: LocalStorageConfig = {
|
|
229
|
-
type: 'local',
|
|
230
|
-
uploadDir: './uploads',
|
|
231
|
-
serveUrl: '/uploads',
|
|
232
|
-
}
|
|
233
|
-
const provider = new LocalStorageProvider(config)
|
|
234
|
-
const expectedBuffer = Buffer.from('downloaded file content')
|
|
235
|
-
fs.readFile.mockResolvedValueOnce(expectedBuffer)
|
|
236
|
-
|
|
237
|
-
const result = await provider.download('test-file.txt')
|
|
238
|
-
|
|
239
|
-
expect(fs.readFile).toHaveBeenCalledWith('uploads/test-file.txt')
|
|
240
|
-
expect(result).toBe(expectedBuffer)
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
it('should construct correct file path for download', async () => {
|
|
244
|
-
const config: LocalStorageConfig = {
|
|
245
|
-
type: 'local',
|
|
246
|
-
uploadDir: './my-storage',
|
|
247
|
-
serveUrl: '/files',
|
|
248
|
-
}
|
|
249
|
-
const provider = new LocalStorageProvider(config)
|
|
250
|
-
|
|
251
|
-
await provider.download('document.pdf')
|
|
252
|
-
|
|
253
|
-
expect(fs.readFile).toHaveBeenCalledWith('my-storage/document.pdf')
|
|
254
|
-
})
|
|
255
|
-
|
|
256
|
-
it('should propagate filesystem errors', async () => {
|
|
257
|
-
const config: LocalStorageConfig = {
|
|
258
|
-
type: 'local',
|
|
259
|
-
uploadDir: './uploads',
|
|
260
|
-
serveUrl: '/uploads',
|
|
261
|
-
}
|
|
262
|
-
const provider = new LocalStorageProvider(config)
|
|
263
|
-
fs.readFile.mockRejectedValueOnce(new Error('File not found'))
|
|
264
|
-
|
|
265
|
-
await expect(provider.download('nonexistent.txt')).rejects.toThrow('File not found')
|
|
266
|
-
})
|
|
267
|
-
})
|
|
268
|
-
|
|
269
|
-
describe('delete', () => {
|
|
270
|
-
it('should delete file successfully', async () => {
|
|
271
|
-
const config: LocalStorageConfig = {
|
|
272
|
-
type: 'local',
|
|
273
|
-
uploadDir: './uploads',
|
|
274
|
-
serveUrl: '/uploads',
|
|
275
|
-
}
|
|
276
|
-
const provider = new LocalStorageProvider(config)
|
|
277
|
-
|
|
278
|
-
await provider.delete('file-to-delete.txt')
|
|
279
|
-
|
|
280
|
-
expect(fs.unlink).toHaveBeenCalledWith('uploads/file-to-delete.txt')
|
|
281
|
-
})
|
|
282
|
-
|
|
283
|
-
it('should construct correct file path for deletion', async () => {
|
|
284
|
-
const config: LocalStorageConfig = {
|
|
285
|
-
type: 'local',
|
|
286
|
-
uploadDir: './my-storage/files',
|
|
287
|
-
serveUrl: '/files',
|
|
288
|
-
}
|
|
289
|
-
const provider = new LocalStorageProvider(config)
|
|
290
|
-
|
|
291
|
-
await provider.delete('old-file.jpg')
|
|
292
|
-
|
|
293
|
-
expect(fs.unlink).toHaveBeenCalledWith('my-storage/files/old-file.jpg')
|
|
294
|
-
})
|
|
295
|
-
|
|
296
|
-
it('should propagate filesystem errors on delete', async () => {
|
|
297
|
-
const config: LocalStorageConfig = {
|
|
298
|
-
type: 'local',
|
|
299
|
-
uploadDir: './uploads',
|
|
300
|
-
serveUrl: '/uploads',
|
|
301
|
-
}
|
|
302
|
-
const provider = new LocalStorageProvider(config)
|
|
303
|
-
fs.unlink.mockRejectedValueOnce(new Error('Permission denied'))
|
|
304
|
-
|
|
305
|
-
await expect(provider.delete('protected.txt')).rejects.toThrow('Permission denied')
|
|
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
|
-
})
|
|
322
|
-
})
|
|
323
|
-
|
|
324
|
-
describe('getUrl', () => {
|
|
325
|
-
it('should return correct URL for filename', () => {
|
|
326
|
-
const config: LocalStorageConfig = {
|
|
327
|
-
type: 'local',
|
|
328
|
-
uploadDir: './uploads',
|
|
329
|
-
serveUrl: '/uploads',
|
|
330
|
-
}
|
|
331
|
-
const provider = new LocalStorageProvider(config)
|
|
332
|
-
|
|
333
|
-
const url = provider.getUrl('photo.jpg')
|
|
334
|
-
|
|
335
|
-
expect(url).toBe('/uploads/photo.jpg')
|
|
336
|
-
})
|
|
337
|
-
|
|
338
|
-
it('should work with custom serve URL', () => {
|
|
339
|
-
const config: LocalStorageConfig = {
|
|
340
|
-
type: 'local',
|
|
341
|
-
uploadDir: './storage',
|
|
342
|
-
serveUrl: 'https://cdn.example.com/files',
|
|
343
|
-
}
|
|
344
|
-
const provider = new LocalStorageProvider(config)
|
|
345
|
-
|
|
346
|
-
const url = provider.getUrl('document.pdf')
|
|
347
|
-
|
|
348
|
-
expect(url).toBe('https://cdn.example.com/files/document.pdf')
|
|
349
|
-
})
|
|
350
|
-
|
|
351
|
-
it('should handle filenames with special characters', () => {
|
|
352
|
-
const config: LocalStorageConfig = {
|
|
353
|
-
type: 'local',
|
|
354
|
-
uploadDir: './uploads',
|
|
355
|
-
serveUrl: '/uploads',
|
|
356
|
-
}
|
|
357
|
-
const provider = new LocalStorageProvider(config)
|
|
358
|
-
|
|
359
|
-
const url = provider.getUrl('my file (1).txt')
|
|
360
|
-
|
|
361
|
-
expect(url).toBe('/uploads/my file (1).txt')
|
|
362
|
-
})
|
|
363
|
-
})
|
|
364
|
-
})
|