diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..1fe1b00e4f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +node_modules/ diff --git a/output.txt b/output.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/cli/args.js b/src/cli/args.js index 8283f7f7aa..9d289056bc 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,15 @@ const parseArgs = () => { - // Write your code here + const outputArray = []; + + for (let index = 2; index < process.argv.length; index += 2) { + const key = process.argv[index].slice(2); + const value = process.argv[index + 1]; + + outputArray.push(`${key} is ${value}`); + } + + const outputSeparator = ', '; + console.log(outputArray.join(outputSeparator)) }; -parseArgs(); \ No newline at end of file +parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index fe4aa4a8df..56f5cdc5e9 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,17 @@ const parseEnv = () => { - // Write your code here -}; + const prefix = 'RSS_' -parseEnv(); \ No newline at end of file + const allEnvVariables = process.env + const suitableVariables = [] + + for (const [key, value] of Object.entries(allEnvVariables)) { + if (key.startsWith(prefix)) { + suitableVariables.push(`${key}=${value}`) + } + } + + const outputSeparator = '; ' + console.log(suitableVariables.join(outputSeparator)); +} + +parseEnv() diff --git a/src/cp/cp.js b/src/cp/cp.js index 4a87b98c55..258690d285 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,17 @@ +import { spawn } from 'child_process'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + + const spawnChildProcess = async (args) => { - // Write your code here + const childProcessCodePath = path.resolve(__dirname, './files/script.js'); + const childProcess = spawn('node', [childProcessCodePath, ...args]); + + process.stdin.pipe(childProcess.stdin); + childProcess.stdout.pipe(process.stdout); }; -// Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess( ['arg1', 'arg2']); diff --git a/src/fs/copy.js b/src/fs/copy.js index bd17fe3991..35f0d6a3fe 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,25 @@ +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const copy = async () => { - // Write your code here -}; + const sourcePath = path.resolve(__dirname, './files'); + const destinationPath = path.resolve(__dirname, './files_copy'); + + if (!fs.existsSync(sourcePath) || fs.existsSync(destinationPath)) { + throw new Error('FS operation failed'); + } + + await new Promise(resolve => { + fs.mkdir(destinationPath, {}, resolve); + }); + + await new Promise(resolve => { + fs.cp(sourcePath, destinationPath, { recursive: true}, resolve); + }); +} await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 8d18cf9fc2..5062eb8899 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,19 @@ +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const create = async () => { - // Write your code here + const fileContent = 'I am fresh and young'; + const filePath = path.resolve(__dirname, './files/fresh.txt'); + + if (fs.existsSync(filePath)) { + throw new Error('FS operation failed'); + } + + await fs.promises.writeFile(filePath, fileContent, { encoding: 'utf-8' }); }; -await create(); \ No newline at end of file +await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index 4718dbc4c5..b19458a471 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,18 @@ +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const remove = async () => { - // Write your code here + const filePathToDelete = path.resolve(__dirname, './files/fileToRemove.txt'); + + if (!fs.existsSync(filePathToDelete)) { + throw new Error('FS operation failed'); + } + + fs.rmSync(filePathToDelete); }; -await remove(); \ No newline at end of file +await remove(); diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt index 43e64cd45c..568ad5c5d8 100644 --- a/src/fs/files/fileToRemove.txt +++ b/src/fs/files/fileToRemove.txt @@ -1 +1 @@ -How dare you! \ No newline at end of file +How dare you 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/list.js b/src/fs/list.js index c0a83dea15..91b97aa999 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,24 @@ +import fs from 'fs' +import path from 'path' +import {fileURLToPath} from 'url' + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const list = async () => { - // Write your code here -}; + const folderPath = path.resolve(__dirname, './files/') + + if (!fs.existsSync(folderPath) || !fs.lstatSync(folderPath).isDirectory()) { + throw new Error('FS operation failed'); + } + + const files = await new Promise(resolve => { + fs.readdir(folderPath, (error, files) => { + resolve(files); + }); + }); + + console.log(files); +} -await list(); \ No newline at end of file +await list(); diff --git a/src/fs/read.js b/src/fs/read.js index 52c78cc6ee..764736beac 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,19 @@ +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from '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'); + + if (!fs.existsSync(filePath) || !fs.lstatSync(filePath).isFile()) { + throw new Error('FS operation failed'); + } + + const content = await fs.promises.readFile(filePath, { encoding: 'utf-8' }); + console.log(content); }; -await read(); \ No newline at end of file +await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js index 2bb99ecdb5..80be1467f2 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,19 @@ +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const rename = async () => { - // Write your code here + const sourcePath = path.resolve(__dirname, './files/wrongFilename.txt'); + const resultPath = path.resolve(__dirname, './files/properFilename.md'); + + if (!fs.existsSync(sourcePath) || fs.existsSync(resultPath)) { + throw new Error('FS operation failed'); + } + + fs.renameSync(sourcePath, resultPath); }; -await rename(); \ No newline at end of file +await rename(); diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 450f8f72e2..34cd4afe7f 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,33 @@ +import { createHash } from 'crypto'; +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from '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'); + + const fileContent = await new Promise((resolve) => { + let data = ''; + + const stream = fs.createReadStream(filePath, {encoding: 'utf8'}); + + stream.on('data', part => { + data += part.toString(); + }); + + stream.on('close', () => { + resolve(data); + }); + }); + + const hash = createHash('sha256'); + hash.update(fileContent); + const hashedValue = hash.digest('hex'); + + console.log(hashedValue); }; -await calculateHash(); \ No newline at end of file +await calculateHash(); diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs new file mode 100644 index 0000000000..d448e0295f --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,45 @@ +import path from 'path'; +import {createRequire} from 'module'; +import {fileURLToPath} from 'url'; +import {release, version} from 'os'; +import {createServer as createServerHttp} from 'http'; +import './files/c.js'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const random = Math.random(); + +let unknownObject; + +const require = createRequire(import.meta.url); +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'); +}); + +export { + unknownObject, myServer +}; + diff --git a/src/streams/read.js b/src/streams/read.js index 52c78cc6ee..30cf6460a1 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,31 @@ +import {createHash} from 'crypto'; +import fs from 'fs'; +import path from 'path'; +import {fileURLToPath} from '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 data = await new Promise(resolve => { + const stream = fs.createReadStream(filePath); + let fileData = ''; + + stream.on('readable', () => { + let chunk; + + while (null !== (chunk = stream.read(1))) { + fileData += chunk; + } + }); + + stream.on('end', () => resolve(fileData)); + }); + + + process.stdout.write(data); }; -await read(); \ No newline at end of file +await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 315fc6597f..6b31a8e51a 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,16 @@ +import {Transform} from 'stream'; + + const transform = async () => { - // Write your code here + const reverseTransform = new Transform({ + transform(chunk, _, callback) { + this.push([...chunk.toString()].reverse().join('')); + callback(); + }, + encoding: 'utf-8' + }); + + process.stdin.pipe(reverseTransform).pipe(process.stdout); }; -await transform(); \ No newline at end of file +await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index fc917160a2..6986090016 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,16 @@ +import fs from 'fs'; +import path from 'path'; +import {fileURLToPath} from 'url'; +import {pipeline} from 'stream'; + +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 fileWriteStream = fs.createWriteStream(filePath, {encoding: 'utf-8'}); + + process.stdin.pipe(fileWriteStream); }; -await write(); \ No newline at end of file +await write(); diff --git a/src/wt/main.js b/src/wt/main.js index 37d80484ec..89ca2e8b48 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,40 @@ +import os from 'os'; +import {Worker} from 'worker_threads'; +import path from 'path'; +import url from 'url'; + +const __filename = url.fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const hostMachineCoresCount = os.cpus().length; + const performCalculations = async () => { - // Write your code here + const workerPath = path.resolve(__dirname, './worker.js'); + const workersCount = hostMachineCoresCount; + + const dataForWorkers = new Array(workersCount).fill(10).map((value, index) => value + index); + + const workerPromises = dataForWorkers.map(value => { + return new Promise((resolve, reject) => { + const worker = new Worker(workerPath, { + workerData: value + }); + + worker.on('message', value => { + resolve({status: 'resolved', data: value}); + worker.terminate(); + }); + + worker.on('error', () => { + resolve({status: 'error', data: null}); + worker.terminate(); + }); + }); + }); + + const results = await Promise.all(workerPromises); + + 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..732e549959 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 +sendResult(); diff --git a/src/zip/compress.js b/src/zip/compress.js index bb328f43c6..52823329b7 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,25 @@ +import fs from 'fs'; +import path from 'path'; +import url from 'url'; +import zlib from 'zlib'; +import stream from 'stream'; +import utils from 'util'; + + +const __filename = url.fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const compress = async () => { - // Write your code here + const sourcePath = path.resolve(__dirname, './files/fileToCompress.txt'); + const destinationPath = path.resolve(__dirname, './files/archive.gz'); + + const sourceStream = fs.createReadStream(sourcePath); + const destinationStream = fs.createWriteStream(destinationPath); + const gzip = zlib.createGzip(); + + const pipe = utils.promisify(stream.pipeline); + + await pipe(sourceStream, gzip, destinationStream); }; -await compress(); \ No newline at end of file +await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 69f6c345f8..644acaa4e1 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,26 @@ +import fs from 'fs'; +import path from 'path'; +import url from 'url'; +import zlib from 'zlib'; +import stream from 'stream'; +import utils from 'util'; + + +const __filename = url.fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const decompress = async () => { - // Write your code here + const sourcePath = path.resolve(__dirname, './files/archive.gz'); + const destinationPath = path.resolve(__dirname, './files/fileToCompress.txt'); + + const sourceStream = fs.createReadStream(sourcePath); + const destinationStream = fs.createWriteStream(destinationPath); + const gzip = zlib.createUnzip(); + + const pipe = utils.promisify(stream.pipeline); + + await pipe(sourceStream, gzip, destinationStream); }; -await decompress(); \ No newline at end of file + +await decompress(); diff --git a/src/zip/files/archive.gz b/src/zip/files/archive.gz new file mode 100644 index 0000000000..661bf7c1a9 Binary files /dev/null and b/src/zip/files/archive.gz differ diff --git a/src/zip/files/fileToCompress.txt b/src/zip/files/fileToCompress.txt index 4d4efc82fe..8ba354e8ec 100644 --- a/src/zip/files/fileToCompress.txt +++ b/src/zip/files/fileToCompress.txt @@ -1 +1 @@ -Compress me! \ No newline at end of file +Compress me !