Comments (8)
@gutenye @sindresorhus In version 3.0.0
of node-fetch
you will be able to manually set highWaterMark
and prevent this issue from happening 😄
fetch(url, {highWaterMark: 10}).then(res => res.clone().buffer());
from ky-universal.
It would be pretty challenging to re-implement .clone()
ourselves. In doing so, we would probably end up creating more new bugs than we solved. The only practical way forward that I see here would be to just not clone the response at all.
We currently clone in three places:
- When calling
afterResponse
hooks - When streaming the response to provide progress events
- When body method shortcuts are used (e.g.
ky().json()
)
I'm not sure which, if any, of these places would be safe to remove. None of them seem particularly important to me, though I like having them for safety.
It's worth noting that we only call .clone()
if you use one of the above mentioned features. So you can actually avoid this problem if you can live without those features.
For example, if .clone()
is being called because you are using the .json()
method like this:
const users = await ky.get('users').json();
It's perfectly fine to re-write that to:
const response = await ky.get('users');
const users = await response.json();
... in which case .clone()
is not called. The only caveat, beyond it being more verbose, is that you will also have to set the Accept: application/json
header yourself, if you need that.
Other than those workarounds, I'm not sure there's much we can do, since we are using the API correctly and this is really an upstream issue. We could explore alternative fetch polyfills, but I suspect that would again introduce its own set of problems. Let's keep an eye on that issue you referenced.
In the meantime, should we consider removing .clone()
internally and, if so, where? Removing 3
seems the safest to me and it's probably the most common case as well.
from ky-universal.
I don't think we should remove .clone()
. We should rather get node-fetch
fixed.
I'm gonna add a bounty here that applies to getting node-fetch/node-fetch#386. If you want to work on it, see: node-fetch/node-fetch#563 (comment)
from ky-universal.
Here's a simple repro: https://github.com/exogen/ky-afterResponse-repro
This doesn't seem to happen for every request; I suspect it might have something to do with the response body size. I tried a couple small public JSON API demos before I landed on a GraphQL endpoint that reproduced the behavior.
Under certain conditions, simply including an
afterResponse
hook causes the response Promise to hang. The hook doesn't have to do anything; it can be a no-op function or just return the same cloned response. Removing the hook causes it to start working. Switching tonode-fetch
also works fine.I wonder if the hook causes the response to be cloned and therefore the Promise as well, making one of them never resolve? Just a thought.
from ky-universal.
@issuehunt has funded $80.00 to this issue.
- Submit pull request via IssueHunt to receive this reward.
- Want to contribute? Chip in to this issue via IssueHunt.
- Checkout the IssueHunt Issue Explorer to see more funded issues.
- Need help from developers? Add your repository on IssueHunt to raise funds.
from ky-universal.
Drive by comment as it seems like wheels are already in motion to fix this: I think this should be added to the README.
I've hit this behaviour while writing up endpoint methods for an api in our electron app; b/c I was being lazy I was just running the code via ts-node
to test the responses & such, rather than recompling the whole app and running electron.
I was really confused for a while, getting ready to deep dive into ky
thinking I might have found a major bug, since it looked like just nothing - node would just exit w/o any errors, and no promises would resolve.
Given that it seems switching from .json()
to .then(r => r.json())
will fix this for node w/ no downside in either browser or node, I think it's worth sticking that in the README 😄
from ky-universal.
Thanks @sholladay. I can confirm the workaround works for my case with .json()
.
from ky-universal.
@sindresorhus has rewarded $72.00 to @xxczaki. See it on IssueHunt
- 💰 Total deposit: $80.00
- 🎉 Repository reward(0%): $0.00
- 🔧 Service fee(10%): $8.00
from ky-universal.
Related Issues (20)
- Loading ky-universal from node loads browser.js instead of index.js HOT 4
- next js - error: Cannot use import statement outside a module HOT 6
- Must use import to load ES Module HOT 2
- await is only valid in async functions and the top level bodies of modules HOT 12
- Error when importing in Node.js HOT 1
- Consider switching to Undici HOT 10
- Workaround for ky-universal not being able to be used with Jest HOT 1
- Error [ERR_REQUIRE_ESM]: Must use import to load ES Module HOT 2
- CVE-2022-0235 vulnerability in node-fetch HOT 1
- Using Ky breaks Vuex HOT 2
- Can't access `HTTPError` etc. from `ky-universal` HOT 2
- Ky requires DOM types HOT 1
- Tests fail after upgrading ky-universal from 0.9.1 to 0.10.x (underlying error: TextDecoder is not defined) HOT 1
- I changed how to add proxy in KY HOT 1
- HighWatermark FAQ clarification HOT 3
- globalThis.Request is not a constructor
- Create-React-App testing fails with ReferenceError: Buffer is not defined HOT 2
- .json() shorthand doesn't resolve in nodejs env HOT 5
- Can ky clients also be isomorphic?
- Reference Error : Buffer is not defined. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ky-universal.