@waku/core 0.0.26-7eb3375.0 → 0.0.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.
Files changed (54) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/bundle/{base_protocol-46017f51.js → base_protocol-pDODy0G6.js} +22 -230
  3. package/bundle/{browser-9a6558bb.js → browser-mTOOnVZp.js} +80 -1
  4. package/bundle/index-cmONXM-V.js +595 -0
  5. package/bundle/index.js +358 -425
  6. package/bundle/lib/base_protocol.js +3 -3
  7. package/bundle/lib/message/version_0.js +3 -3
  8. package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
  9. package/bundle/{version_0-7190df43.js → version_0-LQTFNC7k.js} +118 -11
  10. package/dist/.tsbuildinfo +1 -1
  11. package/dist/index.d.ts +2 -1
  12. package/dist/index.js +2 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/lib/base_protocol.d.ts +3 -2
  15. package/dist/lib/base_protocol.js +12 -6
  16. package/dist/lib/base_protocol.js.map +1 -1
  17. package/dist/lib/connection_manager.js.map +1 -1
  18. package/dist/lib/filter/index.js +7 -4
  19. package/dist/lib/filter/index.js.map +1 -1
  20. package/dist/lib/filterPeers.js.map +1 -1
  21. package/dist/lib/keep_alive_manager.js +2 -2
  22. package/dist/lib/keep_alive_manager.js.map +1 -1
  23. package/dist/lib/light_push/index.js +1 -2
  24. package/dist/lib/light_push/index.js.map +1 -1
  25. package/dist/lib/message/version_0.d.ts +3 -3
  26. package/dist/lib/message/version_0.js +5 -6
  27. package/dist/lib/message/version_0.js.map +1 -1
  28. package/dist/lib/metadata/index.d.ts +3 -0
  29. package/dist/lib/metadata/index.js +67 -0
  30. package/dist/lib/metadata/index.js.map +1 -0
  31. package/dist/lib/predefined_bootstrap_nodes.js.map +1 -1
  32. package/dist/lib/store/history_rpc.js.map +1 -1
  33. package/dist/lib/store/index.js +1 -2
  34. package/dist/lib/store/index.js.map +1 -1
  35. package/dist/lib/stream_manager.js.map +1 -1
  36. package/dist/lib/wait_for_remote_peer.js.map +1 -1
  37. package/dist/lib/waku.d.ts +3 -3
  38. package/dist/lib/waku.js +12 -6
  39. package/dist/lib/waku.js.map +1 -1
  40. package/package.json +127 -1
  41. package/src/index.ts +3 -1
  42. package/src/lib/base_protocol.ts +29 -7
  43. package/src/lib/filter/index.ts +10 -3
  44. package/src/lib/keep_alive_manager.ts +2 -2
  45. package/src/lib/light_push/index.ts +1 -2
  46. package/src/lib/message/version_0.ts +16 -8
  47. package/src/lib/metadata/index.ts +110 -0
  48. package/src/lib/store/index.ts +1 -2
  49. package/src/lib/waku.ts +14 -4
  50. package/bundle/index-7581d519.js +0 -31
  51. package/dist/lib/constants.d.ts +0 -4
  52. package/dist/lib/constants.js +0 -5
  53. package/dist/lib/constants.js.map +0 -1
  54. package/src/lib/constants.ts +0 -4
