Skip to content

Commit fd7163b

Browse files
crimson-gaoyndu13
authored andcommitted
fix prepend size
1 parent 0a8307f commit fd7163b

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

alibabacloud-gateway-sls/util/ts/src/common.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import { Readable } from 'stream';
22

33
export 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
}

alibabacloud-gateway-sls/util/ts/src/compress.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import zlib from 'zlib';
44
const supportedCompressor = ['lz4', 'gzip', 'deflate']
55

66
export 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

1011
export async function gzipCompress(data: Buffer): Promise<Buffer> {

alibabacloud-gateway-sls/util/ts/src/decompress.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { Readable } from 'stream';
2-
import { readableStreamToBuffer } from './common';
2+
import { readableStreamToBuffer, readableStreamWithPrependBuffer } from './common';
33
import {uncompress as napiLz4Uncompress} from 'lz4-napi';
44
import zlib from 'zlib';
5-
5+
import fs from 'fs';
66
const supportedDecompressor = ['lz4', 'gzip', 'deflate']
77

88
export async function isDecompressorAvailable(compressType: string): Promise<boolean> {
99
return supportedDecompressor.includes(compressType);
1010
}
1111

1212
export 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
}

0 commit comments

Comments
 (0)