@dignetwork/dig-sdk 0.0.1-alpha.51 → 0.0.1-alpha.54
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.
- package/dist/DataIntegrityTree/DataIntegrityTree.js +1 -1
- package/dist/DigNetwork/DigNetwork.d.ts +2 -4
- package/dist/DigNetwork/DigNetwork.d.ts.map +1 -1
- package/dist/DigNetwork/DigNetwork.js +26 -48
- package/dist/DigNetwork/DigPeer.d.ts +2 -1
- package/dist/DigNetwork/DigPeer.d.ts.map +1 -1
- package/dist/DigNetwork/DigPeer.js +13 -57
- package/dist/DigNetwork/PropagationServer.d.ts +70 -42
- package/dist/DigNetwork/PropagationServer.d.ts.map +1 -1
- package/dist/DigNetwork/PropagationServer.js +308 -409
- package/dist/blockchain/DataStore.d.ts.map +1 -1
- package/dist/blockchain/DataStore.js +15 -5
- package/dist/utils/ContentScanner.d.ts +63 -0
- package/dist/utils/ContentScanner.d.ts.map +1 -0
- package/dist/utils/ContentScanner.js +175 -0
- package/package.json +5 -1
|
@@ -610,7 +610,7 @@ class DataIntegrityTree {
|
|
|
610
610
|
const tree = this.deserializeTree(rootHash);
|
|
611
611
|
const combinedHash = crypto
|
|
612
612
|
.createHash("sha256")
|
|
613
|
-
.update(
|
|
613
|
+
.update(sha256)
|
|
614
614
|
.digest("hex");
|
|
615
615
|
const leaf = Buffer.from(combinedHash, "hex");
|
|
616
616
|
const isInTree = tree.getLeafIndex(leaf) !== -1;
|
|
@@ -8,10 +8,8 @@ export declare class DigNetwork {
|
|
|
8
8
|
static subscribeToStore(storeId: string): Promise<void>;
|
|
9
9
|
static findPeerWithStoreKey(storeId: string, rootHash: string, key?: string, intialBlackList?: string[]): Promise<DigPeer | null>;
|
|
10
10
|
static unsubscribeFromStore(storeId: string): void;
|
|
11
|
-
|
|
11
|
+
syncStoreFromPeers(): Promise<void>;
|
|
12
12
|
private fetchAvailablePeers;
|
|
13
|
-
|
|
14
|
-
private downloadFileFromPeers;
|
|
15
|
-
private runProgressBar;
|
|
13
|
+
downloadFileFromPeers(dataPath: string, filePath: string, overwrite: boolean): Promise<void>;
|
|
16
14
|
}
|
|
17
15
|
//# sourceMappingURL=DigNetwork.d.ts.map
|
|
@@ -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;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;WAOP,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,OAAO,GAAG,IAAI,CAAC;WAyDZ,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAO5C,
|
|
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;WAOP,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,OAAO,GAAG,IAAI,CAAC;WAyDZ,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAO5C,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;YAyGlC,mBAAmB;IAWpB,qBAAqB,CAChC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,OAAO,GACjB,OAAO,CAAC,IAAI,CAAC;CA2CjB"}
|
|
@@ -26,13 +26,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.DigNetwork = void 0;
|
|
27
27
|
const fs = __importStar(require("fs"));
|
|
28
28
|
const path = __importStar(require("path"));
|
|
29
|
-
const cli_progress_1 = require("cli-progress");
|
|
30
29
|
const DigPeer_1 = require("./DigPeer");
|
|
31
|
-
const hashUtils_1 = require("../utils/hashUtils");
|
|
32
30
|
const blockchain_1 = require("../blockchain");
|
|
33
31
|
const config_1 = require("../utils/config");
|
|
34
32
|
const util_1 = require("util");
|
|
35
|
-
const DataIntegrityTree_1 = require("../DataIntegrityTree");
|
|
36
33
|
const rename = (0, util_1.promisify)(fs.rename);
|
|
37
34
|
const unlink = (0, util_1.promisify)(fs.unlink);
|
|
38
35
|
class DigNetwork {
|
|
@@ -47,7 +44,7 @@ class DigNetwork {
|
|
|
47
44
|
recursive: true,
|
|
48
45
|
});
|
|
49
46
|
const digNetwork = new DigNetwork(storeId);
|
|
50
|
-
await digNetwork.
|
|
47
|
+
await digNetwork.syncStoreFromPeers();
|
|
51
48
|
}
|
|
52
49
|
static async findPeerWithStoreKey(storeId, rootHash, key, intialBlackList = []) {
|
|
53
50
|
const peerBlackList = intialBlackList;
|
|
@@ -101,7 +98,7 @@ class DigNetwork {
|
|
|
101
98
|
});
|
|
102
99
|
fs.unlinkSync(path.join(config_1.DIG_FOLDER_PATH, "stores", storeId + ".json"));
|
|
103
100
|
}
|
|
104
|
-
async
|
|
101
|
+
async syncStoreFromPeers() {
|
|
105
102
|
console.log("Starting file download process...");
|
|
106
103
|
let peerBlackList = [];
|
|
107
104
|
let selectedPeer = null;
|
|
@@ -110,13 +107,11 @@ class DigNetwork {
|
|
|
110
107
|
if (!rootHistory.length) {
|
|
111
108
|
throw new Error("No roots found in rootHistory. Cannot proceed with file download.");
|
|
112
109
|
}
|
|
113
|
-
await this.downloadHeightFile(true);
|
|
114
110
|
// Filter out rootInfo entries where the .dat file already exists
|
|
115
111
|
const rootHistoryFiltered = rootHistory
|
|
116
112
|
.filter((item) => item.timestamp !== undefined)
|
|
117
113
|
.filter((item) => !fs.existsSync(`${this.storeDir}/${item.root_hash}.dat`))
|
|
118
|
-
// Reverse to download the latest first
|
|
119
|
-
.reverse();
|
|
114
|
+
.reverse(); // Reverse to download the latest first
|
|
120
115
|
if (!rootHistoryFiltered.length) {
|
|
121
116
|
console.log("All root hashes already exist locally. No need for download.");
|
|
122
117
|
return;
|
|
@@ -125,30 +120,26 @@ class DigNetwork {
|
|
|
125
120
|
for (const rootInfo of rootHistoryFiltered) {
|
|
126
121
|
while (true) {
|
|
127
122
|
try {
|
|
123
|
+
// Find a peer with the store and root hash
|
|
128
124
|
selectedPeer = await DigNetwork.findPeerWithStoreKey(this.dataStore.StoreId, rootInfo.root_hash, undefined, peerBlackList);
|
|
129
125
|
if (!selectedPeer) {
|
|
130
126
|
console.error(`No peer found with root hash ${rootInfo.root_hash}. Skipping download.`);
|
|
131
127
|
break; // Exit loop if no more peers are found
|
|
132
128
|
}
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
if (!
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
console.error(`\x1b[31mIntegrity check failed for file with sha256: ${file.sha256}.\x1b[0m`);
|
|
147
|
-
await unlink(filePath);
|
|
148
|
-
throw new Error(`Store Integrity check failed. Syncing file from another peer.`);
|
|
149
|
-
}
|
|
129
|
+
// Ensure the selected peer has the store by checking with a HEAD request
|
|
130
|
+
const { storeExists, rootHashExists } = await selectedPeer.propagationServer.checkStoreExists(rootInfo.root_hash);
|
|
131
|
+
if (!storeExists) {
|
|
132
|
+
console.warn(`Peer ${selectedPeer.IpAddress} does not have the store. Trying another peer...`);
|
|
133
|
+
peerBlackList.push(selectedPeer.IpAddress); // Add peer to blacklist and try again
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (!rootHashExists) {
|
|
137
|
+
console.warn(`Peer ${selectedPeer.IpAddress} does not have the root hash. Trying another peer...`);
|
|
138
|
+
peerBlackList.push(selectedPeer.IpAddress); // Add peer to blacklist and try again
|
|
139
|
+
continue;
|
|
150
140
|
}
|
|
151
|
-
|
|
141
|
+
// Download the store root and all associated data from the selected peer
|
|
142
|
+
await selectedPeer.downloadStoreRoot(rootInfo.root_hash);
|
|
152
143
|
peerBlackList = []; // Clear the blacklist upon successful download
|
|
153
144
|
// Break out of the retry loop if the download succeeds
|
|
154
145
|
break;
|
|
@@ -160,9 +151,7 @@ class DigNetwork {
|
|
|
160
151
|
}
|
|
161
152
|
}
|
|
162
153
|
}
|
|
163
|
-
//
|
|
164
|
-
// This has an effect where the latest roothash will always be synced first, even if new ones come in.
|
|
165
|
-
// Then it will backfill historical roothashes
|
|
154
|
+
// Process the latest root hash first, breaking after each success to handle new incoming roots
|
|
166
155
|
break;
|
|
167
156
|
}
|
|
168
157
|
console.log("Syncing store complete.");
|
|
@@ -171,7 +160,6 @@ class DigNetwork {
|
|
|
171
160
|
if (selectedPeer) {
|
|
172
161
|
peerBlackList.push(selectedPeer.IpAddress);
|
|
173
162
|
}
|
|
174
|
-
console.trace(error);
|
|
175
163
|
throw error;
|
|
176
164
|
}
|
|
177
165
|
}
|
|
@@ -182,10 +170,6 @@ class DigNetwork {
|
|
|
182
170
|
const peers = await this.serverCoin.sampleCurrentEpoch(10, Array.from(this.peerBlacklist.keys()));
|
|
183
171
|
return peers.map((ip) => new DigPeer_1.DigPeer(ip, this.dataStore.StoreId));
|
|
184
172
|
}
|
|
185
|
-
async downloadHeightFile(forceDownload) {
|
|
186
|
-
const heightFilePath = path.join(this.storeDir, "height.json");
|
|
187
|
-
await this.downloadFileFromPeers("height.json", heightFilePath, forceDownload);
|
|
188
|
-
}
|
|
189
173
|
async downloadFileFromPeers(dataPath, filePath, overwrite) {
|
|
190
174
|
let digPeers = await this.fetchAvailablePeers();
|
|
191
175
|
const tempFilePath = `${filePath}.tmp`;
|
|
@@ -197,7 +181,14 @@ class DigNetwork {
|
|
|
197
181
|
try {
|
|
198
182
|
if (blacklist.has(digPeer.IpAddress))
|
|
199
183
|
continue;
|
|
200
|
-
|
|
184
|
+
// Ensure the selected peer has the store by checking with a HEAD request
|
|
185
|
+
const { storeExists } = await digPeer.propagationServer.checkStoreExists();
|
|
186
|
+
if (!storeExists) {
|
|
187
|
+
console.warn(`Peer ${digPeer.IpAddress} does not have the store. Trying another peer...`);
|
|
188
|
+
blacklist.add(digPeer.IpAddress); // Add peer to blacklist and try again
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
await digPeer.downloadData(dataPath);
|
|
201
192
|
return; // Exit the method if download succeeds
|
|
202
193
|
}
|
|
203
194
|
catch (error) {
|
|
@@ -211,18 +202,5 @@ class DigNetwork {
|
|
|
211
202
|
}
|
|
212
203
|
}
|
|
213
204
|
}
|
|
214
|
-
async runProgressBar(total, name, task) {
|
|
215
|
-
// Using 'any' to work around TypeScript issues
|
|
216
|
-
const multiBar = new cli_progress_1.MultiBar({
|
|
217
|
-
clearOnComplete: false,
|
|
218
|
-
hideCursor: true,
|
|
219
|
-
format: "{bar} | {percentage}% | {name}",
|
|
220
|
-
noTTYOutput: true,
|
|
221
|
-
}, cli_progress_1.Presets.shades_classic);
|
|
222
|
-
const progress = multiBar.create(total, 0, { name });
|
|
223
|
-
await task(progress).finally(() => {
|
|
224
|
-
multiBar.stop();
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
205
|
}
|
|
228
206
|
exports.DigNetwork = DigNetwork;
|
|
@@ -19,6 +19,7 @@ export declare class DigPeer {
|
|
|
19
19
|
static createPaymentHint(storeId: Buffer): Buffer;
|
|
20
20
|
syncStore(): Promise<void>;
|
|
21
21
|
pushStoreRoot(storeId: string, rootHash: string): Promise<void>;
|
|
22
|
-
|
|
22
|
+
downloadStoreRoot(rootHash: string): Promise<void>;
|
|
23
|
+
downloadData(dataPath: string): Promise<void>;
|
|
23
24
|
}
|
|
24
25
|
//# sourceMappingURL=DigPeer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DigPeer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigPeer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAKpD,OAAO,EAKL,MAAM,EACP,MAAM,8BAA8B,CAAC;AAUtC,qBAAa,OAAO;IAClB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,kBAAkB,CAAoB;IAC9C,OAAO,CAAC,gBAAgB,CAAkB;gBAE9B,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAS9C,IAAW,aAAa,IAAI,aAAa,CAExC;IAGD,IAAW,iBAAiB,IAAI,iBAAiB,CAEhD;IAGD,IAAW,eAAe,IAAI,eAAe,CAE5C;IAED,IAAW,SAAS,IAAI,MAAM,CAE7B;WAEa,qBAAqB,CACjC,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EAAE,EACnB,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,MAAM,EAAE,GACd,OAAO,CAAC,IAAI,CAAC;WAuBI,gBAAgB,CAClC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EAAE,GAChB,OAAO,CAAC,IAAI,CAAC;IAwCH,WAAW,CACtB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,MAAM,EAAO,GACnB,OAAO,CAAC,IAAI,CAAC;WAYF,iBAAiB,CAAC,OAAO,EAAE,MAAM;IAqBlC,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"DigPeer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/DigPeer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAKpD,OAAO,EAKL,MAAM,EACP,MAAM,8BAA8B,CAAC;AAUtC,qBAAa,OAAO;IAClB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,kBAAkB,CAAoB;IAC9C,OAAO,CAAC,gBAAgB,CAAkB;gBAE9B,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAS9C,IAAW,aAAa,IAAI,aAAa,CAExC;IAGD,IAAW,iBAAiB,IAAI,iBAAiB,CAEhD;IAGD,IAAW,eAAe,IAAI,eAAe,CAE5C;IAED,IAAW,SAAS,IAAI,MAAM,CAE7B;WAEa,qBAAqB,CACjC,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EAAE,EACnB,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,MAAM,EAAE,GACd,OAAO,CAAC,IAAI,CAAC;WAuBI,gBAAgB,CAClC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EAAE,GAChB,OAAO,CAAC,IAAI,CAAC;IAwCH,WAAW,CACtB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,MAAM,EAAO,GACnB,OAAO,CAAC,IAAI,CAAC;WAYF,iBAAiB,CAAC,OAAO,EAAE,MAAM;IAqBlC,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAc1B,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI/D,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKlD,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG3D"}
|
|
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.DigPeer = void 0;
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
7
|
const crypto_1 = __importDefault(require("crypto"));
|
|
9
8
|
const ContentServer_1 = require("./ContentServer");
|
|
10
9
|
const PropagationServer_1 = require("./PropagationServer");
|
|
@@ -15,7 +14,6 @@ const datalayer_driver_1 = require("@dignetwork/datalayer-driver");
|
|
|
15
14
|
const blockchain_2 = require("../blockchain");
|
|
16
15
|
const blockchain_3 = require("../blockchain");
|
|
17
16
|
const coins_1 = require("../blockchain/coins");
|
|
18
|
-
const config_1 = require("../utils/config");
|
|
19
17
|
const util_1 = require("util");
|
|
20
18
|
const rename = (0, util_1.promisify)(fs_1.default.rename);
|
|
21
19
|
const unlink = (0, util_1.promisify)(fs_1.default.unlink);
|
|
@@ -103,64 +101,22 @@ class DigPeer {
|
|
|
103
101
|
async syncStore() {
|
|
104
102
|
const dataStore = blockchain_1.DataStore.from(this.storeId);
|
|
105
103
|
const rootHistory = await dataStore.getRootHistory();
|
|
106
|
-
rootHistory
|
|
107
|
-
.filter((root) => root.synced)
|
|
108
|
-
.
|
|
109
|
-
|
|
110
|
-
|
|
104
|
+
const localRootHistory = rootHistory
|
|
105
|
+
.filter((root) => Boolean(root.synced))
|
|
106
|
+
.reverse();
|
|
107
|
+
console.log(`Syncing store ${this.storeId} with ${this.IpAddress}`);
|
|
108
|
+
for (const item of localRootHistory) {
|
|
109
|
+
await this.pushStoreRoot(this.storeId, item.root_hash);
|
|
110
|
+
}
|
|
111
111
|
}
|
|
112
112
|
async pushStoreRoot(storeId, rootHash) {
|
|
113
|
-
|
|
114
|
-
const alreadySynced = await this.contentServer.hasRootHash(rootHash);
|
|
115
|
-
if (alreadySynced) {
|
|
116
|
-
console.log(`Root hash ${rootHash} already synced.`);
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
const tree = await dataStore.Tree.serialize(rootHash);
|
|
120
|
-
// @ts-ignore
|
|
121
|
-
tree.files.forEach(async (file) => {
|
|
122
|
-
await dataStore.Tree.verifyKeyIntegrity(file.sha256, rootHash);
|
|
123
|
-
console.log(`Pushing file ${file.key} to ${this.IpAddress}`);
|
|
124
|
-
const dataPath = path_1.default.join("data", file.sha256.match(/.{1,2}/g).join("/"));
|
|
125
|
-
const fileLocation = path_1.default.join(config_1.STORE_PATH, storeId, dataPath);
|
|
126
|
-
await this.propagationServer.pushFile(fileLocation, dataPath);
|
|
127
|
-
});
|
|
113
|
+
await PropagationServer_1.PropagationServer.uploadStore(storeId, rootHash, this.IpAddress);
|
|
128
114
|
}
|
|
129
|
-
async
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (!headStoreResponse.success) {
|
|
135
|
-
throw new Error("Data not accessible from store.");
|
|
136
|
-
}
|
|
137
|
-
const directory = path_1.default.dirname(tempFilePath);
|
|
138
|
-
if (!fs_1.default.existsSync(directory)) {
|
|
139
|
-
fs_1.default.mkdirSync(directory, { recursive: true });
|
|
140
|
-
}
|
|
141
|
-
const fileStream = fs_1.default.createWriteStream(tempFilePath);
|
|
142
|
-
const dataStream = await this.propagationServer.streamStoreData(dataPath);
|
|
143
|
-
await new Promise((resolve, reject) => {
|
|
144
|
-
dataStream.pipe(fileStream);
|
|
145
|
-
dataStream.on("end", resolve);
|
|
146
|
-
dataStream.on("error", reject);
|
|
147
|
-
fileStream.on("error", reject);
|
|
148
|
-
});
|
|
149
|
-
await rename(tempFilePath, filePath);
|
|
150
|
-
console.log(`Downloaded ${dataPath} from ${this.IpAddress}`);
|
|
151
|
-
}
|
|
152
|
-
catch (error) {
|
|
153
|
-
console.error(`Failed to download data: ${error.message}`);
|
|
154
|
-
if (fs_1.default.existsSync(tempFilePath)) {
|
|
155
|
-
await unlink(tempFilePath);
|
|
156
|
-
}
|
|
157
|
-
// Check if directory is empty and remove it if it is
|
|
158
|
-
const directory = path_1.default.dirname(tempFilePath);
|
|
159
|
-
if (fs_1.default.existsSync(directory) && fs_1.default.readdirSync(directory).length === 0) {
|
|
160
|
-
fs_1.default.rmdirSync(directory);
|
|
161
|
-
console.log(`Removed empty directory: ${directory}`);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
115
|
+
async downloadStoreRoot(rootHash) {
|
|
116
|
+
await PropagationServer_1.PropagationServer.downloadStore(this.storeId, rootHash, this.IpAddress);
|
|
117
|
+
}
|
|
118
|
+
async downloadData(dataPath) {
|
|
119
|
+
await this.propagationServer.downloadFile(dataPath);
|
|
164
120
|
}
|
|
165
121
|
}
|
|
166
122
|
exports.DigPeer = DigPeer;
|
|
@@ -1,47 +1,75 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Readable } from "stream";
|
|
1
|
+
import https from "https";
|
|
3
2
|
export declare class PropagationServer {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
storeId: string;
|
|
4
|
+
sessionId: string;
|
|
5
|
+
publicKey: string;
|
|
6
|
+
wallet: any;
|
|
7
|
+
ipAddress: string;
|
|
8
|
+
certPath: string;
|
|
9
|
+
keyPath: string;
|
|
10
|
+
username: string | undefined;
|
|
11
|
+
password: string | undefined;
|
|
8
12
|
private static readonly port;
|
|
9
|
-
private static
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
private static multiBar;
|
|
14
|
+
constructor(storeId: string, ipAddress: string);
|
|
15
|
+
/**
|
|
16
|
+
* Initialize the Wallet instance.
|
|
17
|
+
*/
|
|
18
|
+
initializeWallet(): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Create an Axios HTTPS Agent with self-signed certificate allowance.
|
|
21
|
+
*/
|
|
22
|
+
createHttpsAgent(): https.Agent;
|
|
23
|
+
/**
|
|
24
|
+
* Check if the store and optional root hash exist by making a HEAD request.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} [rootHash] - Optional root hash to check for existence.
|
|
27
|
+
* @returns {Promise<{ storeExists: boolean, rootHashExists: boolean }>} - An object indicating if the store and root hash exist.
|
|
28
|
+
*/
|
|
29
|
+
checkStoreExists(rootHash?: string): Promise<{
|
|
30
|
+
storeExists: boolean;
|
|
31
|
+
rootHashExists: boolean;
|
|
22
32
|
}>;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Start an upload session by sending a POST request to the server.
|
|
35
|
+
*/
|
|
36
|
+
startUploadSession(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Request a nonce for a file by sending a HEAD request to the server.
|
|
39
|
+
*/
|
|
40
|
+
getFileNonce(filename: string): Promise<string>;
|
|
41
|
+
/**
|
|
42
|
+
* Upload a file to the server by sending a PUT request.
|
|
43
|
+
* Logs progress using cli-progress for each file.
|
|
44
|
+
*/
|
|
45
|
+
uploadFile(filePath: string): Promise<any>;
|
|
46
|
+
/**
|
|
47
|
+
* Static function to handle the entire upload process for multiple files based on rootHash.
|
|
48
|
+
* @param {string} storeId - The ID of the DataStore.
|
|
49
|
+
* @param {string} rootHash - The root hash used to derive the file set.
|
|
50
|
+
* @param {string} publicKey - The public key of the user.
|
|
51
|
+
* @param {string} ipAddress - The IP address of the server.
|
|
52
|
+
*/
|
|
53
|
+
static uploadStore(storeId: string, rootHash: string, ipAddress: string): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Fetch a file from the server by sending a GET request and return its content in memory.
|
|
56
|
+
* Logs progress using cli-progress.
|
|
57
|
+
* @param {string} dataPath - The data path of the file to download.
|
|
58
|
+
* @returns {Promise<Buffer>} - The file content in memory as a Buffer.
|
|
59
|
+
*/
|
|
60
|
+
fetchFile(dataPath: string): Promise<Buffer>;
|
|
61
|
+
/**
|
|
62
|
+
* Download a file from the server by sending a GET request.
|
|
63
|
+
* Logs progress using cli-progress.
|
|
64
|
+
* @param {string} dataPath - The data path of the file to download.
|
|
65
|
+
*/
|
|
66
|
+
downloadFile(dataPath: string): Promise<void>;
|
|
67
|
+
/**
|
|
68
|
+
* Static function to handle downloading multiple files from a DataStore based on file paths.
|
|
69
|
+
* @param {string} storeId - The ID of the DataStore.
|
|
70
|
+
* @param {string[]} dataPaths - The list of data paths to download.
|
|
71
|
+
* @param {string} ipAddress - The IP address of the server.
|
|
72
|
+
*/
|
|
73
|
+
static downloadStore(storeId: string, rootHash: string, ipAddress: string): Promise<void>;
|
|
46
74
|
}
|
|
47
75
|
//# sourceMappingURL=PropagationServer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"PropagationServer.d.ts","sourceRoot":"","sources":["../../src/DigNetwork/PropagationServer.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,MAAM,OAAO,CAAC;AAO1B,qBAAa,iBAAiB;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,GAAG,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAE7B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAQ;IAGpC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAYrB;gBAEU,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;IAY9C;;OAEG;IACG,gBAAgB;IAKtB;;OAEG;IACH,gBAAgB;IAQhB;;;;;OAKG;IACG,gBAAgB,CACpB,QAAQ,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC;QAAE,WAAW,EAAE,OAAO,CAAC;QAAC,cAAc,EAAE,OAAO,CAAA;KAAE,CAAC;IA4C7D;;OAEG;IACG,kBAAkB;IAqCxB;;OAEG;IACG,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAoBrD;;;OAGG;IACG,UAAU,CAAC,QAAQ,EAAE,MAAM;IAuDjC;;;;;;OAMG;WACU,WAAW,CACtB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;IAyCnB;;;;;OAKG;IACG,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IA4ClD;;;;OAIG;IACG,YAAY,CAAC,QAAQ,EAAE,MAAM;IAkEnC;;;;;OAKG;WACU,aAAa,CACxB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM;CA4BpB"}
|