-
Notifications
You must be signed in to change notification settings - Fork 609
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
feat: use kType instead of instanceOf on undici classes #2659
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #2659 +/- ##
==========================================
- Coverage 85.54% 85.41% -0.14%
==========================================
Files 76 84 +8
Lines 6858 7551 +693
==========================================
+ Hits 5867 6450 +583
- Misses 991 1101 +110 ☔ View full report in Codecov by Sentry. |
I don't think this is correct. Those various classes could depend on internals, and we can guarantee that the internal API would stay consistent. How about having a |
I dont understand. Which classes depend on internals? Should setupGlobals override the symbols or the classes of the global or should it override the symbols and classes from the userland undici? I would actually expect that undici as userland module would not manipulate undici as node core dep. So it makes the direction of information flow node core dep undici => userland module undici. So this is why I took the symbols from globalThis. Can you please explain more, what change you would like to see? |
If we change the content of kState or any of the other properties, this won't work anymore. fetch(), Request and Response are all intertwined and this works only because it's essentially the same version. However, we can't guarantee this would work for 3+ years.
I don't have a good solution to this problem, minus having a function that replaces the core globals with the one in undici, essentially installing it globally. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overriding the globals isn't actually a bad idea (if it's manual, not automatic); node doesn't set them up 'correctly' for startup performance reasons, so nothing would noticeably change.
I dont know if this is the right approach. @jimmywarting especially mentioned, that we wanted to tree shake the cache implementation and use native fetch and response etc.. So the goal of his request was to reduce the userland code. If we want to override the global objects, than this means more userland code. So the goal of jimmy of less userland code is not possible. We will always have the issue of potential breaking changes. A potential solution could be to store the major version of undici as a symbol to the fetch implementation and if userland undici is same as node core undici, we take the symbols of global Request. Or we make it strict, that they only sync the symbols when the versions are the same. That way we never run in any annoying issue, that a dev can complain about breaking his code. |
undici updates are common and it's unlikely that the version of undici being used in node core is the same as the one installed. But, overriding globals also has plenty of issues. As you can tell, there's a reason I didn't take a shot at fixing this lol |
Yes I see that. I think it is either solve it somehow or document and close the issue. But I think for the beginning it would be ok, to ensure that undici objects are compatible to each other if the versions match. And if somebody really relies on this feature, he has to ensure that the versions are the same. |
but that idea won't really work in practice, or it would cause very hard to debug issues. |
I would actually expect that a function is provided by userland undici, lets call it On the other hand, overwriting globals via undici package can be easily implemented, but the goal of @jimmywarting is not achievable. This PR is imho still somehow "good". We could avoid instanceOf, which is probably slower than a attribute check? wdyt? |
This is the reason why we didn't implement this feature. How about we get the undici version number from I still think we should add a |
Checking for compatibility at all would cause issues, especially if teams use different node versions of versions of undici 😕. Overriding the globals may cause issues with something like WebAssembly.instantiateStreaming for example, but we could probably figure it out. |
Is the check of |
no, |
This could be worth exploring, especially as it will require explicit calls from the user space, meaning they know the possible risks. We can make a best effort to ensure compatibility and fix possible issues. @mcollina What do you have in mind about it? maybe we can create an issue to redirect the discussion there |
Go for it |
I have some ideas too I'll throw in to the discussion. |
@KhafraDev, please be my guest: #2674 |
I modified this PR. It introduces a kType |
I don't see how this fixes the problem. The internal symbols are different anyway, and internal state is going to be accessed. |
Didnt you write to use a kType instead of instanceof? If I understood that correctly, this should be the first step. Second step is to overwrite the globals?! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about. .constructor.name === 'Request
? We use that in other places.
we actually got rid of those checks because they weren't reliable |
an example being class A extends Request {} new A('https://a').constructor.name // TypeError |
I dont think this is gonna get merged ;) |
Resolves #2155
I think this is the most minimalistic solution for the issue as it avoids constantly calling
Object.getOwnPropertySymbols
on Request Objects.But somehow I expect you not liking the changes in symbols.js.
Anyhow... lets discuss this. Maybe we agree on a different solution.
This relates to...
Rationale
Changes
Features
Bug Fixes
Breaking Changes and Deprecations
Status