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

Ping network connection instead of making a HEAD/GET request #163

Open
kettanaito opened this issue Sep 5, 2024 · 1 comment
Open

Ping network connection instead of making a HEAD/GET request #163

kettanaito opened this issue Sep 5, 2024 · 1 comment

Comments

@kettanaito
Copy link

kettanaito commented Sep 5, 2024

Hi! Thank you for creating this library?

May I suggest to migrate from making a HEAD/GET request to verifying if the connection to the given address can be established?

Current behavior

const result = await axios(httpOptions);

Proposed behavior

import net from 'node:net'

net.connect({ host, port })

Motivation

Performing a HEAD/GET request to the specified address is inefficient for a number of reasons:

  1. It actually fetched the / resource on the given address. If the server needs to perform additional requests to serve /, this library would have to await those requests to finish before it resolves the HEAD/GET request. This is an unnecessary overhead.
  2. The intention of wait-on is to resolve when the given address is available. The operability of the root route is irrelevant here, as long as the given address returns 200 OK in this case. You can verify that via net.connect() as well.

I raise this mostly because wait-on is a dependency in start-server-and-test, which is a widely used utility in E2E testing on the web. Performing a HEAD/GET request to / before the test run may have undesirable side effects, and this proposal aims to prevent that while keeping the familiar developer experience that's been around for years.

Benefits

  • Using net.connect() means support for protocols other than HTTP, which results in a more versatile library.
  • I believe you can stop caring about custom Agents altogether. Afaik, those are relevant only for HTTP requests, and you won't be making any with net.connect().
  • You can drop axios as a dependency.

Caution

I don't believe net.connect() will follow redirects so additional logic around it may be required to retain backward compatibility in wait-on.

I am happy with opening a pull request to implement this, but would kindly ask for your feedback and guidance first. Thanks.

@kettanaito
Copy link
Author

I also have a tiny proof of concept of using net.connect() to determine if the given address is running in https://github.com/kettanaito/until-connected. It's not a direct replacement with wait-on though, neither it has a goal to be one. Just an implementation reference if you are curious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant