@@ -7,8 +7,9 @@ import https from "https";
7
7
import os from "os" ;
8
8
import path from "path" ;
9
9
import ProgressStream from "progress-stream" ;
10
+ import * as zlib from "zlib" ;
10
11
11
- import { asyncPool } from "../utils/asyncPool " ;
12
+ import { asyncPool } from "../utils/promiseUtils " ;
12
13
import { createSpinner } from "nanospinner" ;
13
14
import { getFilePathFromSha256 } from "../utils/hashUtils" ;
14
15
import { getOrCreateSSLCerts } from "../utils/ssl" ;
@@ -18,7 +19,7 @@ import { PassThrough } from "stream";
18
19
import { promptCredentials } from "../utils/credentialsUtils" ;
19
20
import { STORE_PATH } from "../utils/config" ;
20
21
import { Wallet , DataStore } from "../blockchain" ;
21
- import { formatHost } from ' ../utils/network' ;
22
+ import { formatHost } from " ../utils/network" ;
22
23
23
24
// Helper function to trim long filenames with ellipsis and ensure consistent padding
24
25
function formatFilename ( filename : string | undefined , maxLength = 30 ) : string {
@@ -121,7 +122,9 @@ export class PropagationServer {
121
122
httpsAgent : this . createHttpsAgent ( ) ,
122
123
} ;
123
124
124
- let url = `https://${ formatHost ( this . ipAddress ) } :${ PropagationServer . port } /${ this . storeId } ` ;
125
+ let url = `https://${ formatHost ( this . ipAddress ) } :${
126
+ PropagationServer . port
127
+ } /${ this . storeId } `;
125
128
if ( rootHash ) {
126
129
url += `?hasRootHash=${ rootHash } ` ;
127
130
}
@@ -194,7 +197,9 @@ export class PropagationServer {
194
197
} ;
195
198
}
196
199
197
- const url = `https://${ formatHost ( this . ipAddress ) } :${ PropagationServer . port } /upload/${ this . storeId } ?roothash=${ rootHash } ` ;
200
+ const url = `https://${ formatHost ( this . ipAddress ) } :${
201
+ PropagationServer . port
202
+ } /upload/${ this . storeId } ?roothash=${ rootHash } `;
198
203
const response = await axios . post ( url , formData , config ) ;
199
204
200
205
this . sessionId = response . data . sessionId ;
@@ -223,7 +228,9 @@ export class PropagationServer {
223
228
httpsAgent : this . createHttpsAgent ( ) ,
224
229
} ;
225
230
226
- const url = `https://${ formatHost ( this . ipAddress ) } :${ PropagationServer . port } /upload/${ this . storeId } /${ this . sessionId } /${ filename } ` ;
231
+ const url = `https://${ formatHost ( this . ipAddress ) } :${
232
+ PropagationServer . port
233
+ } /upload/${ this . storeId } /${ this . sessionId } /${ filename } `;
227
234
const response = await axios . head ( url , config ) ;
228
235
229
236
// Check for 'x-file-exists' header
@@ -246,7 +253,7 @@ export class PropagationServer {
246
253
* Upload a file to the server by sending a PUT request.
247
254
* Logs progress using a local cli-progress bar.
248
255
*/
249
- async uploadFile ( label : string , dataPath : string ) {
256
+ async uploadFile ( label : string , dataPath : string , uncompress : boolean = false ) {
250
257
const filePath = path . join ( STORE_PATH , this . storeId , dataPath ) ;
251
258
252
259
const { nonce, fileExists } = await this . getFileNonce ( dataPath ) ;
@@ -306,8 +313,20 @@ export class PropagationServer {
306
313
PropagationServer . inactivityTimeout
307
314
) ;
308
315
309
- // Pipe the read stream through the progress stream into the PassThrough stream
310
- fileReadStream . pipe ( progressStream ) . pipe ( passThroughStream ) ;
316
+ // Decide whether to uncompress the file during upload
317
+ if ( uncompress ) {
318
+ // Create a gunzip (uncompression) stream
319
+ const gunzip = zlib . createGunzip ( ) ;
320
+
321
+ // Pipe the streams: fileReadStream -> gunzip -> progressStream -> passThroughStream
322
+ fileReadStream
323
+ . pipe ( gunzip )
324
+ . pipe ( progressStream )
325
+ . pipe ( passThroughStream ) ;
326
+ } else {
327
+ // Pipe the streams: fileReadStream -> progressStream -> passThroughStream
328
+ fileReadStream . pipe ( progressStream ) . pipe ( passThroughStream ) ;
329
+ }
311
330
312
331
// Use form-data to construct the request body
313
332
const formData = new FormData ( ) ;
@@ -327,7 +346,9 @@ export class PropagationServer {
327
346
maxBodyLength : Infinity ,
328
347
} ;
329
348
330
- const url = `https://${ formatHost ( this . ipAddress ) } :${ PropagationServer . port } /upload/${ this . storeId } /${ this . sessionId } /${ dataPath } ` ;
349
+ const url = `https://${ formatHost ( this . ipAddress ) } :${
350
+ PropagationServer . port
351
+ } /upload/${ this . storeId } /${ this . sessionId } /${ dataPath } `;
331
352
332
353
// Create a promise that resolves when the progress stream ends
333
354
const progressPromise = new Promise < void > ( ( resolve , reject ) => {
@@ -369,7 +390,9 @@ export class PropagationServer {
369
390
: undefined ,
370
391
} ;
371
392
372
- const url = `https://${ formatHost ( this . ipAddress ) } :${ PropagationServer . port } /commit/${ this . storeId } /${ this . sessionId } ` ;
393
+ const url = `https://${ formatHost ( this . ipAddress ) } :${
394
+ PropagationServer . port
395
+ } /commit/${ this . storeId } /${ this . sessionId } `;
373
396
const response = await axios . post ( url , { } , config ) ;
374
397
375
398
spinner . success ( {
@@ -424,8 +447,7 @@ export class PropagationServer {
424
447
await propagationServer . startUploadSession ( rootHash ) ;
425
448
426
449
const dataStore = DataStore . from ( storeId ) ;
427
- const files = await dataStore . getFileSetForRootHash ( rootHash ) ;
428
-
450
+ const files = await dataStore . getFileSetForRootHash ( rootHash , true ) ;
429
451
// Prepare upload tasks
430
452
const uploadTasks = files . map ( ( file ) => ( {
431
453
label : file . name ,
@@ -436,7 +458,7 @@ export class PropagationServer {
436
458
const concurrencyLimit = 10 ; // Adjust this number as needed
437
459
438
460
await asyncPool ( concurrencyLimit , uploadTasks , async ( task ) => {
439
- await propagationServer . uploadFile ( task . label , task . dataPath ) ;
461
+ await propagationServer . uploadFile ( task . label , task . dataPath , true ) ;
440
462
} ) ;
441
463
442
464
// Commit the session after all files have been uploaded
@@ -454,7 +476,9 @@ export class PropagationServer {
454
476
* Logs progress using a local cli-progress bar.
455
477
*/
456
478
async fetchFile ( dataPath : string ) : Promise < Buffer > {
457
- const url = `https://${ formatHost ( this . ipAddress ) } :${ PropagationServer . port } /fetch/${ this . storeId } /${ dataPath } ` ;
479
+ const url = `https://${ formatHost ( this . ipAddress ) } :${
480
+ PropagationServer . port
481
+ } /fetch/${ this . storeId } /${ dataPath } `;
458
482
const config : AxiosRequestConfig = {
459
483
responseType : "stream" ,
460
484
httpsAgent : this . createHttpsAgent ( ) ,
@@ -545,7 +569,9 @@ export class PropagationServer {
545
569
httpsAgent : this . createHttpsAgent ( ) ,
546
570
} ;
547
571
548
- const url = `https://${ formatHost ( this . ipAddress ) } :${ PropagationServer . port } /store/${ this . storeId } /${ rootHash } /${ dataPath } ` ;
572
+ const url = `https://${ formatHost ( this . ipAddress ) } :${
573
+ PropagationServer . port
574
+ } /store/${ this . storeId } /${ rootHash } /${ dataPath } `;
549
575
const response = await axios . head ( url , config ) ;
550
576
551
577
// Check the headers for file existence and size
@@ -575,7 +601,9 @@ export class PropagationServer {
575
601
rootHash : string ,
576
602
baseDir : string
577
603
) {
578
- const url = `https://${ formatHost ( this . ipAddress ) } :${ PropagationServer . port } /fetch/${ this . storeId } /${ dataPath } ` ;
604
+ const url = `https://${ formatHost ( this . ipAddress ) } :${
605
+ PropagationServer . port
606
+ } /fetch/${ this . storeId } /${ dataPath } `;
579
607
let downloadPath = path . join ( baseDir , dataPath ) ;
580
608
581
609
// Ensure that the directory for the file exists
0 commit comments