diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..952f3bc578 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,14 @@ const parseArgs = () => { - // Write your code here + const args = process.argv.slice(2); + const result = []; + + for (let i = 0; i < args.length; i += 2) { + const prop = args[i].replace('--', ''); + const value = args[i + 1]; + result.push(`${prop} is ${value}`); + } + + console.log(result.join(', ')); }; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..8bfb56c3de 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,11 @@ const parseEnv = () => { - // Write your code here + const prefix = 'RSS_'; + const envVars = Object.entries(process.env) + .filter(([key]) => key.startsWith(prefix)) + .map(([key, value]) => `${key}=${value}`) + .join('; '); + + console.log(envVars); }; parseEnv(); diff --git a/src/cp/cp.js b/src/cp/cp.js index 72c6addc9c..211d4a2752 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,15 @@ +import { spawn } from 'child_process'; +import path from 'path'; +import { fileURLToPath } from 'url'; + const spawnChildProcess = async (args) => { - // Write your code here + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + + const child = spawn('node', [path.join(__dirname, 'files', 'script.js'), ...args]); + + child.stdout.pipe(process.stdout); + process.stdin.pipe(child.stdin); }; -// Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess(['arg1', 'arg2', 'arg3']); diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..04dd3cdf90 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,22 @@ +import { cp } from 'fs/promises'; +import { access } from 'fs/promises'; + const copy = async () => { - // Write your code here + try { + await access('./src/fs/files'); + try { + await access('./src/fs/files_copy'); + throw new Error('FS operation failed'); + } catch (err) { + if (err.code === 'ENOENT') { + await cp('./src/fs/files', './src/fs/files_copy', { recursive: true }); + } else { + throw new Error('FS operation failed'); + } + } + } catch (err) { + throw new Error('FS operation failed'); + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..6913b702ac 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,11 @@ +import { writeFile } from 'fs/promises'; + const create = async () => { - // Write your code here + try { + await writeFile('./src/fs/files/fresh.txt', 'I am fresh and young', { flag: 'wx' }); + } catch (err) { + throw new Error('FS operation failed'); + } }; await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..610970670e 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,13 @@ +import { unlink } from 'fs/promises'; +import { access } from 'fs/promises'; + const remove = async () => { - // Write your code here + try { + await access('./src/fs/files/fileToRemove.txt'); + await unlink('./src/fs/files/fileToRemove.txt'); + } catch (err) { + throw new Error('FS operation failed'); + } }; await remove(); 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/list.js b/src/fs/list.js index 0c0fa21f7e..0875ebeb86 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,14 @@ +import { readdir } from 'fs/promises'; +import { access } from 'fs/promises'; + const list = async () => { - // Write your code here + try { + await access('./src/fs/files'); + const files = await readdir('./src/fs/files'); + console.log(files); + } catch (err) { + throw new Error('FS operation failed'); + } }; await list(); diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..53e3ee2b6b 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,14 @@ +import { readFile } from 'fs/promises'; +import { access } from 'fs/promises'; + const read = async () => { - // Write your code here + try { + await access('./src/fs/files/fileToRead.txt'); + const content = await readFile('./src/fs/files/fileToRead.txt', { encoding: 'utf8' }); + console.log(content); + } catch (err) { + throw new Error('FS operation failed'); + } }; await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..054fbca34e 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,21 @@ +import { rename as renameFile, access } from 'fs/promises'; + const rename = async () => { - // Write your code here + try { + await access('./src/fs/files/wrongFilename.txt'); + try { + await access('./src/fs/files/properFilename.md'); + throw new Error('FS operation failed'); + } catch (err) { + if (err.code === 'ENOENT') { + await renameFile('./src/fs/files/wrongFilename.txt', './src/fs/files/properFilename.md'); + } else { + throw new Error('FS operation failed'); + } + } + } catch (err) { + throw new Error('FS operation failed'); + } }; await rename(); diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..4a7d31404a 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,17 @@ +import { createReadStream } from 'fs'; +import { createHash } from 'crypto'; + const calculateHash = async () => { - // Write your code here + const hash = createHash('sha256'); + const stream = createReadStream('./src/hash/files/fileToCalculateHashFor.txt'); + + stream.on('data', (data) => { + hash.update(data); + }); + + 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..0b389b13e8 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,43 @@ +import path from 'node:path'; +import os from 'node:os'; +import { createServer } from 'node:http'; +import { fileURLToPath } from 'node:url'; +import { readFile } from 'fs/promises'; + +import './files/c.cjs'; + +const random = Math.random(); + +const aJson = JSON.parse(await readFile('./src/modules/files/a.json', 'utf8')); +const bJson = JSON.parse(await readFile('./src/modules/files/b.json', 'utf8')); + +const unknownObject = random > 0.5 ? aJson : bJson; + +console.log(`Release ${os.release()}`); +console.log(`Version ${os.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 = createServer((_, 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..119602c67c 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,8 @@ +import { createReadStream } from 'fs'; + const read = async () => { - // Write your code here + const stream = createReadStream('./src/streams/files/fileToRead.txt'); + stream.pipe(process.stdout); }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..418945a432 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,14 @@ +import { Transform } from 'stream'; + const transform = async () => { - // Write your code here + const reverse = new Transform({ + transform(chunk, encoding, callback) { + const reversed = chunk.toString().split('').reverse().join(''); + callback(null, reversed); + } + }); + + process.stdin.pipe(reverse).pipe(process.stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..c364b08a6d 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,8 @@ +import { createWriteStream } from 'fs'; + const write = async () => { - // Write your code here + const stream = createWriteStream('./src/streams/files/fileToWrite.txt'); + process.stdin.pipe(stream); }; await write(); diff --git a/src/wt/main.js b/src/wt/main.js index e2ef054d41..886d46e284 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,42 @@ +import { Worker } from 'worker_threads'; +import { cpus } from 'os'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const performCalculations = async () => { - // Write your code here + const cpuCount = cpus().length; + console.log(`Number of CPU cores: ${cpuCount}`); + + const workers = []; + + for (let i = 0; i < cpuCount; i++) { + const worker = new Worker(path.join(__dirname, 'worker.js')); + workers.push(worker); + } + + const promises = workers.map((worker, i) => { + return new Promise((resolve) => { + const data = 10 + i; + worker.postMessage(data); + + worker.on('message', (message) => { + resolve({ status: 'resolved', data: message }); + }); + + worker.on('error', () => { + resolve({ status: 'error', data: null }); + }); + }); + }); + + const results = await Promise.all(promises); + + workers.forEach(worker => worker.terminate()); + + console.log(results); }; await performCalculations(); diff --git a/src/wt/worker.js b/src/wt/worker.js index 405595394d..95104028e4 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,12 @@ -// n should be received from main thread +import { parentPort } from '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..7755e17f31 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,13 @@ +import { createReadStream, createWriteStream } from 'fs'; +import { createGzip } from 'zlib'; +import { pipeline } from 'stream/promises'; + const compress = async () => { - // Write your code here + const source = createReadStream('./src/zip/files/fileToCompress.txt'); + const destination = createWriteStream('./src/zip/files/archive.gz'); + const gzip = createGzip(); + + await pipeline(source, gzip, destination); }; await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..3b5098e605 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,13 @@ +import { createReadStream, createWriteStream } from 'fs'; +import { createGunzip } from 'zlib'; +import { pipeline } from 'stream/promises'; + const decompress = async () => { - // Write your code here + const source = createReadStream('./src/zip/files/archive.gz'); + const destination = createWriteStream('./src/zip/files/fileToCompress.txt'); + const gunzip = createGunzip(); + + await pipeline(source, gunzip, destination); }; await decompress();