From 5edba3e20e3e9b7606771413bf6dc0ae68f3e08d Mon Sep 17 00:00:00 2001 From: Klim Fominskiy <132931200+KlimFominskiy@users.noreply.github.com> Date: Sun, 28 Jan 2024 09:06:38 +0300 Subject: [PATCH] init: completed --- src/cli/args.js | 13 +++++++- src/cli/env.js | 11 ++++++- src/cp/cp.js | 23 +++++++++++++- src/fs/copy.js | 23 +++++++++++++- src/fs/create.js | 24 +++++++++++++- src/fs/delete.js | 23 +++++++++++++- src/fs/list.js | 22 ++++++++++++- src/fs/read.js | 24 +++++++++++++- src/fs/rename.js | 37 +++++++++++++++++++++- src/hash/calcHash.js | 23 +++++++++++++- src/modules/{cjsToEsm.cjs => cjsToEsm.mjs} | 29 +++++++---------- src/streams/read.js | 20 +++++++++++- src/streams/transform.js | 18 ++++++++++- src/streams/write.js | 17 +++++++++- src/wt/main.js | 27 ++++++++++++++++ src/wt/worker.js | 3 ++ src/zip/compress.js | 20 +++++++++++- src/zip/decompress.js | 20 +++++++++++- 18 files changed, 345 insertions(+), 32 deletions(-) rename src/modules/{cjsToEsm.cjs => cjsToEsm.mjs} (54%) diff --git a/src/cli/args.js b/src/cli/args.js index 8283f7f7aa..aa64bdf7ca 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,16 @@ const parseArgs = () => { - // Write your code here + // Write your code here + const argumentsFromCL = process.argv.slice(2); + const reducedArguments = argumentsFromCL.reduce((previousValue, currentValue, currentIndex, array) => { + const nextValue = array[currentIndex + 1]; + if (nextValue !== undefined && currentValue.startsWith('--')) { + previousValue.push(`${currentValue.slice(2)} is ${nextValue}`); + } + + return previousValue; + }, []); + + console.log(reducedArguments.join(', ')); }; parseArgs(); \ No newline at end of file diff --git a/src/cli/env.js b/src/cli/env.js index fe4aa4a8df..5c3449afb7 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,14 @@ const parseEnv = () => { - // Write your code here + // Write your code here + const RSSVariables = Object.entries(process.env).reduce((accumulator, [key, value]) => { + if (key.startsWith('RSS_')) { + accumulator.push(`${key}=${value}`); + } + + return accumulator; + }, []); + + console.log(RSSVariables.join('; ')); }; parseEnv(); \ No newline at end of file diff --git a/src/cp/cp.js b/src/cp/cp.js index 4a87b98c55..a5790414ce 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,27 @@ const spawnChildProcess = async (args) => { // Write your code here + const childProcess = await import('node:child_process'); + const url = await import('node:url'); + const path = await import('node:path'); + + const currentJSFileAbsolutePath = url.fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const childFile = 'script.js'; + const filePathWithChildFile = path.join(folderPath, childFile); + + const childScriptProcess = childProcess.spawn(`node`, + [filePathWithChildFile, ...args.split(' ')]); + + process.stdin.on('data', (message) => { + childScriptProcess.stdin.write(message); + }) + + childScriptProcess.stdout.on('data', (data) => { + console.log(data.toString()); + }) }; // Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess('--arg1 d--arg2'); diff --git a/src/fs/copy.js b/src/fs/copy.js index bd17fe3991..cd09aef8a4 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,26 @@ const copy = async () => { - // Write your code here + // Write your code here + const fs = await import('node:fs/promises'); + const path = await import('node:path'); + const {fileURLToPath} = await import('node:url') + + const currentJSFileAbsolutePath = fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const sourceFolderName = 'files'; + const sourceFolderPath = path.join(currentDirPath, sourceFolderName); + const destinationFolderName = 'files_copy'; + const destinationFolderPath = path.join(currentDirPath, destinationFolderName); + const errorMessage = 'FS operation failed.'; + + try { + await fs.cp(sourceFolderPath, destinationFolderPath, {recursive: true, errorOnExist: true, force: false}); + } + catch (error) { + if (error.code === 'ERR_FS_CP_EEXIST') { + error.message = errorMessage; + } + throw error; + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 8d18cf9fc2..382a318218 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,27 @@ const create = async () => { - // Write your code here + // Write your code here + const fs= await import('node:fs/promises'); + const path= await import('node:path'); + const {fileURLToPath} = await import('node:url') + + const currentJSFileAbsolutePath = fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const fileName = 'fresh.txt'; + const filePath = path.join(folderPath, fileName); + const fileContent = 'I am fresh and young'; + const errorMessage = 'FS operation failed.' + + try { + await fs.writeFile(filePath, fileContent, {flag: 'wx'}); + } + catch (error) { + if (error.code === 'EEXIST') { + error.message = errorMessage; + } + throw error; + } }; await create(); \ No newline at end of file diff --git a/src/fs/delete.js b/src/fs/delete.js index 4718dbc4c5..c3c534bf09 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,26 @@ const remove = async () => { - // Write your code here + // Write your code here + const fs= await import('node:fs/promises'); + const path= await import('node:path'); + const {fileURLToPath} = await import('node:url') + + const currentJSFileAbsolutePath = fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const fileToRemoveName = 'fileToRemove.txt'; + const filePathWithFileToDelete = path.join(folderPath, fileToRemoveName); + const errorMessage = 'FS operation failed.'; + + try { + await fs.rm(filePathWithFileToDelete) + } + catch (error) { + if (error.code === 'ENOENT') { + error.message = errorMessage; + } + throw error; + } }; await remove(); \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js index c0a83dea15..86bdcd9100 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,25 @@ const list = async () => { - // Write your code here + // Write your code here + const fs= await import('node:fs/promises'); + const path= await import('node:path'); + const {fileURLToPath} = await import('node:url') + + const currentJSFileAbsolutePath = fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const errorMessage = 'FS operation failed.'; + + try { + const files = await fs.readdir(folderPath); + console.log(files); + } + catch (error) { + if (error.code === 'ENOENT') { + error.message = errorMessage; + } + throw error; + } }; await list(); \ No newline at end of file diff --git a/src/fs/read.js b/src/fs/read.js index 52c78cc6ee..3cfe66a2f6 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,27 @@ const read = async () => { - // Write your code here + // Write your code here + const fs= await import('node:fs/promises'); + const path= await import('node:path'); + const {fileURLToPath} = await import('node:url') + + const currentJSFileAbsolutePath = fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const fileToReadName = 'fileToRead.txt'; + const filePathWithFileToRead = path.join(folderPath, fileToReadName); + const errorMessage = 'FS operation failed.'; + + try { + const fileContent = await fs.readFile(filePathWithFileToRead, 'utf8'); + console.log(fileContent); + } + catch (error) { + if (error.code === 'ENOENT') { + error.message = errorMessage; + } + throw error; + } }; await read(); \ No newline at end of file diff --git a/src/fs/rename.js b/src/fs/rename.js index 2bb99ecdb5..c2e40614ce 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,40 @@ const rename = async () => { - // Write your code here + // Write your code here + const fs= await import('node:fs/promises'); + const path= await import('node:path'); + const {fileURLToPath} = await import('node:url') + + const currentJSFileAbsolutePath = fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const wrongFilename = 'wrongFilename.txt'; + const filePathWithWrongFile = path.join(folderPath, wrongFilename); + const properFilename = 'properFilename.md'; + const filePathWithProperFile = path.join(folderPath, properFilename); + const errorMessage = 'FS operation failed.'; + + try { + await fs.access(filePathWithProperFile, fs.constants.F_OK); + throw new Error(errorMessage) + } + catch (error) { + if (error.code !== 'ENOENT') { + throw error; + } + } + + try { + + await fs.access(filePathWithWrongFile, fs.constants.F_OK); + await fs.rename(filePathWithWrongFile, filePathWithProperFile); + } + catch (error) { + if (error.code === 'ENOENT') { + error.message = errorMessage; + } + throw error; + } }; await rename(); \ No newline at end of file diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 450f8f72e2..9f6dd51cfe 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,26 @@ const calculateHash = async () => { - // Write your code here + // Write your code here + const fs= await import('node:fs/promises'); + const path= await import('node:path'); + const {fileURLToPath} = await import('node:url') + const {createHash} = await import('node:crypto'); + + const currentJSFileAbsolutePath = fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const filenameToRead = 'fileToCalculateHashFor.txt'; + const filePathWithFileToRead = path.join(folderPath, filenameToRead); + const hash = createHash('sha256') + + try { + const fileContent = await fs.readFile(filePathWithFileToRead); + hash.update(fileContent); + console.log('Hash = ' + hash.digest('hex')); + } + catch (error) { + throw error; + } }; await calculateHash(); \ No newline at end of file diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.mjs similarity index 54% rename from src/modules/cjsToEsm.cjs rename to src/modules/cjsToEsm.mjs index 8b7be2a48b..8a52938310 100644 --- a/src/modules/cjsToEsm.cjs +++ b/src/modules/cjsToEsm.mjs @@ -1,17 +1,16 @@ -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'; +import {fileURLToPath} from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const random = Math.random(); -let unknownObject; - -if (random > 0.5) { - unknownObject = require('./files/a.json'); -} else { - unknownObject = require('./files/b.json'); -} +let unknownObject = random > 0.5 ? + await import('./files/a.json', {assert: {type: 'json'}}) : + await import('./files/b.json', {assert: {type: 'json'}}); console.log(`Release ${release()}`); console.log(`Version ${version()}`); @@ -33,8 +32,4 @@ myServer.listen(PORT, () => { console.log('To terminate it, use Ctrl+C combination'); }); -module.exports = { - unknownObject, - myServer, -}; - +export { unknownObject, myServer }; \ No newline at end of file diff --git a/src/streams/read.js b/src/streams/read.js index 52c78cc6ee..c40c78f5ba 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,23 @@ const read = async () => { - // Write your code here + // Write your code here + const fs = await import('node:fs'); + const path = await import('node:path'); + const {fileURLToPath} = await import('node:url') + + const currentJSFileAbsolutePath = fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const filenameToRead = 'fileToRead.txt'; + const filePathWithFileToRead = path.join(folderPath, filenameToRead); + + const readStream = fs.createReadStream(filePathWithFileToRead, {encoding: 'utf8'}); + readStream.on('readable', function() { + let data; + while ((data = readStream.read()) !== null) { + console.log(data); + } + }) }; await read(); \ No newline at end of file diff --git a/src/streams/transform.js b/src/streams/transform.js index 315fc6597f..892f1e5edf 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,21 @@ const transform = async () => { - // Write your code here + // Write your code here + const stream = await import('stream'); + + const input = process.stdin; + const output = process.stdout; + + const reverse = new stream.Transform({ + transform(chunk, encoding, callback) { + const inputString = chunk.toString().trim(); + const reversedString = inputString.split("").reverse().join(""); + callback(null, reversedString + "\n"); + } + }); + + await stream.pipeline(input, reverse, output, function(error){ + console.error(error); + }); }; await transform(); \ No newline at end of file diff --git a/src/streams/write.js b/src/streams/write.js index fc917160a2..5f30c2bbbc 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,20 @@ const write = async () => { - // Write your code here + // Write your code here + const fs = await import('node:fs'); + const path = await import('node:path'); + const url = await import('node:url') + + const currentJSFileAbsolutePath = url.fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const filenameToWrite = 'fileToWrite.txt'; + const filePathWithFileToWrite = path.join(folderPath, filenameToWrite); + + const writeStream = fs.createWriteStream(filePathWithFileToWrite, {encoding: 'utf8'}); + process.stdin.on('data', function (data) { + writeStream.write(data) + }) }; await write(); \ No newline at end of file diff --git a/src/wt/main.js b/src/wt/main.js index 37d80484ec..c39f382906 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,32 @@ const performCalculations = async () => { // Write your code here + const os = await import('node:os'); + const workerThreads = await import('node:worker_threads'); + const path = await import('node:path'); + const url = await import('node:url'); + + const currentJSFileAbsolutePath = url.fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const workerFileName = 'worker.js'; + const filePathToWorker = path.join(currentDirPath, workerFileName); + const coresNumber = os.cpus(); + let numberToSend = 10; + + const workerResults = await Promise.allSettled(coresNumber.map(() => { + return new Promise(function(resolve, reject) { + const worker = new workerThreads.Worker(filePathToWorker, + {workerData: numberToSend++}); + worker.on('message', resolve); + worker.on('error', reject); + }) + })) + + const results = workerResults.map(workerResult => ({ + status: workerResult.status === 'fulfilled' ? 'resolved' : 'error', + data : workerResult.status === 'fulfilled' ? workerResult.value : null + })); + + console.log(results); }; await performCalculations(); \ No newline at end of file diff --git a/src/wt/worker.js b/src/wt/worker.js index 441b2154f8..4a868a8b01 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,11 @@ +import workerThreads from 'node: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 + workerThreads.parentPort.postMessage(nthFibonacci(workerThreads.workerData)); }; sendResult(); \ No newline at end of file diff --git a/src/zip/compress.js b/src/zip/compress.js index bb328f43c6..67122ba19b 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,23 @@ const compress = async () => { - // Write your code here + // Write your code here + const fs = await import('node:fs'); + const path = await import('node:path'); + const url = await import('node:url') + const zlib = await import('node:zlib'); + + const currentJSFileAbsolutePath = url.fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const fileToCompress = 'fileToCompress.txt'; + const filePathWithFileToCompress = path.join(folderPath, fileToCompress); + const fileCompressed = 'archive.gz'; + const filePathWithFileCompressed = path.join(folderPath, fileCompressed); + + const readStream = fs.createReadStream(filePathWithFileToCompress); + const writeStream = fs.createWriteStream(filePathWithFileCompressed); + const gzip = zlib.createGzip(); + readStream.pipe(gzip).pipe(writeStream); }; await compress(); \ No newline at end of file diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 69f6c345f8..a9b0a33859 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,23 @@ const decompress = async () => { - // Write your code here + // Write your code here + const fs = await import('node:fs'); + const path = await import('node:path'); + const url = await import('node:url') + const zlib = await import('node:zlib'); + + const currentJSFileAbsolutePath = url.fileURLToPath(import.meta.url); + const currentDirPath = path.dirname(currentJSFileAbsolutePath); + const folderName = 'files'; + const folderPath = path.join(currentDirPath, folderName); + const fileToDecompress = 'archive.gz'; + const filePathWithFileToDecompress = path.join(folderPath, fileToDecompress); + const fileDecompressed = 'fileToCompress.txt'; + const filePathWithFileDecompressed = path.join(folderPath, fileDecompressed); + + const readStream = fs.createReadStream(filePathWithFileToDecompress); + const writeStream = fs.createWriteStream(filePathWithFileDecompressed); + const unzip = zlib.createUnzip(); + readStream.pipe(unzip).pipe(writeStream); }; await decompress(); \ No newline at end of file