diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000..f43de5d8af Binary files /dev/null and b/.DS_Store differ 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/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000..ea50686741 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..447714b0f9 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,10 @@ const parseArgs = () => { - // Write your code here + 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 e3616dc8e7..3e70631a31 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) { + if (key.startsWith("RSS_")) console.log(`${key}=${process.env[key]}`); + } }; parseEnv(); 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"]); diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..b9de5a177d 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,22 @@ +import fs from "fs/promises"; +import path from "path"; +import { cp } from "fs/promises"; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + 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..c2f0bb9423 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,20 @@ +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 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/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..e69de29bb2 100644 --- a/src/fs/files/fileToRemove.txt +++ b/src/fs/files/fileToRemove.txt @@ -1 +0,0 @@ -How dare you! \ No newline at end of file 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..19334d05a2 --- /dev/null +++ b/src/fs/files_copy/fileToRemove.txt @@ -0,0 +1 @@ +How dare you are!!! \ 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/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..b74577bfb0 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,20 @@ +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 () => { - // Write your code here + 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(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..f641664b97 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,32 @@ +import { access } from "fs/promises"; +import { constants } from "fs/promises"; +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 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(); 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(); 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..e5dda29169 --- /dev/null +++ b/src/modules/esm.mjs @@ -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 diff --git a/src/streams/files/fileToRead.txt b/src/streams/files/fileToRead.txt index c7e8d132a1..d7a894908e 100644 --- a/src/streams/files/fileToRead.txt +++ b/src/streams/files/fileToRead.txt @@ -1 +1 @@ -This file should be read using Streams API \ No newline at end of file +вфывфывфы \ No newline at end of file diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt index e69de29bb2..7840f9875b 100644 --- a/src/streams/files/fileToWrite.txt +++ b/src/streams/files/fileToWrite.txt @@ -0,0 +1,3 @@ +dfgdfg +fdgdfg +dfgdf diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..d3322fc07c 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,20 @@ +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, { end: false }); + + readStream.on("end", () => { + console.log(); + }); }; 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(); 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(); diff --git a/src/zip/.DS_Store b/src/zip/.DS_Store new file mode 100644 index 0000000000..f0fa18507e Binary files /dev/null and b/src/zip/.DS_Store differ diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..e7563fa2eb 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,22 @@ +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 compress = async () => { - // 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();