diff --git a/node_modules/.yarn-integrity b/node_modules/.yarn-integrity new file mode 100644 index 0000000000..b9b7edf672 --- /dev/null +++ b/node_modules/.yarn-integrity @@ -0,0 +1,10 @@ +{ + "systemParams": "darwin-x64-115", + "modulesFolders": [], + "flags": [], + "linkedModules": [], + "topLevelPatterns": [], + "lockfileEntries": {}, + "files": [], + "artifacts": {} +} \ No newline at end of file diff --git a/src/cli/args.js b/src/cli/args.js index 8283f7f7aa..a36a4f40fe 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,17 @@ const parseArgs = () => { - // Write your code here + // Skip the first two elements (node and script path) + const args = process.argv.slice(2); + + const parsedArgs = []; + + // why i += 2 -> [ '--some-arg', 'value1', '--other', '1337', '--arg2', '42' ] + for (let i = 0; i < args.length; i += 2) { + const propName = args[i].replace("--", ""); + const value = args[i + 1]; + parsedArgs.push(`${propName} is ${value}`); + } + + console.log(parsedArgs.join(", ")); }; -parseArgs(); \ No newline at end of file +parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index fe4aa4a8df..b3fe5e62b4 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,12 @@ const parseEnv = () => { - // Write your code here + const envVars = process.env; + console.log("Object.keys(envVars)", Object.keys(envVars)); + const rssVars = Object.keys(envVars) + .filter((key) => key.startsWith("RSS_")) + .map((key) => `${key}=${envVars[key]}`) + .join("; "); + + console.log(rssVars); }; -parseEnv(); \ No newline at end of file +parseEnv(); diff --git a/src/cp/cp.js b/src/cp/cp.js index 4a87b98c55..7e0c668488 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,21 @@ +import { spawn } from "child_process"; + const spawnChildProcess = async (args) => { - // Write your code here + const child = spawn("node", ["./src/cp/files/script.js", ...args]); + + process.stdin.pipe(child.stdin); + + child.stdout.pipe(process.stdout); + + child.on("error", (error) => { + console.error(`Error: ${error.message}`); + }); + + child.on("exit", (code) => { + console.log(`Child process exited with code ${code}`); + }); }; +const args = process.argv.slice(2); // Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess(args); diff --git a/src/cp/files/script.js b/src/cp/files/script.js index 44a0622fbc..4d4494a7a2 100644 --- a/src/cp/files/script.js +++ b/src/cp/files/script.js @@ -4,9 +4,9 @@ console.log(`Total number of arguments is ${args.length}`); console.log(`Arguments: ${JSON.stringify(args)}`); const echoInput = (chunk) => { - const chunkStringified = chunk.toString(); - if (chunkStringified.includes('CLOSE')) process.exit(0); - process.stdout.write(`Received from master process: ${chunk.toString()}\n`) + const chunkStringified = chunk.toString(); + if (chunkStringified.includes("CLOSE")) process.exit(0); + process.stdout.write(`Received from master process: ${chunk.toString()}\n`); }; -process.stdin.on('data', echoInput); +process.stdin.on("data", echoInput); diff --git a/src/fs/copy.js b/src/fs/copy.js index bd17fe3991..d95db0f6d0 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,30 @@ +import { promises as fs } from "fs"; +import path from "path"; + const copy = async () => { - // Write your code here + const folder = "src/fs/"; + const sourceDir = path.join(folder, "files"); + const targetDir = path.join(folder, "files_copy"); + + try { + await fs.access(sourceDir); + + try { + await fs.access(targetDir); + throw new Error("FS operation failed: Target directory already exists"); + } catch {} + + await fs.mkdir(targetDir); + + const files = await fs.readdir(sourceDir); + for (const file of files) { + const srcFile = path.join(sourceDir, file); + const destFile = path.join(targetDir, file); + await fs.copyFile(srcFile, destFile); + } + } catch (error) { + throw new Error(`FS operation failed: ${error}`); + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 8d18cf9fc2..58f9079bb7 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,35 @@ +import { promises as fs } from "fs"; +import path from "path"; + const create = async () => { - // Write your code here + const folder = "src/fs/files"; + const pathToFolder = path.join(folder, "fresh.txt"); + const text = "I am fresh and young"; + + let folderExists = true; + try { + await fs.access(folder, fs.constants.R_OK); + } catch { + folderExists = false; + } + + if (!folderExists) { + await fs.mkdir(folder); + } + + let fileExists = true; + try { + await fs.access(pathToFolder); + } catch { + fileExists = false; + } + + if (!fileExists) { + await fs.writeFile(pathToFolder, text, "utf-8"); + console.log("File created successfully."); + } else { + console.error("FS operation failed: File already exists"); + } }; -await create(); \ No newline at end of file +await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index 4718dbc4c5..a27ca3cfd0 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,17 @@ -const remove = async () => { - // Write your code here +import { promises as fs } from "fs"; +import path from "path"; + +const removeFile = async () => { + const folder = "src/fs/files/"; + const fileToRemove = path.join(folder, "fileToRemove.txt"); + + try { + await fs.access(fileToRemove); + + await fs.unlink(fileToRemove); + } catch (error) { + throw new Error(`FS operation failed: ${error.message}`); + } }; -await remove(); \ No newline at end of file +await removeFile(); diff --git a/src/fs/list.js b/src/fs/list.js index c0a83dea15..5f9e660282 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,19 @@ -const list = async () => { - // Write your code here +import { promises as fs } from "fs"; +import path from "path"; + +const listFiles = async () => { + const folder = "src/fs/"; + + const filesFolder = path.join(folder, "files"); + + try { + await fs.access(filesFolder); + + const filenames = await fs.readdir(filesFolder); + console.log(filenames); + } catch (error) { + throw new Error(`FS operation failed: ${error.message}`); + } }; -await list(); \ No newline at end of file +await listFiles(); diff --git a/src/fs/read.js b/src/fs/read.js index 52c78cc6ee..4e2cd51ae9 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,18 @@ -const read = async () => { - // Write your code here +import { promises as fs } from "fs"; +import path from "path"; + +const readFile = async () => { + const folder = "src/fs/files"; + const fileToRead = path.join(folder, "fileToRead.txt"); + + try { + await fs.access(fileToRead); + + const content = await fs.readFile(fileToRead, "utf8"); + console.log(content); + } catch (error) { + throw new Error(`FS operation failed: ${error.message}`); + } }; -await read(); \ No newline at end of file +await readFile(); diff --git a/src/fs/rename.js b/src/fs/rename.js index 2bb99ecdb5..ddcbe5b896 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,23 @@ -const rename = async () => { - // Write your code here +import { promises as fs } from "fs"; +import path from "path"; + +const renameFile = async () => { + const folder = "src/fs/files/"; + const sourceFile = path.join(folder, "wrongFilename.txt"); + const targetFile = path.join(folder, "properFilename.md"); + + try { + await fs.access(sourceFile); + + try { + await fs.access(targetFile); + throw new Error("FS operation failed: properFilename.md already exists"); + } catch {} + + await fs.rename(sourceFile, targetFile); + } catch (error) { + throw new Error(`FS operation failed: ${error.message}`); + } }; -await rename(); \ No newline at end of file +await renameFile(); diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 450f8f72e2..cd3ba59f29 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,22 @@ +import crypto from "crypto"; +import fs from "fs"; + const calculateHash = async () => { - // Write your code here + const hash = crypto.createHash("sha256"); + + try { + const hashDigest = hash.digest("hex"); + + await fs.promises.writeFile( + "src/hash/files/fileToCalculateHashFor.txt", + hashDigest, + "utf-8" + ); + + console.log(`Hash calculated successfully: ${hashDigest}`); + } catch (error) { + console.error("Error calculating hash:", error); + } }; -await calculateHash(); \ No newline at end of file +await calculateHash(); diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.cjs deleted file mode 100644 index 8b7be2a48b..0000000000 --- a/src/modules/cjsToEsm.cjs +++ /dev/null @@ -1,40 +0,0 @@ -const path = require('path'); -const { release, version } = require('os'); -const { createServer: createServerHttp } = require('http'); -require('./files/c'); - -const random = Math.random(); - -let unknownObject; - -if (random > 0.5) { - unknownObject = require('./files/a.json'); -} else { - unknownObject = 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..f075f10da9 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,42 @@ +import path from "path"; +import { release, version } from "os"; +import { createServer as createServerHttp } from "http"; +import { fileURLToPath } from "url"; + +import "./files/c.js"; + +const random = Math.random(); + +let unknownObject; + +if (random > 0.5) { + unknownObject = import("./files/a.json", { assert: { type: "json" } }); +} else { + unknownObject = import("./files/b.json", { assert: { type: "json" } }); +} + +console.log(`Release ${release()}`); +console.log(`Version ${version()}`); +console.log(`Path segment separator is "${path.sep}"`); + +console.log(`Path to current file is ${fileURLToPath(import.meta.url)}`); +console.log(`Path to current directory is ${fileURLToPath(import.meta.url)}`); + +const myServer = createServerHttp((_, res) => { + res.end("Request accepted"); +}); + +const PORT = 3000; + +console.log(unknownObject); + +unknownObject.then((unknownObjectModule) => { + console.log(unknownObjectModule.default); + + 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 52c78cc6ee..463165d474 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,12 @@ +import fs from "fs"; + const read = async () => { - // Write your code here + const readableStream = fs.createReadStream( + "src/streams/files/fileToRead.txt", + "utf8" + ); + + readableStream.pipe(process.stdout); }; -await read(); \ No newline at end of file +await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 315fc6597f..73e5c6e6e5 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,21 @@ +import { Transform, pipeline } from "stream"; +import { promisify } from "util"; + +const pipelineAsync = promisify(pipeline); + +const reverseTextTransform = new Transform({ + transform(chunk, _, cl) { + const reversed = chunk.toString().split("").reverse().join(""); + cl(null, reversed); + }, +}); + const transform = async () => { - // Write your code here + try { + await pipelineAsync(process.stdin, reverseTextTransform, process.stdout); + } catch (error) { + console.error("An error occurred:", error); + } }; -await transform(); \ No newline at end of file +transform(); diff --git a/src/streams/write.js b/src/streams/write.js index fc917160a2..5e918e88f3 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,20 @@ +import fs from "fs"; + const write = async () => { - // Write your code here + const writableStream = fs.createWriteStream( + "src/streams/files/fileToWrite.txt", + "utf8" + ); + + process.stdin.pipe(writableStream); + + process.stdin.on("end", () => { + writableStream.end(); + }); + + console.log( + "Type something and press Enter to write to the file. Press Ctrl+D to end." + ); }; -await write(); \ No newline at end of file +await write(); diff --git a/src/wt/main.js b/src/wt/main.js index 37d80484ec..fa22dfcda2 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,43 @@ +import { Worker } from "worker_threads"; +import os from "os"; + const performCalculations = async () => { - // Write your code here + const numCores = os.cpus().length; + const results = []; + const promises = []; + + for (let i = 0; i < numCores; i++) { + const promise = new Promise((resolve, reject) => { + const worker = new Worker("./src/wt/worker.js"); + const valueToSend = 10 + i; + + worker.postMessage(valueToSend); + + worker.on("message", (result) => { + results.push({ status: "resolved", data: result }); + resolve(); + worker.terminate(); + }); + + worker.on("error", (error) => { + console.error(`Worker error: ${error}`); + results.push({ status: "error", data: null }); + reject(error); + worker.terminate(); + }); + + worker.on("exit", (code) => { + if (code !== 0) { + console.error(`Worker stopped with exit code: ${code}`); + } + }); + }); + + promises.push(promise); + } + + await Promise.all(promises); + console.log(results); }; -await performCalculations(); \ No newline at end of file +await performCalculations(); diff --git a/src/wt/worker.js b/src/wt/worker.js index 441b2154f8..121bf7c30c 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 "worker_threads"; -const sendResult = () => { - // This function sends result of nthFibonacci computations to main thread +const nthFibonacci = (n) => + n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); + +const sendResult = (result) => { + parentPort.postMessage(result); }; -sendResult(); \ No newline at end of file +parentPort.on("message", (n) => { + const result = nthFibonacci(n); + sendResult(result); +}); diff --git a/src/zip/compress.js b/src/zip/compress.js index bb328f43c6..4548a84687 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,13 @@ +import fs from "fs"; +import zlib from "zlib"; + const compress = async () => { - // Write your code here + const readableStream = fs.createReadStream( + "src/zip/files/fileToCompress.txt" + ); + const writableStream = fs.createWriteStream("src/zip/files/archive.gz"); + const gzip = zlib.createGzip(); + readableStream.pipe(gzip).pipe(writableStream); }; -await compress(); \ No newline at end of file +await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 69f6c345f8..371a940d25 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,13 @@ +import fs from "fs"; +import zlib from "zlib"; + const decompress = async () => { - // Write your code here + const readableStream = fs.createReadStream("src/zip/files/archive.gz"); + const writableStream = fs.createWriteStream( + "src/zip/files/fileToCompress.txt" + ); + const gunzip = zlib.createGunzip(); + readableStream.pipe(gunzip).pipe(writableStream); }; -await decompress(); \ No newline at end of file +await decompress(); diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000000..fb57ccd13a --- /dev/null +++ b/yarn.lock @@ -0,0 +1,4 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + +