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..983380b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,13 +12,13 @@ "devDependencies": { "@faker-js/faker": "^8.4.1", "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^1.9.12", "axios": "^1.7.2", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", "form-data": "^4.0.0", - "formidable": "^3.5.1", + "formidable": "^3.5.2", "jest": "^29.7.0", "prettier": "^3.3.2" } @@ -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": "1.9.12", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.9.12.tgz", + "integrity": "sha512-/OcmxMa34lYLFlGx7Ig926W1U1qjrnXbjFJ2TzUcDaLmED+A5se652NcWwGOidXRuMAOYLPU2jNYBEkKyXrFJA==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", @@ -4270,13 +4271,14 @@ } }, "node_modules/formidable": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.1.tgz", - "integrity": "sha512-WJWKelbRHN41m5dumb0/k8TeAx7Id/y3a+Z7QfhxP/htI9Js5zYaEDtG8uMgG0vM0lOlqnmjE99/kfpOYi/0Og==", + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.2.tgz", + "integrity": "sha512-Jqc1btCy3QzRbJaICGwKcBfGWuLADRerLzDqi2NwSt/UkXLsHJw2TVResiaoBufHVHy9aSgClOHCeJsSsFLTbg==", "dev": true, + "license": "MIT", "dependencies": { "dezalgo": "^1.0.4", - "hexoid": "^1.0.0", + "hexoid": "^2.0.0", "once": "^1.4.0" }, "funding": { @@ -4625,10 +4627,11 @@ } }, "node_modules/hexoid": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/hexoid/-/hexoid-1.0.0.tgz", - "integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/hexoid/-/hexoid-2.0.0.tgz", + "integrity": "sha512-qlspKUK7IlSQv2o+5I7yhUd7TxlOG2Vr5LTa3ve2XSNVKAL/n/u/7KLvKmFNimomDIKvZFXWHv0T12mv7rT8Aw==", "dev": true, + "license": "MIT", "engines": { "node": ">=8" } diff --git a/package.json b/package.json index 1d03d64..5b25d27 100644 --- a/package.json +++ b/package.json @@ -18,13 +18,13 @@ "devDependencies": { "@faker-js/faker": "^8.4.1", "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^1.9.12", "axios": "^1.7.2", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", "form-data": "^4.0.0", - "formidable": "^3.5.1", + "formidable": "^3.5.2", "jest": "^29.7.0", "prettier": "^3.3.2" }, diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..91ecaf4 --- /dev/null +++ b/public/index.html @@ -0,0 +1,19 @@ + + + + + + Document + + +
+ + + +
+ + diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..afe1935 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,75 @@ +/* eslint-disable no-console */ 'use strict'; +const http = require('node:http'); +const fs = require('node:fs'); +const path = require('node:path'); +const { formidable } = require('formidable'); +const zlib = require('node:zlib'); +const { pipeline } = require('node:stream'); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + return http.createServer((req, res) => { + if (req.url === '/compress' && req.method === 'GET') { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + + return res.end('Request shoul be Post method!'); + } + + if (req.url === '/' && req.method === 'GET') { + const normalizePath = path.resolve(__dirname, '../public/index.html'); + const fileStream = fs.createReadStream(normalizePath); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + fileStream.pipe(res).on('error', (err) => console.error(err)); + res.on('close', () => fileStream.destroy()); + + return; + } + + if (req.method === 'POST' && req.url === '/compress') { + const form = formidable({}); + const compressors = { + gzip: zlib.createGzip, + br: zlib.createBrotliCompress, + deflate: zlib.createDeflate, + }; + + form.parse(req, (errors, { compressionType }, { file }) => { + if (!compressionType || !file || errors) { + res.statusCode = 400; + + return res.end('Form error!'); + } + + if (!compressors.hasOwnProperty([compressionType])) { + res.statusCode = 400; + + return res.end('No such compression type!'); + } + + const files = file[0]; + const compressionTypes = compressionType[0]; + const fileStream = fs.createReadStream(files.filepath); + const compression = compressors[compressionTypes](); + + res.writeHead(200, { + 'Content-Disposition': `attachment; filename=${files.originalFilename}.${compressionTypes}`, + }); + + pipeline(fileStream, compression, res, (err) => { + if (err) { + return err; + } + }); + }); + + return; + } + + res.statusCode = 404; + res.end('Page not found!'); + }); } module.exports = {