@palbase/web 7.2.2 → 7.3.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.
Files changed (38) hide show
  1. package/dist/{analytics-facade-DLfnVVwL.d.ts → analytics-facade-BER0EyYT.d.ts} +8 -3
  2. package/dist/{analytics-facade-jyXv7Cu4.d.cts → analytics-facade-CQXCQSoF.d.cts} +8 -3
  3. package/dist/{chunk-IREKQSHT.js → chunk-YBURINUN.js} +60 -14
  4. package/dist/chunk-YBURINUN.js.map +1 -0
  5. package/dist/gen/cli.cjs +6 -12
  6. package/dist/gen/cli.cjs.map +1 -1
  7. package/dist/gen/cli.js +6 -12
  8. package/dist/gen/cli.js.map +1 -1
  9. package/dist/index.cjs +51 -4
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +3 -3
  12. package/dist/index.d.ts +3 -3
  13. package/dist/index.js +1 -1
  14. package/dist/internal.cjs +59 -13
  15. package/dist/internal.cjs.map +1 -1
  16. package/dist/internal.d.cts +3 -3
  17. package/dist/internal.d.ts +3 -3
  18. package/dist/internal.js +1 -1
  19. package/dist/next/client.cjs +67 -21
  20. package/dist/next/client.cjs.map +1 -1
  21. package/dist/next/client.js +5 -3
  22. package/dist/next/client.js.map +1 -1
  23. package/dist/next/index.cjs +73 -27
  24. package/dist/next/index.cjs.map +1 -1
  25. package/dist/next/index.d.cts +2 -22
  26. package/dist/next/index.d.ts +2 -22
  27. package/dist/next/index.js +5 -4
  28. package/dist/next/index.js.map +1 -1
  29. package/dist/{pb-DdzpEkPY.d.ts → pb-BRlmBIAm.d.ts} +1 -1
  30. package/dist/{pb-CfYQGEn0.d.cts → pb-WAQvwCfM.d.cts} +1 -1
  31. package/dist/react/index.cjs +50 -3
  32. package/dist/react/index.cjs.map +1 -1
  33. package/dist/react/index.d.cts +1 -1
  34. package/dist/react/index.d.ts +1 -1
  35. package/dist/react/index.js +1 -1
  36. package/package.json +13 -13
  37. package/LICENSE +0 -21
  38. package/dist/chunk-IREKQSHT.js.map +0 -1
@@ -1,7 +1,7 @@
1
- import { P as PalbeConfig } from './analytics-facade-jyXv7Cu4.cjs';
2
- export { d as PalbeRuntime, e as buildRuntime } from './analytics-facade-jyXv7Cu4.cjs';
1
+ import { P as PalbeConfig } from './analytics-facade-CQXCQSoF.cjs';
2
+ export { d as PalbeRuntime, e as buildRuntime } from './analytics-facade-CQXCQSoF.cjs';
3
3
  import { B as BackendError } from './errors-fDoNdTrJ.cjs';
4
- export { P as PB, c as createBoundClient } from './pb-CfYQGEn0.cjs';
4
+ export { P as PB, c as createBoundClient } from './pb-WAQvwCfM.cjs';
5
5
  import './storage-BPaeSG8K.cjs';
6
6
  import './pooled-flags-4GtDtsiu.js';
7
7
 
@@ -1,7 +1,7 @@
1
- import { P as PalbeConfig } from './analytics-facade-DLfnVVwL.js';
2
- export { d as PalbeRuntime, e as buildRuntime } from './analytics-facade-DLfnVVwL.js';
1
+ import { P as PalbeConfig } from './analytics-facade-BER0EyYT.js';
2
+ export { d as PalbeRuntime, e as buildRuntime } from './analytics-facade-BER0EyYT.js';
3
3
  import { B as BackendError } from './errors-fDoNdTrJ.js';
4
- export { P as PB, c as createBoundClient } from './pb-DdzpEkPY.js';
4
+ export { P as PB, c as createBoundClient } from './pb-BRlmBIAm.js';
5
5
  import './storage-BPaeSG8K.js';
6
6
  import './pooled-flags-4GtDtsiu.js';
7
7
 
package/dist/internal.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  createBoundClient,
7
7
  getRuntime,
8
8
  onConfigured
9
- } from "./chunk-IREKQSHT.js";
9
+ } from "./chunk-YBURINUN.js";
10
10
  import "./chunk-ACCJV6FV.js";
11
11
  import "./chunk-CFDU23TB.js";
12
12
  import "./chunk-PZ5AY32C.js";
@@ -35,6 +35,13 @@ __export(client_exports, {
35
35
  });
