From 47ac60e38d3e69302bd2411e11c35318233cebd6 Mon Sep 17 00:00:00 2001 From: Slavashevich Date: Tue, 13 Jun 2023 12:52:11 +0300 Subject: [PATCH 1/8] File system (src/fs) --- src/fs/copy.js | 16 +++++++++++++++- src/fs/create.js | 15 ++++++++++++++- src/fs/delete.js | 15 ++++++++++++++- src/fs/list.js | 17 ++++++++++++++++- src/fs/read.js | 17 ++++++++++++++++- src/fs/rename.js | 16 +++++++++++++++- 6 files changed, 90 insertions(+), 6 deletions(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index bd17fe3991..13fdd0f5ff 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,19 @@ +import { existsSync } from 'fs'; +import { cp } from 'fs/promises'; +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 sourceFolder = `${__dirname}/files`; + const destinationFolder = `${__dirname}/files_copy`; + + if (!existsSync(sourceFolder) || existsSync(destinationFolder)) + throw new Error('FS operation failed'); + + await cp(sourceFolder, destinationFolder, { recursive: true }); }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 8d18cf9fc2..83228b1d28 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,18 @@ +import { existsSync } from 'fs'; +import { writeFile } from 'fs/promises'; +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 filePath = `${__dirname}/files/fresh.txt`; + + if (existsSync(filePath)) + throw new Error('FS operation failed'); + + await writeFile(filePath, 'I am fresh and young'); }; await create(); \ No newline at end of file diff --git a/src/fs/delete.js b/src/fs/delete.js index 4718dbc4c5..ffd9ccdb38 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,18 @@ +import { unlink } from 'fs/promises'; +import { existsSync } 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 deleteFilePath = `${__dirname}/files/fileToRemove.txt`; + + if (!existsSync(deleteFilePath)) + throw new Error('FS operation failed'); + + await unlink(deleteFilePath); }; await remove(); \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js index c0a83dea15..8dbdef0fdb 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,20 @@ +import { readdir } from 'fs/promises'; +import { existsSync } 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 sourceFolder = `${__dirname}/files`; + + if (!existsSync(sourceFolder)) + throw new Error('FS operation failed'); + + const files = await readdir(sourceFolder); + + console.log(files); }; await list(); \ No newline at end of file diff --git a/src/fs/read.js b/src/fs/read.js index 52c78cc6ee..d92ad64f39 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,20 @@ +import { existsSync } from 'fs'; +import { readFile } from 'fs/promises'; +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 fileToRead = `${__dirname}/files/fileToRead.txt`; + + if (!existsSync(fileToRead)) + throw new Error('FS operation failed'); + + const data = await readFile(fileToRead, 'utf8'); + + console.log(data); }; await read(); \ No newline at end of file diff --git a/src/fs/rename.js b/src/fs/rename.js index 2bb99ecdb5..899466f841 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,19 @@ +import fs from 'fs/promises'; +import { existsSync } from 'fs'; +import path, { dirname } 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 filePath = `${__dirname}/files/wrongFilename.txt`; + const targetFilePath = `${__dirname}/files/properFilename.md`; + + if (!existsSync(filePath) || existsSync(targetFilePath)) + throw new Error('FS operation failed'); + + await fs.rename(filePath, targetFilePath); }; await rename(); \ No newline at end of file From e64efeed92e4f431646d0c63b09b63b7939d80ce Mon Sep 17 00:00:00 2001 From: Slavashevich Date: Wed, 14 Jun 2023 11:15:55 +0300 Subject: [PATCH 2/8] Command line interface(src/cli) --- src/cli/args.js | 12 +++++++++++- src/cli/env.js | 9 ++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cli/args.js b/src/cli/args.js index 8283f7f7aa..e7cd6a1955 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,15 @@ const parseArgs = () => { - // Write your code here + const args = process.argv.slice(2); + + const argsAsArray = []; + + for (let i = 0; i < args.length; i += 2) { + argsAsArray.push([args[i].slice(2), args[i + 1]]); + } + + const result = argsAsArray.map((ar => `${ar[0]} is ${ar[1]}`)).join(', '); + + console.log(result); }; parseArgs(); \ No newline at end of file diff --git a/src/cli/env.js b/src/cli/env.js index fe4aa4a8df..39964c61b7 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,12 @@ const parseEnv = () => { - // Write your code here + const prefix = 'RSS_'; + + const result = Object.keys(process.env) + .filter(key => key.startsWith(prefix)) + .map(key => `${key}=${process.env[key]}`) + .join('; '); + + console.log(result) }; parseEnv(); \ No newline at end of file From b6757f29eaf7fad3adf76340ebd4038850d3787d Mon Sep 17 00:00:00 2001 From: Slavashevich Date: Wed, 14 Jun 2023 11:33:40 +0300 Subject: [PATCH 3/8] Modules(src/modules) --- src/modules/cjsToEsm.cjs | 40 ---------------------------------------- src/modules/esm.mjs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 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..d0e614f49d --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,40 @@ +import path from "path"; +import os from "os"; +import { createServer } from "http"; +import "./files/c.js"; + +const random = Math.random(); + +let unknownObject; + +if (random > 0.5) { + unknownObject = import("./files/a.json", { + assert: { type: "json" } + }); +} else { + unknownObject = import("./files/b.json", { + assert: { type: "json" } + }); +} + +console.log(`Release ${os.release()}`); +console.log(`Version ${os.version()}`); +console.log(`Path segment separator is "${path.sep}"`); + +console.log(`Path to current file is ${import.meta.url}`); +console.log(`Path to current directory is ${path.dirname(import.meta.url)}`); + +const myServer = createServer((_, res) => { + res.end("Request accepted"); +}); + +const PORT = 3000; + +console.log((await unknownObject).default); + +myServer.listen(PORT, () => { + console.log(`Server is listening on port ${PORT}`); + console.log("To terminate it, use Ctrl+C combination"); +}); + +export { unknownObject, myServer }; From 19d7a4ea9e6cb739c5f503e8245502914fdf51d2 Mon Sep 17 00:00:00 2001 From: Slavashevich Date: Wed, 14 Jun 2023 11:42:55 +0300 Subject: [PATCH 4/8] Hash (src/hash) --- src/hash/calcHash.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 450f8f72e2..c3dd5e3a04 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,21 @@ +import { createHash } from 'node:crypto' +import { readFile } from 'fs/promises'; +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 = `${__dirname}/files/fileToCalculateHashFor.txt`; + + const fileData = await readFile(filePath); + + const hash = createHash('sha256') + .update(fileData) + .digest('hex'); + + console.log(hash); }; await calculateHash(); \ No newline at end of file From b9a6f5da7b20dffee26c5502b9587405e05cca88 Mon Sep 17 00:00:00 2001 From: Slavashevich Date: Fri, 16 Jun 2023 13:31:43 +0300 Subject: [PATCH 5/8] Streams (src/streams) --- src/streams/read.js | 15 ++++++++++++++- src/streams/transform.js | 10 +++++++++- src/streams/write.js | 15 ++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/streams/read.js b/src/streams/read.js index 52c78cc6ee..2f7ca6d4d2 100644 --- a/src/streams/read.js +++ b/src/streams/read.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 read = async () => { - // Write your code here + const filePath = `${__dirname}/files/fileToRead.txt`; + + const readableStream = fs.createReadStream(filePath); + + readableStream.on('data', (chunk) => { + process.stdout.write(chunk); + }); }; await read(); \ No newline at end of file diff --git a/src/streams/transform.js b/src/streams/transform.js index 315fc6597f..9c142cdae0 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,13 @@ +import { Transform } from 'stream'; + const transform = async () => { - // Write your code here + const reverse = new Transform({ + transform(chunk, encoding, callback) { + callback(null, chunk.toString().split('').reverse().join('')); + } + }); + + process.stdin.pipe(reverse).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..6627ab0b63 100644 --- a/src/streams/write.js +++ b/src/streams/write.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 write = async () => { - // Write your code here + const filePath = `${__dirname}/files/fileToWrite.txt`; + + const writableStream = fs.createWriteStream(filePath); + + process.stdin.pipe(writableStream); + + console.log('data to write:') }; await write(); \ No newline at end of file From c58b9ab012e66eaa86acebe3b6af610d4a002df0 Mon Sep 17 00:00:00 2001 From: Slavashevich Date: Fri, 16 Jun 2023 15:43:11 +0300 Subject: [PATCH 6/8] Zlib (src/zip) --- src/zip/compress.js | 18 +++++++++++++++++- src/zip/decompress.js | 19 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/zip/compress.js b/src/zip/compress.js index bb328f43c6..ded72c24c8 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,21 @@ +import fs from 'fs'; +import path from 'path'; +import zlib from 'zlib'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const compress = async () => { - // Write your code here + const fileToCompress = `${__dirname}/files/fileToCompress.txt`; + const compressedFilePath = `${__dirname}/files/archive.gz`; + + const readStream = fs.createReadStream(fileToCompress); + const writeStream = fs.createWriteStream(compressedFilePath); + + 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..58661344e1 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,22 @@ +import fs from 'fs'; +import path from 'path'; +import zlib from 'zlib'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const decompress = async () => { - // Write your code here + `${__dirname}/files/fileToCompress.txt` + const fileToDecompress = `${__dirname}/files/archive.gz`; + const decompressedFilePath = `${__dirname}/files/fileToCompress.txt`; + + const readStream = fs.createReadStream(fileToDecompress); + const writeStream = fs.createWriteStream(decompressedFilePath); + + const gunZip = zlib.createGunzip(); + + readStream.pipe(gunZip).pipe(writeStream); }; await decompress(); \ No newline at end of file From e512bd7b0321007fee6101d2f5603aac68ddf0ea Mon Sep 17 00:00:00 2001 From: Slavashevich Date: Fri, 16 Jun 2023 16:05:41 +0300 Subject: [PATCH 7/8] Child Processes (src/cp) --- src/cp/cp.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/cp/cp.js b/src/cp/cp.js index 4a87b98c55..41abc68ee0 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,16 @@ +import { spawn } from 'node: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 childProcess = spawn('node', [`${__dirname}/files/script.js`, ...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(['firstArg', 'secArg']); From 051601a6e74bfe9a9a13bb0eb4289da696e12b52 Mon Sep 17 00:00:00 2001 From: Slavashevich Date: Sun, 18 Jun 2023 20:29:41 +0300 Subject: [PATCH 8/8] Worker Threads (src/wt) --- src/wt/main.js | 30 +++++++++++++++++++++++++++++- src/wt/worker.js | 7 ++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/wt/main.js b/src/wt/main.js index 37d80484ec..f77da74303 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,33 @@ +import os from 'os'; +import { Worker } from 'worker_threads'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const performCalculations = async () => { - // Write your code here + const workerPath = `${__dirname}/worker.js`; + const cpuCount= os.cpus().length + + const processes = []; + + const n = 10; + + for (let i = n; i < n + cpuCount; i++) { + processes.push( + new Promise((resolve) => { + const worker = new Worker(workerPath); + worker.postMessage(i); + worker.on('message', (result) => resolve({ status: 'resolved', data: result })); + worker.on('error', (error) => resolve({ status: error, data: null })); + }) + ); + } + + const result = await Promise.all(processes); + + console.log(result); }; await performCalculations(); \ No newline at end of file diff --git a/src/wt/worker.js b/src/wt/worker.js index 441b2154f8..b5454c65bf 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,13 @@ +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', (n) => { + const result = nthFibonacci(n); + parentPort.postMessage(result) + }); }; sendResult(); \ No newline at end of file