@profullstack/threatcrush 0.15.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/daemon.js CHANGED
@@ -18220,11 +18220,14 @@ function buildFeedSet(parts, allow, sources, now = /* @__PURE__ */ new Date()) {
18220
18220
  }
18221
18221
  return { fetched_at: now.toISOString(), ips, domains, sources, skipped_shared: skipped };
18222
18222
  }
18223
+ var FETCH_ATTEMPTS = 3;
18224
+ var FatalFeedError = class extends Error {
18225
+ };
18223
18226
  function redact(message, key) {
18224
18227
  if (!key) return message;
18225
18228
  return message.split(key).join("<auth-key>").split(encodeURIComponent(key)).join("<auth-key>");
18226
18229
  }
18227
- async function fetchFeeds(settings, fetchImpl = fetch) {
18230
+ async function fetchFeeds(settings, fetchImpl = fetch, retryDelayMs = 5e3) {
18228
18231
  const sources = [];
18229
18232
  const parts = [];
18230
18233
  const key = settings.authKey?.trim() || void 0;
@@ -18236,13 +18239,24 @@ async function fetchFeeds(settings, fetchImpl = fetch) {
18236
18239
  }
18237
18240
  try {
18238
18241
  const extra = request2.init?.headers ?? {};
18239
- const res = await fetchImpl(request2.url, { ...request2.init, signal: AbortSignal.timeout(6e4), headers: { ...ua, ...extra } });
18240
- if (res.status === 401 || res.status === 403) throw new Error(`HTTP ${res.status}: abuse.ch refused the Auth-Key`);
18241
- if (!res.ok) throw new Error(`HTTP ${res.status}`);
18242
- const text = await res.text();
18243
- const type = res.headers.get("content-type") ?? "";
18244
- if (type.includes("text/html") || /^\s*<(!doctype|html)/i.test(text)) throw new Error(`HTTP ${res.status} but an HTML page, not feed data`);
18245
- const part = parse(text);
18242
+ let part;
18243
+ for (let attempt = 1; ; attempt++) {
18244
+ try {
18245
+ const res = await fetchImpl(request2.url, { ...request2.init, signal: AbortSignal.timeout(6e4), headers: { ...ua, ...extra } });
18246
+ if (res.status === 401 || res.status === 403) throw new FatalFeedError(`HTTP ${res.status}: abuse.ch refused the Auth-Key`);
18247
+ if (res.status >= 400 && res.status < 500) throw new FatalFeedError(`HTTP ${res.status}`);
18248
+ if (!res.ok) throw new Error(`HTTP ${res.status}`);
18249
+ const text = await res.text();
18250
+ const type = res.headers.get("content-type") ?? "";
18251
+ if (type.includes("text/html") || /^\s*<(!doctype|html)/i.test(text)) throw new FatalFeedError(`HTTP ${res.status} but an HTML page, not feed data`);
18252
+ part = parse(text);
18253
+ break;
18254
+ } catch (err) {
18255
+ const retryable = !(err instanceof FatalFeedError) && !/^ThreatFox API: /.test(err.message);
18256
+ if (!retryable || attempt >= FETCH_ATTEMPTS) throw err;
18257
+ await new Promise((r) => setTimeout(r, retryDelayMs * attempt));
18258
+ }
18259
+ }
18246
18260
  const count = (part.ips?.size ?? 0) + (part.domains?.size ?? 0);
18247
18261
  if (count === 0 && !emptyOk) throw new Error("no entries in the response");
18248
18262
  parts.push(part);
@@ -26911,7 +26925,7 @@ var ThreatFeeds = class {
26911
26925
  }
26912
26926
  async refresh() {
26913
26927
  try {
26914
- const set = await fetchFeeds(this.opts.settings, this.opts.fetchImpl);
26928
+ const set = await fetchFeeds(this.opts.settings, this.opts.fetchImpl, this.opts.retryDelayMs);
26915
26929
  const failed = set.sources.filter((s) => !s.ok);
26916
26930
  if (failed.length === set.sources.length) {
26917
26931
  this.opts.log(`[threat-feeds] refresh failed, keeping previous list: ${failed.map((s) => `${s.feed}: ${s.error}`).join("; ")}`);