@dignetwork/dig-sdk 0.0.1-alpha.16 → 0.0.1-alpha.161

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 (83) hide show
  1. package/dist/DataIntegrityTree/DataIntegrityTree.d.ts +11 -2
  2. package/dist/DataIntegrityTree/DataIntegrityTree.d.ts.map +1 -1
  3. package/dist/DataIntegrityTree/DataIntegrityTree.js +94 -13
  4. package/dist/DigNetwork/ContentServer.d.ts +24 -5
  5. package/dist/DigNetwork/ContentServer.d.ts.map +1 -1
  6. package/dist/DigNetwork/ContentServer.js +239 -27
  7. package/dist/DigNetwork/DigNetwork.d.ts +15 -9
  8. package/dist/DigNetwork/DigNetwork.d.ts.map +1 -1
  9. package/dist/DigNetwork/DigNetwork.js +129 -189
  10. package/dist/DigNetwork/DigPeer.d.ts +8 -3
  11. package/dist/DigNetwork/DigPeer.d.ts.map +1 -1
  12. package/dist/DigNetwork/DigPeer.js +68 -115
  13. package/dist/DigNetwork/IncentiveServer.d.ts.map +1 -1
  14. package/dist/DigNetwork/IncentiveServer.js +6 -5
  15. package/dist/DigNetwork/PropagationServer.d.ts +88 -33
  16. package/dist/DigNetwork/PropagationServer.d.ts.map +1 -1
  17. package/dist/DigNetwork/PropagationServer.js +636 -357
  18. package/dist/blockchain/DataStore.d.ts +25 -7
  19. package/dist/blockchain/DataStore.d.ts.map +1 -1
  20. package/dist/blockchain/DataStore.js +115 -159
  21. package/dist/blockchain/DataStoreSerializer.d.ts +1 -1
  22. package/dist/blockchain/DataStoreSerializer.d.ts.map +1 -1
  23. package/dist/blockchain/FullNodePeer.d.ts +94 -10
  24. package/dist/blockchain/FullNodePeer.d.ts.map +1 -1
  25. package/dist/blockchain/FullNodePeer.js +318 -104
  26. package/dist/blockchain/ServerCoin.d.ts +13 -5
  27. package/dist/blockchain/ServerCoin.d.ts.map +1 -1
  28. package/dist/blockchain/ServerCoin.js +90 -28
  29. package/dist/blockchain/StoreMonitorRegistry.d.ts +85 -0
  30. package/dist/blockchain/StoreMonitorRegistry.d.ts.map +1 -0
  31. package/dist/blockchain/StoreMonitorRegistry.js +241 -0
  32. package/dist/blockchain/Wallet.d.ts +2 -2
  33. package/dist/blockchain/Wallet.d.ts.map +1 -1
  34. package/dist/blockchain/Wallet.js +50 -18
  35. package/dist/blockchain/coins.d.ts +1 -2
  36. package/dist/blockchain/coins.d.ts.map +1 -1
  37. package/dist/blockchain/coins.js +1 -55
  38. package/dist/blockchain/index.d.ts +1 -0
  39. package/dist/blockchain/index.d.ts.map +1 -1
  40. package/dist/blockchain/index.js +1 -0
  41. package/dist/types.d.ts +2 -0
  42. package/dist/types.d.ts.map +1 -1
  43. package/dist/utils/ContentScanner.d.ts +63 -0
  44. package/dist/utils/ContentScanner.d.ts.map +1 -0
  45. package/dist/utils/ContentScanner.js +175 -0
  46. package/dist/utils/Environment.d.ts +21 -0
  47. package/dist/utils/Environment.d.ts.map +1 -0
  48. package/dist/utils/Environment.js +93 -0
  49. package/dist/utils/FileCache.d.ts +1 -1
  50. package/dist/utils/FileCache.d.ts.map +1 -1
  51. package/dist/utils/FileCache.js +2 -2
  52. package/dist/utils/FileTransfer.d.ts +47 -0
  53. package/dist/utils/FileTransfer.d.ts.map +1 -0
  54. package/dist/utils/FileTransfer.js +209 -0
  55. package/dist/utils/NconfManager.js +2 -1
  56. package/dist/utils/PeerRanker.d.ts +55 -0
  57. package/dist/utils/PeerRanker.d.ts.map +1 -0
  58. package/dist/utils/PeerRanker.js +186 -0
  59. package/dist/utils/StoreArchiveManager.d.ts +45 -0
  60. package/dist/utils/StoreArchiveManager.d.ts.map +1 -0
  61. package/dist/utils/StoreArchiveManager.js +153 -0
  62. package/dist/utils/config.d.ts +4 -3
  63. package/dist/utils/config.d.ts.map +1 -1
  64. package/dist/utils/config.js +12 -7
  65. package/dist/utils/directoryUtils.d.ts.map +1 -1
  66. package/dist/utils/directoryUtils.js +9 -12
  67. package/dist/utils/index.d.ts +3 -1
  68. package/dist/utils/index.d.ts.map +1 -1
  69. package/dist/utils/index.js +3 -1
  70. package/dist/utils/merkle.d.ts +2 -0
  71. package/dist/utils/merkle.d.ts.map +1 -0
  72. package/dist/utils/merkle.js +28 -0
  73. package/dist/utils/network.d.ts +2 -1
  74. package/dist/utils/network.d.ts.map +1 -1
  75. package/dist/utils/network.js +40 -15
  76. package/dist/utils/promiseUtils.d.ts +19 -0
  77. package/dist/utils/promiseUtils.d.ts.map +1 -0
  78. package/dist/utils/promiseUtils.js +43 -0
  79. package/dist/utils/ssl.js +2 -2
  80. package/package.json +17 -5
  81. package/dist/utils/deltaUtils.d.ts +0 -2
  82. package/dist/utils/deltaUtils.d.ts.map +0 -1
  83. package/dist/utils/deltaUtils.js +0 -83
