From eb3989e680460eb027cc42144dbd3a66390826ab Mon Sep 17 00:00:00 2001 From: Stanislav Date: Sat, 27 Jun 2026 10:50:26 +0300 Subject: [PATCH] add solution --- .github/workflows/test.yml-template | 23 ++++++++ package-lock.json | 31 ++++++++-- package.json | 5 +- public/index.html | 22 +++++++ src/createServer.js | 91 ++++++++++++++++++++++++++++- 5 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/test.yml-template create mode 100644 public/index.html diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 0000000..bb13dfc --- /dev/null +++ b/.github/workflows/test.yml-template @@ -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 diff --git a/package-lock.json b/package-lock.json index d0b3b95..7fe4887 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,10 +9,13 @@ "version": "1.0.0", "hasInstallScript": true, "license": "GPL-3.0", + "dependencies": { + "busboy": "^1.6.0" + }, "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", @@ -1487,10 +1490,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", @@ -2739,6 +2743,17 @@ "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", "dev": true }, + "node_modules/busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "dependencies": { + "streamsearch": "^1.1.0" + }, + "engines": { + "node": ">=10.16.0" + } + }, "node_modules/call-bind": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.7.tgz", @@ -8040,6 +8055,14 @@ "node": ">=8" } }, + "node_modules/streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/string-length": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/string-length/-/string-length-4.0.2.tgz", diff --git a/package.json b/package.json index 1d03d64..9f3cce9 100644 --- a/package.json +++ b/package.json @@ -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", @@ -30,5 +30,8 @@ }, "mateAcademy": { "projectType": "javascript" + }, + "dependencies": { + "busboy": "^1.6.0" } } diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..c3cdad0 --- /dev/null +++ b/public/index.html @@ -0,0 +1,22 @@ + + + + + + node_compression-app + + +

Compression App (with Node.js)

+
+ + + + + +
+ + diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..384803e 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -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') { + 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 = {