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
23 changes: 23 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!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="http://localhost:5700/compress"
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 action attribute is hardcoded to a specific URL. It's better practice to use a relative path like /compress to make the form independent of the server's host and port. Additionally, please note that the server implementation is configured to serve the file located at src/index.html, not this one.

method="POST"
enctype="multipart/form-data"
>
<select name="compressionType" required >
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<input type="file" name="file" required>

<button type="submit">Send</button>
</form>
</body>
</html>
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"
}
}
137 changes: 132 additions & 5 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,137 @@
'use strict';

const fs = require('fs');
const path = require('path');
const http = require('http');
const zlib = require('zlib');
const busboy = require('busboy');
const { PassThrough } = require('stream');

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

server.on('request', (req, res) => {
if (req.url === '/' && req.method === 'GET') {
const filePath = path.resolve(__dirname, 'index.html');
const stream = fs.createReadStream(filePath);

stream.on('error', () => {
res.statusCode = 500;

return res.end('Cannot read index.html');
});

res.writeHead(200, { 'Content-Type': 'text/html' });

return stream.pipe(res);
}

if (req.url !== '/compress') {
res.statusCode = 404;

return res.end('Not Found');
}

if (req.method !== 'POST') {
res.statusCode = 400;

return res.end('Only POST allowed!');
}

let bb;

try {
bb = busboy({ headers: req.headers });
} catch (err) {
res.statusCode = 400;

return res.end('Invalid Content-Type');
}

let filename = '';
let compressionType = '';
let filesStream = null;
let gotFile = false;

const startCompression = () => {
if (!gotFile || !compressionType || !filesStream || res.headersSent) {
return;
}

let compressStream;
let contentType = 'application/octet-stream';

if (compressionType === 'gzip') {
compressStream = zlib.createGzip();
contentType = 'application/gzip';
} else if (compressionType === 'deflate') {
compressStream = zlib.createDeflate();
contentType = 'application/zlib';
} else if (compressionType === 'br') {
compressStream = zlib.createBrotliCompress();
contentType = 'application/brotli';
} else {
res.statusCode = 400;

return res.end('Unsupported compression type');
}
Comment on lines +64 to +77
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 block correctly checks for 'gzip', 'deflate', and 'br'. However, the src/index.html file is sending different values ('gz', 'dfl') from its form for gzip and deflate options. This mismatch will cause requests for gzip and deflate compression to fail with an 'Unsupported compression type' error. Please ensure the value attributes in the <option> tags in src/index.html match the values expected by the server.


res.writeHead(200, {
'Content-Type': contentType,
'Content-Disposition': `attachment; filename=${filename}.${compressionType}`,
});

filesStream.pipe(compressStream).pipe(res);
};

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

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

const pass = new PassThrough();

stream.pipe(pass);
filesStream = pass;

startCompression();

stream.on('error', () => {
if (!res.writableEnded) {
res.statusCode = 500;
res.end('File stream error');
}
});
} else {
stream.resume();
}
});

bb.on('close', () => {
if (!res.headersSent) {
res.statusCode = 400;
res.end('Invalid Form');
}
});

bb.on('error', () => {
if (!res.writableEnded) {
res.statusCode = 400;
res.end('Busboy error');
}
});

req.pipe(bb);
});

return server;
}

module.exports = {
createServer,
};
module.exports = { createServer };
1 change: 1 addition & 0 deletions src/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sdfsdfsdsdf
24 changes: 24 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!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"
>
<select name="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.

Consider adding the required attribute to this select element. This would prevent the form from being submitted if the user hasn't chosen a compression type, which improves the user experience by providing instant client-side validation.

<option value="">Choose a compression</option>
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<input type="file" name="file" required>

<button type="submit">Send</button>
</form>
</body>
</html>
Loading