diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..7780016740 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,16 @@ const parseArgs = () => { - // Write your code here + const args = process.argv.slice(2); + const parsedArgs = []; + + for (let i = 0; i < args.length; i += 2) { + if (i + 1 < args.length) { + const propName = args[i].replace("--", ""); + const value = args[i + 1]; + parsedArgs.push(`${propName} is ${value}`); + } + } + + console.log(parsedArgs.join(", ")); }; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..f12ad05ddf 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,13 @@ const parseEnv = () => { - // Write your code here + const prefix = "RSS_"; + const entries = Object.entries(process.env) + .filter(([k]) => k.startsWith(prefix)) + .map(([k, v]) => `${k}=${v}`); + if (entries.length === 0) { + console.log(""); + return; + } + console.log(entries.join("; ")); }; parseEnv(); diff --git a/src/cp/cp.js b/src/cp/cp.js index 72c6addc9c..76af8fdeed 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,27 @@ +import { spawn } from "node:child_process"; +import { join } from "node:path"; + const spawnChildProcess = async (args) => { - // Write your code here + const scriptPath = join(process.cwd(), "src", "cp", "files", "script.js"); + + const child = spawn("node", [scriptPath, ...args], { + stdio: ["inherit", "inherit", "inherit", "ipc"], + }); + + // Forward stdin to child process + process.stdin.pipe(child.stdin); + + // Forward child stdout to parent stdout + child.stdout.pipe(process.stdout); + + // Handle child process events + child.on("error", (error) => { + console.error("Child process error:", error); + }); + + child.on("exit", (code) => { + process.exit(code); + }); }; -// Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess(["someArgument1", "someArgument2"]); diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..f3efff28ed 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,33 @@ +import { cp, access } from "node:fs/promises"; +import { join } from "node:path"; + const copy = async () => { - // Write your code here + const sourcePath = join(process.cwd(), "src", "fs", "files"); + const destPath = join(process.cwd(), "src", "fs", "files_copy"); + + try { + // Check if files directory exists + await access(sourcePath); + + // Check if files_copy directory exists + try { + await access(destPath); + throw new Error("FS operation failed"); + } catch (error) { + if (error.code === "ENOENT") { + // do copy + await cp(sourcePath, destPath, { recursive: true }); + } else { + throw new Error("FS operation failed"); + } + } + } catch (error) { + if (error.message === "FS operation failed") { + throw error; + } + + throw new Error("FS operation failed"); + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..c37e0f10ad 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,26 @@ +import { writeFile, access } from "node:fs/promises"; +import { join } from "node:path"; + const create = async () => { - // Write your code here + const filePath = join(process.cwd(), "src", "fs", "files", "fresh.txt"); + + try { + // Check if file exists + await access(filePath); + throw new Error("FS operation failed"); + } catch (error) { + if (error.code === "ENOENT") { + // File doesn't exist, create + try { + await writeFile(filePath, "I am fresh and young"); + } catch (writeError) { + throw new Error("FS operation failed"); + } + } else { + // other + throw new Error("FS operation failed"); + } + } }; await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..aacfd57e8a 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,25 @@ +import { unlink, access } from "node:fs/promises"; +import { join } from "node:path"; + const remove = async () => { - // Write your code here + const filePath = join( + process.cwd(), + "src", + "fs", + "files", + "fileToRemove.txt" + ); + + try { + await access(filePath); + await unlink(filePath); + } catch (error) { + if (error.code === "ENOENT") { + throw new Error("FS operation failed"); + } else { + throw new Error("FS operation failed"); + } + } }; await remove(); diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..f6fc9a8ff0 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,21 @@ +import { readdir, access } from "node:fs/promises"; +import { join } from "node:path"; + const list = async () => { - // Write your code here + const dirPath = join(process.cwd(), "src", "fs", "files"); + + try { + await access(dirPath); + const files = await readdir(dirPath); + console.log(files); + } catch (error) { + if (error.code === "ENOENT") { + // Directory doesn't exist, error + throw new Error("FS operation failed"); + } else { + throw new Error("FS operation failed"); + } + } }; await list(); diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..1a9934d343 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,20 @@ +import { readFile, access } from "node:fs/promises"; +import { join } from "node:path"; + const read = async () => { - // Write your code here + const filePath = join(process.cwd(), "src", "fs", "files", "fileToRead.txt"); + + try { + await access(filePath); + const content = await readFile(filePath, "utf8"); + console.log(content); + } catch (error) { + if (error.code === "ENOENT") { + throw new Error("FS operation failed"); + } else { + throw new Error("FS operation failed"); + } + } }; await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..fd0ccc911a 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,43 @@ +import { rename, access } from "node:fs/promises"; +import { join } from "node:path"; + const rename = async () => { - // Write your code here + const oldPath = join( + process.cwd(), + "src", + "fs", + "files", + "wrongFilename.txt" + ); + const newPath = join( + process.cwd(), + "src", + "fs", + "files", + "properFilename.md" + ); + + try { + // Check if wrongFilename.txt exists + await access(oldPath); + + // Check if properFilename.md file exists + try { + await access(newPath); + throw new Error("FS operation failed"); + } catch (error) { + if (error.code === "ENOENT") { + await rename(oldPath, newPath); + } else { + throw new Error("FS operation failed"); + } + } + } catch (error) { + if (error.message === "FS operation failed") { + throw error; + } + throw new Error("FS operation failed"); + } }; await rename(); diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..3eba04f0d9 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,30 @@ +import { createReadStream } from "node:fs"; +import { createHash } from "node:crypto"; +import { join } from "node:path"; + const calculateHash = async () => { - // Write your code here + const filePath = join( + process.cwd(), + "src", + "hash", + "files", + "fileToCalculateHashFor.txt" + ); + + const hash = createHash("sha256"); + const stream = createReadStream(filePath); + + stream.on("data", (chunk) => { + hash.update(chunk); + }); + + stream.on("end", () => { + console.log(hash.digest("hex")); + }); + + stream.on("error", (error) => { + console.error("Error reading file:", error); + }); }; await calculateHash(); diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs new file mode 100644 index 0000000000..7e906254db --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,38 @@ +import path from "node:path"; +import { release, version } from "node:os"; +import { createServer as createServerHttp } from "node:http"; +import { fileURLToPath } from "node:url"; + +import("./files/c.cjs"); + +const random = Math.random(); + +const unknownObject = + random > 0.5 + ? (await import("./files/a.json", { assert: { type: "json" } })).default + : (await import("./files/b.json", { assert: { type: "json" } })).default; + +console.log(`Release ${release()}`); +console.log(`Version ${version()}`); +console.log(`Path segment separator is "${path.sep}"`); + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +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..8c18509197 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,17 @@ +import { createReadStream } from "node:fs"; +import { join } from "node:path"; + const read = async () => { - // Write your code here + const filePath = join( + process.cwd(), + "src", + "streams", + "files", + "fileToRead.txt" + ); + + const stream = createReadStream(filePath); + stream.pipe(process.stdout); }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..75092b63ce 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,14 @@ +import { Transform } from "node:stream"; + const transform = async () => { - // Write your code here + const reverseTransform = new Transform({ + transform(chunk, encoding, callback) { + const reversed = chunk.toString().split("").reverse().join(""); + callback(null, reversed); + }, + }); + + process.stdin.pipe(reverseTransform).pipe(process.stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..3ddc0186b3 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,17 @@ +import { createWriteStream } from "node:fs"; +import { join } from "node:path"; + const write = async () => { - // Write your code here + const filePath = join( + process.cwd(), + "src", + "streams", + "files", + "fileToWrite.txt" + ); + + const stream = createWriteStream(filePath); + process.stdin.pipe(stream); }; await write(); diff --git a/src/wt/main.js b/src/wt/main.js index e2ef054d41..8706686f4d 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,45 @@ +import { Worker } from "node:worker_threads"; +import { cpus } from "node:os"; +import { join } from "node:path"; + const performCalculations = async () => { - // Write your code here + const numCPUs = cpus().length; + const workers = []; + const results = []; + + // Create workers equal to number of CPU cores + for (let i = 0; i < numCPUs; i++) { + const worker = new Worker(join(process.cwd(), "src", "wt", "worker.js")); + workers.push(worker); + + // Send incremental number starting from 10 + const number = 10 + i; + worker.postMessage(number); + + // Handle worker response + worker.on("message", (result) => { + results[i] = { status: "resolved", data: result }; + worker.terminate(); + }); + + // Handle worker error + worker.on("error", (error) => { + results[i] = { status: "error", data: null }; + worker.terminate(); + }); + } + + // Wait for all workers to complete + await Promise.all( + workers.map( + (worker) => + new Promise((resolve) => { + worker.on("exit", resolve); + }) + ) + ); + + console.log(results); }; await performCalculations(); diff --git a/src/wt/worker.js b/src/wt/worker.js index 405595394d..90026d780a 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,13 @@ -// n should be received from main thread -const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); +import { parentPort } from "node:worker_threads"; + +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) => { + const result = nthFibonacci(data); + parentPort.postMessage(result); + }); }; sendResult(); diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..de0e454a54 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,22 @@ +import { createReadStream, createWriteStream } from "node:fs"; +import { createGzip } from "node:zlib"; +import { join } from "node:path"; + const compress = async () => { - // Write your code here + const inputPath = join( + process.cwd(), + "src", + "zip", + "files", + "fileToCompress.txt" + ); + const outputPath = join(process.cwd(), "src", "zip", "files", "archive.gz"); + + const readStream = createReadStream(inputPath); + const writeStream = createWriteStream(outputPath); + const gzipStream = createGzip(); + + readStream.pipe(gzipStream).pipe(writeStream); }; await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..ef9657cb5a 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,22 @@ +import { createReadStream, createWriteStream } from "node:fs"; +import { createGunzip } from "node:zlib"; +import { join } from "node:path"; + const decompress = async () => { - // Write your code here + const inputPath = join(process.cwd(), "src", "zip", "files", "archive.gz"); + const outputPath = join( + process.cwd(), + "src", + "zip", + "files", + "fileToCompress.txt" + ); + + const readStream = createReadStream(inputPath); + const writeStream = createWriteStream(outputPath); + const gunzipStream = createGunzip(); + + readStream.pipe(gunzipStream).pipe(writeStream); }; await decompress();