@dignetwork/dig-sdk 0.0.1-alpha.39 → 0.0.1-alpha.41
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":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"DigNetwork.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigNetwork.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAWpC,qBAAa,UAAU;IACrB,OAAO,CAAC,SAAS,CAAY;IAC7B,OAAO,CAAC,UAAU,CAAa;IAC/B,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,aAAa,CAA2B;gBAEpC,OAAO,EAAE,MAAM;YAOb,eAAe;IA8ChB,eAAe,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAsDhD,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;WA6BrC,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;WAQhD,oBAAoB,CACtC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,GAAG,CAAC,EAAE,MAAM,EACZ,eAAe,GAAE,MAAM,EAAO,GAC7B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;WAuCX,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAQ5C,aAAa,CACxB,aAAa,GAAE,OAAe,EAC9B,iBAAiB,GAAE,OAAc,EACjC,QAAQ,GAAE,OAAe,GACxB,OAAO,CAAC,IAAI,CAAC;YAwEF,mBAAmB;YAWnB,kBAAkB;YASlB,oBAAoB;YASpB,qBAAqB;YA2ErB,cAAc;CAoB7B"}
|
|
@@ -32,6 +32,9 @@ const deltaUtils_1 = require("../utils/deltaUtils");
|
|
|
32
32
|
const hashUtils_1 = require("../utils/hashUtils");
|
|
33
33
|
const blockchain_1 = require("../blockchain");
|
|
34
34
|
const config_1 = require("../utils/config");
|
|
35
|
+
const util_1 = require("util");
|
|
36
|
+
const rename = (0, util_1.promisify)(fs.rename);
|
|
37
|
+
const unlink = (0, util_1.promisify)(fs.unlink);
|
|
35
38
|
class DigNetwork {
|
|
36
39
|
constructor(storeId) {
|
|
37
40
|
this.dataStore = blockchain_1.DataStore.from(storeId);
|
|
@@ -165,7 +168,7 @@ class DigNetwork {
|
|
|
165
168
|
if (!rootHistory.length) {
|
|
166
169
|
throw new Error("No roots found in rootHistory. Cannot proceed with file download.");
|
|
167
170
|
}
|
|
168
|
-
await this.downloadHeightFile(
|
|
171
|
+
await this.downloadHeightFile(true);
|
|
169
172
|
const rootHistorySorted = rootHistory
|
|
170
173
|
.filter((item) => item.timestamp !== undefined)
|
|
171
174
|
.sort((a, b) => b.timestamp - a.timestamp);
|
|
@@ -185,7 +188,7 @@ class DigNetwork {
|
|
|
185
188
|
const filePath = (0, hashUtils_1.getFilePathFromSha256)(file.sha256, `${this.storeDir}/data`);
|
|
186
189
|
if (!fs.existsSync(filePath) || forceDownload) {
|
|
187
190
|
console.log(`Downloading file with sha256: ${file.sha256}...`);
|
|
188
|
-
await this.downloadFileFromPeers(file.sha256.match(/.{1,2}/g).join("/")
|
|
191
|
+
await this.downloadFileFromPeers(`data/${file.sha256.match(/.{1,2}/g).join("/")}`, filePath, forceDownload);
|
|
189
192
|
}
|
|
190
193
|
}));
|
|
191
194
|
}
|
|
@@ -215,6 +218,7 @@ class DigNetwork {
|
|
|
215
218
|
}
|
|
216
219
|
async downloadFileFromPeers(dataPath, filePath, overwrite) {
|
|
217
220
|
let digPeers = await this.fetchAvailablePeers();
|
|
221
|
+
const tempFilePath = `${filePath}.tmp`;
|
|
218
222
|
while (true) {
|
|
219
223
|
if (!overwrite && fs.existsSync(filePath))
|
|
220
224
|
return;
|
|
@@ -224,22 +228,24 @@ class DigNetwork {
|
|
|
224
228
|
continue;
|
|
225
229
|
try {
|
|
226
230
|
// Create directory if it doesn't exist
|
|
227
|
-
const directory = path.dirname(
|
|
231
|
+
const directory = path.dirname(tempFilePath);
|
|
228
232
|
if (!fs.existsSync(directory)) {
|
|
229
233
|
fs.mkdirSync(directory, { recursive: true });
|
|
230
234
|
}
|
|
231
|
-
// Stream the file data
|
|
232
|
-
const fileStream = fs.createWriteStream(
|
|
235
|
+
// Stream the file data to a temporary file
|
|
236
|
+
const fileStream = fs.createWriteStream(tempFilePath);
|
|
233
237
|
// Start streaming the data from the peer
|
|
234
238
|
const peerStream = await digPeer.propagationServer.streamStoreData(dataPath);
|
|
235
|
-
// Pipe the peer stream
|
|
239
|
+
// Pipe the peer stream to the temp file
|
|
236
240
|
await new Promise((resolve, reject) => {
|
|
237
241
|
peerStream.pipe(fileStream);
|
|
238
|
-
peerStream.on(
|
|
239
|
-
peerStream.on(
|
|
240
|
-
fileStream.on(
|
|
242
|
+
peerStream.on('end', resolve);
|
|
243
|
+
peerStream.on('error', reject);
|
|
244
|
+
fileStream.on('error', reject);
|
|
241
245
|
});
|
|
242
|
-
|
|
246
|
+
// Rename the temp file to the final file path after successful download
|
|
247
|
+
await rename(tempFilePath, filePath);
|
|
248
|
+
if (process.env.DIG_DEBUG === '1') {
|
|
243
249
|
console.log(`Downloaded ${dataPath} from ${digPeer.IpAddress}`);
|
|
244
250
|
}
|
|
245
251
|
return; // Exit the method if download succeeds
|
|
@@ -247,11 +253,15 @@ class DigNetwork {
|
|
|
247
253
|
catch (error) {
|
|
248
254
|
console.warn(`Failed to download ${dataPath} from ${digPeer.IpAddress}, blacklisting peer and trying next...`);
|
|
249
255
|
blacklist.add(digPeer.IpAddress);
|
|
256
|
+
// Clean up the temp file in case of failure
|
|
257
|
+
if (fs.existsSync(tempFilePath)) {
|
|
258
|
+
await unlink(tempFilePath);
|
|
259
|
+
}
|
|
250
260
|
}
|
|
251
261
|
}
|
|
252
262
|
this.peerBlacklist.set(dataPath, blacklist);
|
|
253
263
|
if (blacklist.size >= digPeers.length) {
|
|
254
|
-
if (process.env.DIG_DEBUG ===
|
|
264
|
+
if (process.env.DIG_DEBUG === '1') {
|
|
255
265
|
console.warn(`All peers blacklisted for ${dataPath}. Refreshing peers...`);
|
|
256
266
|
}
|
|
257
267
|
digPeers = await this.fetchAvailablePeers();
|