Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task solution #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
82 changes: 79 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,85 @@
/* eslint-disable no-console */
'use strict';

const http = require('node:http');
const fs = require('fs');
const { createTransformer } = require('./services/createTransformer');
const formidable = require('formidable');
const { pipeline } = require('node:stream');

const OK = 200;
const BAD_REQUEST = 400;
const NOT_FOUND = 404;
const availableCompressionTypes = ['gzip', 'deflate', 'br'];

function createServer() {
/* Write your code here */
// Return instance of http.Server class
}
return http.createServer(async(req, res) => {
switch (req.url) {
case '/':
case '/index.html':
res.writeHead(OK, { 'Content-Type': 'text/html' });

fs.createReadStream('public/index.html')
.pipe(res);

break;

case '/compress':
if (req.method !== 'POST') {
res.writeHead(BAD_REQUEST, { 'Content-Type': 'text/plain' });
res.end('Wrong form method');

return;
}

const form = new formidable.IncomingForm();

form.parse(
req,
(err, { compressionType: compressionTypesArr }, { file: files }) => {
if (err || !compressionTypesArr || !files) {
res.writeHead(BAD_REQUEST, { 'Content-Type': 'text/plain' });
res.end('Both file and compression type are required');

return;
}

const [compressionType] = compressionTypesArr;
const [file] = files;

if (!availableCompressionTypes.includes(compressionType)) {
res.writeHead(BAD_REQUEST, { 'Content-Type': 'text/plain' });
res.end('Unsupported compression type');

return;
}

const transformer = createTransformer(compressionType);

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

pipeline(
fs.createReadStream(file.filepath),
transformer,
res,
(error) => {
if (error) {
console.error('error -', error);
}
});
});

break;

default:
res.writeHead(NOT_FOUND, { 'Content-Type': 'text/plain' });
res.end('Not found');
}
});
};

module.exports = {
createServer,
Expand Down
18 changes: 18 additions & 0 deletions src/services/createTransformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const zlib = require('zlib');

const createTransformer = (transformer) => {
switch (transformer) {
case 'gzip':
return zlib.createGzip();
case 'br':
return zlib.createBrotliCompress();
case 'deflate':
return zlib.createDeflate();
}
};

module.exports = {
createTransformer,
};
Loading