@lumeweb/sia-testing 0.0.0-stage → 0.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 (90) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +202 -2
  3. package/dist/esm/client.d.ts +8 -0
  4. package/dist/esm/client.js +6 -0
  5. package/dist/esm/http.d.ts +3 -0
  6. package/dist/esm/http.js +3 -0
  7. package/dist/esm/mock.d.ts +8 -0
  8. package/dist/esm/mock.js +7 -0
  9. package/dist/esm/model/index.js +509 -0
  10. package/dist/esm/model/state.js +188 -0
  11. package/dist/esm/msw.d.ts +2 -0
  12. package/dist/esm/msw.js +3 -0
  13. package/dist/esm/node.d.ts +4 -0
  14. package/dist/esm/node.js +4 -0
  15. package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/_md.js +169 -0
  16. package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/_u64.js +12 -0
  17. package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/sha2.js +199 -0
  18. package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/utils.js +220 -0
  19. package/dist/esm/sdk/abort.js +23 -0
  20. package/dist/esm/sdk/control.js +129 -0
  21. package/dist/esm/sdk/index.js +454 -0
  22. package/dist/esm/sdk/link.js +55 -0
  23. package/dist/esm/sdk/object.js +24 -0
  24. package/dist/esm/sdk/payload-store.d.ts +9 -0
  25. package/dist/esm/sdk/payload-store.js +67 -0
  26. package/dist/esm/sdk/state.js +74 -0
  27. package/dist/esm/testing/application-client.d.ts +8 -0
  28. package/dist/esm/testing/application-client.js +295 -0
  29. package/dist/esm/testing/application-metadata.js +17 -0
  30. package/dist/esm/testing/application-transport.js +363 -0
  31. package/dist/esm/testing/auth-client.d.ts +20 -0
  32. package/dist/esm/testing/auth-client.js +63 -0
  33. package/dist/esm/testing/authentication.js +48 -0
  34. package/dist/esm/testing/byte-view.js +28 -0
  35. package/dist/esm/testing/chunked-files.d.ts +12 -0
  36. package/dist/esm/testing/chunked-files.js +50 -0
  37. package/dist/esm/testing/client.d.ts +8 -0
  38. package/dist/esm/testing/client.js +105 -0
  39. package/dist/esm/testing/controls.d.ts +44 -0
  40. package/dist/esm/testing/controls.js +255 -0
  41. package/dist/esm/testing/diagnostics.js +55 -0
  42. package/dist/esm/testing/files.d.ts +9 -0
  43. package/dist/esm/testing/files.js +86 -0
  44. package/dist/esm/testing/http-body.js +33 -0
  45. package/dist/esm/testing/http.d.ts +14 -0
  46. package/dist/esm/testing/http.js +181 -0
  47. package/dist/esm/testing/live/agent.d.ts +1 -0
  48. package/dist/esm/testing/live/agent.js +211 -0
  49. package/dist/esm/testing/live/approval.js +42 -0
  50. package/dist/esm/testing/live/cleanup.js +74 -0
  51. package/dist/esm/testing/live/journal.js +180 -0
  52. package/dist/esm/testing/live/lease.js +56 -0
  53. package/dist/esm/testing/live/private-files.js +50 -0
  54. package/dist/esm/testing/live/process.js +208 -0
  55. package/dist/esm/testing/live/profile.js +214 -0
  56. package/dist/esm/testing/live/recovery.js +164 -0
  57. package/dist/esm/testing/live/resource-limits.js +9 -0
  58. package/dist/esm/testing/live/run.js +390 -0
  59. package/dist/esm/testing/live/session.js +93 -0
  60. package/dist/esm/testing/live/types.d.ts +8 -0
  61. package/dist/esm/testing/mock.d.ts +8 -0
  62. package/dist/esm/testing/mock.js +1364 -0
  63. package/dist/esm/testing/msw.d.ts +21 -0
  64. package/dist/esm/testing/msw.js +61 -0
  65. package/dist/esm/testing/mutations.d.ts +59 -0
  66. package/dist/esm/testing/mutations.js +58 -0
  67. package/dist/esm/testing/node-payload-store.js +353 -0
  68. package/dist/esm/testing/node.d.ts +40 -0
  69. package/dist/esm/testing/node.js +148 -0
  70. package/dist/esm/testing/owner-client.d.ts +9 -0
  71. package/dist/esm/testing/owner-client.js +101 -0
  72. package/dist/esm/testing/owner-shares.js +80 -0
  73. package/dist/esm/testing/packed.d.ts +46 -0
  74. package/dist/esm/testing/packed.js +326 -0
  75. package/dist/esm/testing/reader-lifetime.js +40 -0
  76. package/dist/esm/testing/reply-loss.js +14 -0
  77. package/dist/esm/testing/runtime.d.ts +7 -0
  78. package/dist/esm/testing/runtime.js +13 -0
  79. package/dist/esm/testing/sdk-upload.d.ts +45 -0
  80. package/dist/esm/testing/sdk-upload.js +198 -0
  81. package/dist/esm/testing/streaming-source.js +72 -0
  82. package/dist/esm/testing/transport.d.ts +22 -0
  83. package/dist/esm/testing/transport.js +238 -0
  84. package/dist/esm/testing/types.d.ts +276 -0
  85. package/dist/esm/vitest.d.ts +23 -0
  86. package/dist/esm/vitest.js +34 -0
  87. package/docs/live-auth.md +154 -0
  88. package/docs/live-runs.md +188 -0
  89. package/docs/mock-contracts.md +647 -0
  90. package/package.json +91 -3
