-
Notifications
You must be signed in to change notification settings - Fork 293
Develop #207
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?
Develop #207
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,113 @@ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const zlib = require('zlib'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| if (req.method === 'GET' && req.url === '/') { | ||
| res.statusCode = 200; | ||
| res.end('OK'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.url !== '/compress') { | ||
| res.statusCode = 404; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (req.method !== 'POST') { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const contentType = req.headers['content-type'] || ''; | ||
|
|
||
| if (!contentType.includes('multipart/form-data')) { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const boundary = '--' + contentType.split('boundary=')[1]; | ||
|
|
||
| const chunks = []; | ||
|
|
||
| req.on('data', (chunk) => chunks.push(chunk)); | ||
|
|
||
| req.on('end', () => { | ||
| const body = Buffer.concat(chunks).toString('binary'); | ||
|
|
||
| const parts = body.split(boundary).filter((p) => p.trim()); | ||
|
|
||
| let fileBuffer = null; | ||
| let filename = null; | ||
| let compressionType = null; | ||
|
|
||
| for (const part of parts) { | ||
| if (part.includes('name="file"')) { | ||
| const headerEnd = part.indexOf('\r\n\r\n'); | ||
|
|
||
| const headers = part.slice(0, headerEnd); | ||
| const content = part.slice(headerEnd + 4, part.lastIndexOf('\r\n')); | ||
|
|
||
| const match = headers.match(/filename="(.+?)"/); | ||
|
|
||
| if (match) { | ||
| filename = match[1]; | ||
| } | ||
|
|
||
| fileBuffer = Buffer.from(content, 'binary'); | ||
| } | ||
|
|
||
| if (part.includes('name="compressionType"')) { | ||
| const headerEnd = part.indexOf('\r\n\r\n'); | ||
|
|
||
| const value = part | ||
| .slice(headerEnd + 4) | ||
| .replace(/\r\n$/, '') | ||
| .trim(); | ||
|
|
||
| compressionType = value; | ||
| } | ||
| } | ||
|
|
||
| const compressors = { | ||
| gzip: zlib.gzipSync, | ||
| deflate: zlib.deflateSync, | ||
| br: zlib.brotliCompressSync, | ||
| }; | ||
|
|
||
| if (!fileBuffer || !compressionType || !compressors[compressionType]) { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const compressed = compressors[compressionType](fileBuffer); | ||
|
Comment on lines
+81
to
+94
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 use of synchronous |
||
|
|
||
| res.statusCode = 200; | ||
|
|
||
| res.setHeader( | ||
| '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 filename extension is being set directly from the |
||
| ); | ||
|
|
||
| res.end(compressed); | ||
| }); | ||
|
|
||
| req.on('error', () => { | ||
| res.statusCode = 500; | ||
| res.end(); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| 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 buffers the entire request body into memory before processing it, which does not meet the core 'use Streams' requirement. A streaming approach processes data in chunks as it arrives. You should refactor this to parse the incoming request stream, pipe the file data through a
zlibtransform stream (e.g.,zlib.createGzip()), and then pipe the result to the response stream. This avoids loading the entire file into memory.