scrapely 1.2.18 → 1.2.19

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/actor.js CHANGED
@@ -1062,9 +1062,16 @@ class Actor {
1062
1062
  label: 'PUT status message',
1063
1063
  retryWindowMs: options?.retryWindowMs,
1064
1064
  }, async (response) => (await response.json().catch(() => ({})))).catch((err) => {
1065
- throw new Error(`Failed to update status message: ${describeRunApiError(err)}`);
1065
+ const error = new Error(`Failed to update status message: ${describeRunApiError(err)}`);
1066
+ // A progress update is best effort: if the API still fails after the
1067
+ // retry window (e.g. a long 503), log it and carry on instead of
1068
+ // crashing the run. Terminal updates keep throwing.
1069
+ if (isStatusMessageTerminal)
1070
+ throw error;
1071
+ console.warn(`[Scrapely] ${error.message} (continuing)`);
1072
+ return undefined;
1066
1073
  });
1067
- return (body.data ?? {});
1074
+ return (body?.data ?? {});
1068
1075
  }
1069
1076
  /**
1070
1077
  * Creates a proxy configuration and returns a promise resolving to an instance
@@ -234,6 +234,8 @@ export declare function pushDataAndCharge<T>({ chargingManager, items, eventName
234
234
  /**
235
235
  * @internal True when a dataset push certainly stored nothing: the storage API
236
236
  * answered with an error status, or the request never reached it. Timeouts and
237
- * resets after the request was sent are not proof (the write may have landed).
237
+ * resets after the request was sent are not proof (the write may have landed),
238
+ * and neither is any error of a push one of whose earlier attempts may have
239
+ * been applied (`outcomeUnknown`, set by the storage client's retries).
238
240
  */
239
241
  export declare function pushCertainlyFailed(err: unknown): boolean;
package/dist/charging.js CHANGED
@@ -25,7 +25,7 @@ const PENDING_MAX_ENTRIES = 1024;
25
25
  // so the retries here can honour deadlines: 429, 408, 5xx, timeouts and
26
26
  // network errors are re-sent with the same idempotency key, with exponential
27
27
  // backoff and jitter, never sooner than the server's Retry-After, within the API
28
- // retry window (2 minutes, SCRAPELY_API_RETRY_WINDOW_SECS) or the caller's
28
+ // retry window (5 minutes, SCRAPELY_API_RETRY_WINDOW_SECS) or the caller's
29
29
  // deadline. Then the charge stays pending / the void is re-queued and retried
30
30
  // before the next push and at exit, the same as the Python SDK.
31
31
  const POST_ATTEMPTS = 64;
