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
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
183 changes: 181 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,187 @@
'use strict';

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

const HTML_PAGE = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Compression App</title>
</head>
<body>
<form action="/compress" method="POST" 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>
</body>
</html>`;

const compressionStreams = {
gzip: () => zlib.createGzip(),
deflate: () => zlib.createDeflate(),
br: () => zlib.createBrotliCompress(),
};

function sendTextResponse(res, statusCode, message) {
res.writeHead(statusCode, {
'Content-Type': 'text/plain; charset=utf-8',
});
res.end(message);
}

function getBoundary(contentType = '') {
const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/i);

return match ? match[1] || match[2] : null;
}

function collectRequestBody(req) {
return new Promise((resolve, reject) => {
const chunks = [];

req.on('data', (chunk) => chunks.push(chunk));
req.on('end', () => resolve(Buffer.concat(chunks)));
req.on('error', reject);
});
}

function parseMultipartFormData(bodyBuffer, boundary) {
const normalizedBody = bodyBuffer.toString('latin1');
const parts = normalizedBody
.split(`--${boundary}`)
.slice(1, -1)
.map((part) => part.replace(/^\r\n|\r\n$/g, ''));
const parsedData = {};

for (const part of parts) {
const [rawHeaders, rawContent] = part.split('\r\n\r\n');

if (!rawHeaders || rawContent === undefined) {
continue;
}

const nameMatch = rawHeaders.match(/name="([^"]+)"/i);

if (!nameMatch) {
continue;
}

const fieldName = nameMatch[1];
const content = rawContent.replace(/\r\n$/g, '');
const filenameMatch = rawHeaders.match(/filename="([^"]*)"/i);

if (filenameMatch) {
parsedData[fieldName] = {
filename: filenameMatch[1],
content: Buffer.from(content, 'latin1'),
};
} else {
parsedData[fieldName] = content;
}
}

return parsedData;
}

async function handleCompressRequest(req, res) {
const boundary = getBoundary(req.headers['content-type']);

if (!boundary) {
sendTextResponse(res, 400, 'Invalid form data');

return;
}

const body = await collectRequestBody(req);
const formData = parseMultipartFormData(body, boundary);
const file = formData.file;
const compressionType = formData.compressionType;

if (
!file ||
!file.filename ||
!Buffer.isBuffer(file.content) ||
!compressionType
) {
sendTextResponse(res, 400, 'Invalid form data');

return;
}

const createCompressionStream = compressionStreams[compressionType];

if (!createCompressionStream) {
sendTextResponse(res, 400, 'Unsupported compression type');

return;
}

const sourceStream = Readable.from(file.content);
const compressionStream = createCompressionStream();

res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename=${file.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 requires specific file extensions for different compression types: .gz for gzip and .dfl for deflate. The current implementation uses the compressionType value directly, resulting in incorrect extensions like .gzip and .deflate. You'll need to map the compression type to its correct extension.

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. This line currently uses the compressionType variable directly, which results in incorrect extensions like .gzip and .deflate. You'll need to map the compression type to the correct extension before setting the Content-Disposition header.

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 requirements specify that the compressed file extension for gzip should be .gz and for deflate it should be .dfl. Your current implementation uses the compressionType variable directly, which results in incorrect extensions like .gzip and .deflate. You should create a mapping from the compression type to the correct file extension.

});

sourceStream.on('error', () => {
if (!res.headersSent) {
sendTextResponse(res, 500, 'Failed to read file');
} else {
res.destroy();
}
});

compressionStream.on('error', () => {
if (!res.headersSent) {
sendTextResponse(res, 500, 'Compression failed');
} else {
res.destroy();
}
});

sourceStream.pipe(compressionStream).pipe(res);
}

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/') {
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8',
});
res.end(HTML_PAGE);

return;
}

if (req.url === '/compress' && req.method === 'GET') {
sendTextResponse(res, 400, 'GET method is not supported for /compress');

return;
}

if (req.url === '/compress' && req.method === 'POST') {
handleCompressRequest(req, res).catch(() => {
if (!res.headersSent) {
sendTextResponse(res, 400, 'Invalid form data');
} else {
res.destroy();
}
});

return;
}

sendTextResponse(res, 404, 'Not found');
});
}

module.exports = {
Expand Down
Loading