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/src/createServer.js b/src/createServer.js index 1cf1dda..93c92fc 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -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]; + + 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 = { diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..5d57add --- /dev/null +++ b/src/index.html @@ -0,0 +1,31 @@ + + +
+ + +