1
1
import { parse } from "reference-spec-reader" ;
2
- import { fetch_range , strip_prefix , uri2href } from "./util.js" ;
2
+ import { fetch_range , merge_init , strip_prefix , uri2href } from "./util.js" ;
3
3
import type { AbsolutePath , AsyncReadable } from "./types.js" ;
4
4
5
5
/**
@@ -36,19 +36,22 @@ type ReferenceEntry = string | [url: string | null] | [
36
36
37
37
interface ReferenceStoreOptions {
38
38
target ?: string | URL ;
39
+ overrides ?: RequestInit ;
39
40
}
40
41
41
42
/** @experimental */
42
43
class ReferenceStore implements AsyncReadable < RequestInit > {
43
44
#refs: Map < string , ReferenceEntry > ;
44
45
#opts: ReferenceStoreOptions ;
46
+ #overrides: RequestInit ;
45
47
46
48
constructor (
47
49
refs : Map < string , ReferenceEntry > ,
48
50
opts : ReferenceStoreOptions = { } ,
49
51
) {
50
52
this . #refs = refs ;
51
53
this . #opts = opts ;
54
+ this . #overrides = opts . overrides || { } ;
52
55
}
53
56
54
57
async get (
@@ -72,7 +75,12 @@ class ReferenceStore implements AsyncReadable<RequestInit> {
72
75
throw Error ( `No url for key ${ key } , and no target url provided.` ) ;
73
76
}
74
77
75
- let res = await fetch_range ( uri2href ( url ) , offset , size , opts ) ;
78
+ let res = await fetch_range (
79
+ uri2href ( url ) ,
80
+ offset ,
81
+ size ,
82
+ merge_init ( this . #overrides, opts ) ,
83
+ ) ;
76
84
77
85
if ( res . status === 200 || res . status === 206 ) {
78
86
return new Uint8Array ( await res . arrayBuffer ( ) ) ;
@@ -89,7 +97,7 @@ class ReferenceStore implements AsyncReadable<RequestInit> {
89
97
}
90
98
91
99
static async fromUrl ( url : string | URL , opts ?: ReferenceStoreOptions ) {
92
- let spec = await fetch ( url ) . then ( ( res ) => res . json ( ) ) ;
100
+ let spec = await fetch ( url , opts ?. overrides ) . then ( ( res ) => res . json ( ) ) ;
93
101
return ReferenceStore . fromSpec ( spec , opts ) ;
94
102
}
95
103
}
0 commit comments