diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000..c70ec1c --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,8 @@ +{ + "permissions": { + "allow": [ + "Bash(npm test:*)", + "Bash(npm run:*)" + ] + } +} 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..421da1c 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,139 @@ 'use strict'; +const http = require('http'); +const fs = require('fs'); +const path = require('path'); +const zlib = require('zlib'); +const { formidable } = require('formidable'); + +// Map compression types to their file extensions +const extensionMap = new Map([ + ['gzip', '.gz'], + ['deflate', '.dfl'], + ['br', '.br'], +]); + +// Map compression types to their zlib stream creators +const compressionStreamMap = new Map([ + ['gzip', () => zlib.createGzip()], + ['deflate', () => zlib.createDeflate()], + ['br', () => zlib.createBrotliCompress()], +]); + function createServer() { - /* Write your code here */ - // Return instance of http.Server class + const server = http.createServer((req, res) => { + // Handle root path - serve HTML file + if (req.url === '/' && req.method === 'GET') { + const htmlPath = path.join(__dirname, 'public', 'index.html'); + + fs.readFile(htmlPath, 'utf8', (err, data) => { + if (err) { + res.writeHead(500, { 'Content-Type': 'text/plain' }); + res.end('Internal Server Error'); + + return; + } + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(data); + }); + + return; + } + + // Handle CSS file + if (req.url === '/styles.css' && req.method === 'GET') { + const cssPath = path.join(__dirname, 'public', 'styles.css'); + + fs.readFile(cssPath, 'utf8', (err, data) => { + if (err) { + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Not found'); + + return; + } + + res.writeHead(200, { 'Content-Type': 'text/css' }); + res.end(data); + }); + + return; + } + + // Handle /compress endpoint + if (req.url === '/compress') { + if (req.method !== 'POST') { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Bad Request: Only POST method is allowed'); + + return; + } + + const form = formidable({}); + + form.parse(req, (err, fields, files) => { + if (err) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Bad Request: Could not parse form data'); + + return; + } + + const compressionType = Array.isArray(fields.compressionType) + ? fields.compressionType[0] + : fields.compressionType; + + const file = Array.isArray(files.file) ? files.file[0] : files.file; + + if (!file) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Bad Request: No file provided'); + + return; + } + + if (!compressionType) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Bad Request: No compression type provided'); + + return; + } + + // Validate compression type using the Map + if (!extensionMap.has(compressionType)) { + res.writeHead(400, { 'Content-Type': 'text/plain' }); + res.end('Bad Request: Unsupported compression type'); + + return; + } + + // Get the correct file extension from the Map + const fileName = file.originalFilename; + const extension = extensionMap.get(compressionType); + const compressedFileName = `${fileName}${extension}`; + + // Get the compression stream creator from the Map + const createCompressionStream = + compressionStreamMap.get(compressionType); + const compressionStream = createCompressionStream(); + + res.writeHead(200, { + 'Content-Type': 'application/octet-stream', + 'Content-Disposition': `attachment; filename=${compressedFileName}`, + }); + + fs.createReadStream(file.filepath).pipe(compressionStream).pipe(res); + }); + + return; + } + + // Handle 404 for all other routes + res.writeHead(404, { 'Content-Type': 'text/plain' }); + res.end('Not found'); + }); + + return server; } module.exports = { diff --git a/src/public/index.html b/src/public/index.html new file mode 100644 index 0000000..5a40410 --- /dev/null +++ b/src/public/index.html @@ -0,0 +1,37 @@ + + +
+ + +