-
Notifications
You must be signed in to change notification settings - Fork 200
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
Develop #129
base: master
Are you sure you want to change the base?
Develop #129
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,8 +1,144 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable curly */ | ||
'use strict'; | ||
|
||
const http = require('node:http'); | ||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
const { formidable } = require('formidable'); | ||
const { | ||
createGzip, | ||
createBrotliCompress, | ||
createDeflate, | ||
} = require('node:zlib'); | ||
|
||
function createServer() { | ||
/* Write your code here */ | ||
// Return instance of http.Server class | ||
const server = new http.Server(); | ||
|
||
server.on('request', async (req, res) => { | ||
if (req.url === '/favicon.ico') { | ||
res.statusCode = 200; | ||
res.end(''); | ||
|
||
return; | ||
} | ||
|
||
if (req.url === '/compress' && req.method === 'GET') { | ||
res.statusCode = 400; | ||
res.end(''); | ||
|
||
return; | ||
} | ||
|
||
if (req.url === '/' && req.method === 'GET') { | ||
const normalizePath = path.resolve(__dirname, 'index.html'); | ||
|
||
res.statusCode = 200; | ||
res.setHeader('Content-Type', 'text/html'); | ||
|
||
const fileStream = fs.createReadStream(normalizePath); | ||
|
||
fileStream.pipe(res).on('error', () => {}); | ||
|
||
fileStream.on('error', (err) => { | ||
res.statusCode = 500; | ||
res.end(`Server error ${err}`); | ||
}); | ||
|
||
res.on('close', () => fileStream.destroy()); | ||
|
||
return; | ||
} | ||
|
||
if (req.method === 'POST' && req.url === '/compress') { | ||
const form = formidable({}); | ||
let fields, files; | ||
|
||
try { | ||
[fields, files] = await form.parse(req); | ||
} catch (err) { | ||
res.writeHead(err.httpCode || 400, { 'Content-Type': 'text/plain' }); | ||
res.end(String(err)); | ||
|
||
return; | ||
} | ||
|
||
const compressionType = Array.isArray(fields.compressionType) | ||
? fields.compressionType[0] | ||
: fields.compressionType; | ||
|
||
const filePath = Array.isArray(files.file) | ||
? files.file[0].filepath | ||
: files.file; | ||
const fileName = Array.isArray(files.file) | ||
? files.file[0].originalFilename | ||
: files.file; | ||
|
||
if (!filePath || !fileName || !compressionType) { | ||
res.statusCode = 400; | ||
res.end(); | ||
|
||
return; | ||
} | ||
|
||
if (!fs.existsSync(filePath)) { | ||
res.statusCode = 404; | ||
res.end('File not found!'); | ||
} | ||
mrsvolodya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let compressionData, extension; | ||
|
||
switch (compressionType) { | ||
case 'gzip': | ||
compressionData = createGzip(); | ||
res.setHeader('Content-Encoding', 'gz'); | ||
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 content encoding header for gzip should be 'gzip' instead of 'gz'. This is a critical issue as it may cause clients to misinterpret the encoding. 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. |
||
extension = 'gzip'; | ||
break; | ||
case 'deflate': | ||
compressionData = createDeflate(); | ||
res.setHeader('Content-Encoding', '.dfl'); | ||
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 content encoding headers are incorrect. For gzip, the correct header should be 'gzip' instead of 'gz'. Similarly, for deflate, the correct header should be 'deflate' instead of '.dfl'. Please update these headers to reflect the correct content encoding values. 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. For gzip, I should use 'gz' because it doesn't pass the test.!!!! Similarly, for deflate, I should use '.dfl' 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 content encoding header for deflate should be 'deflate' instead of '.dfl'. This is a critical issue as it may cause clients to misinterpret the encoding. |
||
extension = 'deflate'; | ||
break; | ||
case 'br': | ||
compressionData = createBrotliCompress(); | ||
res.setHeader('Content-Encoding', '.br'); | ||
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 content encoding header for Brotli should be 'br' instead of '.br'. This is a critical issue as it may cause clients to misinterpret the encoding. |
||
extension = 'br'; | ||
break; | ||
default: | ||
res.statusCode = 400; | ||
res.end(); | ||
|
||
return; | ||
} | ||
|
||
const fileStream = fs.createReadStream(filePath); | ||
|
||
res.statusCode = 200; | ||
|
||
res.writeHead(200, { | ||
'Content-Type': 'text/html', | ||
'Content-Disposition': `attachment; filename=${fileName}.${extension}`, | ||
}); | ||
console.log(fields, files, req.method, compressionType); | ||
|
||
fileStream | ||
.pipe(compressionData) | ||
.pipe(res) | ||
.on('error', (err) => { | ||
res.statusCode = 500; | ||
res.end(`Stream error: ${err.message}`); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
if (req.url !== '/') { | ||
res.statusCode = 404; | ||
res.end(); | ||
} | ||
}); | ||
|
||
return server; | ||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!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"> | ||
<input name="file" type="file" /> | ||
<select name="compressionType"> | ||
<option value="gzip">gzip</option> | ||
<option value="deflate">deflate</option> | ||
<option value="br">br</option> | ||
</select> | ||
<input type="submit" /> | ||
</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.
There is a potential issue with the way
filePath
andfileName
are assigned. Iffiles.file
is not an array, the code assigns the entire file object tofilePath
andfileName
, which is likely incorrect. You should access thefilepath
andoriginalFilename
properties directly in the else case.