@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/internal.js CHANGED
@@ -6,8 +6,8 @@ import {
6
6
  createBoundClient,
7
7
  getRuntime,
8
8
  onConfigured
9
- } from "./chunk-XF7NM6HF.js";
10
- import "./chunk-43BGZCNU.js";
9
+ } from "./chunk-2VURERR2.js";
10
+ import "./chunk-6DGCUHSO.js";
11
11
  import "./chunk-CFDU23TB.js";
12
12
  import "./chunk-PZ5AY32C.js";
13
13
  export {
@@ -70,6 +70,29 @@ function asPowChallenge(details) {
70
70
  return { id, prefix, difficulty };
71
71
  }
72
72
  var encoder = new TextEncoder();
73
+ var hasher = null;
74
+ async function digester() {
75
+ if (hasher) return hasher;
76
+ const runtime = globalThis;
77
+ const nodeish = runtime.process?.versions?.node !== void 0 || runtime.process?.versions?.bun !== void 0;
78
+ if (nodeish) {
79
+ try {
80
+ const mod = await import(
81
+ /* @vite-ignore */
82
+ `${"node:"}crypto`
83
+ );
84
+ if (typeof mod.createHash === "function") {
85
+ const createHash = mod.createHash;
86
+ hasher = (input) => new Uint8Array(createHash("sha256").update(input).digest());
87
+ return hasher;
88
+ }
89
+ } catch {
90
+ }
91
+ }
92
+ hasher = async (input) => new Uint8Array(await crypto.subtle.digest("SHA-256", encoder.encode(input)));
93
+ return hasher;
94
+ }
95
+ var now = () => typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
73
96
  function leadingZeroBits(hash) {
74
97
  let bits = 0;
75
98
  for (const byte of hash) {
@@ -85,18 +108,47 @@ var MAX_POW_DIFFICULTY = 24;
85
108
  function powBudget(difficulty) {
86
109
  return 8 * 2 ** difficulty;
87
110
  }
111
+ var POW_TIME_BUDGET_MS = 12e4;
112
+ var CALIBRATION_WARMUP = 1024;
113
+ var CALIBRATION_END = 9216;
88
114
  async function solvePowChallenge(challenge, maxIterations = powBudget(challenge.difficulty), signal) {
89
115
  if (challenge.difficulty > MAX_POW_DIFFICULTY) {
90
116
  throw new Error(
91
117
  `proof-of-work: refusing difficulty ${challenge.difficulty}; this client attempts at most ${MAX_POW_DIFFICULTY}, which is the highest a Palbase stack issues`
92
118
  );
93
119
  }
120
+ const digest = await digester();
121
+ let warmedAt = 0;
122
+ let calibrated = false;
123
+ let deadline = Number.POSITIVE_INFINITY;
94
124
  for (let nonce = 0; nonce < maxIterations; nonce++) {
95
- if ((nonce & 1023) === 0 && signal?.aborted) {
96
- throw new DOMException("proof-of-work solve aborted", "AbortError");
125
+ if ((nonce & 1023) === 0) {
126
+ if (signal?.aborted) {
127
+ throw new DOMException("proof-of-work solve aborted", "AbortError");
128
+ }
129
+ if (now() > deadline) {
130
+ throw new Error(
131
+ `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`
132
+ );
133
+ }
134
+ }
135
+ if (nonce === CALIBRATION_WARMUP) {
136
+ warmedAt = now();
137
+ }
138
+ if (!calibrated && nonce === CALIBRATION_END) {
139
+ calibrated = true;
140
+ const elapsed = Math.max(now() - warmedAt, 1e-3);
141
+ const rate = (CALIBRATION_END - CALIBRATION_WARMUP) / (elapsed / 1e3);
142
+ const expectedMs = 2 ** challenge.difficulty / rate * 1e3;
143
+ if (expectedMs > POW_TIME_BUDGET_MS) {
144
+ throw new Error(
145
+ `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`
146
+ );
147
+ }
148
+ deadline = now() + (POW_TIME_BUDGET_MS - (now() - warmedAt));
97
149
  }
98
- const digest = await crypto.subtle.digest("SHA-256", encoder.encode(challenge.prefix + nonce));
99
- if (leadingZeroBits(new Uint8Array(digest)) >= challenge.difficulty) {
150
+ const hash = await digest(challenge.prefix + nonce);
151
+ if (leadingZeroBits(hash) >= challenge.difficulty) {
100
152
  return {
101
153
  [POW_CHALLENGE_ID_HEADER]: challenge.id,
102
154
  [POW_NONCE_HEADER]: String(nonce)
@@ -9167,7 +9219,7 @@ function defaultSessionStorage(key) {
9167
9219
  }
9168
9220
 
9169
9221
  // src/version.ts
9170
- var VERSION = "7.3.3";
9222
+ var VERSION = "7.3.5";
9171
9223
 
9172
9224
  // src/runtime.ts
9173
9225
  function buildRuntime(config) {