@@ -46,7 +46,10 @@ export class BrowserHTTPRequest implements IOHandler {
46
46
'browserHTTPRequest is not supported outside the web browser ' +
47
47
'without a fetch polyfill.' ) ;
48
48
}
49
- this . fetchFunc = fetch ;
49
+ // Make sure fetch is always bound to window (the
50
+ // original object) when available.
51
+ this . fetchFunc =
52
+ fetch . bind ( typeof window === 'undefined' ? null : window ) ;
50
53
} else {
51
54
assert (
52
55
typeof fetchFunc === 'function' ,
@@ -110,7 +113,7 @@ export class BrowserHTTPRequest implements IOHandler {
110
113
'model.weights.bin' ) ;
111
114
}
112
115
113
- const response = await this . fetchFunc ( this . path as string , init ) ;
116
+ const response = await this . getFetchFunc ( ) ( this . path as string , init ) ;
114
117
115
118
if ( response . ok ) {
116
119
return {
@@ -142,7 +145,8 @@ export class BrowserHTTPRequest implements IOHandler {
142
145
*/
143
146
private async loadBinaryTopology ( ) : Promise < ArrayBuffer > {
144
147
try {
145
- const response = await this . fetchFunc ( this . path [ 0 ] , this . requestInit ) ;
148
+ const response =
149
+ await this . getFetchFunc ( ) ( this . path [ 0 ] , this . requestInit ) ;
146
150
if ( ! response . ok ) {
147
151
throw new Error (
148
152
`BrowserHTTPRequest.load() failed due to HTTP response: ${
@@ -157,7 +161,7 @@ export class BrowserHTTPRequest implements IOHandler {
157
161
protected async loadBinaryModel ( ) : Promise < ModelArtifacts > {
158
162
const graphPromise = this . loadBinaryTopology ( ) ;
159
163
const manifestPromise =
160
- await this . fetchFunc ( this . path [ 1 ] , this . requestInit ) ;
164
+ await this . getFetchFunc ( ) ( this . path [ 1 ] , this . requestInit ) ;
161
165
if ( ! manifestPromise . ok ) {
162
166
throw new Error ( `BrowserHTTPRequest.load() failed due to HTTP response: ${
163
167
manifestPromise . statusText } `) ;
@@ -181,7 +185,7 @@ export class BrowserHTTPRequest implements IOHandler {
181
185
182
186
protected async loadJSONModel ( ) : Promise < ModelArtifacts > {
183
187
const modelConfigRequest =
184
- await this . fetchFunc ( this . path as string , this . requestInit ) ;
188
+ await this . getFetchFunc ( ) ( this . path as string , this . requestInit ) ;
185
189
if ( ! modelConfigRequest . ok ) {
186
190
throw new Error ( `BrowserHTTPRequest.load() failed due to HTTP response: ${
187
191
modelConfigRequest . statusText } `) ;
@@ -230,9 +234,20 @@ export class BrowserHTTPRequest implements IOHandler {
230
234
return [
231
235
weightSpecs ,
232
236
concatenateArrayBuffers ( await loadWeightsAsArrayBuffer (
233
- fetchURLs , this . requestInit , this . fetchFunc ) )
237
+ fetchURLs , this . requestInit , this . getFetchFunc ( ) ) )
234
238
] ;
235
239
}
240
+
241
+ /**
242
+ * Helper method to get the `fetch`-like function set for this instance.
243
+ *
244
+ * This is mainly for avoiding confusion with regard to what context
245
+ * the `fetch`-like function is bound to. In the default (browser) case,
246
+ * the function will be bound to `window`, instead of `this`.
247
+ */
248
+ private getFetchFunc ( ) {
249
+ return this . fetchFunc ;
250
+ }
236
251
}
237
252
238
253
/**
0 commit comments