@rasmx/hash 1.0.0 → 1.0.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.
Files changed (3) hide show
  1. package/index.d.ts +60 -53
  2. package/index.js +126 -72
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,106 +1,113 @@
1
1
  export type InputData = string | Uint8Array | ArrayBuffer | SharedArrayBuffer;
2
2
 
3
3
  export interface HashFamily {
4
- v224: (d: InputData) => Promise<string>;
5
- v256: (d: InputData) => Promise<string>;
6
- v384: (d: InputData) => Promise<string>;
7
- v512: (d: InputData) => Promise<string>;
8
- v512_224: (d: InputData) => Promise<string>;
9
- v512_256: (d: InputData) => Promise<string>;
4
+ v224: (d: InputData) => string;
5
+ v256: (d: InputData) => string;
6
+ v384: (d: InputData) => string;
7
+ v512: (d: InputData) => string;
8
+ v512_224: (d: InputData) => string;
9
+ v512_256: (d: InputData) => string;
10
10
  }
11
11
 
12
12
  export const hash: {
13
13
  sha2: HashFamily;
14
- sha3: HashFamily;
14
+ sha3: {
15
+ v224: (d: InputData) => string;
16
+ v256: (d: InputData) => string;
17
+ v384: (d: InputData) => string;
18
+ v512: (d: InputData) => string;
19
+ };
15
20
  keccak: HashFamily;
16
- blake3: (d: InputData, key?: InputData) => Promise<string>;
21
+ blake3: (d: InputData, key?: InputData) => string;
17
22
  shake: {
18
- v128: (d: InputData, len: number) => Promise<string>;
19
- v256: (d: InputData, len: number) => Promise<string>;
23
+ v128: (d: InputData, len: number) => string;
24
+ v256: (d: InputData, len: number) => string;
20
25
  };
21
26
  xxh: {
22
- v32: (d: InputData, seed?: number) => Promise<string>;
23
- v64: (d: InputData, seed?: bigint) => Promise<string>;
24
- v3_64: (d: InputData) => Promise<string>;
25
- v3_64_seed: (d: InputData, seed: bigint) => Promise<string>;
26
- v3_128: (d: InputData) => Promise<string>;
27
- v3_128_seed: (d: InputData, seed: bigint) => Promise<string>;
27
+ v32: (d: InputData, seed?: number) => string;
28
+ v64: (d: InputData, seed?: bigint) => string;
29
+ v3_64: (d: InputData) => string;
30
+ v3_64_seed: (d: InputData, seed: bigint) => string;
31
+ v3_128: (d: InputData) => string;
32
+ v3_128_seed: (d: InputData, seed: bigint) => string;
28
33
  };
29
34
  hmac: {
30
- sha256: (key: InputData, d: InputData) => Promise<string>;
31
- sha512: (key: InputData, d: InputData) => Promise<string>;
35
+ sha256: (key: InputData, d: InputData) => string;
36
+ sha512: (key: InputData, d: InputData) => string;
32
37
  };
33
- md5: (d: InputData) => Promise<string>;
34
- stream: (readable: ReadableStream, hasher: any) => Promise<string>;
38
+ md5: (d: InputData) => string;
39
+ stream: (readable: ReadableStream, hasher: IHasher) => Promise<string>;
35
40
  };
36
41
 
37
42
  export interface IHasher {
38
- update(d: InputData): Promise<void>;
39
- finalize(): Promise<string>;
40
- free(): Promise<void>;
43
+ update(d: InputData): void;
44
+ finalize(): string;
45
+ free(): void;
41
46
  }
42
47
 
43
48
  export class IncrementalSha256 implements IHasher {
44
49
  constructor();
45
- update(d: InputData): Promise<void>;
46
- finalize(): Promise<string>;
47
- free(): Promise<void>;
50
+ update(d: InputData): void;
51
+ finalize(): string;
52
+ free(): void;
48
53
  }
49
54
 
50
55
  export class IncrementalSha512 implements IHasher {
51
56
  constructor();
52
- update(d: InputData): Promise<void>;
53
- finalize(): Promise<string>;
54
- free(): Promise<void>;
57
+ update(d: InputData): void;
58
+ finalize(): string;
59
+ free(): void;
55
60
  }
56
61
 
