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..e267868 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.2", "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.2", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.2.tgz", + "integrity": "sha512-gUXFdqqOfYzF9R3RSx2pCa5GLdOkxB9bFbF+dpUpzucdgGAANqOGdqpmNnMj+e3xA9YHraUWq3xo9cwe5vD9pQ==", "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..bf27fc7 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.2", "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.css b/public/index.css new file mode 100644 index 0000000..26ce2b3 --- /dev/null +++ b/public/index.css @@ -0,0 +1,50 @@ +body { + font-family: Arial, sans-serif; + background: #f5f5f5; + margin: 0; + padding: 0; + + display: flex; + justify-content: center; + align-items: center; + height: 100vh; +} + +.form { + background: #fff; + padding: 25px 30px; + border-radius: 12px; + box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); + width: 320px; + display: flex; + flex-direction: column; + gap: 15px; +} + +label { + font-weight: bold; + margin-bottom: 5px; +} + +input[type="file"], +select { + padding: 8px; + border: 1px solid #ccc; + border-radius: 6px; + font-size: 14px; +} + +button { + padding: 10px; + background: #007bff; + border: none; + color: white; + font-size: 16px; + border-radius: 6px; + cursor: pointer; + transition: 0.2s; +} + +button:hover { + background: #0056c7; +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..2a5ed54 --- /dev/null +++ b/public/index.html @@ -0,0 +1,24 @@ + + + + + + Document + + + +
+ + + + + + + +
+ + diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..c8f9514 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,165 @@ 'use strict'; +const http = require('http'); +const fs = require('fs'); +const path = require('path'); +const zlib = require('zlib'); +const busboy = require('busboy'); + +// EXTENSIONS EXPECTED BY TESTS +const EXT_MAP = { + gzip: 'gzip', + deflate: 'deflate', + br: 'br', +}; + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + const server = new http.Server(); + + server.on('request', (req, res) => { + const url = new URL(req.url, `http://${req.headers.host}`); + + if (req.method === 'POST' && url.pathname === '/compress') { + const bb = busboy({ headers: req.headers }); + + let compressionType = null; + let fileInfo = null; + + bb.on('field', (name, val) => { + if (name === 'compressionType') { + compressionType = val; + + if (fileInfo && !fileInfo.started) { + fileInfo.start(); + } + } + }); + + bb.on('file', (name, file, info) => { + file.pause(); + + fileInfo = { + file, + info, + started: false, + start() { + if (this.started) { + return; + } + this.started = true; + + if (!compressionType) { + return; + } // дочекаємося пізніше + + if (!EXT_MAP[compressionType]) { + res.statusCode = 400; + + return res.end('Invalid compressionType'); + } + + const outName = `${info.filename}.${EXT_MAP[compressionType]}`; + + const compressor = + compressionType === 'gzip' + ? zlib.createGzip() + : compressionType === 'deflate' + ? zlib.createDeflate() + : zlib.createBrotliCompress(); + + res.statusCode = 200; + + res.setHeader( + 'Content-Disposition', + `attachment; filename=${outName}`, + ); + + file.on('error', () => { + if (!res.headersSent) { + res.statusCode = 500; + } + res.end(); + }); + + compressor.on('error', () => { + if (!res.headersSent) { + res.statusCode = 500; + } + res.end(); + }); + + file.resume(); + file.pipe(compressor).pipe(res); + }, + }; + + if (compressionType) { + fileInfo.start(); + } + }); + + bb.on('finish', () => { + if (!fileInfo) { + res.statusCode = 400; + + return res.end('No file'); + } + + if (!compressionType) { + // прибираємо пайпи, якщо були + fileInfo.file.unpipe(); + + // дочитуємо файл до кінця, інакше busboy зависне + fileInfo.file.resume(); + res.statusCode = 400; + + return res.end('Missing compressionType'); + } + + if (!fileInfo.started) { + fileInfo.start(); + } + }); + + req.pipe(bb); + + return; + } + + if (req.method === 'GET' && url.pathname === '/compress') { + res.statusCode = 400; + res.end(); + + return; + } + + const fileName = url.pathname.slice(1) || 'index.html'; + const filePath = path.resolve('public', fileName); + + if (!fs.existsSync(filePath)) { + res.statusCode = 404; + + return res.end('file dont found'); + } + + const ext = path.extname(filePath); + const mimeTypes = { + '.html': 'text/html', + '.css': 'text/css', + '.js': 'application/javascript', + '.json': 'application/json', + '.png': 'image/png', + '.jpg': 'image/jpeg', + '.gif': 'image/gif', + '.svg': 'image/svg+xml', + '.ico': 'image/x-icon', + }; + + res.setHeader('Content-Type', mimeTypes[ext] || 'text/plain'); + fs.createReadStream(filePath).pipe(res); + }); + + return server; } module.exports = {