cencori 1.7.3 → 1.7.4

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/index.mjs CHANGED
@@ -1777,10 +1777,26 @@ var CencoriEmbeddedApiError = class extends Error {
1777
1777
  this.name = "CencoriEmbeddedApiError";
1778
1778
  }
1779
1779
  };
1780
- function parseRetryAfterSeconds(value) {
1780
+ var HTTP_DATE_PATTERNS = [
1781
+ // IMF-fixdate, e.g. Sun, 06 Nov 1994 08:49:37 GMT (what toUTCString emits)
1782
+ /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/,
1783
+ // RFC 850, e.g. Sunday, 06-Nov-94 08:49:37 GMT
1784
+ /^[A-Za-z]+, \d{2}-[A-Za-z]{3}-\d{2} \d{2}:\d{2}:\d{2} GMT$/,
1785
+ // asctime, e.g. Sun Nov 6 08:49:37 1994
1786
+ /^[A-Za-z]{3} [A-Za-z]{3} ( \d|\d{2}) \d{2}:\d{2}:\d{2} \d{4}$/
1787
+ ];
1788
+ function parseRetryAfterSeconds(value, nowMs = Date.now()) {
1781
1789
  if (!value) return null;
1782
- const seconds = Number.parseInt(value.trim(), 10);
1783
- return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1790
+ const trimmed = value.trim();
1791
+ if (!trimmed) return null;
1792
+ if (/^[0-9]+$/.test(trimmed)) {
1793
+ const seconds = Number.parseInt(trimmed, 10);
1794
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1795
+ }
1796
+ if (!HTTP_DATE_PATTERNS.some((pattern) => pattern.test(trimmed))) return null;
1797
+ const timestamp = Date.parse(trimmed);
1798
+ if (!Number.isFinite(timestamp)) return null;
1799
+ return Math.max(0, Math.ceil((timestamp - nowMs) / 1e3));
1784
1800
  }
1785
1801
  async function request(config, method, path, body, extraHeaders, options) {
1786
1802
  const response = await fetch(`${config.baseUrl}${path}`, {
@@ -2422,8 +2438,7 @@ var Cencori = class {
2422
2438
  }
2423
2439
  if (response.status === 429) {
2424
2440
  const rawRetryAfter = response.headers.get("Retry-After") ?? response.headers.get("retry-after");
2425
- const parsed = rawRetryAfter ? Number.parseInt(rawRetryAfter.trim(), 10) : NaN;
2426
- let retryAfterSeconds = Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
2441
+ let retryAfterSeconds = parseRetryAfterSeconds(rawRetryAfter);
2427
2442
  if (retryAfterSeconds == null) {
2428
2443
  const secHint = errorData.retry_after_seconds ?? errorData.retry_after;
2429
2444
  if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {