@cap.js/widget 0.1.24 → 0.1.26

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/src/worker.js CHANGED
@@ -1,109 +1,115 @@
1
1
  (() => {
2
- if (
3
- typeof WebAssembly !== "object" ||
4
- typeof WebAssembly?.instantiate !== "function"
5
- ) {
6
- self.onmessage = async ({ data: { salt, target } }) => {
7
- // Fallback solver in case WASM is not available
8
-
9
- let nonce = 0;
10
- const batchSize = 50000;
11
- let processed = 0;
12
- const encoder = new TextEncoder();
13
-
14
- const targetBytes = new Uint8Array(target.length / 2);
15
- for (let k = 0; k < targetBytes.length; k++) {
16
- targetBytes[k] = parseInt(target.substring(k * 2, k * 2 + 2), 16);
17
- }
18
- const targetBytesLength = targetBytes.length;
19
-
20
- while (true) {
21
- try {
22
- for (let i = 0; i < batchSize; i++) {
23
- const inputString = salt + nonce;
24
- const inputBytes = encoder.encode(inputString);
25
-
26
- const hashBuffer = await crypto.subtle.digest(
27
- "SHA-256",
28
- inputBytes
29
- );
30
-
31
- const hashBytes = new Uint8Array(hashBuffer, 0, targetBytesLength);
32
-
33
- let matches = true;
34
- for (let k = 0; k < targetBytesLength; k++) {
35
- if (hashBytes[k] !== targetBytes[k]) {
36
- matches = false;
37
- break;
38
- }
39
- }
40
-
41
- if (matches) {
42
- self.postMessage({ nonce, found: true });
43
- return;
44
- }
45
-
46
- nonce++;
47
- }
48
-
49
- processed += batchSize;
50
- } catch (error) {
51
- console.error("[cap worker] fallback worker error", error);
52
- self.postMessage({
53
- found: false,
54
- error: error.message,
55
- });
56
- return;
57
- }
58
- }
59
- };
60
-
61
- return console.warn(
62
- "[cap worker] wasm not supported, falling back to alternative solver. this will be significantly slower."
63
- );
64
- }
65
-
66
- let wasmCacheUrl, solve_pow_function;
67
-
68
- self.onmessage = async ({ data: { salt, target, wasmUrl } }) => {
69
- if (wasmCacheUrl !== wasmUrl) {
70
- wasmCacheUrl = wasmUrl;
71
- await import(wasmUrl)
72
- .then((wasmModule) => {
73
- return wasmModule.default().then((instance) => {
74
- solve_pow_function = (
75
- instance && instance.exports ? instance.exports : wasmModule
76
- ).solve_pow;
77
- });
78
- })
79
- .catch((e) => {
80
- console.error("[cap worker] using fallback solver due to error:", e);
81
- });
82
- }
83
-
84
- try {
85
- const startTime = performance.now();
86
- const nonce = solve_pow_function(salt, target);
87
- const endTime = performance.now();
88
-
89
- self.postMessage({
90
- nonce: Number(nonce),
91
- found: true,
92
- durationMs: (endTime - startTime).toFixed(2),
93
- });
94
- } catch (error) {
95
- console.error("[cap worker]", error);
96
- self.postMessage({
97
- found: false,
98
- error: error.message || String(error),
99
- });
100
- }
101
- };
102
-
103
- self.onerror = (error) => {
104
- self.postMessage({
105
- found: false,
106
- error,
107
- });
108
- };
2
+ const solveFallback = async ({ salt, target }) => {
3
+ let nonce = 0;
4
+ const batchSize = 50000;
5
+ const encoder = new TextEncoder();
6
+
7
+ const targetBytes = new Uint8Array(target.length / 2);
8
+ for (let k = 0; k < targetBytes.length; k++) {
9
+ targetBytes[k] = parseInt(target.substring(k * 2, k * 2 + 2), 16);
10
+ }
11
+ const targetBytesLength = targetBytes.length;
12
+
13
+ while (true) {
14
+ try {
15
+ for (let i = 0; i < batchSize; i++) {
16
+ const inputString = salt + nonce;
17
+ const inputBytes = encoder.encode(inputString);
18
+
19
+ const hashBuffer = await crypto.subtle.digest("SHA-256", inputBytes);
20
+
21
+ const hashBytes = new Uint8Array(hashBuffer, 0, targetBytesLength);
22
+
23
+ let matches = true;
24
+ for (let k = 0; k < targetBytesLength; k++) {
25
+ if (hashBytes[k] !== targetBytes[k]) {
26
+ matches = false;
27
+ break;
28
+ }
29
+ }
30
+
31
+ if (matches) {
32
+ self.postMessage({ nonce, found: true });
33
+ return;
34
+ }
35
+
36
+ nonce++;
37
+ }
38
+ } catch (error) {
39
+ console.error("[cap worker]", error);
40
+ self.postMessage({
41
+ found: false,
42
+ error: error.message,
43
+ });
44
+ return;
45
+ }
46
+ }
47
+ };
48
+
49
+ if (
50
+ typeof WebAssembly !== "object" ||
51
+ typeof WebAssembly?.instantiate !== "function"
52
+ ) {
53
+ console.warn(
54
+ "[cap worker] wasm not supported, falling back to alternative solver. this will be significantly slower.",
55
+ );
56
+
57
+ self.onmessage = async ({ data: { salt, target } }) => {
58
+ return solveFallback({ salt, target });
59
+ };
60
+
61
+ return;
62
+ }
63
+
64
+ let wasmCacheUrl, solve_pow_function;
65
+
66
+ self.onmessage = async ({ data: { salt, target, wasmUrl } }) => {
67
+ let fb;
68
+
69
+ if (wasmCacheUrl !== wasmUrl) {
70
+ wasmCacheUrl = wasmUrl;
71
+ await import(wasmUrl)
72
+ .then((wasmModule) => {
73
+ return wasmModule.default().then((instance) => {
74
+ solve_pow_function = (
75
+ instance?.exports ? instance.exports : wasmModule
76
+ ).solve_pow;
77
+ });
78
+ })
79
+ .catch((e) => {
80
+ console.error("[cap worker] using fallback solver due to error:", e);
81
+ fb = true;
82
+
83
+ return solveFallback({ salt, target });
84
+ });
85
+
86
+ if (fb) return;
87
+ }
88
+
89
+ try {
90
+ const startTime = performance.now();
91
+ const nonce = solve_pow_function(salt, target);
92
+ const endTime = performance.now();
93
+
94
+ self.postMessage({
95
+ nonce: Number(nonce),
96
+ found: true,
97
+ durationMs: (endTime - startTime).toFixed(2),
98
+ });
99
+ } catch (error) {
100
+ console.error("[cap worker]", error);
101
+
102
+ self.postMessage({
103
+ found: false,
104
+ error: error.message || String(error),
105
+ });
106
+ }
107
+ };
108
+
109
+ self.onerror = (error) => {
110
+ self.postMessage({
111
+ found: false,
112
+ error,
113
+ });
114
+ };
109
115
  })();