Code Monkey home page Code Monkey logo

Comments (5)

iscekic avatar iscekic commented on May 18, 2024

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.

sindresorhus avatar sindresorhus commented on May 18, 2024

This is most likely in issue with node-fetch. Ky doesn't have any web reported issues about .json().

from ky-universal.

iscekic avatar iscekic commented on May 18, 2024

Looks like a very similar issue was already reported here: node-fetch/node-fetch#1131

.clone() seems to be the problem.

from ky-universal.

jimmywarting avatar jimmywarting commented on May 18, 2024

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.

smeijer avatar smeijer commented on May 18, 2024

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;
	}
}

https://github.com/sindresorhus/ky/blob/356d61c1534c9dc2cfb8ce1c1ff5c4832579e11b/source/core/Ky.ts#L34-L45


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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.