Open matthieusieben opened 7 months ago
cc @KhafraDev wdyt? I think this is needded.
we've talked about it in the past and there is no good way to support it
Then we should document it properly.
Is it not a good way to support it, because it would be too complex or, or because it would eat up a lot of performance?
Then we should document it properly.
Definitely
Is it not a good way to support it, because it would be too complex or, or because it would eat up a lot of performance?
Neither... well, complexity is also an issue, but it's just that the solution would be sloppy and probably wouldn't work on every version of node we support. For example: what if undici releases a minor version that updates internal state of Request/Response/etc. and is no longer compatible with a node version(s)? It's not impossible to work around, but there is fundamentally no good solution to the problem.
It's important to remember that while undici implements fetch, we do not control it when it's used in node. The webidl implementation we have is also meant to be spec compliant, there is no such thing as "RequestInterface" as a possible value of RequestInfo.
Why things won't work:
class A extends Request {}
will fail the check.It's totally possible for a third party library to handle the issues themselves, access whatever internal state, and make the two compatible. Take the internal state from one, add whatever is needed (if anything), and assign it to the other. Not a good thing for undici to implement ourselves, but I can see someone else doing it.
I think adding that script and a undici.__overrideGlobals()
method would be good.
I think you are right, sadly. The docs should indeed clearly indicate that undici
's exported members cannot/mustnot be used with those from another version (or Node's).
IMO, the docs should probably recommend to avoid using fetch
/ Request
/ Response
& co from undici
at all if the runtime supports those, and rely on something like this if an up-to-date version is required.
This would solve...
undici is not compatible with other versions of itself, or Node's.
If a library uses
undici
'sfetch()
internally and the user of the library uses Node'sRequest
, or vice versa,fetch()
call will fail with an error like so:This error is misleading as the input is actually a Request that gets casted to string because it comes from another version of
undici
:https://github.com/nodejs/undici/blob/f51f226522ec75a0613a07a4efc8f78938030c45/lib/web/fetch/request.js#L924-L934
The implementation should look like...
When checking if an input is a request, instead of using
instanceof
, an thorough interface check could be performed. Every fields from theinput
should be checked against the spec's interface https://fetch.spec.whatwg.org/#requestinfoNote that every field of the input Request (including
dispatcher
) should be carried over to the new request.Symbols would probably need to be using a globally namespaced form instead of locally declared symbols (
Symbol.for('undici.XYZ')
) for the initalization process to work here:https://github.com/nodejs/undici/blob/f51f226522ec75a0613a07a4efc8f78938030c45/lib/web/fetch/request.js#L107-L118
I have also considered...
Additional context
I am trying to make a library to ease the use of
fetch()
by allowing to tranform Requests, and wrap node'sfetch()
. This is not possible if Request/fetch from different sources are mixed together