-
Notifications
You must be signed in to change notification settings - Fork 275
node_compression-app_TetSh #204
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?
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,110 @@ | ||
| /* eslint-disable no-console */ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const zlib = require('zlib'); | ||
| const { pipeline, Readable } = require('stream'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const { method, url } = req; | ||
|
|
||
| if (url === '/' && method === 'GET') { | ||
| res.writeHead(200, { 'Content-type': 'text/html' }); | ||
|
|
||
| return res.end(` | ||
| <form action="/compress" method="POST" enctype="multipart/form-data"> | ||
| <input type="file" name="file" required> | ||
| <select name="compressionType"> | ||
| <option value="gzip">gzip</option> | ||
| <option value="deflate">deflate</option> | ||
| <option value="br">br</option> | ||
| </select> | ||
| <button type="submit">Compress</button> | ||
| </form> | ||
| `); | ||
| } | ||
|
|
||
| if (url !== '/compress') { | ||
| res.statusCode = 404; | ||
|
|
||
| return res.end('The endpoint does not exist'); | ||
| } | ||
|
|
||
| if (method !== 'POST') { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Only POST requests are allowed'); | ||
| } | ||
|
|
||
| let bodyBuffer = Buffer.alloc(0); | ||
| let isStreamingStarted = false; | ||
|
|
||
| req.on('data', (chunk) => { | ||
| if (isStreamingStarted) { | ||
| return; | ||
| } | ||
|
|
||
| bodyBuffer = Buffer.concat([bodyBuffer, chunk]); | ||
|
|
||
| const bodyStr = bodyBuffer.toString('binary'); | ||
| const filePartHeaderMatch = bodyStr.match( | ||
| /filename="(.+?)"\r\nContent-Type: .+?\r\n\r\n/, | ||
| ); | ||
| const typeMatch = bodyStr.match( | ||
| /name="compressionType"\r\n\r\n(.+?)\r\n/, | ||
| ); | ||
|
|
||
| if (filePartHeaderMatch && typeMatch) { | ||
| isStreamingStarted = true; | ||
|
|
||
| const filename = filePartHeaderMatch[1]; | ||
| const compressionType = typeMatch[1].trim(); | ||
|
|
||
| const validTypes = { | ||
| gzip: { create: zlib.createGzip, ext: 'gz' }, | ||
| deflate: { create: zlib.createDeflate, ext: 'dfl' }, | ||
| br: { create: zlib.createBrotliCompress, ext: 'br' }, | ||
| }; | ||
|
|
||
| const config = validTypes[compressionType]; | ||
|
|
||
| if (!config) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Unsupported compression type'); | ||
| } | ||
|
|
||
| // Prepare response | ||
| res.writeHead(200, { | ||
| 'Content-Type': 'application/octet-stream', | ||
| 'Content-Disposition': `attachment; filename="${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 requires using the correct file extension for the compressed file (e.g., |
||
| }); | ||
|
|
||
| const headerEndIndex = | ||
| bodyStr.indexOf(filePartHeaderMatch[0]) + | ||
| filePartHeaderMatch[0].length; | ||
| const initialFileData = bodyBuffer.slice(headerEndIndex); | ||
|
|
||
| const boundary = bodyStr.split('\r\n')[0]; | ||
| const footerIndex = initialFileData | ||
| .toString('binary') | ||
| .indexOf(boundary); | ||
|
|
||
| const actualFileSource = Readable.from( | ||
| footerIndex !== -1 | ||
| ? initialFileData.slice(0, footerIndex - 2) | ||
| : initialFileData, | ||
| ); | ||
|
|
||
| pipeline(actualFileSource, config.create(), res, (err) => { | ||
| if (err) { | ||
| console.error(err); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
|
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 server needs to handle cases where the form submission is invalid (e.g., no file is uploaded) and respond with a 400 status code as required. Currently, if the |
||
| }); | ||
| } | ||
|
|
||
| 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.
This implementation has a few issues. First, by buffering the entire request with
Buffer.concat, it defeats the purpose of using streams for potentially large files, risking out-of-memory errors. Second, theisStreamingStartedcheck on the next line will cause your server to ignore all subsequent data chunks after the file headers are found, leading to truncated and corrupted files. The file's content needs to be piped from the request stream to the compression stream as it arrives.