-
Notifications
You must be signed in to change notification settings - Fork 298
Solution #224
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 #224
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 |
|---|---|---|
| @@ -1,10 +1,190 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const fs = require('fs'); | ||
| const os = require('os'); | ||
| const path = require('path'); | ||
| const zlib = require('zlib'); | ||
| const busboy = require('busboy'); | ||
|
|
||
| const data = ` | ||
| <form method="POST" action="/compress" enctype="multipart/form-data"> | ||
| <input type="file" name="file" required> | ||
| <select name="compressionType" required> | ||
| <option value="gzip">gzip</option> | ||
| <option value="deflate">deflate</option> | ||
| <option value="br">br</option> | ||
| </select> | ||
| <button type="submit">Compress</button> | ||
| </form> | ||
| `; | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| if (req.method === 'GET') { | ||
| if (req.url === '/') { | ||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
| res.end(data); | ||
| } else if (req.url === '/compress') { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Incorrect request'); | ||
| } else { | ||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Page not found'); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.method === 'POST') { | ||
| if (req.url === '/compress') { | ||
| const bb = busboy({ headers: req.headers }); | ||
|
|
||
| bb.on('error', () => { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Form parsing error'); | ||
| }); | ||
|
|
||
| let filename = ''; | ||
| let type = ''; | ||
| let hasFile = false; | ||
| let streamError = false; | ||
| let tmpPath = ''; | ||
| let uploadDone = false; | ||
| let fileWritten = false; | ||
| let maybeResponded = false; | ||
|
|
||
| const tryHandleRequest = () => { | ||
| if (!uploadDone || !fileWritten || maybeResponded) { | ||
| return; | ||
| } | ||
|
|
||
| if (streamError) { | ||
| maybeResponded = true; | ||
| res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
| res.end('Stream error'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!hasFile || !filename || !type) { | ||
| maybeResponded = true; | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Invalid form'); | ||
|
|
||
| if (tmpPath) { | ||
| fs.unlink(tmpPath, () => {}); | ||
| } | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| let compressor = null; | ||
|
|
||
| switch (type) { | ||
| case 'gzip': | ||
| compressor = zlib.createGzip(); | ||
| break; | ||
| case 'deflate': | ||
| compressor = zlib.createDeflate(); | ||
| break; | ||
| case 'br': | ||
| compressor = zlib.createBrotliCompress(); | ||
| break; | ||
| default: | ||
| maybeResponded = true; | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Unsupported type'); | ||
|
|
||
| fs.unlink(tmpPath, () => {}); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| maybeResponded = true; | ||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/octet-stream', | ||
|
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. The extension uses |
||
| 'Content-Disposition': `attachment; filename=${filename}.${type}`, | ||
|
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. The filename extension does not match the requirements. For a compression type of 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. The task requires specific file extensions: |
||
| }); | ||
|
|
||
| const fileStream = fs.createReadStream(tmpPath); | ||
| const cleanup = () => { | ||
| fs.unlink(tmpPath, () => {}); | ||
| }; | ||
|
|
||
| fileStream.on('error', (err) => { | ||
| cleanup(); | ||
| res.destroy(err); | ||
| }); | ||
|
|
||
| compressor.on('error', (err) => { | ||
| cleanup(); | ||
| res.destroy(err); | ||
| }); | ||
|
|
||
| res.on('close', cleanup); | ||
| res.on('finish', cleanup); | ||
|
|
||
| fileStream.pipe(compressor).pipe(res); | ||
| }; | ||
|
|
||
| bb.on('file', (fieldname, file, info) => { | ||
| hasFile = true; | ||
| filename = info.filename; | ||
|
|
||
| tmpPath = path.join( | ||
| os.tmpdir(), | ||
| `compression-${Date.now()}-${Math.random().toString(16).slice(2)}`, | ||
| ); | ||
|
|
||
| const writeStream = fs.createWriteStream(tmpPath); | ||
|
|
||
| file.pipe(writeStream); | ||
|
|
||
| writeStream.on('finish', () => { | ||
| fileWritten = true; | ||
| tryHandleRequest(); | ||
| }); | ||
|
|
||
| file.on('error', () => { | ||
| streamError = true; | ||
| }); | ||
|
|
||
| writeStream.on('error', () => { | ||
| streamError = true; | ||
| }); | ||
| }); | ||
|
|
||
| bb.on('field', (fieldname, val) => { | ||
| if (fieldname === 'compressionType') { | ||
| type = val; | ||
| } | ||
| }); | ||
|
|
||
| bb.on('finish', () => { | ||
| uploadDone = true; | ||
|
|
||
| if (!hasFile && !maybeResponded) { | ||
| maybeResponded = true; | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Invalid form'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| tryHandleRequest(); | ||
| }); | ||
|
|
||
| req.pipe(bb); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.writeHead(404, { 'Content-Type': 'text/plain' }); | ||
| res.end('Page not found'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
| }; | ||
| module.exports = { createServer }; | ||
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.
Using synchronous compression (gzipSync, deflateSync, brotliCompressSync). The requirement specifies using streams - use
zlib.createGzip(),zlib.createDeflate(),zlib.createBrotliCompress()instead.