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/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(); 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(); 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"]); diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..ddb985360f 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 failed"); + } + } }; await copy(); 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(); 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(); 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(); 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(); 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(); 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(); 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 }; 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(); 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(); 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(); 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(); 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(); 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();