@@ -3,14 +3,16 @@ import type {
33 Fetcher ,
44 FilehandleOptions ,
55 GenericFilehandle ,
6- RequestOverrides ,
76 Stats ,
87} from './filehandle.ts'
98
109function getMessage ( e : unknown ) {
1110 const r =
12- typeof e === 'object' && e !== null && 'message' in e
13- ? ( e . message as string )
11+ typeof e === 'object' &&
12+ e !== null &&
13+ 'message' in e &&
14+ typeof e . message === 'string'
15+ ? e . message
1416 : `${ e } `
1517 return r . replace ( / \. $ / , '' )
1618}
@@ -19,15 +21,15 @@ export default class RemoteFile implements GenericFilehandle {
1921 protected url : string
2022 private _stat ?: Stats
2123 private fetchImplementation : Fetcher
22- private baseOverrides : RequestOverrides = { }
24+ private baseHeaders : Record < string , string >
2325
2426 public constructor ( source : string , opts : FilehandleOptions = { } ) {
2527 this . url = source
26- const fetch = opts . fetch || globalThis . fetch . bind ( globalThis )
27- if ( opts . overrides ) {
28- this . baseOverrides = opts . overrides
29- }
30- this . fetchImplementation = fetch
28+ this . baseHeaders = opts . headers ?? { }
29+ this . fetchImplementation =
30+ opts . fetch ??
31+ ( ( input : RequestInfo , init ?: RequestInit ) =>
32+ globalThis . fetch ( input , init ) )
3133 }
3234
3335 public async fetch (
@@ -77,20 +79,14 @@ export default class RemoteFile implements GenericFilehandle {
7779 `read() called with NaN length or position (length=${ length } , position=${ position } ). The index file may be corrupt.` ,
7880 )
7981 }
80- const { headers = { } , signal, overrides = { } } = opts
82+ const { headers = { } , signal } = opts
8183 if ( length < Infinity ) {
8284 headers . range = `bytes=${ position } -${ position + length - 1 } `
8385 } else if ( length === Infinity && position !== 0 ) {
8486 headers . range = `bytes=${ position } -`
8587 }
8688 const res = await this . fetch ( this . url , {
87- ...this . baseOverrides ,
88- ...overrides ,
89- headers : {
90- ...this . baseOverrides . headers ,
91- ...overrides . headers ,
92- ...headers ,
93- } ,
89+ headers : { ...this . baseHeaders , ...headers } ,
9490 method : 'GET' ,
9591 redirect : 'follow' ,
9692 mode : 'cors' ,
@@ -104,7 +100,7 @@ export default class RemoteFile implements GenericFilehandle {
104100 if ( ( res . status === 200 && position === 0 ) || res . status === 206 ) {
105101 // try to parse out the size of the remote file
106102 const contentRange = res . headers . get ( 'content-range' )
107- const sizeMatch = / \/ ( \d + ) $ / . exec ( contentRange || '' )
103+ const sizeMatch = / \/ ( \d + ) $ / . exec ( contentRange ?? '' )
108104 if ( sizeMatch ?. [ 1 ] ) {
109105 this . _stat = {
110106 size : parseInt ( sizeMatch [ 1 ] , 10 ) ,
@@ -145,25 +141,11 @@ export default class RemoteFile implements GenericFilehandle {
145141 public async readFile (
146142 options : FilehandleOptions | BufferEncoding = { } ,
147143 ) : Promise < Uint8Array < ArrayBuffer > | string > {
148- let encoding
149- let opts
150- if ( typeof options === 'string' ) {
151- encoding = options
152- opts = { }
153- } else {
154- encoding = options . encoding
155- const { encoding : _ , ...rest } = options
156- opts = rest
157- }
158- const { headers = { } , signal, overrides = { } } = opts
144+ const encoding = typeof options === 'string' ? options : options . encoding
145+ const opts = typeof options === 'string' ? { } : options
146+ const { headers = { } , signal } = opts
159147 const res = await this . fetch ( this . url , {
160- ...this . baseOverrides ,
161- ...overrides ,
162- headers : {
163- ...this . baseOverrides . headers ,
164- ...overrides . headers ,
165- ...headers ,
166- } ,
148+ headers : { ...this . baseHeaders , ...headers } ,
167149 method : 'GET' ,
168150 redirect : 'follow' ,
169151 mode : 'cors' ,
@@ -193,7 +175,7 @@ export default class RemoteFile implements GenericFilehandle {
193175 return this . _stat
194176 }
195177
196- public async close ( ) : Promise < void > {
197- return
178+ public close ( ) : Promise < void > {
179+ return Promise . resolve ( )
198180 }
199181}
0 commit comments