@@ -0,0 +1,4 @@
1
+ import { LiveRun, LiveRunSummary, MockRun, RunLimits, RunResult, RunSummary, SiaRun, TestFile, TestObject, TestShare } from "./testing/types.js";
2
+ import { chunkedFixture } from "./testing/chunked-files.js";
3
+ import { LiveNodeRunOptions, MockNodeRunOptions, RecoveryOptions, RunOptions, fileFixture, recoverSiaRun, withSiaRun } from "./testing/node.js";
4
+ export { type LiveNodeRunOptions, type LiveRun, type LiveRunSummary, type MockNodeRunOptions, type MockRun, type RecoveryOptions, type RunLimits, type RunOptions, type RunResult, type RunSummary, type SiaRun, type TestFile, type TestObject, type TestShare, chunkedFixture, fileFixture, recoverSiaRun, withSiaRun };
@@ -0,0 +1,4 @@
1
+ import { chunkedFixture } from "./testing/chunked-files.js";
2
+ import { fileFixture, recoverSiaRun, withSiaRun } from "./testing/node.js";
3
+
4
+ export { chunkedFixture, fileFixture, recoverSiaRun, withSiaRun };
@@ -0,0 +1,169 @@
1
+ import { setU64FromNum } from "./_u64.js";
2
+ import { abytes, aexists, aoutput, createView } from "./utils.js";
3
+
4
+ //#region ../../node_modules/.pnpm/@noble+hashes@2.4.0/node_modules/@noble/hashes/_md.js
5
+ /**
6
+ * Internal Merkle-Damgard hash utils.
7
+ * @module
8
+ */
9
+ /**
10
+ * Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
11
+ * Returns bits from `b` when `a` is set, otherwise from `c`.
12
+ * The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
13
+ * set the same bit.
14
+ * @param a - selector word
15
+ * @param b - word chosen when selector bit is set
16
+ * @param c - word chosen when selector bit is clear
17
+ * @returns Mixed 32-bit word.
18
+ * @example
19
+ * Combine three words with the shared 32-bit choice primitive.
20
+ * ```ts
21
+ * Chi(0xffffffff, 0x12345678, 0x87654321);
22
+ * ```
23
+ */
24
+ function Chi(a, b, c) {
25
+ return a & b ^ ~a & c;
26
+ }
27
+ /**
28
+ * Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
29
+ * Returns bits shared by at least two inputs.
30
+ * @param a - first input word
31
+ * @param b - second input word
32
+ * @param c - third input word
33
+ * @returns Mixed 32-bit word.
34
+ * @example
35
+ * Combine three words with the shared 32-bit majority primitive.
36
+ * ```ts
37
+ * Maj(0xffffffff, 0x12345678, 0x87654321);
38
+ * ```
39
+ */
40
+ function Maj(a, b, c) {
41
+ return a & b ^ a & c ^ b & c;
42
+ }
43
+ /**
44
+ * Merkle-Damgard hash construction base class.
45
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
46
+ * Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
47
+ * strings with partial-byte tails.
48
+ * @param blockLen - internal block size in bytes
49
+ * @param outputLen - digest size in bytes
50
+ * @param padOffset - trailing length field size in bytes
51
+ * @param isLE - whether length and state words are encoded in little-endian
52
+ * @example
53
+ * Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
54
+ * ```ts
55
+ * import { _SHA1 } from '@noble/hashes/legacy.js';
56
+ * const hash = new _SHA1();
57
+ * hash.update(new Uint8Array([97, 98, 99]));
58
+ * hash.digest();
59
+ * ```
60
+ */
61
+ var HashMD = class {
62
+ blockLen;
63
+ outputLen;
64
+ canXOF = false;
65
+ padOffset;
66
+ isLE;
67
+ buffer;
68
+ view;
69
+ finished = false;
70
+ length = 0;
71
+ pos = 0;
72
+ destroyed = false;
73
+ constructor(blockLen, outputLen, padOffset, isLE) {
74
+ this.blockLen = blockLen;
75
+ this.outputLen = outputLen;
76
+ this.padOffset = padOffset;
77
+ this.isLE = isLE;
78
+ this.buffer = new Uint8Array(blockLen);
79
+ this.view = createView(this.buffer);
80
+ }
81
+ update(data) {
82
+ aexists(this);
83
+ abytes(data);
84
+ const { view, buffer, blockLen } = this;
85
+ const len = data.length;
86
+ let processed = false;
87
+ for (let pos = 0; pos < len;) {
88
+ const take = Math.min(blockLen - this.pos, len - pos);
89
+ if (take === blockLen) {
90
+ const dataView = createView(data);
91
+ for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
92
+ processed = true;
93
+ continue;
94
+ }
95
+ buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
96
+ this.pos += take;
97
+ pos += take;
98
+ if (this.pos === blockLen) {
99
+ this.process(view, 0);
100
+ this.pos = 0;
101
+ processed = true;
102
+ }
103
+ }
104
+ this.length += data.length;
105
+ if (processed) this.roundClean();
106
+ return this;
107
+ }
108
+ digestInto(out) {
109
+ aexists(this);
110
+ aoutput(out, this);
111
+ this.finished = true;
112
+ const { buffer, view, blockLen, isLE } = this;
113
+ let { pos } = this;
114
+ buffer[pos++] = 128;
115
+ buffer.fill(0, pos);
116
+ if (this.padOffset > blockLen - pos) {
117
+ this.process(view, 0);
118
+ buffer.fill(0);
119
+ }
120
+ setU64FromNum(view, blockLen - 8, this.length * 8, isLE);
121
+ this.process(view, 0);
122
+ this.roundClean();
123
+ const oview = out === buffer ? view : createView(out);
124
+ const len = this.outputLen;
125
+ const outLen = len / 4;
126
+ const state = this.get();
127
+ if (len % 4 || outLen > state.length) throw new Error("invalid outputLen");
128
+ for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
129
+ }
130
+ digest() {
131
+ const { buffer, outputLen } = this;
132
+ this.digestInto(buffer);
133
+ const res = buffer.slice(0, outputLen);
134
+ this.destroy();
135
+ return res;
136
+ }
137
+ _cloneIntoMeta(to) {
138
+ const { buffer, length, finished, destroyed, pos } = this;
139
+ to.destroyed = destroyed;
140
+ to.finished = finished;
141
+ to.length = length;
142
+ to.pos = pos;
143
+ if (pos) to.buffer.set(buffer);
144
+ return to;
145
+ }
146
+ clone() {
147
+ return this._cloneInto();
148
+ }
149
+ };
150
+ /**
151
+ * Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
152
+ * Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
153
+ */
154
+ /** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
155
+ * square roots of the first eight prime numbers. Exported as a shared table; callers must treat
156
+ * it as read-only because constructors copy words from it by index. */
157
+ const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
158
+ 1779033703,
159
+ 3144134277,
160
+ 1013904242,
161
+ 2773480762,
162
+ 1359893119,
163
+ 2600822924,
164
+ 528734635,
165
+ 1541459225
166
+ ]);
167
+
168
+ //#endregion
169
+ export { Chi, HashMD, Maj, SHA256_IV };
@@ -0,0 +1,12 @@
1
+ //#region ../../node_modules/.pnpm/@noble+hashes@2.4.0/node_modules/@noble/hashes/_u64.js
2
+ const fromNumH = (n) => n / 2 ** 32 | 0;
3
+ const fromNumL = (n) => n >>> 0;
4
+ function setU64FromNum(view, byteOffset, n, isLE) {
5
+ const h = fromNumH(n);
6
+ const l = fromNumL(n);
7
+ view.setUint32(byteOffset, isLE ? l : h, isLE);
8
+ view.setUint32(byteOffset + 4, isLE ? h : l, isLE);
9
+ }
10
+
11
+ //#endregion
12
+ export { fromNumH, fromNumL, setU64FromNum };
@@ -0,0 +1,199 @@
1
+ import { clean, createHasher, oidNist, rotr } from "./utils.js";
2
+ import { Chi, HashMD, Maj, SHA256_IV } from "./_md.js";
3
+
4
+ //#region ../../node_modules/.pnpm/@noble+hashes@2.4.0/node_modules/@noble/hashes/sha2.js
5
+ /**
6
+ * SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
7
+ * SHA256 is the fastest hash implementable in JS, even faster than Blake3.
8
+ * Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
9
+ * {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
10
+ * @module
11
+ */
12
+ /**
13
+ * SHA-224 / SHA-256 round constants from RFC 6234 §5.1: the first 32 bits
14
+ * of the cube roots of the first 64 primes (2..311).
15
+ */
16
+ const SHA256_K = /* @__PURE__ */ Uint32Array.from([
17
+ 1116352408,
18
+ 1899447441,
19
+ 3049323471,
20
+ 3921009573,
21
+ 961987163,
22
+ 1508970993,
23
+ 2453635748,
24
+ 2870763221,
25
+ 3624381080,
26
+ 310598401,
27
+ 607225278,
28
+ 1426881987,
29
+ 1925078388,
30
+ 2162078206,
31
+ 2614888103,
32
+ 3248222580,
33
+ 3835390401,
34
+ 4022224774,
35
+ 264347078,
36
+ 604807628,
37
+ 770255983,
38
+ 1249150122,
39
+ 1555081692,
40
+ 1996064986,
41
+ 2554220882,
42
+ 2821834349,
43
+ 2952996808,
44
+ 3210313671,
45
+ 3336571891,
46
+ 3584528711,
47
+ 113926993,
48
+ 338241895,
49
+ 666307205,
50
+ 773529912,
51
+ 1294757372,
52
+ 1396182291,
53
+ 1695183700,
54
+ 1986661051,
55
+ 2177026350,
56
+ 2456956037,
57
+ 2730485921,
58
+ 2820302411,
59
+ 3259730800,
60
+ 3345764771,
61
+ 3516065817,
62
+ 3600352804,
63
+ 4094571909,
64
+ 275423344,
65
+ 430227734,
66
+ 506948616,
67
+ 659060556,
68
+ 883997877,
69
+ 958139571,
70
+ 1322822218,
71
+ 1537002063,
72
+ 1747873779,
73
+ 1955562222,
74
+ 2024104815,
75
+ 2227730452,
76
+ 2361852424,
77
+ 2428436474,
78
+ 2756734187,
79
+ 3204031479,
80
+ 3329325298
81
+ ]);
82
+ /** Reusable SHA-224 / SHA-256 message schedule buffer `W_t` from RFC 6234 §6.2 step 1. */
83
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
84
+ /** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
85
+ var SHA2_32B = class extends HashMD {
86
+ A = 0;
87
+ B = 0;
88
+ C = 0;
89
+ D = 0;
90
+ E = 0;
91
+ F = 0;
92
+ G = 0;
93
+ H = 0;
94
+ constructor(outputLen, IV) {
95
+ super(64, outputLen, 8, false);
96
+ this.A = IV[0] | 0;
97
+ this.B = IV[1] | 0;
98
+ this.C = IV[2] | 0;
99
+ this.D = IV[3] | 0;
100
+ this.E = IV[4] | 0;
101
+ this.F = IV[5] | 0;
102
+ this.G = IV[6] | 0;
103
+ this.H = IV[7] | 0;
104
+ }
105
+ get() {
106
+ const { A, B, C, D, E, F, G, H } = this;
107
+ return [
108
+ A,
109
+ B,
110
+ C,
111
+ D,
112
+ E,
113
+ F,
114
+ G,
115
+ H
116
+ ];
117
+ }
118
+ set(A, B, C, D, E, F, G, H) {
119
+ this.A = A | 0;
120
+ this.B = B | 0;
121
+ this.C = C | 0;
122
+ this.D = D | 0;
123
+ this.E = E | 0;
124
+ this.F = F | 0;
125
+ this.G = G | 0;
126
+ this.H = H | 0;
127
+ }
128
+ _cloneInto(to) {
129
+ (to ||= new this.constructor()).set(...this.get());
130
+ return this._cloneIntoMeta(to);
131
+ }
132
+ process(view, offset) {
133
+ for (let i = 0; i < 16; i++, offset += 4) SHA256_W[i] = view.getUint32(offset, false);
134
+ for (let i = 16; i < 64; i++) {
135
+ const W15 = SHA256_W[i - 15];
136
+ const W2 = SHA256_W[i - 2];
137
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
138
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
139
+ SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
140
+ }
141
+ let { A, B, C, D, E, F, G, H } = this;
142
+ for (let i = 0; i < 64; i++) {
143
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
144
+ const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
145
+ const T2 = (rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22)) + Maj(A, B, C) | 0;
146
+ H = G;
147
+ G = F;
148
+ F = E;
149
+ E = D + T1 | 0;
150
+ D = C;
151
+ C = B;
152
+ B = A;
153
+ A = T1 + T2 | 0;
154
+ }
155
+ A = A + this.A | 0;
156
+ B = B + this.B | 0;
157
+ C = C + this.C | 0;
158
+ D = D + this.D | 0;
159
+ E = E + this.E | 0;
160
+ F = F + this.F | 0;
161
+ G = G + this.G | 0;
162
+ H = H + this.H | 0;
163
+ this.set(A, B, C, D, E, F, G, H);
164
+ }
165
+ roundClean() {
166
+ clean(SHA256_W);
167
+ }
168
+ destroy() {
169
+ this.destroyed = true;
170
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
171
+ clean(this.buffer);
172
+ }
173
+ };
174
+ /** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
175
+ var _SHA256 = class extends SHA2_32B {
176
+ constructor() {
177
+ super(32, SHA256_IV);
178
+ }
179
+ };
180
+ /**
181
+ * SHA2-256 hash function from RFC 4634. In JS it's the fastest: even faster than Blake3. Some info:
182
+ *
183
+ * - Trying 2^128 hashes would get 50% chance of collision, using birthday attack.
184
+ * - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
185
+ * - Each sha256 hash is executing 2^18 bit operations.
186
+ * - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
187
+ * @param msg - message bytes to hash
188
+ * @param opts - Reserved hash options.
189
+ * @returns Digest bytes.
190
+ * @example
191
+ * Hash a message with SHA2-256.
192
+ * ```ts
193
+ * sha256(new Uint8Array([97, 98, 99]));
194
+ * ```
195
+ */
196
+ const sha256 = /* @__PURE__ */ createHasher(() => new _SHA256(), /* @__PURE__ */ oidNist(1));
197
+
198
+ //#endregion
199
+ export { _SHA256, sha256 };
@@ -0,0 +1,220 @@
1
+ //#region ../../node_modules/.pnpm/@noble+hashes@2.4.0/node_modules/@noble/hashes/utils.js
2
+ /**
3
+ * Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
4
+ * @param a - value to test
5
+ * @returns `true` when the value is a Uint8Array-compatible view.
6
+ * @example
7
+ * Check whether a value is a Uint8Array-compatible view.
8
+ * ```ts
9
+ * isBytes(new Uint8Array([1, 2, 3]));
10
+ * ```
11
+ */
12
+ function isBytes(a) {
13
+ return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
14
+ }
15
+ const atitle = (title) => title ? `"${title}" ` : "";
16
+ /**
17
+ * Asserts something is a non-negative integer.
18
+ * @param n - number to validate
19
+ * @param title - label included in thrown errors
20
+ * @returns The validated number.
21
+ * @throws On wrong argument types. {@link TypeError}
22
+ * @throws On wrong argument ranges or values. {@link RangeError}
23
+ * @example
24
+ * Validate a non-negative integer option.
25
+ * ```ts
26
+ * anumber(32, 'length');
27
+ * ```
28
+ */
29
+ function anumber(n, title = "") {
30
+ if (typeof n !== "number") throw new TypeError(atitle(title) + "expected number, got " + typeof n);
31
+ if (!Number.isSafeInteger(n) || n < 0) throw new RangeError(atitle(title) + "expected integer >= 0, got " + n);
32
+ return n;
33
+ }
34
+ /**
35
+ * Asserts something is Uint8Array.
36
+ * @param value - value to validate
37
+ * @param length - optional exact length constraint
38
+ * @param title - label included in thrown errors
39
+ * @returns The validated byte array.
40
+ * @throws On wrong argument types. {@link TypeError}
41
+ * @throws On wrong argument ranges or values. {@link RangeError}
42
+ * @example
43
+ * Validate that a value is a byte array.
44
+ * ```ts
45
+ * abytes(new Uint8Array([1, 2, 3]));
46
+ * ```
47
+ */
48
+ function abytes(value, length, title = "") {
49
+ if (isBytes(value) && (length === void 0 || value.length === length)) return value;
50
+ if (length !== void 0) anumber(length, "length");
51
+ const bytes = isBytes(value);
52
+ const ofLen = length !== void 0 ? ` of length ${length}` : "";
53
+ const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
54
+ const message = atitle(title) + "expected Uint8Array" + ofLen + ", got " + got;
55
+ if (!bytes) throw new TypeError(message);
56
+ throw new RangeError(message);
57
+ }
58
+ const aobject = (value, label) => {
59
+ if (value === null || typeof value !== "object" || Array.isArray(value)) throw new TypeError((label === "object" ? "" : `"${label}" `) + "expected object, got type=" + typeof value);
60
+ };
61
+ const aopts = (value, label) => {
62
+ aobject(value, label);
63
+ const proto = Object.getPrototypeOf(value);
64
+ if (proto !== Object.prototype && proto !== null) throw new TypeError(`"${label}" expected plain object`);
65
+ if (Object.hasOwn(value, "__proto__")) throw new TypeError(`"${label}.__proto__" is not allowed`);
66
+ };
67
+ /**
68
+ * Asserts a hash instance has not been destroyed or finished.
69
+ * @param instance - hash instance to validate
70
+ * @param checkFinished - whether to reject finalized instances
71
+ * @throws If the hash instance has already been destroyed or finalized. {@link Error}
72
+ * @example
73
+ * Validate that a hash instance is still usable.
74
+ * ```ts
75
+ * import { aexists } from '@noble/hashes/utils.js';
76
+ * import { sha256 } from '@noble/hashes/sha2.js';
77
+ * const hash = sha256.create();
78
+ * aexists(hash);
79
+ * ```
80
+ */
81
+ function aexists(instance, checkFinished = true) {
82
+ if (instance.destroyed) throw new Error("hash was destroyed");
83
+ if (checkFinished && instance.finished) throw new Error("digest() was already called");
84
+ }
85
+ /**
86
+ * Asserts output is a sufficiently-sized byte array.
87
+ * @param out - destination buffer
88
+ * @param instance - hash instance providing output length
89
+ * Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
90
+ * @throws On wrong argument types. {@link TypeError}
91
+ * @throws On wrong argument ranges or values. {@link RangeError}
92
+ * @example
93
+ * Validate a caller-provided digest buffer.
94
+ * ```ts
95
+ * import { aoutput } from '@noble/hashes/utils.js';
96
+ * import { sha256 } from '@noble/hashes/sha2.js';
97
+ * const hash = sha256.create();
98
+ * aoutput(new Uint8Array(hash.outputLen), hash);
99
+ * ```
100
+ */
101
+ function aoutput(out, instance) {
102
+ abytes(out, void 0, "output");
103
+ const min = instance.outputLen;
104
+ if (!(out.length >= min)) throw new RangeError("\"output\" expected length >= " + min);
105
+ }
106
+ /**
107
+ * Zeroizes typed arrays in place. Warning: JS provides no guarantees.
108
+ * @param arrays - arrays to overwrite with zeros
109
+ * @example
110
+ * Zeroize sensitive buffers in place.
111
+ * ```ts
112
+ * clean(new Uint8Array([1, 2, 3]));
113
+ * ```
114
+ */
115
+ function clean(...arrays) {
116
+ for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
117
+ }
118
+ /**
119
+ * Creates a DataView for byte-level manipulation.
120
+ * @param arr - source typed array
121
+ * @returns DataView over the same buffer region.
122
+ * @example
123
+ * Create a DataView over an existing buffer.
124
+ * ```ts
125
+ * createView(new Uint8Array(4));
126
+ * ```
127
+ */
128
+ function createView(arr) {
129
+ return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
130
+ }
131
+ /**
132
+ * Rotate-right operation for uint32 values.
133
+ * @param word - source word
134
+ * @param shift - shift amount in bits
135
+ * @returns Rotated word.
136
+ * @example
137
+ * Rotate a 32-bit word to the right.
138
+ * ```ts
139
+ * rotr(0x12345678, 8);
140
+ * ```
141
+ */
142
+ function rotr(word, shift) {
143
+ return word << 32 - shift | word >>> shift;
144
+ }
145
+ /**
146
+ * Merges default options and passed options.
147
+ * @param defaults - base option object
148
+ * @param opts - user overrides
149
+ * @param title - label included in thrown override errors
150
+ * @returns Fresh merged option object with a null prototype.
151
+ * @throws On wrong argument types. {@link TypeError}
152
+ * @example
153
+ * Merge user overrides onto default options.
154
+ * ```ts
155
+ * checkOpts({ dkLen: 32 }, { asyncTick: 10 });
156
+ * ```
157
+ */
158
+ function checkOpts(defaults, opts, title = "opts") {
159
+ aopts(defaults, "defaults");
160
+ if (opts !== void 0) aopts(opts, title);
161
+ return Object.assign(Object.create(null), defaults, opts);
162
+ }
163
+ /**
164
+ * Creates a callable hash function from a stateful class constructor.
165
+ * @param hashCons - hash constructor or factory
166
+ * @param info - optional metadata such as DER OID
167
+ * @returns Frozen callable hash wrapper with `.create()`.
168
+ * Wrapper construction eagerly calls `hashCons(undefined)` once to read
169
+ * `outputLen` / `blockLen`, so constructor side effects happen at module
170
+ * init time.
171
+ * @throws On wrong argument types. {@link TypeError}
172
+ * @example
173
+ * Wrap a stateful hash constructor into a callable helper.
174
+ * ```ts
175
+ * import { createHasher } from '@noble/hashes/utils.js';
176
+ * import { sha256 } from '@noble/hashes/sha2.js';
177
+ * const wrapped = createHasher(sha256.create, { oid: sha256.oid });
178
+ * wrapped(new Uint8Array([1]));
179
+ * ```
180
+ */
181
+ function createHasher(hashCons, info = {}) {
182
+ if (typeof hashCons !== "function") throw new TypeError("\"hashCons\" expected function, got type=" + typeof hashCons);
183
+ info = checkOpts({}, info, "info");
184
+ const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
185
+ const tmp = hashCons(void 0);
186
+ hashC.outputLen = tmp.outputLen;
187
+ hashC.blockLen = tmp.blockLen;
188
+ hashC.canXOF = tmp.canXOF;
189
+ hashC.create = (opts) => hashCons(opts);
190
+ Object.assign(hashC, info);
191
+ return Object.freeze(hashC);
192
+ }
193
+ /**
194
+ * Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
195
+ * @param suffix - final OID byte for the selected hash.
196
+ * The helper accepts any byte even though only the documented NIST hash
197
+ * suffixes are meaningful downstream.
198
+ * @returns Object containing the DER-encoded OID.
199
+ * @example
200
+ * Build OID metadata for a NIST hash.
201
+ * ```ts
202
+ * oidNist(0x01);
203
+ * ```
204
+ */
205
+ const oidNist = (suffix) => ({ oid: Uint8Array.from([
206
+ 6,
207
+ 9,
208
+ 96,
209
+ 134,
210
+ 72,
211
+ 1,
212
+ 101,
213
+ 3,
214
+ 4,
215
+ 2,
216
+ suffix
217
+ ]) });
218
+
219
+ //#endregion
220
+ export { abytes, aexists, anumber, aoutput, checkOpts, clean, createHasher, createView, isBytes, oidNist, rotr };
@@ -0,0 +1,23 @@
1
+ //#region src/sdk/abort.ts
2
+ /** Cancels one caller's wait without releasing holds that belong to other callers. */
3
+ function withAbort(work, signal) {
4
+ if (!signal) return work;
5
+ return new Promise((resolve, reject) => {
6
+ const aborted = () => {
7
+ signal.removeEventListener("abort", aborted);
8
+ reject(new DOMException("mock operation cancelled", "AbortError"));
9
+ };
10
+ signal.addEventListener("abort", aborted, { once: true });
11
+ work.then((value) => {
12
+ signal.removeEventListener("abort", aborted);
13
+ resolve(value);
14
+ }, (error) => {
15
+ signal.removeEventListener("abort", aborted);
16
+ reject(error instanceof Error ? error : new Error("mock operation failed", { cause: error }));
17
+ });
18
+ if (signal.aborted) aborted();
19
+ });
20
+ }
21
+
22
+ //#endregion
23
+ export { withAbort };