@palbase/web 7.3.0 → 7.3.1

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.
@@ -6,8 +6,8 @@ import {
6
6
  import {
7
7
  __configure,
8
8
  getRuntime
9
- } from "../chunk-YBURINUN.js";
10
- import "../chunk-ACCJV6FV.js";
9
+ } from "../chunk-RRHJV6YD.js";
10
+ import "../chunk-53TN7KYR.js";
11
11
  import {
12
12
  environmentRefFromPublishableApiKey
13
13
  } from "../chunk-CFDU23TB.js";
@@ -166,6 +166,45 @@ var PalbaseError = class extends Error {
166
166
  this.details = details;
167
167
  }
168
168
  };
169
+ var POW_CHALLENGE_ID_HEADER = "X-PoW-Challenge-ID";
170
+ var POW_NONCE_HEADER = "X-PoW-Nonce";
171
+ function asPowChallenge(details) {
172
+ if (typeof details !== "object" || details === null) return null;
173
+ const env = details;
174
+ if (env.error !== "pow_required") return null;
175
+ const c = env.challenge;
176
+ if (typeof c !== "object" || c === null) return null;
177
+ const { id, prefix, difficulty } = c;
178
+ if (typeof id !== "string" || typeof prefix !== "string") return null;
179
+ if (typeof difficulty !== "number" || !Number.isInteger(difficulty) || difficulty < 0) return null;
180
+ return { id, prefix, difficulty };
181
+ }
182
+ var encoder = new TextEncoder();
183
+ function leadingZeroBits(hash) {
184
+ let bits = 0;
185
+ for (const byte of hash) {
186
+ if (byte === 0) {
187
+ bits += 8;
188
+ continue;
189
+ }
190
+ return bits + Math.clz32(byte) - 24;
191
+ }
192
+ return bits;
193
+ }
194
+ async function solvePowChallenge(challenge, maxIterations = 1 << 24) {
195
+ for (let nonce = 0; nonce < maxIterations; nonce++) {
196
+ const digest = await crypto.subtle.digest("SHA-256", encoder.encode(challenge.prefix + nonce));
197
+ if (leadingZeroBits(new Uint8Array(digest)) >= challenge.difficulty) {
198
+ return {
199
+ [POW_CHALLENGE_ID_HEADER]: challenge.id,
200
+ [POW_NONCE_HEADER]: String(nonce)
201
+ };
202
+ }
203
+ }
204
+ throw new Error(
205
+ `proof-of-work: no nonce found for difficulty ${challenge.difficulty} within ${maxIterations} attempts`
206
+ );
207
+ }
169
208
  function detectPlatform() {
170
209
  if (typeof Deno !== "undefined") {
171
210
  return "deno";
@@ -302,9 +341,9 @@ var HttpClient = class _HttpClient {
302
341
  }
303
342
  return headers;
304
343
  }
305
- async executeWithRetry(method, path, options, attempt) {
344
+ async executeWithRetry(method, path, options, attempt, earned) {
306
345
  const url = `${this.getBaseUrl()}${path}`;
307
- const headers = this.buildHeaders(options);
346
+ const headers = { ...this.buildHeaders(options), ...earned };
308
347
  for (const interceptor of this.interceptors) {
309
348
  await interceptor({ headers, method, path });
310
349
  }
@@ -323,7 +362,7 @@ var HttpClient = class _HttpClient {
323
362
  if (attempt < MAX_RETRIES - 1) {
324
363
  const backoff = INITIAL_BACKOFF_MS * 2 ** attempt;
325
364
  await this.delay(backoff);
326
- return this.executeWithRetry(method, path, options, attempt + 1);
365
+ return this.executeWithRetry(method, path, options, attempt + 1, earned);
327
366
  }
328
367
  throw new PalbaseError(
329
368
  "network_error",
@@ -337,7 +376,7 @@ var HttpClient = class _HttpClient {
337
376
  const parsed = retryAfter ? Number.parseInt(retryAfter, 10) : Number.NaN;
338
377
  const delayMs = Number.isNaN(parsed) ? INITIAL_BACKOFF_MS * 2 ** attempt : Math.min(parsed * 1e3, MAX_RETRY_DELAY_MS);
339
378
  await this.delay(delayMs);
340
- return this.executeWithRetry(method, path, options, attempt + 1);
379
+ return this.executeWithRetry(method, path, options, attempt + 1, earned);
341
380
  }
342
381
  }
343
382
  let data = null;
@@ -351,6 +390,18 @@ var HttpClient = class _HttpClient {
351
390
  errorBody = body;
352
391
  }
353
392
  }
393
+ if (response.status === 403 && !earned) {
394
+ const challenge = asPowChallenge(errorBody);
395
+ if (challenge) {
396
+ return this.executeWithRetry(
397
+ method,
398
+ path,
399
+ options,
400
+ attempt,
401
+ await solvePowChallenge(challenge)
402
+ );
403
+ }
404
+ }
354
405
  if (!response.ok) {
355
406
  return {
356
407
  data: null,
@@ -568,47 +619,6 @@ function redactUrl(rawUrl) {
568
619
  return path.split("/").map((seg2) => isSensitiveSegment(seg2) ? ":id" : seg2).join("/");
569
620
  }
570
621
 
571
- // src/pow.ts
572
- var POW_CHALLENGE_ID_HEADER = "X-PoW-Challenge-ID";
573
- var POW_NONCE_HEADER = "X-PoW-Nonce";
574
- function asPowChallenge(details) {
575
- if (typeof details !== "object" || details === null) return null;
576
- const env = details;
577
- if (env.error !== "pow_required") return null;
578
- const c = env.challenge;
579
- if (typeof c !== "object" || c === null) return null;
580
- const { id, prefix, difficulty } = c;
581
- if (typeof id !== "string" || typeof prefix !== "string") return null;
582
- if (typeof difficulty !== "number" || !Number.isInteger(difficulty) || difficulty < 0) return null;
583
- return { id, prefix, difficulty };
584
- }
585
- var encoder = new TextEncoder();
586
- function leadingZeroBits(hash) {
587
- let bits = 0;
588
- for (const byte of hash) {
589
- if (byte === 0) {
590
- bits += 8;
591
- continue;
592
- }
593
- return bits + Math.clz32(byte) - 24;
594
- }
595
- return bits;
596
- }
597
- async function solvePowChallenge(challenge, maxIterations = 1 << 24) {
598
- for (let nonce = 0; nonce < maxIterations; nonce++) {
599
- const digest = await crypto.subtle.digest("SHA-256", encoder.encode(challenge.prefix + nonce));
600
- if (leadingZeroBits(new Uint8Array(digest)) >= challenge.difficulty) {
601
- return {
602
- [POW_CHALLENGE_ID_HEADER]: challenge.id,
603
- [POW_NONCE_HEADER]: String(nonce)
604
- };
605
- }
606
- }
607
- throw new Error(
608
- `proof-of-work: no nonce found for difficulty ${challenge.difficulty} within ${maxIterations} attempts`
609
- );
610
- }
611
-
612
622
  // src/request.ts
613
623
  var MUTATING = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH", "DELETE"]);
614
624
  var PERF_EXCLUDED_PREFIX = "/v1/analytics/";
@@ -9354,7 +9364,7 @@ function defaultSessionStorage(key) {
9354
9364
  }
9355
9365
 
9356
9366
  // src/version.ts
9357
- var VERSION = "7.3.0";
9367
+ var VERSION = "7.3.1";
9358
9368
 
9359
9369
  // src/runtime.ts
9360
9370
  function buildRuntime(config) {