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..a9f9d72 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,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", @@ -1487,9 +1487,9 @@ } }, "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, "dependencies": { "@octokit/rest": "^17.11.2", diff --git a/package.json b/package.json index 1d03d64..8e6392d 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", diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..05e66be 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,10 +1,110 @@ +/* eslint-disable no-console */ 'use strict'; +const http = require('http'); +const zlib = require('zlib'); +const { pipeline, Readable } = require('stream'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((req, res) => { + const { method, url } = req; + + if (url === '/' && method === 'GET') { + res.writeHead(200, { 'Content-type': 'text/html' }); + + return res.end(` +
+ `); + } + + if (url !== '/compress') { + res.statusCode = 404; + + return res.end('The endpoint does not exist'); + } + + if (method !== 'POST') { + res.statusCode = 400; + + return res.end('Only POST requests are allowed'); + } + + let bodyBuffer = Buffer.alloc(0); + let isStreamingStarted = false; + + req.on('data', (chunk) => { + if (isStreamingStarted) { + return; + } + + bodyBuffer = Buffer.concat([bodyBuffer, chunk]); + + const bodyStr = bodyBuffer.toString('binary'); + const filePartHeaderMatch = bodyStr.match( + /filename="(.+?)"\r\nContent-Type: .+?\r\n\r\n/, + ); + const typeMatch = bodyStr.match( + /name="compressionType"\r\n\r\n(.+?)\r\n/, + ); + + if (filePartHeaderMatch && typeMatch) { + isStreamingStarted = true; + + const filename = filePartHeaderMatch[1]; + const compressionType = typeMatch[1].trim(); + + const validTypes = { + gzip: { create: zlib.createGzip, ext: 'gz' }, + deflate: { create: zlib.createDeflate, ext: 'dfl' }, + br: { create: zlib.createBrotliCompress, ext: 'br' }, + }; + + const config = validTypes[compressionType]; + + if (!config) { + res.statusCode = 400; + + return res.end('Unsupported compression type'); + } + + // Prepare response + res.writeHead(200, { + 'Content-Type': 'application/octet-stream', + 'Content-Disposition': `attachment; filename="${filename}.${compressionType}"`, + }); + + const headerEndIndex = + bodyStr.indexOf(filePartHeaderMatch[0]) + + filePartHeaderMatch[0].length; + const initialFileData = bodyBuffer.slice(headerEndIndex); + + const boundary = bodyStr.split('\r\n')[0]; + const footerIndex = initialFileData + .toString('binary') + .indexOf(boundary); + + const actualFileSource = Readable.from( + footerIndex !== -1 + ? initialFileData.slice(0, footerIndex - 2) + : initialFileData, + ); + + pipeline(actualFileSource, config.create(), res, (err) => { + if (err) { + console.error(err); + } + }); + } + }); + }); } -module.exports = { - createServer, -}; +module.exports = { createServer };