57
62
  export class IncrementalMd5 implements IHasher {
58
63
  constructor();
59
- update(d: InputData): Promise<void>;
60
- finalize(): Promise<string>;
61
- free(): Promise<void>;
64
+ update(d: InputData): void;
65
+ finalize(): string;
66
+ free(): void;
62
67
  }
63
68
 
64
69
  export class IncrementalBlake3 implements IHasher {
65
70
  constructor();
66
- update(d: InputData): Promise<void>;
67
- finalize(): Promise<string>;
68
- free(): Promise<void>;
71
+ update(d: InputData): void;
72
+ finalize(): string;
73
+ free(): void;
69
74
  }
70
75
 
71
76
  export class IncrementalXxh3 {
72
77
  constructor();
73
- update(d: InputData): Promise<void>;
74
- finalize64(): Promise<string>;
75
- finalize128(): Promise<string>;
76
- finalize(): Promise<string>;
77
- free(): Promise<void>;
78
+ update(d: InputData): void;
79
+ finalize64(): string;
80
+ finalize128(): string;
81
+ finalize(): string;
82
+ free(): void;
78
83
  }
79
84
 
80
85
  export class IncrementalXxh32 implements IHasher {
81
86
  constructor(seed?: number);
82
- update(d: InputData): Promise<void>;
83
- finalize(): Promise<string>;
84
- free(): Promise<void>;
87
+ update(d: InputData): void;
88
+ finalize(): string;
89
+ free(): void;
85
90
  }
86
91
 
87
92
  export class IncrementalXxh64 implements IHasher {
88
93
  constructor(seed?: bigint);
89
- update(d: InputData): Promise<void>;
90
- finalize(): Promise<string>;
91
- free(): Promise<void>;
94
+ update(d: InputData): void;
95
+ finalize(): string;
96
+ free(): void;
92
97
  }
93
98
 
94
99
  export class IncrementalSha3_256 implements IHasher {
95
100
  constructor();
96
- update(d: InputData): Promise<void>;
97
- finalize(): Promise<string>;
98
- free(): Promise<void>;
101
+ update(d: InputData): void;
102
+ finalize(): string;
103
+ free(): void;
99
104
  }
100
105
 
101
106
  export class IncrementalKeccak256 implements IHasher {
102
107
  constructor();
103
- update(d: InputData): Promise<void>;
104
- finalize(): Promise<string>;
105
- free(): Promise<void>;
106
- }
108
+ update(d: InputData): void;
109
+ finalize(): string;
110
+ free(): void;
111
+ }
112
+
113
+ export default function init(input?: string | URL | Request): Promise<void>;
package/index.js CHANGED
@@ -1,13 +1,17 @@
1
- import init, * as wasm from './pkg/rasmx_hash.js';
1
+ import initWasm, * as wasm from './pkg/rasmx_hash.js';
2
2
 
3
3
  let ready = false;
