in src/utils/hibp.ts [55:98]
async function _throttledFetch(
url: string,
reqOptions?: RequestInit,
tryCount = 1,
) {
try {
const response = await fetch(url, reqOptions);
const responseJson = await response.json();
if (response.ok) return responseJson as unknown;
switch (response.status) {
case 404:
// 404 can mean "no results", return undefined response
logger.info("_throttledFetch", {
exception: "Error 404, not going to retry. TryCount: " + tryCount,
});
return undefined;
case 429:
logger.info("_throttledFetch", {
exception: "Error 429, tryCount: " + tryCount,
});
// @ts-ignore TODO: Explicitly parse into a number
if (tryCount >= HIBP_THROTTLE_MAX_TRIES) {
throw new Error("error_hibp_throttled");
} else {
tryCount++;
await new Promise((resolve) =>
setTimeout(
resolve,
Number.parseInt(HIBP_THROTTLE_DELAY, 10) * tryCount,
),
);
return await _throttledFetch(url, reqOptions, tryCount);
}
default:
throw responseJson;
}
} catch (e) {
if (e instanceof Error) {
logger.error("hibp_throttle_fetch_error", { stack: e.stack });
}
throw e;
}
}