From f554a658cbf9e80f2dba14042d04e1d444f08f60 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Sun, 26 Oct 2025 20:35:40 +0300 Subject: [PATCH 01/18] feat: add implementation of create.js --- .gitignore | 1 + Readme.md | 2 +- src/fs/create.js | 18 +++++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..b512c09d47 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/Readme.md b/Readme.md index 611a505f49..229bf69f52 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,3 @@ -# Node.js basics +й# Node.js basics ## !!! Please don't submit Pull Requests to this repository !!! diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..e731e18a70 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,21 @@ +import { writeFile } from "node:fs/promises"; +import { fileURLToPath } from "node:url"; +import { dirname, join } from "path"; + const create = async () => { - // Write your code here + const __dirname = dirname(fileURLToPath(import.meta.url)); + const filesDir = join(__dirname, "files"); + const targetPath = join(filesDir, "fresh.txt"); + + try { + await writeFile(targetPath, "I am fresh and young", { + flag: "wx", + }); + } catch (error) { + if (error.code === "EEXIST") { + throw new Error("FS operation failed"); + } + } }; await create(); From c1e9787c48538d2f57029e81e71c2c3f5d559dd6 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Sun, 26 Oct 2025 20:35:48 +0300 Subject: [PATCH 02/18] feat: add implementation of copy.js --- src/fs/copy.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..df3047f7e0 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,23 @@ +import { cp } from "fs/promises"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const copy = async () => { - // Write your code here + const srcDir = join(__dirname, "files"); + const destDir = join(__dirname, "files_copy"); + try { + await cp(srcDir, destDir, { + recursive: true, + errorOnExist: true, + force: false, + }); + } catch (error) { + if (error.code === "ENOENT" || error.code === "ERR_FS_CP_EEXIST") { + throw new Error("FS operation faile"); + } + } }; await copy(); From a1d60f943bd845875fa75f8c4b320577ace7121d Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Sun, 26 Oct 2025 21:22:11 +0300 Subject: [PATCH 03/18] feat: add implementation of rename.js --- src/fs/copy.js | 2 +- src/fs/rename.js | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index df3047f7e0..ddb985360f 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -15,7 +15,7 @@ const copy = async () => { }); } catch (error) { if (error.code === "ENOENT" || error.code === "ERR_FS_CP_EEXIST") { - throw new Error("FS operation faile"); + throw new Error("FS operation failed"); } } }; diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..c3916a2018 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,30 @@ +import { access, rename as renameFn } from "fs/promises"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const rename = async () => { - // Write your code here + const filesDir = join(__dirname, "files"); + const srcFilePath = join(filesDir, "wrongFilename.txt"); + const targetFilePath = join(filesDir, "properFilename.md"); + + try { + await access(targetFilePath); + throw new Error("FS operation faile"); + } catch (error) { + if (error.code !== "ENOENT") { + throw error; + } + } + + try { + await renameFn(srcFilePath, targetFilePath); + } catch (error) { + if (error.code === "ENOENT") { + throw new Error("FS operation failed"); + } + } }; await rename(); From 106c9e0574c1f196fce0c908fb0b6a3106c19099 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Sun, 26 Oct 2025 21:22:21 +0300 Subject: [PATCH 04/18] feat: add implementation of delete.js --- src/fs/delete.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..4f35e00d50 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,20 @@ +import { rm } from "fs/promises"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const remove = async () => { - // Write your code here + const filesDir = join(__dirname, "files"); + const targetFilePath = join(filesDir, "fileToRemove.txt"); + + try { + await rm(targetFilePath); + } catch (error) { + if (error.code === "ENOENT") { + throw new Error("FS operation failed"); + } + } }; await remove(); From a1a67a7c22f87058280c0b9b5063c842e595e2f4 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Sun, 26 Oct 2025 21:22:28 +0300 Subject: [PATCH 05/18] feat: add implementation of list.js --- src/fs/list.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..251c99ddf4 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,20 @@ +import { readdir } from "fs/promises"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const list = async () => { - // Write your code here + const filesDir = join(__dirname, "files"); + + try { + const files = await readdir(filesDir); + console.log(files); + } catch (error) { + if (error.code === "ENOENT") { + throw new Error("FS operation failed"); + } + } }; await list(); From f47adf5ecb50750eb997cdf7ee575680951a4008 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Sun, 26 Oct 2025 21:22:35 +0300 Subject: [PATCH 06/18] feat: add implementation of read.js --- src/fs/read.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..82e78cb580 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,21 @@ +import { readFile } from "fs/promises"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const read = async () => { - // Write your code here + const filesDir = join(__dirname, "files"); + const targetFilePath = join(filesDir, "fileToRead.txt"); + + try { + const fileContent = await readFile(targetFilePath, { encoding: "utf-8" }); + console.log(fileContent); + } catch (error) { + if (error.code === "ENOENT") { + throw new Error("FS operation failed"); + } + } }; await read(); From bb922d3c3bfff81f98e99dba9bcf7767bdde6718 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:27:22 +0300 Subject: [PATCH 07/18] feat: add implementation of env.js --- src/cli/env.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..6352998067 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,20 @@ +import { env } from "process"; + const parseEnv = () => { - // Write your code here + const variables = Object.keys(env).filter((value) => + value.startsWith("RSS_") + ); + + let msg = ""; + for (let i = 0; i < variables.length; i++) { + if (i + 1 === variables.length) { + msg += `${variables[i]}=${env[variables[i]]}`; + break; + } + msg += `${variables[i]}=${env[variables[i]]}; `; + } + + console.log(msg); }; parseEnv(); From c902d1f08cd27d3d3397fa6455b7c52aa8923744 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:27:40 +0300 Subject: [PATCH 08/18] feat: add implementation of args.js --- src/cli/args.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..7a51139351 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,24 @@ +import { argv } from "process"; + const parseArgs = () => { - // Write your code here + const args = argv.slice(2); + const argsObj = {}; + + for (let i = 0; i < args.length; i += 2) { + argsObj[args[i].slice(2)] = args[i + 1]; + } + + let msg = ""; + const argsKeys = Object.keys(argsObj); + for (let i = 0; i < argsKeys.length; i++) { + if (i + 1 === argsKeys.length) { + msg += `${argsKeys[i]} is ${argsObj[argsKeys[i]]}`; + break; + } + msg += `${argsKeys[i]} is ${argsObj[argsKeys[i]]}, `; + } + + console.log(msg); }; parseArgs(); From be513117e677205a3ff9473bea894aa6426eec9c Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:28:11 +0300 Subject: [PATCH 09/18] refactor: implementation of esm.mjs --- src/modules/cjsToEsm.cjs | 34 ---------------------------------- src/modules/esm.mjs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 34 deletions(-) delete mode 100644 src/modules/cjsToEsm.cjs create mode 100644 src/modules/esm.mjs diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.cjs deleted file mode 100644 index 089bd2db13..0000000000 --- a/src/modules/cjsToEsm.cjs +++ /dev/null @@ -1,34 +0,0 @@ -const path = require('node:path'); -const { release, version } = require('node:os'); -const { createServer: createServerHttp } = require('node:http'); - -require('./files/c.cjs'); - -const random = Math.random(); - -const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/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'); -}); - -module.exports = { - unknownObject, - myServer, -}; diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs new file mode 100644 index 0000000000..f4d4e2c558 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,39 @@ +import { createServer as createServerHttp } from "node:http"; +import { release, version } from "node:os"; +import { fileURLToPath } from "url"; +import path from "node:path"; +import { dirname } from "path"; + +import "./files/c.cjs"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(fileURLToPath(import.meta.url)); + +const random = Math.random(); + +const jsonPath = random > 0.5 ? "./files/a.json" : "./files/b.json"; +const unknownObject = ( + await import(path.join(__dirname, jsonPath), { with: { type: "json" } }) +).default; + +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 ea4a8c3213ae4534bbc598f2bcd0c691e0b7d182 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:28:23 +0300 Subject: [PATCH 10/18] feat: add implementation of calcHash.js --- src/hash/calcHash.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..c8dfc0c18d 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,24 @@ +import { createHash } from "crypto"; +import { createReadStream } from "fs"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const calculateHash = async () => { - // Write your code here + const filesDir = join(__dirname, "files"); + const srcFilePath = join(filesDir, "fileToCalculateHashFor.txt"); + + const stream = createReadStream(srcFilePath); + + return new Promise((resolve) => { + const hash = createHash("sha256"); + stream.on("data", (data) => hash.update(data, "utf-8")); + stream.on("end", () => { + console.log(hash.digest("hex")); + resolve(); + }); + }); }; await calculateHash(); From 2f6bc367ca1f74fbaddf41b18a0381d3e506f6b8 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:28:39 +0300 Subject: [PATCH 11/18] feat: add implementation of read.js by streams --- src/streams/read.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..047ed4b248 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,23 @@ +import { createReadStream } from "fs"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; +import { stdout } from "process"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const read = async () => { - // Write your code here + const filesDir = join(__dirname, "files"); + const srcFilePath = join(filesDir, "fileToRead.txt"); + + const readStream = createReadStream(srcFilePath); + + return new Promise((resolve) => { + readStream.on("data", (data) => stdout.write(data)); + readStream.on("end", () => { + stdout.write("\n"); + resolve(); + }); + }); }; await read(); From 8bebc6b34e3465cab4fc3aae072842cc0383bbd5 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:28:47 +0300 Subject: [PATCH 12/18] feat: add implementation of write.js by streams --- src/streams/write.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..1479d4932e 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,16 @@ +import { createWriteStream } from "fs"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; +import { stdin } from "process"; +import { pipeline } from "stream/promises"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const write = async () => { - // Write your code here + const filesDir = join(__dirname, "files"); + const targetFilePath = join(filesDir, "fileToWrite.txt"); + + await pipeline(stdin, createWriteStream(targetFilePath)); }; await write(); From 4813133892e08bce935786121bab2247f2da8e08 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:28:55 +0300 Subject: [PATCH 13/18] feat: add implementation of transform.js by streams --- src/streams/transform.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..c3a4fef5b5 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,17 @@ +import { stdin, stdout } from "process"; +import { Transform } from "stream"; +import { pipeline } from "stream/promises"; + const transform = async () => { - // Write your code here + const transformStream = new Transform({ + transform(data, _, cb) { + const result = data.toString().split("").reverse().join(""); + + cb(null, result + "\n"); + }, + }); + + await pipeline(stdin, transformStream, stdout); }; await transform(); From 67b7d2b99720c55080aeb9899157ee17f934b46b Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:29:14 +0300 Subject: [PATCH 14/18] feat: add implementation of compress.js --- src/zip/compress.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..939cc76d2e 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,22 @@ +import { createReadStream, createWriteStream } from "fs"; +import { dirname, join } from "path"; +import { pipeline } from "stream/promises"; +import { fileURLToPath } from "url"; +import { createGzip } from "zlib"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const compress = async () => { - // Write your code here + const filesDir = join(__dirname, "files"); + const srcFilePath = join(filesDir, "fileToCompress.txt"); + const targetFilePath = join(filesDir, "archive.gz"); + const gzip = createGzip(); + + await pipeline( + createReadStream(srcFilePath), + gzip, + createWriteStream(targetFilePath) + ); }; await compress(); From 036dc370634130832fc9efd18ab7094417da5d20 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:29:19 +0300 Subject: [PATCH 15/18] feat: add implementation of decompress.js --- src/zip/decompress.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..694f4e4dc1 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,22 @@ +import { createReadStream, createWriteStream } from "fs"; +import { dirname, join } from "path"; +import { pipeline } from "stream/promises"; +import { fileURLToPath } from "url"; +import { createGunzip } from "zlib"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const decompress = async () => { - // Write your code here + const filesDir = join(__dirname, "files"); + const srcFilePath = join(filesDir, "archive.gz"); + const targetFilePath = join(filesDir, "fileToCompress.txt"); + const gzip = createGunzip(); + + await pipeline( + createReadStream(srcFilePath), + gzip, + createWriteStream(targetFilePath) + ); }; await decompress(); From fd7cba7eec1f64cda847b7a70f5e1c2678741996 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:29:47 +0300 Subject: [PATCH 16/18] feat: add implementation of worker threads --- src/wt/main.js | 33 ++++++++++++++++++++++++++++++++- src/wt/worker.js | 20 ++++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/wt/main.js b/src/wt/main.js index e2ef054d41..7718963c5b 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,36 @@ +import { cpus } from "os"; +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; +import { Worker } from "worker_threads"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const performCalculations = async () => { - // Write your code here + const workerFilePath = join(__dirname, "worker.js"); + + const numberCores = cpus().length; + let number = 10; + const workers = Array.from( + { length: numberCores }, + () => + new Promise((resolve, reject) => { + const worker = new Worker(workerFilePath); + worker.postMessage(number++); + + worker.on("message", (data) => { + resolve(data); + worker.terminate(); + }); + + worker.on("error", (data) => { + reject(data); + worker.terminate(); + }); + }) + ); + + const msg = await Promise.all(workers); + console.log(msg); }; await performCalculations(); diff --git a/src/wt/worker.js b/src/wt/worker.js index 405595394d..0cded11ee6 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,24 @@ +import { parentPort } from "worker_threads"; + // n should be received from main thread -const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); +const nthFibonacci = (n) => + n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); const sendResult = () => { - // This function sends result of nthFibonacci computations to main thread + parentPort.on("message", (data) => { + try { + const result = nthFibonacci(data); + parentPort.postMessage({ + status: "resolved", + data: result, + }); + } catch (error) { + parentPort.postMessage({ + status: "error", + data: null, + }); + } + }); }; sendResult(); From 805fa9c1499f349f3184a5244f9d2b425d1ebf7e Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 00:30:10 +0300 Subject: [PATCH 17/18] feat: add implementation of child processes --- src/cp/cp.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/cp/cp.js b/src/cp/cp.js index 72c6addc9c..c09a1d05b0 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,14 @@ +import { dirname, join } from "path"; +import { fileURLToPath } from "url"; +import { fork } from "node:child_process"; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + const spawnChildProcess = async (args) => { - // Write your code here + const filesDir = join(__dirname, "files"); + const scriptFilePath = join(filesDir, "script.js"); + + fork(scriptFilePath, args); }; -// Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess(["mem, wow, kek"]); From 1618b5eb9c2fddd20665ff15ce2e425f292ec700 Mon Sep 17 00:00:00 2001 From: "nikita.azizov" Date: Mon, 27 Oct 2025 01:02:32 +0300 Subject: [PATCH 18/18] docs: update README.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 229bf69f52..611a505f49 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1,3 @@ -й# Node.js basics +# Node.js basics ## !!! Please don't submit Pull Requests to this repository !!!