Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 127 additions & 1 deletion src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,134 @@
'use strict';

const http = require('http');
const zlib = require('node:zlib');
const { Readable } = require('stream');

function createServer() {
/* Write your code here */
// Return instance of http.Server class
const server = http.createServer((req, res) => {
if (req.url === '/' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('hello world');

return;
}

if (req.url === '/compress') {
if (req.method !== 'POST') {
res.statusCode = 400;

return res.end('trouble');
}

const contentType = req.headers['content-type'] || '';
const boundaryMatch = contentType.match(/boundary=(.*)/);

if (!boundaryMatch) {
res.statusCode = 400;

return res.end();
}

const boundary = boundaryMatch[1];

const chunks = [];

req.on('data', (chunk) => {
chunks.push(chunk);
});

req.on('end', () => {
const buffer = Buffer.concat(chunks);
Comment on lines +37 to +42
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation still buffers the entire request body into memory before processing it. A key requirement is to use streams to process the file upload, which means you should not collect all chunks in an array. The request stream should be processed as data arrives to avoid high memory usage and support large files. This issue was also mentioned in the previous review.

const body = buffer.toString('binary');

const parts = body.split(`--${boundary}`);

let fileContent = null;
let filename = 'file';
let compressionType = null;

for (const part of parts) {
if (part.includes('name="file"')) {
const filenameMatch = part.match(/filename="(.+?)"/);

if (filenameMatch) {
filename = filenameMatch[1];
}

const fileStart = part.indexOf('\r\n\r\n') + 4;
const fileEnd = part.lastIndexOf('\r\n');

fileContent = Buffer.from(
part.substring(fileStart, fileEnd),
'binary',
);
} else if (part.includes('name="compressionType"')) {
const valStart = part.indexOf('\r\n\r\n') + 4;
const parsedType = part
.substring(valStart, part.lastIndexOf('\r\n'))
.trim();

if (['gzip', 'deflate', 'br'].includes(parsedType)) {
compressionType = parsedType;
}
}
}

if (fileContent === null || !filename) {
res.statusCode = 400;

return res.end();
}

if (!compressionType) {
res.statusCode = 400;

return res.end();
}

let compressor;
let ext;

if (compressionType === 'gzip') {
compressor = zlib.createGzip();
ext = '.gzip';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task description, the file extension for gzip compression should be .gz.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task description, the file extension for gzip compression should be .gz, not .gzip.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task description, the file extension for gzip compression should be .gz.

} else if (compressionType === 'deflate') {
compressor = zlib.createDeflate();
ext = '.deflate';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task description specifies the file extension for deflate compression should be .dfl.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task description specifies the file extension for deflate should be .dfl, not .deflate.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task description specifies .dfl as the file extension for deflate compression.

} else if (compressionType === 'br') {
compressor = zlib.createBrotliCompress();
ext = '.br';
}

res.statusCode = 200;

res.setHeader(
'Content-Disposition',
`attachment; filename=${filename}${ext}`,
);

const fileStream = new Readable();

fileStream.push(fileContent);
fileStream.push(null);

fileStream.pipe(compressor).pipe(res);
});

req.on('error', () => {
res.statusCode = 500;
res.end();
});

return;
}

res.statusCode = 404;
res.end('Not Found');
});

return server;
}

module.exports = {
Expand Down
Loading