-
Notifications
You must be signed in to change notification settings - Fork 305
solution #217
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 #217
Changes from 1 commit
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,8 +1,122 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const zlib = require('zlib'); | ||
| const { Readable } = require('stream'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((request, response) => { | ||
| if (request.url === '/' && request.method === 'GET') { | ||
| const htmlString = ` | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Form Example</title> | ||
| </head> | ||
| <body> | ||
| <form action="/compress" method="POST" enctype="multipart/form-data"> | ||
| <input type="file" name="file"> | ||
| <select name="compressionType"> | ||
| <option value="gzip">GZIP Compression</option> | ||
| <option value="deflate">DEFLATE Compression</option> | ||
| <option value="br">BR Compression</option> | ||
| </select> | ||
| <button type="submit">Submit</button> | ||
| </form> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| response.setHeader('Content-Type', 'text/html'); | ||
| response.statusCode = 200; | ||
| response.end(htmlString); | ||
| } else if (request.url === '/compress') { | ||
| if (request.method === 'GET') { | ||
| response.statusCode = 400; | ||
| response.end('Bad Request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (request.method === 'POST') { | ||
| const boundary = request.headers['content-type'].split('boundary=')[1]; | ||
|
|
||
| const chunks = []; | ||
|
|
||
| request.on('data', (chunk) => { | ||
| chunks.push(chunk); | ||
| }); | ||
|
|
||
| request.on('end', () => { | ||
| const fullBuffer = Buffer.concat(chunks); | ||
| const fileNameMatch = fullBuffer.toString().match(/filename="(.+?)"/); | ||
| const compressionMatch = fullBuffer | ||
| .toString() | ||
| .match(/name="compressionType"\r\n\r\n(.+?)\r\n/); | ||
|
|
||
| if (!fileNameMatch || !compressionMatch) { | ||
| response.statusCode = 400; | ||
| response.end('Bad Request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const fileName = fileNameMatch[1]; | ||
| const compressionType = compressionMatch[1]; | ||
|
|
||
| if (!['gzip', 'deflate', 'br'].includes(compressionType)) { | ||
| response.statusCode = 400; | ||
| response.end('Invalid Compression Type'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const startFileName = fullBuffer.indexOf('filename="' + fileName); | ||
| const fileContentStart = | ||
| fullBuffer.indexOf('\r\n\r\n', startFileName) + 4; | ||
| const fileContendEnd = fullBuffer.indexOf( | ||
| '\r\n--' + boundary, | ||
| fileContentStart, | ||
| ); | ||
| const fileBuffer = fullBuffer.subarray( | ||
| fileContentStart, | ||
| fileContendEnd, | ||
| ); | ||
|
|
||
| const fileStream = Readable.from(fileBuffer); | ||
|
|
||
| let compressedStream; | ||
|
|
||
| if (compressionType === 'gzip') { | ||
| compressedStream = zlib.createGzip(); | ||
| } | ||
|
|
||
| if (compressionType === 'deflate') { | ||
| compressedStream = zlib.createDeflate(); | ||
| } | ||
|
|
||
| if (compressionType === 'br') { | ||
| compressedStream = zlib.createBrotliCompress(); | ||
| } | ||
|
|
||
| const newFileName = `${fileName}.${compressionType}`; | ||
|
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 description requires the file extensions to be |
||
|
|
||
| response.statusCode = 200; | ||
|
|
||
| response.setHeader( | ||
| 'Content-Disposition', | ||
| `attachment; filename=${newFileName}`, | ||
| ); | ||
|
|
||
| fileStream.pipe(compressedStream).pipe(response); | ||
| }); | ||
| } | ||
| } else { | ||
| response.statusCode = 404; | ||
| response.end('Not Found'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| 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.
The application could crash if the 'content-type' header is missing or doesn't contain a boundary. It's important to validate the header before attempting to access its properties. According to the requirements, the server should handle this as an invalid form submission and respond with a 400 status code.