agentgui 1.0.274 → 1.0.275
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/CLAUDE.md +280 -280
- package/IPFS_DOWNLOADER.md +277 -277
- package/TASK_2C_COMPLETION.md +334 -334
- package/bin/gmgui.cjs +54 -54
- package/build-portable.js +3 -42
- package/database.js +1422 -1406
- package/lib/claude-runner.js +1130 -1130
- package/lib/ipfs-downloader.js +459 -459
- package/lib/speech.js +152 -152
- package/package.json +1 -1
- package/readme.md +76 -76
- package/server.js +3787 -3794
- package/setup-npm-token.sh +68 -68
- package/static/app.js +773 -773
- package/static/event-rendering-showcase.html +708 -708
- package/static/index.html +3178 -3180
- package/static/js/agent-auth.js +298 -298
- package/static/js/audio-recorder-processor.js +18 -18
- package/static/js/client.js +2656 -2656
- package/static/js/conversations.js +583 -583
- package/static/js/dialogs.js +267 -267
- package/static/js/event-consolidator.js +101 -101
- package/static/js/event-filter.js +311 -311
- package/static/js/event-processor.js +452 -452
- package/static/js/features.js +413 -413
- package/static/js/kalman-filter.js +67 -67
- package/static/js/progress-dialog.js +130 -130
- package/static/js/script-runner.js +219 -219
- package/static/js/streaming-renderer.js +2123 -2120
- package/static/js/syntax-highlighter.js +269 -269
- package/static/js/tts-websocket-handler.js +152 -152
- package/static/js/ui-components.js +431 -431
- package/static/js/voice.js +849 -849
- package/static/js/websocket-manager.js +596 -596
- package/static/templates/INDEX.html +465 -465
- package/static/templates/README.md +190 -190
- package/static/templates/agent-capabilities.html +56 -56
- package/static/templates/agent-metadata-panel.html +44 -44
- package/static/templates/agent-status-badge.html +30 -30
- package/static/templates/code-annotation-panel.html +155 -155
- package/static/templates/code-suggestion-panel.html +184 -184
- package/static/templates/command-header.html +77 -77
- package/static/templates/command-output-scrollable.html +118 -118
- package/static/templates/elapsed-time.html +54 -54
- package/static/templates/error-alert.html +106 -106
- package/static/templates/error-history-timeline.html +160 -160
- package/static/templates/error-recovery-options.html +109 -109
- package/static/templates/error-stack-trace.html +95 -95
- package/static/templates/error-summary.html +80 -80
- package/static/templates/event-counter.html +48 -48
- package/static/templates/execution-actions.html +97 -97
- package/static/templates/execution-progress-bar.html +80 -80
- package/static/templates/execution-stepper.html +120 -120
- package/static/templates/file-breadcrumb.html +118 -118
- package/static/templates/file-diff-viewer.html +121 -121
- package/static/templates/file-metadata.html +133 -133
- package/static/templates/file-read-panel.html +66 -66
- package/static/templates/file-write-panel.html +120 -120
- package/static/templates/git-branch-remote.html +107 -107
- package/static/templates/git-diff-list.html +101 -101
- package/static/templates/git-log-visualization.html +153 -153
- package/static/templates/git-status-panel.html +115 -115
- package/static/templates/quality-metrics-display.html +170 -170
- package/static/templates/terminal-output-panel.html +87 -87
- package/static/templates/test-results-display.html +144 -144
- package/static/theme.js +72 -72
- package/test-download-progress.js +223 -223
- package/test-websocket-broadcast.js +147 -147
- package/tests/ipfs-downloader.test.js +370 -370
package/lib/ipfs-downloader.js
CHANGED
|
@@ -1,459 +1,459 @@
|
|
|
1
|
-
import https from 'https';
|
|
2
|
-
import http from 'http';
|
|
3
|
-
import fs from 'fs';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import crypto from 'crypto';
|
|
6
|
-
import os from 'os';
|
|
7
|
-
import { queries } from '../database.js';
|
|
8
|
-
|
|
9
|
-
const GATEWAYS = [
|
|
10
|
-
'https://ipfs.io/ipfs/',
|
|
11
|
-
'https://gateway.pinata.cloud/ipfs/',
|
|
12
|
-
'https://cloudflare-ipfs.com/ipfs/',
|
|
13
|
-
'https://dweb.link/ipfs/'
|
|
14
|
-
];
|
|
15
|
-
|
|
16
|
-
const CONFIG = {
|
|
17
|
-
MAX_RESUME_ATTEMPTS: 3,
|
|
18
|
-
MAX_RETRY_ATTEMPTS: 3,
|
|
19
|
-
TIMEOUT_MS: 30000,
|
|
20
|
-
INITIAL_BACKOFF_MS: 1000,
|
|
21
|
-
BACKOFF_MULTIPLIER: 2,
|
|
22
|
-
DOWNLOADS_DIR: path.join(os.homedir(), '.gmgui', 'downloads'),
|
|
23
|
-
RESUME_THRESHOLD: 0.5
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
class IPFSDownloader {
|
|
27
|
-
constructor() {
|
|
28
|
-
this.downloads = new Map();
|
|
29
|
-
this.setupDir();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
setupDir() {
|
|
33
|
-
if (!fs.existsSync(CONFIG.DOWNLOADS_DIR)) {
|
|
34
|
-
fs.mkdirSync(CONFIG.DOWNLOADS_DIR, { recursive: true });
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async download(cid, filename, options = {}) {
|
|
39
|
-
const filepath = path.join(CONFIG.DOWNLOADS_DIR, filename);
|
|
40
|
-
const { modelName = 'unknown', modelType = 'unknown', modelHash = null } = options;
|
|
41
|
-
|
|
42
|
-
try {
|
|
43
|
-
const cidId = queries.recordIpfsCid(cid, modelName, modelType, modelHash, GATEWAYS[0]);
|
|
44
|
-
const downloadId = queries.recordDownloadStart(cidId, filepath, 0);
|
|
45
|
-
|
|
46
|
-
await this.executeDownload(downloadId, cidId, filepath, options);
|
|
47
|
-
return { success: true, downloadId, filepath, cid };
|
|
48
|
-
} catch (error) {
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async executeDownload(downloadId, cidId, filepath, options = {}) {
|
|
54
|
-
let gatewayIndex = 0;
|
|
55
|
-
let resumeAttempts = 0;
|
|
56
|
-
let retryAttempts = 0;
|
|
57
|
-
|
|
58
|
-
while (true) {
|
|
59
|
-
try {
|
|
60
|
-
const gateway = GATEWAYS[gatewayIndex];
|
|
61
|
-
const cidRecord = queries._db.prepare('SELECT * FROM ipfs_cids WHERE id = ?').get(cidId);
|
|
62
|
-
if (!cidRecord) throw new Error('CID record not found');
|
|
63
|
-
const url = `${gateway}${cidRecord.cid}`;
|
|
64
|
-
|
|
65
|
-
const { size, hash } = await this.downloadFile(
|
|
66
|
-
url,
|
|
67
|
-
filepath,
|
|
68
|
-
0,
|
|
69
|
-
options
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
queries.completeDownload(downloadId, cidId);
|
|
73
|
-
|
|
74
|
-
if (options.hashVerify && hash) {
|
|
75
|
-
queries.updateDownloadHash(downloadId, hash);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return queries.getDownload(downloadId);
|
|
79
|
-
} catch (error) {
|
|
80
|
-
if (error.message.includes('Range')) {
|
|
81
|
-
resumeAttempts++;
|
|
82
|
-
if (resumeAttempts > CONFIG.MAX_RESUME_ATTEMPTS) {
|
|
83
|
-
await this.cleanupPartial(filepath);
|
|
84
|
-
gatewayIndex = (gatewayIndex + 1) % GATEWAYS.length;
|
|
85
|
-
resumeAttempts = 0;
|
|
86
|
-
}
|
|
87
|
-
} else if (error.message.includes('timeout') || error.message.includes('ETIMEDOUT')) {
|
|
88
|
-
retryAttempts++;
|
|
89
|
-
if (retryAttempts > CONFIG.MAX_RETRY_ATTEMPTS) {
|
|
90
|
-
queries.recordDownloadError(downloadId, cidId, error.message);
|
|
91
|
-
throw error;
|
|
92
|
-
}
|
|
93
|
-
const backoff = CONFIG.INITIAL_BACKOFF_MS * Math.pow(CONFIG.BACKOFF_MULTIPLIER, retryAttempts - 1);
|
|
94
|
-
await this.sleep(backoff);
|
|
95
|
-
} else if (error.message.includes('network') || error.message.includes('ECONNRESET')) {
|
|
96
|
-
gatewayIndex = (gatewayIndex + 1) % GATEWAYS.length;
|
|
97
|
-
retryAttempts = 0;
|
|
98
|
-
} else {
|
|
99
|
-
queries.recordDownloadError(downloadId, cidId, error.message);
|
|
100
|
-
throw error;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async downloadFile(url, filepath, resumeFrom = 0, options = {}) {
|
|
107
|
-
return new Promise((resolve, reject) => {
|
|
108
|
-
const protocol = url.startsWith('https') ? https : http;
|
|
109
|
-
const headers = {};
|
|
110
|
-
|
|
111
|
-
if (resumeFrom > 0) {
|
|
112
|
-
headers['Range'] = `bytes=${resumeFrom}-`;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const req = protocol.get(url, { headers, timeout: CONFIG.TIMEOUT_MS }, (res) => {
|
|
116
|
-
if (res.statusCode === 416) {
|
|
117
|
-
reject(new Error('Range not supported - will delete partial and restart'));
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (![200, 206].includes(res.statusCode)) {
|
|
122
|
-
reject(new Error(`HTTP ${res.statusCode}`));
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const contentLength = parseInt(res.headers['content-length'], 10);
|
|
127
|
-
const hash = crypto.createHash('sha256');
|
|
128
|
-
let downloaded = resumeFrom;
|
|
129
|
-
|
|
130
|
-
const mode = resumeFrom > 0 ? 'a' : 'w';
|
|
131
|
-
const stream = fs.createWriteStream(filepath, { flags: mode });
|
|
132
|
-
|
|
133
|
-
res.on('data', (chunk) => {
|
|
134
|
-
hash.update(chunk);
|
|
135
|
-
downloaded += chunk.length;
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
res.pipe(stream);
|
|
139
|
-
|
|
140
|
-
stream.on('finish', () => {
|
|
141
|
-
resolve({ size: downloaded, hash: hash.digest('hex') });
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
stream.on('error', (err) => {
|
|
145
|
-
reject(new Error(`Write error: ${err.message}`));
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
req.on('timeout', () => {
|
|
150
|
-
req.abort();
|
|
151
|
-
reject(new Error('timeout'));
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
req.on('error', (err) => {
|
|
155
|
-
reject(new Error(`network: ${err.message}`));
|
|
156
|
-
});
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
async verifyHash(filepath, expectedHash) {
|
|
161
|
-
return new Promise((resolve, reject) => {
|
|
162
|
-
const hash = crypto.createHash('sha256');
|
|
163
|
-
const stream = fs.createReadStream(filepath);
|
|
164
|
-
|
|
165
|
-
stream.on('data', (chunk) => {
|
|
166
|
-
hash.update(chunk);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
stream.on('end', () => {
|
|
170
|
-
resolve(hash.digest('hex') === expectedHash);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
stream.on('error', (err) => {
|
|
174
|
-
reject(err);
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async resume(downloadId, options = {}) {
|
|
180
|
-
const record = queries.getDownload(downloadId);
|
|
181
|
-
|
|
182
|
-
if (!record) {
|
|
183
|
-
throw new Error('Download not found');
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (record.status === 'success') {
|
|
187
|
-
return record;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const attempts = (record.attempts || 0) + 1;
|
|
191
|
-
if (attempts > CONFIG.MAX_RESUME_ATTEMPTS) {
|
|
192
|
-
throw new Error('Max resume attempts exceeded');
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
try {
|
|
196
|
-
const currentSize = fs.existsSync(record.downloadPath)
|
|
197
|
-
? fs.statSync(record.downloadPath).size
|
|
198
|
-
: 0;
|
|
199
|
-
|
|
200
|
-
if (currentSize === 0) {
|
|
201
|
-
queries.recordDownloadStart(record.cidId, record.downloadPath, record.total_bytes);
|
|
202
|
-
return this.resumeFromOffset(downloadId, record, 0, options);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
queries.markDownloadResuming(downloadId);
|
|
206
|
-
|
|
207
|
-
const downloadPercent = (currentSize / (record.total_bytes || currentSize)) * 100;
|
|
208
|
-
|
|
209
|
-
if (downloadPercent > CONFIG.RESUME_THRESHOLD * 100) {
|
|
210
|
-
return this.resumeFromOffset(downloadId, record, currentSize, options);
|
|
211
|
-
} else {
|
|
212
|
-
await this.cleanupPartial(record.downloadPath);
|
|
213
|
-
return this.resumeFromOffset(downloadId, record, 0, options);
|
|
214
|
-
}
|
|
215
|
-
} catch (error) {
|
|
216
|
-
const newAttempts = (record.attempts || 0) + 1;
|
|
217
|
-
const newStatus = newAttempts >= CONFIG.MAX_RESUME_ATTEMPTS ? 'failed' : 'paused';
|
|
218
|
-
queries.updateDownloadResume(downloadId, record.downloaded_bytes, newAttempts, Date.now(), newStatus);
|
|
219
|
-
|
|
220
|
-
if (newStatus === 'failed') {
|
|
221
|
-
throw error;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
return queries.getDownload(downloadId);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async resumeFromOffset(downloadId, record, offset, options) {
|
|
229
|
-
try {
|
|
230
|
-
const cidRecord = queries.getIpfsCidByModel(record.modelName, record.modelType);
|
|
231
|
-
const gateway = GATEWAYS[0];
|
|
232
|
-
const url = `${gateway}${cidRecord.cid}`;
|
|
233
|
-
|
|
234
|
-
const { size, hash } = await this.downloadFile(
|
|
235
|
-
url,
|
|
236
|
-
record.downloadPath,
|
|
237
|
-
offset,
|
|
238
|
-
options
|
|
239
|
-
);
|
|
240
|
-
|
|
241
|
-
if (options.hashVerify && record.hash) {
|
|
242
|
-
const verified = await this.verifyHash(record.downloadPath, record.hash);
|
|
243
|
-
if (!verified) {
|
|
244
|
-
await this.cleanupPartial(record.downloadPath);
|
|
245
|
-
const newAttempts = (record.attempts || 0) + 1;
|
|
246
|
-
queries.updateDownloadResume(downloadId, 0, newAttempts, Date.now(), 'pending');
|
|
247
|
-
throw new Error('Hash verification failed - restarting');
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
queries.completeDownload(downloadId, record.cidId);
|
|
252
|
-
if (hash) {
|
|
253
|
-
queries.updateDownloadHash(downloadId, hash);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return queries.getDownload(downloadId);
|
|
257
|
-
} catch (error) {
|
|
258
|
-
const newAttempts = (record.attempts || 0) + 1;
|
|
259
|
-
const newStatus = newAttempts >= CONFIG.MAX_RESUME_ATTEMPTS ? 'failed' : 'paused';
|
|
260
|
-
queries.updateDownloadResume(downloadId, offset, newAttempts, Date.now(), newStatus);
|
|
261
|
-
|
|
262
|
-
if (newStatus === 'failed') {
|
|
263
|
-
throw error;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
return queries.getDownload(downloadId);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
async cleanupPartial(filepath) {
|
|
271
|
-
if (fs.existsSync(filepath)) {
|
|
272
|
-
try {
|
|
273
|
-
fs.unlinkSync(filepath);
|
|
274
|
-
} catch (err) {
|
|
275
|
-
console.error(`Failed to cleanup partial file: ${filepath}`, err.message);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
sleep(ms) {
|
|
281
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
getDownloadStatus(downloadId) {
|
|
285
|
-
return queries.getDownload(downloadId);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
listDownloads(status = null) {
|
|
289
|
-
if (status) {
|
|
290
|
-
return queries.getDownloadsByStatus(status);
|
|
291
|
-
}
|
|
292
|
-
const allDownloads = queries.getDownloadsByStatus('in_progress');
|
|
293
|
-
return allDownloads.concat(
|
|
294
|
-
queries.getDownloadsByStatus('success'),
|
|
295
|
-
queries.getDownloadsByStatus('paused'),
|
|
296
|
-
queries.getDownloadsByStatus('failed')
|
|
297
|
-
);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
async cancelDownload(downloadId) {
|
|
301
|
-
const record = queries.getDownload(downloadId);
|
|
302
|
-
if (!record) return false;
|
|
303
|
-
|
|
304
|
-
await this.cleanupPartial(record.downloadPath);
|
|
305
|
-
queries.markDownloadPaused(downloadId, 'Cancelled by user');
|
|
306
|
-
|
|
307
|
-
return true;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
async downloadWithProgress(url, destination, onProgress = null) {
|
|
311
|
-
const dir = path.dirname(destination);
|
|
312
|
-
if (!fs.existsSync(dir)) {
|
|
313
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
let bytesDownloaded = 0;
|
|
317
|
-
let totalBytes = 0;
|
|
318
|
-
let lastProgressTime = Date.now();
|
|
319
|
-
let lastProgressBytes = 0;
|
|
320
|
-
const speeds = [];
|
|
321
|
-
let gatewayIndex = 0;
|
|
322
|
-
let retryCount = 0;
|
|
323
|
-
|
|
324
|
-
const emitProgress = () => {
|
|
325
|
-
const now = Date.now();
|
|
326
|
-
const deltaTime = (now - lastProgressTime) / 1000;
|
|
327
|
-
const deltaBytes = bytesDownloaded - lastProgressBytes;
|
|
328
|
-
const speed = deltaTime > 0 ? Math.round(deltaBytes / deltaTime) : 0;
|
|
329
|
-
|
|
330
|
-
if (speed > 0) {
|
|
331
|
-
speeds.push(speed);
|
|
332
|
-
if (speeds.length > 10) speeds.shift();
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
const avgSpeed = speeds.length > 0 ? Math.round(speeds.reduce((a, b) => a + b, 0) / speeds.length) : 0;
|
|
336
|
-
const eta = avgSpeed > 0 && totalBytes > bytesDownloaded ? Math.round((totalBytes - bytesDownloaded) / avgSpeed) : 0;
|
|
337
|
-
|
|
338
|
-
if (onProgress) {
|
|
339
|
-
onProgress({
|
|
340
|
-
bytesDownloaded,
|
|
341
|
-
bytesRemaining: Math.max(0, totalBytes - bytesDownloaded),
|
|
342
|
-
totalBytes,
|
|
343
|
-
downloadSpeed: avgSpeed,
|
|
344
|
-
eta,
|
|
345
|
-
retryCount,
|
|
346
|
-
currentGateway: url,
|
|
347
|
-
status: bytesDownloaded >= totalBytes ? 'completed' : 'downloading',
|
|
348
|
-
percentComplete: totalBytes > 0 ? Math.round((bytesDownloaded / totalBytes) * 100) : 0,
|
|
349
|
-
timestamp: now
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
lastProgressTime = now;
|
|
354
|
-
lastProgressBytes = bytesDownloaded;
|
|
355
|
-
};
|
|
356
|
-
|
|
357
|
-
return new Promise((resolve, reject) => {
|
|
358
|
-
const attemptDownload = (gateway) => {
|
|
359
|
-
if (onProgress) {
|
|
360
|
-
onProgress({
|
|
361
|
-
bytesDownloaded: 0,
|
|
362
|
-
bytesRemaining: 0,
|
|
363
|
-
totalBytes: 0,
|
|
364
|
-
downloadSpeed: 0,
|
|
365
|
-
eta: 0,
|
|
366
|
-
retryCount,
|
|
367
|
-
currentGateway: gateway,
|
|
368
|
-
status: 'connecting',
|
|
369
|
-
percentComplete: 0,
|
|
370
|
-
timestamp: Date.now()
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
const protocol = gateway.startsWith('https') ? https : http;
|
|
375
|
-
protocol.get(gateway, { timeout: CONFIG.TIMEOUT_MS }, (res) => {
|
|
376
|
-
if ([301, 302, 307, 308].includes(res.statusCode)) {
|
|
377
|
-
const location = res.headers.location;
|
|
378
|
-
if (location) return attemptDownload(location);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
if (res.statusCode !== 200) {
|
|
382
|
-
res.resume();
|
|
383
|
-
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
384
|
-
retryCount++;
|
|
385
|
-
gatewayIndex++;
|
|
386
|
-
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
387
|
-
}
|
|
388
|
-
return reject(new Error(`HTTP ${res.statusCode}`));
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
totalBytes = parseInt(res.headers['content-length'], 10) || 0;
|
|
392
|
-
bytesDownloaded = 0;
|
|
393
|
-
lastProgressBytes = 0;
|
|
394
|
-
lastProgressTime = Date.now();
|
|
395
|
-
|
|
396
|
-
const file = fs.createWriteStream(destination);
|
|
397
|
-
let lastEmit = Date.now();
|
|
398
|
-
|
|
399
|
-
res.on('data', (chunk) => {
|
|
400
|
-
bytesDownloaded += chunk.length;
|
|
401
|
-
const now = Date.now();
|
|
402
|
-
if (now - lastEmit >= 200) {
|
|
403
|
-
emitProgress();
|
|
404
|
-
lastEmit = now;
|
|
405
|
-
}
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
res.on('end', () => {
|
|
409
|
-
emitProgress();
|
|
410
|
-
file.destroy();
|
|
411
|
-
resolve({ destination, bytesDownloaded, success: true });
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
res.on('error', (err) => {
|
|
415
|
-
file.destroy();
|
|
416
|
-
fs.unlink(destination, () => {});
|
|
417
|
-
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
418
|
-
retryCount++;
|
|
419
|
-
gatewayIndex++;
|
|
420
|
-
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
421
|
-
}
|
|
422
|
-
reject(err);
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
file.on('error', (err) => {
|
|
426
|
-
res.destroy();
|
|
427
|
-
fs.unlink(destination, () => {});
|
|
428
|
-
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
429
|
-
retryCount++;
|
|
430
|
-
gatewayIndex++;
|
|
431
|
-
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
432
|
-
}
|
|
433
|
-
reject(err);
|
|
434
|
-
});
|
|
435
|
-
|
|
436
|
-
res.pipe(file);
|
|
437
|
-
}).on('timeout', () => {
|
|
438
|
-
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
439
|
-
retryCount++;
|
|
440
|
-
gatewayIndex++;
|
|
441
|
-
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
442
|
-
}
|
|
443
|
-
reject(new Error('Download timeout'));
|
|
444
|
-
}).on('error', (err) => {
|
|
445
|
-
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
446
|
-
retryCount++;
|
|
447
|
-
gatewayIndex++;
|
|
448
|
-
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
449
|
-
}
|
|
450
|
-
reject(err);
|
|
451
|
-
});
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
attemptDownload(GATEWAYS[0]);
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
export default new IPFSDownloader();
|
|
1
|
+
import https from 'https';
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import crypto from 'crypto';
|
|
6
|
+
import os from 'os';
|
|
7
|
+
import { queries } from '../database.js';
|
|
8
|
+
|
|
9
|
+
const GATEWAYS = [
|
|
10
|
+
'https://ipfs.io/ipfs/',
|
|
11
|
+
'https://gateway.pinata.cloud/ipfs/',
|
|
12
|
+
'https://cloudflare-ipfs.com/ipfs/',
|
|
13
|
+
'https://dweb.link/ipfs/'
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
const CONFIG = {
|
|
17
|
+
MAX_RESUME_ATTEMPTS: 3,
|
|
18
|
+
MAX_RETRY_ATTEMPTS: 3,
|
|
19
|
+
TIMEOUT_MS: 30000,
|
|
20
|
+
INITIAL_BACKOFF_MS: 1000,
|
|
21
|
+
BACKOFF_MULTIPLIER: 2,
|
|
22
|
+
DOWNLOADS_DIR: path.join(os.homedir(), '.gmgui', 'downloads'),
|
|
23
|
+
RESUME_THRESHOLD: 0.5
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
class IPFSDownloader {
|
|
27
|
+
constructor() {
|
|
28
|
+
this.downloads = new Map();
|
|
29
|
+
this.setupDir();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
setupDir() {
|
|
33
|
+
if (!fs.existsSync(CONFIG.DOWNLOADS_DIR)) {
|
|
34
|
+
fs.mkdirSync(CONFIG.DOWNLOADS_DIR, { recursive: true });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async download(cid, filename, options = {}) {
|
|
39
|
+
const filepath = path.join(CONFIG.DOWNLOADS_DIR, filename);
|
|
40
|
+
const { modelName = 'unknown', modelType = 'unknown', modelHash = null } = options;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const cidId = queries.recordIpfsCid(cid, modelName, modelType, modelHash, GATEWAYS[0]);
|
|
44
|
+
const downloadId = queries.recordDownloadStart(cidId, filepath, 0);
|
|
45
|
+
|
|
46
|
+
await this.executeDownload(downloadId, cidId, filepath, options);
|
|
47
|
+
return { success: true, downloadId, filepath, cid };
|
|
48
|
+
} catch (error) {
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async executeDownload(downloadId, cidId, filepath, options = {}) {
|
|
54
|
+
let gatewayIndex = 0;
|
|
55
|
+
let resumeAttempts = 0;
|
|
56
|
+
let retryAttempts = 0;
|
|
57
|
+
|
|
58
|
+
while (true) {
|
|
59
|
+
try {
|
|
60
|
+
const gateway = GATEWAYS[gatewayIndex];
|
|
61
|
+
const cidRecord = queries._db.prepare('SELECT * FROM ipfs_cids WHERE id = ?').get(cidId);
|
|
62
|
+
if (!cidRecord) throw new Error('CID record not found');
|
|
63
|
+
const url = `${gateway}${cidRecord.cid}`;
|
|
64
|
+
|
|
65
|
+
const { size, hash } = await this.downloadFile(
|
|
66
|
+
url,
|
|
67
|
+
filepath,
|
|
68
|
+
0,
|
|
69
|
+
options
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
queries.completeDownload(downloadId, cidId);
|
|
73
|
+
|
|
74
|
+
if (options.hashVerify && hash) {
|
|
75
|
+
queries.updateDownloadHash(downloadId, hash);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return queries.getDownload(downloadId);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error.message.includes('Range')) {
|
|
81
|
+
resumeAttempts++;
|
|
82
|
+
if (resumeAttempts > CONFIG.MAX_RESUME_ATTEMPTS) {
|
|
83
|
+
await this.cleanupPartial(filepath);
|
|
84
|
+
gatewayIndex = (gatewayIndex + 1) % GATEWAYS.length;
|
|
85
|
+
resumeAttempts = 0;
|
|
86
|
+
}
|
|
87
|
+
} else if (error.message.includes('timeout') || error.message.includes('ETIMEDOUT')) {
|
|
88
|
+
retryAttempts++;
|
|
89
|
+
if (retryAttempts > CONFIG.MAX_RETRY_ATTEMPTS) {
|
|
90
|
+
queries.recordDownloadError(downloadId, cidId, error.message);
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
const backoff = CONFIG.INITIAL_BACKOFF_MS * Math.pow(CONFIG.BACKOFF_MULTIPLIER, retryAttempts - 1);
|
|
94
|
+
await this.sleep(backoff);
|
|
95
|
+
} else if (error.message.includes('network') || error.message.includes('ECONNRESET')) {
|
|
96
|
+
gatewayIndex = (gatewayIndex + 1) % GATEWAYS.length;
|
|
97
|
+
retryAttempts = 0;
|
|
98
|
+
} else {
|
|
99
|
+
queries.recordDownloadError(downloadId, cidId, error.message);
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async downloadFile(url, filepath, resumeFrom = 0, options = {}) {
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
const protocol = url.startsWith('https') ? https : http;
|
|
109
|
+
const headers = {};
|
|
110
|
+
|
|
111
|
+
if (resumeFrom > 0) {
|
|
112
|
+
headers['Range'] = `bytes=${resumeFrom}-`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const req = protocol.get(url, { headers, timeout: CONFIG.TIMEOUT_MS }, (res) => {
|
|
116
|
+
if (res.statusCode === 416) {
|
|
117
|
+
reject(new Error('Range not supported - will delete partial and restart'));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (![200, 206].includes(res.statusCode)) {
|
|
122
|
+
reject(new Error(`HTTP ${res.statusCode}`));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const contentLength = parseInt(res.headers['content-length'], 10);
|
|
127
|
+
const hash = crypto.createHash('sha256');
|
|
128
|
+
let downloaded = resumeFrom;
|
|
129
|
+
|
|
130
|
+
const mode = resumeFrom > 0 ? 'a' : 'w';
|
|
131
|
+
const stream = fs.createWriteStream(filepath, { flags: mode });
|
|
132
|
+
|
|
133
|
+
res.on('data', (chunk) => {
|
|
134
|
+
hash.update(chunk);
|
|
135
|
+
downloaded += chunk.length;
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
res.pipe(stream);
|
|
139
|
+
|
|
140
|
+
stream.on('finish', () => {
|
|
141
|
+
resolve({ size: downloaded, hash: hash.digest('hex') });
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
stream.on('error', (err) => {
|
|
145
|
+
reject(new Error(`Write error: ${err.message}`));
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
req.on('timeout', () => {
|
|
150
|
+
req.abort();
|
|
151
|
+
reject(new Error('timeout'));
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
req.on('error', (err) => {
|
|
155
|
+
reject(new Error(`network: ${err.message}`));
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async verifyHash(filepath, expectedHash) {
|
|
161
|
+
return new Promise((resolve, reject) => {
|
|
162
|
+
const hash = crypto.createHash('sha256');
|
|
163
|
+
const stream = fs.createReadStream(filepath);
|
|
164
|
+
|
|
165
|
+
stream.on('data', (chunk) => {
|
|
166
|
+
hash.update(chunk);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
stream.on('end', () => {
|
|
170
|
+
resolve(hash.digest('hex') === expectedHash);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
stream.on('error', (err) => {
|
|
174
|
+
reject(err);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async resume(downloadId, options = {}) {
|
|
180
|
+
const record = queries.getDownload(downloadId);
|
|
181
|
+
|
|
182
|
+
if (!record) {
|
|
183
|
+
throw new Error('Download not found');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (record.status === 'success') {
|
|
187
|
+
return record;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const attempts = (record.attempts || 0) + 1;
|
|
191
|
+
if (attempts > CONFIG.MAX_RESUME_ATTEMPTS) {
|
|
192
|
+
throw new Error('Max resume attempts exceeded');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
const currentSize = fs.existsSync(record.downloadPath)
|
|
197
|
+
? fs.statSync(record.downloadPath).size
|
|
198
|
+
: 0;
|
|
199
|
+
|
|
200
|
+
if (currentSize === 0) {
|
|
201
|
+
queries.recordDownloadStart(record.cidId, record.downloadPath, record.total_bytes);
|
|
202
|
+
return this.resumeFromOffset(downloadId, record, 0, options);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
queries.markDownloadResuming(downloadId);
|
|
206
|
+
|
|
207
|
+
const downloadPercent = (currentSize / (record.total_bytes || currentSize)) * 100;
|
|
208
|
+
|
|
209
|
+
if (downloadPercent > CONFIG.RESUME_THRESHOLD * 100) {
|
|
210
|
+
return this.resumeFromOffset(downloadId, record, currentSize, options);
|
|
211
|
+
} else {
|
|
212
|
+
await this.cleanupPartial(record.downloadPath);
|
|
213
|
+
return this.resumeFromOffset(downloadId, record, 0, options);
|
|
214
|
+
}
|
|
215
|
+
} catch (error) {
|
|
216
|
+
const newAttempts = (record.attempts || 0) + 1;
|
|
217
|
+
const newStatus = newAttempts >= CONFIG.MAX_RESUME_ATTEMPTS ? 'failed' : 'paused';
|
|
218
|
+
queries.updateDownloadResume(downloadId, record.downloaded_bytes, newAttempts, Date.now(), newStatus);
|
|
219
|
+
|
|
220
|
+
if (newStatus === 'failed') {
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return queries.getDownload(downloadId);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async resumeFromOffset(downloadId, record, offset, options) {
|
|
229
|
+
try {
|
|
230
|
+
const cidRecord = queries.getIpfsCidByModel(record.modelName, record.modelType);
|
|
231
|
+
const gateway = GATEWAYS[0];
|
|
232
|
+
const url = `${gateway}${cidRecord.cid}`;
|
|
233
|
+
|
|
234
|
+
const { size, hash } = await this.downloadFile(
|
|
235
|
+
url,
|
|
236
|
+
record.downloadPath,
|
|
237
|
+
offset,
|
|
238
|
+
options
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
if (options.hashVerify && record.hash) {
|
|
242
|
+
const verified = await this.verifyHash(record.downloadPath, record.hash);
|
|
243
|
+
if (!verified) {
|
|
244
|
+
await this.cleanupPartial(record.downloadPath);
|
|
245
|
+
const newAttempts = (record.attempts || 0) + 1;
|
|
246
|
+
queries.updateDownloadResume(downloadId, 0, newAttempts, Date.now(), 'pending');
|
|
247
|
+
throw new Error('Hash verification failed - restarting');
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
queries.completeDownload(downloadId, record.cidId);
|
|
252
|
+
if (hash) {
|
|
253
|
+
queries.updateDownloadHash(downloadId, hash);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return queries.getDownload(downloadId);
|
|
257
|
+
} catch (error) {
|
|
258
|
+
const newAttempts = (record.attempts || 0) + 1;
|
|
259
|
+
const newStatus = newAttempts >= CONFIG.MAX_RESUME_ATTEMPTS ? 'failed' : 'paused';
|
|
260
|
+
queries.updateDownloadResume(downloadId, offset, newAttempts, Date.now(), newStatus);
|
|
261
|
+
|
|
262
|
+
if (newStatus === 'failed') {
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return queries.getDownload(downloadId);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async cleanupPartial(filepath) {
|
|
271
|
+
if (fs.existsSync(filepath)) {
|
|
272
|
+
try {
|
|
273
|
+
fs.unlinkSync(filepath);
|
|
274
|
+
} catch (err) {
|
|
275
|
+
console.error(`Failed to cleanup partial file: ${filepath}`, err.message);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
sleep(ms) {
|
|
281
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
getDownloadStatus(downloadId) {
|
|
285
|
+
return queries.getDownload(downloadId);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
listDownloads(status = null) {
|
|
289
|
+
if (status) {
|
|
290
|
+
return queries.getDownloadsByStatus(status);
|
|
291
|
+
}
|
|
292
|
+
const allDownloads = queries.getDownloadsByStatus('in_progress');
|
|
293
|
+
return allDownloads.concat(
|
|
294
|
+
queries.getDownloadsByStatus('success'),
|
|
295
|
+
queries.getDownloadsByStatus('paused'),
|
|
296
|
+
queries.getDownloadsByStatus('failed')
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
async cancelDownload(downloadId) {
|
|
301
|
+
const record = queries.getDownload(downloadId);
|
|
302
|
+
if (!record) return false;
|
|
303
|
+
|
|
304
|
+
await this.cleanupPartial(record.downloadPath);
|
|
305
|
+
queries.markDownloadPaused(downloadId, 'Cancelled by user');
|
|
306
|
+
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async downloadWithProgress(url, destination, onProgress = null) {
|
|
311
|
+
const dir = path.dirname(destination);
|
|
312
|
+
if (!fs.existsSync(dir)) {
|
|
313
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
let bytesDownloaded = 0;
|
|
317
|
+
let totalBytes = 0;
|
|
318
|
+
let lastProgressTime = Date.now();
|
|
319
|
+
let lastProgressBytes = 0;
|
|
320
|
+
const speeds = [];
|
|
321
|
+
let gatewayIndex = 0;
|
|
322
|
+
let retryCount = 0;
|
|
323
|
+
|
|
324
|
+
const emitProgress = () => {
|
|
325
|
+
const now = Date.now();
|
|
326
|
+
const deltaTime = (now - lastProgressTime) / 1000;
|
|
327
|
+
const deltaBytes = bytesDownloaded - lastProgressBytes;
|
|
328
|
+
const speed = deltaTime > 0 ? Math.round(deltaBytes / deltaTime) : 0;
|
|
329
|
+
|
|
330
|
+
if (speed > 0) {
|
|
331
|
+
speeds.push(speed);
|
|
332
|
+
if (speeds.length > 10) speeds.shift();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const avgSpeed = speeds.length > 0 ? Math.round(speeds.reduce((a, b) => a + b, 0) / speeds.length) : 0;
|
|
336
|
+
const eta = avgSpeed > 0 && totalBytes > bytesDownloaded ? Math.round((totalBytes - bytesDownloaded) / avgSpeed) : 0;
|
|
337
|
+
|
|
338
|
+
if (onProgress) {
|
|
339
|
+
onProgress({
|
|
340
|
+
bytesDownloaded,
|
|
341
|
+
bytesRemaining: Math.max(0, totalBytes - bytesDownloaded),
|
|
342
|
+
totalBytes,
|
|
343
|
+
downloadSpeed: avgSpeed,
|
|
344
|
+
eta,
|
|
345
|
+
retryCount,
|
|
346
|
+
currentGateway: url,
|
|
347
|
+
status: bytesDownloaded >= totalBytes ? 'completed' : 'downloading',
|
|
348
|
+
percentComplete: totalBytes > 0 ? Math.round((bytesDownloaded / totalBytes) * 100) : 0,
|
|
349
|
+
timestamp: now
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
lastProgressTime = now;
|
|
354
|
+
lastProgressBytes = bytesDownloaded;
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
return new Promise((resolve, reject) => {
|
|
358
|
+
const attemptDownload = (gateway) => {
|
|
359
|
+
if (onProgress) {
|
|
360
|
+
onProgress({
|
|
361
|
+
bytesDownloaded: 0,
|
|
362
|
+
bytesRemaining: 0,
|
|
363
|
+
totalBytes: 0,
|
|
364
|
+
downloadSpeed: 0,
|
|
365
|
+
eta: 0,
|
|
366
|
+
retryCount,
|
|
367
|
+
currentGateway: gateway,
|
|
368
|
+
status: 'connecting',
|
|
369
|
+
percentComplete: 0,
|
|
370
|
+
timestamp: Date.now()
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const protocol = gateway.startsWith('https') ? https : http;
|
|
375
|
+
protocol.get(gateway, { timeout: CONFIG.TIMEOUT_MS }, (res) => {
|
|
376
|
+
if ([301, 302, 307, 308].includes(res.statusCode)) {
|
|
377
|
+
const location = res.headers.location;
|
|
378
|
+
if (location) return attemptDownload(location);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (res.statusCode !== 200) {
|
|
382
|
+
res.resume();
|
|
383
|
+
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
384
|
+
retryCount++;
|
|
385
|
+
gatewayIndex++;
|
|
386
|
+
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
387
|
+
}
|
|
388
|
+
return reject(new Error(`HTTP ${res.statusCode}`));
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
totalBytes = parseInt(res.headers['content-length'], 10) || 0;
|
|
392
|
+
bytesDownloaded = 0;
|
|
393
|
+
lastProgressBytes = 0;
|
|
394
|
+
lastProgressTime = Date.now();
|
|
395
|
+
|
|
396
|
+
const file = fs.createWriteStream(destination);
|
|
397
|
+
let lastEmit = Date.now();
|
|
398
|
+
|
|
399
|
+
res.on('data', (chunk) => {
|
|
400
|
+
bytesDownloaded += chunk.length;
|
|
401
|
+
const now = Date.now();
|
|
402
|
+
if (now - lastEmit >= 200) {
|
|
403
|
+
emitProgress();
|
|
404
|
+
lastEmit = now;
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
res.on('end', () => {
|
|
409
|
+
emitProgress();
|
|
410
|
+
file.destroy();
|
|
411
|
+
resolve({ destination, bytesDownloaded, success: true });
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
res.on('error', (err) => {
|
|
415
|
+
file.destroy();
|
|
416
|
+
fs.unlink(destination, () => {});
|
|
417
|
+
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
418
|
+
retryCount++;
|
|
419
|
+
gatewayIndex++;
|
|
420
|
+
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
421
|
+
}
|
|
422
|
+
reject(err);
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
file.on('error', (err) => {
|
|
426
|
+
res.destroy();
|
|
427
|
+
fs.unlink(destination, () => {});
|
|
428
|
+
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
429
|
+
retryCount++;
|
|
430
|
+
gatewayIndex++;
|
|
431
|
+
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
432
|
+
}
|
|
433
|
+
reject(err);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
res.pipe(file);
|
|
437
|
+
}).on('timeout', () => {
|
|
438
|
+
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
439
|
+
retryCount++;
|
|
440
|
+
gatewayIndex++;
|
|
441
|
+
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
442
|
+
}
|
|
443
|
+
reject(new Error('Download timeout'));
|
|
444
|
+
}).on('error', (err) => {
|
|
445
|
+
if (gatewayIndex < GATEWAYS.length - 1) {
|
|
446
|
+
retryCount++;
|
|
447
|
+
gatewayIndex++;
|
|
448
|
+
return setTimeout(() => attemptDownload(GATEWAYS[gatewayIndex]), 1000 * Math.pow(2, Math.min(retryCount, 3)));
|
|
449
|
+
}
|
|
450
|
+
reject(err);
|
|
451
|
+
});
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
attemptDownload(GATEWAYS[0]);
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export default new IPFSDownloader();
|