@@ -1182,9 +1182,15 @@ const NOT_SENT_CODES = new Set(['ECONNREFUSED', 'ENOTFOUND', 'EAI_AGAIN', 'EHOST
1182
1182
  /**
1183
1183
  * @internal True when a dataset push certainly stored nothing: the storage API
1184
1184
  * answered with an error status, or the request never reached it. Timeouts and
1185
- * resets after the request was sent are not proof (the write may have landed).
1185
+ * resets after the request was sent are not proof (the write may have landed),
1186
+ * and neither is any error of a push one of whose earlier attempts may have
1187
+ * been applied (`outcomeUnknown`, set by the storage client's retries).
1186
1188
  */
1187
1189
  function pushCertainlyFailed(err) {
1190
+ for (let e = err, depth = 0; e && depth < 4; e = e.cause, depth++) {
1191
+ if (e.outcomeUnknown === true)
1192
+ return false;
1193
+ }
1188
1194
  if ((0, api_storage_client_js_1.isConnectFailure)(err))
1189
1195
  return true;
1190
1196
  for (let e = err, depth = 0; e && depth < 4; e = e.cause, depth++) {
@@ -24,11 +24,15 @@ export declare class ApiStorageClientError extends Error {
24
24
  */
25
25
  export declare function isRetryableResponse(status: number, idempotent: boolean, errorType?: string, bodyText?: string): boolean;
26
26
  /** Default time budget for retrying one API call (429, 503, network errors). */
27
- export declare const DEFAULT_API_RETRY_WINDOW_MS = 120000;
27
+ export declare const DEFAULT_API_RETRY_WINDOW_MS = 300000;
28
+ /** Whether the API at `url` (any URL of it; default: the last one seen) de-duplicates keyed writes. */
29
+ export declare function apiDedupesKeyedWrites(url?: string): boolean;
30
+ /** Test hook: forget what was learned about the API, or force an answer for every origin. */
31
+ export declare function _resetKeyedWriteSupport(value?: boolean): void;
28
32
  /**
29
33
  * How long one API call keeps retrying before it gives up:
30
34
  * SCRAPELY_API_RETRY_WINDOW_SECS when set, else {@link BaseApiClient.retryWindowMs}
31
- * (2 minutes by default).
35
+ * (5 minutes by default).
32
36
  */
33
37
  export declare function apiRetryWindowMs(): number;
34
38
  /**
@@ -51,6 +55,13 @@ export interface RetryingFetchOptions {
51
55
  retryWindowMs?: number;
52
56
  /** Short label for log lines, e.g. "POST /datasets/x/items". */
53
57
  label: string;
58
+ /**
59
+ * Idempotency-Key for a write (one per logical write, the same on every
60
+ * attempt). Once the API is known to de-duplicate keyed writes, the write
61
+ * is retried like an idempotent call: a re-send of an applied write returns
62
+ * the original answer instead of applying it again.
63
+ */
64
+ idempotencyKey?: string;
54
65
  }
55
66
  export interface RetryingFetchInit {
56
67
  method: string;
@@ -59,15 +70,17 @@ export interface RetryingFetchInit {
59
70
  }
60
71
  /**
61
72
  * fetch() with the API retry policy. Retries, with exponential backoff and
62
- * jitter that honours Retry-After, until the retry window (2 minutes by
73
+ * jitter that honours Retry-After, until the retry window (5 minutes by
63
74
  * default) runs out:
64
75
  * - 429 (run API rate limit, dataset backpressure): always; nothing was applied.
65
76
  * - 503 from the limiter or HAProxy: always; nothing was forwarded.
66
- * - 500/502/503/504, timeouts and dropped connections: idempotent calls only.
77
+ * - 500/502/503/504, timeouts and dropped connections: idempotent calls, and
78
+ * writes with an Idempotency-Key once the API is known to de-duplicate them.
67
79
  * - Refused/unresolvable connections: always; the request never left.
68
- * A write (dataset push, KV PUT, metamorph) whose request may have reached the
69
- * API is never re-sent: there is no idempotency key for it, so a re-send could
70
- * store the same rows twice. Such failures surface to the caller instead.
80
+ * Any other write (KV PUT, metamorph, a keyed push to an API that did not say
81
+ * it de-duplicates) whose request may have reached the API is never re-sent:
82
+ * a re-send could apply it twice. Such failures surface to the caller instead,
83
+ * marked `outcomeUnknown` when an attempt may have been applied.
71
84
  * Non-2xx answers that are not retried throw ApiStorageClientError.
72
85
  */
73
86
  export declare function fetchWithRetries<R>(url: string, init: RetryingFetchInit, options: RetryingFetchOptions, decode: (response: Response) => Promise<R>): Promise<R>;
@@ -81,6 +94,8 @@ interface ApiRequestOptions {
81
94
  doNotRetryTimeouts?: boolean;
82
95
  /** Override the method-based idempotency (e.g. get-or-create by name is safe to repeat). */
83
96
  idempotent?: boolean;
97
+ /** Idempotency-Key for a write; see {@link RetryingFetchOptions.idempotencyKey}. */
98
+ idempotencyKey?: string;
84
99
  }
85
100
  /**
86
101
  * Base class for API-based storage clients.
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiStorageClientFactory = exports.ApiRequestQueueClient = exports.ApiKeyValueStoreClient = exports.ApiDatasetClient = exports.BaseApiClient = exports.DEFAULT_API_RETRY_WINDOW_MS = exports.ApiStorageClientError = void 0;
4
4
  exports.isRetryableResponse = isRetryableResponse;
5
+ exports.apiDedupesKeyedWrites = apiDedupesKeyedWrites;
6
+ exports._resetKeyedWriteSupport = _resetKeyedWriteSupport;
5
7
  exports.apiRetryWindowMs = apiRetryWindowMs;
6
8
  exports.serverRetryDelayMs = serverRetryDelayMs;
7
9
  exports.isConnectFailure = isConnectFailure;
@@ -161,12 +163,72 @@ function isRetryableResponse(status, idempotent, errorType, bodyText) {
161
163
  return errorType === 'rate-limit-unavailable' || (bodyText ?? '').includes(NO_BACKEND_MARKER);
162
164
  }
163
165
  /** Default time budget for retrying one API call (429, 503, network errors). */
164
- exports.DEFAULT_API_RETRY_WINDOW_MS = 120_000;
166
+ exports.DEFAULT_API_RETRY_WINDOW_MS = 300_000;
167
+ /**
168
+ * Response header the API sets on every dataset push that sent an
169
+ * Idempotency-Key, once it de-duplicates such pushes. An older API ignores the
170
+ * key, and re-sending a push there could store the rows twice, so ambiguous
171
+ * failures of keyed writes are retried only while the latest keyed answer from
172
+ * that API origin carried this header. A 2xx without it (an older instance,
173
+ * e.g. mid-deploy or after a rollback) switches the retries off again.
174
+ */
175
+ const IDEMPOTENCY_SUPPORT_HEADER = 'x-scrapely-idempotency';
176
+ const keyedWritesDeduped = new Map();
177
+ /** Test override for every origin (undefined: learn from answers). */
178
+ let keyedWritesDedupedOverride;
179
+ function originOf(url) {
180
+ try {
181
+ return new URL(url).origin;
182
+ }
183
+ catch {
184
+ return url;
185
+ }
186
+ }
187
+ /** Whether the API at `url` (any URL of it; default: the last one seen) de-duplicates keyed writes. */
188
+ function apiDedupesKeyedWrites(url) {
189
+ if (keyedWritesDedupedOverride !== undefined)
190
+ return keyedWritesDedupedOverride;
191
+ if (url !== undefined)
192
+ return keyedWritesDeduped.get(originOf(url)) === true;
193
+ return [...keyedWritesDeduped.values()].pop() === true;
194
+ }
195
+ /** Test hook: forget what was learned about the API, or force an answer for every origin. */
196
+ function _resetKeyedWriteSupport(value) {
197
+ keyedWritesDeduped.clear();
198
+ keyedWritesDedupedOverride = value === true ? true : undefined;
199
+ }
200
+ function noteKeyedAnswer(url, response) {
201
+ const origin = originOf(url);
202
+ if (response.headers.has(IDEMPOTENCY_SUPPORT_HEADER)) {
203
+ keyedWritesDeduped.delete(origin);
204
+ keyedWritesDeduped.set(origin, true);
205
+ }
206
+ else if (response.ok) {
207
+ // Stored by a handler that ignored the key: this API (instance) does not de-duplicate.
208
+ keyedWritesDeduped.delete(origin);
209
+ keyedWritesDeduped.set(origin, false);
210
+ if (keyedWritesDedupedOverride === true)
211
+ keyedWritesDedupedOverride = undefined;
212
+ }
213
+ }
214
+ /** API error types of a keyed write: still being stored (retry), or stored-or-not unknown (final). */
215
+ const KEY_IN_PROGRESS = 'idempotency-key-in-progress';
216
+ const OUTCOME_UNKNOWN = 'push-outcome-unknown';
217
+ /**
218
+ * Marks an error whose request may have been applied by an earlier attempt
219
+ * (timeout, dropped connection or 5xx before the final answer), so callers
220
+ * don't treat it as "nothing was stored".
221
+ */
222
+ function markOutcomeUnknown(err) {
223
+ if (err && typeof err === 'object')
224
+ err.outcomeUnknown = true;
225
+ return err;
226
+ }
165
227
  const MAX_BACKOFF_MS = 30_000;
166
228
  /**
167
229
  * How long one API call keeps retrying before it gives up:
168
230
  * SCRAPELY_API_RETRY_WINDOW_SECS when set, else {@link BaseApiClient.retryWindowMs}
169
- * (2 minutes by default).
231
+ * (5 minutes by default).
170
232
  */
171
233
  function apiRetryWindowMs() {
172
234
  const env = Number(process.env.SCRAPELY_API_RETRY_WINDOW_SECS);
@@ -231,22 +293,32 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
231
293
  const sharedRateLimiter = new AdaptiveRateLimiter();
232
294
  /**
233
295
  * fetch() with the API retry policy. Retries, with exponential backoff and
234
- * jitter that honours Retry-After, until the retry window (2 minutes by
296
+ * jitter that honours Retry-After, until the retry window (5 minutes by
235
297
  * default) runs out:
236
298
  * - 429 (run API rate limit, dataset backpressure): always; nothing was applied.
237
299
  * - 503 from the limiter or HAProxy: always; nothing was forwarded.
238
- * - 500/502/503/504, timeouts and dropped connections: idempotent calls only.
300
+ * - 500/502/503/504, timeouts and dropped connections: idempotent calls, and
301
+ * writes with an Idempotency-Key once the API is known to de-duplicate them.
239
302
  * - Refused/unresolvable connections: always; the request never left.
240
- * A write (dataset push, KV PUT, metamorph) whose request may have reached the
241
- * API is never re-sent: there is no idempotency key for it, so a re-send could
242
- * store the same rows twice. Such failures surface to the caller instead.
303
+ * Any other write (KV PUT, metamorph, a keyed push to an API that did not say
304
+ * it de-duplicates) whose request may have reached the API is never re-sent:
305
+ * a re-send could apply it twice. Such failures surface to the caller instead,
306
+ * marked `outcomeUnknown` when an attempt may have been applied.
243
307
  * Non-2xx answers that are not retried throw ApiStorageClientError.
244
308
  */
245
309
  async function fetchWithRetries(url, init, options, decode) {
246
- const { idempotent, timeoutMs, label } = options;
247
- const retryTimeouts = options.retryTimeouts !== false && idempotent;
310
+ const { timeoutMs, label, idempotencyKey } = options;
311
+ const headers = idempotencyKey ? { ...init.headers, 'Idempotency-Key': idempotencyKey } : init.headers;
312
+ // Re-sending is safe for idempotent calls, and for keyed writes once the
313
+ // API showed it de-duplicates them (learned from any keyed answer).
314
+ const safeToResend = () => options.idempotent || (idempotencyKey !== undefined && apiDedupesKeyedWrites(url));
315
+ const retryTimeouts = () => options.retryTimeouts !== false && safeToResend();
248
316
  const deadline = Date.now() + Math.max(0, options.retryWindowMs ?? apiRetryWindowMs());
249
317
  let attempt = 0;
318
+ // An attempt of a non-idempotent call may have been applied (timeout,
319
+ // dropped connection, 5xx): the final error must not claim "nothing applied".
320
+ let maybeApplied = false;
321
+ const fail = (err) => (maybeApplied && !options.idempotent ? markOutcomeUnknown(err) : err);
250
322
  /** Sleeps before the next attempt; false once the retry window is used up. */
251
323
  const backoff = async (why, retryAfter) => {
252
324
  // Retrying sooner than asked would only be refused again: give up when
@@ -264,8 +336,10 @@ async function fetchWithRetries(url, init, options, decode) {
264
336
  const controller = new AbortController();
265
337
  const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
266
338
  try {
267
- const response = await fetch(url, { method: init.method, headers: init.headers, body: init.body, signal: controller.signal });
339
+ const response = await fetch(url, { method: init.method, headers, body: init.body, signal: controller.signal });
268
340
  sharedRateLimiter.updateFromHeaders(response.headers);
341
+ if (idempotencyKey)
342
+ noteKeyedAnswer(url, response);
269
343
  if (response.ok) {
270
344
  try {
271
345
  // Decoded inside the retry loop: a body cut off after
@@ -275,19 +349,20 @@ async function fetchWithRetries(url, init, options, decode) {
275
349
  catch (bodyErr) {
276
350
  // The request was applied (2xx); a write whose answer
277
351
  // timed out while being read must not be sent again.
352
+ maybeApplied = true;
278
353
  if (bodyErr.name === 'AbortError') {
279
- if (!idempotent || !retryTimeouts) {
280
- const timeoutError = new Error(`Request timed out after ${timeoutMs}ms (response body${idempotent ? '' : '; the request was applied'})`);
354
+ if (!retryTimeouts()) {
355
+ const timeoutError = new Error(`Request timed out after ${timeoutMs}ms (response body${options.idempotent ? '' : '; the request was applied'})`);
281
356
  timeoutError.isTimeout = true;
282
- throw timeoutError;
357
+ throw fail(timeoutError);
283
358
  }
284
359
  if (await backoff(`response body timed out after ${timeoutMs}ms`, null))
285
360
  continue;
286
- throw bodyErr;
361
+ throw fail(bodyErr);
287
362
  }
288
- if (idempotent && await backoff(`response body failed (${describeFetchError(bodyErr)})`, null))
363
+ if (safeToResend() && await backoff(`response body failed (${describeFetchError(bodyErr)})`, null))
289
364
  continue;
290
- throw bodyErr;
365
+ throw fail(bodyErr);
291
366
  }
292
367
  }
293
368
  const text = await response.text().catch(() => '');
@@ -298,11 +373,27 @@ async function fetchWithRetries(url, init, options, decode) {
298
373
  catch {
299
374
  errorBody = undefined;
300
375
  }
301
- if (isRetryableResponse(response.status, idempotent, errorBody?.error?.type, text)
376
+ const errorType = errorBody?.error?.type;
377
+ const apiError = new ApiStorageClientError(errorBody?.error?.message || response.statusText || `API request failed: ${response.status}`, response.status);
378
+ if (idempotencyKey && errorType === OUTCOME_UNKNOWN) {
379
+ // Stored or not, nobody knows: the API refuses repeats of this key.
380
+ maybeApplied = true;
381
+ throw fail(apiError);
382
+ }
383
+ // A 5xx other than the limiter's / HAProxy's 503 may come after the write was applied.
384
+ if (!options.idempotent && response.status >= 500 && !isRetryableResponse(response.status, false, errorType, text)) {
385
+ maybeApplied = true;
386
+ }
387
+ const resend = safeToResend();
388
+ // 409 for a keyed write: an earlier attempt is still being stored.
389
+ const inProgress = idempotencyKey !== undefined && response.status === 409 && errorType === KEY_IN_PROGRESS;
390
+ if ((inProgress || isRetryableResponse(response.status, resend, errorType, text))
302
391
  && await backoff(`answered ${response.status}`, response.headers.get('retry-after'))) {
303
392
  continue;
304
393
  }
305
- throw new ApiStorageClientError(errorBody?.error?.message || response.statusText || `API request failed: ${response.status}`, response.status);
394
+ if (inProgress)
395
+ maybeApplied = true;
396
+ throw fail(apiError);
306
397
  }
307
398
  catch (err) {
308
399
  // HTTP errors (already decided above) are final.
@@ -310,19 +401,25 @@ async function fetchWithRetries(url, init, options, decode) {
310
401
  throw err;
311
402
  if (err?.isTimeout)
312
403
  throw err;
404
+ if (err?.outcomeUnknown)
405
+ throw err;
313
406
  if (err.name === 'AbortError') {
407
+ maybeApplied = true;
314
408
  const timeoutError = new Error(`Request timed out after ${timeoutMs}ms`);
315
409
  timeoutError.isTimeout = true;
316
- if (retryTimeouts && await backoff(`timed out after ${timeoutMs}ms`, null))
410
+ if (retryTimeouts() && await backoff(`timed out after ${timeoutMs}ms`, null))
317
411
  continue;
318
- throw timeoutError;
412
+ throw fail(timeoutError);
319
413
  }
320
414
  // fetch() failed without a response. A refused/unresolvable
321
415
  // connection never reached the API, so it is safe for every
322
- // method; anything else (reset mid-request) only for idempotent ones.
323
- if ((idempotent || isConnectFailure(err)) && await backoff(`failed (${describeFetchError(err)})`, null))
416
+ // method; anything else (reset mid-request) only when re-sending is safe.
417
+ const connectFailure = isConnectFailure(err);
418
+ if (!connectFailure)
419
+ maybeApplied = true;
420
+ if ((safeToResend() || connectFailure) && await backoff(`failed (${describeFetchError(err)})`, null))
324
421
  continue;
325
- throw err;
422
+ throw fail(err);
326
423
  }
327
424
  finally {
328
425
  clearTimeout(timeoutId);
@@ -391,7 +488,7 @@ class BaseApiClient {
391
488
  headers;
392
489
  // How long one call keeps retrying 429/503/network errors before it gives
393
490
  // up (SCRAPELY_API_RETRY_WINDOW_SECS overrides it). Long enough that a
394
- // throttled run slows down instead of failing, and covers a main redeploy.
491
+ // throttled run slows down instead of failing, and covers an API redeploy.
395
492
  static retryWindowMs = exports.DEFAULT_API_RETRY_WINDOW_MS;
396
493
  // Default timeout for requests (30 seconds)
397
494
  static defaultTimeoutMs = 30000;
@@ -427,6 +524,7 @@ class BaseApiClient {
427
524
  timeoutMs,
428
525
  retryTimeouts: options?.doNotRetryTimeouts !== true,
429
526
  label: `${method} ${path}`,
527
+ idempotencyKey: options?.idempotencyKey,
430
528
  }, decode);
431
529
  }
432
530
  async request(method, path, body, contentType, options) {
@@ -584,7 +682,9 @@ class ApiDatasetClient extends BaseApiClient {
584
682
  // must not store unbilled rows in its default dataset.
585
683
  if (this.id === (0, charging_js_1.defaultDatasetIdFromEnv)() && (0, charging_js_1.refuseDefaultDatasetPush)(parsed.length))
586
684
  return;
587
- await this.request('POST', `/datasets/${this.id}/items`, parsed);
685
+ // One key per push, the same on every attempt: the API stores a
686
+ // re-sent push once, so a push whose connection dropped can be retried.
687
+ await this.request('POST', `/datasets/${this.id}/items`, parsed, undefined, { idempotencyKey: (0, node_crypto_1.randomUUID)() });
588
688
  }
589
689
  }
590
690
  exports.ApiDatasetClient = ApiDatasetClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scrapely",
3
- "version": "1.2.18",
3
+ "version": "1.2.19",
4
4
  "description": "Scrapely SDK for building JavaScript and TypeScript Actors",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,6 +24,7 @@
24
24
  "test:charging": "npx tsx --test tests/charging-pending.test.ts",
25
25
  "test:charge-limit-guard": "npx tsx --test tests/charge-limit-guard.test.ts",
26
26
  "test:storage-retries": "npx tsx --test tests/storage-retries.test.ts",
27
+ "test:push-idempotency": "npx tsx --test tests/push-idempotency.test.ts",
27
28
  "test:stdio-drain": "npx tsx --test tests/stdio-drain.test.ts",
28
29
  "test:charging-client": "npx tsx --test tests/charging-client.test.ts",
29
30
  "test:proxy": "npx tsx --test tests/proxy-configuration.test.ts",