From 561eed1c830a5eff3f4200f10fe24652031af8ac Mon Sep 17 00:00:00 2001 From: TetiankaSh Date: Wed, 28 Jan 2026 19:30:01 +0200 Subject: [PATCH 1/3] initial commit --- .github/workflows/test.yml-template | 23 ++++++ package-lock.json | 8 +- package.json | 2 +- src/createServer.js | 111 ++++++++++++++++++++++++++-- 4 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/test.yml-template 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..c5137ac 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,10 +1,111 @@ 'use strict'; +const http = require('http'); +const zlib = require('zlib'); + 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 === 'GET') { + res.statusCode = 400; + + return res.end('Only POST requests are allowed'); + } + + if (method === 'POST') { + const body = []; + + req.on('data', (chunk) => body.push(chunk)); + + req.on('end', () => { + const buffer = Buffer.concat(body); + + const bodyStr = buffer.toString('binary'); + + const filenameMatch = bodyStr.match(/filename="(.+?)"/); + const typeMatch = bodyStr.match( + /name="compressionType"\r\n\r\n(.+?)\r\n/, + ); + + if (!filenameMatch || !typeMatch) { + res.statusCode = 400; + + return res.end('Invalid form data'); + } + + const filename = filenameMatch[1]; + const compressionType = typeMatch[1].trim(); + + const filePartHeader = bodyStr.match( + /filename=".+?"\r\nContent-Type: .+?\r\n\r\n/, + ); + + if (!filePartHeader) { + res.statusCode = 400; + + return res.end('Invalid file part'); + } + + const startIdx = + bodyStr.indexOf(filePartHeader[0]) + filePartHeader[0].length; + const boundary = bodyStr.split('\r\n')[0]; + const endIdx = bodyStr.indexOf(boundary, startIdx) - 2; + + const fileBuffer = buffer.slice(startIdx, endIdx); + + const validTypes = { + gzip: { method: zlib.gzipSync, ext: 'gzip' }, + deflate: { method: zlib.deflateSync, ext: 'deflate' }, + br: { method: zlib.brotliCompressSync, ext: 'br' }, + }; + + const config = validTypes[compressionType]; + + if (!config) { + res.statusCode = 400; + + return res.end('Unsupported compression type'); + } + + try { + const compressedData = config.method(fileBuffer); + + res.writeHead(200, { + 'Content-type': 'application/octet-stream', + 'Content-Disposition': `attachment; filename=${filename}.${config.ext}`, + }); + + res.end(compressedData); + } catch (err) { + res.statusCode = 400; + res.end('Compression failed'); + } + }); + } + }); } -module.exports = { - createServer, -}; +module.exports = { createServer }; From a4539b2e2657b567b20244b6337a47c93175ec3d Mon Sep 17 00:00:00 2001 From: TetiankaSh Date: Thu, 29 Jan 2026 12:05:38 +0200 Subject: [PATCH 2/3] some fixes required from the AI --- src/createServer.js | 90 ++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 62 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index c5137ac..7401c09 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -2,6 +2,7 @@ const http = require('http'); const zlib = require('zlib'); +const { pipeline } = require('stream'); function createServer() { return http.createServer((req, res) => { @@ -29,82 +30,47 @@ function createServer() { return res.end('The endpoint does not exist'); } - if (method === 'GET') { + if (method !== 'POST') { res.statusCode = 400; return res.end('Only POST requests are allowed'); } - if (method === 'POST') { - const body = []; + const urlParams = new URL(url, `http://${req.headers.host}`); + const compressionType = + urlParams.searchParams.get('compressionType') || + req.headers['x-compression-type'] || + 'gzip'; - req.on('data', (chunk) => body.push(chunk)); + const validTypes = { + gzip: { create: zlib.createGzip, ext: 'gz' }, + deflate: { create: zlib.createDeflate, ext: 'dfl' }, + br: { create: zlib.createBrotliCompress, ext: 'br' }, + }; - req.on('end', () => { - const buffer = Buffer.concat(body); + const config = validTypes[compressionType]; - const bodyStr = buffer.toString('binary'); - - const filenameMatch = bodyStr.match(/filename="(.+?)"/); - const typeMatch = bodyStr.match( - /name="compressionType"\r\n\r\n(.+?)\r\n/, - ); - - if (!filenameMatch || !typeMatch) { - res.statusCode = 400; - - return res.end('Invalid form data'); - } - - const filename = filenameMatch[1]; - const compressionType = typeMatch[1].trim(); - - const filePartHeader = bodyStr.match( - /filename=".+?"\r\nContent-Type: .+?\r\n\r\n/, - ); - - if (!filePartHeader) { - res.statusCode = 400; - - return res.end('Invalid file part'); - } - - const startIdx = - bodyStr.indexOf(filePartHeader[0]) + filePartHeader[0].length; - const boundary = bodyStr.split('\r\n')[0]; - const endIdx = bodyStr.indexOf(boundary, startIdx) - 2; - - const fileBuffer = buffer.slice(startIdx, endIdx); - - const validTypes = { - gzip: { method: zlib.gzipSync, ext: 'gzip' }, - deflate: { method: zlib.deflateSync, ext: 'deflate' }, - br: { method: zlib.brotliCompressSync, ext: 'br' }, - }; - - const config = validTypes[compressionType]; - - if (!config) { - res.statusCode = 400; - - return res.end('Unsupported compression type'); - } + if (!config) { + res.statusCode = 400; - try { - const compressedData = config.method(fileBuffer); + return res.end('Unsupported compression type'); + } - res.writeHead(200, { - 'Content-type': 'application/octet-stream', - 'Content-Disposition': `attachment; filename=${filename}.${config.ext}`, - }); + res.writeHead(200, { + 'Content-Type': 'application/octet-stream', + 'Content-Disposition': `attachment; filename="file.${config.ext}"`, + }); - res.end(compressedData); - } catch (err) { + pipeline(req, config.create(), res, (err) => { + if (err) { + if (!res.headersSent) { res.statusCode = 400; res.end('Compression failed'); + } else { + res.end(); } - }); - } + } + }); }); } From 0e781c503bf0c21eed5a90b91a2d77fa71c46281 Mon Sep 17 00:00:00 2001 From: TetiankaSh Date: Thu, 29 Jan 2026 12:25:41 +0200 Subject: [PATCH 3/3] more fixes --- src/createServer.js | 85 +++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/src/createServer.js b/src/createServer.js index 7401c09..05e66be 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,8 +1,9 @@ +/* eslint-disable no-console */ 'use strict'; const http = require('http'); const zlib = require('zlib'); -const { pipeline } = require('stream'); +const { pipeline, Readable } = require('stream'); function createServer() { return http.createServer((req, res) => { @@ -36,39 +37,71 @@ function createServer() { return res.end('Only POST requests are allowed'); } - const urlParams = new URL(url, `http://${req.headers.host}`); - const compressionType = - urlParams.searchParams.get('compressionType') || - req.headers['x-compression-type'] || - 'gzip'; + let bodyBuffer = Buffer.alloc(0); + let isStreamingStarted = false; - const validTypes = { - gzip: { create: zlib.createGzip, ext: 'gz' }, - deflate: { create: zlib.createDeflate, ext: 'dfl' }, - br: { create: zlib.createBrotliCompress, ext: 'br' }, - }; + req.on('data', (chunk) => { + if (isStreamingStarted) { + return; + } - const config = validTypes[compressionType]; + bodyBuffer = Buffer.concat([bodyBuffer, chunk]); - if (!config) { - res.statusCode = 400; + 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/, + ); - return res.end('Unsupported compression type'); - } + if (filePartHeaderMatch && typeMatch) { + isStreamingStarted = true; - res.writeHead(200, { - 'Content-Type': 'application/octet-stream', - 'Content-Disposition': `attachment; filename="file.${config.ext}"`, - }); + const filename = filePartHeaderMatch[1]; + const compressionType = typeMatch[1].trim(); - pipeline(req, config.create(), res, (err) => { - if (err) { - if (!res.headersSent) { + 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; - res.end('Compression failed'); - } else { - res.end(); + + 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); + } + }); } }); });