Skip to content

Commit

Permalink
Rely on fetch-ponyfill
Browse files Browse the repository at this point in the history
Due to "self is not defined" JakeChampion/fetch#657
  • Loading branch information
Empact committed Mar 18, 2019
1 parent b69c1d0 commit 1e11f3e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 35 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 fetch-ponyfill using a custom fetch module, which,
// when called for the first time, requires and re-exports 'fetch-ponyfill'.
// However, when a dependency of the project tries to require fetch-ponyfill
// either directly or indirectly, fetch-ponyfill is required before
// RN can lazy load fetch-ponyfill. As fetch-ponyfill 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 fetch-ponyfill 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 (fetch-ponyfill) or RN's fetch.
if (!fetch) {
fetch = global.fetch || require('fetch').fetch;
fetch = global.fetch || require('fetch-ponyfill').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('fetch-ponyfill').fetch); // flowlint-line untyped-import:off
polyfillGlobal('Headers', () => require('fetch-ponyfill').Headers); // flowlint-line untyped-import:off
polyfillGlobal('Request', () => require('fetch-ponyfill').Request); // flowlint-line untyped-import:off
polyfillGlobal('Response', () => require('fetch-ponyfill').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 @@ -175,6 +175,7 @@
"event-target-shim": "^1.0.5",
"fbjs": "^1.0.0",
"fbjs-scripts": "^1.0.0",
"fetch-ponyfill": "^6.0.2",
"fs-extra": "^1.0.0",
"glob": "^7.1.1",
"graceful-fs": "^4.1.3",
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 @@
}
}
}
}
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3369,6 +3369,13 @@ fbjs@^1.0.0:
setimmediate "^1.0.5"
ua-parser-js "^0.7.18"

fetch-ponyfill@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/fetch-ponyfill/-/fetch-ponyfill-6.0.2.tgz#bc2d92afd0140e39518beccd2fd0682ab4491cb6"
integrity sha512-hBWVb6Z0chjr9r0g4N5uknldAnyFstWTnX6oFxMsL9ZlSL7fa66Keis3y4SCMgpTggY4GeGbB4z3z5g5SmrgkQ==
dependencies:
node-fetch "~2.1.0"

figures@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962"
Expand Down Expand Up @@ -5836,6 +5843,11 @@ node-fetch@^2.2.0:
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5"
integrity sha512-OayFWziIxiHY8bCUyLX6sTpDH8Jsbp4FfYd1j1f7vZyfgkcOnAyM4oQR16f8a0s7Gl/viMGRey8eScYk4V4EZA==

node-fetch@~2.1.0:
version "2.1.2"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5"
integrity sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down

0 comments on commit 1e11f3e

Please sign in to comment.