From 7cbbee85327fd9dd53cd8a8f497828af4c5bb6b5 Mon Sep 17 00:00:00 2001 From: Natallia Date: Tue, 30 Jan 2024 00:48:33 +0100 Subject: [PATCH 1/8] File system practice --- src/fs/copy.js | 18 +++++++++++++++++- src/fs/create.js | 13 ++++++++++++- src/fs/delete.js | 14 +++++++++++++- src/fs/list.js | 15 ++++++++++++++- src/fs/read.js | 13 ++++++++++++- src/fs/rename.js | 20 +++++++++++++++++++- 6 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index bd17fe3991..022962f3ac 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,21 @@ +import fs from 'fs'; +import path from 'path'; +import {fileURLToPath} from 'url'; + +const currentFilename = fileURLToPath(import.meta.url); +const currentDirectory = path.dirname(currentFilename); +const filesDirectory = path.join(currentDirectory, '/files'); +const copyFilesDirectory = path.join(currentDirectory, '/files_copy'); +const errorMsg = 'FS operation failed'; const copy = async () => { - // Write your code here + if (!fs.existsSync(filesDirectory) || fs.existsSync(copyFilesDirectory)) throw new Error(errorMsg); + + const filenames = fs.readdirSync(filesDirectory); + fs.mkdirSync(copyFilesDirectory); + + for (let filename of filenames) { + fs.copyFile(path.join(filesDirectory, `/${filename}`), path.join(copyFilesDirectory, `/${filename}`), (e)=>{}) + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 8d18cf9fc2..101596e41d 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,16 @@ +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 filePath = `${dirname}/files/fresh.txt`; const create = async () => { - // Write your code here + try { + await fs.writeFile(filePath, 'I am fresh and young', {flag: 'wx'}); + } catch (e) { + 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..96511c5675 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,17 @@ +import fs from 'fs/promises'; +import {fileURLToPath} from 'url'; +import path from 'path'; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); +const fileToRemove = path.join(dirname, '/files/fileToRemove.txt'); +const errorMsg = 'FS operation failed'; const remove = async () => { - // Write your code here + try { + await fs.rm(fileToRemove) + } catch (e) { + throw new Error(errorMsg); + } }; await remove(); \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js index c0a83dea15..2563a9bd39 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,18 @@ +import fs from 'fs/promises'; +import path from 'path'; +import {fileURLToPath} from 'url'; + +const currentFilename = fileURLToPath(import.meta.url); +const filesDirectory = path.join(path.dirname(currentFilename), '/files') const list = async () => { - // Write your code here + try { + const files = await fs.readdir(filesDirectory); + for (const file of files) { + console.log(file); + } + } catch (err) { + 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..69eda3b9e5 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,16 @@ +import fs from 'fs/promises'; +import path from 'path'; +import {fileURLToPath} from 'url'; + +const currentFilename = fileURLToPath(import.meta.url); +const fileToRead = path.join(path.dirname(currentFilename), '/files/fileToRea3d.txt'); const read = async () => { - // Write your code here + try { + const content = await fs.readFile(fileToRead, {encoding: 'utf8'}); + console.log(content); + } catch (e) { + 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..fd6b7d2155 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,23 @@ +import fs from 'fs/promises'; +import {fileURLToPath} from "url"; +import path from "path"; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); +const oldFilePath = path.join(dirname, '/files/wrongFilename.txt'); +const newFilePath = path.join(dirname, '/files/properFilename.md'); +const errorMsg = 'FS operation failed'; + const rename = async () => { - // Write your code here + const fileExists = await fs.access(newFilePath, fs.constants.F_OK).then(() => true, () => false); + if (fileExists) throw new Error(errorMsg); + + try { + await fs.rename(oldFilePath, newFilePath); + } catch (e) { + console.log(e); + throw new Error(errorMsg); + } }; await rename(); \ No newline at end of file From f7fc634e86fd1d8fc1879a7df099724c956401d0 Mon Sep 17 00:00:00 2001 From: Natallia Date: Tue, 30 Jan 2024 00:49:04 +0100 Subject: [PATCH 2/8] Command line interface practice --- src/cli/args.js | 6 +++++- src/cli/env.js | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cli/args.js b/src/cli/args.js index 8283f7f7aa..037db4dd9d 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,9 @@ const parseArgs = () => { - // Write your code here + process.argv.slice(2).forEach((arg, i, arr) => { + if (arg.startsWith('--')) { + console.log(`${arg.replace('--', '')} is ${arr[i + 1]}`); + } + }); }; parseArgs(); \ No newline at end of file diff --git a/src/cli/env.js b/src/cli/env.js index fe4aa4a8df..a6854fa8f5 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,9 @@ const parseEnv = () => { - // Write your code here + for (const [key, value] of Object.entries(process.env)) { + if(key.startsWith('RSS_')) { + console.log(`${key}=${value}`); + } + } }; parseEnv(); \ No newline at end of file From fefad82e0346c5014549022c44634e03af27f80d Mon Sep 17 00:00:00 2001 From: Natallia Date: Tue, 30 Jan 2024 00:49:45 +0100 Subject: [PATCH 3/8] To ES6 Modules Practice --- src/modules/{cjsToEsm.cjs => esm.mjs} | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) rename src/modules/{cjsToEsm.cjs => esm.mjs} (58%) diff --git a/src/modules/cjsToEsm.cjs b/src/modules/esm.mjs similarity index 58% rename from src/modules/cjsToEsm.cjs rename to src/modules/esm.mjs index 8b7be2a48b..2eb740be48 100644 --- a/src/modules/cjsToEsm.cjs +++ b/src/modules/esm.mjs @@ -1,18 +1,22 @@ -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.js'; +import {fileURLToPath} from "url"; const random = Math.random(); +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); let unknownObject; if (random > 0.5) { - unknownObject = require('./files/a.json'); + ({default: unknownObject} = await import("./files/a.json", {assert: {type: "json"}})) } else { - unknownObject = require('./files/b.json'); + ({default: 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}"`); @@ -33,7 +37,7 @@ myServer.listen(PORT, () => { console.log('To terminate it, use Ctrl+C combination'); }); -module.exports = { +export { unknownObject, myServer, }; From 8e58afa7464b96226a42405bd6982ba4aa6ccefd Mon Sep 17 00:00:00 2001 From: Natallia Date: Tue, 30 Jan 2024 00:50:05 +0100 Subject: [PATCH 4/8] Hash practice --- src/hash/calcHash.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 450f8f72e2..31f0e6ba31 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,23 @@ +import crypto from 'crypto'; +import fs from 'fs'; +import {fileURLToPath} from "url"; +import path from "path"; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); +const hashFilePath = `${dirname}/files/fileToCalculateHashFor.txt`; + const calculateHash = async () => { - // Write your code here + const fd = fs.createReadStream(hashFilePath); + const hash = crypto.createHash('sha256'); + hash.setEncoding('hex'); + + fd.on('end', function () { + hash.end(); + console.log(hash.read()); + }); + + fd.pipe(hash); }; await calculateHash(); \ No newline at end of file From 4660075266245a373a894232d7a7e23489636278 Mon Sep 17 00:00:00 2001 From: Natallia Date: Tue, 30 Jan 2024 00:50:21 +0100 Subject: [PATCH 5/8] Streams practice --- src/streams/files/fileToWrite.txt | 2 ++ src/streams/read.js | 9 ++++++++- src/streams/transform.js | 10 +++++++++- src/streams/write.js | 10 +++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt index e69de29bb2..35b38b28ff 100644 --- a/src/streams/files/fileToWrite.txt +++ b/src/streams/files/fileToWrite.txt @@ -0,0 +1,2 @@ +test write +test yhdfbd diff --git a/src/streams/read.js b/src/streams/read.js index 52c78cc6ee..ef8ca4d30a 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,12 @@ +import fs from 'fs'; +import path from 'path'; +import {fileURLToPath} from 'url'; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); +const filePath = `${dirname}/files/fileToRead.txt`; const read = async () => { - // Write your code here + fs.createReadStream(filePath).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..26f1bcffda 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,13 @@ +import { Transform } from 'stream'; + +const reverseTransform = new Transform({ + transform(chunk, encoding, callback) { + this.push(chunk.toString().split("").reverse().join("")); + callback(); + } +}) const transform = async () => { - // Write your code here + process.stdin.pipe(reverseTransform).pipe(process.stdout); }; await transform(); \ No newline at end of file diff --git a/src/streams/write.js b/src/streams/write.js index fc917160a2..782d3c16cd 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,13 @@ +import fs from 'fs'; +import path from 'path'; +import {fileURLToPath} from 'url'; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); +const filePath = `${dirname}/files/fileToWrite.txt`; const write = async () => { - // Write your code here + const file = fs.createWriteStream(filePath, {flags: 'a'}); + process.stdin.pipe(file); }; await write(); \ No newline at end of file From 1b3a3cd15e5b59e42756e4ae16a4d54e1a68b8d1 Mon Sep 17 00:00:00 2001 From: Natallia Date: Tue, 30 Jan 2024 00:51:05 +0100 Subject: [PATCH 6/8] Zlib practice --- src/zip/compress.js | 16 +++++++++++++++- src/zip/decompress.js | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/zip/compress.js b/src/zip/compress.js index bb328f43c6..7f8000d769 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,19 @@ +import {createGzip} from 'zlib'; +import {pipeline} from 'stream/promises'; +import {createReadStream, createWriteStream} from 'fs'; +import {fileURLToPath} from "url"; +import path from "path"; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); +const compressFile = `${dirname}/files/fileToCompress.txt`; +const compressedFile = `${dirname}/files/archive.gz`; + const compress = async () => { - // Write your code here + const gzip = createGzip(); + const source = createReadStream(compressFile); + const destination = createWriteStream(compressedFile); + await pipeline(source, gzip, destination); }; await compress(); \ No newline at end of file diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 69f6c345f8..69fef822cf 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,19 @@ +import {createGunzip} from 'zlib'; +import {pipeline} from 'stream/promises'; +import {createReadStream, createWriteStream} from 'fs'; +import {fileURLToPath} from "url"; +import path from "path"; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); +const decompressedFile = `${dirname}/files/fileToDecompress.txt`; +const compressedFile = `${dirname}/files/archive.gz`; + const decompress = async () => { - // Write your code here + const gzip = createGunzip(); + const source = createReadStream(compressedFile); + const destination = createWriteStream(decompressedFile); + await pipeline(source, gzip, destination); }; await decompress(); \ No newline at end of file From bdbbf64530ac8f2a4fbccd30d0ed5a42756bff4c Mon Sep 17 00:00:00 2001 From: Natallia Date: Tue, 30 Jan 2024 00:51:24 +0100 Subject: [PATCH 7/8] Worker Threads practice --- src/wt/main.js | 23 +++++++++++++++++++++-- src/wt/worker.js | 3 ++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/wt/main.js b/src/wt/main.js index 37d80484ec..08642bbb33 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,24 @@ +import {cpus} from 'os'; +import {fileURLToPath} from "url"; +import path from "path"; +import {Worker} from 'worker_threads'; + + +const filename = fileURLToPath(import.meta.url); +const workerPath = `${path.dirname(filename)}/worker.js`; const performCalculations = async () => { - // Write your code here -}; + const cpuNumber = cpus().length; + const workersArray = []; + for (let i = 0; i < cpuNumber; i++) { + const promise = new Promise((resolve) => { + const worker = new Worker(workerPath, {workerData: i + 10}); + worker.on('message', (res) => resolve({status: 'resolved', data: res})); + worker.on('error', () => resolve({status: 'error', data: null})); + }); + workersArray.push(promise); + } + const resul = await Promise.all(workersArray); + console.log(resul); +} await performCalculations(); \ No newline at end of file diff --git a/src/wt/worker.js b/src/wt/worker.js index 441b2154f8..e117eae3bb 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,9 @@ +import {workerData, parentPort} 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 From 94bd08ae5e047421b95273537e6b0dc9d8d80f98 Mon Sep 17 00:00:00 2001 From: Natallia Date: Tue, 30 Jan 2024 00:51:40 +0100 Subject: [PATCH 8/8] Child Processes practice --- src/cp/cp.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/cp/cp.js b/src/cp/cp.js index 4a87b98c55..5dd31e4216 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,15 @@ +import {fork} from 'child_process'; +import {fileURLToPath} from "url"; +import path from "path"; + +const filename = fileURLToPath(import.meta.url); +const dirname = path.dirname(filename); +const scriptPath = `${dirname}/files/script.js`; const spawnChildProcess = async (args) => { - // Write your code here + const childProcess = fork(scriptPath, args, {silent: true}); + process.stdin.pipe(childProcess.stdin); + childProcess.stdout.pipe(process.stdout); }; // Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess( ['someArgument1', 'someArgument2'] );