shipflow 0.3.1 → 0.3.2

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.
@@ -8,9 +8,14 @@
8
8
  * request-bound contexts, and Retry-After is a contract — don't retry early)
9
9
  * - otherwise → full-jitter exponential backoff
10
10
  *
11
- * The inter-attempt sleep uses `node:timers/promises` with an AbortSignal so a
12
- * caller cancellation cleans up the timer (no zombie retries, no leaked `abort`
13
- * listeners). Never hand-roll `new Promise(r => setTimeout(r, ms))` here.
11
+ * The inter-attempt sleep uses a Web-standard abortable `sleep` helper (defined
12
+ * below) rather than `node:timers/promises`, so ShipFlow stays runtime-agnostic:
13
+ * `node:timers/promises` requires `nodejs_compat` on Cloudflare Workers and fails
14
+ * at module load on other edge runtimes — even for callers who never retry. The
15
+ * helper is built deliberately on globals available on Node 18+, Deno, Bun, and
16
+ * edge/workers (setTimeout/clearTimeout, DOMException, AbortSignal), and removes
17
+ * its `abort` listener on normal resolution so a caller's signal never
18
+ * accumulates leaked listeners across attempts.
14
19
  */
15
20
  export interface RetryOptions {
16
21
  /** Max number of retries after the first attempt. */
@@ -1 +1 @@
1
- {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/core/retry.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;GAaG;AAIH,MAAM,WAAW,YAAY;IAC3B,qDAAqD;IACrD,OAAO,EAAE,MAAM,CAAC;IAChB,4DAA4D;IAC5D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kDAAkD;IAClD,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;IACzC,0EAA0E;IAC1E,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,CAAC;IACtD,sFAAsF;IACtF,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,wBAAsB,SAAS,CAAC,CAAC,EAC/B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,YAAY,GACpB,OAAO,CAAC,CAAC,CAAC,CAuCZ;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GACrC,MAAM,GAAG,SAAS,CAUpB"}
1
+ {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/core/retry.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;;;GAkBG;AA0BH,MAAM,WAAW,YAAY;IAC3B,qDAAqD;IACrD,OAAO,EAAE,MAAM,CAAC;IAChB,4DAA4D;IAC5D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kDAAkD;IAClD,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;IACzC,0EAA0E;IAC1E,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GAAG,SAAS,CAAC;IACtD,sFAAsF;IACtF,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,wBAAsB,SAAS,CAAC,CAAC,EAC/B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,EAAE,YAAY,GACpB,OAAO,CAAC,CAAC,CAAC,CAuCZ;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GACrC,MAAM,GAAG,SAAS,CAUpB"}
@@ -801,7 +801,23 @@ class BaseCarrierAdapter {
801
801
  }
802
802
 
803
803
  // src/core/retry.ts
804
- import { setTimeout as sleep } from "node:timers/promises";
804
+ function sleep(ms, signal) {
805
+ return new Promise((resolve, reject) => {
806
+ if (signal?.aborted) {
807
+ reject(new DOMException("Aborted", "AbortError"));
808
+ return;
809
+ }
810
+ const onAbort = () => {
811
+ clearTimeout(timer);
812
+ reject(new DOMException("Aborted", "AbortError"));
813
+ };
814
+ const timer = setTimeout(() => {
815
+ signal?.removeEventListener("abort", onAbort);
816
+ resolve();
817
+ }, ms);
818
+ signal?.addEventListener("abort", onAbort, { once: true });
819
+ });
820
+ }
805
821
  async function withRetry(fn, options) {
806
822
  const {
807
823
  retries,
@@ -828,7 +844,7 @@ async function withRetry(fn, options) {
828
844
  delay = Math.random() * Math.min(maxMs, baseMs * 2 ** attempt);
829
845
  }
830
846
  try {
831
- await sleep(delay, undefined, { signal });
847
+ await sleep(delay, signal);
832
848
  } catch {
833
849
  throw error;
834
850
  }
@@ -935,7 +951,7 @@ class HttpClient {
935
951
  ...this.config.headers,
936
952
  ...headers
937
953
  },
938
- body: body ? JSON.stringify(body) : undefined,
954
+ body: body !== undefined ? JSON.stringify(body) : undefined,
939
955
  signal
940
956
  });
941
957
  let json;
@@ -1023,9 +1039,19 @@ class HttpClient {
1023
1039
  raw: json
1024
1040
  });
1025
1041
  }
1042
+ static MAX_ERROR_TEXT_LENGTH = 500;
1043
+ sanitizeErrorText(text) {
1044
+ const stripped = text.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
1045
+ if (stripped.length > HttpClient.MAX_ERROR_TEXT_LENGTH) {
1046
+ return `${stripped.slice(0, HttpClient.MAX_ERROR_TEXT_LENGTH)}... [truncated]`;
1047
+ }
1048
+ return stripped;
1049
+ }
1026
1050
  extractErrorMessage(json) {
1027
- if (typeof json === "string")
1028
- return json.trim() || undefined;
1051
+ if (typeof json === "string") {
1052
+ const trimmed = json.trim();
1053
+ return trimmed ? this.sanitizeErrorText(trimmed) : undefined;
1054
+ }
1029
1055
  if (!json || typeof json !== "object")
1030
1056
  return;
1031
1057
  const obj = json;
@@ -1064,4 +1090,4 @@ class HttpClient {
1064
1090
 
1065
1091
  export { ShipFlowError, NetworkError, APIError, RateLimitError, ValidationError, AuthenticationError, WebhookVerificationError, UnsupportedOperationError, AddressSchema, WeightSchema, DimensionsSchema, ParcelSchema, CODDetailsSchema, DeclaredValueSchema, CreateShipmentInputSchema, PickupRequestSchema, WebhookConfigSchema, validateCreateShipmentInput, validatePickupRequest, BaseCarrierAdapter, HttpClient };
1066
1092
 
1067
- //# debugId=B88EAD18635FEEDC64756E2164756E21
1093
+ //# debugId=2E558E460F11A10664756E2164756E21