Skip to content
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
9 changes: 5 additions & 4 deletions package-lock.json

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

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
94 changes: 92 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,98 @@
'use strict';

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

const COMPRESSION_TYPES = ['gzip', 'deflate', 'br'];

const ZLIB_COMPRESSOR = {
gzip: { formatter: () => zlib.createGzip(), format: 'gz' },
deflate: { formatter: () => zlib.createDeflate(), format: 'dfl' },
br: { formatter: () => zlib.createBrotliCompress(), format: 'br' },
};

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = new http.Server();

server.on('request', (req, res) => {
const url = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
const pathname = url.pathname.slice(1);

if (pathname === 'compress') {
if (req.method !== 'POST') {
res.statusCode = 400;
res.end('Invalid request');

return;
}

const form = formidable({ multiples: false });

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

return;
}

const compressionType =
fields.compressionType?.[0] ?? fields.compressionType;
const file = files.file?.[0] ?? files.file;

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

return;
}

const ext = ZLIB_COMPRESSOR[compressionType].format;
const fileStream = fs.createReadStream(file.filepath);

res.statusCode = 200;

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

fileStream.pipe(ZLIB_COMPRESSOR[compressionType].formatter()).pipe(res);
});
} else if (pathname === '') {
res.end(`
<body>
<h1>Choose File and Compress</h1>

<form action="/compress" method="post" enctype="multipart/form-data">
<input type="file" name="file" />

<select name="compressionType">
<option value="gzip">GZIP</option>
<option value="deflate">DEFLATE</option>
<option value="br">BR</option>
</select>

<button type="submit">Compress</button>
</form>
</body>
`);
} else {
res.statusCode = 404;
res.end('Not Found');
}
});

server.on('error', () => { });

return server;
}

module.exports = {
Expand Down
Loading