Skip to content

Commit d051e52

Browse files
authored
Fix context for window.fetch; Remove unnecessary done() function in test (tensorflow#1435)
1 parent c6a71db commit d051e52

File tree

2 files changed

+143
-190
lines changed

2 files changed

+143
-190
lines changed

src/io/browser_http.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ export class BrowserHTTPRequest implements IOHandler {
4646
'browserHTTPRequest is not supported outside the web browser ' +
4747
'without a fetch polyfill.');
4848
}
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);
5053
} else {
5154
assert(
5255
typeof fetchFunc === 'function',
@@ -110,7 +113,7 @@ export class BrowserHTTPRequest implements IOHandler {
110113
'model.weights.bin');
111114
}
112115

113-
const response = await this.fetchFunc(this.path as string, init);
116+
const response = await this.getFetchFunc()(this.path as string, init);
114117

115118
if (response.ok) {
116119
return {
@@ -142,7 +145,8 @@ export class BrowserHTTPRequest implements IOHandler {
142145
*/
143146
private async loadBinaryTopology(): Promise<ArrayBuffer> {
144147
try {
145-
const response = await this.fetchFunc(this.path[0], this.requestInit);
148+
const response =
149+
await this.getFetchFunc()(this.path[0], this.requestInit);
146150
if (!response.ok) {
147151
throw new Error(
148152
`BrowserHTTPRequest.load() failed due to HTTP response: ${
@@ -157,7 +161,7 @@ export class BrowserHTTPRequest implements IOHandler {
157161
protected async loadBinaryModel(): Promise<ModelArtifacts> {
158162
const graphPromise = this.loadBinaryTopology();
159163
const manifestPromise =
160-
await this.fetchFunc(this.path[1], this.requestInit);
164+
await this.getFetchFunc()(this.path[1], this.requestInit);
161165
if (!manifestPromise.ok) {
162166
throw new Error(`BrowserHTTPRequest.load() failed due to HTTP response: ${
163167
manifestPromise.statusText}`);
@@ -181,7 +185,7 @@ export class BrowserHTTPRequest implements IOHandler {
181185

182186
protected async loadJSONModel(): Promise<ModelArtifacts> {
183187
const modelConfigRequest =
184-
await this.fetchFunc(this.path as string, this.requestInit);
188+
await this.getFetchFunc()(this.path as string, this.requestInit);
185189
if (!modelConfigRequest.ok) {
186190
throw new Error(`BrowserHTTPRequest.load() failed due to HTTP response: ${
187191
modelConfigRequest.statusText}`);
@@ -230,9 +234,20 @@ export class BrowserHTTPRequest implements IOHandler {
230234
return [
231235
weightSpecs,
232236
concatenateArrayBuffers(await loadWeightsAsArrayBuffer(
233-
fetchURLs, this.requestInit, this.fetchFunc))
237+
fetchURLs, this.requestInit, this.getFetchFunc()))
234238
];
235239
}
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+
}
236251
}
237252

238253
/**

0 commit comments

Comments
 (0)