Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace node-fetch with native fetch in tests and docs #1327

Closed
wants to merge 1 commit into from

Conversation

vzaidman
Copy link
Contributor

@vzaidman vzaidman commented Aug 20, 2024

Summary:
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: nodejs/undici#3492

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.

Differential Revision: D61336391

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 20, 2024
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D61336391

vzaidman added a commit that referenced this pull request Aug 20, 2024
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'}});
```
However, since we only use `node-fetch` in this test it seems like a good chance to remove it completely and use the native `fetch` provided by node (stable since Node.js v21, but works well in older node versions for the purpose of testing).
The native fetch also defaults to `keepAlive: true` (https://github.com/nodejs/undici?tab=readme-ov-file#pipelining) which I manually tweaked to be false.

Differential Revision: D61336391
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D61336391

facebook-github-bot pushed a commit that referenced this pull request Aug 20, 2024
Summary:

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'}});
```

Differential Revision: D61336391
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D61336391

1 similar comment
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D61336391

vzaidman added a commit that referenced this pull request Aug 20, 2024
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'}});
```

Differential Revision: D61336391
@vzaidman vzaidman changed the title removed node-fetch from metro Update node-fetch Aug 20, 2024
facebook-github-bot pushed a commit that referenced this pull request Aug 21, 2024
Summary:

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'}});
```

Differential Revision: D61336391
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D61336391

@robhogan
Copy link
Contributor

Interesting, feels like a Node bug to me...

const {createServer} = require('node:http');

const port = 8080;
const url = 'http://localhost:' + port

async function main() {
    const server1 = createServer((req, res) => res.end());
    await new Promise(r => server1.listen(port, r));
    await fetch(url);
    await new Promise(r => setTimeout(r, 0));
    await new Promise(r => server1.close(r));

    const server2 = createServer((req, res) => res.end());
    await new Promise(r => server2.listen(port, r));
    await fetch(url); // Throws
    await new Promise(r => server2.close(r));
}

main();
node:internal/deps/undici/undici:13193
      Error.captureStackTrace(err);
            ^

TypeError: fetch failed
    at node:internal/deps/undici/undici:13193:13
    at async main (.../fetch-test.js:15:5) {
  [cause]: SocketError: other side closed
      at Socket.<anonymous> (node:internal/deps/undici/undici:6025:28)
      at Socket.emit (node:events:532:35)
      at endReadableNT (node:internal/streams/readable:1696:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    code: 'UND_ERR_SOCKET',
    socket: {
      localAddress: '::1',
      localPort: 61899,
      remoteAddress: undefined,
      remotePort: undefined,
      remoteFamily: undefined,
      timeout: undefined,
      bytesWritten: 338,
      bytesRead: 122
    }
  }
}

Node.js v22.3.0
``

@robhogan
Copy link
Contributor

Opened nodejs/undici#3492

@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D61336391

vzaidman added a commit that referenced this pull request Aug 21, 2024
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'}});
```

Reviewed By: robhogan

Differential Revision: D61336391
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D61336391

vzaidman added a commit that referenced this pull request Aug 21, 2024
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'}});
```

Reviewed By: robhogan

Differential Revision: D61336391
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

Reviewed By: robhogan

Differential Revision: D61336391
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D61336391

@facebook-github-bot
Copy link
Contributor

This pull request has been merged in 60de111.

@robhogan robhogan changed the title Update node-fetch Replace node-fetch with native fetch in tests and docs Aug 22, 2024
@vzaidman vzaidman deleted the export-D61336391 branch October 1, 2024 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fetch may try to use a closed connection
3 participants