diff --git a/src/cli/args.js b/src/cli/args.js index 8283f7f7aa..935a5a61be 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,17 @@ const parseArgs = () => { // Write your code here + const args = process.argv.slice(2); + const parsedArgs = {}; + + for (let i = 0; i < args.length; i += 2) { + const propName = args[i].replace('--',''); + const value = args[i + 1]; + parsedArgs[propName] = value; + } + + for (const prop in parsedArgs) { + console.log(`${prop} is ${parsedArgs[prop]}`); + } }; parseArgs(); \ No newline at end of file diff --git a/src/cli/env.js b/src/cli/env.js index fe4aa4a8df..b68d281241 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,11 @@ const parseEnv = () => { // Write your code here + const envVars = Object.keys(process.env) + .filter(key => key.startsWith('RSS_')) + .map(key => `${key}=${process.env[key]}`) + .join("; "); + + console.log(envVars); }; parseEnv(); \ No newline at end of file diff --git a/src/cp/cp.js b/src/cp/cp.js index 4a87b98c55..99c2d5c5fc 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,16 @@ +import { fileURLToPath } from 'url'; +import path from 'path'; +import { fork } from 'child_process'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const filePath = path.join(__dirname, 'files', 'script.js'); + const spawnChildProcess = async (args) => { // Write your code here + fork(filePath, args, { silent: false }) }; // 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 bd17fe3991..79a8d5efee 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,20 @@ +import fs from 'fs/promises'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const sourcePath = path.join(__dirname, 'files'); +const destPath = path.join(__dirname, 'files_copy'); + const copy = async () => { // Write your code here + try { + await fs.cp(sourcePath, destPath, { recursive: true, force: false, errorOnExist: true }); + } catch (error) { + throw new Error('FS operation failed'); + } }; -await copy(); +await copy(); \ No newline at end of file diff --git a/src/fs/create.js b/src/fs/create.js index 8d18cf9fc2..b5cfed5511 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,19 @@ +import fs from 'fs/promises'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const pathToFreshFile = path.join(__dirname, 'files', 'fresh.txt'); + const create = async () => { // Write your code here + try { + await fs.writeFile(pathToFreshFile, 'I am fresh and young', { flag: 'wx' }); + } catch (error) { + throw new Error('FS operation failed'); + } + }; await create(); \ No newline at end of file diff --git a/src/fs/delete.js b/src/fs/delete.js index 4718dbc4c5..f514d34a36 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,19 @@ +import fs from 'fs/promises'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const fileToRemove = path.join(__dirname, 'files', 'fileToRemove.txt'); + const remove = async () => { // Write your code here + try { + await fs.rm(fileToRemove); + } catch (error) { + throw new Error('FS operation failed'); + } }; await remove(); \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js index c0a83dea15..7aeced8096 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,20 @@ +import fs from 'fs/promises'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const sourcePath = path.join(__dirname, 'files'); + const list = async () => { // Write your code here + try { + const fileList = await fs.readdir(sourcePath); + console.log(fileList); + } catch (error) { + throw new Error('FS operation failed'); + } }; await list(); \ No newline at end of file diff --git a/src/fs/read.js b/src/fs/read.js index 52c78cc6ee..64c8fe0497 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,20 @@ +import fs from 'fs/promises'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const pathToFileToRead = path.join(__dirname, 'files', 'fileToRead.txt'); + const read = async () => { // Write your code here + try { + const content = await fs.readFile(pathToFileToRead, { encoding: 'utf8' }); + console.log(content); + } catch (error) { + throw new Error('FS operation failed'); + } }; await read(); \ No newline at end of file diff --git a/src/fs/rename.js b/src/fs/rename.js index 2bb99ecdb5..9bd95a5b83 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,20 @@ +import fs from 'fs/promises'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const pathToWrongFile = path.join(__dirname, 'files', 'wrongFilename.txt'); +const pathToProperFile = path.join(__dirname, 'files', 'properFilename.md'); + const rename = async () => { // Write your code here + try { + await fs.rename(pathToWrongFile, pathToProperFile); + } catch (error) { + throw new Error('FS operation failed'); + } }; await rename(); \ No newline at end of file diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 450f8f72e2..0741dd24a8 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,18 @@ +import path from 'path'; +import { readFile } from 'fs/promises'; +import { fileURLToPath } from 'url'; +import { createHash } from 'crypto'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt'); + const calculateHash = async () => { // Write your code here + const fileContent = await readFile(filePath); + const hash = createHash('sha256').update(fileContent).digest('hex'); + console.log(`SHA256 hash: ${hash}`); }; await calculateHash(); \ No newline at end of file diff --git a/src/modules/cjsToEsm.cjs b/src/modules/esm.mjs similarity index 50% rename from src/modules/cjsToEsm.cjs rename to src/modules/esm.mjs index 8b7be2a48b..59e0884206 100644 --- a/src/modules/cjsToEsm.cjs +++ b/src/modules/esm.mjs @@ -1,24 +1,24 @@ -const path = require('path'); -const { release, version } = require('os'); -const { createServer: createServerHttp } = require('http'); -require('./files/c'); +import path from 'path'; +import { release, version } from 'os'; +import { createServer as createServerHttp } from 'http'; +import './files/c'; const random = Math.random(); let unknownObject; if (random > 0.5) { - unknownObject = require('./files/a.json'); + unknownObject = await import('./files/a.json', { assert: { type: "json" } }); } else { - unknownObject = require('./files/b.json'); + unknownObject = await 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 ${__filename}`); -console.log(`Path to current directory is ${__dirname}`); +console.log(`Path to current file is ${import.meta.url}`); +console.log(`Path to current directory is ${new URL('.', import.meta.url).pathname}`); const myServer = createServerHttp((_, res) => { res.end('Request accepted'); @@ -33,8 +33,5 @@ myServer.listen(PORT, () => { console.log('To terminate it, use Ctrl+C combination'); }); -module.exports = { - unknownObject, - myServer, -}; +export { unknownObject, myServer }; diff --git a/src/streams/read.js b/src/streams/read.js index 52c78cc6ee..64d7e42170 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,17 @@ +import { fileURLToPath } from 'url'; +import path from 'path'; +import { createReadStream } from 'fs'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const filePath = path.join(__dirname, 'files', 'fileToRead.txt'); + const read = async () => { - // Write your code here + // Write your code here + const readStream = createReadStream(filePath, 'utf8'); + readStream.pipe(process.stdout); + }; await read(); \ No newline at end of file diff --git a/src/streams/transform.js b/src/streams/transform.js index 315fc6597f..ab2f8eca76 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,16 @@ +import { Transform } from 'stream'; +import { pipeline } from 'stream/promises'; + const transform = async () => { // Write your code here + const reverseTransform = new Transform({ + transform(chunk, encoding, callback) { + const reversedChunk = chunk.toString().split('').reverse().join(''); + callback(null, reversedChunk + '\n'); + } + }); + + await pipeline(process.stdin, reverseTransform, process.stdout); }; await transform(); \ No newline at end of file diff --git a/src/streams/write.js b/src/streams/write.js index fc917160a2..48a4150d8c 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,16 @@ +import { fileURLToPath } from 'url'; +import path from 'path'; +import { createWriteStream } from 'fs'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const filePath = path.join(__dirname, 'files', 'fileToWrite.txt') + const write = async () => { // Write your code here + const writeStream = createWriteStream(filePath); + process.stdin.pipe(writeStream); }; await write(); \ No newline at end of file diff --git a/src/wt/main.js b/src/wt/main.js index 37d80484ec..e61c3a4e4c 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,35 @@ +import { fileURLToPath } from 'url'; +import path from 'path'; +import { cpus } from 'os'; +import { Worker } from 'worker_threads'; + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const filePath = path.join(__dirname, 'worker.js'); + const performCalculations = async () => { // Write your code here + const numCores = cpus().length; + const workers = []; + + const createWorker = (data) => { + return new Promise((resolve) => { + const worker = new Worker(filePath, {workerData: data}); + + worker.on('message', (data) => resolve({status: 'resolved', data})); + worker.on('error', () => resolve({status: 'error', data: null})) + }); + } + + for (let i = 0; i < numCores; i++) { + workers.push(createWorker(i + 10)); + } + + const workerResults = await Promise.all(workers); + + console.log(workerResults); }; await performCalculations(); \ No newline at end of file diff --git a/src/wt/worker.js b/src/wt/worker.js index 441b2154f8..ced35251be 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,11 @@ +import { parentPort, workerData } from 'worker_threads'; + // n should be received from main thread 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.postMessage(nthFibonacci(workerData)); }; sendResult(); \ No newline at end of file diff --git a/src/zip/compress.js b/src/zip/compress.js index bb328f43c6..553c06ee3f 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,25 @@ +import { fileURLToPath } from 'url'; +import path from 'path'; +import { createReadStream, createWriteStream } from 'fs'; +import { pipeline } from 'stream'; +import { createGzip } from 'zlib'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const fileToCompress = path.join(__dirname, 'files', 'fileToCompress.txt'); +const archiveFile = path.join(__dirname, 'files', 'archive.gz'); + +const readStream = createReadStream(fileToCompress); +const writeStream = createWriteStream(archiveFile); + const compress = async () => { // Write your code here + pipeline(readStream, + createGzip(), + writeStream, + (error) => console.error(error) + ); }; await compress(); \ No newline at end of file diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 69f6c345f8..f31c225aa3 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,25 @@ +import { fileURLToPath } from 'url'; +import path from 'path'; +import { createReadStream, createWriteStream } from 'fs'; +import { pipeline } from 'stream'; +import { createUnzip } from 'zlib'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const fileToCompress = path.join(__dirname, 'files', 'fileToCompress.txt'); +const archiveFile = path.join(__dirname, 'files', 'archive.gz'); + +const readStream = createReadStream(archiveFile); +const writeStream = createWriteStream(fileToCompress); + const decompress = async () => { // Write your code here + pipeline(readStream, + createUnzip(), + writeStream, + (error) => console.error(error) + ); }; await decompress(); \ No newline at end of file