Skip to content

Conversation

erunion
Copy link
Member

@erunion erunion commented Mar 18, 2022

Because main isn't ready to go out yet I've branched this change directly off the v4.2.0 release and will publish it from here and later backport this change into main.

This PR should not merged after it's approved. 🚨

🧰 Changes

We've found a quirk in how we're processing responses in Node 16 when making a successful API call where Node immediately kills the program without any errors or traces:

const api = require('api')
const ashby = api('@ashby/v1.0#3hpc18l02t6450');
ashby.auth('APIKEY');

try {
  (async () => {
    await ashby.joblist({ status: ['Open', 'Closed', 'Archived'] }).then(console.log).catch(console.error)
  })();
} catch (err) {
  throw err;
}

We've narrowed this down to the response.clone() call in parseResponse where every time that line is called it pollutes the general response object that we have present and the program dies when we reach await response.json().

Infuriatingly, we have tests for this case of if we can't process a non-JSON response with .json() to instead call .text() and this code, with the .clone() call, work all just fine on Node 16 in the isolated test case:

const { Response } = require('node-fetch');
const parseResponse = require('./src/lib/parseResponse');

const invalidJsonResponse = new Response('plain text', {
  headers: {
    'Content-Type': 'application/json',
  },
});

(async () => {
  await parseResponse(invalidJsonResponse).then(console.log).catch(console.error);
})();

There's just something different about how it's run through the entire api flow that breaks it that we're unable to find so instead of trying to mess around with it I've slightly refactored the parseResponse library to never call .clone() instead, opting to run JSON.parse() through a try-catch if we think that the content is JSON.

It's not the best solution but hey it works.

🧬 QA & Testing

It's really hard to debug this because all the tests here have been, and still, pass, but if the code looks good to you then 👍

@erunion erunion added the bug Something isn't working label Mar 18, 2022
@erunion erunion requested review from Dashron and ilias-t March 18, 2022 19:46
Copy link

@Dashron Dashron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow this sounds like a weird one. code looks fine.

@erunion erunion closed this Mar 18, 2022
@erunion erunion deleted the fix/node16-compat branch March 18, 2022 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants