in src/https.ts [158:194]
function getJsonDestination(
uri: string,
statusCode: number | undefined,
headers: IncomingHttpHeaders
) {
return async (responseIterable: AsyncIterableIterator<Buffer>) => {
if (statusCode === 429) {
throw new FetchError(uri, "Too many requests");
} else if (statusCode !== 200) {
throw new NonRetryableFetchError(
uri,
`Status code is ${statusCode}, expected 200`
);
}
if (
!headers["content-type"]?.toLowerCase().startsWith("application/json")
) {
throw new NonRetryableFetchError(
uri,
`Content-type is "${headers["content-type"]}", expected "application/json"`
);
}
const collected = [] as Buffer[];
for await (const chunk of responseIterable) {
collected.push(chunk);
}
try {
return safeJsonParse(
new TextDecoder("utf8", { fatal: true, ignoreBOM: true }).decode(
Buffer.concat(collected)
)
);
} catch (err) {
throw new NonRetryableFetchError(uri, err);
}
};
}