From ec1ad099c100783678834db2fb6f9c94e69baeeb Mon Sep 17 00:00:00 2001 From: Madina Date: Thu, 26 Feb 2026 22:00:19 +0500 Subject: [PATCH 1/8] feat: implement functions copy, create, delete, rename --- files/fileToRead.txt | 1 + files/fresh.txt | 1 + files/properFilename.md | 0 src/fs/copy.js | 16 +++++++++++++++- src/fs/create.js | 11 ++++++++++- src/fs/delete.js | 13 ++++++++++++- src/fs/rename.js | 19 ++++++++++++++++++- 7 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 files/fileToRead.txt create mode 100644 files/fresh.txt create mode 100644 files/properFilename.md diff --git a/files/fileToRead.txt b/files/fileToRead.txt new file mode 100644 index 0000000000..ce2e116606 --- /dev/null +++ b/files/fileToRead.txt @@ -0,0 +1 @@ +Hello from Node.js! \ No newline at end of file diff --git a/files/fresh.txt b/files/fresh.txt new file mode 100644 index 0000000000..205d704cb7 --- /dev/null +++ b/files/fresh.txt @@ -0,0 +1 @@ +I am fresh and young \ No newline at end of file diff --git a/files/properFilename.md b/files/properFilename.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..d67f111d90 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,19 @@ +import { cp } from 'node:fs/promises'; +import path from 'node:path'; + const copy = async () => { - // Write your code here + try { + const src = path.resolve('files'); + const dest = path.resolve('files_copy'); + + await cp(src, dest, { + recursive: true, + errorOnExist: true, + force: false, + }); + } catch { + throw new Error('FS operation failed'); + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..e0a759f385 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,14 @@ +import { writeFile } from 'node:fs/promises'; +import path from 'node:path'; + const create = async () => { - // Write your code here + try { + const filePath = path.resolve('files', 'fresh.txt'); + + await writeFile(filePath, 'I am fresh and young', { flag: 'wx' }); + } catch { + throw new Error('FS operation failed'); + } }; await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..0996d01582 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,16 @@ +import { rm, access } from 'node:fs/promises'; +import path from 'node:path'; + const remove = async () => { - // Write your code here + try { + const filePath = path.resolve('files', 'fileToRemove.txt'); + + await access(filePath); + + await rm(filePath); + } catch { + throw new Error('FS operation failed'); + } }; await remove(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..d38d9d20b5 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,22 @@ +import { rename as fsRename, access } from 'node:fs/promises'; +import path from 'node:path'; + const rename = async () => { - // Write your code here + try { + const srcPath = path.resolve('files', 'wrongFilename.txt'); + const destPath = path.resolve('files', 'properFilename.md'); + + await access(srcPath); + + try { + await access(destPath); + throw new Error(); + } catch {} + + await fsRename(srcPath, destPath); + } catch { + throw new Error('FS operation failed'); + } }; await rename(); From 2c77ead671888dd580b735ffd2254400d143f0fd Mon Sep 17 00:00:00 2001 From: Madina Date: Sun, 1 Mar 2026 13:31:15 +0500 Subject: [PATCH 2/8] feat: implement functions command line interface --- src/cli/args.js | 13 ++++++++++++- src/cli/env.js | 9 ++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..7e212c11a9 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,16 @@ const parseArgs = () => { - // Write your code here + const args = process.argv.slice(2); + + const result = []; + + for (let i = 0; i < args.length; i += 2) { + const propName = args[i].replace('--', ''); + const value = args[i + 1]; + + result.push(`${propName} is ${value}`); + } + + console.log(result.join(', ')); }; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..e5b3f41c44 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,12 @@ const parseEnv = () => { - // Write your code here + const envVars = process.env; + + const result = Object.entries(envVars) + .filter(([key]) => key.startsWith('RSS_')) + .map(([key, value]) => `${key}=${value}`) + .join('; '); + + console.log(result); }; parseEnv(); From 7fe75568ef5395247afb55a6cf93023863b15a47 Mon Sep 17 00:00:00 2001 From: Madina Date: Sun, 1 Mar 2026 13:32:03 +0500 Subject: [PATCH 3/8] feat: add functions for list, read --- src/fs/list.js | 13 ++++++++++++- src/fs/read.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..49411a826c 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,16 @@ +import { readdir } from 'node:fs/promises'; +import path from 'node:path'; + const list = async () => { - // Write your code here + try { + const folderPath = path.resolve('files'); + + const files = await readdir(folderPath); + + console.log(files); + } catch { + throw new Error('FS operation failed'); + } }; await list(); diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..fd51989a89 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,16 @@ +import { readFile } from 'node:fs/promises'; +import path from 'node:path'; + const read = async () => { - // Write your code here + try { + const filePath = path.resolve('files', 'fileToRead.txt'); + + const content = await readFile(filePath, 'utf-8'); + + console.log(content); + } catch { + throw new Error('FS operation failed'); + } }; await read(); From e538311e9522a26479d43d42754849d6f2a7c367 Mon Sep 17 00:00:00 2001 From: Madina Date: Sun, 1 Mar 2026 14:55:25 +0500 Subject: [PATCH 4/8] feat: implement FS tasks create, copy, rename, delete, list, read with proper error handling --- src/fs/copy.js | 8 ++++++-- src/fs/create.js | 6 +++++- src/fs/delete.js | 6 +++++- src/fs/files/fileToRemove.txt | 1 - src/fs/list.js | 6 +++++- src/fs/read.js | 6 +++++- src/fs/rename.js | 8 ++++++-- 7 files changed, 32 insertions(+), 9 deletions(-) delete mode 100644 src/fs/files/fileToRemove.txt diff --git a/src/fs/copy.js b/src/fs/copy.js index d67f111d90..3ed9356323 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,10 +1,14 @@ import { cp } from 'node:fs/promises'; import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const copy = async () => { try { - const src = path.resolve('files'); - const dest = path.resolve('files_copy'); + const src = path.resolve(__dirname, 'files'); + const dest = path.resolve(__dirname, 'files_copy'); await cp(src, dest, { recursive: true, diff --git a/src/fs/create.js b/src/fs/create.js index e0a759f385..a7daee8f52 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,9 +1,13 @@ import { writeFile } from 'node:fs/promises'; import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const create = async () => { try { - const filePath = path.resolve('files', 'fresh.txt'); + const filePath = path.resolve(__dirname, 'files', 'fresh.txt'); await writeFile(filePath, 'I am fresh and young', { flag: 'wx' }); } catch { diff --git a/src/fs/delete.js b/src/fs/delete.js index 0996d01582..0766126490 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,9 +1,13 @@ import { rm, access } from 'node:fs/promises'; import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const remove = async () => { try { - const filePath = path.resolve('files', 'fileToRemove.txt'); + const filePath = path.resolve(__dirname, 'files', 'fileToRemove.txt'); await access(filePath); diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt deleted file mode 100644 index 43e64cd45c..0000000000 --- a/src/fs/files/fileToRemove.txt +++ /dev/null @@ -1 +0,0 @@ -How dare you! \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js index 49411a826c..594120ca3e 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,9 +1,13 @@ import { readdir } from 'node:fs/promises'; import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const list = async () => { try { - const folderPath = path.resolve('files'); + const folderPath = path.resolve(__dirname, 'files'); const files = await readdir(folderPath); diff --git a/src/fs/read.js b/src/fs/read.js index fd51989a89..fc0441cc7c 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,9 +1,13 @@ import { readFile } from 'node:fs/promises'; import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const read = async () => { try { - const filePath = path.resolve('files', 'fileToRead.txt'); + const filePath = path.resolve(__dirname, 'files', 'fileToRead.txt'); const content = await readFile(filePath, 'utf-8'); diff --git a/src/fs/rename.js b/src/fs/rename.js index d38d9d20b5..3cfa11da94 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,10 +1,14 @@ import { rename as fsRename, access } from 'node:fs/promises'; import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const rename = async () => { try { - const srcPath = path.resolve('files', 'wrongFilename.txt'); - const destPath = path.resolve('files', 'properFilename.md'); + const srcPath = path.resolve(__dirname, 'files', 'wrongFilename.txt'); + const destPath = path.resolve(__dirname, 'files', 'properFilename.md'); await access(srcPath); From 9a8ac3bda266c0ae7ca4e64a0d7652d4c1002c50 Mon Sep 17 00:00:00 2001 From: Madina Date: Sun, 1 Mar 2026 14:57:18 +0500 Subject: [PATCH 5/8] feat: implement Streams read, write, transform with proper piping and stdout/stdin handlin --- src/streams/files/fileToWrite.txt | 0 src/streams/read.js | 17 ++++++++++++++++- src/streams/transform.js | 11 ++++++++++- src/streams/write.js | 13 ++++++++++++- 4 files changed, 38 insertions(+), 3 deletions(-) delete mode 100644 src/streams/files/fileToWrite.txt diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..7d1ec4dd38 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,20 @@ +import { createReadStream } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node: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 readable = createReadStream(filePath); + + readable.on('error', () => { + throw new Error('FS operation failed'); + }); + + readable.pipe(process.stdout); }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..9001ff6876 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,14 @@ +import { Transform } from 'node:stream'; + const transform = async () => { - // Write your code here + const reverseStream = new Transform({ + transform(chunk, _, callback) { + const reversed = chunk.toString().split('').reverse().join(''); + callback(null, reversed); + }, + }); + + process.stdin.pipe(reverseStream).pipe(process.stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..1af705b4b4 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,16 @@ +import { createWriteStream } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +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 writable = createWriteStream(filePath); + + process.stdin.pipe(writable); }; await write(); From 87bd4546d74edd27f417455baa9c1e74d3f17789 Mon Sep 17 00:00:00 2001 From: Madina Date: Sun, 1 Mar 2026 14:58:15 +0500 Subject: [PATCH 6/8] refactor: modules convert cjsToEsm.cjs to ECMAScript esm.mjs --- src/modules/{cjsToEsm.cjs => esm.mjs} | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) rename src/modules/{cjsToEsm.cjs => esm.mjs} (51%) diff --git a/src/modules/cjsToEsm.cjs b/src/modules/esm.mjs similarity index 51% rename from src/modules/cjsToEsm.cjs rename to src/modules/esm.mjs index 089bd2db13..fed4191f7a 100644 --- a/src/modules/cjsToEsm.cjs +++ b/src/modules/esm.mjs @@ -1,12 +1,19 @@ -const path = require('node:path'); -const { release, version } = require('node:os'); -const { createServer: createServerHttp } = require('node:http'); +import path from 'node:path'; +import { release, version } from 'node:os'; +import { createServer as createServerHttp } from 'node:http'; +import { fileURLToPath } from 'node:url'; -require('./files/c.cjs'); +import './files/c.cjs'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const random = Math.random(); -const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/b.json'); +const unknownObject = + random > 0.5 + ? (await import('./files/a.json', { with: { type: 'json' } })).default + : (await import('./files/b.json', { with: { type: 'json' } })).default; console.log(`Release ${release()}`); console.log(`Version ${version()}`); @@ -28,7 +35,4 @@ myServer.listen(PORT, () => { console.log('To terminate it, use Ctrl+C combination'); }); -module.exports = { - unknownObject, - myServer, -}; +export { unknownObject, myServer }; From 6c5b79ed482947ab80883c8e9877854f80f424d3 Mon Sep 17 00:00:00 2001 From: Madina Date: Sun, 1 Mar 2026 14:59:17 +0500 Subject: [PATCH 7/8] feat: implement SHA256 hash calculation for file using Streams API --- src/hash/calcHash.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..2e405d3350 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,31 @@ +import { createReadStream } from 'node:fs'; +import { createHash } from 'node:crypto'; +import path from 'node:path'; +import { fileURLToPath } from 'node: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', + ); + + return new Promise((resolve, reject) => { + const hash = createHash('sha256'); + const stream = createReadStream(filePath); + + stream.on('error', () => reject(new Error('FS operation failed'))); + + stream.on('data', (chunk) => hash.update(chunk)); + + stream.on('end', () => { + console.log(hash.digest('hex')); + resolve(); + }); + }); }; await calculateHash(); From d1163b61c9bad81f062731d4fa6257c75354c4f9 Mon Sep 17 00:00:00 2001 From: Madina Date: Sun, 1 Mar 2026 15:00:19 +0500 Subject: [PATCH 8/8] feat: implement zip and worker threads --- files/fileToRead.txt | 1 - files/fresh.txt | 1 - files/properFilename.md | 0 src/cp/cp.js | 28 +++++++++++++++++++++++++--- src/wt/main.js | 32 +++++++++++++++++++++++++++++++- src/wt/worker.js | 25 ++++++++++++++++++++++--- src/zip/compress.js | 24 +++++++++++++++++++++++- src/zip/decompress.js | 24 +++++++++++++++++++++++- 8 files changed, 124 insertions(+), 11 deletions(-) delete mode 100644 files/fileToRead.txt delete mode 100644 files/fresh.txt delete mode 100644 files/properFilename.md diff --git a/files/fileToRead.txt b/files/fileToRead.txt deleted file mode 100644 index ce2e116606..0000000000 --- a/files/fileToRead.txt +++ /dev/null @@ -1 +0,0 @@ -Hello from Node.js! \ No newline at end of file diff --git a/files/fresh.txt b/files/fresh.txt deleted file mode 100644 index 205d704cb7..0000000000 --- a/files/fresh.txt +++ /dev/null @@ -1 +0,0 @@ -I am fresh and young \ No newline at end of file diff --git a/files/properFilename.md b/files/properFilename.md deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/cp/cp.js b/src/cp/cp.js index 72c6addc9c..5b35483523 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,28 @@ +import { spawn } from 'node:child_process'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const spawnChildProcess = async (args) => { - // Write your code here + const childScript = path.resolve(__dirname, 'files', 'script.js'); + + const child = spawn('node', [childScript, ...args], { + stdio: ['pipe', 'pipe', 'inherit'], + }); + + process.stdin.pipe(child.stdin); + + child.stdout.pipe(process.stdout); + + child.on('close', (code) => { + console.log(`\nChild process exited with code ${code}`); + }); + + child.on('error', (err) => { + console.error('Failed to start child process:', err); + }); }; -// Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess(['smth', 'bbb']); diff --git a/src/wt/main.js b/src/wt/main.js index e2ef054d41..9de68c5cd5 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,35 @@ +import { Worker } from 'node:worker_threads'; +import os from 'node:os'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const performCalculations = async () => { - // Write your code here + const numCPUs = os.cpus().length; + const results = []; + const workers = []; + + for (let i = 0; i < numCPUs; i++) { + const worker = new Worker(path.resolve(__dirname, 'worker.js')); + workers.push(worker); + + const n = 10 + i; + + const promise = new Promise((resolve) => { + worker.once('message', (msg) => resolve(msg)); + worker.once('error', () => resolve({ status: 'error', data: null })); + }); + + worker.postMessage(n); + results.push(promise); + } + + const finalResults = await Promise.all(results); + console.log(finalResults); + + workers.forEach((w) => w.terminate()); }; await performCalculations(); diff --git a/src/wt/worker.js b/src/wt/worker.js index 405595394d..f8af98662a 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,27 @@ -// n should be received from main thread -const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); +import { parentPort } from 'node:worker_threads'; + +const nthFibonacci = (n) => + n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); + +let lastResult = null; const sendResult = () => { - // This function sends result of nthFibonacci computations to main thread + if (parentPort && lastResult !== null) { + parentPort.postMessage(lastResult); + } }; +if (parentPort) { + parentPort.on('message', (n) => { + try { + const fib = nthFibonacci(n); + lastResult = { status: 'resolved', data: fib }; + sendResult(); + } catch (err) { + lastResult = { status: 'error', data: null }; + sendResult(); + } + }); +} + sendResult(); diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..162e29f24a 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,27 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { createGzip } from 'node:zlib'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const compress = async () => { - // Write your code here + const inputFile = path.resolve(__dirname, 'files', 'fileToCompress.txt'); + const outputFile = path.resolve(__dirname, 'files', 'archive.gz'); + + return new Promise((resolve, reject) => { + const readStream = createReadStream(inputFile); + const writeStream = createWriteStream(outputFile); + const gzip = createGzip(); + + readStream.on('error', () => reject(new Error('FS operation failed'))); + writeStream.on('error', () => reject(new Error('FS operation failed'))); + + writeStream.on('finish', resolve); + + readStream.pipe(gzip).pipe(writeStream); + }); }; await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..90964735b5 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,27 @@ +import { createReadStream, createWriteStream } from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { createGunzip } from 'node:zlib'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + const decompress = async () => { - // Write your code here + const inputFile = path.resolve(__dirname, 'files', 'archive.gz'); + const outputFile = path.resolve(__dirname, 'files', 'fileToCompress.txt'); + + return new Promise((resolve, reject) => { + const readStream = createReadStream(inputFile); + const writeStream = createWriteStream(outputFile); + const gunzip = createGunzip(); + + readStream.on('error', () => reject(new Error('FS operation failed'))); + writeStream.on('error', () => reject(new Error('FS operation failed'))); + + writeStream.on('finish', resolve); + + readStream.pipe(gunzip).pipe(writeStream); + }); }; await decompress();