From e98ca76b5cffcbf770a69f143f05ef7736e0147e Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Sat, 17 Jun 2023 21:29:52 +0400 Subject: [PATCH 01/20] Implemented function that creates new file fresh.txt --- src/fs/create.js | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/fs/create.js b/src/fs/create.js index 8d18cf9fc2..7e48306754 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,44 @@ +// implement function that creates new file fresh.txt with content "I am fresh and young" +// inside of the files folder (if file already exists Error with message FS operation failed must be thrown) +import fs from 'fs'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const create = async () => { - // Write your code here + const filesDir = path.join(__dirname, 'files'); + fs.stat(filesDir, (err, stats) => { + if (err && err.code === 'ENOENT') { + throw new Error(filesDir + " does not exist."); + } + if (!stats.isDirectory()) { + throw new Error(filesDir + " is not a directory."); + } + }); + fs.access(filesDir, fs.constants.W_OK, (err) => { + if (err) { + throw new Error(filesDir + " is not writable."); + } + }); + + const freshFile = path.join(filesDir, 'fresh.txt'); + fs.access(freshFile, fs.constants.F_OK, (err) => { + if (err === null) { + throw new Error("FS operation failed"); + } + }); + + const freshContent = 'I am fresh and young'; + fs.writeFile(freshFile, freshContent, (err) => { + if (err) { + throw err; + } + }); }; -await create(); \ No newline at end of file +await create(); From daddcb7b4d4e77fa7919d6851e6d12ef629919db Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Sun, 18 Jun 2023 00:46:55 +0400 Subject: [PATCH 02/20] Implemented function that copies folder 'files' files with all its content into folder 'files_copy' at the same level. --- src/fs/copy.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index bd17fe3991..d13f8e5a80 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,56 @@ +// copy.js - implement function that copies folder files files with all its content into folder files_copy +// at the same level (if files folder doesn't exists or files_copy has already been created Error with +// message FS operation failed must be thrown) +import fs from 'fs'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const copy = async () => { - // Write your code here + const filesDir = path.join(__dirname, 'files'); + fs.stat(filesDir, (err, stats) => { + if (err && err.code === 'ENOENT') { + throw new Error("FS operation failed"); + } + if (!stats.isDirectory()) { + throw new Error(filesDir + " is not a directory."); + } + }); + fs.access(filesDir, fs.constants.R_OK, (err) => { + if (err) { + throw new Error(filesDir + " is not readable."); + } + }); + + const filesCopyDir = path.join(__dirname, 'files_copy'); + fs.access(filesCopyDir, fs.constants.F_OK, (err) => { + if (err === null) { + throw new Error("FS operation failed"); + } + }); + + fs.mkdir(filesCopyDir, { recursive: true }, (err) => { + if (err) throw err; + }); + + fs.readdir(filesDir, (err, files) => { + if (err) { + console.error('Error reading directory:', err); + return; + } + files.forEach((file) => { + const srcPath = path.join(filesDir, file); + const dstPath = path.join(filesCopyDir, file); + fs.copyFile(srcPath, dstPath, (err) => { + if (err) throw err; + }); + }); + }); }; await copy(); From 9430f5177e832499863126a01a0677da0234b7f9 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Sun, 18 Jun 2023 20:07:26 +0400 Subject: [PATCH 03/20] Implemented function that renames file. --- src/fs/rename.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/fs/rename.js b/src/fs/rename.js index 2bb99ecdb5..5bba98c1cc 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,39 @@ +// rename.js - implement function that renames file wrongFilename.txt to properFilename +// with extension .md (if there's no file wrongFilename.txt or properFilename.md already +// exists Error with message "FS operation failed" must be thrown) + +import fs from 'fs'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const rename = async () => { - // Write your code here + const filesDir = path.join(__dirname, 'files'); + + const wrongFile = path.join(filesDir, 'wrongFilename.txt'); + fs.access(wrongFile, fs.constants.F_OK, (err) => { + if (err) { + throw new Error("FS operation failed"); + } + }); + + const properFile = path.join(filesDir, 'properFilename.md'); + fs.access(properFile, fs.constants.F_OK, (err) => { + if (err === null) { + throw new Error("FS operation failed"); + } + }); + + fs.rename(wrongFile, properFile, (err) => { + if (err) { + throw new Error("FS operation failed"); + } + }); }; -await rename(); \ No newline at end of file +await rename(); From 8ff6abab1f012fefec68f06f5337c1f9f7354149 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Sun, 18 Jun 2023 21:28:37 +0400 Subject: [PATCH 04/20] Implemented function that deletes file. --- src/fs/delete.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/fs/delete.js b/src/fs/delete.js index 4718dbc4c5..6a40cf6d54 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,32 @@ +// delete.js - implement function that deletes file fileToRemove.txt +// (if there's no file fileToRemove.txt Error with message FS operation failed must be thrown) + +import fs from 'fs'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const remove = async () => { - // Write your code here + const filesDir = path.join(__dirname, 'files'); + + const removedFile = path.join(filesDir, 'fileToRemove.txt'); + fs.access(removedFile, fs.constants.F_OK, (err) => { + if (err) { + throw new Error("FS operation failed"); + } + }); + + fs.unlink(removedFile, (err) => { + if (err) { + throw err; + } + }); + }; -await remove(); \ No newline at end of file +await remove(); From f738c5c4524d8231896b8b55c4e2b59d3939996f Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Sun, 18 Jun 2023 23:29:13 +0400 Subject: [PATCH 05/20] Implemented function that prints array of filenames from "files" folder. --- src/fs/list.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/fs/list.js b/src/fs/list.js index c0a83dea15..c135a91458 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,29 @@ +// list.js - implement function that prints all array of filenames from +// files folder into console (if files folder doesn't exists Error with +// message FS operation failed must be thrown) +import fs from 'fs'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const list = async () => { - // Write your code here + const filesDir = path.join(__dirname, 'files'); + fs.stat(filesDir, (err, stats) => { + if (err && err.code === 'ENOENT') { + throw new Error("FS operation failed"); + } + }); + fs.readdir(filesDir, (err, files) => { + if (err) { + throw new Error("FS operation failed"); + } + console.log(files); + }); }; await list(); \ No newline at end of file From 544a56eb341d9d313c0d25eb82bdeb35a5eb8489 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Mon, 19 Jun 2023 01:04:11 +0400 Subject: [PATCH 06/20] Implemented function that prints content of the fileToRead.txt into console. --- src/fs/read.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/fs/read.js b/src/fs/read.js index 52c78cc6ee..96016cd33c 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,31 @@ +// read.js - implement function that prints content of the fileToRead.txt into console +// (if there's no file fileToRead.txt Error with message FS operation failed must be thrown) +import fs from 'fs'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const read = async () => { - // Write your code here + const filesDir = path.join(__dirname, 'files'); + const readFile = path.join(filesDir, 'fileToRead.txt'); + + fs.stat(readFile, (err, stats) => { + if (err && err.code === 'ENOENT') { + throw new Error("FS operation failed"); + } + }); + + fs.readFile(readFile, 'utf8', (err, data) => { + if (err) { + throw new Error("FS operation failed"); + } + console.log(data); + }); }; -await read(); \ No newline at end of file +await read(); From cdbbbea7c21e656dc53f826f7c3f2f05f1d1fa23 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Mon, 19 Jun 2023 01:20:58 +0400 Subject: [PATCH 07/20] Implemented function that parses environment variables with prefix RSS_ and prints them to the console. --- src/cli/env.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/cli/env.js b/src/cli/env.js index fe4aa4a8df..a104732fbb 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,15 @@ +// env.js - implement function that parses environment variables with prefix RSS_ +// and prints them to the console in the format "RSS_name1=value1; RSS_name2=value2" + const parseEnv = () => { - // Write your code here + const re = /^RSS_/; + const rssvars = []; + Object.keys(process.env).forEach((key) => { + if (re.test(key)) { + rssvars.push(`${key}=${process.env[key]}`); + } + }); + console.log(rssvars.join('; ')); }; -parseEnv(); \ No newline at end of file +parseEnv(); From 92159e241cd690462f4756adaf91a82d8878b618 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Mon, 19 Jun 2023 13:05:52 +0400 Subject: [PATCH 08/20] Implemented function that parses command line arguments. --- src/cli/args.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cli/args.js b/src/cli/args.js index 8283f7f7aa..972db14cdd 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,18 @@ +// args.js - implement function that parses command line arguments +// (given in format --propName value --prop2Name value2, you don't need to validate it) +// and prints them to the console in the format propName is value, prop2Name is value2 const parseArgs = () => { - // Write your code here + const re = /^--.+/; + const res = []; + const args = process.argv; + for (let i = 0; i < args.length; i++) { + if (i + 1 < args.length && re.test(args[i]) && !re.test(args[i + 1])) { + const name = args[i].substring(2); + const val = args[i + 1]; + res.push(name + ' is ' + val); + } + } + console.log(res.join(', ')); }; -parseArgs(); \ No newline at end of file +parseArgs(); From 4bab4abe8e29197a4f754967b6f462ec779d3c2f Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Mon, 19 Jun 2023 17:05:16 +0400 Subject: [PATCH 09/20] The module was rewritten according to ECMAScript notation and renamed to esm.mjs. --- src/modules/cjsToEsm.cjs | 40 --------------------------- src/modules/esm.mjs | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 40 deletions(-) delete mode 100644 src/modules/cjsToEsm.cjs create mode 100644 src/modules/esm.mjs diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.cjs deleted file mode 100644 index 8b7be2a48b..0000000000 --- a/src/modules/cjsToEsm.cjs +++ /dev/null @@ -1,40 +0,0 @@ -const path = require('path'); -const { release, version } = require('os'); -const { createServer: createServerHttp } = require('http'); -require('./files/c'); - -const random = Math.random(); - -let unknownObject; - -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'); -}); - -module.exports = { - unknownObject, - myServer, -}; - diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs new file mode 100644 index 0000000000..213604d894 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,60 @@ +// rewrite it to it's equivalent in ECMAScript notation (and rename it to esm.mjs) + +import path from 'path'; +import { release, version } from 'os'; +import { createServer as createServerHttp } from 'http'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +import { readFile } from 'fs/promises'; + +import './files/c.js'; + + +const importJson = async (fileName) => { + const data = await readFile(fileName, { encoding: 'utf8' }); + return JSON.parse(data); +}; + +const random = Math.random(); + +let unknownObject; +let fileName; + +if (random > 0.5) { + fileName = path.join(__dirname, '/files/a.json'); +} else { + fileName = path.join(__dirname, '/files/b.json'); +} + +unknownObject = await importJson(fileName); + +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, +}; + From d74e1e3318418dda1b8b10a40e36bcf0d897d6d9 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Mon, 19 Jun 2023 19:16:12 +0400 Subject: [PATCH 10/20] Implemented function that calculates SHA256 hash for file --- src/hash/calcHash.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 450f8f72e2..c6c64a952f 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,29 @@ +// implement function that calculates SHA256 hash for file fileToCalculateHashFor.txt +// and logs it into console as hex +import fs from 'fs'; +import path from 'path'; +import crypto from 'crypto'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const calculateHash = async () => { - // Write your code here + const hash = crypto.createHash('sha256'); + const fileName = path.join(__dirname, 'files/fileToCalculateHashFor.txt'); + + const fileStream = fs.createReadStream(fileName); + + fileStream.on('data', (data) => { + hash.update(data); + }); + + fileStream.on('end', () => { + console.log(hash.digest('hex')); + }); + }; -await calculateHash(); \ No newline at end of file +await calculateHash(); From 4fabc6d194e454f79d51dec061ef9679c9c91741 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Mon, 19 Jun 2023 21:11:04 +0400 Subject: [PATCH 11/20] Implemeted functions that work withj streams. --- src/streams/read.js | 19 +++++++++++++++++-- src/streams/transform.js | 24 ++++++++++++++++++++++-- src/streams/write.js | 19 +++++++++++++++++-- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/streams/read.js b/src/streams/read.js index 52c78cc6ee..0a2b390f5a 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,20 @@ +// implement function that reads file fileToRead.txt content using +// Readable Stream and prints it's content into process.stdout +import fs from 'fs'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const read = async () => { - // Write your code here + const fileName = path.join(__dirname, 'files/fileToRead.txt'); + + const inputStream = fs.createReadStream(fileName); + + inputStream.pipe(process.stdout); }; -await read(); \ No newline at end of file +await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 315fc6597f..4010f41775 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,25 @@ +// implement function that reads data from process.stdin, reverses text +// using Transform Stream and then writes it into process.stdout +import fs from 'fs'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import { Transform } from 'stream'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +class ReverseStream extends Transform { + _transform(chunk, encoding, callback) { + const transformedChunk = chunk.toString().split('').reverse().join(''); + this.push(transformedChunk); + callback(); + } +} + const transform = async () => { - // Write your code here + const reverseStream = new ReverseStream(); + process.stdin.pipe(reverseStream).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..bb3ee74b6d 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,20 @@ +// implement function that writes process.stdin data into file fileToWrite.txt +// content using Writable Stream +import fs from 'fs'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const write = async () => { - // Write your code here + const fileName = path.join(__dirname, 'files/fileToWrite.txt'); + + const outputStream = fs.createWriteStream(fileName); + + process.stdin.pipe(outputStream); }; -await write(); \ No newline at end of file +await write(); From a4c5605e3197f46245da62a00be08639791450cb Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Mon, 19 Jun 2023 22:05:25 +0400 Subject: [PATCH 12/20] Implemented functions that compresses/decompresses file. --- src/zip/compress.js | 23 +++++++++++++++++++++-- src/zip/decompress.js | 24 ++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/zip/compress.js b/src/zip/compress.js index bb328f43c6..82a64b25c0 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,24 @@ +// implement function that compresses file fileToCompress.txt to archive.gz using zlib and Streams API + +import fs from 'fs'; +import path from 'path'; +import zlib from 'zlib'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const compress = async () => { - // Write your code here + const fileName = path.join(__dirname, 'files/fileToCompress.txt'); + const archName = path.join(__dirname, 'files/archive.gz'); + + const inputStream = fs.createReadStream(fileName); + const gzipStream = zlib.createGzip(); + const outputStream = fs.createWriteStream(archName); + + inputStream.pipe(gzipStream).pipe(outputStream); }; -await compress(); \ No newline at end of file +await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 69f6c345f8..e6d8338fdc 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,25 @@ +// implement function that decompresses archive.gz back to the fileToCompress.txt +// with same content as before compression using zlib and Streams API + +import fs from 'fs'; +import path from 'path'; +import zlib from 'zlib'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const decompress = async () => { - // Write your code here + const archName = path.join(__dirname, 'files/archive.gz'); + const fileName = path.join(__dirname, 'files/fileToCompress.txt'); + + const inputStream = fs.createReadStream(archName); + const gzipStream = zlib.createGunzip(); + const outputStream = fs.createWriteStream(fileName); + + inputStream.pipe(gzipStream).pipe(outputStream); }; -await decompress(); \ No newline at end of file +await decompress(); From 946d3715d0a8ac92173a396d09f15913f8bc62af Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Tue, 20 Jun 2023 01:09:31 +0400 Subject: [PATCH 13/20] work with threads --- src/wt/main.js | 45 +++++++++++++++++++++++++++++++++++++++++++-- src/wt/worker.js | 11 ++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/wt/main.js b/src/wt/main.js index 37d80484ec..52ccde94e1 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,46 @@ +// implement function that creates number of worker threads (equal to the number +// of host machine logical CPU cores) from file worker.js + +import os from 'os'; +import path, { resolve } from 'path'; +import { Worker } from 'worker_threads'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const performCalculations = async () => { - // Write your code here + const workerFile = path.join(__dirname, 'worker.js'); + const cpus = os.cpus().length; + const results = []; + const promises = []; + const wid2idx = {}; + for (let i = 0; i < cpus; i++) { + const promise = new Promise((resolve, reject) => { + const worker = new Worker(workerFile); + wid2idx[worker.threadId] = i; + worker.postMessage(10 + i); + worker.on('message', (data) => { + results[wid2idx[worker.threadId]] = { 'status': 'resolved', 'data': data }; + resolve(); + }); + worker.on('error', (eror) => { + results[wid2idx[worker.threadId]] = { 'status': 'resolved', 'data': data }; + reject(); + }); + }); + promises.push(promise); + } + + Promise.all(promises) + .then(() => { + console.log(results); + }) + .catch(() => { + 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..3a3f3acef6 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,17 @@ +// extend given function to work with data received from main thread and implement +// function which sends result of the computation to the main thread + +import { 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.on('message', (data) => { + parentPort.postMessage(nthFibonacci(data)); + parentPort.close(); + }); }; -sendResult(); \ No newline at end of file +sendResult(); From 48a498235e3eb4f5bbdb01c627e05149f6bb0eb4 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Tue, 20 Jun 2023 01:48:34 +0400 Subject: [PATCH 14/20] Work with child process. --- src/cp/cp.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/cp/cp.js b/src/cp/cp.js index 4a87b98c55..54d1aa4811 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,22 @@ +// implement function spawnChildProcess that receives array of arguments args and creates child process from file +import { spawn } from 'child_process'; +import path from 'path'; + +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const spawnChildProcess = async (args) => { - // Write your code here + const nodejs = process.argv[0]; + const scriptFile = path.join(__dirname, 'files', 'script.js'); + args.unshift(scriptFile); + + const childProcess = spawn(nodejs, 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(['--opt1', 'val1', '--opt2', 'val2']); From d7e93db698c7a82f584194391dd01be8534c6c76 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Tue, 20 Jun 2023 02:51:27 +0400 Subject: [PATCH 15/20] fix: copy.js --- src/fs/copy.js | 49 +++++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index d13f8e5a80..d0b50f907a 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -13,41 +13,30 @@ const __dirname = dirname(__filename); const copy = async () => { const filesDir = path.join(__dirname, 'files'); + const filesCopyDir = path.join(__dirname, 'files_copy'); fs.stat(filesDir, (err, stats) => { if (err && err.code === 'ENOENT') { throw new Error("FS operation failed"); } - if (!stats.isDirectory()) { - throw new Error(filesDir + " is not a directory."); - } - }); - fs.access(filesDir, fs.constants.R_OK, (err) => { - if (err) { - throw new Error(filesDir + " is not readable."); - } - }); - - const filesCopyDir = path.join(__dirname, 'files_copy'); - fs.access(filesCopyDir, fs.constants.F_OK, (err) => { - if (err === null) { - throw new Error("FS operation failed"); - } - }); - - fs.mkdir(filesCopyDir, { recursive: true }, (err) => { - if (err) throw err; - }); - - fs.readdir(filesDir, (err, files) => { - if (err) { - console.error('Error reading directory:', err); - return; - } - files.forEach((file) => { - const srcPath = path.join(filesDir, file); - const dstPath = path.join(filesCopyDir, file); - fs.copyFile(srcPath, dstPath, (err) => { + fs.access(filesCopyDir, fs.constants.F_OK, (err) => { + if (err === null) { + throw new Error("FS operation failed"); + } + fs.mkdir(filesCopyDir, { recursive: true }, (err) => { if (err) throw err; + fs.readdir(filesDir, (err, files) => { + if (err) { + console.error('Error reading directory:', err); + return; + } + files.forEach((file) => { + const srcPath = path.join(filesDir, file); + const dstPath = path.join(filesCopyDir, file); + fs.copyFile(srcPath, dstPath, (err) => { + if (err) throw err; + }); + }); + }); }); }); }); From e27094f25b7b1fdc96c364f9d45b64a53cc0386d Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Tue, 20 Jun 2023 02:54:10 +0400 Subject: [PATCH 16/20] fix: create.js --- src/fs/create.js | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/fs/create.js b/src/fs/create.js index 7e48306754..f91b17b1a2 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -12,33 +12,20 @@ const __dirname = dirname(__filename); const create = async () => { const filesDir = path.join(__dirname, 'files'); - fs.stat(filesDir, (err, stats) => { - if (err && err.code === 'ENOENT') { - throw new Error(filesDir + " does not exist."); - } - if (!stats.isDirectory()) { - throw new Error(filesDir + " is not a directory."); - } - }); - fs.access(filesDir, fs.constants.W_OK, (err) => { - if (err) { - throw new Error(filesDir + " is not writable."); - } - }); - const freshFile = path.join(filesDir, 'fresh.txt'); + fs.access(freshFile, fs.constants.F_OK, (err) => { if (err === null) { throw new Error("FS operation failed"); } + const freshContent = 'I am fresh and young'; + fs.writeFile(freshFile, freshContent, (err) => { + if (err) { + throw err; + } + }); }); - const freshContent = 'I am fresh and young'; - fs.writeFile(freshFile, freshContent, (err) => { - if (err) { - throw err; - } - }); }; await create(); From c6ba5ac8e7bc932a54a0bcd2949e12698217b2e1 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Tue, 20 Jun 2023 02:56:53 +0400 Subject: [PATCH 17/20] fixed: delete.js --- src/fs/delete.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/fs/delete.js b/src/fs/delete.js index 6a40cf6d54..ae4702bff2 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -13,20 +13,17 @@ const __dirname = dirname(__filename); const remove = async () => { const filesDir = path.join(__dirname, 'files'); - const removedFile = path.join(filesDir, 'fileToRemove.txt'); fs.access(removedFile, fs.constants.F_OK, (err) => { if (err) { throw new Error("FS operation failed"); } + fs.unlink(removedFile, (err) => { + if (err) { + throw err; + } + }); }); - - fs.unlink(removedFile, (err) => { - if (err) { - throw err; - } - }); - }; await remove(); From 23028d1eec04139eba5d0ea63552b7588402137d Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Tue, 20 Jun 2023 02:58:50 +0400 Subject: [PATCH 18/20] fix: list.js --- src/fs/list.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/fs/list.js b/src/fs/list.js index c135a91458..4b0c7a2944 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -17,12 +17,12 @@ const list = async () => { if (err && err.code === 'ENOENT') { throw new Error("FS operation failed"); } - }); - fs.readdir(filesDir, (err, files) => { - if (err) { - throw new Error("FS operation failed"); - } - console.log(files); + fs.readdir(filesDir, (err, files) => { + if (err) { + throw new Error("FS operation failed"); + } + console.log(files); + }); }); }; From aaf2e374a5390aab797149e4c2a20d2faf0d7844 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Tue, 20 Jun 2023 02:59:37 +0400 Subject: [PATCH 19/20] fix: read.js --- src/fs/read.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/fs/read.js b/src/fs/read.js index 96016cd33c..d0cc463ff6 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -18,14 +18,14 @@ const read = async () => { if (err && err.code === 'ENOENT') { throw new Error("FS operation failed"); } + fs.readFile(readFile, 'utf8', (err, data) => { + if (err) { + throw new Error("FS operation failed"); + } + console.log(data); + }); }); - fs.readFile(readFile, 'utf8', (err, data) => { - if (err) { - throw new Error("FS operation failed"); - } - console.log(data); - }); }; await read(); From a935f1b45bb653a159568c8933c67f46643c5f00 Mon Sep 17 00:00:00 2001 From: Dmitry Kostikov Date: Tue, 20 Jun 2023 03:01:59 +0400 Subject: [PATCH 20/20] fix: rename.js --- src/fs/rename.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/fs/rename.js b/src/fs/rename.js index 5bba98c1cc..ce3a4709f5 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -14,25 +14,22 @@ const __dirname = dirname(__filename); const rename = async () => { const filesDir = path.join(__dirname, 'files'); - const wrongFile = path.join(filesDir, 'wrongFilename.txt'); - fs.access(wrongFile, fs.constants.F_OK, (err) => { - if (err) { - throw new Error("FS operation failed"); - } - }); - const properFile = path.join(filesDir, 'properFilename.md'); - fs.access(properFile, fs.constants.F_OK, (err) => { - if (err === null) { - throw new Error("FS operation failed"); - } - }); - - fs.rename(wrongFile, properFile, (err) => { + fs.access(wrongFile, fs.constants.F_OK, (err) => { if (err) { throw new Error("FS operation failed"); } + fs.access(properFile, fs.constants.F_OK, (err) => { + if (err === null) { + throw new Error("FS operation failed"); + } + fs.rename(wrongFile, properFile, (err) => { + if (err) { + throw new Error("FS operation failed"); + } + }); + }); }); };