4
- async function ensure() {
5
- if (!ready) {
6
- await init();
7
- ready = true;
8
- }
4
+
5
+ export default async function init(input) {
6
+ if (ready) return;
7
+ await initWasm(input);
8
+ ready = true;
9
9
  }
10
10
 
11
+ const check = () => {
12
+ if (!ready) throw new Error("@rasmx/hash: WASM module not initialized. Call 'await init()' once before use.");
13
+ };
14
+
11
15
  function toBuf(data) {
12
16
  if (data instanceof Uint8Array) return data;
13
17
  if (typeof data === 'string') return new TextEncoder().encode(data);
@@ -19,63 +23,72 @@ function toBuf(data) {
19
23
 
20
24
  export const hash = {
21
25
  sha2: {
22
- v224: async (d) => { await ensure(); return wasm.sha2_224(toBuf(d)); },
23
- v256: async (d) => { await ensure(); return wasm.sha2_256(toBuf(d)); },
24
- v384: async (d) => { await ensure(); return wasm.sha2_384(toBuf(d)); },
25
- v512: async (d) => { await ensure(); return wasm.sha2_512(toBuf(d)); },
26
- v512_224: async (d) => { await ensure(); return wasm.sha2_512_224(toBuf(d)); },
27
- v512_256: async (d) => { await ensure(); return wasm.sha2_512_256(toBuf(d)); }
26
+ v224: (d) => { check(); return wasm.sha2_224(toBuf(d)); },
27
+ v256: (d) => { check(); return wasm.sha2_256(toBuf(d)); },
28
+ v384: (d) => { check(); return wasm.sha2_384(toBuf(d)); },
29
+ v512: (d) => { check(); return wasm.sha2_512(toBuf(d)); },
30
+ v512_224: (d) => { check(); return wasm.sha2_512_224(toBuf(d)); },
31
+ v512_256: (d) => { check(); return wasm.sha2_512_256(toBuf(d)); }
28
32
  },
29
33
  sha3: {
30
- v224: async (d) => { await ensure(); return wasm.sha3_224(toBuf(d)); },
31
- v256: async (d) => { await ensure(); return wasm.sha3_256(toBuf(d)); },
32
- v384: async (d) => { await ensure(); return wasm.sha3_384(toBuf(d)); },
33
- v512: async (d) => { await ensure(); return wasm.sha3_512(toBuf(d)); }
34
+ v224: (d) => { check(); return wasm.sha3_224(toBuf(d)); },
35
+ v256: (d) => { check(); return wasm.sha3_256(toBuf(d)); },
36
+ v384: (d) => { check(); return wasm.sha3_384(toBuf(d)); },
37
+ v512: (d) => { check(); return wasm.sha3_512(toBuf(d)); }
34
38
  },
35
39
  keccak: {
36
- v224: async (d) => { await ensure(); return wasm.keccak_224(toBuf(d)); },
37
- v256: async (d) => { await ensure(); return wasm.keccak_256(toBuf(d)); },
38
- v384: async (d) => { await ensure(); return wasm.keccak_384(toBuf(d)); },
39
- v512: async (d) => { await ensure(); return wasm.keccak_512(toBuf(d)); }
40
+ v224: (d) => { check(); return wasm.keccak_224(toBuf(d)); },
41
+ v256: (d) => { check(); return wasm.keccak_256(toBuf(d)); },
42
+ v384: (d) => { check(); return wasm.keccak_384(toBuf(d)); },
43
+ v512: (d) => { check(); return wasm.keccak_512(toBuf(d)); }
40
44
  },
41
45
  shake: {
42
- v128: async (d, len) => { await ensure(); return wasm.shake128(toBuf(d), len); },
43
- v256: async (d, len) => { await ensure(); return wasm.shake256(toBuf(d), len); }
46
+ v128: (d, len) => { check(); return wasm.shake128(toBuf(d), len); },
47
+ v256: (d, len) => { check(); return wasm.shake256(toBuf(d), len); }
44
48
  },
45
- blake3: async (d, key = null) => {
46
- await ensure();
49
+ blake3: (d, key = null) => {
50
+ check();
47
51
  return key ? wasm.blake3_keyed_hex(toBuf(d), toBuf(key)) : wasm.blake3_hex(toBuf(d));
48
52
  },
49
53
  xxh: {
50
- v32: async (d, seed = 0) => { await ensure(); return wasm.xxh32_hex(toBuf(d), seed); },
51
- v64: async (d, seed = 0n) => { await ensure(); return wasm.xxh64_hex(toBuf(d), BigInt(seed)); },
52
- v3_64: async (d) => { await ensure(); return wasm.xxh3_64_hex(toBuf(d)); },
53
- v3_64_seed: async (d, seed) => { await ensure(); return wasm.xxh3_64_seed_hex(toBuf(d), BigInt(seed)); },
54
- v3_128: async (d) => { await ensure(); return wasm.xxh3_128_hex(toBuf(d)); },
55
- v3_128_seed: async (d, seed) => { await ensure(); return wasm.xxh3_128_seed_hex(toBuf(d), BigInt(seed)); }
54
+ v32: (d, seed = 0) => { check(); return wasm.xxh32_hex(toBuf(d), seed); },
55
+ v64: (d, seed = 0n) => { check(); return wasm.xxh64_hex(toBuf(d), BigInt(seed)); },
56
+ v3_64: (d) => { check(); return wasm.xxh3_64_hex(toBuf(d)); },
57
+ v3_64_seed: (d, seed) => { check(); return wasm.xxh3_64_seed_hex(toBuf(d), BigInt(seed)); },
58
+ v3_128: (d) => { check(); return wasm.xxh3_128_hex(toBuf(d)); },
59
+ v3_128_seed: (d, seed) => { check(); return wasm.xxh3_128_seed_hex(toBuf(d), BigInt(seed)); }
56
60
  },
57
61
  hmac: {
58
- sha256: async (key, d) => { await ensure(); return wasm.hmac_sha256(toBuf(key), toBuf(d)); },
59
- sha512: async (key, d) => { await ensure(); return wasm.hmac_sha512(toBuf(key), toBuf(d)); }
62
+ sha256: (key, d) => { check(); return wasm.hmac_sha256(toBuf(key), toBuf(d)); },
63
+ sha512: (key, d) => { check(); return wasm.hmac_sha512(toBuf(key), toBuf(d)); }
60
64
  },
61
- md5: async (d) => { await ensure(); return wasm.md5_hex(toBuf(d)); },
65
+ md5: (d) => { check(); return wasm.md5_hex(toBuf(d)); },
66
+
62
67
  stream: async (readable, hasher) => {
63
68
  const reader = readable.getReader();
64
- while (true) {
65
- const { done, value } = await reader.read();
66
- if (done) break;
67
- await hasher.update(value);
69
+ try {
70
+ while (true) {
71
+ const { done, value } = await reader.read();
72
+ if (done) break;
73
+ hasher.update(value);
74
+ }
75
+ return hasher.finalize();
76
+ } finally {
77
+ if (hasher.free) hasher.free();
68
78
  }
69
- const res = await hasher.finalize();
70
- if (hasher.free) await hasher.free();
71
- return res;
72
79
  }
73
80
  };
74
81
 
75
82
  class BaseIncremental {
76
- constructor() { this.p = ensure(); this.i = null; }
77
- async free() {
78
- await this.p;
83
+ constructor(wasmClassInstance) {
84
+ check();
85
+ this.i = wasmClassInstance;
86
+ }
87
+ update(d) {
88
+ if (!this.i) throw new Error("Hasher already finalized or freed");
89
+ this.i.update(toBuf(d));
90
+ }
91
+ free() {
79
92
  if (this.i) {
80
93
  this.i.free();
81
94
  this.i = null;
@@ -84,57 +97,98 @@ class BaseIncremental {
84
97
  }
85
98
 
86
99
  export class IncrementalSha256 extends BaseIncremental {
87
- constructor() { super(); this.p = this.p.then(() => this.i = new wasm.IncrementalSha256()); }
88
- async update(d) { await this.p; this.i.update(toBuf(d)); }
89
- async finalize() { await this.p; return this.i.finalize(); }
100
+ constructor() { super(new wasm.IncrementalSha256()); }
101
+ finalize() {
102
+ if (!this.i) throw new Error("Hasher already finalized or freed");
103
+ const res = this.i.finalize();
104
+ this.i = null;
105
+ return res;
106
+ }
90
107
  }
91
108
 
92
109
  export class IncrementalSha512 extends BaseIncremental {
93
- constructor() { super(); this.p = this.p.then(() => this.i = new wasm.IncrementalSha512()); }
94
- async update(d) { await this.p; this.i.update(toBuf(d)); }
95
- async finalize() { await this.p; return this.i.finalize(); }
110
+ constructor() { super(new wasm.IncrementalSha512()); }
111
+ finalize() {
112
+ if (!this.i) throw new Error("Hasher already finalized or freed");
113
+ const res = this.i.finalize();
114
+ this.i = null;
115
+ return res;
116
+ }
96
117
  }
97
118
 
98
119
  export class IncrementalSha3_256 extends BaseIncremental {
99
- constructor() { super(); this.p = this.p.then(() => this.i = new wasm.IncrementalSha3_256()); }
100
- async update(d) { await this.p; this.i.update(toBuf(d)); }
101
- async finalize() { await this.p; return this.i.finalize(); }
120
+ constructor() { super(new wasm.IncrementalSha3_256()); }
121
+ finalize() {
122
+ if (!this.i) throw new Error("Hasher already finalized or freed");
123
+ const res = this.i.finalize();
124
+ this.i = null;
125
+ return res;
126
+ }
102
127
  }
103
128
 
104
129
  export class IncrementalKeccak256 extends BaseIncremental {
105
- constructor() { super(); this.p = this.p.then(() => this.i = new wasm.IncrementalKeccak256()); }
106
- async update(d) { await this.p; this.i.update(toBuf(d)); }
107
- async finalize() { await this.p; return this.i.finalize(); }
130
+ constructor() { super(new wasm.IncrementalKeccak256()); }
131
+ finalize() {
132
+ if (!this.i) throw new Error("Hasher already finalized or freed");
133
+ const res = this.i.finalize();
134
+ this.i = null;
135
+ return res;
136
+ }
108
137
  }
109
138
 
110
139
  export class IncrementalMd5 extends BaseIncremental {
111
- constructor() { super(); this.p = this.p.then(() => this.i = new wasm.IncrementalMd5()); }
112
- async update(d) { await this.p; this.i.update(toBuf(d)); }
113
- async finalize() { await this.p; return this.i.finalize(); }
140
+ constructor() { super(new wasm.IncrementalMd5()); }
141
+ finalize() {
142
+ if (!this.i) throw new Error("Hasher already finalized or freed");
143
+ const res = this.i.finalize();
144
+ this.i = null;
145
+ return res;
146
+ }
114
147
  }
115
148
 
116
149
  export class IncrementalBlake3 extends BaseIncremental {
117
- constructor() { super(); this.p = this.p.then(() => this.i = new wasm.IncrementalBlake3()); }
118
- async update(d) { await this.p; this.i.update(toBuf(d)); }
119
- async finalize() { await this.p; return this.i.finalize(); }
150
+ constructor() { super(new wasm.IncrementalBlake3()); }
151
+ finalize() {
152
+ if (!this.i) throw new Error("Hasher already finalized or freed");
153
+ const res = this.i.finalize();
154
+ this.i = null;
155
+ return res;
156
+ }
120
157
  }
121
158
 
122
159
  export class IncrementalXxh3 extends BaseIncremental {
123
- constructor() { super(); this.p = this.p.then(() => this.i = new wasm.IncrementalXxh3()); }
124
- async update(d) { await this.p; this.i.update(toBuf(d)); }
125
- async finalize64() { await this.p; return this.i.finalize_64(); }
126
- async finalize128() { await this.p; return this.i.finalize_128(); }
127
- async finalize() { return this.finalize64(); }
160
+ constructor() { super(new wasm.IncrementalXxh3()); }
161
+ finalize64() {
162
+ if (!this.i) throw new Error("Hasher already finalized or freed");
163
+ const res = this.i.finalize_64();
164
+ this.i = null;
165
+ return res;
166
+ }
167
+ finalize128() {
168
+ if (!this.i) throw new Error("Hasher already finalized or freed");
169
+ const res = this.i.finalize_128();
170
+ this.i = null;
171
+ return res;
172
+ }
173
+ finalize() { return this.finalize64(); }
128
174
  }
129
175
 
130
176
  export class IncrementalXxh32 extends BaseIncremental {
131
- constructor(seed = 0) { super(); this.p = this.p.then(() => this.i = new wasm.IncrementalXxh32(seed)); }
132
- async update(d) { await this.p; this.i.update(toBuf(d)); }
133
- async finalize() { await this.p; return this.i.finalize(); }
177
+ constructor(seed = 0) { super(new wasm.IncrementalXxh32(seed)); }
178
+ finalize() {
179
+ if (!this.i) throw new Error("Hasher already finalized or freed");
180
+ const res = this.i.finalize();
181
+ this.i = null;
182
+ return res;
183
+ }
134
184
  }
135
185
 
136
186
  export class IncrementalXxh64 extends BaseIncremental {
137
- constructor(seed = 0n) { super(); this.p = this.p.then(() => this.i = new wasm.IncrementalXxh64(BigInt(seed))); }
138
- async update(d) { await this.p; this.i.update(toBuf(d)); }
139
- async finalize() { await this.p; return this.i.finalize(); }
187
+ constructor(seed = 0n) { super(new wasm.IncrementalXxh64(BigInt(seed))); }
188
+ finalize() {
189
+ if (!this.i) throw new Error("Hasher already finalized or freed");
190
+ const res = this.i.finalize();
191
+ this.i = null;
192
+ return res;
193
+ }
140
194
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rasmx/hash",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Blazing fast WASM hashing library powered by Rust. Support for SHA2, SHA3, Keccak, XXHash, and MD5 with streaming support.",
5
5
  "publishConfig": {
6
6
  "access": "public"