Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion tfjs-core/src/platforms/platform_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ export const getNodeFetch = {
importFetch: () => require('node-fetch')
};

export let systemFetch: (url: string, init?: RequestInit) => Promise<Response>;
type FetchFn = (url: string, init?: RequestInit) => Promise<Response>;
let systemFetch: FetchFn;
// These getters and setters are for testing so we don't export a mutable
// variable.
export function resetSystemFetch() {
systemFetch = null;
}
export function setSystemFetch(fetchFn: FetchFn) {
systemFetch = fetchFn;
}
export function getSystemFetch(): FetchFn {
return systemFetch;
}

export class PlatformNode implements Platform {
private textEncoder: TextEncoder;
Expand Down
8 changes: 3 additions & 5 deletions tfjs-core/src/platforms/platform_node_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ describeWithFlags('PlatformNode', NODE_ENVS, () => {

const platform = new PlatformNode();

const savedFetch = platform_node.systemFetch;
const savedFetch = platform_node.getSystemFetch();

// Null out the system fetch so we force it to require node-fetch.
// @ts-ignore
platform_node.systemFetch = null;
platform_node.resetSystemFetch();

const testFetch = {fetch: (url: string, init: RequestInit) => {}};

Expand All @@ -63,8 +62,7 @@ describeWithFlags('PlatformNode', NODE_ENVS, () => {
expect(platform_node.getNodeFetch.importFetch).toHaveBeenCalled();
expect(testFetch.fetch).toHaveBeenCalledWith('test/url', {method: 'GET'});

// @ts-ignore
platform_node.systemFetch = savedFetch;
platform_node.setSystemFetch(savedFetch);
ENV.global.fetch = globalFetch;
});

Expand Down