@@ -4,7 +4,7 @@ import { env, version as nodeVersion } from 'node:process'
4
4
5
5
import semver from 'semver'
6
6
import tmp from 'tmp-promise'
7
- import { describe , test , expect , beforeAll } from 'vitest'
7
+ import { describe , test , expect , beforeAll , afterEach } from 'vitest'
8
8
9
9
import { MockFetch } from '../test/mock_fetch.js'
10
10
import { streamToString } from '../test/util.js'
@@ -26,8 +26,12 @@ beforeAll(async () => {
26
26
}
27
27
} )
28
28
29
- const deployID = 'abcdef'
30
- const siteID = '12345'
29
+ afterEach ( ( ) => {
30
+ delete env . NETLIFY_BLOBS_CONTEXT
31
+ } )
32
+
33
+ const deployID = '6527dfab35be400008332a1d'
34
+ const siteID = '9a003659-aaaa-0000-aaaa-63d3720d8621'
31
35
const key = '54321'
32
36
const complexKey = '/artista/canção'
33
37
const value = 'some value'
@@ -191,7 +195,7 @@ describe('get', () => {
191
195
} )
192
196
} )
193
197
194
- describe ( 'With context credentials' , ( ) => {
198
+ describe ( 'With edge credentials' , ( ) => {
195
199
test ( 'Reads from the blob store' , async ( ) => {
196
200
const mockStore = new MockFetch ( )
197
201
. get ( {
@@ -265,6 +269,53 @@ describe('get', () => {
265
269
266
270
expect ( mockStore . fulfilled ) . toBeTruthy ( )
267
271
} )
272
+
273
+ test ( 'Loads credentials from the environment' , async ( ) => {
274
+ const tokens = [ 'some-token-1' , 'another-token-2' ]
275
+ const mockStore = new MockFetch ( )
276
+ . get ( {
277
+ headers : { authorization : `Bearer ${ tokens [ 0 ] } ` } ,
278
+ response : new Response ( value ) ,
279
+ url : `${ edgeURL } /${ siteID } /images/${ key } ` ,
280
+ } )
281
+ . get ( {
282
+ headers : { authorization : `Bearer ${ tokens [ 0 ] } ` } ,
283
+ response : new Response ( value ) ,
284
+ url : `${ edgeURL } /${ siteID } /images/${ key } ` ,
285
+ } )
286
+ . get ( {
287
+ headers : { authorization : `Bearer ${ tokens [ 1 ] } ` } ,
288
+ response : new Response ( value ) ,
289
+ url : `${ edgeURL } /${ siteID } /images/${ key } ` ,
290
+ } )
291
+ . get ( {
292
+ headers : { authorization : `Bearer ${ tokens [ 1 ] } ` } ,
293
+ response : new Response ( value ) ,
294
+ url : `${ edgeURL } /${ siteID } /images/${ key } ` ,
295
+ } )
296
+
297
+ globalThis . fetch = mockStore . fetcher
298
+
299
+ for ( let index = 0 ; index <= 1 ; index ++ ) {
300
+ const context = {
301
+ edgeURL,
302
+ siteID,
303
+ token : tokens [ index ] ,
304
+ }
305
+
306
+ env . NETLIFY_BLOBS_CONTEXT = Buffer . from ( JSON . stringify ( context ) ) . toString ( 'base64' )
307
+
308
+ const store = getStore ( 'images' )
309
+
310
+ const string = await store . get ( key )
311
+ expect ( string ) . toBe ( value )
312
+
313
+ const stream = await store . get ( key , { type : 'stream' } )
314
+ expect ( await streamToString ( stream as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
315
+ }
316
+
317
+ expect ( mockStore . fulfilled ) . toBeTruthy ( )
318
+ } )
268
319
} )
269
320
270
321
test ( 'Throws when the instance is missing required configuration properties' , async ( ) => {
@@ -560,7 +611,7 @@ describe('set', () => {
560
611
} )
561
612
} )
562
613
563
- describe ( 'With context credentials' , ( ) => {
614
+ describe ( 'With edge credentials' , ( ) => {
564
615
test ( 'Writes to the blob store' , async ( ) => {
565
616
const mockStore = new MockFetch ( )
566
617
. put ( {
@@ -712,7 +763,7 @@ describe('setJSON', () => {
712
763
} )
713
764
} )
714
765
715
- describe ( 'With context credentials' , ( ) => {
766
+ describe ( 'With edge credentials' , ( ) => {
716
767
test ( 'Writes to the blob store' , async ( ) => {
717
768
const mockStore = new MockFetch ( ) . put ( {
718
769
body : JSON . stringify ( { value } ) ,
@@ -829,7 +880,7 @@ describe('delete', () => {
829
880
} )
830
881
} )
831
882
832
- describe ( 'With context credentials' , ( ) => {
883
+ describe ( 'With edge credentials' , ( ) => {
833
884
test ( 'Deletes from the blob store' , async ( ) => {
834
885
const mockStore = new MockFetch ( ) . delete ( {
835
886
headers : { authorization : `Bearer ${ edgeToken } ` } ,
@@ -898,51 +949,56 @@ describe('delete', () => {
898
949
} )
899
950
} )
900
951
901
- describe ( 'Global client ', ( ) => {
902
- test ( 'Reads from the blob store ' , async ( ) => {
903
- const tokens = [ 'some-token-1' , 'another-token-2' ]
952
+ describe . only ( 'Deploy scope ', ( ) => {
953
+ test ( 'Returns a deploy-scoped store if the `deployID` parameter is supplied ' , async ( ) => {
954
+ const mockToken = 'some-token'
904
955
const mockStore = new MockFetch ( )
905
956
. get ( {
906
- headers : { authorization : `Bearer ${ tokens [ 0 ] } ` } ,
957
+ headers : { authorization : `Bearer ${ mockToken } ` } ,
907
958
response : new Response ( value ) ,
908
959
url : `${ edgeURL } /${ siteID } /images/${ key } ` ,
909
960
} )
910
961
. get ( {
911
- headers : { authorization : `Bearer ${ tokens [ 0 ] } ` } ,
962
+ headers : { authorization : `Bearer ${ mockToken } ` } ,
912
963
response : new Response ( value ) ,
913
964
url : `${ edgeURL } /${ siteID } /images/${ key } ` ,
914
965
} )
915
966
. get ( {
916
- headers : { authorization : `Bearer ${ tokens [ 1 ] } ` } ,
967
+ headers : { authorization : `Bearer ${ mockToken } ` } ,
917
968
response : new Response ( value ) ,
918
- url : `${ edgeURL } /${ siteID } /images /${ key } ` ,
969
+ url : `${ edgeURL } /${ siteID } /${ deployID } /${ key } ` ,
919
970
} )
920
971
. get ( {
921
- headers : { authorization : `Bearer ${ tokens [ 1 ] } ` } ,
972
+ headers : { authorization : `Bearer ${ mockToken } ` } ,
922
973
response : new Response ( value ) ,
923
- url : `${ edgeURL } /${ siteID } /images /${ key } ` ,
974
+ url : `${ edgeURL } /${ siteID } /${ deployID } /${ key } ` ,
924
975
} )
925
976
926
977
globalThis . fetch = mockStore . fetcher
927
978
928
- for ( let index = 0 ; index <= 1 ; index ++ ) {
929
- const context = {
930
- edgeURL,
931
- deployID,
932
- siteID,
933
- token : tokens [ index ] ,
934
- }
979
+ const context = {
980
+ edgeURL,
981
+ siteID,
982
+ token : mockToken ,
983
+ }
935
984
936
- env . NETLIFY_BLOBS_1 = Buffer . from ( JSON . stringify ( context ) ) . toString ( 'base64' )
985
+ env . NETLIFY_BLOBS_CONTEXT = Buffer . from ( JSON . stringify ( context ) ) . toString ( 'base64' )
937
986
938
- const store = getStore ( 'images' )
987
+ const siteStore = getStore ( 'images' )
939
988
940
- const string = await store . get ( key )
941
- expect ( string ) . toBe ( value )
989
+ const string1 = await siteStore . get ( key )
990
+ expect ( string1 ) . toBe ( value )
942
991
943
- const stream = await store . get ( key , { type : 'stream' } )
944
- expect ( await streamToString ( stream as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
945
- }
992
+ const stream1 = await siteStore . get ( key , { type : 'stream' } )
993
+ expect ( await streamToString ( stream1 as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
994
+
995
+ const deployStore = getStore ( { deployID } )
996
+
997
+ const string2 = await deployStore . get ( key )
998
+ expect ( string2 ) . toBe ( value )
999
+
1000
+ const stream2 = await deployStore . get ( key , { type : 'stream' } )
1001
+ expect ( await streamToString ( stream2 as unknown as NodeJS . ReadableStream ) ) . toBe ( value )
946
1002
947
1003
expect ( mockStore . fulfilled ) . toBeTruthy ( )
948
1004
} )
0 commit comments