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

Code smells #57

Closed
1 task done
rotu opened this issue Sep 11, 2023 · 5 comments
Closed
1 task done

Code smells #57

rotu opened this issue Sep 11, 2023 · 5 comments

Comments

@rotu
Copy link

rotu commented Sep 11, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

The following code is slightly suspect. If written as intended, it could use a comment.

Due to the way once works, this expression throws an Array containing a single Error instead of the probably intended Error.

agent/lib/agents.js

Lines 126 to 128 in 4b2c4d1

(s) => once(socket, 'error', s).then((err) => {
throw err
}),

This code creates a non-resolving promise (maybe instead of an immediately-resolving Promise?).

return signal ? new Promise(() => {}) : null

Initially reported when trying to figure out the root cause behind: npm/cli#6763

Expected Behavior

No response

Steps To Reproduce

  1. In this environment...
  2. With this config...
  3. Run '...'
  4. See error...

Environment

  • npm:
  • Node:
  • OS:
  • platform:
@lukekarrys
Copy link
Contributor

Due to the way once works, this expression throws an Array containing a single Error instead of the probably intended Error.

This is a mistake and should be fixed. Nice catch!

This code creates a non-resolving promise (maybe instead of an immediately-resolving Promise?).

This could use a comment, or a refactor. This code path is only used in a Promise.race so passing a non-resolving promise is done on purpose if no timeout is passed in. Using an immediately resolving promise would make it immediately resolve the Promise.race which is not what is wanted.

@wraithgar
Copy link
Member

If the promise never resolves, what is it racing?

@wraithgar
Copy link
Member

wraithgar commented Sep 12, 2023

oh ... if no timeout is passed. We should just return the original promise then.

ETA I think a bit of a refactor is in order here too. These are good catches!

@rotu
Copy link
Author

rotu commented Sep 12, 2023

I figured the immediate resolution was intended, since that's also the behavior for when delay is 0.

Also, the createTimeout returns either null, a non-resolving Promise, a {start, clear} object, or an inscrutable wrapper promise. And it has an argument called signal which seems to be given an AbortController, not an AbortSignal?

This code is an impressive fractal of WTF!

lukekarrys added a commit that referenced this issue Sep 13, 2023
This allows a single agent to serve as both the http and https agents.

This PR also refactors some things based on feedback in #57.
lukekarrys added a commit that referenced this issue Sep 13, 2023
This allows a single agent to serve as both the http and https agents.

This PR also refactors some things based on feedback in #57.
lukekarrys added a commit that referenced this issue Sep 13, 2023
This allows a single agent to serve as both the http and https agents.

This PR also refactors some things based on feedback in #57.
lukekarrys added a commit that referenced this issue Sep 13, 2023
This allows a single agent to serve as both the http and https agents.

This PR also refactors some things based on feedback in #57.
lukekarrys added a commit that referenced this issue Sep 13, 2023
This allows a single agent to serve as both the http and https agents.

This PR also refactors some things based on feedback in #57.
lukekarrys added a commit that referenced this issue Sep 13, 2023
This allows a single agent to serve as both the http and https agents.

This PR also refactors some things based on feedback in #57.
lukekarrys added a commit that referenced this issue Oct 2, 2023
This allows a single agent to serve as both the http and https agents.

This PR also refactors some things based on feedback in #57.
lukekarrys added a commit that referenced this issue Oct 2, 2023
This allows a single agent to serve as both the http and https agents.

This PR also refactors some things based on feedback in #57.
lukekarrys added a commit that referenced this issue Oct 2, 2023
This allows a single agent to serve as both the http and https agents.

This PR also refactors some things based on feedback in #57.
@hashtagchris
Copy link
Contributor

Thanks @rotu! Core feedback addressed.

First array element is now thrown:

agent/lib/agents.js

Lines 149 to 151 in d3187c4

once(socket, 'error', { signal }).then((err) => {
throw err[0]
}),

util.js has been removed, and I couldn't find any non-resolving Promises.

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

4 participants