Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
getChunksFromBody(body, headers) {
if (!body) {
return [];
}
if (Buffer.isBuffer(body)) {
return [body];
}
// If content-encoding is set in the header then the body/content
// is as an array of hex strings
if (isContentEncoded(headers)) {
const hexChunks = JSON.parse(body);
return hexChunks.map(chunk => Buffer.from(chunk, 'hex'));
}
const buffer = Buffer.from(body);
// The body can be one of two things:
// 1. A hex string which then means its binary data.
// 2. A utf8 string which means a regular string.
return [Buffer.from(buffer, isUtf8Representable(buffer) ? 'utf8' : 'hex')];
}
}
getBodyFromChunks(chunks, headers) {
// If content-encoding is set in the header then the body/content
// should not be concatenated. Instead, the chunks should
// be preserved as-is so that each chunk can be mocked individually
if (isContentEncoded(headers)) {
const hexChunks = chunks.map(chunk => {
if (!Buffer.isBuffer(chunk)) {
this.assert(
'content-encoded responses must all be binary buffers',
typeof chunk === 'string'
);
chunk = Buffer.from(chunk);
}
return chunk.toString('hex');
});
return JSON.stringify(hexChunks);
}
const buffer = mergeChunks(chunks);