jaredwray/cacheable

Cache decompressed response body

simontabor opened this issue · 5 comments

Feature request from sindresorhus/got#1158.

I noticed that the request cache is storing the compressed body. This adds extra computational load when using the cache - we're decompressing it every time.

Is there any way that the cache could store the decompressed body to avoid this?

Hmmn, I'm not sure that's a good thing to add directly in cacheable-request. It could cause unpredictable results. If we decompress the response do we keep or remove compression headers? Are people consuming this API expecting it to do that?

I would suggest maybe a better idea would be to add a sort of hook to process the response before it's stored. This would allow Got to set any compression stuff it wants and handle it correctly without changing the way the module will function for other consumers.

e.g If that was implemented it could be used something like this in Got:

const processCompressedResponse = response => {
	if (isCompressed(response)) {
		return decompress(response);
	}

	return response;
};

cacheableRequest.addHook('response', processCompressedResponse)

Yeah, that feels like a more natural implementation - this library doesn't care about whether the response is compressed or not. @szmarczak any thoughts?

I agree with @lukechilds - the idea of hooks seems promising.

@szmarczak @simontabor - we are looking to accept the changes by @alphmth with the hooks integrated into the system. In addition to this it will be the v9 release of cacheable-request which will require ESM moving forward. Just FYI and we will have release notes on the changes and updates.

@szmarczak and @simontabor - this is now merged and being released. Please let us know if you would like any changes.