Skip to content

Commit

Permalink
Rely on cross-fetch
Browse files Browse the repository at this point in the history
And rely on cross-fetch directly, as that version provides the desired
interface.

Due to "self is not defined" JakeChampion/fetch#657
  • Loading branch information
Empact committed Mar 18, 2019
1 parent b69c1d0 commit 65a5255
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 36 deletions.
16 changes: 8 additions & 8 deletions Libraries/Core/Devtools/symbolicateStackTrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ function isSourcedFromDisk(sourcePath: string): boolean {
async function symbolicateStackTrace(
stack: Array<StackFrame>,
): Promise<Array<StackFrame>> {
// RN currently lazy loads whatwg-fetch using a custom fetch module, which,
// when called for the first time, requires and re-exports 'whatwg-fetch'.
// However, when a dependency of the project tries to require whatwg-fetch
// either directly or indirectly, whatwg-fetch is required before
// RN can lazy load whatwg-fetch. As whatwg-fetch checks
// RN currently lazy loads cross-fetch using a custom fetch module, which,
// when called for the first time, requires and re-exports 'cross-fetch'.
// However, when a dependency of the project tries to require cross-fetch
// either directly or indirectly, cross-fetch is required before
// RN can lazy load cross-fetch. As cross-fetch checks
// for a fetch polyfill before loading, it will in turn try to load
// RN's fetch module, which immediately tries to import whatwg-fetch AGAIN.
// RN's fetch module, which immediately tries to import cross-fetch AGAIN.
// This causes a circular require which results in RN's fetch module
// exporting fetch as 'undefined'.
// The fix below postpones trying to load fetch until the first call to symbolicateStackTrace.
// At that time, we will have either global.fetch (whatwg-fetch) or RN's fetch.
// At that time, we will have either global.fetch (cross-fetch) or RN's fetch.
if (!fetch) {
fetch = global.fetch || require('fetch').fetch;
fetch = global.fetch || require('cross-fetch').fetch;
}

const devServer = getDevServer();
Expand Down
8 changes: 4 additions & 4 deletions Libraries/Core/setUpXHR.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const {polyfillGlobal} = require('PolyfillFunctions');
polyfillGlobal('XMLHttpRequest', () => require('XMLHttpRequest'));
polyfillGlobal('FormData', () => require('FormData'));

polyfillGlobal('fetch', () => require('fetch').fetch); // flowlint-line untyped-import:off
polyfillGlobal('Headers', () => require('fetch').Headers); // flowlint-line untyped-import:off
polyfillGlobal('Request', () => require('fetch').Request); // flowlint-line untyped-import:off
polyfillGlobal('Response', () => require('fetch').Response); // flowlint-line untyped-import:off
polyfillGlobal('fetch', () => require('cross-fetch').fetch); // flowlint-line untyped-import:off
polyfillGlobal('Headers', () => require('cross-fetch').Headers); // flowlint-line untyped-import:off
polyfillGlobal('Request', () => require('cross-fetch').Request); // flowlint-line untyped-import:off
polyfillGlobal('Response', () => require('cross-fetch').Response); // flowlint-line untyped-import:off
polyfillGlobal('WebSocket', () => require('WebSocket'));
polyfillGlobal('Blob', () => require('Blob'));
polyfillGlobal('File', () => require('File'));
Expand Down
20 changes: 0 additions & 20 deletions Libraries/Network/fetch.js

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"compression": "^1.7.1",
"connect": "^3.6.5",
"create-react-class": "^15.6.3",
"cross-fetch": "^3.0.1",
"debug": "^2.2.0",
"denodeify": "^1.2.1",
"errorhandler": "^1.5.0",
Expand All @@ -187,7 +188,6 @@
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"morgan": "^1.9.0",
"node-fetch": "^2.2.0",
"node-notifier": "^5.2.1",
"npmlog": "^2.0.4",
"nullthrows": "^1.1.0",
Expand All @@ -205,7 +205,6 @@
"serve-static": "^1.13.1",
"shell-quote": "1.6.1",
"stacktrace-parser": "^0.1.3",
"whatwg-fetch": "^1.0.0",
"ws": "^1.1.5",
"xmldoc": "^0.4.0",
"yargs": "^9.0.0"
Expand Down Expand Up @@ -257,4 +256,4 @@
}
}
}
}
}
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,14 @@ create-react-class@^15.6.3:
loose-envify "^1.3.1"
object-assign "^4.1.1"

cross-fetch@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.1.tgz#3f207bbd829a76e9aa2a953348bd1f2a3cf388a7"
integrity sha512-qWtpgBAF8ioqBOddRD+pHhrdzm/UWOArkrlIU7c08DlNbOxo5GfUbSY2vr90ZypWf0raW+HNN1F38pi5CEOjiQ==
dependencies:
node-fetch "2.3.0"
whatwg-fetch "3.0.0"

cross-spawn@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-4.0.2.tgz#7b9247621c23adfdd3856004a823cbe397424d41"
Expand Down Expand Up @@ -5823,6 +5831,11 @@ node-dir@^0.1.17:
dependencies:
minimatch "^3.0.2"

node-fetch@2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==

node-fetch@^1.0.1:
version "1.7.3"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
Expand Down Expand Up @@ -7857,7 +7870,7 @@ whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3:
dependencies:
iconv-lite "0.4.24"

whatwg-fetch@>=0.10.0:
whatwg-fetch@3.0.0, whatwg-fetch@>=0.10.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz#fc804e458cc460009b1a2b966bc8817d2578aefb"
integrity sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==
Expand Down

0 comments on commit 65a5255

Please sign in to comment.