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
28 changes: 24 additions & 4 deletions package-lock.json

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

5 changes: 4 additions & 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 All @@ -30,5 +30,8 @@
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"busboy": "^1.6.0"
Comment on lines +33 to +35
}
}
213 changes: 208 additions & 5 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,213 @@
/* eslint-disable no-console */
'use strict';

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

function parseMultipart(req, callback) {
const contentType = req.headers['content-type'] || '';
const boundaryMatch = contentType.match(/boundary=(.+)$/);

if (!boundaryMatch) {
return callback(new Error('No boundary'), null);
}

const boundary = boundaryMatch[1];
const chunks = [];

req.on('data', (chunk) => chunks.push(chunk));

req.on('end', () => {
const buffer = Buffer.concat(chunks);
const result = { fields: {}, file: null };

// Розбиваємо на частини по boundary
const delimiter = Buffer.from(`\r\n--${boundary}`);
const parts = splitBuffer(buffer, delimiter);

for (const part of parts) {
// Заголовки та тіло частини розділені \r\n\r\n
const headerEnd = indexOfSequence(part, Buffer.from('\r\n\r\n'));

if (headerEnd === -1) {
continue;
}

const headerStr = part.slice(0, headerEnd).toString();
// Тіло — все після \r\n\r\n
const body = part.slice(headerEnd + 4);

// Ім'я поля з Content-Disposition
const nameMatch = headerStr.match(/name="([^"]+)"/);

if (!nameMatch) {
continue;
}

const fieldName = nameMatch[1];
const filenameMatch = headerStr.match(/filename="([^"]+)"/);

if (filenameMatch) {
// Це файлове поле
result.file = {
filename: filenameMatch[1],
data: body,
};
} else {
// Звичайне текстове поле
result.fields[fieldName] = body.toString();
}
}

callback(null, result);
});

req.on('error', (err) => callback(err, null));
}

// Розбиває Buffer на масив Buffer-ів по розділювачу
function splitBuffer(buf, delimiter) {
const parts = [];
let start = 0;
let pos = 0;

while (pos <= buf.length - delimiter.length) {
if (buf.slice(pos, pos + delimiter.length).equals(delimiter)) {
parts.push(buf.slice(start, pos));
pos += delimiter.length;
start = pos;
} else {
pos++;
}
}

parts.push(buf.slice(start));

return parts;
}

module.exports = {
createServer,
// Шукає позицію послідовності байт у Buffer
function indexOfSequence(buf, seq) {
for (let i = 0; i <= buf.length - seq.length; i++) {
if (buf.slice(i, i + seq.length).equals(seq)) {
return i;
}
}

return -1;
}

const COMPRESSION_MAP = {
gzip: { create: () => zlib.createGzip(), ext: 'gz' },
deflate: { create: () => zlib.createDeflate(), ext: 'dfl' },
br: { create: () => zlib.createBrotliCompress(), ext: 'br' },
};

function createServer() {
const server = http.createServer((req, res) => {
const { method, url } = req;

// GET / → повертаємо HTML форму
if (method === 'GET' && url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' });

res.end(`
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="/compress" 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">Стиснути</button>
</form>
</body>
</html>
`);

return;
}

// GET /compress → 400
if (method === 'GET' && url === '/compress') {
res.writeHead(400);
res.end();

return;
}

// Будь-який запит на невідомий шлях → 404
if (url !== '/compress') {
res.writeHead(404);
res.end();

return;
}

// POST /compress → основна логіка
if (method === 'POST' && url === '/compress') {
parseMultipart(req, (err, parsed) => {
if (err || !parsed) {
res.writeHead(400);

return res.end();
}

const { file, fields } = parsed;
const compressionType = (fields.compressionType || '').trim();

// Крок 4: Валідація — файл і тип стиснення обов'язкові
if (!file) {
res.writeHead(400);

return res.end();
}

if (!compressionType) {
res.writeHead(400);

return res.end();
}

if (!COMPRESSION_MAP[compressionType]) {
res.writeHead(400);

return res.end();
}

// Крок 5: Формуємо ім'я вихідного файлу
const { create, ext } = COMPRESSION_MAP[compressionType];
const outputFilename = `${file.filename}.${ext}`;

res.writeHead(200, {
'Content-Disposition': `attachment; filename=${outputFilename}`,
});
Comment on lines +182 to +187

// Крок 6: Стискаємо через Stream
// file.data — це Buffer; перетворюємо на Readable stream
const { Readable } = require('stream');
const readable = new Readable();

Comment on lines +189 to +193
readable.push(file.data);
readable.push(null);

const compressor = create();

readable.pipe(compressor).pipe(res);
});

return;
}

// Fallback
res.writeHead(404);
res.end();
});

return server;
}

module.exports = { createServer };
Loading