-
Notifications
You must be signed in to change notification settings - Fork 304
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 2 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,125 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| 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); | ||
| res.end('Form parsing error'); | ||
| }); | ||
|
|
||
| let filename = ''; | ||
| let type = ''; | ||
| let hasFile = false; | ||
| const chunks = []; | ||
| let fileStreamError = false; | ||
|
|
||
| bb.on('file', (fieldname, file, info) => { | ||
| hasFile = true; | ||
| filename = info.filename; | ||
|
|
||
| file.on('data', (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| file.on('error', () => { | ||
| fileStreamError = true; | ||
| }); | ||
| }); | ||
|
|
||
| bb.on('field', (fieldname, val) => { | ||
| if (fieldname === 'compressionType') { | ||
| type = val; | ||
| } | ||
| }); | ||
|
|
||
| bb.on('finish', () => { | ||
| if (fileStreamError) { | ||
| res.writeHead(500, { 'Content-Type': 'text/plain' }); | ||
| res.end('Stream error'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!hasFile || !filename || !type) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Invalid form'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!['gzip', 'deflate', 'br'].includes(type)) { | ||
| res.writeHead(400, { 'Content-Type': 'text/plain' }); | ||
| res.end('Unsupported type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const rawFile = Buffer.concat(chunks); | ||
| let compressedFile; | ||
|
|
||
| switch (type) { | ||
| case 'gzip': | ||
| compressedFile = zlib.gzipSync(rawFile); | ||
| break; | ||
| case 'deflate': | ||
| compressedFile = zlib.deflateSync(rawFile); | ||
| break; | ||
| case 'br': | ||
| compressedFile = zlib.brotliCompressSync(rawFile); | ||
| break; | ||
|
Comment on lines
+83
to
+93
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. Using synchronous compression (gzipSync, deflateSync, brotliCompressSync). The requirement specifies using streams - use |
||
| } | ||
|
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. This implementation buffers the entire file into memory before compressing it, which goes against the requirement to use Streams. For a proper stream-based solution, you should pipe the file stream from |
||
|
|
||
| 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: |
||
| }); | ||
| res.end(compressedFile); | ||
| }); | ||
|
|
||
| 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.
The code buffers the entire file in memory before compression. Per requirements, you should pipe the file stream from busboy through a zlib transform stream (e.g.,
file.pipe(zlib.createGzip())) directly to the response, not collect chunks in an array first.