@palbase/web 7.3.4 → 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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  importNextServer
3
- } from "../chunk-IVD6BP5Z.js";
3
+ } from "../chunk-HT5SNA5H.js";
4
4
  import {
5
5
  SESSION_COOKIE_ATTRS,
6
6
  clearedSessionCookieNames,
@@ -14,10 +14,10 @@ import {
14
14
  buildRuntime,
15
15
  createBoundClient,
16
16
  getRuntime
17
- } from "../chunk-PYJ3CMA2.js";
17
+ } from "../chunk-2VURERR2.js";
18
18
  import {
19
19
  BackendError
20
- } from "../chunk-RAP37ILS.js";
20
+ } from "../chunk-6DGCUHSO.js";
21
21
  import {
22
22
  environmentRefFromApiKey,
23
23
  environmentRefFromPublishableApiKey
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  importNextServer
3
- } from "../chunk-IVD6BP5Z.js";
3
+ } from "../chunk-HT5SNA5H.js";
4
4
  import {
5
5
  SESSION_COOKIE_ATTRS,
6
6
  clearedSessionCookieNames,
@@ -9,7 +9,7 @@ import {
9
9
  } from "../chunk-6VGKMNXO.js";
10
10
  import {
11
11
  BackendError
12
- } from "../chunk-RAP37ILS.js";
12
+ } from "../chunk-6DGCUHSO.js";
13
13
  import {
14
14
  environmentRefFromPublishableApiKey
15
15
  } from "../chunk-CFDU23TB.js";
@@ -63,6 +63,28 @@ function asPowChallenge(details) {
63
63
  return { id, prefix, difficulty };
64
64
  }
65
65
  var encoder = new TextEncoder();
66
+ var hasher = null;
67
+ async function digester() {
68
+ if (hasher) return hasher;
69
+ const runtime = globalThis;
70
+ const nodeish = runtime.process?.versions?.node !== void 0 || runtime.process?.versions?.bun !== void 0;
71
+ if (nodeish) {
72
+ try {
73
+ const mod = await import(
74
+ /* @vite-ignore */
75
+ `${"node:"}crypto`
76
+ );
77
+ if (typeof mod.createHash === "function") {
78
+ const createHash = mod.createHash;
79
+ hasher = (input) => new Uint8Array(createHash("sha256").update(input).digest());
80
+ return hasher;
81
+ }
82
+ } catch {
83
+ }
84
+ }
85
+ hasher = async (input) => new Uint8Array(await crypto.subtle.digest("SHA-256", encoder.encode(input)));
86
+ return hasher;
87
+ }
66
88
  var now = () => typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
67
89
  function leadingZeroBits(hash) {
68
90
  let bits = 0;
@@ -79,14 +101,19 @@ var MAX_POW_DIFFICULTY = 24;
79
101
  function powBudget(difficulty) {
80
102
  return 8 * 2 ** difficulty;
81
103
  }
82
- var POW_DEADLINE_MS = 12e4;
104
+ var POW_TIME_BUDGET_MS = 12e4;
105
+ var CALIBRATION_WARMUP = 1024;
106
+ var CALIBRATION_END = 9216;
83
107
  async function solvePowChallenge(challenge, maxIterations = powBudget(challenge.difficulty), signal) {
84
108
  if (challenge.difficulty > MAX_POW_DIFFICULTY) {
85
109
  throw new Error(
86
110
  `proof-of-work: refusing difficulty ${challenge.difficulty}; this client attempts at most ${MAX_POW_DIFFICULTY}, which is the highest a Palbase stack issues`
87
111
  );
88
112
  }
89
- const deadline = now() + POW_DEADLINE_MS;
113
+ const digest = await digester();
114
+ let warmedAt = 0;
115
+ let calibrated = false;
116
+ let deadline = Number.POSITIVE_INFINITY;
90
117
  for (let nonce = 0; nonce < maxIterations; nonce++) {
91
118
  if ((nonce & 1023) === 0) {
92
119
  if (signal?.aborted) {
@@ -94,12 +121,27 @@ async function solvePowChallenge(challenge, maxIterations = powBudget(challenge.
94
121
  }
95
122
  if (now() > deadline) {
96
123
  throw new Error(
97
- `proof-of-work: gave up after ${POW_DEADLINE_MS / 1e3}s at difficulty ${challenge.difficulty} (${nonce} attempts)`
124
+ `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`
125
+ );
126
+ }
127
+ }
128
+ if (nonce === CALIBRATION_WARMUP) {
129
+ warmedAt = now();
130
+ }
131
+ if (!calibrated && nonce === CALIBRATION_END) {
132
+ calibrated = true;
133
+ const elapsed = Math.max(now() - warmedAt, 1e-3);
134
+ const rate = (CALIBRATION_END - CALIBRATION_WARMUP) / (elapsed / 1e3);
135
+ const expectedMs = 2 ** challenge.difficulty / rate * 1e3;
136
+ if (expectedMs > POW_TIME_BUDGET_MS) {
137
+ throw new Error(
138
+ `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`
98
139
  );
99
140
  }
141
+ deadline = now() + (POW_TIME_BUDGET_MS - (now() - warmedAt));
100
142
  }
101
- const digest = await crypto.subtle.digest("SHA-256", encoder.encode(challenge.prefix + nonce));
102
- if (leadingZeroBits(new Uint8Array(digest)) >= challenge.difficulty) {
143
+ const hash = await digest(challenge.prefix + nonce);
144
+ if (leadingZeroBits(hash) >= challenge.difficulty) {
103
145
  return {
104
146
  [POW_CHALLENGE_ID_HEADER]: challenge.id,
105
147
  [POW_NONCE_HEADER]: String(nonce)
@@ -424,7 +466,7 @@ function applyBrowserOriginHeader(headers) {
424
466
  }
425
467
 
426
468
  // src/version.ts
427
- var VERSION = "7.3.4";
469
+ var VERSION = "7.3.5";
428
470
 
429
471
  // src/call.ts
430
472
  async function callEndpoint(resolveRt, name, input, options) {