Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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"
}
}
125 changes: 120 additions & 5 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,125 @@
'use strict';

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

const data = `
<form method="POST" action="/compress" enctype="multipart/form-data">
<input type="file" name="file" required>
<select name="compressionType" required>
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<button type="submit">Compress</button>
</form>
`;

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'GET') {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
} else if (req.url === '/compress') {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Incorrect request');
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Page not found');
}

return;
}

if (req.method === 'POST') {
if (req.url === '/compress') {
const bb = busboy({ headers: req.headers });

bb.on('error', () => {
res.writeHead(400);
res.end('Form parsing error');
});

let filename = '';
let type = '';
let hasFile = false;
const chunks = [];
let fileStreamError = false;

bb.on('file', (fieldname, file, info) => {
hasFile = true;
filename = info.filename;

file.on('data', (chunk) => {
chunks.push(chunk);
});

file.on('error', () => {
fileStreamError = true;
});
});

bb.on('field', (fieldname, val) => {
if (fieldname === 'compressionType') {
type = val;
}
});

bb.on('finish', () => {
if (fileStreamError) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Stream error');

return;
}

if (!hasFile || !filename || !type) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Invalid form');

return;
}

if (!['gzip', 'deflate', 'br'].includes(type)) {
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Unsupported type');

return;
}

const rawFile = Buffer.concat(chunks);
let compressedFile;

switch (type) {
case 'gzip':
compressedFile = zlib.gzipSync(rawFile);
break;
case 'deflate':
compressedFile = zlib.deflateSync(rawFile);
break;
case 'br':
compressedFile = zlib.brotliCompressSync(rawFile);
break;
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 code buffers the entire file in memory before compression. Per requirements, you should pipe the file stream from busboy through a zlib transform stream (e.g., file.pipe(zlib.createGzip())) directly to the response, not collect chunks in an array first.

Comment on lines +83 to +93
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using synchronous compression (gzipSync, deflateSync, brotliCompressSync). The requirement specifies using streams - use zlib.createGzip(), zlib.createDeflate(), zlib.createBrotliCompress() instead.

}
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 file into memory before compressing it, which goes against the requirement to use Streams. For a proper stream-based solution, you should pipe the file stream from busboy directly into a zlib transform stream (e.g., zlib.createGzip()) and then pipe the result to the response stream. This avoids loading large files completely into memory.


res.writeHead(200, {
'Content-Type': 'application/octet-stream',
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 extension uses ${type} which gives '.gzip', '.deflate', '.br' but requirements specify '.gz' for gzip and '.dfl' for deflate. Need to map: gzip→gz, deflate→dfl, br→br

'Content-Disposition': `attachment; filename=${filename}.${type}`,
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 does not match the requirements. For a compression type of gzip, the extension should be .gz, and for deflate, it should be .dfl. The current implementation uses the compression type value directly, resulting in incorrect extensions like .gzip and .deflate.

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 task requires specific file extensions: .gz for gzip and .dfl for deflate. Currently, you're using the full compressionType string (.gzip, .deflate), which doesn't match the requirements. You'll need to map the compression type to the correct extension before constructing the filename.

});
res.end(compressedFile);
});

req.pipe(bb);

return;
}

res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Page not found');
}
});
}

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