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
31 changes: 27 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"
}
}
113 changes: 108 additions & 5 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,113 @@
'use strict';

const http = require('http');
const zlib = require('zlib');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
res.statusCode = 200;
res.end('OK');

return;
}

if (req.url !== '/compress') {
res.statusCode = 404;
res.end();

return;
}

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

return;
}

const contentType = req.headers['content-type'] || '';

if (!contentType.includes('multipart/form-data')) {
res.statusCode = 400;
res.end();

return;
}

const boundary = '--' + contentType.split('boundary=')[1];

const chunks = [];

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

req.on('end', () => {
const body = Buffer.concat(chunks).toString('binary');
Comment on lines +42 to +45
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation buffers the entire request body into memory before processing it, which does not meet the core 'use Streams' requirement. A streaming approach processes data in chunks as it arrives. You should refactor this to parse the incoming request stream, pipe the file data through a zlib transform stream (e.g., zlib.createGzip()), and then pipe the result to the response stream. This avoids loading the entire file into memory.


const parts = body.split(boundary).filter((p) => p.trim());

let fileBuffer = null;
let filename = null;
let compressionType = null;

for (const part of parts) {
if (part.includes('name="file"')) {
const headerEnd = part.indexOf('\r\n\r\n');

const headers = part.slice(0, headerEnd);
const content = part.slice(headerEnd + 4, part.lastIndexOf('\r\n'));

const match = headers.match(/filename="(.+?)"/);

if (match) {
filename = match[1];
}

fileBuffer = Buffer.from(content, 'binary');
}

if (part.includes('name="compressionType"')) {
const headerEnd = part.indexOf('\r\n\r\n');

const value = part
.slice(headerEnd + 4)
.replace(/\r\n$/, '')
.trim();

compressionType = value;
}
}

const compressors = {
gzip: zlib.gzipSync,
deflate: zlib.deflateSync,
br: zlib.brotliCompressSync,
};

if (!fileBuffer || !compressionType || !compressors[compressionType]) {
res.statusCode = 400;
res.end();

return;
}

const compressed = compressors[compressionType](fileBuffer);
Comment on lines +81 to +94
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of synchronous zlib methods blocks the server's event loop, which is inefficient. A streaming implementation would use asynchronous, stream-based methods like zlib.createGzip(), zlib.createDeflate(), and zlib.createBrotliCompress() which can be connected using .pipe().


res.statusCode = 200;

res.setHeader(
'Content-Disposition',
`attachment; filename=${filename}.${compressionType}`,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filename extension is being set directly from the compressionType variable, which results in extensions like .gzip. According to the task requirements, you need to map the compression type to a specific extension: gzip -> .gz, deflate -> .dfl, and br -> .br.

);

res.end(compressed);
});

req.on('error', () => {
res.statusCode = 500;
res.end();
});
});
}

module.exports = {
createServer,
};
module.exports = { createServer };