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..3650660 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,10 +1487,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", 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..284433f 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,159 @@ 'use strict'; +const http = require('http'); +const zlib = require('zlib'); + +const compressionTypes = { + gzip: { + extension: 'gz', + createStream: () => zlib.createGzip(), + }, + deflate: { + extension: 'dfl', + createStream: () => zlib.createDeflate(), + }, + br: { + extension: 'br', + createStream: () => zlib.createBrotliCompress(), + }, +}; + +function parseMultipart(body, boundary) { + const boundaryBuffer = Buffer.from(`--${boundary}`); + const parts = []; + + let start = body.indexOf(boundaryBuffer); + + while (start !== -1) { + const next = body.indexOf(boundaryBuffer, start + boundaryBuffer.length); + + if (next === -1) { + break; + } + + const part = body.subarray(start + boundaryBuffer.length, next); + + if (part.length > 4) { + parts.push(part); + } + + start = next; + } + + const result = {}; + + parts.forEach((part) => { + const headerEnd = part.indexOf('\r\n\r\n'); + + if (headerEnd === -1) { + return; + } + + const headers = part.subarray(0, headerEnd).toString(); + let content = part.subarray(headerEnd + 4); + + if (content.subarray(-2).toString() === '\r\n') { + content = content.subarray(0, -2); + } + + const nameMatch = headers.match(/name="([^"]+)"/); + const filenameMatch = headers.match(/filename="([^"]+)"/); + + if (!nameMatch) { + return; + } + + const { 1: name } = nameMatch; + + if (filenameMatch) { + result[name] = { + filename: filenameMatch[1], + content, + }; + } else { + result[name] = content.toString(); + } + }); + + return result; +} + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((req, res) => { + if (req.url === '/') { + res.statusCode = 200; + res.end(); + + 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']; + const boundaryMatch = contentType?.match(/boundary=(?:"([^"]+)"|([^;]+))/); + + if (!boundaryMatch) { + res.statusCode = 400; + res.end(); + + return; + } + + const boundary = boundaryMatch[1] || boundaryMatch[2]; + const chunks = []; + + req.on('data', (chunk) => { + chunks.push(chunk); + }); + + req.on('end', () => { + const body = Buffer.concat(chunks); + const formData = parseMultipart(body, boundary); + + const file = formData.file; + const compressionType = formData.compressionType; + + if (!file || !compressionType) { + res.statusCode = 400; + res.end(); + + return; + } + + const compression = compressionTypes[compressionType]; + + if (!compression) { + res.statusCode = 400; + res.end(); + + return; + } + + res.statusCode = 200; + + res.setHeader( + 'Content-Disposition', + `attachment; filename=${file.filename}.${compression.extension}`, + ); + + const compressor = compression.createStream(); + + compressor.pipe(res); + compressor.end(file.content); + }); + }); } module.exports = {