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..73515ee 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,10 +1,113 @@ 'use strict'; +const http = require('http'); +const zlib = require('zlib'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((req, res) => { + if (req.method === 'GET' && req.url === '/') { + res.statusCode = 200; + res.end('OK'); + + return; + } + + if (req.url !== '/compress') { + res.statusCode = 404; + res.end(); + + return; + } + + if (req.method !== 'POST') { + res.statusCode = 400; + res.end(); + + return; + } + + const contentType = req.headers['content-type'] || ''; + + if (!contentType.includes('multipart/form-data')) { + res.statusCode = 400; + res.end(); + + return; + } + + const boundary = '--' + contentType.split('boundary=')[1]; + + const chunks = []; + + req.on('data', (chunk) => chunks.push(chunk)); + + req.on('end', () => { + const body = Buffer.concat(chunks).toString('binary'); + + const parts = body.split(boundary).filter((p) => p.trim()); + + let fileBuffer = null; + let filename = null; + let compressionType = null; + + for (const part of parts) { + if (part.includes('name="file"')) { + const headerEnd = part.indexOf('\r\n\r\n'); + + const headers = part.slice(0, headerEnd); + const content = part.slice(headerEnd + 4, part.lastIndexOf('\r\n')); + + const match = headers.match(/filename="(.+?)"/); + + if (match) { + filename = match[1]; + } + + fileBuffer = Buffer.from(content, 'binary'); + } + + if (part.includes('name="compressionType"')) { + const headerEnd = part.indexOf('\r\n\r\n'); + + const value = part + .slice(headerEnd + 4) + .replace(/\r\n$/, '') + .trim(); + + compressionType = value; + } + } + + const compressors = { + gzip: zlib.gzipSync, + deflate: zlib.deflateSync, + br: zlib.brotliCompressSync, + }; + + if (!fileBuffer || !compressionType || !compressors[compressionType]) { + res.statusCode = 400; + res.end(); + + return; + } + + const compressed = compressors[compressionType](fileBuffer); + + res.statusCode = 200; + + res.setHeader( + 'Content-Disposition', + `attachment; filename=${filename}.${compressionType}`, + ); + + res.end(compressed); + }); + + req.on('error', () => { + res.statusCode = 500; + res.end(); + }); + }); } -module.exports = { - createServer, -}; +module.exports = { createServer };