Skip to content

Commit

Permalink
updated node-fetch (#1327)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #1327

Updating `node-fetch` to the latest version caused tests in `packages/metro/src/integration_tests/__tests__/server-test.js` to fail. Running each test case inside the file failed individually, but there seems to have been a race condition between the tests causing subsequent tests to fail with the following error:
```
FetchError: request to http://localhost:10028/import-export/index.bundle?platform=ios&dev=true&minify=false failed, reason: socket hang up
```
It happens because the "connection: close" header is removed in the latest version of `node-fetch`: node-fetch/node-fetch#1765 and can be fixed by adding this header manually to fetches.
```
fetch('path', {headers: {'Connection': 'close'}});
```
This diff introduces a workaround where we add that header, however `fetch` is expected to work even without it when the following bug is fixed: https://github.com/nodejs/node/issues/54484

Also, since `node-fetch` was only used in this one test, it was actually a good opportunity to remove it from the project's dependencies altogether.

Reviewed By: robhogan

Differential Revision: D61336391

fbshipit-source-id: 898c9745dbbc1e622699ff36b4112a95c3c68656
  • Loading branch information
vzaidman authored and facebook-github-bot committed Aug 21, 2024
1 parent c6e5686 commit 60de111
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 38 deletions.
1 change: 0 additions & 1 deletion packages/metro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"metro-transform-plugins": "0.80.10",
"metro-transform-worker": "0.80.10",
"mime-types": "^2.1.27",
"node-fetch": "^2.2.0",
"nullthrows": "^1.1.1",
"serialize-error": "^2.1.0",
"source-map": "^0.5.6",
Expand Down
26 changes: 16 additions & 10 deletions packages/metro/src/integration_tests/__tests__/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@
const Metro = require('../../..');
const execBundle = require('../execBundle');
const fs = require('fs');
const fetch = require('node-fetch');
const path = require('path');

jest.unmock('cosmiconfig');

jest.setTimeout(60 * 1000);

// Workaround for https://github.com/nodejs/node/issues/54484:
// Fetch with connection: close to prevent Node reusing connections across tests
const fetchAndClose = (path: string) =>
fetch(path, {
headers: {Connection: 'close'},
});

describe('Metro development server serves bundles via HTTP', () => {
let config;
let httpServer;
const bundlesDownloaded = new Set();

async function downloadAndExec(path: string, context = {}): mixed {
const response = await fetch(
const response = await fetchAndClose(
'http://localhost:' + config.server.port + path,
);
bundlesDownloaded.add(path);
Expand Down Expand Up @@ -111,14 +117,14 @@ describe('Metro development server serves bundles via HTTP', () => {
});

test('responds with 404 when the bundle cannot be resolved', async () => {
const response = await fetch(
const response = await fetchAndClose(
'http://localhost:' + config.server.port + '/doesnotexist.bundle',
);
expect(response.status).toBe(404);
});

test('responds with 500 when an import inside the bundle cannot be resolved', async () => {
const response = await fetch(
const response = await fetchAndClose(
'http://localhost:' +
config.server.port +
'/build-errors/inline-requires-cannot-resolve-import.bundle',
Expand All @@ -128,7 +134,7 @@ describe('Metro development server serves bundles via HTTP', () => {

describe('dedicated endpoints for serving source files', () => {
test('under /[metro-project]/', async () => {
const response = await fetch(
const response = await fetchAndClose(
'http://localhost:' +
config.server.port +
'/[metro-project]/TestBundle.js',
Expand All @@ -143,7 +149,7 @@ describe('Metro development server serves bundles via HTTP', () => {
});

test('under /[metro-watchFolders]/', async () => {
const response = await fetch(
const response = await fetchAndClose(
'http://localhost:' +
config.server.port +
'/[metro-watchFolders]/1/metro/src/integration_tests/basic_bundle/TestBundle.js',
Expand All @@ -158,7 +164,7 @@ describe('Metro development server serves bundles via HTTP', () => {
});

test('under /[metro-project]/', async () => {
const response = await fetch(
const response = await fetchAndClose(
'http://localhost:' +
config.server.port +
'/[metro-project]/TestBundle.js',
Expand All @@ -173,7 +179,7 @@ describe('Metro development server serves bundles via HTTP', () => {
});

test('no access to files without source extensions', async () => {
const response = await fetch(
const response = await fetchAndClose(
'http://localhost:' +
config.server.port +
'/[metro-project]/not_a_source_file.xyz',
Expand All @@ -188,7 +194,7 @@ describe('Metro development server serves bundles via HTTP', () => {
});

test('no access to source files excluded from the file map', async () => {
const response = await fetch(
const response = await fetchAndClose(
'http://localhost:' +
config.server.port +
'/[metro-project]/excluded_from_file_map.js',
Expand All @@ -203,7 +209,7 @@ describe('Metro development server serves bundles via HTTP', () => {
});

test('requested with aggressive URL encoding /%5Bmetro-project%5D', async () => {
const response = await fetch(
const response = await fetchAndClose(
'http://localhost:' +
config.server.port +
'/%5Bmetro-project%5D/Foo%2Ejs',
Expand Down
1 change: 0 additions & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"@sideway/formula": "^3.0.1",
"got": "^11.8.5",
"http-cache-semantics": "^4.1.1",
"node-fetch": "^2.0.0",
"terser": "^5.14.2",
"trim": "^0.0.3"
}
Expand Down
2 changes: 1 addition & 1 deletion website/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7727,7 +7727,7 @@ node-emoji@^1.10.0:
dependencies:
lodash.toarray "^4.4.0"

[email protected], node-fetch@^2.0.0:
[email protected]:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
Expand Down
25 changes: 0 additions & 25 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4930,13 +4930,6 @@ node-dir@^0.1.17:
dependencies:
minimatch "^3.0.2"

node-fetch@^2.2.0:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"

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 Expand Up @@ -5959,11 +5952,6 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=

ts-api-utils@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.0.3.tgz#f12c1c781d04427313dbac808f453f050e54a331"
Expand Down Expand Up @@ -6142,19 +6130,6 @@ walker@^1.0.8:
dependencies:
makeerror "1.0.12"

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
integrity sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
integrity sha1-lmRU6HZUYuN2RNNib2dCzotwll0=
dependencies:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"

which-boxed-primitive@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6"
Expand Down

0 comments on commit 60de111

Please sign in to comment.