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

solution #101

Open
wants to merge 3 commits 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
5,903 changes: 4,015 additions & 1,888 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"init": "mate-scripts init",
"start": "node src/index.js",
"watch": "nodemon src/index.js",
"lint": "npm run format && mate-scripts lint",
"format": "prettier --ignore-path .prettierignore --write './src/**/*.{js,ts}'",
"test:only": "mate-scripts test",
Expand All @@ -18,7 +19,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.7.3",
"@mate-academy/scripts": "^1.8.1",
"axios": "^1.6.7",
"eslint": "^7.32.0",
"eslint-plugin-jest": "^22.4.1",
Expand All @@ -30,5 +31,8 @@
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"nodemon": "^3.1.0"
}
}
20 changes: 20 additions & 0 deletions public/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 id="form" action="/compress" method="post">
<input name="file" type="file" />
<select name="compressionType">
<option value="">Select compression type</option>
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<button type="submit">submit</button>
</form>
</body>
</html>
22 changes: 0 additions & 22 deletions readme.md

This file was deleted.

5 changes: 5 additions & 0 deletions src/constants/methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const methods = {
post: 'POST',
};

module.exports = { methods };
26 changes: 26 additions & 0 deletions src/constants/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const response = {
200: {
statusCode: 200,
},
400: {
statusCode: 400,
messages: {
encoding: 'Inappropriate encoding extension.',
method: `It should be POST request for "/compress"`,
},
},
404: {
statusCode: 404,
messages: {
notFound: 'Page not found',
},
},
503: {
statusCode: 503,
messages: {
service: 'Service unavailable',
},
},
};

module.exports = { response };
6 changes: 6 additions & 0 deletions src/constants/text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const text = {
root: '/',
compress: '/compress',
};

module.exports = { text };
31 changes: 29 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
'use strict';

const http = require('http');
const url = require('url');
const { httpGetMainPage } = require('./routes/httpGetMainPage');
const { httpVerifyRequest } = require('./routes/httpVerifyRequest');
const { httpCompress } = require('./routes/httpCompress');
const { text } = require('./constants/text');
const { methods } = require('./constants/methods');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer(async (req, res) => {
const method = req.method;
const normalizedText = new url.URL(req.url, `http://${req.headers.host}`)
.pathname;

if (normalizedText === text.root) {
await httpGetMainPage(res);

return;
}

const verifiedPath = httpVerifyRequest(res, normalizedText, method);

if (!verifiedPath) {
return;
}

if (method === methods.post && normalizedText === text.compress) {
await httpCompress(req, res);
}
});
}

module.exports = {
Expand Down
70 changes: 70 additions & 0 deletions src/routes/httpCompress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const fs = require('fs');
const zlib = require('zlib');
const { pipeline } = require('stream');
const formidable = require('formidable');
const { response } = require('../constants/response');

async function httpCompress(req, res) {
const form = new formidable.Formidable({});
let fields;
let files;

try {
[fields, files] = await form.parse(req);

const compressionMethod = fields.compressionType[0];
const filePath = files.file[0].filepath;
const fileName = files.file[0].originalFilename;
let extension;
let compressionStream;

const readFileStream = fs.createReadStream(filePath);

const onError = (e) => {
if (e) {
res.statusCode = response[503].statusCode;
res.end(response[503].messages.service);
}
};

if (compressionMethod === 'deflate') {
extension = 'dfl';
compressionStream = zlib.createDeflate();
} else if (compressionMethod === 'gzip') {
extension = 'gz';
compressionStream = zlib.createGzip();
} else if (compressionMethod === 'br') {
extension = 'br';
compressionStream = zlib.createBrotliCompress();
} else {
res.statusCode = response[400].statusCode;
res.end(response[400].messages.encoding);

return;
}

res.statusCode = response[200].statusCode;

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

const writeFileStream = fs.createWriteStream(`${filePath}.${extension}`);

compressionStream.pipe(writeFileStream);

pipeline(readFileStream, compressionStream, res, onError);

res.on('close', () => {
readFileStream.destroy();
});
} catch (err) {
res.writeHead(err.httpCode || response[400].statusCode, {
'Content-Type': 'text/plain',
});
res.end(String(err));
}
}

module.exports = { httpCompress };
24 changes: 24 additions & 0 deletions src/routes/httpGetMainPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require('fs');
const path = require('path');
const { response } = require('../constants/response');

async function httpGetMainPage(res) {
const indexFile = path.join(__dirname, '..', '..', 'public', 'index.html');

await fs.readFile(indexFile, (err, data) => {
res.setHeader('Content-Type', 'text/html');

if (err) {
res.statusCode = response[503].statusCode;

res.end(response[503].messages.service);

return;
}

res.statusCode = response[200].statusCode;
res.end(data);
});
}

module.exports = { httpGetMainPage };
29 changes: 29 additions & 0 deletions src/routes/httpVerifyRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { methods } = require('../constants/methods');
const { response } = require('../constants/response');
const { text } = require('../constants/text');

function httpVerifyRequest(res, normalizedPath, method) {
let result = true;

res.setHeader('Content-Type', 'text/plain');

if (normalizedPath !== '/compress') {
res.statusCode = response[404].statusCode;

res.end(response[404].messages.notFound);

result = false;
}

if (method !== methods.post && normalizedPath === text.compress) {
res.statusCode = response[400].statusCode;

res.end(response[400].messages.notFound);

result = false;
}

return result;
}

module.exports = { httpVerifyRequest };
Loading