-
Notifications
You must be signed in to change notification settings - Fork 541
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
Avoiding instanceof Request
, etc.
#2155
Comments
Unfortunately loosening that check wouldn't solve the issue; we need to access the internal Request state (via a symbol) that will not work since the global fetch's symbols are different from undici's symbols. The only way I can think of solving this is to use const request = {
[Symbol('kState')]: 'lol',
[Symbol('kSignal')]: null,
[Symbol.toStringTag]: 'Request'
} I'm not sure if this can be fixed, without causing other issues. |
Use SymbolFor? |
Possible, but creating a dozen global symbols (this would need to be done for all fetch globals, not just Request) would cause more issues like nodejs/node#43157. Alternatively we could move all undici symbols under a single global one? globalThis[Symbol.for('undici.global.symbol')] = {
state: Symbol('kState'),
// etc
} which still seems undesirable because it could be used to access the internal state of fetch objects |
I don't think we want to expose the internals as part of our API contract. We want to be able to change them and not worry if we would break this use case or not. I think we should just document this quirk. |
We should still consider avoiding instanceof because it can be overridden with Symbol.hasInstance. Instead we should be doing an an example: Object.defineProperty(Response, Symbol.hasInstance, {
value: () => true
})
console.log([] instanceof Response) // true |
It's kind of a bummer that undici and builtin fetch don't play ball. i have for instance wanted to only import the CacheStorage and tree shake away the rest and hopped that i could use it together with builtin Request/Response, but it's a very strict brand checking |
…edge-runtime and undici limitations The usual way to duplicate a request object is to pass the original request object to the Request constructor both as the `input` and `init` parameters, eg: super(req, req) However, this fails in certain environments like Vercel Edge Runtime when a framework like Remix polyfills the global Request object. This happens because `undici` performs the following instanceof check which, instead of testing against the global Request object, tests against the Request class defined in the same file (local Request class). For more details, please refer to: nodejs/undici#2155 https://github.com/nodejs/undici/blob/7153a1c78d51840bbe16576ce353e481c3934701/lib/fetch/request.js#L854
…edge-runtime and undici limitations The usual way to duplicate a request object is to pass the original request object to the Request constructor both as the `input` and `init` parameters, eg: super(req, req) However, this fails in certain environments like Vercel Edge Runtime when a framework like Remix polyfills the global Request object. This happens because `undici` performs the following instanceof check which, instead of testing against the global Request object, tests against the Request class defined in the same file (local Request class). For more details, please refer to: nodejs/undici#2155 https://github.com/nodejs/undici/blob/7153a1c78d51840bbe16576ce353e481c3934701/lib/fetch/request.js#L854
…edge-runtime and undici limitations The usual way to duplicate a request object is to pass the original request object to the Request constructor both as the `input` and `init` parameters, eg: super(req, req) However, this fails in certain environments like Vercel Edge Runtime when a framework like Remix polyfills the global Request object. This happens because `undici` performs the following instanceof check which, instead of testing against the global Request object, tests against the Request class defined in the same file (local Request class). For more details, please refer to: nodejs/undici#2155 https://github.com/nodejs/undici/blob/7153a1c78d51840bbe16576ce353e481c3934701/lib/fetch/request.js#L854
…edge-runtime and undici limitations (#3129) * fix(backend,remix): Tweak ClerkRequest initialization to work-around edge-runtime and undici limitations The usual way to duplicate a request object is to pass the original request object to the Request constructor both as the `input` and `init` parameters, eg: super(req, req) However, this fails in certain environments like Vercel Edge Runtime when a framework like Remix polyfills the global Request object. This happens because `undici` performs the following instanceof check which, instead of testing against the global Request object, tests against the Request class defined in the same file (local Request class). For more details, please refer to: nodejs/undici#2155 https://github.com/nodejs/undici/blob/7153a1c78d51840bbe16576ce353e481c3934701/lib/fetch/request.js#L854 * fix(backend,remix): Tweak ClerkRequest initialization to work-around edge-runtime and undici limitations The usual way to duplicate a request object is to pass the original request object to the Request constructor both as the `input` and `init` parameters, eg: super(req, req) However, this fails in certain environments like Vercel Edge Runtime when a framework like Remix polyfills the global Request object. This happens because `undici` performs the following instanceof check which, instead of testing against the global Request object, tests against the Request class defined in the same file (local Request class). For more details, please refer to: nodejs/undici#2155 https://github.com/nodejs/undici/blob/7153a1c78d51840bbe16576ce353e481c3934701/lib/fetch/request.js#L854
In Node.js 18.6, this piece of code doesn't work:
Error:
This is because
instanceof Request
fails due to the globalRequest
and the UndiciRequest
not being the same.undici/lib/fetch/request.js
Line 854 in 7153a1c
While many libraries accept a
fetch
argument (likeky
), not many libraries accept another set of globals (Request
,Response
,Headers
). Next to that,global.Request
is a read-only property in Node.js. I think that instead of usinginstanceof
Undici should be using another type of check for these. (e.g.request[Symbol.toStringTag] === 'Request'
).I'd like to collect thoughts and then I'll create a PR.
This would also fix cloudflare/miniflare#454
The text was updated successfully, but these errors were encountered: