-
Notifications
You must be signed in to change notification settings - Fork 293
Solution #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VictorKomara
wants to merge
1
commit into
mate-academy:master
Choose a base branch
from
VictorKomara:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−5
Open
Solution #208
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,109 @@ | ||
| '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 url = new URL(req.url, `http://${req.headers.host}`); | ||
|
|
||
| if (url.pathname === '/' && req.method === 'GET') { | ||
| res.statusCode = 200; | ||
|
|
||
| return res.end('OK'); | ||
| } | ||
|
|
||
| if (url.pathname !== '/compress') { | ||
| res.statusCode = 404; | ||
|
|
||
| return res.end('Not Found'); | ||
| } | ||
|
|
||
| if (req.method === 'GET') { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Bad Request'); | ||
| } | ||
|
|
||
| // Збираємо тіло запиту в один Buffer (бінарно) | ||
| const chunks = []; | ||
|
|
||
| req.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| req.on('end', () => { | ||
| const fullBody = Buffer.concat(chunks); | ||
| // Перетворюємо в рядок тільки для пошуку метаданих, | ||
| // але сам файл будемо витягувати як Buffer | ||
| const bodyString = fullBody.toString('binary'); | ||
|
|
||
| // 1. Витягуємо тип стиснення | ||
| // Шукаємо рядок після name="compressionType" | ||
| const compMatch = bodyString.match(/name="compressionType"\r\n\r\n(\w+)/); | ||
| const compressionType = compMatch ? compMatch[1].trim() : null; | ||
|
|
||
| // 2. Витягуємо файл та його назву | ||
| // Шукаємо назву файлу | ||
| const filenameMatch = bodyString.match(/name="file"; filename="(.+?)"/); | ||
| const filename = filenameMatch ? filenameMatch[1] : null; | ||
|
|
||
| // Знаходимо початок і кінець вмісту файлу | ||
| // Вміст файлу починається після \r\n\r\n після заголовків частини | ||
| const fileHeaderMarker = 'name="file"'; | ||
| const headerEndIndex = | ||
| bodyString.indexOf('\r\n\r\n', bodyString.indexOf(fileHeaderMarker)) + | ||
| 4; | ||
|
|
||
| // Кінець файлу — це наступний розділювач (починається з --) | ||
| const boundary = req.headers['content-type'].split('boundary=')[1]; | ||
| const footerStartIndex = | ||
| bodyString.indexOf('--' + boundary, headerEndIndex) - 2; // -2 для \r\n | ||
|
|
||
| if ( | ||
| !compressionType || | ||
| !filename || | ||
| headerEndIndex < 4 || | ||
| footerStartIndex < 0 | ||
| ) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Bad Request: Invalid Form'); | ||
| } | ||
|
|
||
| const fileContent = fullBody.slice(headerEndIndex, footerStartIndex); | ||
|
|
||
| // 3. Налаштовуємо стиснення | ||
| let compressStream; | ||
|
|
||
| if (compressionType === 'gzip') { | ||
| compressStream = zlib.createGzip(); | ||
| } else if (compressionType === 'deflate') { | ||
| compressStream = zlib.createDeflate(); | ||
| } else if (compressionType === 'br') { | ||
| compressStream = zlib.createBrotliCompress(); | ||
| } else { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Unsupported compression type'); | ||
| } | ||
|
|
||
| res.setHeader( | ||
| 'Content-Disposition', | ||
| `attachment; filename=${filename}.${compressionType}`, | ||
| ); | ||
| res.statusCode = 200; | ||
|
|
||
| // Пускаємо потік: Buffer -> Stream -> Zlib -> Response | ||
| pipeline(Readable.from(fileContent), compressStream, res, (err) => { | ||
| if (err) { | ||
| if (!res.headersSent) { | ||
| res.statusCode = 500; | ||
| res.end(); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
| }; | ||
| module.exports = { createServer }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filename extension doesn't fully match the requirements. According to the description,
gzipcompression should result in a.gzextension, anddeflatein a.dflextension. Currently, thecompressionTypevalue is used directly, which creates incorrect extensions like.gzipand.deflate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried using short extensions (.gz, .dfl) as you suggested, but the automated tests fail because they expect full names (.gzip, .deflate) that match the compressionType field. So I returned the full names so the tests would pass.