@@ -24,6 +24,14 @@ const execAsync = promisify(exec);
2424const execFileAsync = promisify ( execFile ) ;
2525const writeFileAsync = promisify ( writeFile ) ;
2626
27+ async function streamToBuffer ( stream ) {
28+ const chunks = [ ] ;
29+ for await ( const chunk of stream ) {
30+ chunks . push ( Buffer . isBuffer ( chunk ) ? chunk : Buffer . from ( chunk ) ) ;
31+ }
32+ return Buffer . concat ( chunks ) ;
33+ }
34+
2735// Get the expected end values for various ranges (e.g., '-10', '10-', '-')
2836function getOuterRange ( range , bytes ) {
2937 const arr = range . split ( '-' ) ;
@@ -62,11 +70,11 @@ function checkRanges(range, bytes) {
6270 assert . deepStrictEqual ( res . ContentType , 'application/octet-stream' ) ;
6371 assert . deepStrictEqual ( res . Metadata , { } ) ;
6472
65- // Write a file using the buffer so getRangeExec can then check bytes .
66- // If the getRangeExec program fails, then the range is incorrect.
67- return writeFileAsync ( `hashedFile.${ bytes } .${ range } ` , res . Body )
68- . then ( ( ) => execFileAsync ( './getRangeExec' , [ '--check' , '--size' , total ,
69- '--offset' , begin , `hashedFile.${ bytes } .${ range } ` ] ) ) ;
73+ // SDK v3 returns a stream body: convert before writing to disk .
74+ return streamToBuffer ( res . Body )
75+ . then ( bodyBuffer => writeFileAsync ( `hashedFile.${ bytes } .${ range } ` , bodyBuffer ) )
76+ . then ( ( ) => execFileAsync ( './getRangeExec' , [ '--check' , '--size' , total ,
77+ '--offset' , begin , `hashedFile.${ bytes } .${ range } ` ] ) ) ;
7078 } ) ;
7179}
7280
@@ -82,7 +90,7 @@ async function uploadParts(bytes, uploadId) {
8290 `skip=${ part - 1 } ` ,
8391 'count=1' ,
8492 ] ) ;
85- await s3 . send ( new UploadPartCommand ( {
93+ return s3 . send ( new UploadPartCommand ( {
8694 Bucket : bucket ,
8795 Key : key ,
8896 PartNumber : part ,
0 commit comments