@@ -103,37 +103,45 @@ test('reads file with encoding', async () => {
103103} )
104104
105105test ( 'reads remote partially' , async ( ) => {
106- mockFetch = vi . fn ( ) . mockImplementation ( async ( url : string , args : { headers : Record < string , string > } ) => {
107- const file = getFile ( url )
108- const range = rangeParser ( 10000 , args . headers . range )
109- const { start, end } = range [ 0 ]
110- const len = end - start + 1
111- const buf = await file . read ( len , start )
112- const stat = await file . stat ( )
113-
114- return createResponse ( buf , 206 , {
115- 'content-range' : `${ start } -${ end } /${ stat . size } ` ,
116- } )
117- } )
106+ mockFetch = vi
107+ . fn ( )
108+ . mockImplementation (
109+ async ( url : string , args : { headers : Record < string , string > } ) => {
110+ const file = getFile ( url )
111+ const range = rangeParser ( 10000 , args . headers . range )
112+ const { start, end } = range [ 0 ]
113+ const len = end - start + 1
114+ const buf = await file . read ( len , start )
115+ const stat = await file . stat ( )
116+
117+ return createResponse ( buf , 206 , {
118+ 'content-range' : `${ start } -${ end } /${ stat . size } ` ,
119+ } )
120+ } ,
121+ )
118122
119123 const f = new RemoteFile ( 'http://fakehost/test.txt' , { fetch : mockFetch } )
120124 const buf = await f . read ( 3 , 0 )
121125 expect ( toString ( buf ) ) . toEqual ( 'tes' )
122126} )
123127
124128test ( 'reads remote clipped at the end' , async ( ) => {
125- mockFetch = vi . fn ( ) . mockImplementation ( async ( url : string , args : { headers : Record < string , string > } ) => {
126- const file = getFile ( url )
127- const range = rangeParser ( 10000 , args . headers . range )
128- const { start, end } = range [ 0 ]
129- const len = end - start + 1
130- const buf = await file . read ( len , start )
131- const stat = await file . stat ( )
132-
133- return createResponse ( buf , 206 , {
134- 'content-range' : `${ start } -${ end } /${ stat . size } ` ,
135- } )
136- } )
129+ mockFetch = vi
130+ . fn ( )
131+ . mockImplementation (
132+ async ( url : string , args : { headers : Record < string , string > } ) => {
133+ const file = getFile ( url )
134+ const range = rangeParser ( 10000 , args . headers . range )
135+ const { start, end } = range [ 0 ]
136+ const len = end - start + 1
137+ const buf = await file . read ( len , start )
138+ const stat = await file . stat ( )
139+
140+ return createResponse ( buf , 206 , {
141+ 'content-range' : `${ start } -${ end } /${ stat . size } ` ,
142+ } )
143+ } ,
144+ )
137145
138146 const f = new RemoteFile ( 'http://fakehost/test.txt' , { fetch : mockFetch } )
139147 const buf = await f . read ( 3 , 6 )
@@ -160,38 +168,52 @@ test('throws error if file missing', async () => {
160168 await expect ( res ) . rejects . toThrow ( / H T T P 4 0 4 / )
161169} )
162170
171+ test ( 'throws on NaN length or position' , async ( ) => {
172+ const f = new RemoteFile ( 'http://fakehost/test.txt' , { fetch : mockFetch } )
173+ await expect ( f . read ( NaN , 0 ) ) . rejects . toThrow ( / N a N l e n g t h o r p o s i t i o n / )
174+ await expect ( f . read ( 10 , NaN ) ) . rejects . toThrow ( / N a N l e n g t h o r p o s i t i o n / )
175+ } )
176+
163177test ( 'zero read' , async ( ) => {
164- mockFetch = vi . fn ( ) . mockImplementation ( async ( url : string , args : { headers : Record < string , string > } ) => {
165- const file = getFile ( url )
166- const range = rangeParser ( 10000 , args . headers . range )
167- const { start, end } = range [ 0 ]
168- const len = end - start + 1
169- const buf = await file . read ( len , start )
170- const stat = await file . stat ( )
171-
172- return createResponse ( buf , 206 , {
173- 'content-range' : `${ start } -${ end } /${ stat . size } ` ,
174- } )
175- } )
178+ mockFetch = vi
179+ . fn ( )
180+ . mockImplementation (
181+ async ( url : string , args : { headers : Record < string , string > } ) => {
182+ const file = getFile ( url )
183+ const range = rangeParser ( 10000 , args . headers . range )
184+ const { start, end } = range [ 0 ]
185+ const len = end - start + 1
186+ const buf = await file . read ( len , start )
187+ const stat = await file . stat ( )
188+
189+ return createResponse ( buf , 206 , {
190+ 'content-range' : `${ start } -${ end } /${ stat . size } ` ,
191+ } )
192+ } ,
193+ )
176194
177195 const f = new RemoteFile ( 'http://fakehost/test.txt' , { fetch : mockFetch } )
178196 const buf = toString ( await f . read ( 0 , 0 ) )
179197 expect ( buf ) . toBe ( '' )
180198} )
181199
182200test ( 'stat' , async ( ) => {
183- mockFetch = vi . fn ( ) . mockImplementation ( async ( url : string , args : { headers : Record < string , string > } ) => {
184- const file = getFile ( url )
185- const range = rangeParser ( 10000 , args . headers . range )
186- const { start, end } = range [ 0 ]
187- const len = end - start + 1
188- const buf = await file . read ( len , start )
189- const stat = await file . stat ( )
190-
191- return createResponse ( buf , 206 , {
192- 'content-range' : `${ start } -${ end } /${ stat . size } ` ,
193- } )
194- } )
201+ mockFetch = vi
202+ . fn ( )
203+ . mockImplementation (
204+ async ( url : string , args : { headers : Record < string , string > } ) => {
205+ const file = getFile ( url )
206+ const range = rangeParser ( 10000 , args . headers . range )
207+ const { start, end } = range [ 0 ]
208+ const len = end - start + 1
209+ const buf = await file . read ( len , start )
210+ const stat = await file . stat ( )
211+
212+ return createResponse ( buf , 206 , {
213+ 'content-range' : `${ start } -${ end } /${ stat . size } ` ,
214+ } )
215+ } ,
216+ )
195217
196218 const f = new RemoteFile ( 'http://fakehost/test.txt' , { fetch : mockFetch } )
197219 const stat = await f . stat ( )
0 commit comments