Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/compress" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<select name="compressionType">
<option value="gzip">GZIP Compression</option>
<option value="deflate">DEFLATE Compression</option>
<option value="br">BR Compression</option>
</select>

<button type="submit">Submit</button>
</form>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
102 changes: 100 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,106 @@
/* eslint-disable no-console */
'use strict';

const http = require('node:http');
const fs = require('node:fs');
const path = require('node:path');
const mime = require('mime-types');
const zlib = require('node:zlib');
const { formidable } = require('formidable');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
const url = new URL(req.url || '', `http://${req.headers.host}`);
const requestPath = url.pathname.slice(1) || 'index.html';
const realPath = requestPath;

const compressionMap = {
gzip: () => zlib.createGzip(),
deflate: () => zlib.createDeflate(),
br: () => zlib.createBrotliCompress(),
};

if (req.url === '/compress' && req.method === 'POST') {
const form = formidable({ multiples: false });

form.parse(req, (err, fields, files) => {
if (err) {
res.statusCode = 400;
res.end('Invalid form');

return;
}

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

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

if (!file || !compressionType) {
res.statusCode = 400;
res.end('Invalid form');

return;
}

const createCompressionStream = compressionMap[compressionType];

if (!createCompressionStream) {
res.statusCode = 400;
res.end('Unsupported compression type');

return;
}

const readStream = fs.createReadStream(file.filepath);
const compressStream = createCompressionStream();
const outputFileName = `${file.originalFilename}.${compressionType}`;
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 output filename extension does not match the requirements. The task specifies .gz for gzip compression and .dfl for deflate. Currently, the code appends the full compression type name (e.g., file.txt.gzip), which is incorrect. Consider creating a mapping from the compression type to the correct file extension.


res.statusCode = 200;

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

readStream.on('error', () => {
res.statusCode = 500;
res.end('Server error');
});

compressStream.on('error', () => {
res.statusCode = 500;
res.end('Server error');
});

readStream.pipe(compressStream).pipe(res);
});

return;
}

if (req.url === '/compress' && req.method === 'GET') {
res.statusCode = 400;
res.end('Wrong URL');

return;
}

if (req.url === '/' && req.method === 'GET') {
const fileStream = fs.createReadStream(realPath);
const mimeType = mime.contentType(path.extname(realPath)) || 'text/plain';

res.statusCode = 200;
res.setHeader('Content-Type', mimeType);
fileStream.pipe(res);
} else {
if (!fs.existsSync(realPath)) {
res.statusCode = 404;
res.end('Not Found');
}
}
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 logic for handling 404 errors is not quite right. While it correctly returns a 404 for a path that doesn't exist on the filesystem, it fails to send any response for paths that do exist but aren't explicitly handled (e.g., /package.json if it exists). This will cause the client's request to hang and eventually time out. Any request that doesn't match a defined endpoint like / or /compress should receive a 404 response.

});
}

module.exports = {
Expand Down
Loading