diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..7e212c11a9 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 result = []; + + for (let i = 0; i < args.length; i += 2) { + const propName = args[i].replace('--', ''); + const value = args[i + 1]; + + result.push(`${propName} is ${value}`); + } + + console.log(result.join(', ')); }; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..e5b3f41c44 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; + + const result = Object.entries(envVars) + .filter(([key]) => key.startsWith('RSS_')) + .map(([key, value]) => `${key}=${value}`) + .join('; '); + + console.log(result); }; parseEnv(); diff --git a/src/cp/cp.js b/src/cp/cp.js index 72c6addc9c..5b35483523 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,28 @@ +import { spawn } from 'node:child_process'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const spawnChildProcess = async (args) => { - // Write your code here + const childScript = path.resolve(__dirname, 'files', 'script.js'); + + const child = spawn('node', [childScript, ...args], { + stdio: ['pipe', 'pipe', 'inherit'], + }); + + process.stdin.pipe(child.stdin); + + child.stdout.pipe(process.stdout); + + child.on('close', (code) => { + console.log(`\nChild process exited with code ${code}`); + }); + + child.on('error', (err) => { + console.error('Failed to start child process:', err); + }); }; -// Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess(['smth', 'bbb']); diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..3ed9356323 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,23 @@ +import { cp } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const copy = async () => { - // Write your code here + try { + const src = path.resolve(__dirname, 'files'); + const dest = path.resolve(__dirname, 'files_copy'); + + await cp(src, dest, { + recursive: true, + errorOnExist: true, + force: false, + }); + } catch { + throw new Error('FS operation failed'); + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..a7daee8f52 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,18 @@ +import { writeFile } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const create = async () => { - // Write your code here + try { + const filePath = path.resolve(__dirname, 'files', 'fresh.txt'); + + await writeFile(filePath, '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..0766126490 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,20 @@ +import { rm, access } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const remove = async () => { - // Write your code here + try { + const filePath = path.resolve(__dirname, 'files', 'fileToRemove.txt'); + + await access(filePath); + + await rm(filePath); + } catch { + throw new Error('FS operation failed'); + } }; await remove(); diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt deleted file mode 100644 index 43e64cd45c..0000000000 --- a/src/fs/files/fileToRemove.txt +++ /dev/null @@ -1 +0,0 @@ -How dare you! \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..594120ca3e 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,20 @@ +import { readdir } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const list = async () => { - // Write your code here + try { + const folderPath = path.resolve(__dirname, 'files'); + + const files = await readdir(folderPath); + + 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..fc0441cc7c 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,20 @@ +import { readFile } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const read = async () => { - // Write your code here + try { + const filePath = path.resolve(__dirname, 'files', 'fileToRead.txt'); + + const content = await readFile(filePath, 'utf-8'); + + console.log(content); + } catch { + throw new Error('FS operation failed'); + } }; await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..3cfa11da94 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,26 @@ +import { rename as fsRename, access } from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const rename = async () => { - // Write your code here + try { + const srcPath = path.resolve(__dirname, 'files', 'wrongFilename.txt'); + const destPath = path.resolve(__dirname, 'files', 'properFilename.md'); + + await access(srcPath); + + try { + await access(destPath); + throw new Error(); + } catch {} + + await fsRename(srcPath, destPath); + } catch { + throw new Error('FS operation failed'); + } }; await rename(); diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..2e405d3350 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,31 @@ +import { createReadStream } from 'node:fs'; +import { createHash } from 'node:crypto'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const calculateHash = async () => { - // Write your code here + const filePath = path.resolve( + __dirname, + 'files', + 'fileToCalculateHashFor.txt', + ); + + return new Promise((resolve, reject) => { + const hash = createHash('sha256'); + const stream = createReadStream(filePath); + + stream.on('error', () => reject(new Error('FS operation failed'))); + + stream.on('data', (chunk) => hash.update(chunk)); + + stream.on('end', () => { + console.log(hash.digest('hex')); + resolve(); + }); + }); }; await calculateHash(); diff --git a/src/modules/cjsToEsm.cjs b/src/modules/esm.mjs similarity index 51% rename from src/modules/cjsToEsm.cjs rename to src/modules/esm.mjs index 089bd2db13..fed4191f7a 100644 --- a/src/modules/cjsToEsm.cjs +++ b/src/modules/esm.mjs @@ -1,12 +1,19 @@ -const path = require('node:path'); -const { release, version } = require('node:os'); -const { createServer: createServerHttp } = require('node:http'); +import path from 'node:path'; +import { release, version } from 'node:os'; +import { createServer as createServerHttp } from 'node:http'; +import { fileURLToPath } from 'node:url'; -require('./files/c.cjs'); +import './files/c.cjs'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const random = Math.random(); -const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/b.json'); +const unknownObject = + random > 0.5 + ? (await import('./files/a.json', { with: { type: 'json' } })).default + : (await import('./files/b.json', { with: { type: 'json' } })).default; console.log(`Release ${release()}`); console.log(`Version ${version()}`); @@ -28,7 +35,4 @@ myServer.listen(PORT, () => { console.log('To terminate it, use Ctrl+C combination'); }); -module.exports = { - unknownObject, - myServer, -}; +export { unknownObject, myServer }; diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..7d1ec4dd38 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,20 @@ +import { createReadStream } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const read = async () => { - // Write your code here + const filePath = path.resolve(__dirname, 'files', 'fileToRead.txt'); + + const readable = createReadStream(filePath); + + readable.on('error', () => { + throw new Error('FS operation failed'); + }); + + readable.pipe(process.stdout); }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..9001ff6876 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 reverseStream = new Transform({ + transform(chunk, _, callback) { + const reversed = chunk.toString().split('').reverse().join(''); + callback(null, reversed); + }, + }); + + process.stdin.pipe(reverseStream).pipe(process.stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..1af705b4b4 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,16 @@ +import { createWriteStream } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const write = async () => { - // Write your code here + const filePath = path.resolve(__dirname, 'files', 'fileToWrite.txt'); + + const writable = createWriteStream(filePath); + + process.stdin.pipe(writable); }; await write(); diff --git a/src/wt/main.js b/src/wt/main.js index e2ef054d41..9de68c5cd5 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,35 @@ +import { Worker } from 'node:worker_threads'; +import os from 'node:os'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const performCalculations = async () => { - // Write your code here + const numCPUs = os.cpus().length; + const results = []; + const workers = []; + + for (let i = 0; i < numCPUs; i++) { + const worker = new Worker(path.resolve(__dirname, 'worker.js')); + workers.push(worker); + + const n = 10 + i; + + const promise = new Promise((resolve) => { + worker.once('message', (msg) => resolve(msg)); + worker.once('error', () => resolve({ status: 'error', data: null })); + }); + + worker.postMessage(n); + results.push(promise); + } + + const finalResults = await Promise.all(results); + console.log(finalResults); + + workers.forEach((w) => w.terminate()); }; await performCalculations(); diff --git a/src/wt/worker.js b/src/wt/worker.js index 405595394d..f8af98662a 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,27 @@ -// 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); + +let lastResult = null; const sendResult = () => { - // This function sends result of nthFibonacci computations to main thread + if (parentPort && lastResult !== null) { + parentPort.postMessage(lastResult); + } }; +if (parentPort) { + parentPort.on('message', (n) => { + try { + const fib = nthFibonacci(n); + lastResult = { status: 'resolved', data: fib }; + sendResult(); + } catch (err) { + lastResult = { status: 'error', data: null }; + sendResult(); + } + }); +} + sendResult(); diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..162e29f24a 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,27 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { createGzip } from 'node:zlib'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const compress = async () => { - // Write your code here + const inputFile = path.resolve(__dirname, 'files', 'fileToCompress.txt'); + const outputFile = path.resolve(__dirname, 'files', 'archive.gz'); + + return new Promise((resolve, reject) => { + const readStream = createReadStream(inputFile); + const writeStream = createWriteStream(outputFile); + const gzip = createGzip(); + + readStream.on('error', () => reject(new Error('FS operation failed'))); + writeStream.on('error', () => reject(new Error('FS operation failed'))); + + writeStream.on('finish', resolve); + + readStream.pipe(gzip).pipe(writeStream); + }); }; await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..90964735b5 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,27 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { createGunzip } from 'node:zlib'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const decompress = async () => { - // Write your code here + const inputFile = path.resolve(__dirname, 'files', 'archive.gz'); + const outputFile = path.resolve(__dirname, 'files', 'fileToCompress.txt'); + + return new Promise((resolve, reject) => { + const readStream = createReadStream(inputFile); + const writeStream = createWriteStream(outputFile); + const gunzip = createGunzip(); + + readStream.on('error', () => reject(new Error('FS operation failed'))); + writeStream.on('error', () => reject(new Error('FS operation failed'))); + + writeStream.on('finish', resolve); + + readStream.pipe(gunzip).pipe(writeStream); + }); }; await decompress();