got: Do not use invalid cache

Describe the bug

  • Node.js version: 13.3
  • OS & version: MacOS Catalina

Actual behavior

I get GotError: Unexpected token in JSON at position 0 in ... when I add responseType: "json" . But this happens 2 times out of 5 … and If I remove the responseType and log the body, all is good…

PS: right before this error there is this warning every time: (node:34140) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

Expected behavior

Should be able to parse JSON, I think got tries to parse before getting full response or something like that

Code to reproduce

const body = await got
    .get(link, {
      cache,
      retry: 4
    })
    .json();

or

const { body } = await got
    .get(link, {
      cache,
      retry: 4,
      responseType: 'json'
    });

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 19

Most upvoted comments

{
  'content-type': 'application/json',
  'last-modified': 'Wed, 27 Nov 2019 01:56:55 GMT',
  'content-encoding': 'gzip',
  vary: 'Accept-Encoding',
  age: '44979'
}
SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse (<anonymous>)
    at parseBody (/Users/devchris/Documents/code/nodejs/project/node_modules/got/dist/source/as-promise.js:12:47)
    at /Users/devchris/Documents/code/nodejs/project/node_modules/got/dist/source/as-promise.js:130:47
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

How can I get more? Isn’t good for you? And I checked: 1/2 times the key is removed in the cache file, so the next time he made the request, it’s working, then error, and he removes, etc…

The first character is the {.

Here’s the cache:

const cache = new KeyvFile({
  filename: join(appDataPath, "cache.json"), // the file path to store the data
  expiredCheckDelay: 10 * 24 * 3600 * 1000, // ms, check and remove expired data in each ms
  writeDelay: 120, // ms, batch write to disk in a specific duration, enhance write performance.
  encode: JSON.stringify, // serialize function
  decode: JSON.parse // deserialize function
});

And I will try to provide you a bigger stacktrace, thanks