@dignetwork/dig-sdk 0.0.1-alpha.15 → 0.0.1-alpha.16
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.
|
@@ -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,
|
|
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,S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ntegrityTree(storeId, {
|
|
114
|
+
return new DataIntegrityTree(storeId, {
|
|
115
|
+
...options,
|
|
116
|
+
disableInitialize: true,
|
|
117
|
+
});
|
|
115
118
|
}
|
|
116
119
|
/**
|
|
117
120
|
* Load the manifest file.
|
|
@@ -188,81 +191,54 @@ class DataIntegrityTree {
|
|
|
188
191
|
const tempFilePath = path.join(tempDir, `${crypto.randomUUID()}.gz`);
|
|
189
192
|
return new Promise((resolve, reject) => {
|
|
190
193
|
const tempWriteStream = fs.createWriteStream(tempFilePath);
|
|
191
|
-
// Update the hash with the original data
|
|
192
194
|
readStream.on("data", (chunk) => {
|
|
193
195
|
uncompressedHash.update(chunk);
|
|
194
196
|
});
|
|
195
|
-
// Pipe the read stream through gzip into the temporary write stream
|
|
196
197
|
readStream.pipe(gzip).pipe(tempWriteStream);
|
|
197
198
|
tempWriteStream.on("finish", async () => {
|
|
198
|
-
|
|
199
|
+
sha256 = uncompressedHash.digest("hex");
|
|
200
|
+
const finalWriteStream = this._createWriteStream(sha256);
|
|
201
|
+
const finalPath = finalWriteStream.path;
|
|
202
|
+
// Ensure the directory exists before copying the file
|
|
203
|
+
const finalDir = path.dirname(finalPath);
|
|
204
|
+
if (!fs.existsSync(finalDir)) {
|
|
205
|
+
fs.mkdirSync(finalDir, { recursive: true });
|
|
206
|
+
}
|
|
199
207
|
try {
|
|
200
|
-
sha256 = uncompressedHash.digest("hex");
|
|
201
|
-
finalWriteStream = this._createWriteStream(sha256);
|
|
202
|
-
const finalPath = finalWriteStream.path;
|
|
203
|
-
// Ensure the directory exists
|
|
204
|
-
const finalDir = path.dirname(finalPath);
|
|
205
|
-
if (!fs.existsSync(finalDir)) {
|
|
206
|
-
fs.mkdirSync(finalDir, { recursive: true });
|
|
207
|
-
}
|
|
208
|
-
// Copy the temporary gzipped file to the final destination
|
|
209
208
|
await this._streamFile(tempFilePath, finalPath);
|
|
210
|
-
await unlink(tempFilePath);
|
|
209
|
+
await unlink(tempFilePath);
|
|
211
210
|
const combinedHash = crypto
|
|
212
211
|
.createHash("sha256")
|
|
213
212
|
.update(`${key}/${sha256}`)
|
|
214
213
|
.digest("hex");
|
|
215
|
-
// Check if the key already exists with the same hash
|
|
216
214
|
if (Array.from(this.files.values()).some((file) => file.hash === combinedHash)) {
|
|
217
215
|
console.log(`No changes detected for key: ${key}`);
|
|
218
216
|
return resolve();
|
|
219
217
|
}
|
|
220
|
-
// Delete existing key if present
|
|
221
218
|
if (this.files.has(key)) {
|
|
222
219
|
this.deleteKey(key);
|
|
223
220
|
}
|
|
224
|
-
// Insert the new key with the hash
|
|
225
221
|
console.log(`Inserted key: ${key}`);
|
|
226
222
|
this.files.set(key, {
|
|
227
223
|
hash: combinedHash,
|
|
228
224
|
sha256: sha256,
|
|
229
225
|
});
|
|
226
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
230
227
|
this._rebuildTree();
|
|
228
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
231
229
|
resolve();
|
|
232
230
|
}
|
|
233
231
|
catch (err) {
|
|
234
|
-
// On error, cleanup the temporary file and reject
|
|
235
|
-
await unlink(tempFilePath).catch(() => { });
|
|
236
232
|
reject(err);
|
|
237
233
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (finalWriteStream) {
|
|
241
|
-
finalWriteStream.end();
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
tempWriteStream.on("error", async (err) => {
|
|
246
|
-
// Close streams and clean up in case of error
|
|
247
|
-
tempWriteStream.destroy();
|
|
248
|
-
gzip.destroy();
|
|
249
|
-
readStream.destroy();
|
|
250
|
-
await unlink(tempFilePath).catch(() => { }); // Clean up the temp file
|
|
251
|
-
reject(err);
|
|
234
|
+
tempWriteStream.end();
|
|
235
|
+
finalWriteStream.end();
|
|
252
236
|
});
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
tempWriteStream.destroy();
|
|
256
|
-
gzip.destroy();
|
|
257
|
-
readStream.destroy();
|
|
258
|
-
await unlink(tempFilePath).catch(() => { }); // Clean up the temp file
|
|
237
|
+
tempWriteStream.on("error", (err) => {
|
|
238
|
+
tempWriteStream.end();
|
|
259
239
|
reject(err);
|
|
260
240
|
});
|
|
261
|
-
|
|
262
|
-
// Handle errors in the gzip stream
|
|
263
|
-
tempWriteStream.destroy();
|
|
264
|
-
gzip.destroy();
|
|
265
|
-
readStream.destroy();
|
|
241
|
+
readStream.on("error", (err) => {
|
|
266
242
|
reject(err);
|
|
267
243
|
});
|
|
268
244
|
});
|
|
@@ -369,7 +345,9 @@ class DataIntegrityTree {
|
|
|
369
345
|
? fs.readFileSync(manifestPath, "utf-8").trim().split("\n")
|
|
370
346
|
: [];
|
|
371
347
|
// Check if the last entry is the same as the rootHash to avoid duplicates
|
|
372
|
-
const latestRootHash = manifestContent.length > 0
|
|
348
|
+
const latestRootHash = manifestContent.length > 0
|
|
349
|
+
? manifestContent[manifestContent.length - 1]
|
|
350
|
+
: null;
|
|
373
351
|
if (latestRootHash !== rootHash) {
|
|
374
352
|
// Append the new rootHash if it is not the same as the last one
|
|
375
353
|
fs.appendFileSync(manifestPath, `${rootHash}\n`);
|