@opensaas/stack-storage-vercel 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.
@@ -1,986 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from 'vitest'
2
- import { VercelBlobStorageProvider, vercelBlobStorage } from '../src/index.js'
3
- import type { VercelBlobStorageConfig } from '../src/index.js'
4
-
5
- // Mirrors @vercel/blob's real BlobNotFoundError shape (extends BlobError)
6
- const { BlobNotFoundError } = vi.hoisted(() => {
7
- class BlobNotFoundError extends Error {
8
- constructor() {
9
- super('The requested blob does not exist')
10
- this.name = 'BlobNotFoundError'
11
- }
12
- }
13
- return { BlobNotFoundError }
14
- })
15
-
16
- // Mock Vercel Blob SDK
17
- vi.mock('@vercel/blob', () => ({
18
- put: vi.fn(),
19
- del: vi.fn(),
20
- head: vi.fn(),
21
- get: vi.fn(),
22
- issueSignedToken: vi.fn(),
23
- presignUrl: vi.fn(),
24
- BlobNotFoundError,
25
- }))
26
-
27
- // Mock crypto for deterministic filename testing
28
- vi.mock('node:crypto', () => ({
29
- randomBytes: vi.fn((size: number) => ({
30
- toString: () => 'a'.repeat(size * 2), // Hex string is 2x the byte length
31
- })),
32
- }))
33
-
34
- // Mock Date.now for predictable timestamps
35
- const MOCK_TIMESTAMP = 1234567890000
36
- vi.spyOn(Date, 'now').mockReturnValue(MOCK_TIMESTAMP)
37
-
38
- function createReadableStream(bytes: Uint8Array): ReadableStream<Uint8Array> {
39
- return new ReadableStream({
40
- start(controller) {
41
- controller.enqueue(bytes)
42
- controller.close()
43
- },
44
- })
45
- }
46
-
47
- describe('VercelBlobStorageProvider', () => {
48
- let put: ReturnType<typeof vi.fn>
49
- let del: ReturnType<typeof vi.fn>
50
- let head: ReturnType<typeof vi.fn>
51
- let get: ReturnType<typeof vi.fn>
52
- let issueSignedToken: ReturnType<typeof vi.fn>
53
- let presignUrl: ReturnType<typeof vi.fn>
54
-
55
- beforeEach(async () => {
56
- vi.clearAllMocks()
57
-
58
- // Import mocked modules
59
- const vercelBlob = await import('@vercel/blob')
60
- put = vercelBlob.put as ReturnType<typeof vi.fn>
61
- del = vercelBlob.del as ReturnType<typeof vi.fn>
62
- head = vercelBlob.head as ReturnType<typeof vi.fn>
63
- get = vercelBlob.get as ReturnType<typeof vi.fn>
64
- issueSignedToken = vercelBlob.issueSignedToken as ReturnType<typeof vi.fn>
65
- presignUrl = vercelBlob.presignUrl as ReturnType<typeof vi.fn>
66
-
67
- // Default mock implementations
68
- put.mockResolvedValue({
69
- url: 'https://blob.vercel-storage.com/test-file.txt',
70
- downloadUrl: 'https://blob.vercel-storage.com/test-file.txt?download=1',
71
- pathname: 'test-file.txt',
72
- })
73
-
74
- head.mockResolvedValue({
75
- url: 'https://blob.vercel-storage.com/test-file.txt',
76
- downloadUrl: 'https://blob.vercel-storage.com/test-file.txt?download=1',
77
- pathname: 'test-file.txt',
78
- size: 12345,
79
- uploadedAt: new Date(),
80
- })
81
-
82
- get.mockResolvedValue({
83
- statusCode: 200,
84
- stream: createReadableStream(new Uint8Array([1, 2, 3, 4, 5])),
85
- headers: new Headers(),
86
- blob: {
87
- url: 'https://blob.vercel-storage.com/test-file.txt',
88
- downloadUrl: 'https://blob.vercel-storage.com/test-file.txt?download=1',
89
- pathname: 'test-file.txt',
90
- contentDisposition: '',
91
- cacheControl: '',
92
- uploadedAt: new Date(),
93
- etag: 'etag',
94
- contentType: 'text/plain',
95
- size: 5,
96
- },
97
- })
98
-
99
- issueSignedToken.mockResolvedValue({
100
- delegationToken: 'delegation-token',
101
- clientSigningToken: 'client-signing-token',
102
- validUntil: MOCK_TIMESTAMP + 3600_000,
103
- })
104
-
105
- presignUrl.mockResolvedValue({
106
- presignedUrl: 'https://blob.vercel-storage.com/test-file.txt?signature=abc',
107
- })
108
- })
109
-
110
- describe('constructor', () => {
111
- it('should create instance with token in config', () => {
112
- const config: VercelBlobStorageConfig = {
113
- type: 'vercel-blob',
114
- token: 'test-token-123',
115
- }
116
-
117
- const provider = new VercelBlobStorageProvider(config)
118
-
119
- expect(provider).toBeInstanceOf(VercelBlobStorageProvider)
120
- })
121
-
122
- it('should create instance with token from env var', () => {
123
- process.env.BLOB_READ_WRITE_TOKEN = 'env-token-456'
124
-
125
- const config: VercelBlobStorageConfig = {
126
- type: 'vercel-blob',
127
- }
128
-
129
- const provider = new VercelBlobStorageProvider(config)
130
-
131
- expect(provider).toBeInstanceOf(VercelBlobStorageProvider)
132
-
133
- delete process.env.BLOB_READ_WRITE_TOKEN
134
- })
135
-
136
- it('should not throw when no static token is configured (OIDC auth)', () => {
137
- delete process.env.BLOB_READ_WRITE_TOKEN
138
-
139
- // With Vercel OIDC auth there is no static token — the SDK resolves
140
- // credentials at call time from VERCEL_OIDC_TOKEN + storeId/BLOB_STORE_ID.
141
- const config: VercelBlobStorageConfig = {
142
- type: 'vercel-blob',
143
- storeId: 'store_abc123',
144
- }
145
-
146
- expect(() => new VercelBlobStorageProvider(config)).not.toThrow()
147
- })
148
-
149
- it('should not throw when no credentials are configured at all', () => {
150
- delete process.env.BLOB_READ_WRITE_TOKEN
151
-
152
- const config: VercelBlobStorageConfig = {
153
- type: 'vercel-blob',
154
- }
155
-
156
- // Credential resolution is delegated to the SDK, which throws its own
157
- // descriptive error at call time if nothing is available.
158
- expect(() => new VercelBlobStorageProvider(config)).not.toThrow()
159
- })
160
- })
161
-
162
- describe('upload', () => {
163
- it('should upload file successfully with Buffer', async () => {
164
- const config: VercelBlobStorageConfig = {
165
- type: 'vercel-blob',
166
- token: 'test-token',
167
- }
168
- const provider = new VercelBlobStorageProvider(config)
169
- const fileBuffer = Buffer.from('test file content')
170
- const filename = 'test.txt'
171
-
172
- const result = await provider.upload(fileBuffer, filename)
173
-
174
- expect(put).toHaveBeenCalledWith(
175
- `${MOCK_TIMESTAMP}-${'a'.repeat(32)}.txt`,
176
- fileBuffer,
177
- expect.objectContaining({
178
- access: 'public',
179
- token: 'test-token',
180
- }),
181
- )
182
- expect(result.filename).toMatch(/\d+-[a-f0-9]+\.txt/)
183
- expect(result.url).toBe('https://blob.vercel-storage.com/test-file.txt')
184
- expect(result.size).toBe(fileBuffer.length)
185
- expect(result.contentType).toBe('application/octet-stream')
186
- expect(result.metadata?.downloadUrl).toBe(
187
- 'https://blob.vercel-storage.com/test-file.txt?download=1',
188
- )
189
- })
190
-
191
- it('should upload file successfully with Uint8Array', async () => {
192
- const config: VercelBlobStorageConfig = {
193
- type: 'vercel-blob',
194
- token: 'test-token',
195
- }
196
- const provider = new VercelBlobStorageProvider(config)
197
- const uint8Array = new Uint8Array([1, 2, 3, 4, 5])
198
-
199
- const result = await provider.upload(uint8Array, 'binary.dat')
200
-
201
- expect(put).toHaveBeenCalledWith(
202
- expect.stringMatching(/\d+-[a-f0-9]+\.dat$/),
203
- expect.any(Buffer),
204
- expect.any(Object),
205
- )
206
- expect(result.size).toBe(uint8Array.length)
207
- })
208
-
209
- it('should generate unique filenames by default', async () => {
210
- const config: VercelBlobStorageConfig = {
211
- type: 'vercel-blob',
212
- token: 'test-token',
213
- }
214
- const provider = new VercelBlobStorageProvider(config)
215
- const fileBuffer = Buffer.from('test')
216
-
217
- const result = await provider.upload(fileBuffer, 'test.txt')
218
-
219
- expect(result.filename).not.toBe('test.txt')
220
- expect(result.filename).toMatch(/\d+-[a-f0-9]+\.txt/)
221
- })
222
-
223
- it('should preserve original filename when generateUniqueFilenames is false', async () => {
224
- const config: VercelBlobStorageConfig = {
225
- type: 'vercel-blob',
226
- token: 'test-token',
227
- generateUniqueFilenames: false,
228
- }
229
- const provider = new VercelBlobStorageProvider(config)
230
-
231
- const result = await provider.upload(Buffer.from('test'), 'original.txt')
232
-
233
- expect(put).toHaveBeenCalledWith('original.txt', expect.any(Buffer), expect.any(Object))
234
- expect(result.filename).toBe('original.txt')
235
- })
236
-
237
- it('should preserve file extension when generating unique names', async () => {
238
- const config: VercelBlobStorageConfig = {
239
- type: 'vercel-blob',
240
- token: 'test-token',
241
- }
242
- const provider = new VercelBlobStorageProvider(config)
243
-
244
- const resultTxt = await provider.upload(Buffer.from('test'), 'file.txt')
245
- const resultJpg = await provider.upload(Buffer.from('test'), 'photo.jpg')
246
- const resultNoExt = await provider.upload(Buffer.from('test'), 'noext')
247
-
248
- expect(resultTxt.filename).toMatch(/\.txt$/)
249
- expect(resultJpg.filename).toMatch(/\.jpg$/)
250
- expect(resultNoExt.filename).not.toMatch(/\./)
251
- })
252
-
253
- it('should generate a clean filename with no trailing fragment for extension-less originals', async () => {
254
- const config: VercelBlobStorageConfig = {
255
- type: 'vercel-blob',
256
- token: 'test-token',
257
- }
258
- const provider = new VercelBlobStorageProvider(config)
259
-
260
- const result = await provider.upload(Buffer.from('test'), 'README')
261
-
262
- expect(result.filename).toBe(`${MOCK_TIMESTAMP}-${'a'.repeat(32)}`)
263
- expect(put).toHaveBeenCalledWith(
264
- `${MOCK_TIMESTAMP}-${'a'.repeat(32)}`,
265
- expect.any(Buffer),
266
- expect.any(Object),
267
- )
268
- })
269
-
270
- it('should apply pathPrefix to uploaded files', async () => {
271
- const config: VercelBlobStorageConfig = {
272
- type: 'vercel-blob',
273
- token: 'test-token',
274
- pathPrefix: 'avatars',
275
- generateUniqueFilenames: false,
276
- }
277
- const provider = new VercelBlobStorageProvider(config)
278
-
279
- await provider.upload(Buffer.from('test'), 'profile.jpg')
280
-
281
- expect(put).toHaveBeenCalledWith(
282
- 'avatars/profile.jpg',
283
- expect.any(Buffer),
284
- expect.any(Object),
285
- )
286
- })
287
-
288
- it('should use provided contentType', async () => {
289
- const config: VercelBlobStorageConfig = {
290
- type: 'vercel-blob',
291
- token: 'test-token',
292
- }
293
- const provider = new VercelBlobStorageProvider(config)
294
-
295
- const result = await provider.upload(Buffer.from('test'), 'test.txt', {
296
- contentType: 'text/plain',
297
- })
298
-
299
- expect(put).toHaveBeenCalledWith(
300
- expect.any(String),
301
- expect.any(Buffer),
302
- expect.objectContaining({
303
- contentType: 'text/plain',
304
- }),
305
- )
306
- expect(result.contentType).toBe('text/plain')
307
- })
308
-
309
- it('should set access to public by default', async () => {
310
- const config: VercelBlobStorageConfig = {
311
- type: 'vercel-blob',
312
- token: 'test-token',
313
- }
314
- const provider = new VercelBlobStorageProvider(config)
315
-
316
- await provider.upload(Buffer.from('test'), 'test.txt')
317
-
318
- expect(put).toHaveBeenCalledWith(
319
- expect.any(String),
320
- expect.any(Buffer),
321
- expect.objectContaining({
322
- access: 'public',
323
- }),
324
- )
325
- })
326
-
327
- it('should set access to private when config.public is false', async () => {
328
- const config: VercelBlobStorageConfig = {
329
- type: 'vercel-blob',
330
- token: 'test-token',
331
- public: false,
332
- }
333
- const provider = new VercelBlobStorageProvider(config)
334
-
335
- await provider.upload(Buffer.from('test'), 'test.txt')
336
-
337
- expect(put).toHaveBeenCalledWith(
338
- expect.any(String),
339
- expect.any(Buffer),
340
- expect.objectContaining({
341
- access: 'private',
342
- }),
343
- )
344
- })
345
-
346
- it('should apply cacheControlMaxAge when configured', async () => {
347
- const config: VercelBlobStorageConfig = {
348
- type: 'vercel-blob',
349
- token: 'test-token',
350
- cacheControlMaxAge: 3600,
351
- }
352
- const provider = new VercelBlobStorageProvider(config)
353
-
354
- await provider.upload(Buffer.from('test'), 'test.txt')
355
-
356
- expect(put).toHaveBeenCalledWith(
357
- expect.any(String),
358
- expect.any(Buffer),
359
- expect.objectContaining({
360
- cacheControlMaxAge: 3600,
361
- }),
362
- )
363
- })
364
-
365
- it('should apply an explicit cacheControlMaxAge of 0', async () => {
366
- const config: VercelBlobStorageConfig = {
367
- type: 'vercel-blob',
368
- token: 'test-token',
369
- cacheControlMaxAge: 0,
370
- }
371
- const provider = new VercelBlobStorageProvider(config)
372
-
373
- await provider.upload(Buffer.from('test'), 'test.txt')
374
-
375
- expect(put).toHaveBeenCalledWith(
376
- expect.any(String),
377
- expect.any(Buffer),
378
- expect.objectContaining({
379
- cacheControlMaxAge: 0,
380
- }),
381
- )
382
- })
383
-
384
- it('should omit cacheControlMaxAge entirely when not configured', async () => {
385
- const config: VercelBlobStorageConfig = {
386
- type: 'vercel-blob',
387
- token: 'test-token',
388
- }
389
- const provider = new VercelBlobStorageProvider(config)
390
-
391
- await provider.upload(Buffer.from('test'), 'test.txt')
392
-
393
- const putOptions = put.mock.calls[0][2] as Record<string, unknown>
394
- expect('cacheControlMaxAge' in putOptions).toBe(false)
395
- })
396
-
397
- it('should include custom metadata in result', async () => {
398
- const config: VercelBlobStorageConfig = {
399
- type: 'vercel-blob',
400
- token: 'test-token',
401
- }
402
- const provider = new VercelBlobStorageProvider(config)
403
- const metadata = { uploadedBy: 'user123', category: 'documents' }
404
-
405
- const result = await provider.upload(Buffer.from('test'), 'test.txt', { metadata })
406
-
407
- expect(result.metadata).toMatchObject(metadata)
408
- expect(result.metadata?.downloadUrl).toBe(
409
- 'https://blob.vercel-storage.com/test-file.txt?download=1',
410
- )
411
- expect(result.metadata?.pathname).toBe('test-file.txt')
412
- })
413
-
414
- it('should pass storeId and oidcToken through for OIDC auth', async () => {
415
- const config: VercelBlobStorageConfig = {
416
- type: 'vercel-blob',
417
- storeId: 'store_abc123',
418
- oidcToken: 'oidc-token-789',
419
- }
420
- const provider = new VercelBlobStorageProvider(config)
421
-
422
- await provider.upload(Buffer.from('test'), 'test.txt')
423
-
424
- expect(put).toHaveBeenCalledWith(
425
- expect.any(String),
426
- expect.any(Buffer),
427
- expect.objectContaining({
428
- storeId: 'store_abc123',
429
- oidcToken: 'oidc-token-789',
430
- }),
431
- )
432
- const putOptions = put.mock.calls[0][2] as Record<string, unknown>
433
- expect('token' in putOptions).toBe(false)
434
- })
435
-
436
- it('should omit undefined auth options so SDK env fallbacks apply', async () => {
437
- const config: VercelBlobStorageConfig = {
438
- type: 'vercel-blob',
439
- token: 'test-token',
440
- }
441
- const provider = new VercelBlobStorageProvider(config)
442
-
443
- await provider.upload(Buffer.from('test'), 'test.txt')
444
-
445
- const putOptions = put.mock.calls[0][2] as Record<string, unknown>
446
- expect(putOptions.token).toBe('test-token')
447
- expect('storeId' in putOptions).toBe(false)
448
- expect('oidcToken' in putOptions).toBe(false)
449
- })
450
-
451
- it('should handle upload errors', async () => {
452
- const config: VercelBlobStorageConfig = {
453
- type: 'vercel-blob',
454
- token: 'test-token',
455
- }
456
- const provider = new VercelBlobStorageProvider(config)
457
- put.mockRejectedValueOnce(new Error('Upload failed'))
458
-
459
- await expect(provider.upload(Buffer.from('test'), 'test.txt')).rejects.toThrow(
460
- 'Upload failed',
461
- )
462
- })
463
-
464
- describe('allowOverwrite', () => {
465
- it('should not send allowOverwrite by default with unique filenames (real API defaults to reject)', async () => {
466
- const config: VercelBlobStorageConfig = {
467
- type: 'vercel-blob',
468
- token: 'test-token',
469
- }
470
- const provider = new VercelBlobStorageProvider(config)
471
-
472
- await provider.upload(Buffer.from('test'), 'test.txt')
473
-
474
- const putOptions = put.mock.calls[0][2] as Record<string, unknown>
475
- expect('allowOverwrite' in putOptions).toBe(false)
476
- })
477
-
478
- it('should default allowOverwrite to true when generateUniqueFilenames is false', async () => {
479
- const config: VercelBlobStorageConfig = {
480
- type: 'vercel-blob',
481
- token: 'test-token',
482
- generateUniqueFilenames: false,
483
- }
484
- const provider = new VercelBlobStorageProvider(config)
485
-
486
- await provider.upload(Buffer.from('test'), 'stable-name.txt')
487
-
488
- expect(put).toHaveBeenCalledWith(
489
- 'stable-name.txt',
490
- expect.any(Buffer),
491
- expect.objectContaining({ allowOverwrite: true }),
492
- )
493
- })
494
-
495
- it('should let an explicit allowOverwrite: false win even with stable filenames', async () => {
496
- const config: VercelBlobStorageConfig = {
497
- type: 'vercel-blob',
498
- token: 'test-token',
499
- generateUniqueFilenames: false,
500
- allowOverwrite: false,
501
- }
502
- const provider = new VercelBlobStorageProvider(config)
503
-
504
- await provider.upload(Buffer.from('test'), 'stable-name.txt')
505
-
506
- expect(put).toHaveBeenCalledWith(
507
- 'stable-name.txt',
508
- expect.any(Buffer),
509
- expect.objectContaining({ allowOverwrite: false }),
510
- )
511
- })
512
-
513
- it('should let an explicit allowOverwrite: true win with generated filenames', async () => {
514
- const config: VercelBlobStorageConfig = {
515
- type: 'vercel-blob',
516
- token: 'test-token',
517
- allowOverwrite: true,
518
- }
519
- const provider = new VercelBlobStorageProvider(config)
520
-
521
- await provider.upload(Buffer.from('test'), 'test.txt')
522
-
523
- expect(put).toHaveBeenCalledWith(
524
- expect.any(String),
525
- expect.any(Buffer),
526
- expect.objectContaining({ allowOverwrite: true }),
527
- )
528
- })
529
-
530
- it('should reject an upload to an existing pathname when allowOverwrite resolves to false (real API behavior)', async () => {
531
- const config: VercelBlobStorageConfig = {
532
- type: 'vercel-blob',
533
- token: 'test-token',
534
- }
535
- const provider = new VercelBlobStorageProvider(config)
536
- put.mockRejectedValueOnce(new Error('This blob already exists, use `allowOverwrite`'))
537
-
538
- await expect(provider.upload(Buffer.from('test'), 'test.txt')).rejects.toThrow(
539
- /already exists/,
540
- )
541
- })
542
- })
543
- })
544
-
545
- describe('download', () => {
546
- it('should download a public file successfully and convert to Buffer', async () => {
547
- const config: VercelBlobStorageConfig = {
548
- type: 'vercel-blob',
549
- token: 'test-token',
550
- }
551
- const provider = new VercelBlobStorageProvider(config)
552
-
553
- get.mockResolvedValueOnce({
554
- statusCode: 200,
555
- stream: createReadableStream(new Uint8Array([1, 2, 3, 4, 5])),
556
- headers: new Headers(),
557
- blob: {
558
- url: 'https://blob.vercel-storage.com/test.txt',
559
- downloadUrl: 'https://blob.vercel-storage.com/test.txt?download=1',
560
- pathname: 'test.txt',
561
- contentDisposition: '',
562
- cacheControl: '',
563
- uploadedAt: new Date(),
564
- etag: 'etag',
565
- contentType: 'text/plain',
566
- size: 5,
567
- },
568
- })
569
-
570
- const result = await provider.download('test.txt')
571
-
572
- expect(get).toHaveBeenCalledWith('test.txt', { access: 'public', token: 'test-token' })
573
- expect(result).toBeInstanceOf(Buffer)
574
- expect(result.length).toBe(5)
575
- expect(Array.from(result)).toEqual([1, 2, 3, 4, 5])
576
- })
577
-
578
- it('should download a private file using private access', async () => {
579
- const config: VercelBlobStorageConfig = {
580
- type: 'vercel-blob',
581
- token: 'test-token',
582
- public: false,
583
- }
584
- const provider = new VercelBlobStorageProvider(config)
585
-
586
- const result = await provider.download('secret.pdf')
587
-
588
- expect(get).toHaveBeenCalledWith('secret.pdf', { access: 'private', token: 'test-token' })
589
- expect(result).toBeInstanceOf(Buffer)
590
- })
591
-
592
- it('should apply pathPrefix when downloading', async () => {
593
- const config: VercelBlobStorageConfig = {
594
- type: 'vercel-blob',
595
- token: 'test-token',
596
- pathPrefix: 'documents',
597
- }
598
- const provider = new VercelBlobStorageProvider(config)
599
-
600
- await provider.download('report.pdf')
601
-
602
- expect(get).toHaveBeenCalledWith('documents/report.pdf', {
603
- access: 'public',
604
- token: 'test-token',
605
- })
606
- })
607
-
608
- it('should pass storeId through to get for OIDC auth', async () => {
609
- const config: VercelBlobStorageConfig = {
610
- type: 'vercel-blob',
611
- storeId: 'store_abc123',
612
- }
613
- const provider = new VercelBlobStorageProvider(config)
614
-
615
- await provider.download('test.txt')
616
-
617
- expect(get).toHaveBeenCalledWith('test.txt', { access: 'public', storeId: 'store_abc123' })
618
- })
619
-
620
- it('should throw a descriptive error when the file is not found (get resolves null)', async () => {
621
- const config: VercelBlobStorageConfig = {
622
- type: 'vercel-blob',
623
- token: 'test-token',
624
- }
625
- const provider = new VercelBlobStorageProvider(config)
626
- get.mockResolvedValueOnce(null)
627
-
628
- await expect(provider.download('nonexistent.txt')).rejects.toThrow(
629
- 'File not found: nonexistent.txt',
630
- )
631
- })
632
-
633
- it('should propagate non-not-found errors from get', async () => {
634
- const config: VercelBlobStorageConfig = {
635
- type: 'vercel-blob',
636
- token: 'test-token',
637
- }
638
- const provider = new VercelBlobStorageProvider(config)
639
- get.mockRejectedValueOnce(new Error('Network error'))
640
-
641
- await expect(provider.download('test.txt')).rejects.toThrow('Network error')
642
- })
643
- })
644
-
645
- describe('delete', () => {
646
- it('should delete file successfully by pathname, without a head() round-trip', async () => {
647
- const config: VercelBlobStorageConfig = {
648
- type: 'vercel-blob',
649
- token: 'test-token',
650
- }
651
- const provider = new VercelBlobStorageProvider(config)
652
-
653
- await provider.delete('test.txt')
654
-
655
- expect(head).not.toHaveBeenCalled()
656
- expect(del).toHaveBeenCalledWith('test.txt', {
657
- token: 'test-token',
658
- })
659
- })
660
-
661
- it('should apply pathPrefix when deleting', async () => {
662
- const config: VercelBlobStorageConfig = {
663
- type: 'vercel-blob',
664
- token: 'test-token',
665
- pathPrefix: 'temp',
666
- }
667
- const provider = new VercelBlobStorageProvider(config)
668
-
669
- await provider.delete('old-file.txt')
670
-
671
- expect(del).toHaveBeenCalledWith('temp/old-file.txt', { token: 'test-token' })
672
- })
673
-
674
- it('should pass storeId through to del for OIDC auth', async () => {
675
- const config: VercelBlobStorageConfig = {
676
- type: 'vercel-blob',
677
- storeId: 'store_abc123',
678
- }
679
- const provider = new VercelBlobStorageProvider(config)
680
-
681
- await provider.delete('test.txt')
682
-
683
- expect(head).not.toHaveBeenCalled()
684
- expect(del).toHaveBeenCalledWith('test.txt', { storeId: 'store_abc123' })
685
- })
686
-
687
- it('should resolve without error when deleting a nonexistent pathname (idempotent)', async () => {
688
- const config: VercelBlobStorageConfig = {
689
- type: 'vercel-blob',
690
- token: 'test-token',
691
- }
692
- const provider = new VercelBlobStorageProvider(config)
693
- del.mockRejectedValueOnce(new BlobNotFoundError())
694
-
695
- await expect(provider.delete('nonexistent.txt')).resolves.toBeUndefined()
696
- })
697
-
698
- it('should propagate non-not-found deletion errors', async () => {
699
- const config: VercelBlobStorageConfig = {
700
- type: 'vercel-blob',
701
- token: 'test-token',
702
- }
703
- const provider = new VercelBlobStorageProvider(config)
704
-
705
- del.mockRejectedValueOnce(new Error('Deletion failed'))
706
-
707
- await expect(provider.delete('test.txt')).rejects.toThrow('Deletion failed')
708
- })
709
- })
710
-
711
- describe('getUrl', () => {
712
- const VALID_TOKEN = 'vercel_blob_rw_STORE123_supersecretvalue'
713
-
714
- it('should return the public-host URL derived from the token store ID', () => {
715
- const config: VercelBlobStorageConfig = {
716
- type: 'vercel-blob',
717
- token: VALID_TOKEN,
718
- }
719
- const provider = new VercelBlobStorageProvider(config)
720
-
721
- const url = provider.getUrl('photo.jpg')
722
-
723
- expect(url).toBe('https://STORE123.public.blob.vercel-storage.com/photo.jpg')
724
- })
725
-
726
- it('should return the private-host URL when public is false', () => {
727
- const config: VercelBlobStorageConfig = {
728
- type: 'vercel-blob',
729
- token: VALID_TOKEN,
730
- public: false,
731
- }
732
- const provider = new VercelBlobStorageProvider(config)
733
-
734
- const url = provider.getUrl('secret.pdf')
735
-
736
- expect(url).toBe('https://STORE123.private.blob.vercel-storage.com/secret.pdf')
737
- })
738
-
739
- it('should apply pathPrefix to URL', () => {
740
- const config: VercelBlobStorageConfig = {
741
- type: 'vercel-blob',
742
- token: VALID_TOKEN,
743
- pathPrefix: 'images',
744
- }
745
- const provider = new VercelBlobStorageProvider(config)
746
-
747
- const url = provider.getUrl('photo.jpg')
748
-
749
- expect(url).toBe('https://STORE123.public.blob.vercel-storage.com/images/photo.jpg')
750
- })
751
-
752
- it('should handle filenames with special characters', () => {
753
- const config: VercelBlobStorageConfig = {
754
- type: 'vercel-blob',
755
- token: VALID_TOKEN,
756
- }
757
- const provider = new VercelBlobStorageProvider(config)
758
-
759
- const url = provider.getUrl('my file (1).txt')
760
-
761
- expect(url).toBe('https://STORE123.public.blob.vercel-storage.com/my file (1).txt')
762
- })
763
-
764
- it('should derive the store ID from an explicit storeId when using OIDC auth', () => {
765
- const config: VercelBlobStorageConfig = {
766
- type: 'vercel-blob',
767
- storeId: 'store_abc123',
768
- oidcToken: 'oidc-token-789',
769
- }
770
- const provider = new VercelBlobStorageProvider(config)
771
-
772
- const url = provider.getUrl('photo.jpg')
773
-
774
- expect(url).toBe('https://abc123.public.blob.vercel-storage.com/photo.jpg')
775
- })
776
-
777
- it('should derive the store ID from BLOB_STORE_ID env var', () => {
778
- process.env.BLOB_STORE_ID = 'store_envstore'
779
-
780
- const config: VercelBlobStorageConfig = { type: 'vercel-blob' }
781
- const provider = new VercelBlobStorageProvider(config)
782
-
783
- const url = provider.getUrl('photo.jpg')
784
-
785
- expect(url).toBe('https://envstore.public.blob.vercel-storage.com/photo.jpg')
786
-
787
- delete process.env.BLOB_STORE_ID
788
- })
789
-
790
- it('should derive the store ID from BLOB_READ_WRITE_TOKEN env var', () => {
791
- process.env.BLOB_READ_WRITE_TOKEN = 'vercel_blob_rw_ENVSTORE_secret'
792
-
793
- const config: VercelBlobStorageConfig = { type: 'vercel-blob' }
794
- const provider = new VercelBlobStorageProvider(config)
795
-
796
- const url = provider.getUrl('photo.jpg')
797
-
798
- expect(url).toBe('https://ENVSTORE.public.blob.vercel-storage.com/photo.jpg')
799
-
800
- delete process.env.BLOB_READ_WRITE_TOKEN
801
- })
802
-
803
- it('should prefer the explicit token over storeId when both are set', () => {
804
- const config: VercelBlobStorageConfig = {
805
- type: 'vercel-blob',
806
- token: VALID_TOKEN,
807
- storeId: 'store_other',
808
- }
809
- const provider = new VercelBlobStorageProvider(config)
810
-
811
- const url = provider.getUrl('photo.jpg')
812
-
813
- expect(url).toBe('https://STORE123.public.blob.vercel-storage.com/photo.jpg')
814
- })
815
-
816
- it('should throw a descriptive error when the token does not yield a store ID', () => {
817
- const config: VercelBlobStorageConfig = {
818
- type: 'vercel-blob',
819
- token: 'not-a-real-token',
820
- }
821
- const provider = new VercelBlobStorageProvider(config)
822
-
823
- expect(() => provider.getUrl('photo.jpg')).toThrow(
824
- /Cannot determine the Vercel Blob store ID/,
825
- )
826
- })
827
-
828
- it('should throw a descriptive error when BLOB_READ_WRITE_TOKEN does not yield a store ID', () => {
829
- process.env.BLOB_READ_WRITE_TOKEN = 'not-a-real-token'
830
-
831
- const config: VercelBlobStorageConfig = { type: 'vercel-blob' }
832
- const provider = new VercelBlobStorageProvider(config)
833
-
834
- expect(() => provider.getUrl('photo.jpg')).toThrow(
835
- /Cannot determine the Vercel Blob store ID/,
836
- )
837
-
838
- delete process.env.BLOB_READ_WRITE_TOKEN
839
- })
840
-
841
- it('should throw a descriptive error when no credentials are configured at all', () => {
842
- delete process.env.BLOB_READ_WRITE_TOKEN
843
- delete process.env.BLOB_STORE_ID
844
-
845
- const config: VercelBlobStorageConfig = { type: 'vercel-blob' }
846
- const provider = new VercelBlobStorageProvider(config)
847
-
848
- expect(() => provider.getUrl('photo.jpg')).toThrow(
849
- /Cannot determine the Vercel Blob store ID/,
850
- )
851
- })
852
- })
853
-
854
- describe('getSignedUrl', () => {
855
- it('should issue a signed token and return a presigned URL for a private file', async () => {
856
- const config: VercelBlobStorageConfig = {
857
- type: 'vercel-blob',
858
- token: 'test-token',
859
- public: false,
860
- }
861
- const provider = new VercelBlobStorageProvider(config)
862
-
863
- const url = await provider.getSignedUrl('secret.pdf', 1800)
864
-
865
- expect(issueSignedToken).toHaveBeenCalledWith({
866
- pathname: 'secret.pdf',
867
- operations: ['get'],
868
- validUntil: MOCK_TIMESTAMP + 1800_000,
869
- token: 'test-token',
870
- })
871
- expect(presignUrl).toHaveBeenCalledWith(
872
- {
873
- delegationToken: 'delegation-token',
874
- clientSigningToken: 'client-signing-token',
875
- },
876
- {
877
- operation: 'get',
878
- pathname: 'secret.pdf',
879
- validUntil: MOCK_TIMESTAMP + 1800_000,
880
- access: 'private',
881
- },
882
- )
883
- expect(url).toBe('https://blob.vercel-storage.com/test-file.txt?signature=abc')
884
- })
885
-
886
- it('should default to a 1 hour expiry', async () => {
887
- const config: VercelBlobStorageConfig = {
888
- type: 'vercel-blob',
889
- token: 'test-token',
890
- }
891
- const provider = new VercelBlobStorageProvider(config)
892
-
893
- await provider.getSignedUrl('report.pdf')
894
-
895
- expect(issueSignedToken).toHaveBeenCalledWith(
896
- expect.objectContaining({ validUntil: MOCK_TIMESTAMP + 3600_000 }),
897
- )
898
- })
899
-
900
- it('should use public access for a public file', async () => {
901
- const config: VercelBlobStorageConfig = {
902
- type: 'vercel-blob',
903
- token: 'test-token',
904
- }
905
- const provider = new VercelBlobStorageProvider(config)
906
-
907
- await provider.getSignedUrl('photo.jpg')
908
-
909
- expect(presignUrl).toHaveBeenCalledWith(
910
- expect.anything(),
911
- expect.objectContaining({ access: 'public' }),
912
- )
913
- })
914
-
915
- it('should apply pathPrefix to the signed pathname', async () => {
916
- const config: VercelBlobStorageConfig = {
917
- type: 'vercel-blob',
918
- token: 'test-token',
919
- pathPrefix: 'documents',
920
- }
921
- const provider = new VercelBlobStorageProvider(config)
922
-
923
- await provider.getSignedUrl('report.pdf')
924
-
925
- expect(issueSignedToken).toHaveBeenCalledWith(
926
- expect.objectContaining({ pathname: 'documents/report.pdf' }),
927
- )
928
- expect(presignUrl).toHaveBeenCalledWith(
929
- expect.anything(),
930
- expect.objectContaining({ pathname: 'documents/report.pdf' }),
931
- )
932
- })
933
- })
934
-
935
- describe('vercelBlobStorage factory', () => {
936
- it('should create config with correct defaults', () => {
937
- const config = vercelBlobStorage({ token: 'test-token' })
938
-
939
- expect(config).toEqual({
940
- type: 'vercel-blob',
941
- token: 'test-token',
942
- generateUniqueFilenames: true,
943
- public: true,
944
- })
945
- })
946
-
947
- it('should merge provided options with defaults', () => {
948
- const config = vercelBlobStorage({
949
- token: 'test-token',
950
- pathPrefix: 'uploads',
951
- generateUniqueFilenames: false,
952
- cacheControlMaxAge: 7200,
953
- })
954
-
955
- expect(config).toEqual({
956
- type: 'vercel-blob',
957
- token: 'test-token',
958
- pathPrefix: 'uploads',
959
- generateUniqueFilenames: false,
960
- public: true,
961
- cacheControlMaxAge: 7200,
962
- })
963
- })
964
-
965
- it('should set type to vercel-blob', () => {
966
- const config = vercelBlobStorage({ token: 'test-token' })
967
-
968
- expect(config.type).toBe('vercel-blob')
969
- })
970
-
971
- it('should pass through OIDC auth options', () => {
972
- const config = vercelBlobStorage({
973
- storeId: 'store_abc123',
974
- oidcToken: 'oidc-token-789',
975
- })
976
-
977
- expect(config).toEqual({
978
- type: 'vercel-blob',
979
- generateUniqueFilenames: true,
980
- public: true,
981
- storeId: 'store_abc123',
982
- oidcToken: 'oidc-token-789',
983
- })
984
- })
985
- })
986
- })