@palbase/web 7.3.3 → 7.3.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.
@@ -6,8 +6,8 @@ import {
6
6
  import {
7
7
  __configure,
8
8
  getRuntime
9
- } from "../chunk-XF7NM6HF.js";
10
- import "../chunk-43BGZCNU.js";
9
+ } from "../chunk-2VURERR2.js";
10
+ import "../chunk-6DGCUHSO.js";
11
11
  import {
12
12
  environmentRefFromPublishableApiKey
13
13
  } from "../chunk-CFDU23TB.js";
@@ -180,6 +180,29 @@ function asPowChallenge(details) {
180
180
  return { id, prefix, difficulty };
181
181
  }
182
182
  var encoder = new TextEncoder();
183
+ var hasher = null;
184
+ async function digester() {
185
+ if (hasher) return hasher;
186
+ const runtime = globalThis;
187
+ const nodeish = runtime.process?.versions?.node !== void 0 || runtime.process?.versions?.bun !== void 0;
188
+ if (nodeish) {
189
+ try {
190
+ const mod = await import(
191
+ /* @vite-ignore */
192
+ `${"node:"}crypto`
193
+ );
194
+ if (typeof mod.createHash === "function") {
195
+ const createHash = mod.createHash;
196
+ hasher = (input) => new Uint8Array(createHash("sha256").update(input).digest());
197
+ return hasher;
198
+ }
199
+ } catch {
200
+ }
201
+ }
202
+ hasher = async (input) => new Uint8Array(await crypto.subtle.digest("SHA-256", encoder.encode(input)));
203
+ return hasher;
204
+ }
205
+ var now = () => typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
183
206
  function leadingZeroBits(hash) {
184
207
  let bits = 0;
185
208
  for (const byte of hash) {
@@ -195,18 +218,47 @@ var MAX_POW_DIFFICULTY = 24;
195
218
  function powBudget(difficulty) {
196
219
  return 8 * 2 ** difficulty;
197
220
  }
221
+ var POW_TIME_BUDGET_MS = 12e4;
222
+ var CALIBRATION_WARMUP = 1024;
223
+ var CALIBRATION_END = 9216;
198
224
  async function solvePowChallenge(challenge, maxIterations = powBudget(challenge.difficulty), signal) {
199
225
  if (challenge.difficulty > MAX_POW_DIFFICULTY) {
200
226
  throw new Error(
201
227
  `proof-of-work: refusing difficulty ${challenge.difficulty}; this client attempts at most ${MAX_POW_DIFFICULTY}, which is the highest a Palbase stack issues`
202
228
  );
203
229
  }
230
+ const digest = await digester();
231
+ let warmedAt = 0;
232
+ let calibrated = false;
233
+ let deadline = Number.POSITIVE_INFINITY;
204
234
  for (let nonce = 0; nonce < maxIterations; nonce++) {
205
- if ((nonce & 1023) === 0 && signal?.aborted) {
206
- throw new DOMException("proof-of-work solve aborted", "AbortError");
235
+ if ((nonce & 1023) === 0) {
236
+ if (signal?.aborted) {
237
+ throw new DOMException("proof-of-work solve aborted", "AbortError");
238
+ }
239
+ if (now() > deadline) {
240
+ throw new Error(
241
+ `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`
242
+ );
243
+ }
244
+ }
245
+ if (nonce === CALIBRATION_WARMUP) {
246
+ warmedAt = now();
247
+ }
248
+ if (!calibrated && nonce === CALIBRATION_END) {
249
+ calibrated = true;
250
+ const elapsed = Math.max(now() - warmedAt, 1e-3);
251
+ const rate = (CALIBRATION_END - CALIBRATION_WARMUP) / (elapsed / 1e3);
252
+ const expectedMs = 2 ** challenge.difficulty / rate * 1e3;
253
+ if (expectedMs > POW_TIME_BUDGET_MS) {
254
+ throw new Error(
255
+ `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`
256
+ );
257
+ }
258
+ deadline = now() + (POW_TIME_BUDGET_MS - (now() - warmedAt));
207
259
  }
208
- const digest = await crypto.subtle.digest("SHA-256", encoder.encode(challenge.prefix + nonce));
209
- if (leadingZeroBits(new Uint8Array(digest)) >= challenge.difficulty) {
260
+ const hash = await digest(challenge.prefix + nonce);
261
+ if (leadingZeroBits(hash) >= challenge.difficulty) {
210
262
  return {
211
263
  [POW_CHALLENGE_ID_HEADER]: challenge.id,
212
264
  [POW_NONCE_HEADER]: String(nonce)
@@ -9376,7 +9428,7 @@ function defaultSessionStorage(key) {
9376
9428
  }
9377
9429
 
9378
9430
  // src/version.ts
9379
- var VERSION = "7.3.3";
9431
+ var VERSION = "7.3.5";
9380
9432
 
9381
9433
  // src/runtime.ts
9382
9434
  function buildRuntime(config) {