@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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  importNextServer
3
- } from "../chunk-IRPS637F.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-XF7NM6HF.js";
17
+ } from "../chunk-2VURERR2.js";
18
18
  import {
19
19
  BackendError
20
- } from "../chunk-43BGZCNU.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-IRPS637F.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-43BGZCNU.js";
12
+ } from "../chunk-6DGCUHSO.js";
13
13
  import {
14
14
  environmentRefFromPublishableApiKey
15
15
  } from "../chunk-CFDU23TB.js";
@@ -63,6 +63,29 @@ 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
+ }
88
+ var now = () => typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
66
89
  function leadingZeroBits(hash) {
67
90
  let bits = 0;
68
91
  for (const byte of hash) {
@@ -78,18 +101,47 @@ var MAX_POW_DIFFICULTY = 24;
78
101
  function powBudget(difficulty) {
79
102
  return 8 * 2 ** difficulty;
80
103
  }
104
+ var POW_TIME_BUDGET_MS = 12e4;
105
+ var CALIBRATION_WARMUP = 1024;
106
+ var CALIBRATION_END = 9216;
81
107
  async function solvePowChallenge(challenge, maxIterations = powBudget(challenge.difficulty), signal) {
82
108
  if (challenge.difficulty > MAX_POW_DIFFICULTY) {
83
109
  throw new Error(
84
110
  `proof-of-work: refusing difficulty ${challenge.difficulty}; this client attempts at most ${MAX_POW_DIFFICULTY}, which is the highest a Palbase stack issues`
85
111
  );
86
112
  }
113
+ const digest = await digester();
114
+ let warmedAt = 0;
115
+ let calibrated = false;
116
+ let deadline = Number.POSITIVE_INFINITY;
87
117
  for (let nonce = 0; nonce < maxIterations; nonce++) {
88
- if ((nonce & 1023) === 0 && signal?.aborted) {
89
- throw new DOMException("proof-of-work solve aborted", "AbortError");
118
+ if ((nonce & 1023) === 0) {
119
+ if (signal?.aborted) {
120
+ throw new DOMException("proof-of-work solve aborted", "AbortError");
121
+ }
122
+ if (now() > deadline) {
123
+ throw new Error(
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`
139
+ );
140
+ }
141
+ deadline = now() + (POW_TIME_BUDGET_MS - (now() - warmedAt));
90
142
  }
91
- const digest = await crypto.subtle.digest("SHA-256", encoder.encode(challenge.prefix + nonce));
92
- if (leadingZeroBits(new Uint8Array(digest)) >= challenge.difficulty) {
143
+ const hash = await digest(challenge.prefix + nonce);
144
+ if (leadingZeroBits(hash) >= challenge.difficulty) {
93
145
  return {
94
146
  [POW_CHALLENGE_ID_HEADER]: challenge.id,
95
147
  [POW_NONCE_HEADER]: String(nonce)
@@ -414,7 +466,7 @@ function applyBrowserOriginHeader(headers) {
414
466
  }
415
467
 
416
468
  // src/version.ts
417
- var VERSION = "7.3.3";
469
+ var VERSION = "7.3.5";
418
470
 
419
471
  // src/call.ts
420
472
  async function callEndpoint(resolveRt, name, input, options) {