Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #129

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
27 changes: 15 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
"form-data": "^4.0.0",
"formidable": "^3.5.1",
"formidable": "^3.5.2",
"jest": "^29.7.0",
"prettier": "^3.3.2"
},
Expand Down
140 changes: 138 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,144 @@
/* eslint-disable no-console */
/* eslint-disable curly */
'use strict';

const http = require('node:http');
const fs = require('node:fs');
const path = require('node:path');
const { formidable } = require('formidable');
const {
createGzip,
createBrotliCompress,
createDeflate,
} = require('node:zlib');

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

server.on('request', async (req, res) => {
if (req.url === '/favicon.ico') {
res.statusCode = 200;
res.end('');

return;
}

if (req.url === '/compress' && req.method === 'GET') {
res.statusCode = 400;
res.end('');

return;
}

if (req.url === '/' && req.method === 'GET') {
const normalizePath = path.resolve(__dirname, 'index.html');

res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');

const fileStream = fs.createReadStream(normalizePath);

fileStream.pipe(res).on('error', () => {});

fileStream.on('error', (err) => {
res.statusCode = 500;
res.end(`Server error ${err}`);
});

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

return;
}

if (req.method === 'POST' && req.url === '/compress') {
const form = formidable({});
let fields, files;

try {
[fields, files] = await form.parse(req);
} catch (err) {
res.writeHead(err.httpCode || 400, { 'Content-Type': 'text/plain' });
res.end(String(err));

return;
}

const compressionType = Array.isArray(fields.compressionType)
? fields.compressionType[0]
: fields.compressionType;

const filePath = Array.isArray(files.file)
? files.file[0].filepath
: files.file;
const fileName = Array.isArray(files.file)
? files.file[0].originalFilename
: files.file;

Choose a reason for hiding this comment

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

There is a potential issue with the way filePath and fileName are assigned. If files.file is not an array, the code assigns the entire file object to filePath and fileName, which is likely incorrect. You should access the filepath and originalFilename properties directly in the else case.


if (!filePath || !fileName || !compressionType) {
res.statusCode = 400;
res.end();

return;
}

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

let compressionData, extension;

switch (compressionType) {
case 'gzip':
compressionData = createGzip();
res.setHeader('Content-Encoding', 'gz');

Choose a reason for hiding this comment

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

The content encoding header for gzip should be 'gzip' instead of 'gz'. This is a critical issue as it may cause clients to misinterpret the encoding.

Copy link
Author

Choose a reason for hiding this comment

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

image
image

extension = 'gzip';
break;
case 'deflate':
compressionData = createDeflate();
res.setHeader('Content-Encoding', '.dfl');

Choose a reason for hiding this comment

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

The content encoding headers are incorrect. For gzip, the correct header should be 'gzip' instead of 'gz'. Similarly, for deflate, the correct header should be 'deflate' instead of '.dfl'. Please update these headers to reflect the correct content encoding values.

Copy link
Author

Choose a reason for hiding this comment

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

For gzip, I should use 'gz' because it doesn't pass the test.!!!! Similarly, for deflate, I should use '.dfl'

Choose a reason for hiding this comment

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

The content encoding header for deflate should be 'deflate' instead of '.dfl'. This is a critical issue as it may cause clients to misinterpret the encoding.

extension = 'deflate';
break;
case 'br':
compressionData = createBrotliCompress();
res.setHeader('Content-Encoding', '.br');

Choose a reason for hiding this comment

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

The content encoding header for Brotli should be 'br' instead of '.br'. This is a critical issue as it may cause clients to misinterpret the encoding.

extension = 'br';
break;
default:
res.statusCode = 400;
res.end();

return;
}

const fileStream = fs.createReadStream(filePath);

res.statusCode = 200;

res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Disposition': `attachment; filename=${fileName}.${extension}`,
});
console.log(fields, files, req.method, compressionType);

fileStream
.pipe(compressionData)
.pipe(res)
.on('error', (err) => {
res.statusCode = 500;
res.end(`Stream error: ${err.message}`);
});

return;
}

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

return server;
}

module.exports = {
Expand Down
19 changes: 19 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!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 name="file" type="file" />
<select name="compressionType">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<input type="submit" />
</form>
</body>
</html>
Loading