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
119 changes: 113 additions & 6 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.1",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand All @@ -30,5 +30,8 @@
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"multiparty": "^4.2.3"
}
}
20 changes: 20 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<form action="/compress" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<select name="compressionType" id="compressionType">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<button type="submit">Send</button>
<button type="reset">Reset</button>
</form>
</body>
</html>
104 changes: 102 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,108 @@
'use strict';

const http = require('http');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const { pipeline } = require('stream');
const multiparty = require('multiparty');

const SUPPORTED_TYPES = {
gzip: { ext: '.gzip', method: zlib.createGzip },
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 file extension for gzip should be ".gz" (not ".gzip") according to the requirements. Update to:

gzip: { ext: '.gz', method: zlib.createGzip },

This is required for correct output filenames.

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 file extension for gzip is set to .gzip, but the requirements specify it must be .gz. Please change this to .gz to comply with the checklist (requirement 1.3, 3.3).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@mateacademy-ai-mentor if I change the file extension, my tests fail. The tests specifically require using .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 extension for gzip should be .gz (not .gzip) according to the requirements. Please update to:
gzip: { ext: '.gz', ... }

deflate: { ext: '.deflate', method: zlib.createDeflate },
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 file extension for deflate should be ".dfl" (not ".deflate") according to the requirements. Update to:

deflate: { ext: '.dfl', method: zlib.createDeflate },

This is required for correct output filenames.

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 file extension for deflate is set to .deflate, but the requirements specify it must be .dfl. Please change this to .dfl to comply with the checklist (requirement 1.3, 3.3).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@mateacademy-ai-mentor if I change the file extension, my tests fail. The tests specifically require using .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 extension for deflate should be .dfl (not .deflate) according to the requirements. Please update to:
deflate: { ext: '.dfl', ... }

br: { ext: '.br', method: zlib.createBrotliCompress },
};

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = new http.Server();

server.on('request', (req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);

if (url.pathname === '/compress') {
if (req.method !== 'POST') {
res.writeHead(400);
res.end('Use POST method');

return;
}

const form = new multiparty.Form();

form.parse(req, (err, fields, files) => {
if (err) {
res.writeHead(400);
res.end('Problem with form');

return;
}

const compressionType = fields.compressionType?.[0];
const file = files.file?.[0];

if (!compressionType || !file) {
res.writeHead(400);
res.end('Incorrectly filled out form');

return;
}

const compression = SUPPORTED_TYPES[compressionType];

if (!compression) {
res.writeHead(400);
res.end('Wrong compression type');

return;
}

const originalFilename = path.basename(file.originalFilename);
const compressedFilename = originalFilename + compression.ext;

res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Disposition': `attachment; filename=${compressedFilename}`,
});

const source = fs.createReadStream(file.path);
const compress = compression.method();

pipeline(source, compress, res, (erro) => {
fs.unlink(file.path, () => {});

if (erro) {
res.writeHead(400);
res.end('Compression error');
}
});
});

return;
}

const fileName = url.pathname.slice(1) || 'index.html';
const filePath = path.resolve('public', fileName);

if (!fs.existsSync(filePath)) {
res.statusCode = 404;
res.end('file not found');

return;
}

const fileStream = fs.createReadStream(filePath);

fileStream.pipe(res);

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

res.on('close', () => fileStream.destroy());
});

return server;
}

module.exports = {
Expand Down