mistval / node-fetch-cache

Node-fetch with built-in response caching.
MIT License
51 stars 8 forks source link

Error thrown in "getResponse" cannot access bodyStream of undefined #39

Closed TimDaub closed 8 months ago

TimDaub commented 8 months ago

I'm requesting an endpoint as normally, however it seems there is an issue with node-fetch cache here because it throws internally. When I request the same endpoint with curl, it just works

[1] 2023-12-23T09:07:27.875Z @attestate/kiwistand Error in response TypeError: Cannot read properties of undefined (reading 'bodyStream')
[1]     at getResponse (file:///Users/timdaub/Projects/attestate/kiwistand/node_modules/node-fetch-cache/src/index.js:158:23)
[1]     at async getTips (file:///Users/timdaub/Projects/attestate/kiwistand/src/tips.mjs:80:16)
[1]     at async index (file:///Users/timdaub/Projects/attestate/kiwistand/src/views/feed.mjs:283:16)
[1]     at async default (file:///Users/timdaub/Projects/attestate/kiwistand/src/views/feed.mjs:327:51)
[1]     at async file:///Users/timdaub/Projects/attestate/kiwistand/src/http.mjs:156:21

I had this problem for a while, and then it magically went away and it seemed the value was available again etc.

mistval commented 8 months ago

Hi, I noticed in your code you have ttl: 60, // 1 min, however the ttl needs to be given in milliseconds, so it should be 60_000 for one minute. I think you have encountered a bug with very short TTLs which is fixable, and I'd also like to rename this property to ttlMs and deprecate ttl, but I think your case will be resolved simply by using the longer TTL that it seems you intended.

TimDaub commented 8 months ago

ah ok thanks. But I think I wish the library would have thrown (e.g. when the cache is being configured) if I define a ttl that is so small that it cannot internally handle the value.

TimDaub commented 8 months ago

also thx btw for looking into my code that's some really nice way of helping out I appreciate it!

mistval commented 8 months ago

👍 I intend to fix the bug so that it will work even with a 0ms TTL (though that would be a little silly).

mistval commented 8 months ago

Fixed in new version 3.1.4 just published to NPM, thanks :)