nano-rspow-web 0.17.0 → 0.17.2

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/README.md CHANGED
@@ -39,7 +39,7 @@ node when it differs. See Nano's
39
39
  [Work Generation guide](https://docs.nano.org/integration-guides/work-generation/)
40
40
  for current network requirements.
41
41
 
42
- ## Benchmarking Dashboard
42
+ ## Benchmarking Dashboards
43
43
 
44
44
  <table>
45
45
  <tr>
@@ -54,9 +54,79 @@ for current network requirements.
54
54
  </tr>
55
55
  </table>
56
56
 
57
- An interactive benchmarking dashboard is included in `browser-demo/index.html` to measure WebGPU and CPU WebAssembly performance.
57
+ Try the browser-only dashboard without building anything:
58
+ **[https://casualsecurityinc.github.io/nano-rspow/](https://casualsecurityinc.github.io/nano-rspow/)**
58
59
 
59
- Try it out for yourself: **[https://casualsecurityinc.github.io/nano-rspow/](https://casualsecurityinc.github.io/nano-rspow/)**
60
+ ### Running them locally
61
+
62
+ Everything runs through `make` from the repository root. Run `make help` for the
63
+ full list.
64
+
65
+ **One-time setup, after cloning:**
66
+
67
+ ```bash
68
+ make web-prereqs
69
+ ```
70
+
71
+ That installs the `wasm32-unknown-unknown` Rust target, builds the
72
+ `nano-rspow-node` native addon and installs the dashboard's npm dependencies,
73
+ skipping whatever is already satisfied. It is safe to re-run. It needs `cargo`,
74
+ `npm` and `python3` on your `PATH`, plus the `wasm-bindgen` CLI, which it does
75
+ *not* install for you — that compiles for several minutes, so the script prints
76
+ the command instead and exits. Both dashboards need `wasm-bindgen`; the native
77
+ addon is the only prerequisite that only the head-to-head dashboard needs, so
78
+ `./scripts/setup-web-toolchain.sh --skip-addon` is enough for the browser-only
79
+ one.
80
+
81
+ Verify the toolchain without changing anything with
82
+ `./scripts/setup-web-toolchain.sh --check`.
83
+
84
+ `cargo benchmark-web` also builds and opens the browser-only dashboard. It is a
85
+ thin wrapper around the same `build-demo.py`, kept because the root README has
86
+ long pointed at it; prefer `make web-demo`, which is the same thing plus the
87
+ prerequisite tooling.
88
+
89
+ **Browser-only dashboard** — measures this package's WebGPU and WASM CPU
90
+ backends, nothing else. No server, no addon:
91
+
92
+ ```bash
93
+ make web-demo # builds and opens nano-rspow-web/browser-demo/index.html
94
+ make web-demo-build # same build, no browser launch
95
+ make web-demo-check # is the built page present, complete and current?
96
+ ```
97
+
98
+ `index.html` is a single self-contained file — the WebAssembly, the
99
+ wasm-bindgen glue and the WGSL shader are all inlined, so it works when opened
100
+ straight from disk over `file://`. It is **generated and not committed**,
101
+ because a 550 KB committed artifact eventually gets opened after the crate has
102
+ moved on and silently benchmarks an old WebAssembly module. If the file is
103
+ missing or older than its sources, `make web-demo-check` says so and tells you
104
+ to rebuild.
105
+
106
+ **Head-to-head dashboard** — compares this package against `nano-pow` and
107
+ against the `nano-rspow-node` addon and the Rust CLI work peer. It needs the
108
+ native addon, which is why it is a separate command:
109
+
110
+ ```bash
111
+ make web-compare-run # builds, then serves on http://localhost:8080/
112
+ ```
113
+
114
+ Overrides: `make web-compare-run PORT=8081`, or `WORK_PEER_PORT`,
115
+ `WORK_PEER_BACKEND=auto|cpu|gpu`, and `NANO_WORK_URL` to point at a work peer
116
+ you are already running. `make web-compare` builds into `dist/` without serving.
117
+ `make web-all` builds both, `make web-clean` removes the generated output.
118
+
119
+ ### What CI covers
120
+
121
+ CI builds and verifies the **browser-only** dashboard only, in the
122
+ `browser-demo` job of `.github/workflows/web-browser-tests.yml`. It runs the same
123
+ prerequisite script, the same `make web-demo-build`, and then
124
+ `scripts/check-browser-demo.sh` — the same check as `make web-demo-check` — so
125
+ CI and your machine cannot disagree about what a valid dashboard is.
126
+
127
+ The head-to-head dashboard is not built in CI because it requires the native
128
+ `nano-rspow-node` addon, which is a platform-specific native compile. It is a
129
+ local tool, and `make web-compare-run` is the supported way to run it.
60
130
 
61
131
 
62
132
 
@@ -78,6 +148,25 @@ Asynchronously generates Proof of Work for a 32-byte block hash at any hexadecim
78
148
  ### `generate_work_gpu(hash_hex: string, threshold_hex: string, cancel_token: WasmCancelToken): Promise<GenerateResult>`
79
149
  Forces WebGPU PoW generation at any hexadecimal threshold. Rejects if WebGPU is unavailable or if the cancellation token is triggered.
80
150
 
151
+ ### GPU resource lifetime and concurrency
152
+
153
+ One WebGPU generator is built per page and reused by every call. Building one
154
+ requests an instance, adapter, device and queue, compiles the WGSL shader,
155
+ creates the compute pipeline and allocates the double-buffered slots, so
156
+ rebuilding it per call charged that whole cost to the caller once per block.
157
+ `recommendLocalPow` builds the same generator, which also warms the cache for
158
+ the first `generate_work` call.
159
+
160
+ Two consequences for callers:
161
+
162
+ - **Generation is serialised.** The generator's ping-pong buffers are shared, so
163
+ overlapping calls would write the same slot and read back each other's
164
+ results, handing back work computed for a different block. Calls are
165
+ therefore queued and run one at a time, in the order they arrive. Concurrent
166
+ calls are safe but wait their turn; they are never rejected or interleaved.
167
+ - **A lost device is recovered automatically.** If the browser drops the
168
+ device, the cached generator is discarded and the next call builds a new one.
169
+
81
170
  ### `generate_work_cpu(hash_hex: string, threshold_hex: string): GenerateResult`
82
171
  Synchronously generates PoW at any hexadecimal threshold, forcing single-threaded CPU WebAssembly.
83
172
 
@@ -85,6 +174,13 @@ Synchronously generates PoW at any hexadecimal threshold, forcing single-threade
85
174
 
86
175
  Tests at most `max_nonces` nonces synchronously on the CPU. Returns a lowercase 16-character nonce when found, otherwise `null`. The CPU random-number-generator state persists between calls. Pass an unsigned 32-bit integer.
87
176
 
177
+ ### `recommendLocalPow(reprobe = false): Promise<boolean>`
178
+
179
+ Returns whether this browser should prefer local PoW. On a cache miss it tests
180
+ WebGPU capability, or the single-threaded WASM CPU fallback's throughput. The
181
+ result is cached internally. Pass `true` to force a fresh evaluation after a
182
+ hardware or browser capability change.
183
+
88
184
  ### `validate_work(hash_hex: string, nonce_hex: string, threshold_hex: string): boolean`
89
185
  Synchronously validates whether a nonce meets an arbitrary hexadecimal threshold for the given block hash.
90
186
 
package/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { default } from './nano_rspow_web.js';
2
2
  export * from './nano_rspow_web.js';
3
- export { createPowEngine } from './pow-engine.js';
3
+ export { createPowEngine, recommendLocalPow } from './pow-engine.js';
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { default } from './nano_rspow_web.js'
2
2
  export * from './nano_rspow_web.js'
3
- export { createPowEngine } from './pow-engine.js'
3
+ export { createPowEngine, recommendLocalPow } from './pow-engine.js'
@@ -51,6 +51,15 @@ export function generate_work_cpu_batch(hash_hex: string, threshold_hex: string,
51
51
  */
52
52
  export function generate_work_gpu(hash_hex: string, threshold_hex: string, cancel_token: WasmCancelToken): Promise<GenerateResult>;
53
53
 
54
+ /**
55
+ * Measure whether this browser can reasonably perform local proof-of-work.
56
+ *
57
+ * A usable WebGPU pipeline is always recommended. Without WebGPU, the
58
+ * single-threaded WASM CPU fallback must sustain the same 15 MH/s threshold
59
+ * used by the native recommendation before it is recommended.
60
+ */
61
+ export function probe_local_pow(): Promise<boolean>;
62
+
54
63
  /**
55
64
  * Synchronously validate a nonce against an arbitrary threshold for a block hash.
56
65
  */
@@ -68,12 +77,13 @@ export interface InitOutput {
68
77
  readonly generate_work_gpu: (a: number, b: number, c: number, d: number, e: number) => any;
69
78
  readonly generateresult_is_gpu: (a: number) => number;
70
79
  readonly generateresult_nonce: (a: number) => [number, number];
80
+ readonly probe_local_pow: () => any;
71
81
  readonly validate_work: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number, number];
72
82
  readonly wasmcanceltoken_cancel: (a: number) => void;
73
83
  readonly wasmcanceltoken_new: () => number;
74
- readonly wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue__core_f0fd674eaa06beef___result__Result_____wasm_bindgen_8b74beb86e9b2264___JsError___true_: (a: number, b: number, c: any) => [number, number];
75
- readonly wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
76
- readonly wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue______true_: (a: number, b: number, c: any) => void;
84
+ readonly wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___wasm_bindgen_eea6accdcfb85281___JsValue__core_ed718c3d60ebd546___result__Result_____wasm_bindgen_eea6accdcfb85281___JsError___true_: (a: number, b: number, c: any) => [number, number];
85
+ readonly wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
86
+ readonly wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___wasm_bindgen_eea6accdcfb85281___JsValue______true_: (a: number, b: number, c: any) => void;
77
87
  readonly __wbindgen_malloc: (a: number, b: number) => number;
78
88
  readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
79
89
  readonly __wbindgen_exn_store: (a: number) => void;
package/nano_rspow_web.js CHANGED
@@ -149,6 +149,19 @@ export function generate_work_gpu(hash_hex, threshold_hex, cancel_token) {
149
149
  return ret;
150
150
  }
151
151
 
152
+ /**
153
+ * Measure whether this browser can reasonably perform local proof-of-work.
154
+ *
155
+ * A usable WebGPU pipeline is always recommended. Without WebGPU, the
156
+ * single-threaded WASM CPU fallback must sustain the same 15 MH/s threshold
157
+ * used by the native recommendation before it is recommended.
158
+ * @returns {Promise<boolean>}
159
+ */
160
+ export function probe_local_pow() {
161
+ const ret = wasm.probe_local_pow();
162
+ return ret;
163
+ }
164
+
152
165
  /**
153
166
  * Synchronously validate a nonce against an arbitrary threshold for a block hash.
154
167
  * @param {string} hash_hex
@@ -296,6 +309,16 @@ function __wbg_get_imports() {
296
309
  const ret = result;
297
310
  return ret;
298
311
  },
312
+ __wbg_instanceof_GpuDeviceLostInfo_0e99a9595225a57d: function(arg0) {
313
+ let result;
314
+ try {
315
+ result = arg0 instanceof GPUDeviceLostInfo;
316
+ } catch (_) {
317
+ result = false;
318
+ }
319
+ const ret = result;
320
+ return ret;
321
+ },
299
322
  __wbg_label_9a8583e3a20fafc7: function(arg0, arg1) {
300
323
  const ret = arg1.label;
301
324
  const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
@@ -310,10 +333,21 @@ function __wbg_get_imports() {
310
333
  __wbg_log_eb752234eec406d1: function(arg0) {
311
334
  console.log(arg0);
312
335
  },
336
+ __wbg_lost_b787a12a0e44349f: function(arg0) {
337
+ const ret = arg0.lost;
338
+ return ret;
339
+ },
313
340
  __wbg_mapAsync_e3cfbd141919d03c: function(arg0, arg1, arg2, arg3) {
314
341
  const ret = arg0.mapAsync(arg1 >>> 0, arg2, arg3);
315
342
  return ret;
316
343
  },
344
+ __wbg_message_c717665d7f0d1da0: function(arg0, arg1) {
345
+ const ret = arg1.message;
346
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
347
+ const len1 = WASM_VECTOR_LEN;
348
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
349
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
350
+ },
317
351
  __wbg_navigator_3334c390f542c642: function(arg0) {
318
352
  const ret = arg0.navigator;
319
353
  return ret;
@@ -337,7 +371,7 @@ function __wbg_get_imports() {
337
371
  const a = state0.a;
338
372
  state0.a = 0;
339
373
  try {
340
- return wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined_______true_(a, state0.b, arg0, arg1);
374
+ return wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined_______true_(a, state0.b, arg0, arg1);
341
375
  } finally {
342
376
  state0.a = a;
343
377
  }
@@ -355,7 +389,7 @@ function __wbg_get_imports() {
355
389
  const a = state0.a;
356
390
  state0.a = 0;
357
391
  try {
358
- return wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined_______true_(a, state0.b, arg0, arg1);
392
+ return wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined_______true_(a, state0.b, arg0, arg1);
359
393
  } finally {
360
394
  state0.a = a;
361
395
  }
@@ -370,6 +404,10 @@ function __wbg_get_imports() {
370
404
  const ret = new Uint8Array(arg0, arg1 >>> 0, arg2 >>> 0);
371
405
  return ret;
372
406
  },
407
+ __wbg_now_81363d44c96dd239: function() {
408
+ const ret = Date.now();
409
+ return ret;
410
+ },
373
411
  __wbg_onSubmittedWorkDone_5f36409816d68e04: function(arg0) {
374
412
  const ret = arg0.onSubmittedWorkDone();
375
413
  return ret;
@@ -392,6 +430,10 @@ function __wbg_get_imports() {
392
430
  const ret = arg0.queue;
393
431
  return ret;
394
432
  },
433
+ __wbg_reason_170684b0bb329a56: function(arg0) {
434
+ const ret = arg0.reason;
435
+ return (__wbindgen_enum_GpuDeviceLostReason.indexOf(ret) + 1 || 3) - 1;
436
+ },
395
437
  __wbg_requestAdapter_0049683abd339828: function(arg0, arg1) {
396
438
  const ret = arg0.requestAdapter(arg1);
397
439
  return ret;
@@ -592,6 +634,10 @@ function __wbg_get_imports() {
592
634
  __wbg_submit_b3bbead76cbf7627: function(arg0, arg1) {
593
635
  arg0.submit(arg1);
594
636
  },
637
+ __wbg_then_7b57a40e3ee05615: function(arg0, arg1) {
638
+ const ret = arg0.then(arg1);
639
+ return ret;
640
+ },
595
641
  __wbg_then_837494e384b37459: function(arg0, arg1) {
596
642
  const ret = arg0.then(arg1);
597
643
  return ret;
@@ -611,13 +657,13 @@ function __wbg_get_imports() {
611
657
  arg0.writeBuffer(arg1, arg2, getArrayU8FromWasm0(arg3, arg4), arg5, arg6);
612
658
  }, arguments); },
613
659
  __wbindgen_cast_0000000000000001: function(arg0, arg1) {
614
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 64, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
615
- const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue______true_);
660
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 111, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
661
+ const ret = makeMutClosure(arg0, arg1, wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___wasm_bindgen_eea6accdcfb85281___JsValue__core_ed718c3d60ebd546___result__Result_____wasm_bindgen_eea6accdcfb85281___JsError___true_);
616
662
  return ret;
617
663
  },
618
664
  __wbindgen_cast_0000000000000002: function(arg0, arg1) {
619
- // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 98, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
620
- const ret = makeMutClosure(arg0, arg1, wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue__core_f0fd674eaa06beef___result__Result_____wasm_bindgen_8b74beb86e9b2264___JsError___true_);
665
+ // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 77, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
666
+ const ret = makeMutClosure(arg0, arg1, wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___wasm_bindgen_eea6accdcfb85281___JsValue______true_);
621
667
  return ret;
622
668
  },
623
669
  __wbindgen_cast_0000000000000003: function(arg0) {
@@ -651,25 +697,28 @@ function __wbg_get_imports() {
651
697
  };
652
698
  }
653
699
 
654
- function wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue______true_(arg0, arg1, arg2) {
655
- wasm.wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue______true_(arg0, arg1, arg2);
700
+ function wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___wasm_bindgen_eea6accdcfb85281___JsValue______true_(arg0, arg1, arg2) {
701
+ wasm.wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___wasm_bindgen_eea6accdcfb85281___JsValue______true_(arg0, arg1, arg2);
656
702
  }
657
703
 
658
- function wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue__core_f0fd674eaa06beef___result__Result_____wasm_bindgen_8b74beb86e9b2264___JsError___true_(arg0, arg1, arg2) {
659
- const ret = wasm.wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___wasm_bindgen_8b74beb86e9b2264___JsValue__core_f0fd674eaa06beef___result__Result_____wasm_bindgen_8b74beb86e9b2264___JsError___true_(arg0, arg1, arg2);
704
+ function wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___wasm_bindgen_eea6accdcfb85281___JsValue__core_ed718c3d60ebd546___result__Result_____wasm_bindgen_eea6accdcfb85281___JsError___true_(arg0, arg1, arg2) {
705
+ const ret = wasm.wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___wasm_bindgen_eea6accdcfb85281___JsValue__core_ed718c3d60ebd546___result__Result_____wasm_bindgen_eea6accdcfb85281___JsError___true_(arg0, arg1, arg2);
660
706
  if (ret[1]) {
661
707
  throw takeFromExternrefTable0(ret[0]);
662
708
  }
663
709
  }
664
710
 
665
- function wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined_______true_(arg0, arg1, arg2, arg3) {
666
- wasm.wasm_bindgen_8b74beb86e9b2264___convert__closures_____invoke___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined___js_sys_7c33e7a1356e6034___Function_fn_wasm_bindgen_8b74beb86e9b2264___JsValue_____wasm_bindgen_8b74beb86e9b2264___sys__Undefined_______true_(arg0, arg1, arg2, arg3);
711
+ function wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined_______true_(arg0, arg1, arg2, arg3) {
712
+ wasm.wasm_bindgen_eea6accdcfb85281___convert__closures_____invoke___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined___js_sys_8383356d580dd162___Function_fn_wasm_bindgen_eea6accdcfb85281___JsValue_____wasm_bindgen_eea6accdcfb85281___sys__Undefined_______true_(arg0, arg1, arg2, arg3);
667
713
  }
668
714
 
669
715
 
670
716
  const __wbindgen_enum_GpuBufferBindingType = ["uniform", "storage", "read-only-storage"];
671
717
 
672
718
 
719
+ const __wbindgen_enum_GpuDeviceLostReason = ["unknown", "destroyed"];
720
+
721
+
673
722
  const __wbindgen_enum_GpuPowerPreference = ["low-power", "high-performance"];
674
723
 
675
724
 
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nano-rspow-web",
3
- "version": "0.17.0",
3
+ "version": "0.17.2",
4
4
  "type": "module",
5
5
  "description": "WebGPU-accelerated Nano (XNO) Proof of Work generation in the browser using WebAssembly",
6
6
  "keywords": [
@@ -29,10 +29,13 @@
29
29
  "index.d.ts",
30
30
  "pow-engine.js",
31
31
  "pow-engine.d.ts",
32
+ "recommendation.js",
32
33
  "README.md"
33
34
  ],
34
35
  "scripts": {
35
- "build": "cargo build -p nano-rspow-web --target wasm32-unknown-unknown --release && wasm-bindgen --target web --out-dir . ../target/wasm32-unknown-unknown/release/nano_rspow_web.wasm"
36
+ "build": "cargo build -p nano-rspow-web --target wasm32-unknown-unknown --release && wasm-bindgen --target web --out-dir . ../target/wasm32-unknown-unknown/release/nano_rspow_web.wasm",
37
+ "test": "node --test recommendation.test.js",
38
+ "test:browser": "npm run build && node --test tests/browser/*.test.mjs"
36
39
  },
37
40
  "dependencies": {
38
41
  "@openrai/nano-pow-contract": "^0.1.0"
package/pow-engine.d.ts CHANGED
@@ -2,3 +2,11 @@ import type { PowEngine } from '@openrai/nano-pow-contract';
2
2
 
3
3
  /** Create the nano-pow-contract adapter for the WebAssembly/WebGPU engine. */
4
4
  export declare function createPowEngine(): PowEngine;
5
+
6
+ /**
7
+ * Return whether this browser should prefer local PoW.
8
+ *
9
+ * The first call performs a capability and CPU-throughput evaluation. Later
10
+ * calls return its cached result unless `reprobe` is `true`.
11
+ */
12
+ export declare function recommendLocalPow(reprobe?: boolean): Promise<boolean>;
package/pow-engine.js CHANGED
@@ -1,4 +1,5 @@
1
- import init, { generate_work, validate_work } from './nano_rspow_web.js'
1
+ import init, { generate_work, probe_local_pow, validate_work } from './nano_rspow_web.js'
2
+ import { createLocalPowRecommendation } from './recommendation.js'
2
3
 
3
4
  let initialization
4
5
 
@@ -7,6 +8,15 @@ function ready () {
7
8
  return initialization
8
9
  }
9
10
 
11
+ /**
12
+ * Return whether this browser should prefer local PoW. The result is cached;
13
+ * pass `true` to force a fresh browser capability and performance probe.
14
+ */
15
+ export const recommendLocalPow = createLocalPowRecommendation(async () => {
16
+ await ready()
17
+ return probe_local_pow()
18
+ })
19
+
10
20
  /** Create the nano-pow-contract adapter for the WebAssembly/WebGPU engine. */
11
21
  export function createPowEngine () {
12
22
  return {
@@ -0,0 +1,72 @@
1
+ const CACHE_KEY = 'nano-rspow-web.local-pow-recommendation.v1'
2
+
3
+ function browserStorage () {
4
+ try {
5
+ return globalThis.localStorage ?? null
6
+ } catch {
7
+ return null
8
+ }
9
+ }
10
+
11
+ /**
12
+ * Cache an asynchronous local-PoW recommendation without exposing browser
13
+ * storage details to consumers.
14
+ *
15
+ * @param {() => Promise<boolean>} probe runs the actual capability check
16
+ * @param {{ storage?: Storage | null }} [options] testable browser storage override
17
+ * @returns {(reprobe?: boolean) => Promise<boolean>} cached recommendation function
18
+ */
19
+ export function createLocalPowRecommendation (probe, { storage = browserStorage() } = {}) {
20
+ let cacheRead = false
21
+ let cachedRecommendation
22
+ let activeProbe
23
+ let generation = 0
24
+
25
+ function readCache () {
26
+ if (cacheRead) return
27
+ cacheRead = true
28
+
29
+ try {
30
+ const value = storage?.getItem(CACHE_KEY)
31
+ if (value === 'true') cachedRecommendation = true
32
+ if (value === 'false') cachedRecommendation = false
33
+ } catch {
34
+ // Storage is optional: a private-browser or quota failure falls back to memory.
35
+ }
36
+ }
37
+
38
+ function writeCache (recommendation) {
39
+ try {
40
+ storage?.setItem(CACHE_KEY, String(recommendation))
41
+ } catch {
42
+ // The in-memory result remains valid for this page session.
43
+ }
44
+ }
45
+
46
+ return function recommendLocalPow (reprobe = false) {
47
+ readCache()
48
+
49
+ if (!reprobe && cachedRecommendation !== undefined) {
50
+ return Promise.resolve(cachedRecommendation)
51
+ }
52
+
53
+ if (!reprobe && activeProbe) return activeProbe.promise
54
+
55
+ const probeGeneration = ++generation
56
+ const promise = Promise.resolve()
57
+ .then(probe)
58
+ .then(recommendation => {
59
+ if (probeGeneration === generation) {
60
+ cachedRecommendation = recommendation
61
+ writeCache(recommendation)
62
+ }
63
+ return recommendation
64
+ })
65
+ .finally(() => {
66
+ if (activeProbe?.generation === probeGeneration) activeProbe = undefined
67
+ })
68
+
69
+ activeProbe = { generation: probeGeneration, promise }
70
+ return promise
71
+ }
72
+ }