@shop-on/customer-runtime 0.6.0 → 0.8.0

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.
@@ -1,4 +1,4 @@
1
- import { relayHeaders } from "./relay.js";
1
+ import { relayHeaders, shardBuyerHeaders } from "./relay.js";
2
2
  import { traceHeaders } from "./trace.js";
3
3
  //#region src/internal-api.ts
4
4
  function apiUrl() {
@@ -42,6 +42,7 @@ async function internalPost(path, body, { signal } = {}) {
42
42
  "Content-Type": "application/json",
43
43
  "X-Internal-Auth": secret,
44
44
  ...traceHeaders(),
45
+ ...shardBuyerHeaders(),
45
46
  ...relayHeaders()
46
47
  },
47
48
  body: JSON.stringify(body),
@@ -62,6 +63,7 @@ async function internalDelete(path) {
62
63
  headers: {
63
64
  "X-Internal-Auth": secret,
64
65
  ...traceHeaders(),
66
+ ...shardBuyerHeaders(),
65
67
  ...relayHeaders()
66
68
  }
67
69
  });
package/dist/lease.d.ts CHANGED
@@ -11,7 +11,8 @@ export interface Lease<T> {
11
11
  /**
12
12
  * One `work` per key across concurrent requests. The first caller takes the
13
13
  * lease and runs `work`; others poll on their own timer and take the outcome
14
- * it settles with (value or error). If the holder never settles, the next
14
+ * it settles with (value or error, but not a 429: a waiter then becomes the
15
+ * next holder). If the holder never settles, the next
15
16
  * waiter takes over once the lease expires. Calls within one request share
16
17
  * that request's promise directly.
17
18
  *
package/dist/lease.js CHANGED
@@ -1,10 +1,12 @@
1
1
  import { requestSlot } from "./request-scope.js";
2
2
  //#region src/lease.ts
3
+ const isThrottled = (error) => error instanceof Error && error.response?.status === 429;
3
4
  const isLive = (lease) => lease !== void 0 && lease.until > Date.now();
4
5
  /**
5
6
  * One `work` per key across concurrent requests. The first caller takes the
6
7
  * lease and runs `work`; others poll on their own timer and take the outcome
7
- * it settles with (value or error). If the holder never settles, the next
8
+ * it settles with (value or error, but not a 429: a waiter then becomes the
9
+ * next holder). If the holder never settles, the next
8
10
  * waiter takes over once the lease expires. Calls within one request share
9
11
  * that request's promise directly.
10
12
  *
@@ -70,7 +72,7 @@ function createLease({ leaseMs = 3e3, pollMs = 10 } = {}) {
70
72
  const outcome = outcomes.get(key);
71
73
  if (outcome && outcome.settledAt >= waitStart) {
72
74
  if (outcome.ok) return outcome.value;
73
- throw outcome.error;
75
+ if (!isThrottled(outcome.error)) throw outcome.error;
74
76
  }
75
77
  }
76
78
  return await take(key, work, slot);
package/dist/relay.d.ts CHANGED
@@ -11,4 +11,11 @@ export declare function relayHeaders(): Record<string, string>;
11
11
  * a request, or without a valid IP: with no secret Rails would ignore the IP.
12
12
  */
13
13
  export declare function staffRelayHeaders(): Record<string, string>;
14
+ export declare const SHARD_BUYER_TOKEN_HEADER = "x-shard-buyer";
15
+ /**
16
+ * The router's buyer token, copied verbatim from the request this call runs
17
+ * under. Empty outside a request or when the router minted none (no
18
+ * `CLIENT_IP_SOURCE`, no secret, or not behind a shard at all).
19
+ */
20
+ export declare function shardBuyerHeaders(): Record<string, string>;
14
21
  //#endregion
package/dist/relay.js CHANGED
@@ -34,5 +34,15 @@ function staffRelayHeaders() {
34
34
  "X-Relay-Auth": secret
35
35
  };
36
36
  }
37
+ const SHARD_BUYER_TOKEN_HEADER = "x-shard-buyer";
38
+ /**
39
+ * The router's buyer token, copied verbatim from the request this call runs
40
+ * under. Empty outside a request or when the router minted none (no
41
+ * `CLIENT_IP_SOURCE`, no secret, or not behind a shard at all).
42
+ */
43
+ function shardBuyerHeaders() {
44
+ const token = currentRequest()?.headers.get(SHARD_BUYER_TOKEN_HEADER);
45
+ return token ? { "X-Shard-Buyer": token } : {};
46
+ }
37
47
  //#endregion
38
- export { relayHeaders, staffRelayHeaders };
48
+ export { SHARD_BUYER_TOKEN_HEADER, relayHeaders, shardBuyerHeaders, staffRelayHeaders };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shop-on/customer-runtime",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "license": "SEE LICENSE IN dist/LICENSE.md",
5
5
  "files": [
6
6
  "dist"