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"
}
}
22 changes: 22 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>node_compression-app</title>
</head>
<body>
<h1>Compression App (with Node.js)</h1>
<form action="/compress" method="post" enctype="multipart/form-data">
<label for="select">choose compression type</label>
<select name="compressionType" id="select">
<option value="gzip">gzip</option>
<option value="deflate">deflate</option>
<option value="br">br</option>
</select>
<label for="file">select the file</label>
<input type="file" name="file" id="file" />
<button type="submit">send to server</button>
</form>
</body>
</html>
91 changes: 89 additions & 2 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,95 @@
'use strict';

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

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const compressionTypeMap = {
gzip: [zlib.createGzip, 'gz'],
deflate: [zlib.createDeflate, 'dfl'],
br: [zlib.createBrotliCompress, 'br'],
};

return http.createServer(async (req, res) => {
if (req.method !== 'POST' && req.url === '/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.

This condition treats any method other than POST to /compress as 400, while the requirement specifically mentions handling GET requests with 400. Consider changing this to explicitly check req.method === 'GET' && req.url === '/compress' so the behavior matches checklist item #12 more precisely and avoids unintended 400 responses for other HTTP methods.

res.statusCode = 400;
res.end();

return;
}

if (req.method === 'GET' && req.url === '/') {
try {
const page = await fs.readFile('./public/index.html');

res.end(page);

return;
} catch (err) {
res.end('Something went wrong');

return;
}
}

if (req.method === 'POST' && req.url === '/compress') {
const bb = busboy({ headers: req.headers });
let compressionType = '';
let filename;
let hasFile = false;
const pass = new PassThrough();

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

bb.on('file', (name, sourceFile, info) => {
hasFile = true;
filename = info.filename;
sourceFile.pipe(pass);
});

bb.on('finish', () => {
if (!hasFile) {
res.statusCode = 400;
res.end('No file provided');

return;
}

if (!compressionTypeMap[compressionType]) {
res.statusCode = 400;
res.end('Invalid compression type');
pass.destroy();

return;
}

const [compressMethod, extension] = compressionTypeMap[compressionType];
const compress = compressMethod();

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

pass.pipe(compress);
compress.pipe(res);
});

req.pipe(bb);

return;
}

res.statusCode = 404;
res.end('non-existing route');
});
}

module.exports = {
Expand Down
Loading