-
Notifications
You must be signed in to change notification settings - Fork 293
Develop #215
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 #215
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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
| <form action="http://localhost:5700/compress" | ||
| method="POST" | ||
| enctype="multipart/form-data" | ||
| > | ||
| <select name="compressionType" required > | ||
| <option value="gzip">gzip</option> | ||
| <option value="deflate">deflate</option> | ||
| <option value="br">br</option> | ||
| </select> | ||
| <input type="file" name="file" required> | ||
|
|
||
| <button type="submit">Send</button> | ||
| </form> | ||
| </body> | ||
| </html> | ||
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,137 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const http = require('http'); | ||
| const zlib = require('zlib'); | ||
| const busboy = require('busboy'); | ||
| const { PassThrough } = require('stream'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new http.Server(); | ||
|
|
||
| server.on('request', (req, res) => { | ||
| if (req.url === '/' && req.method === 'GET') { | ||
| const filePath = path.resolve(__dirname, 'index.html'); | ||
| const stream = fs.createReadStream(filePath); | ||
|
|
||
| stream.on('error', () => { | ||
| res.statusCode = 500; | ||
|
|
||
| return res.end('Cannot read index.html'); | ||
| }); | ||
|
|
||
| res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
|
|
||
| return stream.pipe(res); | ||
| } | ||
|
|
||
| if (req.url !== '/compress') { | ||
| res.statusCode = 404; | ||
|
|
||
| return res.end('Not Found'); | ||
| } | ||
|
|
||
| if (req.method !== 'POST') { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Only POST allowed!'); | ||
| } | ||
|
|
||
| let bb; | ||
|
|
||
| try { | ||
| bb = busboy({ headers: req.headers }); | ||
| } catch (err) { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Invalid Content-Type'); | ||
| } | ||
|
|
||
| let filename = ''; | ||
| let compressionType = ''; | ||
| let filesStream = null; | ||
| let gotFile = false; | ||
|
|
||
| const startCompression = () => { | ||
| if (!gotFile || !compressionType || !filesStream || res.headersSent) { | ||
| return; | ||
| } | ||
|
|
||
| let compressStream; | ||
| let contentType = 'application/octet-stream'; | ||
|
|
||
| if (compressionType === 'gzip') { | ||
| compressStream = zlib.createGzip(); | ||
| contentType = 'application/gzip'; | ||
| } else if (compressionType === 'deflate') { | ||
| compressStream = zlib.createDeflate(); | ||
| contentType = 'application/zlib'; | ||
| } else if (compressionType === 'br') { | ||
| compressStream = zlib.createBrotliCompress(); | ||
| contentType = 'application/brotli'; | ||
| } else { | ||
| res.statusCode = 400; | ||
|
|
||
| return res.end('Unsupported compression type'); | ||
| } | ||
|
Comment on lines
+64
to
+77
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 block correctly checks for |
||
|
|
||
| res.writeHead(200, { | ||
| 'Content-Type': contentType, | ||
| 'Content-Disposition': `attachment; filename=${filename}.${compressionType}`, | ||
| }); | ||
|
|
||
| filesStream.pipe(compressStream).pipe(res); | ||
| }; | ||
|
|
||
| bb.on('field', (name, val) => { | ||
| if (name === 'compressionType') { | ||
| compressionType = val; | ||
| startCompression(); | ||
| } | ||
| }); | ||
|
|
||
| bb.on('file', (fieldname, stream, info) => { | ||
| if (fieldname === 'file') { | ||
| gotFile = true; | ||
| filename = info.filename; | ||
|
|
||
| const pass = new PassThrough(); | ||
|
|
||
| stream.pipe(pass); | ||
| filesStream = pass; | ||
|
|
||
| startCompression(); | ||
|
|
||
| stream.on('error', () => { | ||
| if (!res.writableEnded) { | ||
| res.statusCode = 500; | ||
| res.end('File stream error'); | ||
| } | ||
| }); | ||
| } else { | ||
| stream.resume(); | ||
| } | ||
| }); | ||
|
|
||
| bb.on('close', () => { | ||
| if (!res.headersSent) { | ||
| res.statusCode = 400; | ||
| res.end('Invalid Form'); | ||
| } | ||
| }); | ||
|
|
||
| bb.on('error', () => { | ||
| if (!res.writableEnded) { | ||
| res.statusCode = 400; | ||
| res.end('Busboy error'); | ||
| } | ||
| }); | ||
|
|
||
| req.pipe(bb); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
| }; | ||
| module.exports = { createServer }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| sdfsdfsdsdf |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Document</title> | ||
| </head> | ||
| <body> | ||
| <form action="/compress" | ||
| method="POST" | ||
| enctype="multipart/form-data" | ||
| > | ||
| <select name="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. Consider adding the |
||
| <option value="">Choose a compression</option> | ||
| <option value="gzip">gzip</option> | ||
| <option value="deflate">deflate</option> | ||
| <option value="br">br</option> | ||
| </select> | ||
| <input type="file" name="file" required> | ||
|
|
||
| <button type="submit">Send</button> | ||
| </form> | ||
| </body> | ||
| </html> | ||
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
actionattribute is hardcoded to a specific URL. It's better practice to use a relative path like/compressto make the form independent of the server's host and port. Additionally, please note that the server implementation is configured to serve the file located atsrc/index.html, not this one.