-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
.json() shorthand doesn't resolve in nodejs env #46
Comments
The
|
This is most likely in issue with |
Looks like a very similar issue was already reported here: node-fetch/node-fetch#1131
|
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. |
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 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 ky({
// …
hooks: {
afterResponse: [
(request, options, response) => {
// return response so node doesn't keep hanging
return response;
},
],
},
}); |
Broken:
Works:
deps:
NOTE - Seems to depend on the response body somehow (size?), as it doesn't happen for all requests, but it is consistent within the context of a single request (ie. a broken request/response pair stays broken, a valid request/response pair works).
The text was updated successfully, but these errors were encountered: