Skip to content

Commit

Permalink
added object literal as switch for compressors, fixed reach condition
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfujii committed Oct 18, 2024
1 parent ef82781 commit 70f9058
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/createServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function createServer() {
return;
}

if (req.url === '/compress' && req.method === 'GET') {
if (req.url === '/compress' && req.method !== 'POST') {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');

Expand All @@ -40,13 +40,20 @@ function createServer() {
return;
}

if (!['gzip', 'br', 'deflate'].includes(compressionType[0])) {
const compressors = {
gzip: zlib.createGzip,
br: zlib.createBrotliCompress,
deflate: zlib.createDeflate,
};

if (!Object.keys(compressors).includes(compressionType[0])) {
res.statusCode = 400;
res.end('Unsupported compression type');

return;
}

const compressorType = compressors[compressionType[0]]();
const fileStream = fs.createReadStream(file[0].filepath);

res.statusCode = 200;
Expand All @@ -56,19 +63,6 @@ function createServer() {
`attachment; filename=${file[0].originalFilename}.${compressionType[0]}`,
);

let compressorType;

if (compressionType[0] === 'gzip') {
compressorType = zlib.createGzip();
} else if (compressionType[0] === 'br') {
compressorType = zlib.createBrotliCompress();
} else if (compressionType[0] === 'deflate') {
compressorType = zlib.createDeflate();
} else {
res.statusCode = 400;
res.end('unsupported compression type');
}

pipeline(fileStream, compressorType, res, (error) => {
if (error) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 70f9058

Please sign in to comment.