Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
118 changes: 116 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,122 @@
'use strict';

const http = require('http');
const zlib = require('zlib');
const { Readable } = require('stream');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((request, response) => {
if (request.url === '/' && request.method === 'GET') {
const htmlString = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Example</title>
</head>
<body>
<form action="/compress" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<select name="compressionType">
<option value="gzip">GZIP Compression</option>
<option value="deflate">DEFLATE Compression</option>
<option value="br">BR Compression</option>
</select>
<button type="submit">Submit</button>
</form>
</body>
</html>
`;

response.setHeader('Content-Type', 'text/html');
response.statusCode = 200;
response.end(htmlString);
} else if (request.url === '/compress') {
if (request.method === 'GET') {
response.statusCode = 400;
response.end('Bad Request');

return;
}

if (request.method === 'POST') {
const boundary = request.headers['content-type'].split('boundary=')[1];
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 application could crash if the 'content-type' header is missing or doesn't contain a boundary. It's important to validate the header before attempting to access its properties. According to the requirements, the server should handle this as an invalid form submission and respond with a 400 status code.


const chunks = [];

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

request.on('end', () => {
const fullBuffer = Buffer.concat(chunks);
const fileNameMatch = fullBuffer.toString().match(/filename="(.+?)"/);
const compressionMatch = fullBuffer
.toString()
.match(/name="compressionType"\r\n\r\n(.+?)\r\n/);

if (!fileNameMatch || !compressionMatch) {
response.statusCode = 400;
response.end('Bad Request');

return;
}

const fileName = fileNameMatch[1];
const compressionType = compressionMatch[1];

if (!['gzip', 'deflate', 'br'].includes(compressionType)) {
response.statusCode = 400;
response.end('Invalid Compression Type');

return;
}

const startFileName = fullBuffer.indexOf('filename="' + fileName);
const fileContentStart =
fullBuffer.indexOf('\r\n\r\n', startFileName) + 4;
const fileContendEnd = fullBuffer.indexOf(
'\r\n--' + boundary,
fileContentStart,
);
const fileBuffer = fullBuffer.subarray(
fileContentStart,
fileContendEnd,
);

const fileStream = Readable.from(fileBuffer);

let compressedStream;

if (compressionType === 'gzip') {
compressedStream = zlib.createGzip();
}

if (compressionType === 'deflate') {
compressedStream = zlib.createDeflate();
}

if (compressionType === 'br') {
compressedStream = zlib.createBrotliCompress();
}

const newFileName = `${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 task description requires the file extensions to be .gz, .dfl, and .br for gzip, deflate, and br compression types respectively. This line appends the full compression type name as the extension, resulting in incorrect extensions like .gzip and .deflate. You should map the compression type to the correct file extension.


response.statusCode = 200;

response.setHeader(
'Content-Disposition',
`attachment; filename=${newFileName}`,
);

fileStream.pipe(compressedStream).pipe(response);
});
}
} else {
response.statusCode = 404;
response.end('Not Found');
}
});
}

module.exports = {
Expand Down
Loading