Skip to content

Commit

Permalink
fix native fetch error in server-test on node 18 (#1341)
Browse files Browse the repository at this point in the history
Summary:
Fixing node 18 fetch error due to attempt to set the "Connection" header which is not supported on that version of node (nodejs/undici#1305).
However "Connection" is set to "close" by default in node 18 anyway comparing with later version so we just don't touch it on that version.

Pull Request resolved: #1341

Test Plan:
`yarn jest packages/metro/src/integration_tests/__tests__/server-test.js`

Tested ad hoc on Node JS versions:
* `18.0.0`
* `18.14.1`
* `18.14.2`
Plus the versions in the github actions

Reviewed By: robhogan

Differential Revision: D62026647

Pulled By: vzaidman

fbshipit-source-id: fe99cfd0a7cdedc0119b1c68c38125fe464b4742
  • Loading branch information
vzaidman authored and facebook-github-bot committed Aug 30, 2024
1 parent 5d63c99 commit e42bfc8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/metro/src/integration_tests/__tests__/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@ jest.unmock('cosmiconfig');

jest.setTimeout(60 * 1000);

// Can't set the "Connection" header in node < 18.14.1 (undici < 5.15.0): https://github.com/nodejs/undici/pull/1829,
// However in these versions "Connection" is set to "close" by default in node 18 anyway comparing with later version
const [nodeVersionMajor, nodeVersionMinor, nodeVersionPatch] =
process.versions.node.split('.').map(Number);
const canSetConnectionHeader =
nodeVersionMajor > 18 ||
(nodeVersionMajor === 18 && nodeVersionMinor > 14) ||
(nodeVersionMajor === 18 && nodeVersionMinor === 14 && nodeVersionPatch >= 1);

// 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'},
headers: canSetConnectionHeader ? {Connection: 'close'} : {},
});

describe('Metro development server serves bundles via HTTP', () => {
Expand Down

0 comments on commit e42bfc8

Please sign in to comment.