File tree Expand file tree Collapse file tree 3 files changed +22
-12
lines changed
alibabacloud-gateway-sls/util/ts/src Expand file tree Collapse file tree 3 files changed +22
-12
lines changed Original file line number Diff line number Diff line change @@ -2,11 +2,16 @@ import { Readable } from 'stream';
22
33export async function readableStreamToBuffer ( stream : Readable ) : Promise < Buffer > {
44 const chunks : Buffer [ ] = [ ] ;
5- return new Promise ( ( resolve , reject ) => {
6- stream . on ( 'data' , ( chunk : Buffer ) => chunks . push ( chunk ) ) ;
7- stream . on ( 'error' , reject ) ;
8- stream . on ( 'end' , ( ) => {
9- resolve ( Buffer . concat ( chunks ) ) ;
10- } ) ;
11- } ) ;
5+ for await ( const chunk of stream ) {
6+ chunks . push ( chunk as Buffer ) ;
7+ }
8+ return Buffer . concat ( chunks ) ;
9+ }
10+
11+ export async function readableStreamWithPrependBuffer ( stream : Readable , prependBuffer : Buffer ) : Promise < Buffer > {
12+ const chunks : Buffer [ ] = [ ] ;
13+ for await ( const chunk of stream ) {
14+ chunks . push ( chunk as Buffer ) ;
15+ }
16+ return Buffer . concat ( [ prependBuffer , ...chunks ] ) ;
1217}
Original file line number Diff line number Diff line change @@ -4,7 +4,8 @@ import zlib from 'zlib';
44const supportedCompressor = [ 'lz4' , 'gzip' , 'deflate' ]
55
66export async function lz4Compress ( data : Buffer ) : Promise < Buffer > {
7- return await napiLz4Compress ( data ) ;
7+ // eliminate 4 bytes prepended size
8+ return ( await napiLz4Compress ( data ) ) . slice ( 4 ) ;
89}
910
1011export async function gzipCompress ( data : Buffer ) : Promise < Buffer > {
Original file line number Diff line number Diff line change 11import { Readable } from 'stream' ;
2- import { readableStreamToBuffer } from './common' ;
2+ import { readableStreamToBuffer , readableStreamWithPrependBuffer } from './common' ;
33import { uncompress as napiLz4Uncompress } from 'lz4-napi' ;
44import zlib from 'zlib' ;
5-
5+ import fs from 'fs' ;
66const supportedDecompressor = [ 'lz4' , 'gzip' , 'deflate' ]
77
88export async function isDecompressorAvailable ( compressType : string ) : Promise < boolean > {
99 return supportedDecompressor . includes ( compressType ) ;
1010}
1111
1212export async function lz4Decompress ( input : Readable , bodyRawSize : number ) : Promise < Buffer > {
13- const napiOutput = await napiLz4Uncompress ( await readableStreamToBuffer ( input ) ) ;
13+ // prepend 4 bytes size to the buffer
14+ const sizeBuffer = Buffer . alloc ( 4 ) ;
15+ sizeBuffer . writeUInt32LE ( bodyRawSize , 0 ) ;
16+ let compressedBuffer = await readableStreamWithPrependBuffer ( input , sizeBuffer ) ;
17+ const napiOutput = await napiLz4Uncompress ( compressedBuffer ) ;
1418 if ( napiOutput . length !== bodyRawSize ) {
15- return Buffer . from ( napiOutput . slice ( 0 , bodyRawSize ) ) ;
19+ throw new Error ( `unexpected uncompressed size: ${ napiOutput . length } , expected: ${ bodyRawSize } ` ) ;
1620 }
1721 return napiOutput ;
1822}
You can’t perform that action at this time.
0 commit comments