@xyo-network/hash 2.90.17 → 2.90.19
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/browser/PayloadHasher.d.cts +2 -0
- package/dist/browser/PayloadHasher.d.cts.map +1 -1
- package/dist/browser/PayloadHasher.d.mts +2 -0
- package/dist/browser/PayloadHasher.d.mts.map +1 -1
- package/dist/browser/PayloadHasher.d.ts +2 -0
- package/dist/browser/PayloadHasher.d.ts.map +1 -1
- package/dist/browser/index-browser.cjs +51 -10
- package/dist/browser/index-browser.cjs.map +1 -1
- package/dist/browser/index-browser.js +51 -10
- package/dist/browser/index-browser.js.map +1 -1
- package/dist/node/PayloadHasher.d.cts +2 -0
- package/dist/node/PayloadHasher.d.cts.map +1 -1
- package/dist/node/PayloadHasher.d.mts +2 -0
- package/dist/node/PayloadHasher.d.mts.map +1 -1
- package/dist/node/PayloadHasher.d.ts +2 -0
- package/dist/node/PayloadHasher.d.ts.map +1 -1
- package/dist/node/index.cjs +51 -10
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +51 -10
- package/dist/node/index.js.map +1 -1
- package/package.json +2 -2
- package/src/PayloadHasher.ts +54 -9
- package/src/globals.d.ts +6 -0
package/src/PayloadHasher.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { assertEx } from '@xylabs/assert'
|
|
2
2
|
import { asHash, Hash, hexFromArrayBuffer } from '@xylabs/hex'
|
|
3
3
|
import { EmptyObject, ObjectWrapper } from '@xylabs/object'
|
|
4
|
+
import { subtle } from '@xylabs/platform'
|
|
4
5
|
import { WasmSupport } from '@xyo-network/wasm'
|
|
6
|
+
import { sha256 } from 'hash-wasm'
|
|
5
7
|
import shajs from 'sha.js'
|
|
6
8
|
import { ModuleThread, Pool, spawn, Worker } from 'threads'
|
|
7
9
|
// eslint-disable-next-line import/no-internal-modules
|
|
@@ -15,10 +17,19 @@ import { jsHashFunc, subtleHashFunc, wasmHashFunc } from './worker'
|
|
|
15
17
|
const wasmSupportStatic = new WasmSupport(['bigInt'])
|
|
16
18
|
|
|
17
19
|
export class PayloadHasher<T extends EmptyObject = EmptyObject> extends ObjectWrapper<T> {
|
|
20
|
+
static allowHashPooling = true
|
|
18
21
|
static allowSubtle = true
|
|
19
22
|
static createBrowserWorker?: (url?: URL) => Worker | undefined
|
|
20
23
|
static createNodeWorker?: (func?: () => unknown) => Worker | undefined
|
|
21
24
|
|
|
25
|
+
static initialized = (() => {
|
|
26
|
+
globalThis.xyo = globalThis.xyo ?? {}
|
|
27
|
+
if (globalThis.xyo.hashing) {
|
|
28
|
+
console.warn('Two static instances of PayloadHasher detected')
|
|
29
|
+
}
|
|
30
|
+
globalThis.xyo === globalThis.xyo ?? { hashing: PayloadHasher }
|
|
31
|
+
})()
|
|
32
|
+
|
|
22
33
|
static jsHashWorkerUrl?: URL
|
|
23
34
|
static subtleHashWorkerUrl?: URL
|
|
24
35
|
|
|
@@ -29,23 +40,52 @@ export class PayloadHasher<T extends EmptyObject = EmptyObject> extends ObjectWr
|
|
|
29
40
|
static readonly wasmInitialized = wasmSupportStatic.initialize()
|
|
30
41
|
static readonly wasmSupport = wasmSupportStatic
|
|
31
42
|
|
|
43
|
+
// These get set to null if they fail to create and then we just don't use workers - needed for storybook
|
|
32
44
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
-
private static _jsHashPool?: Pool<ModuleThread<WorkerModule<any>>>
|
|
45
|
+
private static _jsHashPool?: Pool<ModuleThread<WorkerModule<any>>> | null
|
|
34
46
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
-
private static _subtleHashPool?: Pool<ModuleThread<WorkerModule<any>>>
|
|
47
|
+
private static _subtleHashPool?: Pool<ModuleThread<WorkerModule<any>>> | null
|
|
36
48
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
-
private static _wasmHashPool?: Pool<ModuleThread<WorkerModule<any>>>
|
|
49
|
+
private static _wasmHashPool?: Pool<ModuleThread<WorkerModule<any>>> | null
|
|
38
50
|
|
|
39
51
|
private static get jsHashPool() {
|
|
40
|
-
|
|
52
|
+
if (!this.allowHashPooling || this._jsHashPool === null) {
|
|
53
|
+
return null
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
return (this._jsHashPool = this._jsHashPool ?? this.jsHashWorkerUrl ? this.createWorkerPool(this.jsHashWorkerUrl, jsHashFunc) : null)
|
|
57
|
+
} catch {
|
|
58
|
+
console.warn('Creating js hash worker failed')
|
|
59
|
+
this._jsHashPool = null
|
|
60
|
+
return null
|
|
61
|
+
}
|
|
41
62
|
}
|
|
42
63
|
|
|
43
64
|
private static get subtleHashPool() {
|
|
44
|
-
|
|
65
|
+
if (!this.allowHashPooling || this._subtleHashPool === null) {
|
|
66
|
+
return null
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
return (this._subtleHashPool =
|
|
70
|
+
this._subtleHashPool ?? this.subtleHashWorkerUrl ? this.createWorkerPool(this.subtleHashWorkerUrl, subtleHashFunc) : null)
|
|
71
|
+
} catch {
|
|
72
|
+
console.warn('Creating subtle hash worker failed')
|
|
73
|
+
this._subtleHashPool = null
|
|
74
|
+
return null
|
|
75
|
+
}
|
|
45
76
|
}
|
|
46
77
|
|
|
47
78
|
private static get wasmHashPool() {
|
|
48
|
-
|
|
79
|
+
if (!this.allowHashPooling || this._wasmHashPool === null) {
|
|
80
|
+
return null
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
return (this._wasmHashPool = this._wasmHashPool ?? this.wasmHashWorkerUrl ? this.createWorkerPool(this.wasmHashWorkerUrl, wasmHashFunc) : null)
|
|
84
|
+
} catch {
|
|
85
|
+
console.warn('Creating wasm hash worker failed')
|
|
86
|
+
this._wasmHashPool = null
|
|
87
|
+
return null
|
|
88
|
+
}
|
|
49
89
|
}
|
|
50
90
|
|
|
51
91
|
static createWorker(url?: URL, func?: () => unknown) {
|
|
@@ -131,7 +171,10 @@ export class PayloadHasher<T extends EmptyObject = EmptyObject> extends ObjectWr
|
|
|
131
171
|
if (PayloadHasher.warnIfUsingJsHash) {
|
|
132
172
|
console.warn('Using jsHash [No subtle or wasm?]')
|
|
133
173
|
}
|
|
134
|
-
|
|
174
|
+
const pool = this.jsHashPool
|
|
175
|
+
return pool === null
|
|
176
|
+
? asHash(shajs('sha256').update(data).digest().toString('hex'), true)
|
|
177
|
+
: await pool.queue(async (thread) => await thread.hash(data))
|
|
135
178
|
}
|
|
136
179
|
|
|
137
180
|
/**
|
|
@@ -154,11 +197,13 @@ export class PayloadHasher<T extends EmptyObject = EmptyObject> extends ObjectWr
|
|
|
154
197
|
}
|
|
155
198
|
|
|
156
199
|
static async subtleHash(data: Uint8Array): Promise<ArrayBuffer> {
|
|
157
|
-
|
|
200
|
+
const pool = this.subtleHashPool
|
|
201
|
+
return pool === null ? await subtle.digest('SHA-256', data) : await pool.queue(async (thread) => await thread.hash(data))
|
|
158
202
|
}
|
|
159
203
|
|
|
160
204
|
static async wasmHash(data: string) {
|
|
161
|
-
|
|
205
|
+
const pool = this.wasmHashPool
|
|
206
|
+
return pool === null ? asHash(await sha256(data), true) : pool.queue(async (thread) => await thread.hash(data))
|
|
162
207
|
}
|
|
163
208
|
|
|
164
209
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|