36
36
  module.exports = __toCommonJS(client_exports);
37
37
 
38
+ // src/api-key.ts
39
+ var PUBLISHABLE_API_KEY_RE = /^pb_([a-z0-9]{4,24})_c[A-Za-z0-9]{20}$/;
40
+ function environmentRefFromPublishableApiKey(apiKey) {
41
+ const match = PUBLISHABLE_API_KEY_RE.exec(apiKey);
42
+ return match ? match[1] ?? "" : "";
43
+ }
44
+
38
45
  // ../core/dist/index.js
39
46
  var CACHE_TTL_MS = 5 * 60 * 1e3;
40
47
  var PalbaseError = class extends Error {
@@ -451,6 +458,47 @@ function redactUrl(rawUrl) {
451
458
  return path.split("/").map((seg2) => isSensitiveSegment(seg2) ? ":id" : seg2).join("/");
452
459
  }
453
460
 
461
+ // src/pow.ts
462
+ var POW_CHALLENGE_ID_HEADER = "X-PoW-Challenge-ID";
463
+ var POW_NONCE_HEADER = "X-PoW-Nonce";
464
+ function asPowChallenge(details) {
465
+ if (typeof details !== "object" || details === null) return null;
466
+ const env = details;
467
+ if (env.error !== "pow_required") return null;
468
+ const c = env.challenge;
469
+ if (typeof c !== "object" || c === null) return null;
470
+ const { id, prefix, difficulty } = c;
471
+ if (typeof id !== "string" || typeof prefix !== "string") return null;
472
+ if (typeof difficulty !== "number" || !Number.isInteger(difficulty) || difficulty < 0) return null;
473
+ return { id, prefix, difficulty };
474
+ }
475
+ var encoder = new TextEncoder();
476
+ function leadingZeroBits(hash) {
477
+ let bits = 0;
478
+ for (const byte of hash) {
479
+ if (byte === 0) {
480
+ bits += 8;
481
+ continue;
482
+ }
483
+ return bits + Math.clz32(byte) - 24;
484
+ }
485
+ return bits;
486
+ }
487
+ async function solvePowChallenge(challenge, maxIterations = 1 << 24) {
488
+ for (let nonce = 0; nonce < maxIterations; nonce++) {
489
+ const digest = await crypto.subtle.digest("SHA-256", encoder.encode(challenge.prefix + nonce));
490
+ if (leadingZeroBits(new Uint8Array(digest)) >= challenge.difficulty) {
491
+ return {
492
+ [POW_CHALLENGE_ID_HEADER]: challenge.id,
493
+ [POW_NONCE_HEADER]: String(nonce)
494
+ };
495
+ }
496
+ }
497
+ throw new Error(
498
+ `proof-of-work: no nonce found for difficulty ${challenge.difficulty} within ${maxIterations} attempts`
499
+ );
500
+ }
501
+
454
502
  // src/request.ts
455
503
  var MUTATING = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH", "DELETE"]);
456
504
  var PERF_EXCLUDED_PREFIX = "/v1/analytics/";
@@ -474,11 +522,11 @@ async function palbeRequest(rt, method, path, spec = {}) {
474
522
  if (MUTATING.has(method) && !callerHasKey) {
475
523
  headers["Idempotency-Key"] = crypto.randomUUID();
476
524
  }
477
- const attempt = async () => {
525
+ const attempt = async (extra) => {
478
526
  try {
479
527
  return await rt.http.request(method, path, {
480
528
  body: spec.body,
481
- headers,
529
+ headers: extra ? { ...headers, ...extra } : headers,
482
530
  signal: spec.signal
483
531
  });
484
532
  } catch (e) {
@@ -513,6 +561,12 @@ async function palbeRequest(rt, method, path, spec = {}) {
513
561
  }
514
562
  res = await attempt();
515
563
  }
564
+ if (res.error?.status === 403 && res.error.code === "pow_required") {
565
+ const challenge = asPowChallenge(res.error.details);
566
+ if (challenge) {
567
+ res = await attempt(await solvePowChallenge(challenge));
568
+ }
569
+ }
516
570
  } catch (e) {
517
571
  if (!isAbort(e)) record(asPalbaseError(e)?.status ?? 0);
518
572
  throw e;
@@ -1058,7 +1112,7 @@ var AnalyticsState = class {
1058
1112
  idKey;
1059
1113
  optOutKey;
1060
1114
  constructor(rt) {
1061
- const ref = rt.config.environmentRef;
1115
+ const ref = rt.projectRef;
1062
1116
  this.idKey = ref ? `palbe.analytics.id.${ref}` : "palbe.analytics.id";
1063
1117
  this.optOutKey = ref ? `palbe.analytics.optout.${ref}` : "palbe.analytics.optout";
1064
1118
  rt.auth.onAuthEvent((event) => {
@@ -1272,13 +1326,6 @@ var PalbeAnalytics = class {
1272
1326
  }
1273
1327
  };
1274
1328
 
1275
- // src/api-key.ts
1276
- var PUBLISHABLE_API_KEY_RE = /^pb_([a-z0-9]{4,24})_c[A-Za-z0-9]{20}$/;
1277
- function environmentRefFromPublishableApiKey(apiKey) {
1278
- const match = PUBLISHABLE_API_KEY_RE.exec(apiKey);
1279
- return match ? match[1] ?? "" : "";
1280
- }
1281
-
1282
1329
  // src/auth-wire.ts
1283
1330
  function asWireAuthResult(raw) {
1284
1331
  if (typeof raw !== "object" || raw === null) return null;
@@ -2699,7 +2746,7 @@ var PalbeFlags = class {
2699
2746
  constructor(rt) {
2700
2747
  this.transport = new FlagsClient(rt.http);
2701
2748
  const browser = typeof document !== "undefined";
2702
- const ref = rt.config.environmentRef;
2749
+ const ref = rt.projectRef;
2703
2750
  const options = {
2704
2751
  auth: palbeAuthAdapter(rt.auth),
2705
2752
  ...ref ? { storageKey: `palbe.flags.${ref}` } : {},
@@ -9098,15 +9145,13 @@ function defaultSessionStorage(key) {
9098
9145
  }
9099
9146
 
9100
9147
  // src/version.ts
9101
- var VERSION = "7.2.2";
9148
+ var VERSION = "7.3.0";
9102
9149
 
9103
9150
  // src/runtime.ts
9104
9151
  function buildRuntime(config) {
9105
- const keyEnvironmentRef = environmentRefFromPublishableApiKey(config.apiKey);
9106
- if (keyEnvironmentRef === "") {
9107
- throw new Error("PalbeConfig.apiKey does not contain a valid publishable Environment identity");
9108
- } else if (config.environmentRef !== keyEnvironmentRef) {
9109
- throw new Error("PalbeConfig.environmentRef must match the Environment identity in apiKey");
9152
+ const projectRef = environmentRefFromPublishableApiKey(config.apiKey);
9153
+ if (projectRef === "") {
9154
+ throw new Error("PalbeConfig.apiKey does not contain a valid publishable project identity");
9110
9155
  }
9111
9156
  let runtimeURL;
9112
9157
  try {
@@ -9114,8 +9159,8 @@ function buildRuntime(config) {
9114
9159
  } catch {
9115
9160
  throw new Error("PalbeConfig.url must be a valid URL");
9116
9161
  }
9117
- if (runtimeURL.hostname.endsWith(".palbase.studio") && runtimeURL.hostname.split(".")[0] !== config.environmentRef) {
9118
- throw new Error("PalbeConfig.url must match environmentRef for palbase.studio");
9162
+ if (runtimeURL.hostname.endsWith(".palbase.studio") && runtimeURL.hostname.split(".")[0] !== projectRef) {
9163
+ throw new Error("PalbeConfig.url must match the project identity in apiKey for palbase.studio");
9119
9164
  }
9120
9165
  const http = new HttpClient(config.apiKey, {
9121
9166
  url: config.url,
@@ -9125,7 +9170,7 @@ function buildRuntime(config) {
9125
9170
  const tokenManager = new TokenManager();
9126
9171
  http.tokenManager = tokenManager;
9127
9172
  const authClient = new AuthClient(http, tokenManager);
9128
- const ref = config.environmentRef;
9173
+ const ref = projectRef;
9129
9174
  const storage = config.storage ?? defaultSessionStorage(ref ? `palbe.session.${ref}` : void 0);
9130
9175
  const persisted = storage.load();
9131
9176
  if (persisted) {
@@ -9170,6 +9215,7 @@ function buildRuntime(config) {
9170
9215
  let perf;
9171
9216
  const rt = {
9172
9217
  config,
9218
+ projectRef,
9173
9219
  http,
9174
9220
  tokenManager,
9175
9221
  authClient,
@@ -9412,7 +9458,7 @@ function setupPalbeNext() {
9412
9458
  const config = getRuntime().config;
9413
9459
  __configure({
9414
9460
  ...config,
9415
- storage: cookieSessionStorage(config.environmentRef)
9461
+ storage: cookieSessionStorage(environmentRefFromPublishableApiKey(config.apiKey))
9416
9462
  });
9417
9463
  }
9418
9464
  // Annotate the CommonJS export names for ESM import in node: