From 210a7c00842a2d55db658e14c32b76255994ba32 Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Sun, 26 Oct 2025 19:06:15 +0300 Subject: [PATCH 01/12] feat: implement fs:read --- src/fs/read.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..77af560658 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,25 @@ +import { readFile } from 'node:fs/promises'; +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const CONFIG = { + file: 'fileToRead.txt', + dirName: 'files', + errorMessage: 'FS operation failed', +}; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const targetDirPath = join(__dirname, CONFIG.dirName); +const targetFilePath = join(targetDirPath, CONFIG.file); + const read = async () => { - // Write your code here + try { + const content = await readFile(targetFilePath, { encoding: 'utf8' }); + console.log(content); + } catch (error) { + throw new Error(CONFIG.errorMessage); + } }; await read(); From 8eee935f06e50cda0d887ec75a0abb2a18f70f18 Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Sun, 26 Oct 2025 19:13:54 +0300 Subject: [PATCH 02/12] feat: implement fs:create --- src/fs/create.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..19426ec513 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,28 @@ +import { writeFile } from 'node:fs/promises'; +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const CONFIG = { + file: 'fresh.txt', + dirName: 'files', + content: 'I am fresh and young', + errorMessage: 'FS operation failed', +}; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const targetDirPath = join(__dirname, CONFIG.dirName); +const targetFilePath = join(targetDirPath, CONFIG.file); + const create = async () => { - // Write your code here + try { + await writeFile(targetFilePath, CONFIG.content, { + encoding: 'utf8', + flag: 'wx', + }); + } catch (err) { + throw new Error(CONFIG.errorMessage); + } }; await create(); From abc453fd552be2859d1bb36fdd46f70bb9ffa112 Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Sun, 26 Oct 2025 19:22:30 +0300 Subject: [PATCH 03/12] feat: implement fs:list --- src/fs/list.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..43dea043ff 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,23 @@ +import { readdir } from 'node:fs/promises'; +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const CONFIG = { + dirName: 'files', + errorMessage: 'FS operation failed', +}; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const targetDirPath = join(__dirname, CONFIG.dirName); + const list = async () => { - // Write your code here + try { + const files = await readdir(targetDirPath); + console.log(files); + } catch (err) { + throw new Error(CONFIG.errorMessage); + } }; await list(); From 3286737c0e063ec9a68e1f5bb71bab778e50414d Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Sun, 26 Oct 2025 19:31:10 +0300 Subject: [PATCH 04/12] feat: implement fs:delete --- src/fs/delete.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..ff2f1aae98 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,25 @@ +import { unlink } from 'node:fs/promises'; +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const CONFIG = { + file: 'fileToRemove.txt', + dirName: 'files', + errorMessage: 'FS operation failed', +}; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const targetDirPath = join(__dirname, CONFIG.dirName); +const targetFilePath = join(targetDirPath, CONFIG.file); + const remove = async () => { - // Write your code here + try { + await unlink(targetFilePath); + console.log(`successfully deleted ${CONFIG.file}`); + } catch (error) { + throw new Error(CONFIG.errorMessage); + } }; await remove(); From fe1d4f01a8d8e39ef6b094b2cc3b22ef3806bec2 Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Sun, 26 Oct 2025 19:49:00 +0300 Subject: [PATCH 05/12] feat: implement fs:rename --- src/fs/rename.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..5cb740ada1 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,29 @@ +import { rename as renameFile } from 'node:fs/promises'; +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const CONFIG = { + file: 'wrongFilename.txt', + newFile: 'properFilename.txt', + dirName: 'files', + errorMessage: 'FS operation failed', +}; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const targetDirPath = join(__dirname, CONFIG.dirName); +const targetOldFilePath = join(targetDirPath, CONFIG.file); +const targetNewFilePath = join(targetDirPath, CONFIG.newFile); + const rename = async () => { - // Write your code here + try { + await renameFile(targetOldFilePath, targetNewFilePath); + console.log( + `successfully renamed from ${CONFIG.file} to ${CONFIG.newFile}` + ); + } catch (error) { + throw new Error(CONFIG.errorMessage); + } }; await rename(); From e84f7b7eba5b4b21f9983b5bc8b49ed8381a9716 Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Sun, 26 Oct 2025 20:33:30 +0300 Subject: [PATCH 06/12] feat: implement fs:copy --- src/fs/copy.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..415d7c87b8 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,36 @@ +import { readdir, copyFile, mkdir } from 'node:fs/promises'; +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const CONFIG = { + dirName: 'files', + copyDirName: 'files_copy', + errorMessage: 'FS operation failed', +}; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +const targetDirPath = join(__dirname, CONFIG.dirName); +const copyDirPath = join(__dirname, CONFIG.copyDirName); + const copy = async () => { - // Write your code here + try { + await mkdir(copyDirPath); + } catch (error) { + throw new Error(CONFIG.errorMessage); + } + + try { + const files = await readdir(targetDirPath); + + for (const file of files) { + const targetFilePath = join(targetDirPath, file); + const copyFilePath = join(copyDirPath, file); + await copyFile(targetFilePath, copyFilePath); + } + } catch (err) { + throw new Error(CONFIG.errorMessage); + } }; await copy(); From 53281b1ad23cc496fbfe3afa7b02b430fabbd15c Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Sun, 26 Oct 2025 22:23:08 +0300 Subject: [PATCH 07/12] feat: implement modules --- src/modules/esm.mjs | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/modules/esm.mjs diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs new file mode 100644 index 0000000000..10a9a34b55 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,48 @@ +import path from 'node:path'; +import { release, version } from 'node:os'; +import { createServer as createServerHttp } from 'node:http'; +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; +import { readFileSync } from 'node:fs'; + +import './files/c.cjs'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +const random = Math.random(); + +const getFileContent = (filePath) => { + try { + const fullPath = join(__dirname, 'files', filePath); + const fileContent = readFileSync(fullPath); + return JSON.parse(fileContent); + } catch (err) { + console.log(err); + } +}; + +const unknownObject = + random > 0.5 ? getFileContent('a.json') : getFileContent('b.json'); + +console.log(`Release ${release()}`); +console.log(`Version ${version()}`); +console.log(`Path segment separator is "${path.sep}"`); + +console.log(`Path to current file is ${__filename}`); +console.log(`Path to current directory is ${__dirname}`); + +const myServer = createServerHttp((_, res) => { + res.end('Request accepted'); +}); + +const PORT = 3000; + +console.log(unknownObject); + +myServer.listen(PORT, () => { + console.log(`Server is listening on port ${PORT}`); + console.log('To terminate it, use Ctrl+C combination'); +}); + +export { unknownObject, myServer }; From df6584fa0bf01b4b9e47f249f94801452c9adb29 Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Sun, 26 Oct 2025 23:16:31 +0300 Subject: [PATCH 08/12] feat: implement cli --- src/cli/args.js | 15 ++++++++++++++- src/cli/env.js | 9 ++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..709a5590ca 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,18 @@ const parseArgs = () => { - // Write your code here + const resArray = []; + const args = process.argv.slice(2); + + for (let i = 0; i < args.length; i++) { + if (args[i]?.startsWith('--')) { + if (args[i + 1]?.startsWith('--')) { + resArray.push(`${args[i]} undefined`); + continue; + } + resArray.push(`${args[i]} ${args[i + 1]}`); + } + } + + return resArray.join(' '); }; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..31ec552486 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,12 @@ const parseEnv = () => { - // Write your code here + const resArray = []; + + for (const key in process.env) { + if (key.startsWith('RSS_')) { + resArray.push(`${key}=${process.env[key]}`); + } + } + return resArray.join('; '); }; parseEnv(); From e16840334887ac5b3e9968ab87f2817206db5d53 Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Mon, 27 Oct 2025 01:05:44 +0300 Subject: [PATCH 09/12] feat: implement hash --- src/hash/calcHash.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..85aaf102ce 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,25 @@ +import { createHash } from 'node:crypto'; +import { createReadStream } from 'node:fs'; +import { stdout } from 'node:process'; +import { join } from 'node:path'; + +const CONFIG = { + file: 'fileToCalculateHashFor.txt', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFilePath = join(__dirname, CONFIG.dirName, CONFIG.file); + const calculateHash = async () => { - // Write your code here + const hash = createHash('sha256'); + + const input = createReadStream(targetFilePath); + input.pipe(hash); + input.on('end', () => { + const finalHash = hash.digest('hex'); + console.log(finalHash); + }); }; await calculateHash(); From b01729ec413d24c0b2c90c52225901aa29a0a307 Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Mon, 27 Oct 2025 01:14:07 +0300 Subject: [PATCH 10/12] refactor: change the way of getting __dirname --- src/fs/copy.js | 6 ++---- src/fs/create.js | 6 ++---- src/fs/delete.js | 6 ++---- src/fs/list.js | 6 ++---- src/fs/read.js | 6 ++---- src/fs/rename.js | 6 ++---- src/modules/esm.mjs | 7 +++---- 7 files changed, 15 insertions(+), 28 deletions(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index 415d7c87b8..a5394ecb68 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,6 +1,5 @@ import { readdir, copyFile, mkdir } from 'node:fs/promises'; -import { fileURLToPath } from 'url'; -import { dirname, join } from 'path'; +import { join } from 'path'; const CONFIG = { dirName: 'files', @@ -8,8 +7,7 @@ const CONFIG = { errorMessage: 'FS operation failed', }; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); +const __dirname = import.meta.dirname; const targetDirPath = join(__dirname, CONFIG.dirName); const copyDirPath = join(__dirname, CONFIG.copyDirName); diff --git a/src/fs/create.js b/src/fs/create.js index 19426ec513..db4a46efeb 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,6 +1,5 @@ import { writeFile } from 'node:fs/promises'; -import { fileURLToPath } from 'url'; -import { dirname, join } from 'path'; +import { join } from 'path'; const CONFIG = { file: 'fresh.txt', @@ -9,8 +8,7 @@ const CONFIG = { errorMessage: 'FS operation failed', }; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); +const __dirname = import.meta.dirname; const targetDirPath = join(__dirname, CONFIG.dirName); const targetFilePath = join(targetDirPath, CONFIG.file); diff --git a/src/fs/delete.js b/src/fs/delete.js index ff2f1aae98..31b2cb56fa 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,6 +1,5 @@ import { unlink } from 'node:fs/promises'; -import { fileURLToPath } from 'url'; -import { dirname, join } from 'path'; +import { join } from 'path'; const CONFIG = { file: 'fileToRemove.txt', @@ -8,8 +7,7 @@ const CONFIG = { errorMessage: 'FS operation failed', }; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); +const __dirname = import.meta.dirname; const targetDirPath = join(__dirname, CONFIG.dirName); const targetFilePath = join(targetDirPath, CONFIG.file); diff --git a/src/fs/list.js b/src/fs/list.js index 43dea043ff..5c1293ab26 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,14 +1,12 @@ import { readdir } from 'node:fs/promises'; -import { fileURLToPath } from 'url'; -import { dirname, join } from 'path'; +import { join } from 'path'; const CONFIG = { dirName: 'files', errorMessage: 'FS operation failed', }; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); +const __dirname = import.meta.dirname; const targetDirPath = join(__dirname, CONFIG.dirName); const list = async () => { diff --git a/src/fs/read.js b/src/fs/read.js index 77af560658..754c5e514c 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,6 +1,5 @@ import { readFile } from 'node:fs/promises'; -import { fileURLToPath } from 'url'; -import { dirname, join } from 'path'; +import { join } from 'path'; const CONFIG = { file: 'fileToRead.txt', @@ -8,8 +7,7 @@ const CONFIG = { errorMessage: 'FS operation failed', }; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); +const __dirname = import.meta.dirname; const targetDirPath = join(__dirname, CONFIG.dirName); const targetFilePath = join(targetDirPath, CONFIG.file); diff --git a/src/fs/rename.js b/src/fs/rename.js index 5cb740ada1..9bf1df8030 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,6 +1,5 @@ import { rename as renameFile } from 'node:fs/promises'; -import { fileURLToPath } from 'url'; -import { dirname, join } from 'path'; +import { join } from 'path'; const CONFIG = { file: 'wrongFilename.txt', @@ -9,8 +8,7 @@ const CONFIG = { errorMessage: 'FS operation failed', }; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); +const __dirname = import.meta.dirname; const targetDirPath = join(__dirname, CONFIG.dirName); const targetOldFilePath = join(targetDirPath, CONFIG.file); const targetNewFilePath = join(targetDirPath, CONFIG.newFile); diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs index 10a9a34b55..9a953b0b27 100644 --- a/src/modules/esm.mjs +++ b/src/modules/esm.mjs @@ -1,14 +1,13 @@ import path from 'node:path'; import { release, version } from 'node:os'; import { createServer as createServerHttp } from 'node:http'; -import { fileURLToPath } from 'url'; -import { dirname, join } from 'path'; +import { join } from 'path'; import { readFileSync } from 'node:fs'; import './files/c.cjs'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); +const __dirname = import.meta.dirname; +const __filename = import.meta.filename; const random = Math.random(); From cfe487bfe237d515b4126fcc246518e5136d5c56 Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Mon, 27 Oct 2025 02:47:46 +0300 Subject: [PATCH 11/12] feat: implement streams --- src/streams/read.js | 23 ++++++++++++++++++++++- src/streams/transform.js | 12 +++++++++++- src/streams/write.js | 20 +++++++++++++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..614e7c051c 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,26 @@ +import { createReadStream } from 'node:fs'; +import { stdout } from 'node:process'; +import { join } from 'node:path'; + +const CONFIG = { + file: 'fileToRead.txt', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFilePath = join(__dirname, CONFIG.dirName, CONFIG.file); + const read = async () => { - // Write your code here + const input = createReadStream(targetFilePath); + + input.on('error', (err) => { + console.error(err); + }); + input.pipe(stdout, { end: false }); + input.on('end', () => { + stdout.write('\n'); + stdout.end(); + }); }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..8f2f81a682 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,15 @@ +import { Transform } from 'node:stream'; +import { stdout, stdin } from 'node:process'; + const transform = async () => { - // Write your code here + const transformStream = new Transform({ + transform(chunk, encoding, callback) { + const upperCaseData = chunk.toString().split('').reverse().join(''); + callback(null, upperCaseData); + }, + }); + + stdin.pipe(transformStream).pipe(stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..63103fe33b 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,23 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import { stdin } from 'node:process'; +import { join } from 'node:path'; + +const CONFIG = { + file: 'fileToWrite.txt', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFilePath = join(__dirname, CONFIG.dirName, CONFIG.file); + const write = async () => { - // Write your code here + const input = createWriteStream(targetFilePath); + + input.on('error', (err) => { + console.error(err); + }); + + stdin.pipe(input); }; await write(); From 15e6dcc6668caea501f69187bff273eba803f8cf Mon Sep 17 00:00:00 2001 From: Aliaksandr Sekunau Date: Mon, 27 Oct 2025 03:17:59 +0300 Subject: [PATCH 12/12] feat: implement zip --- src/zip/compress.js | 26 +++++++++++++++++++++++++- src/zip/decompress.js | 26 +++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..cae02c40a7 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,29 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import { join } from 'node:path'; +import { createGzip } from 'node:zlib'; +import { pipeline } from 'node:stream/promises'; + +const CONFIG = { + fileFrom: 'fileToCompress.txt', + fileTo: 'archive.gz', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFileFromPath = join(__dirname, CONFIG.dirName, CONFIG.fileFrom); +const targetFileToPath = join(__dirname, CONFIG.dirName, CONFIG.fileTo); + const compress = async () => { - // Write your code here + const gzip = createGzip(); + const sourceReadStream = createReadStream(targetFileFromPath); + const destinationWriteStream = createWriteStream(targetFileToPath); + + try { + await pipeline(sourceReadStream, gzip, destinationWriteStream); + } catch (err) { + console.error(err); + process.exitCode = 1; + } }; await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..07e5a5a908 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,29 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import { join } from 'node:path'; +import { createGunzip } from 'node:zlib'; +import { pipeline } from 'node:stream/promises'; + +const CONFIG = { + fileFrom: 'archive.gz', + fileTo: 'ttt.txt', + dirName: 'files', +}; + +const __dirname = import.meta.dirname; +const targetFileFromPath = join(__dirname, CONFIG.dirName, CONFIG.fileFrom); +const targetFileToPath = join(__dirname, CONFIG.dirName, CONFIG.fileTo); + const decompress = async () => { - // Write your code here + const gzip = createGunzip(); + const sourceReadStream = createReadStream(targetFileFromPath); + const destinationWriteStream = createWriteStream(targetFileToPath); + + try { + await pipeline(sourceReadStream, gzip, destinationWriteStream); + } catch (err) { + console.error(err); + process.exitCode = 1; + } }; await decompress();