Skip to content

Commit

Permalink
fix: avoid Buffer re-encode during compression (#184)
Browse files Browse the repository at this point in the history
If the data is already a Buffer then just compress it, don't convert it to a string and back to a Buffer again before compressing it.

A test with Buffer input is added. The input is chosen carefully to ensure that if it was assumed to be UTF-8 encoded it would not be the same when decompressed and added an additional assertion for the decompressed data to match the original.
  • Loading branch information
ricellis authored Dec 17, 2021
1 parent d71f6da commit 8b82f36
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/request-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ export class RequestWrapper {
if (isStream(data)) {
const streamData = await streamToPromise(data);
reqBuffer = Buffer.isBuffer(streamData) ? streamData : Buffer.from(streamData);
} else if (Buffer.isBuffer(data)) {
reqBuffer = data;
} else if (data.toString && data.toString() !== '[object Object]' && !Array.isArray(data)) {
// this handles pretty much any primitive that isnt a JSON object or array
reqBuffer = Buffer.from(data.toString());
Expand Down
13 changes: 13 additions & 0 deletions test/unit/request-wrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,19 @@ describe('gzipRequestBody', () => {
expect(headers['Content-Encoding']).toBe('gzip');
});

it('should compress buffer data into a buffer', async () => {
// Use an invalid UTF-8 overlong encoding as example binary data
const originalData = Buffer.from('f08282ac', 'hex');
const headers = {};

const data = await requestWrapperInstance.gzipRequestBody(originalData, headers);
expect(data).toBeInstanceOf(Buffer);
expect(gzipSpy).toHaveBeenCalled();
expect(headers['Content-Encoding']).toBe('gzip');
// If UTF-8 has been assumed the data will not match
expect(zlib.gunzipSync(data)).toEqual(originalData);
});

it('should log an error and return data unaltered if data cant be stringified', async () => {
let data = { key: 'value' };
data.circle = data;
Expand Down

0 comments on commit 8b82f36

Please sign in to comment.