@palbase/web 7.3.4 → 7.3.6

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/index.d.cts CHANGED
@@ -206,6 +206,6 @@ interface MlsGlue {
206
206
  */
207
207
  declare function initMls(): Promise<MlsGlue>;
208
208
 
209
- declare const VERSION = "7.3.4";
209
+ declare const VERSION = "7.3.6";
210
210
 
211
211
  export { VERSION, initMls };
package/dist/index.d.ts CHANGED
@@ -206,6 +206,6 @@ interface MlsGlue {
206
206
  */
207
207
  declare function initMls(): Promise<MlsGlue>;
208
208
 
209
- declare const VERSION = "7.3.4";
209
+ declare const VERSION = "7.3.6";
210
210
 
211
211
  export { VERSION, initMls };
package/dist/index.js CHANGED
@@ -11,11 +11,11 @@ import {
11
11
  localStorageSessionStorage,
12
12
  memorySessionStorage,
13
13
  pb
14
- } from "./chunk-PYJ3CMA2.js";
14
+ } from "./chunk-SQWG3PTU.js";
15
15
  import {
16
16
  BackendError,
17
17
  isBackendError
18
- } from "./chunk-RAP37ILS.js";
18
+ } from "./chunk-6DGCUHSO.js";
19
19
  import "./chunk-CFDU23TB.js";
20
20
  import "./chunk-PZ5AY32C.js";
21
21
  export {
package/dist/internal.cjs CHANGED
@@ -68,6 +68,28 @@ function asPowChallenge(details) {
68
68
  return { id, prefix, difficulty };
69
69
  }
70
70
  var encoder = new TextEncoder();
71
+ var hasher = null;
72
+ async function digester() {
73
+ if (hasher) return hasher;
74
+ const runtime = globalThis;
75
+ const nodeish = runtime.process?.versions?.node !== void 0 || runtime.process?.versions?.bun !== void 0;
76
+ if (nodeish) {
77
+ try {
78
+ const mod = await import(
79
+ /* @vite-ignore */
80
+ `${"node:"}crypto`
81
+ );
82
+ if (typeof mod.createHash === "function") {
83
+ const createHash = mod.createHash;
84
+ hasher = (input) => new Uint8Array(createHash("sha256").update(input).digest());
85
+ return hasher;
86
+ }
87
+ } catch {
88
+ }
89
+ }
90
+ hasher = async (input) => new Uint8Array(await crypto.subtle.digest("SHA-256", encoder.encode(input)));
91
+ return hasher;
92
+ }
71
93
  var now = () => typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
72
94
  function leadingZeroBits(hash) {
73
95
  let bits = 0;
@@ -84,14 +106,19 @@ var MAX_POW_DIFFICULTY = 24;
84
106
  function powBudget(difficulty) {
85
107
  return 8 * 2 ** difficulty;
86
108
  }
87
- var POW_DEADLINE_MS = 12e4;
109
+ var POW_TIME_BUDGET_MS = 12e4;
110
+ var CALIBRATION_WARMUP = 1024;
111
+ var CALIBRATION_END = 9216;
88
112
  async function solvePowChallenge(challenge, maxIterations = powBudget(challenge.difficulty), signal) {
89
113
  if (challenge.difficulty > MAX_POW_DIFFICULTY) {
90
114
  throw new Error(
91
115
  `proof-of-work: refusing difficulty ${challenge.difficulty}; this client attempts at most ${MAX_POW_DIFFICULTY}, which is the highest a Palbase stack issues`
92
116
  );
93
117
  }
94
- const deadline = now() + POW_DEADLINE_MS;
118
+ const digest = await digester();
119
+ let warmedAt = 0;
120
+ let calibrated = false;
121
+ let deadline = Number.POSITIVE_INFINITY;
95
122
  for (let nonce = 0; nonce < maxIterations; nonce++) {
96
123
  if ((nonce & 1023) === 0) {
97
124
  if (signal?.aborted) {
@@ -99,12 +126,27 @@ async function solvePowChallenge(challenge, maxIterations = powBudget(challenge.
99
126
  }
100
127
  if (now() > deadline) {
101
128
  throw new Error(
102
- `proof-of-work: gave up after ${POW_DEADLINE_MS / 1e3}s at difficulty ${challenge.difficulty} (${nonce} attempts)`
129
+ `proof-of-work: gave up on difficulty ${challenge.difficulty} after ${POW_TIME_BUDGET_MS / 1e3}s and ${nonce.toLocaleString()} attempts \u2014 the tail this run drew is longer than the allowance`
103
130
  );
104
131
  }
105
132
  }
106
- const digest = await crypto.subtle.digest("SHA-256", encoder.encode(challenge.prefix + nonce));
107
- if (leadingZeroBits(new Uint8Array(digest)) >= challenge.difficulty) {
133
+ if (nonce === CALIBRATION_WARMUP) {
134
+ warmedAt = now();
135
+ }
136
+ if (!calibrated && nonce === CALIBRATION_END) {
137
+ calibrated = true;
138
+ const elapsed = Math.max(now() - warmedAt, 1e-3);
139
+ const rate = (CALIBRATION_END - CALIBRATION_WARMUP) / (elapsed / 1e3);
140
+ const expectedMs = 2 ** challenge.difficulty / rate * 1e3;
141
+ if (expectedMs > POW_TIME_BUDGET_MS) {
142
+ throw new Error(
143
+ `proof-of-work: difficulty ${challenge.difficulty} needs about ${Math.round(expectedMs / 1e3)}s here (${Math.round(rate).toLocaleString()} digests/s) and this client allows ${POW_TIME_BUDGET_MS / 1e3}s; refusing before spending the time rather than after`
144
+ );
145
+ }
146
+ deadline = now() + (POW_TIME_BUDGET_MS - (now() - warmedAt));
147
+ }
148
+ const hash = await digest(challenge.prefix + nonce);
149
+ if (leadingZeroBits(hash) >= challenge.difficulty) {
108
150
  return {
109
151
  [POW_CHALLENGE_ID_HEADER]: challenge.id,
110
152
  [POW_NONCE_HEADER]: String(nonce)
@@ -9377,7 +9419,7 @@ function defaultSessionStorage(key) {
9377
9419
  }
9378
9420
 
9379
9421
  // src/version.ts
9380
- var VERSION = "7.3.4";
9422
+ var VERSION = "7.3.6";
9381
9423
 
9382
9424
  // src/runtime.ts
9383
9425
  function buildRuntime(config) {
@@ -9385,15 +9427,11 @@ function buildRuntime(config) {
9385
9427
  if (projectRef === "") {
9386
9428
  throw new Error("PalbeConfig.apiKey does not contain a valid publishable project identity");
9387
9429
  }
9388
- let runtimeURL;
9389
9430
  try {
9390
- runtimeURL = new URL(config.url);
9431
+ new URL(config.url);
9391
9432
  } catch {
9392
9433
  throw new Error("PalbeConfig.url must be a valid URL");
9393
9434
  }
9394
- if (runtimeURL.hostname.endsWith(".palbase.studio") && runtimeURL.hostname.split(".")[0] !== projectRef) {
9395
- throw new Error("PalbeConfig.url must match the project identity in apiKey for palbase.studio");
9396
- }
9397
9435
  const http = new HttpClient(config.apiKey, {
9398
9436
  url: config.url,
9399
9437
  headers: { "X-Client-Info": `palbe-web/${VERSION}`, ...config.headers }