@bowmark/web 1.25.3 → 1.25.5

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/transport.js CHANGED
@@ -103,35 +103,84 @@ async function sendRequest(client, method, path, body, extraHeaders) {
103
103
  if (!client.apiKey)
104
104
  throw new BowmarkError(NO_API_KEY_MESSAGE, { code: "no_api_key" });
105
105
  headers.authorization = `Bearer ${client.apiKey}`;
106
- let response;
107
- try {
108
- response = await client.fetch(`${client.baseUrl}${path}`, {
109
- method,
110
- headers,
111
- ...(body !== undefined ? { body: JSON.stringify(body) } : {}),
112
- signal: client.signal,
113
- });
114
- }
115
- catch (err) {
116
- // A transport failure is not a capability failure, and conflating the two is
117
- // how a caller comes to retry a DNS problem against a site.
118
- throw new BowmarkError(`could not reach ${client.baseUrl}${path}: ${String(err)}`, {
119
- code: "network_error",
120
- });
106
+ for (let attempt = 0;; attempt++) {
107
+ await paceForEdge(client.baseUrl);
108
+ let response;
109
+ try {
110
+ response = await client.fetch(`${client.baseUrl}${path}`, {
111
+ method,
112
+ headers,
113
+ ...(body !== undefined ? { body: JSON.stringify(body) } : {}),
114
+ signal: client.signal,
115
+ });
116
+ }
117
+ catch (err) {
118
+ // A transport failure is not a capability failure, and conflating the two is
119
+ // how a caller comes to retry a DNS problem against a site.
120
+ throw new BowmarkError(`could not reach ${client.baseUrl}${path}: ${String(err)}`, {
121
+ code: "network_error",
122
+ });
123
+ }
124
+ const text = await response.text();
125
+ let payload;
126
+ let parsed = true;
127
+ try {
128
+ payload = text ? JSON.parse(text) : null;
129
+ }
130
+ catch {
131
+ parsed = false;
132
+ }
133
+ // A 429 from the EDGE (Cloudflare's per-IP rate limit on `/v1/*`: 10 requests per
134
+ // 10 seconds) never reached the api, so retrying it is safe and is the only thing
135
+ // that helps. A bare `client()` call is THREE requests (open, call, close), so a
136
+ // page that makes two or three calls trips it on its first click. The api's own
137
+ // 429s carry a `code` (`rate_limited`, a metering refusal) and are NOT retried.
138
+ const edge429 = response.status === 429 &&
139
+ (!parsed || !(payload && typeof payload === "object" && "code" in payload));
140
+ if (edge429 && attempt < EDGE_429_RETRIES) {
141
+ const retryAfter = Number(response.headers?.get?.("retry-after"));
142
+ const wait = Number.isFinite(retryAfter) && retryAfter > 0
143
+ ? Math.min(retryAfter * 1000, EDGE_429_MAX_WAIT_MS)
144
+ : Math.min(2000 * 2 ** attempt, EDGE_429_MAX_WAIT_MS);
145
+ client.onLog?.(`[bowmark] ${path} answered 429, retrying in ${wait}ms`);
146
+ await new Promise((resolve) => setTimeout(resolve, wait));
147
+ continue;
148
+ }
149
+ if (!parsed) {
150
+ // A non-JSON body at this point is a proxy, a captive portal or an outage —
151
+ // never us. Quote a bounded slice of it, because "unexpected token <" with no
152
+ // body is the least actionable error in software.
153
+ throw new BowmarkError(`${client.baseUrl}${path} answered ${response.status} with a non-JSON body: ${text.slice(0, 200)}`, { code: "bad_response", httpStatus: response.status });
154
+ }
155
+ return { status: response.status, payload: payload };
121
156
  }
122
- const text = await response.text();
123
- let payload;
124
- try {
125
- payload = text ? JSON.parse(text) : null;
126
- }
127
- catch {
128
- // A non-JSON body at this point is a proxy, a captive portal or an outage —
129
- // never us. Quote a bounded slice of it, because "unexpected token <" with no
130
- // body is the least actionable error in software.
131
- throw new BowmarkError(`${client.baseUrl}${path} answered ${response.status} with a non-JSON body: ${text.slice(0, 200)}`, { code: "bad_response", httpStatus: response.status });
157
+ }
158
+ /** The edge allows 10 requests per 10 seconds per IP on the hosted api. Stay under it
159
+ * proactively (8 per 10s) instead of finding out by being blocked: a retry costs a
160
+ * 10-second stall and a paginated read is dozens of requests. Hosted api only, so a
161
+ * local or self-hosted api is never slowed. Per process, which is per IP for a server. */
162
+ const EDGE_WINDOW_MS = 10_000;
163
+ const EDGE_BUDGET = 8;
164
+ const recentSends = [];
165
+ async function paceForEdge(baseUrl) {
166
+ if (!/^https:\/\/api\.bowmark\.ai(\/|$)/.test(baseUrl))
167
+ return;
168
+ for (;;) {
169
+ const now = Date.now();
170
+ while (recentSends.length && now - recentSends[0] >= EDGE_WINDOW_MS)
171
+ recentSends.shift();
172
+ if (recentSends.length < EDGE_BUDGET) {
173
+ recentSends.push(now);
174
+ return;
175
+ }
176
+ const wait = EDGE_WINDOW_MS - (now - recentSends[0]) + 25;
177
+ await new Promise((resolve) => setTimeout(resolve, wait));
132
178
  }
133
- return { status: response.status, payload: payload };
134
179
  }
180
+ /** Retries for an edge 429 (see `sendRequest`), and the longest single wait. The edge
181
+ * block lasts 10 seconds, so 2s + 4s + 8s outlasts it. */
182
+ const EDGE_429_RETRIES = 3;
183
+ const EDGE_429_MAX_WAIT_MS = 10_000;
135
184
  /** POST JSON and parse JSON back. `extraHeaders` rides THIS call only — never
136
185
  * captured, never applied to a later call on the same client. */
137
186
  async function postJson(client, path, body, extraHeaders) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bowmark/web",
3
- "version": "1.25.3",
3
+ "version": "1.25.5",
4
4
  "type": "module",
5
5
  "description": "The public client for the Bowmark capability library — real TypeScript for the whole bowmark.* surface, with no Bowmark source on the caller's disk. ZERO runtime dependencies, deliberately and permanently.",
6
6
  "license": "MIT",