Comments (5)
The afterResponse
hook does fire correctly - here's the logged Response
object:
[1] Response {
[1] size: 0,
[1] timeout: 0,
[1] [Symbol(Body internals)]: {
[1] body: PassThrough {
[1] _readableState: [ReadableState],
[1] _events: [Object: null prototype],
[1] _eventsCount: 5,
[1] _maxListeners: undefined,
[1] _writableState: [WritableState],
[1] allowHalfOpen: true,
[1] [Symbol(kCapture)]: false,
[1] [Symbol(kCallback)]: null
[1] },
[1] disturbed: false,
[1] error: null
[1] },
[1] [Symbol(Response internals)]: {
[1] url: 'REDACTED',
[1] status: 200,
[1] statusText: 'OK',
[1] headers: Headers { [Symbol(map)]: [Object: null prototype] },
[1] counter: undefined
[1] }
[1] }
from ky-universal.
This is most likely in issue with node-fetch
. Ky doesn't have any web reported issues about .json()
.
from ky-universal.
Looks like a very similar issue was already reported here: node-fetch/node-fetch#1131
.clone()
seems to be the problem.
from ky-universal.
I found this: https://github.com/sindresorhus/ky/blob/ae2fe071296702381d30790a19e9137dd51babaa/source/core/Ky.ts#L86-L105
const awaitedResult = await result;
const response = awaitedResult.clone();
if (type === 'json') {
if (response.status === 204) {
return '';
}
const arrayBuffer = await response.clone().arrayBuffer();
const responseSize = arrayBuffer.byteLength;
if (responseSize === 0) {
return '';
}
if (options.parseJson) {
return options.parseJson(await response.text());
}
}
return response[type]();
to summarize: you clone the response but only consume one of the responses.
in an ideal world you would consume both tee:ed streams at the same time in parallel if you .clone()
them
from ky-universal.
That's it! I've been neck deep into #8, as all symptoms are the same when using the native node (v18.16) fetch, but simply handling the response, releases the process.
It's the afterResponse
hook that made my process hang, even when it's a simple noop
.
ky({
// …
hooks: {
afterResponse: [
(request, options, response) => {
// consume the response stream so node doesn't keep hanging
response.text().catch(Object);
},
],
},
});
Is this something that can be fixed in https://github.com/sindresorhus/ky?
It looks like it could. I've patched my local instance, and this seems to work. Are there any downsides of doing so?
for (const hook of ky._options.hooks.afterResponse) {
+ const clone = response.clone();
// eslint-disable-next-line no-await-in-loop
const modifiedResponse = await hook(
ky.request,
ky._options as NormalizedOptions,
- ky._decorateResponse(response.clone()),
+ ky._decorateResponse(clone),
);
+ if (!clone.bodyUsed) clone.text().catch(Object);
if (modifiedResponse instanceof globalThis.Response) {
response = modifiedResponse;
}
}
And the more simpler fix is to return the response
from the hook 😬 . Maybe it's just a matter of updating docs & typs?
ky({
// …
hooks: {
afterResponse: [
(request, options, response) => {
// return response so node doesn't keep hanging
return response;
},
],
},
});
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
- 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.