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

const http = require('http');
const fs = require('fs');
const os = require('os');
const path = require('path');
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, { 'Content-Type': 'text/plain' });
res.end('Form parsing error');
});

let filename = '';
let type = '';
let hasFile = false;
let streamError = false;
let tmpPath = '';
let uploadDone = false;
let fileWritten = false;
let maybeResponded = false;

const tryHandleRequest = () => {
if (!uploadDone || !fileWritten || maybeResponded) {
return;
}

if (streamError) {
maybeResponded = true;
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Stream error');

return;
}

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

if (tmpPath) {
fs.unlink(tmpPath, () => {});
}

return;
}

let compressor = null;

switch (type) {
case 'gzip':
compressor = zlib.createGzip();
break;
case 'deflate':
compressor = zlib.createDeflate();
break;
case 'br':
compressor = zlib.createBrotliCompress();
break;
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.

default:
maybeResponded = true;
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Unsupported type');

fs.unlink(tmpPath, () => {});

return;
}

maybeResponded = true;

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.

});

const fileStream = fs.createReadStream(tmpPath);
const cleanup = () => {
fs.unlink(tmpPath, () => {});
};

fileStream.on('error', (err) => {
cleanup();
res.destroy(err);
});

compressor.on('error', (err) => {
cleanup();
res.destroy(err);
});

res.on('close', cleanup);
res.on('finish', cleanup);

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

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

tmpPath = path.join(
os.tmpdir(),
`compression-${Date.now()}-${Math.random().toString(16).slice(2)}`,
);

const writeStream = fs.createWriteStream(tmpPath);

file.pipe(writeStream);

writeStream.on('finish', () => {
fileWritten = true;
tryHandleRequest();
});

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

writeStream.on('error', () => {
streamError = true;
});
});

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

bb.on('finish', () => {
uploadDone = true;

if (!hasFile && !maybeResponded) {
maybeResponded = true;
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Invalid form');

return;
}

tryHandleRequest();
});

req.pipe(bb);

return;
}

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

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