@@ -17,6 +17,7 @@ declare class DataIntegrityTree {
17
17
  files: Map<string, {
18
18
  hash: string;
19
19
  sha256: string;
20
+ bytes: string;
20
21
  }>;
21
22
  private tree;
22
23
  constructor(storeId: string, options?: DataIntegrityTreeOptions);
@@ -78,6 +79,9 @@ declare class DataIntegrityTree {
78
79
  * @returns The serialized Merkle tree.
79
80
  */
80
81
  serialize(rootHash?: string | null): object;
82
+ static getRootOfForeignTree(treeData: {
83
+ leaves: string[];
84
+ }): string;
81
85
  /**
82
86
  * Deserialize a JSON object to a Merkle tree.
83
87
  * @param rootHash - The root hash of the tree.
@@ -106,7 +110,7 @@ declare class DataIntegrityTree {
106
110
  * @param rootHash - The root hash of the tree. Defaults to the latest root hash.
107
111
  * @returns The readable stream for the file.
108
112
  */
109
- getValueStream(hexKey: string, rootHash?: string | null): Readable;
113
+ getValueStream(hexKey: string, rootHash?: string | null, byteOffset?: number | null, length?: number | null): Readable;
110
114
  /**
111
115
  * Delete all leaves from the Merkle tree.
112
116
  */
@@ -152,9 +156,14 @@ declare class DataIntegrityTree {
152
156
  * @param sha256 - The SHA-256 hash of the file.
153
157
  * @param serializedTree - The foreign serialized Merkle tree.
154
158
  * @param expectedRootHash - The expected root hash of the Merkle tree.
159
+ * @param dataDir - The directory where the data is stored.
160
+ * @param verifiedSha256 - A boolean indicating if the SHA-256 hash has already been verified.
155
161
  * @returns A boolean indicating if the SHA-256 is present in the foreign tree and the root hash matches.
156
162
  */
157
- static validateKeyIntegrityWithForeignTree(key: string, sha256: string, serializedTree: object, expectedRootHash: string): boolean;
163
+ static validateKeyIntegrityWithForeignTree(hexkey: string, sha256: string, serializedTree: object, expectedRootHash: string, dataDir: string, verifiedSha256?: boolean): Promise<boolean>;
164
+ private static verifyFileHash;
165
+ private static deserializeAndVerifyTree;
166
+ private static checkHashInTree;
158
167
  }
159
168
  export { DataIntegrityTree };
160
169
  //# sourceMappingURL=DataIntegrityTree.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"DataIntegrityTree.d.ts","sourceRoot":"","sources":["../../src/DataIntegrityTree/DataIntegrityTree.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAuClC,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAGlB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;GAEG;AACH,cAAM,iBAAiB;IACrB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAS;IACjB,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAC5D,OAAO,CAAC,IAAI,CAAa;gBAEb,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,wBAA6B;WAiDrD,IAAI,CAChB,OAAO,EAAE,M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();
96
96
  if (options.rootHash) {
97
- const manifest = this._loadManifest();
98
- if (manifest.includes(options.rootHash)) {
97
+ if (fs.existsSync(path.join(this.storeDir, `${options.rootHash}.dat`))) {
99
98
  this.tree = this.deserializeTree(options.rootHash);
100
99
  }
101
100
  else {
@@ -135,7 +134,10 @@ class DataIntegrityTree {
135
134
  const manifest = this._loadManifest();
136
135
  if (manifest.length > 0) {
137
136
  const latestRootHash = manifest[manifest.length - 1];
138
- return this.deserializeTree(latestRootHash);
137
+ if (latestRootHash && isHexString(latestRootHash)) {
138
+ return this.deserializeTree(latestRootHash);
139
+ }
140
+ return new merkletreejs_1.MerkleTree([], crypto_js_1.SHA256, { sortPairs: true });
139
141
  }
140
142
  else {
141
143
  return new merkletreejs_1.MerkleTree([], crypto_js_1.SHA256, { sortPairs: true });
@@ -189,11 +191,16 @@ class DataIntegrityTree {
189
191
  fs.mkdirSync(tempDir, { recursive: true });
190
192
  }
191
193
  const tempFilePath = path.join(tempDir, `${crypto.randomUUID()}.gz`);
194
+ let totalBytes = 0;
192
195
  return new Promise((resolve, reject) => {
193
196
  const tempWriteStream = fs.createWriteStream(tempFilePath);
194
197
  readStream.on("data", (chunk) => {
195
198
  uncompressedHash.update(chunk);
196
199
  });
200
+ // Now listen to the gzip stream for compressed data size
201
+ gzip.on("data", (chunk) => {
202
+ totalBytes += chunk.length; // This counts compressed bytes
203
+ });
197
204
  readStream.pipe(gzip).pipe(tempWriteStream);
198
205
  tempWriteStream.on("finish", async () => {
199
206
  sha256 = uncompressedHash.digest("hex");
@@ -222,6 +229,7 @@ class DataIntegrityTree {
222
229
  this.files.set(key, {
223
230
  hash: combinedHash,
224
231
  sha256: sha256,
232
+ bytes: totalBytes.toString(),
225
233
  });
226
234
  await new Promise((resolve) => setTimeout(resolve, 100));
227
235
  this._rebuildTree();
@@ -313,6 +321,11 @@ class DataIntegrityTree {
313
321
  files: Object.fromEntries(this.files),
314
322
  };
315
323
  }
324
+ static getRootOfForeignTree(treeData) {
325
+ const leaves = treeData.leaves.map((leaf) => Buffer.from(leaf, "hex"));
326
+ const tree = new merkletreejs_1.MerkleTree(leaves, crypto_js_1.SHA256, { sortPairs: true });
327
+ return tree.getRoot().toString("hex");
328
+ }
316
329
  /**
317
330
  * Deserialize a JSON object to a Merkle tree.
318
331
  * @param rootHash - The root hash of the tree.
@@ -423,7 +436,7 @@ class DataIntegrityTree {
423
436
  * @param rootHash - The root hash of the tree. Defaults to the latest root hash.
424
437
  * @returns The readable stream for the file.
425
438
  */
426
- getValueStream(hexKey, rootHash = null) {
439
+ getValueStream(hexKey, rootHash = null, byteOffset = null, length = null) {
427
440
  if (!isHexString(hexKey)) {
428
441
  throw new Error("key must be a valid hex string");
429
442
  }
@@ -446,8 +459,22 @@ class DataIntegrityTree {
446
459
  if (!fs.existsSync(filePath)) {
447
460
  throw new Error(`File at path ${filePath} does not exist`);
448
461
  }
449
- // Create a read stream and pipe it through a decompression stream using the same algorithm (gzip)
450
- const readStream = fs.createReadStream(filePath);
462
+ const fileSize = fs.statSync(filePath).size;
463
+ // Validate offset and length
464
+ if (byteOffset !== null && length !== null) {
465
+ if (byteOffset + length > fileSize) {
466
+ throw new Error(`Offset (${byteOffset}) and length (${length}) exceed the file size (${fileSize}).`);
467
+ }
468
+ }
469
+ // Create the read stream with optional byte range
470
+ const options = {};
471
+ if (byteOffset !== null) {
472
+ options.start = byteOffset;
473
+ }
474
+ if (length !== null && byteOffset !== null) {
475
+ options.end = byteOffset + length - 1; // `end` is inclusive, hence `byteOffset + length - 1`
476
+ }
477
+ const readStream = fs.createReadStream(filePath, options);
451
478
  const decompressStream = zlib.createGunzip();
452
479
  // Return the combined stream as a generic Readable stream
453
480
  return readStream.pipe(decompressStream);
@@ -589,14 +616,13 @@ class DataIntegrityTree {
589
616
  decompressStream.on("end", () => {
590
617
  const uncompressedSha256 = hash.digest("hex");
591
618
  const isValid = uncompressedSha256 === sha256;
592
- console.log(`SHA-256 of uncompressed file: ${uncompressedSha256}`);
593
619
  if (!isValid) {
594
620
  return resolve(false);
595
621
  }
596
622
  const tree = this.deserializeTree(rootHash);
597
623
  const combinedHash = crypto
598
624
  .createHash("sha256")
599
- .update(`${toHex(sha256)}/${sha256}`)
625
+ .update(sha256)
600
626
  .digest("hex");
601
627
  const leaf = Buffer.from(combinedHash, "hex");
602
628
  const isInTree = tree.getLeafIndex(leaf) !== -1;
@@ -618,10 +644,12 @@ class DataIntegrityTree {
618
644
  * @param sha256 - The SHA-256 hash of the file.
619
645
  * @param serializedTree - The foreign serialized Merkle tree.
620
646
  * @param expectedRootHash - The expected root hash of the Merkle tree.
647
+ * @param dataDir - The directory where the data is stored.
648
+ * @param verifiedSha256 - A boolean indicating if the SHA-256 hash has already been verified.
621
649
  * @returns A boolean indicating if the SHA-256 is present in the foreign tree and the root hash matches.
622
650
  */
623
- static validateKeyIntegrityWithForeignTree(key, sha256, serializedTree, expectedRootHash) {
624
- if (!isHexString(key)) {
651
+ static async validateKeyIntegrityWithForeignTree(hexkey, sha256, serializedTree, expectedRootHash, dataDir, verifiedSha256 = false) {
652
+ if (!isHexString(hexkey)) {
625
653
  throw new Error("key must be a valid hex string");
626
654
  }
627
655
  if (!isHexString(sha256)) {
@@ -630,6 +658,56 @@ class DataIntegrityTree {
630
658
  if (!isHexString(expectedRootHash)) {
631
659
  throw new Error("expectedRootHash must be a valid hex string");
632
660
  }
661
+ if (!verifiedSha256) {
662
+ // Verify the file's SHA-256 hash
663
+ const isFileValid = await this.verifyFileHash(sha256, dataDir);
664
+ if (!isFileValid) {
665
+ return false;
666
+ }
667
+ }
668
+ // Deserialize and verify the tree
669
+ const tree = this.deserializeAndVerifyTree(serializedTree, expectedRootHash);
670
+ if (!tree) {
671
+ return false;
672
+ }
673
+ // Check if the combined hash exists in the foreign tree's leaves
674
+ const isInTree = this.checkHashInTree(tree, hexkey, sha256);
675
+ return isInTree;
676
+ }
677
+ // Function to verify the file's SHA-256 hash
678
+ static async verifyFileHash(sha256, dataDir) {
679
+ const filePath = path.join(dataDir, sha256.match(/.{1,2}/g).join("/"));
680
+ // Check if the file exists
681
+ if (!fs.existsSync(filePath)) {
682
+ throw new Error(`File at path ${filePath} does not exist`);
683
+ }
684
+ const compressedReadStream = fs.createReadStream(filePath);
685
+ const decompressStream = zlib.createGunzip();
686
+ const hash = crypto.createHash("sha256");
687
+ // Process file decompression and hash comparison
688
+ return new Promise((resolve, reject) => {
689
+ compressedReadStream.pipe(decompressStream);
690
+ decompressStream.on("data", (chunk) => {
691
+ hash.update(chunk);
692
+ });
693
+ decompressStream.on("end", () => {
694
+ const uncompressedSha256 = hash.digest("hex");
695
+ if (uncompressedSha256 !== sha256) {
696
+ console.warn(`File hash mismatch. Expected: ${sha256}, got: ${uncompressedSha256}`);
697
+ return resolve(false);
698
+ }
699
+ resolve(true);
700
+ });
701
+ decompressStream.on("error", (err) => {
702
+ reject(err);
703
+ });
704
+ compressedReadStream.on("error", (err) => {
705
+ reject(err);
706
+ });
707
+ });
708
+ }
709
+ // Function to deserialize and verify the tree
710
+ static deserializeAndVerifyTree(serializedTree, expectedRootHash) {
633
711
  // Deserialize the foreign tree
634
712
  const leaves = serializedTree.leaves.map((leaf) => Buffer.from(leaf, "hex"));
635
713
  const tree = new merkletreejs_1.MerkleTree(leaves, crypto_js_1.SHA256, { sortPairs: true });
@@ -637,7 +715,7 @@ class DataIntegrityTree {
637
715
  const treeRootHash = tree.getRoot().toString("hex");
638
716
  if (treeRootHash !== expectedRootHash) {
639
717
  console.warn(`Expected root hash ${expectedRootHash}, but got ${treeRootHash}`);
640
- return false;
718
+ return null;
641
719
  }
642
720
  // Rebuild the files map from the serialized tree
643
721
  // @ts-ignore
@@ -645,10 +723,13 @@ class DataIntegrityTree {
645
723
  key,
646
724
  { hash: value.hash, sha256: value.sha256 },
647
725
  ]));
648
- // Check if the SHA-256 exists in the foreign tree's files
726
+ return tree;
727
+ }
728
+ // Function to check if the combined hash exists in the tree
729
+ static checkHashInTree(tree, hexkey, sha256) {
649
730
  const combinedHash = crypto
650
731
  .createHash("sha256")
651
- .update(`${toHex(key)}/${sha256}`)
732
+ .update(`${hexkey}/${sha256}`)
652
733
  .digest("hex");
653
734
  const leaf = Buffer.from(combinedHash, "hex");
654
735
  const isInTree = tree.getLeafIndex(leaf) !== -1;
@@ -3,26 +3,45 @@ import { Readable } from "stream";
3
3
  export declare class ContentServer {
4
4
  private ipAddress;
5
5
  private storeId;
6
+ private static certPath;
7
+ private static keyPath;
6
8
  private static readonly port;
7
9
  constructor(ipAddress: string, storeId: string);
8
10
  getKey(key: string, rootHash: string, challengeHex?: string): Promise<string>;
9
- getPaymentAddress(): Promise<string>;
11
+ getKeyChunk(key: string, rootHash: string): Promise<Buffer>;
12
+ getPaymentAddress(): Promise<string | null>;
13
+ /**
14
+ * Fetches and caches the .well-known information for the store's IP address.
15
+ *
16
+ * @returns A promise that resolves to the .well-known JSON data.
17
+ */
10
18
  getWellKnown(): Promise<any>;
11
19
  getKnownStores(): Promise<any>;
12
20
  getStoresIndex(): Promise<any>;
13
- getKeysIndex(): Promise<any>;
14
- headKey(key: string): Promise<{
21
+ getKeysIndex(rootHash?: string): Promise<any>;
22
+ headKey(key: string, rootHash?: string): Promise<{
15
23
  success: boolean;
16
24
  headers?: http.IncomingHttpHeaders;
17
25
  }>;
18
- headStore(): Promise<{
26
+ headStore(options?: {
27
+ hasRootHash: string;
28
+ }): Promise<{
19
29
  success: boolean;
20
30
  headers?: http.IncomingHttpHeaders;
21
31
  }>;
22
- streamKey(key: string): Promise<Readable>;
32
+ /**
33
+ * Checks if the store has the specified rootHash.
34
+ * Utilizes caching to improve performance.
35
+ *
36
+ * @param rootHash - The root hash to check.
37
+ * @returns A promise that resolves to true if the root hash exists, otherwise false.
38
+ */
39
+ hasRootHash(rootHash: string): Promise<boolean>;
40
+ streamKey(key: string, rootHash?: string): Promise<Readable>;
23
41
  private head;
24
42
  private fetchJson;
25
43
  private fetchWithRetries;
26
44
  private fetch;
45
+ private fetchFirstChunk;
27
46
  }
28
47
  //# sourceMappingURL=ContentServer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ContentServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/ContentServer.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAGlC,qBAAa,aAAa;IACxB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAM;gBAEtB,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAMjC,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAa7E,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC;IAepC,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC;IAM5B,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC;IAM9B,cAAc,IAAI,OAAO,CAAC,GAAG,CAAC;IAM9B,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC;IAM5B,OAAO,CAClB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,CAAA;KAAE,CAAC;IAMvD,SAAS,IAAI,OAAO,CAAC;QAChC,OAAO,EAAE,OAAO,CAAC;QACjB,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,CAAC;KACpC,CAAC;IAMK,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;YA0ClC,IAAI;YAuEJ,SAAS;YAMT,gBAAgB;YAiChB,KAAK;CAuDpB"}
1
+ {"version":3,"file":"ContentServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/ContentServer.ts"],"names":[],"mappings":"AACA,O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