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.js CHANGED
@@ -1845,10 +1845,26 @@ var CencoriEmbeddedApiError = class extends Error {
1845
1845
  this.name = "CencoriEmbeddedApiError";
1846
1846
  }
1847
1847
  };
1848
- function parseRetryAfterSeconds(value) {
1848
+ var HTTP_DATE_PATTERNS = [
1849
+ // IMF-fixdate, e.g. Sun, 06 Nov 1994 08:49:37 GMT (what toUTCString emits)
1850
+ /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/,
1851
+ // RFC 850, e.g. Sunday, 06-Nov-94 08:49:37 GMT
1852
+ /^[A-Za-z]+, \d{2}-[A-Za-z]{3}-\d{2} \d{2}:\d{2}:\d{2} GMT$/,
1853
+ // asctime, e.g. Sun Nov 6 08:49:37 1994
1854
+ /^[A-Za-z]{3} [A-Za-z]{3} ( \d|\d{2}) \d{2}:\d{2}:\d{2} \d{4}$/
1855
+ ];
1856
+ function parseRetryAfterSeconds(value, nowMs = Date.now()) {
1849
1857
  if (!value) return null;
1850
- const seconds = Number.parseInt(value.trim(), 10);
1851
- return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1858
+ const trimmed = value.trim();
1859
+ if (!trimmed) return null;
1860
+ if (/^[0-9]+$/.test(trimmed)) {
1861
+ const seconds = Number.parseInt(trimmed, 10);
1862
+ return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
1863
+ }
1864
+ if (!HTTP_DATE_PATTERNS.some((pattern) => pattern.test(trimmed))) return null;
1865
+ const timestamp = Date.parse(trimmed);
1866
+ if (!Number.isFinite(timestamp)) return null;
1867
+ return Math.max(0, Math.ceil((timestamp - nowMs) / 1e3));
1852
1868
  }
1853
1869
  async function request(config, method, path, body, extraHeaders, options) {
1854
1870
  const response = await fetch(`${config.baseUrl}${path}`, {
@@ -2490,8 +2506,7 @@ var Cencori = class {
2490
2506
  }
2491
2507
  if (response.status === 429) {
2492
2508
  const rawRetryAfter = response.headers.get("Retry-After") ?? response.headers.get("retry-after");
2493
- const parsed = rawRetryAfter ? Number.parseInt(rawRetryAfter.trim(), 10) : NaN;
2494
- let retryAfterSeconds = Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
2509
+ let retryAfterSeconds = parseRetryAfterSeconds(rawRetryAfter);
2495
2510
  if (retryAfterSeconds == null) {
2496
2511
  const secHint = errorData.retry_after_seconds ?? errorData.retry_after;
2497
2512
  if (typeof secHint === "number" && Number.isFinite(secHint) && secHint >= 0) {