From d25b08542d41cc406d34ece6c0d5fbeb55eff512 Mon Sep 17 00:00:00 2001 From: Artem Date: Tue, 12 Mar 2024 21:11:17 +0200 Subject: [PATCH] add task solution --- src/createServer.js | 82 +++++++++++++++++++++++++++++-- src/services/createTransformer.js | 18 +++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/services/createTransformer.js diff --git a/src/createServer.js b/src/createServer.js index 1cf1dda..bdf9d13 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -1,9 +1,85 @@ +/* eslint-disable no-console */ 'use strict'; +const http = require('node:http'); +const fs = require('fs'); +const { createTransformer } = require('./services/createTransformer'); +const formidable = require('formidable'); +const { pipeline } = require('node:stream'); + +const OK = 200; +const BAD_REQUEST = 400; +const NOT_FOUND = 404; +const availableCompressionTypes = ['gzip', 'deflate', 'br']; + function createServer() { - /* Write your code here */ - // Return instance of http.Server class -} + return http.createServer(async(req, res) => { + switch (req.url) { + case '/': + case '/index.html': + res.writeHead(OK, { 'Content-Type': 'text/html' }); + + fs.createReadStream('public/index.html') + .pipe(res); + + break; + + case '/compress': + if (req.method !== 'POST') { + res.writeHead(BAD_REQUEST, { 'Content-Type': 'text/plain' }); + res.end('Wrong form method'); + + return; + } + + const form = new formidable.IncomingForm(); + + form.parse( + req, + (err, { compressionType: compressionTypesArr }, { file: files }) => { + if (err || !compressionTypesArr || !files) { + res.writeHead(BAD_REQUEST, { 'Content-Type': 'text/plain' }); + res.end('Both file and compression type are required'); + + return; + } + + const [compressionType] = compressionTypesArr; + const [file] = files; + + if (!availableCompressionTypes.includes(compressionType)) { + res.writeHead(BAD_REQUEST, { 'Content-Type': 'text/plain' }); + res.end('Unsupported compression type'); + + return; + } + + const transformer = createTransformer(compressionType); + + res.setHeader( + 'Content-Disposition', + `attachment; filename=${file.originalFilename}.${compressionType}` + ); + + pipeline( + fs.createReadStream(file.filepath), + transformer, + res, + (error) => { + if (error) { + console.error('error -', error); + } + }); + }); + + break; + + default: + res.writeHead(NOT_FOUND, { 'Content-Type': 'text/plain' }); + res.end('Not found'); + } + }); +}; module.exports = { createServer, diff --git a/src/services/createTransformer.js b/src/services/createTransformer.js new file mode 100644 index 0000000..5650b9f --- /dev/null +++ b/src/services/createTransformer.js @@ -0,0 +1,18 @@ +'use strict'; + +const zlib = require('zlib'); + +const createTransformer = (transformer) => { + switch (transformer) { + case 'gzip': + return zlib.createGzip(); + case 'br': + return zlib.createBrotliCompress(); + case 'deflate': + return zlib.createDeflate(); + } +}; + +module.exports = { + createTransformer, +};