From 93f18123d1c35bd42ddc8c6f41ee30490c4f0c5f Mon Sep 17 00:00:00 2001 From: Nikita Date: Mon, 22 Jan 2024 22:11:53 +0300 Subject: [PATCH 01/21] feat: implement fs/create --- src/fs/create.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/fs/create.js b/src/fs/create.js index 8d18cf9fc2..2a0a17fc96 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,20 @@ +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 From 5be4457921cf1ac6230f17430a8bcd89ac73894c Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 18:46:09 +0300 Subject: [PATCH 02/21] feat: fs:copy --- .gitignore | 2 ++ src/fs/copy.js | 21 +++++++++++++++++++-- src/fs/create.js | 7 +++---- src/fs/files/fresh.txt | 1 + 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 .gitignore create mode 100644 src/fs/files/fresh.txt 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/src/fs/copy.js b/src/fs/copy.js index bd17fe3991..7261bd8037 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,22 @@ +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'); + } + + fs.mkdirSync(destinationPath); + + + fs.cpSync(sourcePath, destinationPath, {recursive: true}); +} await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 2a0a17fc96..5062eb8899 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -2,7 +2,6 @@ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; - const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -13,8 +12,8 @@ const create = async () => { if (fs.existsSync(filePath)) { throw new Error('FS operation failed'); } - - await fs.promises.writeFile(filePath, fileContent, { encoding: 'utf-8' }); + + await fs.promises.writeFile(filePath, fileContent, { encoding: 'utf-8' }); }; -await create(); \ No newline at end of file +await create(); 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 From 58414dcb857fc552cb1f07d3e2d0b880bfcf3ae1 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 19:03:30 +0300 Subject: [PATCH 03/21] feat: fs:rename --- src/fs/rename.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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(); From af3c69b76979561876e3f2fe80ea86a4b101154d Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 19:06:37 +0300 Subject: [PATCH 04/21] feat: fs:delete --- src/fs/delete.js | 17 +++++++++++++++-- src/fs/files/fileToRemove.txt | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) 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 From e39a3368fa3c9533470d61845c485db4bc9f7ed6 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 19:18:25 +0300 Subject: [PATCH 05/21] feat: implement fs:list --- src/fs/list.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/fs/list.js b/src/fs/list.js index c0a83dea15..95a9e4bc68 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(); From 5061379d530dcc2ad4e164c93eda7b6aa3be0db5 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 19:19:58 +0300 Subject: [PATCH 06/21] refactor: add semilons for fs:list --- src/fs/list.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fs/list.js b/src/fs/list.js index 95a9e4bc68..91b97aa999 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -2,8 +2,8 @@ import fs from 'fs' import path from 'path' import {fileURLToPath} from 'url' -const __filename = fileURLToPath(import.meta.url) -const __dirname = path.dirname(__filename) +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const list = async () => { const folderPath = path.resolve(__dirname, './files/') From cb0d88b5975ded7997ef627b4c364926b54e32ec Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 19:23:10 +0300 Subject: [PATCH 07/21] feat: fs:read --- src/fs/read.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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(); From 2b4a59ec24ec1cb5c0c4e63bf8ff18cdca7537a0 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 19:29:34 +0300 Subject: [PATCH 08/21] feat: cli:env --- src/cli/env.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) 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() From 9d2124a064b80d5c679c0bbb305ab74581977f57 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 19:36:12 +0300 Subject: [PATCH 09/21] feat: implement cli:args --- src/cli/args.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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(); From 9630184b71e8732fd1f7e6bf73f1ea35d37fc708 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 20:11:14 +0300 Subject: [PATCH 10/21] feat: modules --- src/modules/esm.mjs | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/modules/esm.mjs diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs new file mode 100644 index 0000000000..8ceb7a3a94 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,46 @@ +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 +} + From ce0422f455728b051c34d247e5fd2c71019e2778 Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 23 Jan 2024 20:30:34 +0300 Subject: [PATCH 11/21] feat: hash --- src/hash/calcHash.js | 32 ++++++++++++++++++++++++-- src/modules/esm.mjs | 55 ++++++++++++++++++++++---------------------- 2 files changed, 57 insertions(+), 30 deletions(-) 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 index 8ceb7a3a94..d448e0295f 100644 --- a/src/modules/esm.mjs +++ b/src/modules/esm.mjs @@ -1,46 +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' +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 __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); -const random = Math.random() +const random = Math.random(); -let unknownObject +let unknownObject; -const require = createRequire(import.meta.url) +const require = createRequire(import.meta.url); if (random > 0.5) { - unknownObject = require('./files/a.json') + unknownObject = require('./files/a.json'); } else { - unknownObject = require('./files/b.json') + unknownObject = require('./files/b.json'); } -console.log(`Release ${release()}`) -console.log(`Version ${version()}`) -console.log(`Path segment separator is "${path.sep}"`) +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 ${__filename}`); +console.log(`Path to current directory is ${__dirname}`); const myServer = createServerHttp((_, res) => { - res.end('Request accepted') -}) + res.end('Request accepted'); +}); -const PORT = 3000 +const PORT = 3000; -console.log(unknownObject) +console.log(unknownObject); myServer.listen(PORT, () => { - console.log(`Server is listening on port ${PORT}`) - console.log('To terminate it, use Ctrl+C combination') -}) + console.log(`Server is listening on port ${PORT}`); + console.log('To terminate it, use Ctrl+C combination'); +}); export { - unknownObject, - myServer -} + unknownObject, myServer +}; From f0ba1dd3d0eb61158bd249ac419c5dad2b6ed5bb Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 24 Jan 2024 20:12:37 +0300 Subject: [PATCH 12/21] feat: implement streams:read --- src/streams/read.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) 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(); From 05d935e1fc3554b4a2e24242317348534d74b4ca Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 24 Jan 2024 20:34:39 +0300 Subject: [PATCH 13/21] feat: streams:write --- output.txt | 0 src/streams/files/fileToWrite.txt | 6 ++++++ src/streams/write.js | 25 +++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 output.txt diff --git a/output.txt b/output.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt index e69de29bb2..bfd771358f 100644 --- a/src/streams/files/fileToWrite.txt +++ b/src/streams/files/fileToWrite.txt @@ -0,0 +1,6 @@ +multiline +text +example +from process.stdin +do not forget to push Enter after last +console.log('Write some text and terminate the process (Ctrl+C) — text will appear in file'); diff --git a/src/streams/write.js b/src/streams/write.js index fc917160a2..69c3fafde9 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,26 @@ +import {createHash} from 'crypto'; +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 fileStream = fs.createWriteStream(filePath, {encoding: 'utf-8'}); + + console.log('Write some text, push Enter after last symbol and terminate the process (Ctrl+C) — text will appear in file'); + + pipeline(process.stdin, fileStream, (err) => { + if (err) { + console.error('Pipeline failed.', err); + } else { + console.log('Pipeline succeeded.'); + } + } + ); }; -await write(); \ No newline at end of file +await write(); From 3cab486cd7911219cc351a39b694bbbf3bd81fce Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 24 Jan 2024 20:43:41 +0300 Subject: [PATCH 14/21] feat: implement streams: transform --- src/streams/transform.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/streams/transform.js b/src/streams/transform.js index 315fc6597f..8f9b57b441 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,16 @@ +import streams from 'stream'; + + const transform = async () => { - // Write your code here + const reverseTransform = new streams.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(); From dcda1a2c0aeed6198e3ab369e1e89520044d6fff Mon Sep 17 00:00:00 2001 From: Nikita Date: Wed, 24 Jan 2024 20:51:27 +0300 Subject: [PATCH 15/21] feat: implement zip:compress --- src/streams/write.js | 1 - src/zip/compress.js | 24 ++++++++++++++++++++++-- src/zip/files/archive.gz | Bin 0 -> 32 bytes 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/zip/files/archive.gz diff --git a/src/streams/write.js b/src/streams/write.js index 69c3fafde9..8ff847648a 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,4 +1,3 @@ -import {createHash} from 'crypto'; import fs from 'fs'; import path from 'path'; import {fileURLToPath} from 'url'; 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/files/archive.gz b/src/zip/files/archive.gz new file mode 100644 index 0000000000000000000000000000000000000000..b163d0003a9d024764ddd2e949d977dc65af5b03 GIT binary patch literal 32 jcmb2|=3oE=uHtj&bv3+n^} Date: Wed, 24 Jan 2024 21:04:10 +0300 Subject: [PATCH 16/21] feat: add compression and decompression (zlib) --- src/zip/decompress.js | 25 +++++++++++++++++++++++-- src/zip/files/archive.gz | Bin 32 -> 35 bytes src/zip/files/fileToCompress.txt | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) 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 index b163d0003a9d024764ddd2e949d977dc65af5b03..661bf7c1a95a178768f02d82849a05ef0a15105d 100644 GIT binary patch delta 18 ZcmY#To*>2>@Z>2IgU~c|Z*!m#RT{A?Zu1_l5f&I64A 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 ! From e6751133064543067c83e7d2a02d6bf3ef1af02d Mon Sep 17 00:00:00 2001 From: Nikita Date: Thu, 25 Jan 2024 20:50:24 +0300 Subject: [PATCH 17/21] feat: implement workers to count fibonacci in parallel --- src/wt/main.js | 39 +++++++++++++++++++++++++++++++++++++-- src/wt/worker.js | 7 +++++-- 2 files changed, 42 insertions(+), 4 deletions(-) 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(); From 1ee9c88a1e76a464cbd27926c71f2b8bfc087852 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 26 Jan 2024 17:48:00 +0300 Subject: [PATCH 18/21] feat: cp --- src/cp/cp.js | 17 ++++++++++++++--- src/fs/copy.js | 2 -- 2 files changed, 14 insertions(+), 5 deletions(-) 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 7261bd8037..e5e1ca0c23 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -14,8 +14,6 @@ const copy = async () => { } fs.mkdirSync(destinationPath); - - fs.cpSync(sourcePath, destinationPath, {recursive: true}); } From 49b84ab01b8ec75fa8c0e58269a84c8470a9a22f Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 26 Jan 2024 17:51:20 +0300 Subject: [PATCH 19/21] fix: change copy API to async --- src/fs/copy.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index e5e1ca0c23..35f0d6a3fe 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -13,8 +13,13 @@ const copy = async () => { throw new Error('FS operation failed'); } - fs.mkdirSync(destinationPath); - fs.cpSync(sourcePath, destinationPath, {recursive: true}); + await new Promise(resolve => { + fs.mkdir(destinationPath, {}, resolve); + }); + + await new Promise(resolve => { + fs.cp(sourcePath, destinationPath, { recursive: true}, resolve); + }); } await copy(); From 4aadc50f76c8c8a7fb0dc3a3a1a29fd3794dae8f Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 26 Jan 2024 17:53:43 +0300 Subject: [PATCH 20/21] fix: use pipe instead of pipeline in streams:write --- src/streams/files/fileToWrite.txt | 7 +------ src/streams/write.js | 13 ++----------- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt index bfd771358f..9f358a4add 100644 --- a/src/streams/files/fileToWrite.txt +++ b/src/streams/files/fileToWrite.txt @@ -1,6 +1 @@ -multiline -text -example -from process.stdin -do not forget to push Enter after last -console.log('Write some text and terminate the process (Ctrl+C) — text will appear in file'); +123456 diff --git a/src/streams/write.js b/src/streams/write.js index 8ff847648a..6986090016 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -8,18 +8,9 @@ const __dirname = path.dirname(__filename); const write = async () => { const filePath = path.resolve(__dirname, './files/fileToWrite.txt'); - const fileStream = fs.createWriteStream(filePath, {encoding: 'utf-8'}); + const fileWriteStream = fs.createWriteStream(filePath, {encoding: 'utf-8'}); - console.log('Write some text, push Enter after last symbol and terminate the process (Ctrl+C) — text will appear in file'); - - pipeline(process.stdin, fileStream, (err) => { - if (err) { - console.error('Pipeline failed.', err); - } else { - console.log('Pipeline succeeded.'); - } - } - ); + process.stdin.pipe(fileWriteStream); }; await write(); From 1f8a4a67cf4ad681b8aa674acd75f625b853fbe2 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 26 Jan 2024 17:56:42 +0300 Subject: [PATCH 21/21] refactor: streams:transform --- src/streams/files/fileToWrite.txt | 1 - src/streams/transform.js | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt index 9f358a4add..e69de29bb2 100644 --- a/src/streams/files/fileToWrite.txt +++ b/src/streams/files/fileToWrite.txt @@ -1 +0,0 @@ -123456 diff --git a/src/streams/transform.js b/src/streams/transform.js index 8f9b57b441..6b31a8e51a 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,8 +1,8 @@ -import streams from 'stream'; +import {Transform} from 'stream'; const transform = async () => { - const reverseTransform = new streams.Transform({ + const reverseTransform = new Transform({ transform(chunk, _, callback) { this.push([...chunk.toString()].reverse().join('')); callback();