@@ -3,7 +3,7 @@ import { env, version as nodeVersion } from 'node:process'
33
44import semver from 'semver'
55import tmp from 'tmp-promise'
6- import { describe , test , expect , beforeAll , afterEach } from 'vitest'
6+ import { test , expect , beforeAll , afterEach } from 'vitest'
77
88import { getStore } from './main.js'
99import { BlobsServer } from './server.js'
@@ -28,87 +28,190 @@ afterEach(() => {
2828} )
2929
3030const siteID = '9a003659-aaaa-0000-aaaa-63d3720d8621'
31- const key = '54321'
3231const token = 'my-very-secret-token'
3332
34- describe ( 'Local server' , ( ) => {
35- test ( 'Reads and writes from the file system' , async ( ) => {
36- const directory = await tmp . dir ( )
37- const server = new BlobsServer ( {
38- directory : directory . path ,
39- token,
40- } )
41- const { port } = await server . start ( )
42- const blobs = getStore ( {
43- edgeURL : `http://localhost:${ port } ` ,
44- name : 'mystore' ,
45- token,
46- siteID,
47- } )
48-
49- await blobs . set ( key , 'value 1' )
50- expect ( await blobs . get ( key ) ) . toBe ( 'value 1' )
51-
52- await blobs . set ( key , 'value 2' )
53- expect ( await blobs . get ( key ) ) . toBe ( 'value 2' )
54-
55- await blobs . delete ( key )
56- expect ( await blobs . get ( key ) ) . toBe ( null )
57-
58- await server . stop ( )
59- await fs . rm ( directory . path , { force : true , recursive : true } )
33+ test ( 'Reads and writes from the file system' , async ( ) => {
34+ const directory = await tmp . dir ( )
35+ const server = new BlobsServer ( {
36+ directory : directory . path ,
37+ token,
6038 } )
39+ const { port } = await server . start ( )
40+ const blobs = getStore ( {
41+ edgeURL : `http://localhost:${ port } ` ,
42+ name : 'mystore' ,
43+ token,
44+ siteID,
45+ } )
46+ const metadata = {
47+ features : {
48+ blobs : true ,
49+ functions : true ,
50+ } ,
51+ name : 'Netlify' ,
52+ }
53+
54+ await blobs . set ( 'simple-key' , 'value 1' )
55+ expect ( await blobs . get ( 'simple-key' ) ) . toBe ( 'value 1' )
56+
57+ await blobs . set ( 'simple-key' , 'value 2' , { metadata } )
58+ expect ( await blobs . get ( 'simple-key' ) ) . toBe ( 'value 2' )
59+
60+ await blobs . set ( 'parent/child' , 'value 3' )
61+ expect ( await blobs . get ( 'parent/child' ) ) . toBe ( 'value 3' )
62+ expect ( await blobs . get ( 'parent' ) ) . toBe ( null )
63+
64+ const entry = await blobs . getWithMetadata ( 'simple-key' )
65+ expect ( entry . metadata ) . toEqual ( metadata )
66+
67+ await blobs . delete ( 'simple-key' )
68+ expect ( await blobs . get ( 'simple-key' ) ) . toBe ( null )
69+
70+ await server . stop ( )
71+ await fs . rm ( directory . path , { force : true , recursive : true } )
72+ } )
73+
74+ test ( 'Separates keys from different stores' , async ( ) => {
75+ const directory = await tmp . dir ( )
76+ const server = new BlobsServer ( {
77+ directory : directory . path ,
78+ token,
79+ } )
80+ const { port } = await server . start ( )
81+
82+ const store1 = getStore ( {
83+ edgeURL : `http://localhost:${ port } ` ,
84+ name : 'mystore1' ,
85+ token,
86+ siteID,
87+ } )
88+ const store2 = getStore ( {
89+ edgeURL : `http://localhost:${ port } ` ,
90+ name : 'mystore2' ,
91+ token,
92+ siteID,
93+ } )
94+ const key = 'my-key'
95+
96+ await store1 . set ( key , 'value 1 for store 1' )
97+ await store2 . set ( key , 'value 1 for store 2' )
6198
62- test ( 'Separates keys from different stores' , async ( ) => {
63- const directory = await tmp . dir ( )
64- const server = new BlobsServer ( {
65- directory : directory . path ,
66- token,
67- } )
68- const { port } = await server . start ( )
69-
70- const store1 = getStore ( {
71- edgeURL : `http://localhost:${ port } ` ,
72- name : 'mystore1' ,
73- token,
74- siteID,
75- } )
76- const store2 = getStore ( {
77- edgeURL : `http://localhost:${ port } ` ,
78- name : 'mystore2' ,
79- token,
80- siteID,
81- } )
82-
83- await store1 . set ( key , 'value 1 for store 1' )
84- await store2 . set ( key , 'value 1 for store 2' )
85-
86- expect ( await store1 . get ( key ) ) . toBe ( 'value 1 for store 1' )
87- expect ( await store2 . get ( key ) ) . toBe ( 'value 1 for store 2' )
88-
89- await server . stop ( )
90- await fs . rm ( directory . path , { force : true , recursive : true } )
99+ expect ( await store1 . get ( key ) ) . toBe ( 'value 1 for store 1' )
100+ expect ( await store2 . get ( key ) ) . toBe ( 'value 1 for store 2' )
101+
102+ await server . stop ( )
103+ await fs . rm ( directory . path , { force : true , recursive : true } )
104+ } )
105+
106+ test ( 'If a token is set, rejects any requests with an invalid `authorization` header' , async ( ) => {
107+ const directory = await tmp . dir ( )
108+ const server = new BlobsServer ( {
109+ directory : directory . path ,
110+ token,
111+ } )
112+ const { port } = await server . start ( )
113+ const blobs = getStore ( {
114+ edgeURL : `http://localhost:${ port } ` ,
115+ name : 'mystore' ,
116+ token : 'another token' ,
117+ siteID,
91118 } )
92119
93- test ( 'If a token is set, rejects any requests with an invalid `authorization` header' , async ( ) => {
94- const directory = await tmp . dir ( )
95- const server = new BlobsServer ( {
96- directory : directory . path ,
97- token ,
98- } )
99- const { port } = await server . start ( )
100- const blobs = getStore ( {
101- edgeURL : `http://localhost: ${ port } ` ,
102- name : 'mystore' ,
103- token : 'another token' ,
104- siteID ,
105- } )
106-
107- await expect ( async ( ) => await blobs . get ( key ) ) . rejects . toThrowError (
108- 'Netlify Blobs has generated an internal error: 403 response' ,
109- )
110-
111- await server . stop ( )
112- await fs . rm ( directory . path , { force : true , recursive : true } )
120+ await expect ( async ( ) => await blobs . get ( 'some-key' ) ) . rejects . toThrowError (
121+ 'Netlify Blobs has generated an internal error: 403 response' ,
122+ )
123+
124+ await server . stop ( )
125+ await fs . rm ( directory . path , { force : true , recursive : true } )
126+ } )
127+
128+ test ( 'Lists entries' , async ( ) => {
129+ const directory = await tmp . dir ( )
130+ const server = new BlobsServer ( {
131+ directory : directory . path ,
132+ token ,
133+ } )
134+ const { port } = await server . start ( )
135+ const blobs = getStore ( {
136+ edgeURL : `http://localhost: ${ port } ` ,
137+ name : 'mystore' ,
138+ token ,
139+ siteID ,
113140 } )
141+ const songs : Record < string , string > = {
142+ 'coldplay/parachutes/shiver' : "I'll always be waiting for you" ,
143+ 'coldplay/parachutes/spies' : 'And the spies came out of the water' ,
144+ 'coldplay/parachutes/trouble' : 'And I:I never meant to cause you trouble' ,
145+ 'coldplay/a-rush-of-blood-to-the-head/politik' : 'Give me heart and give me soul' ,
146+ 'coldplay/a-rush-of-blood-to-the-head/in-my-place' : 'How long must you wait for it?' ,
147+ 'coldplay/a-rush-of-blood-to-the-head/the-scientist' : 'Questions of science, science and progress' ,
148+ 'phoenix/united/too-young' : "Oh rainfalls and hard times coming they won't leave me tonight" ,
149+ 'phoenix/united/party-time' : 'Summertime is gone' ,
150+ 'phoenix/ti-amo/j-boy' : 'Something in the middle of the side of the store' ,
151+ 'phoenix/ti-amo/fleur-de-lys' : 'No rest till I get to you, no rest till I get to you' ,
152+ }
153+
154+ for ( const title in songs ) {
155+ await blobs . set ( title , songs [ title ] )
156+ }
157+
158+ const allSongs = await blobs . list ( )
159+
160+ for ( const title in songs ) {
161+ const match = allSongs . blobs . find ( ( blob ) => blob . key === title )
162+
163+ expect ( match ) . toBeTruthy ( )
164+ }
165+
166+ const coldplaySongs = await blobs . list ( { prefix : 'coldplay/' } )
167+
168+ for ( const title in songs ) {
169+ if ( ! title . startsWith ( 'coldplay/' ) ) {
170+ continue
171+ }
172+
173+ const match = coldplaySongs . blobs . find ( ( blob ) => blob . key === title )
174+
175+ expect ( match ) . toBeTruthy ( )
176+ }
177+
178+ const parachutesSongs = await blobs . list ( { prefix : 'coldplay/parachutes/' } )
179+
180+ for ( const title in songs ) {
181+ if ( ! title . startsWith ( 'coldplay/parachutes/' ) ) {
182+ continue
183+ }
184+
185+ const match = parachutesSongs . blobs . find ( ( blob ) => blob . key === title )
186+
187+ expect ( match ) . toBeTruthy ( )
188+ }
189+
190+ const fooFightersSongs = await blobs . list ( { prefix : 'foo-fighters/' } )
191+
192+ expect ( fooFightersSongs . blobs ) . toEqual ( [ ] )
193+
194+ const artists = await blobs . list ( { directories : true } )
195+
196+ expect ( artists . blobs ) . toEqual ( [ ] )
197+ expect ( artists . directories ) . toEqual ( [ 'coldplay' , 'phoenix' ] )
198+
199+ const coldplayAlbums = await blobs . list ( { directories : true , prefix : 'coldplay/' } )
200+
201+ expect ( coldplayAlbums . blobs ) . toEqual ( [ ] )
202+ expect ( coldplayAlbums . directories ) . toEqual ( [ 'coldplay/a-rush-of-blood-to-the-head' , 'coldplay/parachutes' ] )
203+
204+ const parachutesSongs2 = await blobs . list ( { directories : true , prefix : 'coldplay/parachutes/' } )
205+
206+ for ( const title in songs ) {
207+ if ( ! title . startsWith ( 'coldplay/parachutes/' ) ) {
208+ continue
209+ }
210+
211+ const match = parachutesSongs2 . blobs . find ( ( blob ) => blob . key === title )
212+
213+ expect ( match ) . toBeTruthy ( )
214+ }
215+
216+ expect ( parachutesSongs2 . directories ) . toEqual ( [ ] )
114217} )
0 commit comments