@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.
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.3";
209
+ declare const VERSION = "7.3.5";
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.3";
209
+ declare const VERSION = "7.3.5";
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-XF7NM6HF.js";
14
+ } from "./chunk-2VURERR2.js";
15
15
  import {
16
16
  BackendError,
17
17
  isBackendError
18
- } from "./chunk-43BGZCNU.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,29 @@ 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
+ }
93
+ var now = () => typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
71
94
  function leadingZeroBits(hash) {
72
95
  let bits = 0;
73
96
  for (const byte of hash) {
@@ -83,18 +106,47 @@ var MAX_POW_DIFFICULTY = 24;
83
106
  function powBudget(difficulty) {
84
107
  return 8 * 2 ** difficulty;
85
108
  }
109
+ var POW_TIME_BUDGET_MS = 12e4;
110
+ var CALIBRATION_WARMUP = 1024;
111
+ var CALIBRATION_END = 9216;
86
112
  async function solvePowChallenge(challenge, maxIterations = powBudget(challenge.difficulty), signal) {
87
113
  if (challenge.difficulty > MAX_POW_DIFFICULTY) {
88
114
  throw new Error(
89
115
  `proof-of-work: refusing difficulty ${challenge.difficulty}; this client attempts at most ${MAX_POW_DIFFICULTY}, which is the highest a Palbase stack issues`
90
116
  );
91
117
  }
118
+ const digest = await digester();
119
+ let warmedAt = 0;
120
+ let calibrated = false;
121
+ let deadline = Number.POSITIVE_INFINITY;
92
122
  for (let nonce = 0; nonce < maxIterations; nonce++) {
93
- if ((nonce & 1023) === 0 && signal?.aborted) {
94
- throw new DOMException("proof-of-work solve aborted", "AbortError");
123
+ if ((nonce & 1023) === 0) {
124
+ if (signal?.aborted) {
125
+ throw new DOMException("proof-of-work solve aborted", "AbortError");
126
+ }
127
+ if (now() > deadline) {
128
+ throw new Error(
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`
130
+ );
131
+ }
132
+ }
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));
95
147
  }
96
- const digest = await crypto.subtle.digest("SHA-256", encoder.encode(challenge.prefix + nonce));
97
- if (leadingZeroBits(new Uint8Array(digest)) >= challenge.difficulty) {
148
+ const hash = await digest(challenge.prefix + nonce);
149
+ if (leadingZeroBits(hash) >= challenge.difficulty) {
98
150
  return {
99
151
  [POW_CHALLENGE_ID_HEADER]: challenge.id,
100
152
  [POW_NONCE_HEADER]: String(nonce)
@@ -9367,7 +9419,7 @@ function defaultSessionStorage(key) {
9367
9419
  }
9368
9420
 
9369
9421
  // src/version.ts
9370
- var VERSION = "7.3.3";
9422
+ var VERSION = "7.3.5";
9371
9423
 
9372
9424
  // src/runtime.ts
9373
9425
  function buildRuntime(config) {