-
Notifications
You must be signed in to change notification settings - Fork 305
added solution #223
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
yahohulia
wants to merge
4
commits into
mate-academy:master
Choose a base branch
from
yahohulia: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.
Open
added solution #223
Changes from 1 commit
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 |
|---|---|---|
| @@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,8 +1,237 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const { Readable } = require('stream'); | ||
| const zlib = require('zlib'); | ||
|
|
||
| const compressors = { | ||
| gzip: zlib.createGzip, | ||
| deflate: zlib.createDeflate, | ||
| br: zlib.createBrotliCompress, | ||
| }; | ||
|
|
||
| const extensions = { | ||
| gzip: '.gzip', | ||
| deflate: '.deflate', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the task description, the file extension for |
||
| br: '.br', | ||
| }; | ||
|
|
||
| async function readRequestBody(req) { | ||
| const chunks = []; | ||
|
|
||
| for await (const chunk of req) { | ||
| chunks.push(chunk); | ||
| } | ||
|
|
||
| return Buffer.concat(chunks); | ||
| } | ||
|
|
||
| function getBoundary(contentType = '') { | ||
| const match = contentType.match(/boundary=(?:"([^"]+)"|([^;]+))/i); | ||
|
|
||
| return match ? match[1] || match[2] : null; | ||
| } | ||
|
|
||
| function parsePartHeaders(headerText) { | ||
| const headers = {}; | ||
|
|
||
| headerText.split('\r\n').forEach((line) => { | ||
| const idx = line.indexOf(':'); | ||
|
|
||
| if (idx === -1) { | ||
| return; | ||
| } | ||
|
|
||
| const key = line.slice(0, idx).trim().toLowerCase(); | ||
| const value = line.slice(idx + 1).trim(); | ||
|
|
||
| headers[key] = value; | ||
| }); | ||
|
|
||
| return headers; | ||
| } | ||
|
|
||
| function parseContentDisposition(value = '') { | ||
| const nameMatch = value.match(/name="([^"]+)"/i); | ||
| const filenameMatch = value.match(/filename="([^"]*)"/i); | ||
|
|
||
| return { | ||
| name: nameMatch ? nameMatch[1] : null, | ||
| filename: filenameMatch ? filenameMatch[1] : null, | ||
| }; | ||
| } | ||
|
|
||
| function trimCrlf(buf) { | ||
| if (buf.length >= 2 && buf.slice(-2).toString() === '\r\n') { | ||
| return buf.slice(0, -2); | ||
| } | ||
|
|
||
| return buf; | ||
| } | ||
|
|
||
| function parseMultipart(bodyBuffer, boundary) { | ||
| const boundaryToken = Buffer.from(`--${boundary}`); | ||
| const result = { | ||
| fields: {}, | ||
| file: null, | ||
| }; | ||
|
|
||
| let pos = 0; | ||
| const first = bodyBuffer.indexOf(boundaryToken, pos); | ||
|
|
||
| if (first !== 0) { | ||
| return null; | ||
| } | ||
|
|
||
| pos = first; | ||
|
|
||
| while (pos < bodyBuffer.length) { | ||
| const boundaryStart = bodyBuffer.indexOf(boundaryToken, pos); | ||
|
|
||
| if (boundaryStart === -1) { | ||
| break; | ||
| } | ||
|
|
||
| let partStart = boundaryStart + boundaryToken.length; | ||
|
|
||
| // eslint-disable-next-line | ||
| const isFinal = | ||
| bodyBuffer.slice(partStart, partStart + 2).toString() === '--'; | ||
|
|
||
| if (isFinal) { | ||
| break; | ||
| } | ||
|
|
||
| if (bodyBuffer.slice(partStart, partStart + 2).toString() === '\r\n') { | ||
| partStart += 2; | ||
| } | ||
|
|
||
| const headersEnd = bodyBuffer.indexOf(Buffer.from('\r\n\r\n'), partStart); | ||
|
|
||
| if (headersEnd === -1) { | ||
| return null; | ||
| } | ||
|
|
||
| const headerText = bodyBuffer.slice(partStart, headersEnd).toString('utf8'); | ||
| const headers = parsePartHeaders(headerText); | ||
|
|
||
| const cd = parseContentDisposition(headers['content-disposition']); | ||
|
|
||
| if (!cd.name) { | ||
| return null; | ||
| } | ||
|
|
||
| const contentStart = headersEnd + 4; | ||
|
|
||
| const nextBoundary = bodyBuffer.indexOf(boundaryToken, contentStart); | ||
|
|
||
| if (nextBoundary === -1) { | ||
| return null; | ||
| } | ||
|
|
||
| const rawContent = bodyBuffer.slice(contentStart, nextBoundary); | ||
| const content = trimCrlf(rawContent); | ||
|
|
||
| if (cd.filename !== null) { | ||
| result.file = { | ||
| fieldName: cd.name, | ||
| filename: cd.filename, | ||
| buffer: content, | ||
| }; | ||
| } else { | ||
| result.fields[cd.name] = content.toString('utf8').trim(); | ||
| } | ||
|
|
||
| pos = nextBoundary; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer(async (req, res) => { | ||
| if (req.method === 'GET' && 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 boundary = getBoundary(contentType); | ||
|
|
||
| if (!boundary) { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| let body; | ||
|
|
||
| try { | ||
| body = await readRequestBody(req); | ||
| } catch (error) { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const parsed = parseMultipart(body, boundary); | ||
|
|
||
| if (!parsed) { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compressionType = parsed.fields.compressionType; | ||
| const file = parsed.file; | ||
|
|
||
| if ( | ||
| !compressionType || | ||
| !file || | ||
| file.fieldName !== 'file' || | ||
| !file.filename | ||
| ) { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!compressors[compressionType]) { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compressor = compressors[compressionType](); | ||
| const outName = `${file.filename}${extensions[compressionType]}`; | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Disposition': `attachment; filename=${outName}`, | ||
| }); | ||
|
|
||
| Readable.from(file.buffer).pipe(compressor).pipe(res); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Document</title> | ||
| </head> | ||
| <body style="display: flex; justify-content: center;"> | ||
| <form action="/compress" method="post" enctype="multipart/form-data" style="display: flex; flex-direction: column;"> | ||
| <div style="margin-top: 10px;;"><input type="file" name="file"></div> | ||
| <div style="margin-top: 10px;"> | ||
| <select name="compressionType"> | ||
| <option value="gzip">Gzip</option> | ||
| <option value="deflate">Deflate</option> | ||
| <option value="br">Brotli</option> | ||
| </select> | ||
| </div> | ||
|
|
||
| <button type="submit" style="margin-top: 10px;">Submit</button> | ||
| </form> | ||
|
|
||
| </body> | ||
| </html> |
Oops, something went wrong.
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 task description specifies that the file extension for
gzipcompression should be.gz. For example,file.txtshould becomefile.txt.gz.