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

Develop #129

Open
wants to merge 5 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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
27 changes: 15 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
"form-data": "^4.0.0",
"formidable": "^3.5.1",
"formidable": "^3.5.2",
"jest": "^29.7.0",
"prettier": "^3.3.2"
},
Expand Down
19 changes: 19 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!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 name="file" type="file" />
<select name="compressionType">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<input type="submit" />
</form>
</body>
</html>
71 changes: 69 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,75 @@
/* eslint-disable no-console */
'use strict';

const http = require('node:http');
const fs = require('node:fs');
const path = require('node:path');
const { formidable } = require('formidable');
const zlib = require('node:zlib');
const { pipeline } = require('node:stream');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.url === '/compress' && req.method === 'GET') {
res.writeHead(400, { 'Content-Type': 'text/plain' });

return res.end('Request shoul be Post method!');
}

if (req.url === '/' && req.method === 'GET') {
const normalizePath = path.resolve(__dirname, '../public/index.html');
const fileStream = fs.createReadStream(normalizePath);

res.writeHead(200, { 'Content-Type': 'text/html' });
fileStream.pipe(res).on('error', (err) => console.error(err));
res.on('close', () => fileStream.destroy());

return;
}

if (req.method === 'POST' && req.url === '/compress') {
const form = formidable({});
const compressors = {
gzip: zlib.createGzip,
br: zlib.createBrotliCompress,
deflate: zlib.createDeflate,
};

form.parse(req, (errors, { compressionType }, { file }) => {
if (!compressionType || !file || errors) {
res.statusCode = 400;

return res.end('Form error!');
}

if (!compressors.hasOwnProperty([compressionType])) {
res.statusCode = 400;

return res.end('No such compression type!');
}

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

res.writeHead(200, {
'Content-Disposition': `attachment; filename=${files.originalFilename}.${compressionTypes}`,
});

pipeline(fileStream, compression, res, (err) => {
if (err) {
return err;
}
});
});

return;
}

res.statusCode = 404;
res.end('Page not found!');
});
}

module.exports = {
Expand Down
Loading