@@ -0,0 +1,595 @@
1
+ import { i as identityBase, j as base2, k as base8, l as base10, m as base16, n as base32, o as base36, p as base58, q as base64, r as base256emoji, s as debug } from './browser-mTOOnVZp.js';
2
+
3
+ // @ts-check
4
+
5
+
6
+ const bases = { ...identityBase, ...base2, ...base8, ...base10, ...base16, ...base32, ...base36, ...base58, ...base64, ...base256emoji };
7
+
8
+ /**
9
+ * To guarantee Uint8Array semantics, convert nodejs Buffers
10
+ * into vanilla Uint8Arrays
11
+ */
12
+ function asUint8Array(buf) {
13
+ if (globalThis.Buffer != null) {
14
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
15
+ }
16
+ return buf;
17
+ }
18
+
19
+ /**
20
+ * Returns a `Uint8Array` of the requested size. Referenced memory will
21
+ * be initialized to 0.
22
+ */
23
+ function alloc(size = 0) {
24
+ if (globalThis.Buffer?.alloc != null) {
25
+ return asUint8Array(globalThis.Buffer.alloc(size));
26
+ }
27
+ return new Uint8Array(size);
28
+ }
29
+ /**
30
+ * Where possible returns a Uint8Array of the requested size that references
31
+ * uninitialized memory. Only use if you are certain you will immediately
32
+ * overwrite every value in the returned `Uint8Array`.
33
+ */
34
+ function allocUnsafe(size = 0) {
35
+ if (globalThis.Buffer?.allocUnsafe != null) {
36
+ return asUint8Array(globalThis.Buffer.allocUnsafe(size));
37
+ }
38
+ return new Uint8Array(size);
39
+ }
40
+
41
+ function createCodec(name, prefix, encode, decode) {
42
+ return {
43
+ name,
44
+ prefix,
45
+ encoder: {
46
+ name,
47
+ prefix,
48
+ encode
49
+ },
50
+ decoder: {
51
+ decode
52
+ }
53
+ };
54
+ }
55
+ const string = createCodec('utf8', 'u', (buf) => {
56
+ const decoder = new TextDecoder('utf8');
57
+ return 'u' + decoder.decode(buf);
58
+ }, (str) => {
59
+ const encoder = new TextEncoder();
60
+ return encoder.encode(str.substring(1));
61
+ });
62
+ const ascii = createCodec('ascii', 'a', (buf) => {
63
+ let string = 'a';
64
+ for (let i = 0; i < buf.length; i++) {
65
+ string += String.fromCharCode(buf[i]);
66
+ }
67
+ return string;
68
+ }, (str) => {
69
+ str = str.substring(1);
70
+ const buf = allocUnsafe(str.length);
71
+ for (let i = 0; i < str.length; i++) {
72
+ buf[i] = str.charCodeAt(i);
73
+ }
74
+ return buf;
75
+ });
76
+ const BASES = {
77
+ utf8: string,
78
+ 'utf-8': string,
79
+ hex: bases.base16,
80
+ latin1: ascii,
81
+ ascii,
82
+ binary: ascii,
83
+ ...bases
84
+ };
85
+
86
+ /**
87
+ * Turns a `Uint8Array` into a string.
88
+ *
89
+ * Supports `utf8`, `utf-8` and any encoding supported by the multibase module.
90
+ *
91
+ * Also `ascii` which is similar to node's 'binary' encoding.
92
+ */
93
+ function toString(array, encoding = 'utf8') {
94
+ const base = BASES[encoding];
95
+ if (base == null) {
96
+ throw new Error(`Unsupported encoding "${encoding}"`);
97
+ }
98
+ if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) {
99
+ return globalThis.Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8');
100
+ }
101
+ // strip multibase prefix
102
+ return base.encoder.encode(array).substring(1);
103
+ }
104
+
105
+ /**
106
+ * Create a `Uint8Array` from the passed string
107
+ *
108
+ * Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module.
109
+ *
110
+ * Also `ascii` which is similar to node's 'binary' encoding.
111
+ */
112
+ function fromString(string, encoding = 'utf8') {
113
+ const base = BASES[encoding];
114
+ if (base == null) {
115
+ throw new Error(`Unsupported encoding "${encoding}"`);
116
+ }
117
+ if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) {
118
+ return asUint8Array(globalThis.Buffer.from(string, 'utf-8'));
119
+ }
120
+ // add multibase prefix
121
+ return base.decoder.decode(`${base.prefix}${string}`); // eslint-disable-line @typescript-eslint/restrict-template-expressions
122
+ }
123
+
124
+ /**
125
+ * DefaultPubsubTopic is the default gossipsub topic to use for Waku.
126
+ */
127
+ const DefaultPubsubTopic = "/waku/2/default-waku/proto";
128
+
129
+ function bytes(b, ...lengths) {
130
+ if (!(b instanceof Uint8Array))
131
+ throw new Error('Expected Uint8Array');
132
+ if (lengths.length > 0 && !lengths.includes(b.length))
133
+ throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
134
+ }
135
+ function exists(instance, checkFinished = true) {
136
+ if (instance.destroyed)
137
+ throw new Error('Hash instance has been destroyed');
138
+ if (checkFinished && instance.finished)
139
+ throw new Error('Hash#digest() has already been called');
140
+ }
141
+ function output(out, instance) {
142
+ bytes(out);
143
+ const min = instance.outputLen;
144
+ if (out.length < min) {
145
+ throw new Error(`digestInto() expects output buffer of length at least ${min}`);
146
+ }
147
+ }
148
+
149
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
150
+ // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
151
+ // node.js versions earlier than v19 don't declare it in global scope.
152
+ // For node.js, package.json#exports field mapping rewrites import
153
+ // from `crypto` to `cryptoNode`, which imports native module.
154
+ // Makes the utils un-importable in browsers without a bundler.
155
+ // Once node.js 18 is deprecated, we can just drop the import.
156
+ const u8a = (a) => a instanceof Uint8Array;
157
+ // Cast array to view
158
+ const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
159
+ // The rotate right (circular right shift) operation for uint32
160
+ const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
161
+ // big-endian hardware is rare. Just in case someone still decides to run hashes:
162
+ // early-throw an error because we don't support BE yet.
163
+ const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
164
+ if (!isLE)
165
+ throw new Error('Non little-endian hardware is not supported');
166
+ /**
167
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
168
+ */
169
+ function utf8ToBytes$1(str) {
170
+ if (typeof str !== 'string')
171
+ throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
172
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
173
+ }
174
+ /**
175
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
176
+ * Warning: when Uint8Array is passed, it would NOT get copied.
177
+ * Keep in mind for future mutable operations.
178
+ */
179
+ function toBytes(data) {
180
+ if (typeof data === 'string')
181
+ data = utf8ToBytes$1(data);
182
+ if (!u8a(data))
183
+ throw new Error(`expected Uint8Array, got ${typeof data}`);
184
+ return data;
185
+ }
186
+ // For runtime check if class implements interface
187
+ class Hash {
188
+ // Safe version that clones internal state
189
+ clone() {
190
+ return this._cloneInto();
191
+ }
192
+ }
193
+ function wrapConstructor(hashCons) {
194
+ const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
195
+ const tmp = hashCons();
196
+ hashC.outputLen = tmp.outputLen;
197
+ hashC.blockLen = tmp.blockLen;
198
+ hashC.create = () => hashCons();
199
+ return hashC;
200
+ }
201
+
202
+ // Polyfill for Safari 14
203
+ function setBigUint64(view, byteOffset, value, isLE) {
204
+ if (typeof view.setBigUint64 === 'function')
205
+ return view.setBigUint64(byteOffset, value, isLE);
206
+ const _32n = BigInt(32);
207
+ const _u32_max = BigInt(0xffffffff);
208
+ const wh = Number((value >> _32n) & _u32_max);
209
+ const wl = Number(value & _u32_max);
210
+ const h = isLE ? 4 : 0;
211
+ const l = isLE ? 0 : 4;
212
+ view.setUint32(byteOffset + h, wh, isLE);
213
+ view.setUint32(byteOffset + l, wl, isLE);
214
+ }
215
+ // Base SHA2 class (RFC 6234)
216
+ class SHA2 extends Hash {
217
+ constructor(blockLen, outputLen, padOffset, isLE) {
218
+ super();
219
+ this.blockLen = blockLen;
220
+ this.outputLen = outputLen;
221
+ this.padOffset = padOffset;
222
+ this.isLE = isLE;
223
+ this.finished = false;
224
+ this.length = 0;
225
+ this.pos = 0;
226
+ this.destroyed = false;
227
+ this.buffer = new Uint8Array(blockLen);
228
+ this.view = createView(this.buffer);
229
+ }
230
+ update(data) {
231
+ exists(this);
232
+ const { view, buffer, blockLen } = this;
233
+ data = toBytes(data);
234
+ const len = data.length;
235
+ for (let pos = 0; pos < len;) {
236
+ const take = Math.min(blockLen - this.pos, len - pos);
237
+ // Fast path: we have at least one block in input, cast it to view and process
238
+ if (take === blockLen) {
239
+ const dataView = createView(data);
240
+ for (; blockLen <= len - pos; pos += blockLen)
241
+ this.process(dataView, pos);
242
+ continue;
243
+ }
244
+ buffer.set(data.subarray(pos, pos + take), this.pos);
245
+ this.pos += take;
246
+ pos += take;
247
+ if (this.pos === blockLen) {
248
+ this.process(view, 0);
249
+ this.pos = 0;
250
+ }
251
+ }
252
+ this.length += data.length;
253
+ this.roundClean();
254
+ return this;
255
+ }
256
+ digestInto(out) {
257
+ exists(this);
258
+ output(out, this);
259
+ this.finished = true;
260
+ // Padding
261
+ // We can avoid allocation of buffer for padding completely if it
262
+ // was previously not allocated here. But it won't change performance.
263
+ const { buffer, view, blockLen, isLE } = this;
264
+ let { pos } = this;
265
+ // append the bit '1' to the message
266
+ buffer[pos++] = 0b10000000;
267
+ this.buffer.subarray(pos).fill(0);
268
+ // we have less than padOffset left in buffer, so we cannot put length in current block, need process it and pad again
269
+ if (this.padOffset > blockLen - pos) {
270
+ this.process(view, 0);
271
+ pos = 0;
272
+ }
273
+ // Pad until full block byte with zeros
274
+ for (let i = pos; i < blockLen; i++)
275
+ buffer[i] = 0;
276
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
277
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
278
+ // So we just write lowest 64 bits of that value.
279
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
280
+ this.process(view, 0);
281
+ const oview = createView(out);
282
+ const len = this.outputLen;
283
+ // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
284
+ if (len % 4)
285
+ throw new Error('_sha2: outputLen should be aligned to 32bit');
286
+ const outLen = len / 4;
287
+ const state = this.get();
288
+ if (outLen > state.length)
289
+ throw new Error('_sha2: outputLen bigger than state');
290
+ for (let i = 0; i < outLen; i++)
291
+ oview.setUint32(4 * i, state[i], isLE);
292
+ }
293
+ digest() {
294
+ const { buffer, outputLen } = this;
295
+ this.digestInto(buffer);
296
+ const res = buffer.slice(0, outputLen);
297
+ this.destroy();
298
+ return res;
299
+ }
300
+ _cloneInto(to) {
301
+ to || (to = new this.constructor());
302
+ to.set(...this.get());
303
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
304
+ to.length = length;
305
+ to.pos = pos;
306
+ to.finished = finished;
307
+ to.destroyed = destroyed;
308
+ if (length % blockLen)
309
+ to.buffer.set(buffer);
310
+ return to;
311
+ }
312
+ }
313
+
314
+ // SHA2-256 need to try 2^128 hashes to execute birthday attack.
315
+ // BTC network is doing 2^67 hashes/sec as per early 2023.
316
+ // Choice: a ? b : c
317
+ const Chi = (a, b, c) => (a & b) ^ (~a & c);
318
+ // Majority function, true if any two inpust is true
319
+ const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
320
+ // Round constants:
321
+ // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
322
+ // prettier-ignore
323
+ const SHA256_K = /* @__PURE__ */ new Uint32Array([
324
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
325
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
326
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
327
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
328
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
329
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
330
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
331
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
332
+ ]);
333
+ // Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
334
+ // prettier-ignore
335
+ const IV = /* @__PURE__ */ new Uint32Array([
336
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
337
+ ]);
338
+ // Temporary buffer, not used to store anything between runs
339
+ // Named this way because it matches specification.
340
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
341
+ class SHA256 extends SHA2 {
342
+ constructor() {
343
+ super(64, 32, 8, false);
344
+ // We cannot use array here since array allows indexing by variable
345
+ // which means optimizer/compiler cannot use registers.
346
+ this.A = IV[0] | 0;
347
+ this.B = IV[1] | 0;
348
+ this.C = IV[2] | 0;
349
+ this.D = IV[3] | 0;
350
+ this.E = IV[4] | 0;
351
+ this.F = IV[5] | 0;
352
+ this.G = IV[6] | 0;
353
+ this.H = IV[7] | 0;
354
+ }
355
+ get() {
356
+ const { A, B, C, D, E, F, G, H } = this;
357
+ return [A, B, C, D, E, F, G, H];
358
+ }
359
+ // prettier-ignore
360
+ set(A, B, C, D, E, F, G, H) {
361
+ this.A = A | 0;
362
+ this.B = B | 0;
363
+ this.C = C | 0;
364
+ this.D = D | 0;
365
+ this.E = E | 0;
366
+ this.F = F | 0;
367
+ this.G = G | 0;
368
+ this.H = H | 0;
369
+ }
370
+ process(view, offset) {
371
+ // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
372
+ for (let i = 0; i < 16; i++, offset += 4)
373
+ SHA256_W[i] = view.getUint32(offset, false);
374
+ for (let i = 16; i < 64; i++) {
375
+ const W15 = SHA256_W[i - 15];
376
+ const W2 = SHA256_W[i - 2];
377
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
378
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
379
+ SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
380
+ }
381
+ // Compression function main loop, 64 rounds
382
+ let { A, B, C, D, E, F, G, H } = this;
383
+ for (let i = 0; i < 64; i++) {
384
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
385
+ const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
386
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
387
+ const T2 = (sigma0 + Maj(A, B, C)) | 0;
388
+ H = G;
389
+ G = F;
390
+ F = E;
391
+ E = (D + T1) | 0;
392
+ D = C;
393
+ C = B;
394
+ B = A;
395
+ A = (T1 + T2) | 0;
396
+ }
397
+ // Add the compressed chunk to the current hash value
398
+ A = (A + this.A) | 0;
399
+ B = (B + this.B) | 0;
400
+ C = (C + this.C) | 0;
401
+ D = (D + this.D) | 0;
402
+ E = (E + this.E) | 0;
403
+ F = (F + this.F) | 0;
404
+ G = (G + this.G) | 0;
405
+ H = (H + this.H) | 0;
406
+ this.set(A, B, C, D, E, F, G, H);
407
+ }
408
+ roundClean() {
409
+ SHA256_W.fill(0);
410
+ }
411
+ destroy() {
412
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
413
+ this.buffer.fill(0);
414
+ }
415
+ }
416
+ /**
417
+ * SHA2-256 hash function
418
+ * @param message - data that would be hashed
419
+ */
420
+ const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
421
+
422
+ /**
423
+ * Decode byte array to utf-8 string.
424
+ */
425
+ const bytesToUtf8 = (b) => toString(b, "utf8");
426
+ /**
427
+ * Encode utf-8 string to byte array.
428
+ */
429
+ const utf8ToBytes = (s) => fromString(s, "utf8");
430
+ /**
431
+ * Concatenate using Uint8Arrays as `Buffer` has a different behavior with `DataView`
432
+ */
433
+ function concat(byteArrays, totalLength) {
434
+ const len = totalLength ?? byteArrays.reduce((acc, curr) => acc + curr.length, 0);
435
+ const res = new Uint8Array(len);
436
+ let offset = 0;
437
+ for (const bytes of byteArrays) {
438
+ res.set(bytes, offset);
439
+ offset += bytes.length;
440
+ }
441
+ return res;
442
+ }
443
+
444
+ const singleShardInfoToPubsubTopic = (shardInfo) => {
445
+ if (shardInfo.clusterId === undefined || shardInfo.shard === undefined)
446
+ throw new Error("Invalid shard");
447
+ return `/waku/2/rs/${shardInfo.clusterId}/${shardInfo.shard}`;
448
+ };
449
+ const shardInfoToPubsubTopics = (shardInfo) => {
450
+ if (shardInfo.clusterId === undefined)
451
+ throw new Error("Cluster ID must be specified");
452
+ if ("contentTopics" in shardInfo) {
453
+ // Autosharding: explicitly defined content topics
454
+ return Array.from(new Set(shardInfo.contentTopics.map((contentTopic) => contentTopicToPubsubTopic(contentTopic, shardInfo.clusterId))));
455
+ }
456
+ else if ("shards" in shardInfo) {
457
+ // Static sharding
458
+ if (shardInfo.shards === undefined)
459
+ throw new Error("Invalid shard");
460
+ return Array.from(new Set(shardInfo.shards.map((index) => `/waku/2/rs/${shardInfo.clusterId}/${index}`)));
461
+ }
462
+ else {
463
+ // Autosharding: single shard from application and version
464
+ return [
465
+ contentTopicToPubsubTopic(`/${shardInfo.application}/${shardInfo.version}/default/default`)
466
+ ];
467
+ }
468
+ };
469
+ const pubsubTopicToSingleShardInfo = (pubsubTopics) => {
470
+ const parts = pubsubTopics.split("/");
471
+ if (parts.length != 6 ||
472
+ parts[1] !== "waku" ||
473
+ parts[2] !== "2" ||
474
+ parts[3] !== "rs")
475
+ throw new Error("Invalid pubsub topic");
476
+ const clusterId = parseInt(parts[4]);
477
+ const shard = parseInt(parts[5]);
478
+ if (isNaN(clusterId) || isNaN(shard))
479
+ throw new Error("Invalid clusterId or shard");
480
+ return {
481
+ clusterId,
482
+ shard
483
+ };
484
+ };
485
+ function ensurePubsubTopicIsConfigured(pubsubTopic, configuredTopics) {
486
+ if (!configuredTopics.includes(pubsubTopic)) {
487
+ throw new Error(`Pubsub topic ${pubsubTopic} has not been configured on this instance. Configured topics are: ${configuredTopics}. Please update your configuration by passing in the topic during Waku node instantiation.`);
488
+ }
489
+ }
490
+ /**
491
+ * Given a string, will throw an error if it is not formatted as a valid content topic for autosharding based on https://rfc.vac.dev/spec/51/
492
+ * @param contentTopic String to validate
493
+ * @returns Object with each content topic field as an attribute
494
+ */
495
+ function ensureValidContentTopic(contentTopic) {
496
+ const parts = contentTopic.split("/");
497
+ if (parts.length < 5 || parts.length > 6) {
498
+ throw Error("Content topic format is invalid");
499
+ }
500
+ // Validate generation field if present
501
+ let generation = 0;
502
+ if (parts.length == 6) {
503
+ generation = parseInt(parts[1]);
504
+ if (isNaN(generation)) {
505
+ throw new Error("Invalid generation field in content topic");
506
+ }
507
+ if (generation > 0) {
508
+ throw new Error("Generation greater than 0 is not supported");
509
+ }
510
+ }
511
+ // Validate remaining fields
512
+ const fields = parts.splice(-4);
513
+ // Validate application field
514
+ if (fields[0].length == 0) {
515
+ throw new Error("Application field cannot be empty");
516
+ }
517
+ // Validate version field
518
+ if (fields[1].length == 0) {
519
+ throw new Error("Version field cannot be empty");
520
+ }
521
+ // Validate topic name field
522
+ if (fields[2].length == 0) {
523
+ throw new Error("Topic name field cannot be empty");
524
+ }
525
+ // Validate encoding field
526
+ if (fields[3].length == 0) {
527
+ throw new Error("Encoding field cannot be empty");
528
+ }
529
+ return {
530
+ generation,
531
+ application: fields[0],
532
+ version: fields[1],
533
+ topicName: fields[2],
534
+ encoding: fields[3]
535
+ };
536
+ }
537
+ /**
538
+ * Given a string, determines which autoshard index to use for its pubsub topic.
539
+ * Based on the algorithm described in the RFC: https://rfc.vac.dev/spec/51//#algorithm
540
+ */
541
+ function contentTopicToShardIndex(contentTopic, networkShards = 8) {
542
+ const { application, version } = ensureValidContentTopic(contentTopic);
543
+ const digest = sha256(concat([utf8ToBytes(application), utf8ToBytes(version)]));
544
+ const dataview = new DataView(digest.buffer.slice(-8));
545
+ return Number(dataview.getBigUint64(0, false) % BigInt(networkShards));
546
+ }
547
+ function contentTopicToPubsubTopic(contentTopic, clusterId = 1, networkShards = 8) {
548
+ const shardIndex = contentTopicToShardIndex(contentTopic, networkShards);
549
+ return `/waku/2/rs/${clusterId}/${shardIndex}`;
550
+ }
551
+ /**
552
+ * Used when creating encoders/decoders to determine which pubsub topic to use
553
+ */
554
+ function determinePubsubTopic(contentTopic, pubsubTopicShardInfo = DefaultPubsubTopic) {
555
+ if (typeof pubsubTopicShardInfo == "string") {
556
+ return pubsubTopicShardInfo;
557
+ }
558
+ else {
559
+ return pubsubTopicShardInfo
560
+ ? pubsubTopicShardInfo.shard
561
+ ? singleShardInfoToPubsubTopic(pubsubTopicShardInfo)
562
+ : contentTopicToPubsubTopic(contentTopic, pubsubTopicShardInfo.clusterId)
563
+ : DefaultPubsubTopic;
564
+ }
565
+ }
566
+
567
+ const APP_NAME = "waku";
568
+ class Logger {
569
+ _info;
570
+ _warn;
571
+ _error;
572
+ static createDebugNamespace(level, prefix) {
573
+ return prefix ? `${APP_NAME}:${level}:${prefix}` : `${APP_NAME}:${level}`;
574
+ }
575
+ constructor(prefix) {
576
+ this._info = debug(Logger.createDebugNamespace("info", prefix));
577
+ this._warn = debug(Logger.createDebugNamespace("warn", prefix));
578
+ this._error = debug(Logger.createDebugNamespace("error", prefix));
579
+ }
580
+ get info() {
581
+ return this._info;
582
+ }
583
+ get warn() {
584
+ return this._warn;
585
+ }
586
+ get error() {
587
+ return this._error;
588
+ }
589
+ log(level, ...args) {
590
+ const logger = this[level];
591
+ logger(...args);
592
+ }
593
+ }
594
+
595
+ export { DefaultPubsubTopic as D, Logger as L, allocUnsafe as a, asUint8Array as b, bases as c, alloc as d, singleShardInfoToPubsubTopic as e, fromString as f, ensurePubsubTopicIsConfigured as g, sha256 as h, concat as i, determinePubsubTopic as j, bytesToUtf8 as k, pubsubTopicToSingleShardInfo as p, shardInfoToPubsubTopics as s, toString as t, utf8ToBytes as u };