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

const http = require('http');
const path = require('path');
const fs = require('fs');
const os = require('os');
const { pipeline } = require('stream/promises');
const zlib = require('zlib');
const busboy = require('busboy');

const filePath = path.join(__dirname, 'index.html');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
return http.createServer((req, res) => {
if (req.method === 'GET' && req.url === '/compress') {
res.statusCode = 400;

return res.end('Wrong endpoint');
}

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

return res.end('Endpoint not exist');
}

if (req.method === 'POST' && req.url === '/compress') {
const bb = busboy({ headers: req.headers });

let compressionType = '';
let hasFile = false;
let uploadedFilePath = '';
let fileInfo = null;

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

bb.on('file', (name, file, info) => {
hasFile = true;
fileInfo = info;

uploadedFilePath = path.join(
os.tmpdir(),
`${Date.now()}-${info.filename}`,
);

const writeStream = fs.createWriteStream(uploadedFilePath);

file.pipe(writeStream);
});

bb.on('close', async () => {
if (!hasFile) {
res.statusCode = 400;

return res.end('Form is invalid');
}

const compressors = {
gzip: {
stream: zlib.createGzip(),
extension: '.gz',
},
deflate: {
stream: zlib.createDeflate(),
extension: '.dfl',
},
br: {
stream: zlib.createBrotliCompress(),
extension: '.br',
},
};

if (!compressionType) {
res.statusCode = 400;

return res.end('Form is invalid');
}

const compressor = compressors[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.

Currently, if compressionType is never sent in the form, compressionType remains an empty string and falls through to the "Unsupported compression type" branch; according to checklist item #15, a missing required field should cause a 400 "invalid form" response rather than being treated as an unsupported type, so consider explicitly validating that compressionType has a non-empty value before this lookup.


if (!compressor) {
res.statusCode = 400;

return res.end('Unsupported compression type');
}

res.statusCode = 200;
res.setHeader('Content-Type', 'application/octet-stream');

res.setHeader(
'Content-Disposition',
`attachment; filename=${fileInfo.filename}${compressor.extension}`,
);

try {
await pipeline(
fs.createReadStream(uploadedFilePath),
compressor.stream,
res,
);

fs.unlink(uploadedFilePath, () => {});
} catch (err) {
fs.unlink(uploadedFilePath, () => {});

if (!res.headersSent) {
res.statusCode = 500;
res.end('Internal Server Error');
}
}
});

req.pipe(bb);

return;
}

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

fs.createReadStream(filePath).pipe(res);
}
});
}

module.exports = {
Expand Down
31 changes: 31 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Node.js Server</title>
</head>
<body>

<h1>File Compression Service</h1>

<form action="/compress" method="POST" enctype="multipart/form-data">
<div>
<label for="file">Select a file:</label>
<input type="file" id="file" name="file" required>
</div>
<br>
<div>
<label for="compressionType">Choose compression type:</label>
<select id="compressionType" name="compressionType" required>
<option value="gzip">gzip (.gz)</option>
<option value="deflate">deflate (.dfl)</option>
<option value="br">brotli (.br)</option>
</select>
</div>
<br>
<button type="submit">Compress & Download</button>
</form>

</body>
</html>
Loading