-
Notifications
You must be signed in to change notification settings - Fork 274
solution #191
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
base: master
Are you sure you want to change the base?
solution #191
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| body { | ||
| font-family: Arial, sans-serif; | ||
| background: #f5f5f5; | ||
| margin: 0; | ||
| padding: 0; | ||
|
|
||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| height: 100vh; | ||
| } | ||
|
|
||
| .form { | ||
| background: #fff; | ||
| padding: 25px 30px; | ||
| border-radius: 12px; | ||
| box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); | ||
| width: 320px; | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: 15px; | ||
| } | ||
|
|
||
| label { | ||
| font-weight: bold; | ||
| margin-bottom: 5px; | ||
| } | ||
|
|
||
| input[type="file"], | ||
| select { | ||
| padding: 8px; | ||
| border: 1px solid #ccc; | ||
| border-radius: 6px; | ||
| font-size: 14px; | ||
| } | ||
|
|
||
| button { | ||
| padding: 10px; | ||
| background: #007bff; | ||
| border: none; | ||
| color: white; | ||
| font-size: 16px; | ||
| border-radius: 6px; | ||
| cursor: pointer; | ||
| transition: 0.2s; | ||
| } | ||
|
|
||
| button:hover { | ||
| background: #0056c7; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Document</title> | ||
| <link rel="stylesheet" href="./index.css"> | ||
| </head> | ||
| <body> | ||
| <form action="/compress" method="POST" enctype="multipart/form-data" class="form"> | ||
| <label>Select file:</label> | ||
| <input type="file" name="file" required /> | ||
|
|
||
| <label>Compression type:</label> | ||
| <select name="compressionType" required> | ||
| <option value="gzip">GZIP</option> | ||
| <option value="deflate">Deflate</option> | ||
| <option value="br">Brotli</option> | ||
| </select> | ||
|
|
||
| <button type="submit">Compress</button> | ||
| </form> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,165 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const zlib = require('zlib'); | ||
| const busboy = require('busboy'); | ||
|
|
||
| // EXTENSIONS EXPECTED BY TESTS | ||
| const EXT_MAP = { | ||
| gzip: 'gzip', | ||
| deflate: 'deflate', | ||
| br: 'br', | ||
| }; | ||
|
Comment on lines
+10
to
+14
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. (HIGH) EXT_MAP currently maps to compression names ('gzip','deflate','br'). The tests expect file extensions so the produced filename must be e.g. |
||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new http.Server(); | ||
|
|
||
| server.on('request', (req, res) => { | ||
| const url = new URL(req.url, `http://${req.headers.host}`); | ||
|
|
||
| if (req.method === 'POST' && url.pathname === '/compress') { | ||
| const bb = busboy({ headers: req.headers }); | ||
|
|
||
| let compressionType = null; | ||
| let fileInfo = null; | ||
|
|
||
| bb.on('field', (name, val) => { | ||
| if (name === 'compressionType') { | ||
| compressionType = val; | ||
|
|
||
| if (fileInfo && !fileInfo.started) { | ||
| fileInfo.start(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| bb.on('file', (name, file, info) => { | ||
| file.pause(); | ||
|
|
||
| fileInfo = { | ||
| file, | ||
| info, | ||
| started: false, | ||
| start() { | ||
| if (this.started) { | ||
| return; | ||
| } | ||
| this.started = true; | ||
|
|
||
| if (!compressionType) { | ||
| return; | ||
| } // дочекаємося пізніше | ||
|
|
||
| if (!EXT_MAP[compressionType]) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Invalid compressionType'); | ||
|
Comment on lines
+55
to
+58
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. (MEDIUM) When |
||
| } | ||
|
|
||
| const outName = `${info.filename}.${EXT_MAP[compressionType]}`; | ||
|
|
||
| const compressor = | ||
| compressionType === 'gzip' | ||
| ? zlib.createGzip() | ||
| : compressionType === 'deflate' | ||
| ? zlib.createDeflate() | ||
| : zlib.createBrotliCompress(); | ||
|
|
||
| res.statusCode = 200; | ||
|
|
||
| res.setHeader( | ||
| 'Content-Disposition', | ||
| `attachment; filename=${outName}`, | ||
|
Comment on lines
+72
to
+74
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. (LOW) Wrap the filename in quotes in the |
||
| ); | ||
|
|
||
| file.on('error', () => { | ||
| if (!res.headersSent) { | ||
| res.statusCode = 500; | ||
| } | ||
| res.end(); | ||
| }); | ||
|
|
||
| compressor.on('error', () => { | ||
| if (!res.headersSent) { | ||
| res.statusCode = 500; | ||
| } | ||
| res.end(); | ||
| }); | ||
|
|
||
| file.resume(); | ||
| file.pipe(compressor).pipe(res); | ||
| }, | ||
| }; | ||
|
|
||
| if (compressionType) { | ||
| fileInfo.start(); | ||
| } | ||
| }); | ||
|
|
||
| bb.on('finish', () => { | ||
| if (!fileInfo) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('No file'); | ||
| } | ||
|
|
||
| if (!compressionType) { | ||
| // прибираємо пайпи, якщо були | ||
| fileInfo.file.unpipe(); | ||
|
|
||
| // дочитуємо файл до кінця, інакше busboy зависне | ||
| fileInfo.file.resume(); | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Missing compressionType'); | ||
| } | ||
|
|
||
| if (!fileInfo.started) { | ||
| fileInfo.start(); | ||
| } | ||
| }); | ||
|
|
||
| req.pipe(bb); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.method === 'GET' && url.pathname === '/compress') { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const fileName = url.pathname.slice(1) || 'index.html'; | ||
| const filePath = path.resolve('public', fileName); | ||
|
|
||
| if (!fs.existsSync(filePath)) { | ||
| res.statusCode = 404; | ||
|
|
||
| return res.end('file dont found'); | ||
| } | ||
|
|
||
| const ext = path.extname(filePath); | ||
| const mimeTypes = { | ||
| '.html': 'text/html', | ||
| '.css': 'text/css', | ||
| '.js': 'application/javascript', | ||
| '.json': 'application/json', | ||
| '.png': 'image/png', | ||
| '.jpg': 'image/jpeg', | ||
| '.gif': 'image/gif', | ||
| '.svg': 'image/svg+xml', | ||
| '.ico': 'image/x-icon', | ||
| }; | ||
|
|
||
| res.setHeader('Content-Type', mimeTypes[ext] || 'text/plain'); | ||
| fs.createReadStream(filePath).pipe(res); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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.
EXT_MAP currently maps to the compression names (e.g.
'gzip') rather than the file extensions the tests require. Update the mapping so the stored extension values are exactly:gzip: 'gz',deflate: 'dfl',br: 'br'so the produced filename becomesfile.txt.gz,file.txt.dfl, orfile.txt.bras required.