@opensaas/stack-storage-vercel 0.30.0 → 0.31.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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +57 -0
- package/README.md +72 -4
- package/dist/index.d.ts +64 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +149 -33
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +189 -39
- package/tests/vercel-blob-provider.test.ts +514 -93
|
@@ -2,11 +2,26 @@ import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
2
2
|
import { VercelBlobStorageProvider, vercelBlobStorage } from '../src/index.js'
|
|
3
3
|
import type { VercelBlobStorageConfig } from '../src/index.js'
|
|
4
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
|
+
|
|
5
16
|
// Mock Vercel Blob SDK
|
|
6
17
|
vi.mock('@vercel/blob', () => ({
|
|
7
18
|
put: vi.fn(),
|
|
8
19
|
del: vi.fn(),
|
|
9
20
|
head: vi.fn(),
|
|
21
|
+
get: vi.fn(),
|
|
22
|
+
issueSignedToken: vi.fn(),
|
|
23
|
+
presignUrl: vi.fn(),
|
|
24
|
+
BlobNotFoundError,
|
|
10
25
|
}))
|
|
11
26
|
|
|
12
27
|
// Mock crypto for deterministic filename testing
|
|
@@ -20,14 +35,22 @@ vi.mock('node:crypto', () => ({
|
|
|
20
35
|
const MOCK_TIMESTAMP = 1234567890000
|
|
21
36
|
vi.spyOn(Date, 'now').mockReturnValue(MOCK_TIMESTAMP)
|
|
22
37
|
|
|
23
|
-
|
|
24
|
-
|
|
38
|
+
function createReadableStream(bytes: Uint8Array): ReadableStream<Uint8Array> {
|
|
39
|
+
return new ReadableStream({
|
|
40
|
+
start(controller) {
|
|
41
|
+
controller.enqueue(bytes)
|
|
42
|
+
controller.close()
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
}
|
|
25
46
|
|
|
26
47
|
describe('VercelBlobStorageProvider', () => {
|
|
27
48
|
let put: ReturnType<typeof vi.fn>
|
|
28
49
|
let del: ReturnType<typeof vi.fn>
|
|
29
50
|
let head: ReturnType<typeof vi.fn>
|
|
30
|
-
let
|
|
51
|
+
let get: ReturnType<typeof vi.fn>
|
|
52
|
+
let issueSignedToken: ReturnType<typeof vi.fn>
|
|
53
|
+
let presignUrl: ReturnType<typeof vi.fn>
|
|
31
54
|
|
|
32
55
|
beforeEach(async () => {
|
|
33
56
|
vi.clearAllMocks()
|
|
@@ -37,7 +60,9 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
37
60
|
put = vercelBlob.put as ReturnType<typeof vi.fn>
|
|
38
61
|
del = vercelBlob.del as ReturnType<typeof vi.fn>
|
|
39
62
|
head = vercelBlob.head as ReturnType<typeof vi.fn>
|
|
40
|
-
|
|
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>
|
|
41
66
|
|
|
42
67
|
// Default mock implementations
|
|
43
68
|
put.mockResolvedValue({
|
|
@@ -54,10 +79,31 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
54
79
|
uploadedAt: new Date(),
|
|
55
80
|
})
|
|
56
81
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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',
|
|
61
107
|
})
|
|
62
108
|
})
|
|
63
109
|
|
|
@@ -87,16 +133,29 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
87
133
|
delete process.env.BLOB_READ_WRITE_TOKEN
|
|
88
134
|
})
|
|
89
135
|
|
|
90
|
-
it('should throw
|
|
136
|
+
it('should not throw when no static token is configured (OIDC auth)', () => {
|
|
91
137
|
delete process.env.BLOB_READ_WRITE_TOKEN
|
|
92
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.
|
|
93
141
|
const config: VercelBlobStorageConfig = {
|
|
94
142
|
type: 'vercel-blob',
|
|
143
|
+
storeId: 'store_abc123',
|
|
95
144
|
}
|
|
96
145
|
|
|
97
|
-
expect(() => new VercelBlobStorageProvider(config)).toThrow(
|
|
98
|
-
|
|
99
|
-
|
|
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()
|
|
100
159
|
})
|
|
101
160
|
})
|
|
102
161
|
|
|
@@ -191,6 +250,23 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
191
250
|
expect(resultNoExt.filename).not.toMatch(/\./)
|
|
192
251
|
})
|
|
193
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
|
+
|
|
194
270
|
it('should apply pathPrefix to uploaded files', async () => {
|
|
195
271
|
const config: VercelBlobStorageConfig = {
|
|
196
272
|
type: 'vercel-blob',
|
|
@@ -248,7 +324,7 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
248
324
|
)
|
|
249
325
|
})
|
|
250
326
|
|
|
251
|
-
it('should
|
|
327
|
+
it('should set access to private when config.public is false', async () => {
|
|
252
328
|
const config: VercelBlobStorageConfig = {
|
|
253
329
|
type: 'vercel-blob',
|
|
254
330
|
token: 'test-token',
|
|
@@ -262,7 +338,7 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
262
338
|
expect.any(String),
|
|
263
339
|
expect.any(Buffer),
|
|
264
340
|
expect.objectContaining({
|
|
265
|
-
access: '
|
|
341
|
+
access: 'private',
|
|
266
342
|
}),
|
|
267
343
|
)
|
|
268
344
|
})
|
|
@@ -286,6 +362,38 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
286
362
|
)
|
|
287
363
|
})
|
|
288
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
|
+
|
|
289
397
|
it('should include custom metadata in result', async () => {
|
|
290
398
|
const config: VercelBlobStorageConfig = {
|
|
291
399
|
type: 'vercel-blob',
|
|
@@ -303,6 +411,43 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
303
411
|
expect(result.metadata?.pathname).toBe('test-file.txt')
|
|
304
412
|
})
|
|
305
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
|
+
|
|
306
451
|
it('should handle upload errors', async () => {
|
|
307
452
|
const config: VercelBlobStorageConfig = {
|
|
308
453
|
type: 'vercel-blob',
|
|
@@ -315,36 +460,133 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
315
460
|
'Upload failed',
|
|
316
461
|
)
|
|
317
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
|
+
})
|
|
318
543
|
})
|
|
319
544
|
|
|
320
545
|
describe('download', () => {
|
|
321
|
-
it('should download file successfully and convert to Buffer', async () => {
|
|
546
|
+
it('should download a public file successfully and convert to Buffer', async () => {
|
|
322
547
|
const config: VercelBlobStorageConfig = {
|
|
323
548
|
type: 'vercel-blob',
|
|
324
549
|
token: 'test-token',
|
|
325
550
|
}
|
|
326
551
|
const provider = new VercelBlobStorageProvider(config)
|
|
327
|
-
const mockArrayBuffer = new Uint8Array([1, 2, 3, 4, 5]).buffer
|
|
328
|
-
|
|
329
|
-
head.mockResolvedValueOnce({
|
|
330
|
-
url: 'https://blob.vercel-storage.com/test.txt',
|
|
331
|
-
pathname: 'test.txt',
|
|
332
|
-
size: 5,
|
|
333
|
-
uploadedAt: new Date(),
|
|
334
|
-
})
|
|
335
552
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
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
|
+
},
|
|
340
568
|
})
|
|
341
569
|
|
|
342
570
|
const result = await provider.download('test.txt')
|
|
343
571
|
|
|
344
|
-
expect(
|
|
345
|
-
expect(fetch).toHaveBeenCalledWith('https://blob.vercel-storage.com/test.txt')
|
|
572
|
+
expect(get).toHaveBeenCalledWith('test.txt', { access: 'public', token: 'test-token' })
|
|
346
573
|
expect(result).toBeInstanceOf(Buffer)
|
|
347
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)
|
|
348
590
|
})
|
|
349
591
|
|
|
350
592
|
it('should apply pathPrefix when downloading', async () => {
|
|
@@ -357,77 +599,61 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
357
599
|
|
|
358
600
|
await provider.download('report.pdf')
|
|
359
601
|
|
|
360
|
-
expect(
|
|
602
|
+
expect(get).toHaveBeenCalledWith('documents/report.pdf', {
|
|
603
|
+
access: 'public',
|
|
604
|
+
token: 'test-token',
|
|
605
|
+
})
|
|
361
606
|
})
|
|
362
607
|
|
|
363
|
-
it('should
|
|
608
|
+
it('should pass storeId through to get for OIDC auth', async () => {
|
|
364
609
|
const config: VercelBlobStorageConfig = {
|
|
365
610
|
type: 'vercel-blob',
|
|
366
|
-
|
|
611
|
+
storeId: 'store_abc123',
|
|
367
612
|
}
|
|
368
613
|
const provider = new VercelBlobStorageProvider(config)
|
|
369
|
-
head.mockResolvedValueOnce(null)
|
|
370
614
|
|
|
371
|
-
await
|
|
372
|
-
|
|
373
|
-
)
|
|
374
|
-
expect(fetch).not.toHaveBeenCalled()
|
|
615
|
+
await provider.download('test.txt')
|
|
616
|
+
|
|
617
|
+
expect(get).toHaveBeenCalledWith('test.txt', { access: 'public', storeId: 'store_abc123' })
|
|
375
618
|
})
|
|
376
619
|
|
|
377
|
-
it('should throw error when
|
|
620
|
+
it('should throw a descriptive error when the file is not found (get resolves null)', async () => {
|
|
378
621
|
const config: VercelBlobStorageConfig = {
|
|
379
622
|
type: 'vercel-blob',
|
|
380
623
|
token: 'test-token',
|
|
381
624
|
}
|
|
382
625
|
const provider = new VercelBlobStorageProvider(config)
|
|
626
|
+
get.mockResolvedValueOnce(null)
|
|
383
627
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
statusText: 'Internal Server Error',
|
|
387
|
-
})
|
|
388
|
-
|
|
389
|
-
await expect(provider.download('test.txt')).rejects.toThrow(
|
|
390
|
-
'Failed to download file: Internal Server Error',
|
|
628
|
+
await expect(provider.download('nonexistent.txt')).rejects.toThrow(
|
|
629
|
+
'File not found: nonexistent.txt',
|
|
391
630
|
)
|
|
392
631
|
})
|
|
393
632
|
|
|
394
|
-
it('should
|
|
633
|
+
it('should propagate non-not-found errors from get', async () => {
|
|
395
634
|
const config: VercelBlobStorageConfig = {
|
|
396
635
|
type: 'vercel-blob',
|
|
397
636
|
token: 'test-token',
|
|
398
637
|
}
|
|
399
638
|
const provider = new VercelBlobStorageProvider(config)
|
|
639
|
+
get.mockRejectedValueOnce(new Error('Network error'))
|
|
400
640
|
|
|
401
|
-
|
|
402
|
-
ok: false,
|
|
403
|
-
statusText: 'Not Found',
|
|
404
|
-
})
|
|
405
|
-
|
|
406
|
-
await expect(provider.download('missing.txt')).rejects.toThrow(
|
|
407
|
-
'Failed to download file: Not Found',
|
|
408
|
-
)
|
|
641
|
+
await expect(provider.download('test.txt')).rejects.toThrow('Network error')
|
|
409
642
|
})
|
|
410
643
|
})
|
|
411
644
|
|
|
412
645
|
describe('delete', () => {
|
|
413
|
-
it('should delete file successfully', async () => {
|
|
646
|
+
it('should delete file successfully by pathname, without a head() round-trip', async () => {
|
|
414
647
|
const config: VercelBlobStorageConfig = {
|
|
415
648
|
type: 'vercel-blob',
|
|
416
649
|
token: 'test-token',
|
|
417
650
|
}
|
|
418
651
|
const provider = new VercelBlobStorageProvider(config)
|
|
419
652
|
|
|
420
|
-
head.mockResolvedValueOnce({
|
|
421
|
-
url: 'https://blob.vercel-storage.com/test.txt',
|
|
422
|
-
pathname: 'test.txt',
|
|
423
|
-
size: 100,
|
|
424
|
-
uploadedAt: new Date(),
|
|
425
|
-
})
|
|
426
|
-
|
|
427
653
|
await provider.delete('test.txt')
|
|
428
654
|
|
|
429
|
-
expect(head).
|
|
430
|
-
expect(del).toHaveBeenCalledWith('
|
|
655
|
+
expect(head).not.toHaveBeenCalled()
|
|
656
|
+
expect(del).toHaveBeenCalledWith('test.txt', {
|
|
431
657
|
token: 'test-token',
|
|
432
658
|
})
|
|
433
659
|
})
|
|
@@ -440,46 +666,41 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
440
666
|
}
|
|
441
667
|
const provider = new VercelBlobStorageProvider(config)
|
|
442
668
|
|
|
443
|
-
head.mockResolvedValueOnce({
|
|
444
|
-
url: 'https://blob.vercel-storage.com/temp/old-file.txt',
|
|
445
|
-
pathname: 'temp/old-file.txt',
|
|
446
|
-
size: 100,
|
|
447
|
-
uploadedAt: new Date(),
|
|
448
|
-
})
|
|
449
|
-
|
|
450
669
|
await provider.delete('old-file.txt')
|
|
451
670
|
|
|
452
|
-
expect(
|
|
671
|
+
expect(del).toHaveBeenCalledWith('temp/old-file.txt', { token: 'test-token' })
|
|
453
672
|
})
|
|
454
673
|
|
|
455
|
-
it('should
|
|
674
|
+
it('should pass storeId through to del for OIDC auth', async () => {
|
|
456
675
|
const config: VercelBlobStorageConfig = {
|
|
457
676
|
type: 'vercel-blob',
|
|
458
|
-
|
|
677
|
+
storeId: 'store_abc123',
|
|
459
678
|
}
|
|
460
679
|
const provider = new VercelBlobStorageProvider(config)
|
|
461
|
-
head.mockResolvedValueOnce(null)
|
|
462
680
|
|
|
463
|
-
|
|
464
|
-
await provider.delete('nonexistent.txt')
|
|
681
|
+
await provider.delete('test.txt')
|
|
465
682
|
|
|
466
|
-
expect(head).
|
|
467
|
-
expect(del).
|
|
683
|
+
expect(head).not.toHaveBeenCalled()
|
|
684
|
+
expect(del).toHaveBeenCalledWith('test.txt', { storeId: 'store_abc123' })
|
|
468
685
|
})
|
|
469
686
|
|
|
470
|
-
it('should
|
|
687
|
+
it('should resolve without error when deleting a nonexistent pathname (idempotent)', async () => {
|
|
471
688
|
const config: VercelBlobStorageConfig = {
|
|
472
689
|
type: 'vercel-blob',
|
|
473
690
|
token: 'test-token',
|
|
474
691
|
}
|
|
475
692
|
const provider = new VercelBlobStorageProvider(config)
|
|
693
|
+
del.mockRejectedValueOnce(new BlobNotFoundError())
|
|
476
694
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
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)
|
|
483
704
|
|
|
484
705
|
del.mockRejectedValueOnce(new Error('Deletion failed'))
|
|
485
706
|
|
|
@@ -488,41 +709,226 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
488
709
|
})
|
|
489
710
|
|
|
490
711
|
describe('getUrl', () => {
|
|
491
|
-
|
|
712
|
+
const VALID_TOKEN = 'vercel_blob_rw_STORE123_supersecretvalue'
|
|
713
|
+
|
|
714
|
+
it('should return the public-host URL derived from the token store ID', () => {
|
|
492
715
|
const config: VercelBlobStorageConfig = {
|
|
493
716
|
type: 'vercel-blob',
|
|
494
|
-
token:
|
|
717
|
+
token: VALID_TOKEN,
|
|
495
718
|
}
|
|
496
719
|
const provider = new VercelBlobStorageProvider(config)
|
|
497
720
|
|
|
498
721
|
const url = provider.getUrl('photo.jpg')
|
|
499
722
|
|
|
500
|
-
expect(url).toBe('https://blob.vercel-storage.com/photo.jpg')
|
|
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')
|
|
501
737
|
})
|
|
502
738
|
|
|
503
739
|
it('should apply pathPrefix to URL', () => {
|
|
504
740
|
const config: VercelBlobStorageConfig = {
|
|
505
741
|
type: 'vercel-blob',
|
|
506
|
-
token:
|
|
742
|
+
token: VALID_TOKEN,
|
|
507
743
|
pathPrefix: 'images',
|
|
508
744
|
}
|
|
509
745
|
const provider = new VercelBlobStorageProvider(config)
|
|
510
746
|
|
|
511
747
|
const url = provider.getUrl('photo.jpg')
|
|
512
748
|
|
|
513
|
-
expect(url).toBe('https://blob.vercel-storage.com/images/photo.jpg')
|
|
749
|
+
expect(url).toBe('https://STORE123.public.blob.vercel-storage.com/images/photo.jpg')
|
|
514
750
|
})
|
|
515
751
|
|
|
516
752
|
it('should handle filenames with special characters', () => {
|
|
517
753
|
const config: VercelBlobStorageConfig = {
|
|
518
754
|
type: 'vercel-blob',
|
|
519
|
-
token:
|
|
755
|
+
token: VALID_TOKEN,
|
|
520
756
|
}
|
|
521
757
|
const provider = new VercelBlobStorageProvider(config)
|
|
522
758
|
|
|
523
759
|
const url = provider.getUrl('my file (1).txt')
|
|
524
760
|
|
|
525
|
-
expect(url).toBe('https://blob.vercel-storage.com/my file (1).txt')
|
|
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
|
+
)
|
|
526
932
|
})
|
|
527
933
|
})
|
|
528
934
|
|
|
@@ -561,5 +967,20 @@ describe('VercelBlobStorageProvider', () => {
|
|
|
561
967
|
|
|
562
968
|
expect(config.type).toBe('vercel-blob')
|
|
563
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
|
+
})
|
|
564
985
|
})
|
|
565
986
|
})
|