Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 102 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,108 @@
'use strict';

const http = require('http');
const { IncomingForm } = require('formidable');
const zlib = require('zlib');
const fs = require('fs');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'POST' && req.url === '/compress') {
const form = new IncomingForm({ multiples: false });

form.parse(req, (err, fields, files) => {
if (err) {
res.statusCode = 500;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirement states that for an invalid form, the server should respond with a 400 status code. A form parsing error typically means the form data is malformed or invalid, so returning 400 would be more appropriate than 500.


return res.end('Parsing form error');
}

const compressionType = Array.isArray(fields.compressionType)
? fields.compressionType[0]
: fields.compressionType;
const file = files.file;

if (!file) {
res.statusCode = 400;

return res.end('Missing file');
}

if (!compressionType) {
res.statusCode = 400;

return res.end('Missing compression type');
}

const originalFileName = file[0].originalFilename;
const filePath = file[0].filepath.replace(/\\/g, '/');

let compressor;
let ext;

switch (compressionType) {
case 'gzip':
compressor = zlib.createGzip();
ext = '.gzip';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task description, the file extension for gzip should be .gz, not .gzip.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task description, the extension for 'gzip' compression should be '.gz', not '.gzip'.

break;

case 'deflate':
compressor = zlib.createDeflate();
ext = '.deflate';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task description, the file extension for deflate should be .dfl, not .deflate.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task description requires the file extension for 'deflate' compression to be '.dfl', not '.deflate'.

break;
Comment on lines +43 to +52
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compression type values submitted by the form are 'gzip' and 'deflate', but the switch statement is checking for 'gz' and 'dfl'. This discrepancy will cause 'gzip' and 'deflate' requests to be incorrectly handled by the default case, resulting in an 'Unsupported compression type' error. To fix this, you should change the case statements to 'gzip' and 'deflate' to match the values in the HTML form's <option> tags.


case 'br':
compressor = zlib.createBrotliCompress();
ext = '.br';
break;

default:
res.statusCode = 400;
res.end('Unsupported compression type');

return;
}

const compressedFileName = originalFileName + ext;

if (file && compressor) {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/octet-stream');

res.setHeader(
'Content-Disposition',
`attachment; filename=${compressedFileName}`,
);

const readStream = fs.createReadStream(filePath);

readStream.pipe(compressor).pipe(res);
} else {
res.statusCode = 500;
res.end('Error processing file or compressor');
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This else block is unreachable. The file variable is validated on line 25, and the compressor is guaranteed to be assigned if the code proceeds past the switch statement (as the default case returns). Therefore, the condition file && compressor on line 68 will always be true. This else block and the surrounding if can be removed to simplify the code.

});
} else if (req.method === 'GET' && req.url === '/') {
res.statusCode = 200;
res.setHeader('Content-type', 'text/html');

res.end(`<form method="POST" action="/compress" enctype="multipart/form-data">
<input name="file" type="file">
<select name="compressionType">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<button type="submit">Submit</button>
</form>`);
} else if (req.method === 'GET' && req.url === '/compress') {
res.statusCode = 400;
res.end('GET not allowed');
} else {
res.statusCode = 404;
res.end('Not found');
}
});
}

module.exports = {
Expand Down