-
Notifications
You must be signed in to change notification settings - Fork 304
solution #230
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 #230
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 |
|---|---|---|
| @@ -1,8 +1,109 @@ | ||
| 'use strict'; | ||
|
|
||
| /* eslint-disable no-console */ | ||
|
|
||
| const { Server } = require('node:http'); | ||
| const fs = require('node:fs'); | ||
| const zlib = require('node:zlib'); | ||
|
|
||
| const { formidable } = require('formidable'); | ||
| const { pipeline } = require('node:stream'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = new Server(); | ||
|
|
||
| server.on('request', async (req, res) => { | ||
| const path = req.url; | ||
| const method = req.method; | ||
|
|
||
| if (method === 'GET' && path === '/') { | ||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'text/html'); | ||
|
|
||
| const file = fs.createReadStream('public/index.html'); | ||
|
|
||
| file.on('error', () => { | ||
| res.statusCode = 404; | ||
| res.end('No such file'); | ||
| }); | ||
|
|
||
| file.on('close', () => { | ||
| console.log('connection close'); | ||
| file.destroy(); | ||
| }); | ||
| file.pipe(res); | ||
|
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 responds with 400 for GET /compress as required, and 404 for other endpoints — good. However, ensure |
||
| } else if (method === 'POST' && path === '/compress') { | ||
| const form = formidable({}); | ||
|
|
||
| form.parse(req, (err, fields, files) => { | ||
| if (err) { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!files.file?.[0] || !fields.compressionType?.[0]) { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const uploadedFilePath = files.file[0].filepath; | ||
| const readStream = fs.createReadStream(uploadedFilePath); | ||
| const compressionType = fields.compressionType[0]; | ||
| const fileName = files.file[0].originalFilename; | ||
|
Comment on lines
+51
to
+61
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. Form parsing uses
Comment on lines
+51
to
+61
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. Using array indexing for |
||
|
|
||
| let fileCompression; | ||
| let extension = ''; | ||
|
|
||
| switch (compressionType) { | ||
| case 'gzip': | ||
| fileCompression = zlib.createGzip(); | ||
| extension = '.gz'; | ||
|
|
||
| break; | ||
| case 'deflate': | ||
| fileCompression = zlib.createDeflate(); | ||
| extension = '.dfl'; | ||
|
|
||
| break; | ||
| case 'br': | ||
| fileCompression = zlib.createBrotliCompress(); | ||
| extension = '.br'; | ||
|
|
||
| break; | ||
| default: | ||
| res.statusCode = 400; | ||
| res.end(); | ||
|
|
||
| return; | ||
| } | ||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'application/octet-stream'); | ||
|
|
||
| res.setHeader( | ||
| 'Content-Disposition', | ||
| `attachment; filename=${fileName}${extension}`, | ||
|
Comment on lines
+91
to
+93
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 |
||
| ); | ||
|
Comment on lines
+91
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. You set |
||
|
|
||
| pipeline(readStream, fileCompression, res, (error) => { | ||
| if (error) { | ||
| res.end(); | ||
| } | ||
| }); | ||
| }); | ||
| } else if (method === 'GET' && path === '/compress') { | ||
| res.statusCode = 400; | ||
| res.end(); | ||
| } else { | ||
| res.statusCode = 404; | ||
| res.end(); | ||
| } | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| 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.
When serving the index HTML you create a read stream and pipe it to the response, but you never set an explicit error handler on the response stream for pipeline errors. Consider using
pipelinefor consistency (reads + piping) to ensure stream errors close the response properly.