From 557a92eb0fe6c97a04d94fda7cd57419f872d55d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sat, 25 Oct 2025 13:19:19 +0500 Subject: [PATCH 01/14] feat: add copy and create js , add dirname at index.js in fs module --- src/fs/copy.js | 16 +++++++++++++++- src/fs/create.js | 14 +++++++++++++- src/fs/files/fresh.txt | 1 + src/fs/files_copy/dontLookAtMe.txt | 1 + src/fs/files_copy/fileToRead.txt | 7 +++++++ src/fs/files_copy/fileToRemove.txt | 1 + src/fs/files_copy/fresh.txt | 1 + src/fs/files_copy/hello.txt | 1 + src/fs/files_copy/wrongFilename.txt | 3 +++ src/fs/index.js | 2 ++ src/fs/rename.js | 5 +++++ 11 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/fs/files/fresh.txt create mode 100644 src/fs/files_copy/dontLookAtMe.txt create mode 100644 src/fs/files_copy/fileToRead.txt create mode 100644 src/fs/files_copy/fileToRemove.txt create mode 100644 src/fs/files_copy/fresh.txt create mode 100644 src/fs/files_copy/hello.txt create mode 100644 src/fs/files_copy/wrongFilename.txt create mode 100644 src/fs/index.js diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..ff762f5c9a 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,19 @@ +import fs from "fs/promises"; +import path from "path"; +import { cp } from "fs/promises"; +import { __dirname } from "."; + const copy = async () => { - // Write your code here + try { + await fs.mkdir(path.join(__dirname, "files_copy")); + await cp( + path.resolve(__dirname, "files"), + path.resolve(__dirname, "files_copy"), + { recursive: true } + ); + } catch { + throw new Error("fs operation failed"); + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..43d66a7046 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,17 @@ +import fs from "fs/promises"; +import path from "path"; +import { __dirname } from "."; + const create = async () => { - // Write your code here + try { + await fs.writeFile( + path.resolve(__dirname, "files/fresh.txt"), + "I am fresh and young", + { flag: "wx" } + ); + } catch { + throw new Error("FS operation failed"); + } }; await create(); diff --git a/src/fs/files/fresh.txt b/src/fs/files/fresh.txt new file mode 100644 index 0000000000..205d704cb7 --- /dev/null +++ b/src/fs/files/fresh.txt @@ -0,0 +1 @@ +I am fresh and young \ No newline at end of file diff --git a/src/fs/files_copy/dontLookAtMe.txt b/src/fs/files_copy/dontLookAtMe.txt new file mode 100644 index 0000000000..8979bab743 --- /dev/null +++ b/src/fs/files_copy/dontLookAtMe.txt @@ -0,0 +1 @@ +What are you looking at?! \ No newline at end of file diff --git a/src/fs/files_copy/fileToRead.txt b/src/fs/files_copy/fileToRead.txt new file mode 100644 index 0000000000..5d66c332d6 --- /dev/null +++ b/src/fs/files_copy/fileToRead.txt @@ -0,0 +1,7 @@ +My content +should +be +printed +into +console +! \ No newline at end of file diff --git a/src/fs/files_copy/fileToRemove.txt b/src/fs/files_copy/fileToRemove.txt new file mode 100644 index 0000000000..43e64cd45c --- /dev/null +++ b/src/fs/files_copy/fileToRemove.txt @@ -0,0 +1 @@ +How dare you! \ No newline at end of file diff --git a/src/fs/files_copy/fresh.txt b/src/fs/files_copy/fresh.txt new file mode 100644 index 0000000000..205d704cb7 --- /dev/null +++ b/src/fs/files_copy/fresh.txt @@ -0,0 +1 @@ +I am fresh and young \ No newline at end of file diff --git a/src/fs/files_copy/hello.txt b/src/fs/files_copy/hello.txt new file mode 100644 index 0000000000..4e65f7775f --- /dev/null +++ b/src/fs/files_copy/hello.txt @@ -0,0 +1 @@ +Hello Node.js \ No newline at end of file diff --git a/src/fs/files_copy/wrongFilename.txt b/src/fs/files_copy/wrongFilename.txt new file mode 100644 index 0000000000..38cca5db19 --- /dev/null +++ b/src/fs/files_copy/wrongFilename.txt @@ -0,0 +1,3 @@ +# This is a file with a wrong filename + +Hello from **markdown**! \ No newline at end of file diff --git a/src/fs/index.js b/src/fs/index.js new file mode 100644 index 0000000000..6dc95b5bde --- /dev/null +++ b/src/fs/index.js @@ -0,0 +1,2 @@ +export const __filename = fileURLToPath(import.meta.url); +export const __dirname = dirname(__filename); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..62aa35c82a 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,3 +1,8 @@ +import fs from "fs/promises"; +import path from "path"; +import { cp } from "fs/promises"; +import { __dirname } from "."; + const rename = async () => { // Write your code here }; From 2839c1ce5df666fb84a43179fae75b18432f6f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sat, 25 Oct 2025 14:08:24 +0500 Subject: [PATCH 02/14] feat: add rename function --- src/fs/copy.js | 5 ++++- src/fs/create.js | 5 ++++- src/fs/index.js | 2 -- src/fs/rename.js | 28 +++++++++++++++++++++++++--- 4 files changed, 33 insertions(+), 7 deletions(-) delete mode 100644 src/fs/index.js diff --git a/src/fs/copy.js b/src/fs/copy.js index ff762f5c9a..b9de5a177d 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,7 +1,10 @@ import fs from "fs/promises"; import path from "path"; import { cp } from "fs/promises"; -import { __dirname } from "."; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const copy = async () => { try { diff --git a/src/fs/create.js b/src/fs/create.js index 43d66a7046..c2f0bb9423 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,6 +1,9 @@ import fs from "fs/promises"; import path from "path"; -import { __dirname } from "."; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const create = async () => { try { diff --git a/src/fs/index.js b/src/fs/index.js deleted file mode 100644 index 6dc95b5bde..0000000000 --- a/src/fs/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export const __filename = fileURLToPath(import.meta.url); -export const __dirname = dirname(__filename); diff --git a/src/fs/rename.js b/src/fs/rename.js index 62aa35c82a..f641664b97 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,10 +1,32 @@ +import { access } from "fs/promises"; +import { constants } from "fs/promises"; import fs from "fs/promises"; import path from "path"; -import { cp } from "fs/promises"; -import { __dirname } from "."; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const rename = async () => { - // Write your code here + try { + const isTargetExist = await access( + path.resolve(__dirname, "files/properFilename.md"), + constants.F_OK + ) + .then(() => true) + .catch(() => false); + + if (isTargetExist) { + throw new Error(); + } + + await fs.rename( + path.resolve(__dirname, "files/wrongFilename.txt"), + path.resolve(__dirname, "files/properFilename.md") + ); + } catch { + throw new Error("FS operation failed"); + } }; await rename(); From f799cff7ebf84e5257b7c321cb352571bc95a300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sat, 25 Oct 2025 14:13:50 +0500 Subject: [PATCH 03/14] feat: add delete file function --- src/fs/delete.js | 13 ++++++++++++- src/fs/files/fileToRemove.txt | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..eb678a9f34 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,16 @@ +import fs from "fs/promises"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const remove = async () => { - // Write your code here + try { + await fs.unlink(path.resolve(__dirname, "files/fileToRemove.txt")); + } catch { + throw new Error("FS operation failed"); + } }; await remove(); diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt index 43e64cd45c..19334d05a2 100644 --- a/src/fs/files/fileToRemove.txt +++ b/src/fs/files/fileToRemove.txt @@ -1 +1 @@ -How dare you! \ No newline at end of file +How dare you are!!! \ No newline at end of file From c70dceb49c50eead217fde5bcb74038032252c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sat, 25 Oct 2025 15:57:49 +0500 Subject: [PATCH 04/14] feat: add list function --- src/fs/list.js | 14 +++++++++++++- src/fs/read.js | 4 +--- src/zip/files/fileToCompress.txt | 1 - 3 files changed, 14 insertions(+), 5 deletions(-) delete mode 100644 src/zip/files/fileToCompress.txt diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..d73455b130 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,17 @@ +import path from "path"; +import { fileURLToPath } from "url"; +import fs from "fs/promises"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const list = async () => { - // Write your code here + try { + const files = await fs.readdir(path.resolve(__dirname, "files")); + console.log(files); + } catch { + throw new Error("FS operation failed"); + } }; await list(); diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..8edd6f4946 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,3 @@ -const read = async () => { - // Write your code here -}; +const read = async () => {}; await read(); diff --git a/src/zip/files/fileToCompress.txt b/src/zip/files/fileToCompress.txt deleted file mode 100644 index 4d4efc82fe..0000000000 --- a/src/zip/files/fileToCompress.txt +++ /dev/null @@ -1 +0,0 @@ -Compress me! \ No newline at end of file From 03eca2bfcda2cb41a1e6f71cd9b5a174a89b5b46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sat, 25 Oct 2025 16:01:41 +0500 Subject: [PATCH 05/14] feat: add read function --- src/fs/read.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/fs/read.js b/src/fs/read.js index 8edd6f4946..b74577bfb0 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,3 +1,20 @@ -const read = async () => {}; +import path from "path"; +import { fileURLToPath } from "url"; +import fs from "fs/promises"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const read = async () => { + try { + const fileContent = await fs.readFile( + path.resolve(__dirname, "files/fileToRead.txt"), + { encoding: "utf-8" } + ); + console.log(fileContent); + } catch { + throw new Error("FS operation failed"); + } +}; await read(); From 041851d46895e21a2911578863e5742e180c2e97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sat, 25 Oct 2025 16:17:45 +0500 Subject: [PATCH 06/14] feat: add cli env func --- src/cli/env.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..59dfdaa788 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,7 @@ const parseEnv = () => { - // Write your code here + for (const key in process.env) { + console.log(`RSS_${key}=${process.env[key]}`); + } }; parseEnv(); From f9441bbfc3a1b79c4e1b93e048b8da891e195fef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sat, 25 Oct 2025 16:31:20 +0500 Subject: [PATCH 07/14] feat: finish esm --- src/cli/args.js | 4 +-- src/modules/cjsToEsm.cjs | 34 ------------------------ src/modules/esm.js | 43 +++++++++++++++++++++++++++++++ src/modules/files/{c.cjs => c.js} | 0 4 files changed, 44 insertions(+), 37 deletions(-) delete mode 100644 src/modules/cjsToEsm.cjs create mode 100644 src/modules/esm.js rename src/modules/files/{c.cjs => c.js} (100%) diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..d1b733ac57 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,3 @@ -const parseArgs = () => { - // Write your code here -}; +const parseArgs = () => {}; parseArgs(); 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.js b/src/modules/esm.js new file mode 100644 index 0000000000..e5dda29169 --- /dev/null +++ b/src/modules/esm.js @@ -0,0 +1,43 @@ +import path from "node:path"; +import { release, version } from "node:os"; +import { createServer as createServerHttp } from "node:http"; +import { fileURLToPath } from "url"; +import { createRequire } from "node:module"; + +import a from "./files/a.json" with { type: "json" }; +import b from "./files/b.json" with { type: "json" }; + +const require = createRequire(import.meta.url); +require("./files/c.js"); + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const random = Math.random(); + +const unknownObject = random > 0.5 ? a : b + +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 default { + unknownObject, + myServer, +}; diff --git a/src/modules/files/c.cjs b/src/modules/files/c.js similarity index 100% rename from src/modules/files/c.cjs rename to src/modules/files/c.js From c05fb894973bca677103c87084c0a2dc46ad6736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sat, 25 Oct 2025 19:33:40 +0500 Subject: [PATCH 08/14] feat: add read, write and transform functions --- src/streams/files/fileToRead.txt | 1 - src/streams/files/fileToWrite.txt | 2 ++ src/streams/read.js | 13 ++++++++++++- src/streams/transform.js | 19 ++++++++++++++++++- src/streams/write.js | 21 ++++++++++++++++++++- 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/streams/files/fileToRead.txt b/src/streams/files/fileToRead.txt index c7e8d132a1..e69de29bb2 100644 --- a/src/streams/files/fileToRead.txt +++ b/src/streams/files/fileToRead.txt @@ -1 +0,0 @@ -This file should be read using Streams API \ No newline at end of file diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt index e69de29bb2..5e52cfbbd0 100644 --- a/src/streams/files/fileToWrite.txt +++ b/src/streams/files/fileToWrite.txt @@ -0,0 +1,2 @@ +фывфыв +фывфывфыв diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..535465c6e1 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,16 @@ +import { createReadStream } from "node:fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const read = async () => { - // Write your code here + const readStream = createReadStream( + path.resolve(__dirname, "files/fileToRead.txt") + ); + + readStream.pipe(process.stdout); }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..d2adbce86b 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,22 @@ +import { createReadStream, createWriteStream } from "node:fs"; +import path from "path"; +import { fileURLToPath } from "url"; +import { Transform } from "node:stream"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const transform = async () => { - // Write your code here + const reverseStream = new Transform({ + transform(chunk, encoding, callback) { + const reversedString = chunk.toString().split("").reverse().join(""); + + this.push(reversedString); + callback(); + }, + }); + + process.stdin.pipe(reverseStream).pipe(process.stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..98933ae64d 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,24 @@ +import { createWriteStream } from "node:fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const write = async () => { - // Write your code here + const writeStream = createWriteStream( + path.resolve(__dirname, "files/fileToWrite.txt") + ); + + process.stdin.pipe(writeStream); + + writeStream.on("open", () => + console.log("Запись началась введите что-нибудь в консоль") + ); + + writeStream.on("finish", () => + console.log("Запись завершена , хорошего дня тебе друг!") + ); }; await write(); From e3d7838e704b9bdd7d8f5092f615168e738f2db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sat, 25 Oct 2025 19:55:50 +0500 Subject: [PATCH 09/14] feat: finish cli functions --- package-lock.json | 17 +++++++++++++++++ src/cli/args.js | 9 ++++++++- src/cli/env.js | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000000..34b7366dcf --- /dev/null +++ b/package-lock.json @@ -0,0 +1,17 @@ +{ + "name": "node-nodejs-basics", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "node-nodejs-basics", + "version": "1.0.0", + "license": "ISC", + "engines": { + "node": ">=24.10.0", + "npm": ">=10.9.2" + } + } + } +} diff --git a/src/cli/args.js b/src/cli/args.js index d1b733ac57..447714b0f9 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,3 +1,10 @@ -const parseArgs = () => {}; +const parseArgs = () => { + const args = process.argv.slice(2, process.argv.length); + + args.forEach((arg, index) => { + if (arg.startsWith("--")) + console.log(`${args[index]} is ${args[index + 1]}`); + }); +}; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index 59dfdaa788..3e70631a31 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,6 +1,6 @@ const parseEnv = () => { for (const key in process.env) { - console.log(`RSS_${key}=${process.env[key]}`); + if (key.startsWith("RSS_")) console.log(`${key}=${process.env[key]}`); } }; From abfa4f4cf9cf5d0dbdf6f00ca84cec9946747458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sun, 26 Oct 2025 12:04:11 +0500 Subject: [PATCH 10/14] fix: mistakes fix --- src/fs/files/fileToRemove.txt | 1 - src/fs/files_copy/fileToRemove.txt | 2 +- src/modules/{esm.js => esm.mjs} | 0 src/streams/files/fileToRead.txt | 1 + src/streams/files/fileToWrite.txt | 5 +++-- src/streams/read.js | 6 +++++- 6 files changed, 10 insertions(+), 5 deletions(-) rename src/modules/{esm.js => esm.mjs} (100%) diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt index 19334d05a2..e69de29bb2 100644 --- a/src/fs/files/fileToRemove.txt +++ b/src/fs/files/fileToRemove.txt @@ -1 +0,0 @@ -How dare you are!!! \ No newline at end of file diff --git a/src/fs/files_copy/fileToRemove.txt b/src/fs/files_copy/fileToRemove.txt index 43e64cd45c..19334d05a2 100644 --- a/src/fs/files_copy/fileToRemove.txt +++ b/src/fs/files_copy/fileToRemove.txt @@ -1 +1 @@ -How dare you! \ No newline at end of file +How dare you are!!! \ No newline at end of file diff --git a/src/modules/esm.js b/src/modules/esm.mjs similarity index 100% rename from src/modules/esm.js rename to src/modules/esm.mjs diff --git a/src/streams/files/fileToRead.txt b/src/streams/files/fileToRead.txt index e69de29bb2..d7a894908e 100644 --- a/src/streams/files/fileToRead.txt +++ b/src/streams/files/fileToRead.txt @@ -0,0 +1 @@ +вфывфывфы \ No newline at end of file diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt index 5e52cfbbd0..7840f9875b 100644 --- a/src/streams/files/fileToWrite.txt +++ b/src/streams/files/fileToWrite.txt @@ -1,2 +1,3 @@ -фывфыв -фывфывфыв +dfgdfg +fdgdfg +dfgdf diff --git a/src/streams/read.js b/src/streams/read.js index 535465c6e1..d3322fc07c 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -10,7 +10,11 @@ const read = async () => { path.resolve(__dirname, "files/fileToRead.txt") ); - readStream.pipe(process.stdout); + readStream.pipe(process.stdout, { end: false }); + + readStream.on("end", () => { + console.log(); + }); }; await read(); From 7e440284fc75231b89c25b95a50c2b232bef3bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sun, 26 Oct 2025 12:20:19 +0500 Subject: [PATCH 11/14] feat: add zip and unzip functions --- .DS_Store | Bin 0 -> 6148 bytes src/.DS_Store | Bin 0 -> 8196 bytes src/zip/.DS_Store | Bin 0 -> 6148 bytes src/zip/compress.js | 19 ++++++++++++++++++- src/zip/decompress.js | 17 ++++++++++++++++- src/zip/files/fileToCompress.txt | 1 + 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .DS_Store create mode 100644 src/.DS_Store create mode 100644 src/zip/.DS_Store create mode 100644 src/zip/files/fileToCompress.txt diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f43de5d8afa5aeba94977edd1916bcef80e57938 GIT binary patch literal 6148 zcmeHK&rcIU6n;~xZTTTvXcaLg4ZWCvSP@N#G1P+5#ux}CSOl!w-Jz^(XPVtD6$nYs zdeXapfLBi@UOam7>dp8k=+%?H*_j60!i^Y1_9ZjldvD%*GxKeBW)}cRu5Mlj&;THc zO<=eeyCX!5i`FF}<%u8?zlIJKpl}sz;9b#yBVZt4;D2L4yt{Fzfer?2V}JPm3VRZs zgNyGn%iG*2+q{g?;;6`BoBZtyY5@Hm6^_e$-;o!CB|I;5FrkV#iM`oA6B+Bs=)v_k zV01*iIXbG`aBFJSAIde`a=lFED+%?4&zy}!BhkL-Mq!IL3ZCxO-JI^M32T{iyWGnWtNfI(~v>QVdytY%!BJa=gw=*Uv4;foLomD`_M9`%^PFCsNay%)vxz ze`YF!`i)x$2eD`}eeL#AY1^(c{t{P?Lh$_bWk6vIUZJA)Y#BRhH{?reH{Ug_wVzmT zU;n_lK`lNs98biP$>jM9BNs<6jh3|GvTkfw9I?(hZgVGR*XdftvCKkEH!M~udh~@? zu=Z$48|nyMqUO^>*Ya)+V-klBzubL?Qs&oN*xYofSNuy~Eop zUc*~>2Or@xe1jkGi$sV<#>hCCCU?jzc|h{y2{FhPv4|rJgj$dIa{H9TMO!`Mor1$` z&MN3>MYji^sHLWe_MK?epjf~_z`$u>K%5ULYyz_in-cw_1G^BKVmvHR2^DQwTM2@!EkiGB)p>N@rn gaTSkX^99FJLP5+bY)V8AO7J1z(-3q41OJqPKbx}sX8-^I literal 0 HcmV?d00001 diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ea50686741cbfd3e9594ed4ccde34536d3231bbd GIT binary patch literal 8196 zcmeHM%}*0S6n|5yZAE1Hs8)NF$O{j76I$Fowgg6oo07S1wzuZ zp7gGFubxc2c=Y1coAFQ3t0#T)v3#`U;KfLsm(2X$%>3T$%x||lGg|;4*+%&qKmq`w zm^d1}m~}DU&QphlR1J?K7!Ryev{?~LMh90{Xa%$aS^=$qRzNH8ZzzCkHn->z>%MH& zxmG|c@Lwvx-w#Pl95V`A62+?nGjRk!3}dx0^u^{MG07_sGYVT0u?CIEP(&F@)DeTI zvjdUg*q%}RmP8p2L|hsBQCBAFghJHOgCqqVNJgU0wE|j!P6hbd-vcGE!J4%Xo!_(A za^;sW5f(yLy^Zs(gHr+TFo#@fjR(fn*;Ohfo-t5&bSM!*!1e^+sFH2-L^bGo&G{XJ>e54 zBhg5-H@cRqv1ZOU{f3t{-4$M1WXvuGzFAzdXtQ7pPcz3iEr+@yz$#OutUj`wGHYg8 z!zp_rHYXdRv1qJdBzAVjCvS`;C(`NNvESHbCy>dg>N$=3K{SP8Dkcc0k)h-jKw^F{7|0QN&=z ze}=qWum4;KZCZh2q(F}xYl80q?jHU9{}|0k4^=Cm75GyHNO&eU!?(X;RqqnbSNSBS sr$c_j>GT)U z(;MmQABYY_2C1RDr0sr(9PnSMVkdJvA`VPv>fV+0IN*VW%IG+ zlv&eY4X5mh*qoe*#-g!;Hhyq0GdDY>&!*FdQ~JT&Od9o@w+;_u(PZlS?UllgU31t= zP75A|coqs7QW(H1RND4#m3GyR$md{3-!v`QQ*1DPZs`25HZnRkk{C%QlNT { - // Write your code here + const gzip = zlib.createGzip(); + const sourceFile = createReadStream( + path.join(__dirname, "files/fileToCompress.txt") + ); + const resultFile = createWriteStream( + path.join(__dirname, "files/archive.gz") + ); + + await pipeline(sourceFile, gzip, resultFile); }; await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..ec3674f76d 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,20 @@ +import { createReadStream, createWriteStream } from "node:fs"; +import zlib from "node:zlib"; +import path from "node:path"; +import { fileURLToPath } from "url"; +import { pipeline } from "node:stream/promises"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const decompress = async () => { - // Write your code here + const unzip = zlib.createUnzip(); + const sourceFile = createReadStream(path.join(__dirname, "files/archive.gz")); + const resultFile = createWriteStream( + path.join(__dirname, "files/fileToCompress.txt") + ); + + await pipeline(sourceFile, unzip, resultFile); }; await decompress(); diff --git a/src/zip/files/fileToCompress.txt b/src/zip/files/fileToCompress.txt new file mode 100644 index 0000000000..4d4efc82fe --- /dev/null +++ b/src/zip/files/fileToCompress.txt @@ -0,0 +1 @@ +Compress me! \ No newline at end of file From 40a862efd6f823f46d18f2978430979894d3b7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sun, 26 Oct 2025 12:27:02 +0500 Subject: [PATCH 12/14] feat: finish hash --- 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..813966f34d 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,24 @@ +import crypto from "node:crypto"; + +import path from "path"; +import { cp } from "fs/promises"; +import { fileURLToPath } from "url"; +import { createReadStream } from "node:fs"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const calculateHash = async () => { - // Write your code here + const hash = crypto.createHash("sha256"); + const stream = createReadStream( + path.join(__dirname, "files/fileToCalculateHashFor.txt") + ); + + stream.on("data", (chunk) => { + hash.update(chunk); + }); + + stream.on("end", () => console.log(hash.digest("hex"))); }; await calculateHash(); From 65e6b84807237481c38007bcdf91824c61bc7a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sun, 26 Oct 2025 13:14:09 +0500 Subject: [PATCH 13/14] feat: finish workers --- src/wt/main.js | 37 ++++++++++++++++++++++++++++++++++++- src/wt/worker.js | 9 +++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/wt/main.js b/src/wt/main.js index e2ef054d41..389b209cbb 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,40 @@ +import { Worker } from "node:worker_threads"; +import path from "path"; +import { fileURLToPath } from "url"; +import os from "node:os"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const performCalculations = async () => { - // Write your code here + const START_FROM = 10; + + const cpus = os.cpus(); + + const promises = cpus.map((_, index) => { + const worker = new Worker(path.join(__dirname, "worker.js")); + worker.postMessage(START_FROM + index); + return new Promise((resolve, reject) => { + worker.on("message", (msg) => { + resolve({ + status: "resolved", + data: msg, + }); + worker.terminate(); + }); + + worker.on("error", () => { + resolve({ + status: "error", + data: null, + }); + + worker.terminate(); + }); + }); + }); + const result = await Promise.all(promises); + console.log(result); }; await performCalculations(); diff --git a/src/wt/worker.js b/src/wt/worker.js index 405595394d..382e2be956 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,13 @@ +import { parentPort } from "node: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", (n) => { + parentPort.postMessage(nthFibonacci(n)); + }); }; sendResult(); From 9d9291f49f70ffbc2ccbb109d2ed4298b604a2a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=A8=D1=83?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=BD?= Date: Sun, 26 Oct 2025 13:46:02 +0500 Subject: [PATCH 14/14] feat: finish child process function --- src/cp/cp.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/cp/cp.js b/src/cp/cp.js index 72c6addc9c..daac6be6f8 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,21 @@ +import { spawn } from "node:child_process"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const spawnChildProcess = async (args) => { - // Write your code here + const child = spawn( + "node", + [path.join(__dirname, "files/script.js"), ...args], + { + stdio: ["pipe", "pipe", "inherit"], + } + ); + + process.stdin.pipe(child.stdin); + child.stdout.pipe(process.stdout); }; // Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +await spawnChildProcess(["arg1", "arg2"]);