From 3655cffeacaedb5ec49514e5545272e0611af450 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 18:46:54 +0500 Subject: [PATCH 01/28] Add create function to write fresh.txt Implement file creation with error handling for existing files. --- src/fs/create.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..bc31963d9a 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,15 @@ +import { writeFile, access } from 'fs/promises'; +import { constants } from 'fs'; +import path from 'path'; + const create = async () => { - // Write your code here + const filePath = path.join('files', 'fresh.txt'); + try { + await access(filePath, constants.F_OK); + throw new Error('FS operation failed'); + } catch { + await writeFile(filePath, 'I am fresh and young', { flag: 'wx' }); + } }; await create(); From 1bb6c3410dfcd79b92b0dab38391d01a8842a4ec Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 18:52:10 +0500 Subject: [PATCH 02/28] Add copy function to copy files recursively Implement file copy functionality with error handling. --- src/fs/copy.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..0f4fc8a69b 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,22 @@ +import { access, cp } from 'fs/promises'; +import { constants } from 'fs'; +import path from 'path'; + const copy = async () => { - // Write your code here + const source = path.join('files'); + const destination = path.join('files_copy'); + + try { + await access(source, constants.F_OK); + await access(destination, constants.F_OK); + throw new Error('FS operation failed'); + } catch (err) { + if (err.code === 'ENOENT') { + await cp(source, destination, { recursive: true }); + } else { + throw err; + } + } }; await copy(); From 183d96b91ba81a3c33d37166380f60a6d6105b26 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 18:54:17 +0500 Subject: [PATCH 03/28] Implement file renaming with error handling Refactor rename function to rename a file and handle errors. --- src/fs/rename.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..bde182b4d2 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,22 @@ -const rename = async () => { - // Write your code here +import { rename, access } from 'fs/promises'; +import { constants } from 'fs'; +import path from 'path'; + +const renameFile = async () => { + const oldPath = path.join('files', 'wrongFilename.txt'); + const newPath = path.join('files', 'properFilename.md'); + + try { + await access(oldPath, constants.F_OK); + await access(newPath, constants.F_OK); + throw new Error('FS operation failed'); + } catch (err) { + if (err.code === 'ENOENT') { + await rename(oldPath, newPath); + } else { + throw err; + } + } }; -await rename(); +await renameFile(); From 18df431ac393fb9963a5ecf76f6055228b2cd5aa Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 18:55:42 +0500 Subject: [PATCH 04/28] Implement file deletion using fs/promises Refactor remove function to use fs/promises for file deletion. --- src/fs/delete.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..e5547739bd 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,16 @@ +import { unlink, access } from 'fs/promises'; +import { constants } from 'fs'; +import path from 'path'; + const remove = async () => { - // Write your code here + const filePath = path.join('files', 'fileToRemove.txt'); + + try { + await access(filePath, constants.F_OK); + await unlink(filePath); + } catch { + throw new Error('FS operation failed'); + } }; await remove(); From 8151277e9935c37aaeccc17c2a68880db98ed4ea Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 18:56:44 +0500 Subject: [PATCH 05/28] Implement file listing in src/fs/list.js Added functionality to list files in the 'files' directory and handle errors. --- src/fs/list.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..322ca4c485 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,17 @@ +import { readdir, access } from 'fs/promises'; +import { constants } from 'fs'; +import path from 'path'; + const list = async () => { - // Write your code here + const dirPath = path.join('files'); + + try { + await access(dirPath, constants.F_OK); + const files = await readdir(dirPath); + console.log(files); + } catch { + throw new Error('FS operation failed'); + } }; await list(); From 9c5ef39209d2e07fb3e1ca9b0d1811893eed44b1 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 18:58:01 +0500 Subject: [PATCH 06/28] Implement file reading with error handling --- src/fs/read.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..43fc1716ce 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,17 @@ +import { readFile, access } from 'fs/promises'; +import { constants } from 'fs'; +import path from 'path'; + const read = async () => { - // Write your code here + const filePath = path.join('files', 'fileToRead.txt'); + + try { + await access(filePath, constants.F_OK); + const content = await readFile(filePath, 'utf-8'); + console.log(content); + } catch { + throw new Error('FS operation failed'); + } }; await read(); From e94641a3d2bfd32758aa4a3f3b92c8df2da24bdb Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 19:00:29 +0500 Subject: [PATCH 07/28] Implement parsing of RSS environment variables Filter and format environment variables starting with 'RSS_' --- src/cli/env.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..b6da7145e7 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,10 @@ const parseEnv = () => { - // Write your code here + const rssVars = Object.entries(process.env) + .filter(([key]) => key.startsWith('RSS_')) + .map(([key, value]) => `${key}=${value}`) + .join('; '); + + console.log(rssVars); }; parseEnv(); From 74f9144f4f194f0ec609fd7156cf8107c34afa25 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 19:03:23 +0500 Subject: [PATCH 08/28] Add argument parsing functionality to args.js Enhance argument parsing to log key-value pairs. --- src/cli/args.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..3d269131d9 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,10 @@ const parseArgs = () => { - // Write your code here + const args = process.argv.slice(2); + for (let i = 0; i < args.length; i += 2) { + const key = args[i].replace(/^--/, ''); + const value = args[i + 1]; + console.log(`${key} is ${value}`); + } }; parseArgs(); From 6c600d4f159ca067c4cd3341d30ea9e1267c3d08 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 20:24:11 +0500 Subject: [PATCH 09/28] Transform cjsToEsm to fully utilize ES Module syntax --- src/modules/cjsToEsm.cjs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.cjs index 089bd2db13..381fd82742 100644 --- a/src/modules/cjsToEsm.cjs +++ b/src/modules/cjsToEsm.cjs @@ -1,17 +1,23 @@ -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 './files/c.cjs' assert { type: 'javascript' }; -require('./files/c.cjs'); +import aJson from './files/a.json' assert { type: 'json' }; +import bJson from './files/b.json' assert { type: 'json' }; const random = Math.random(); - -const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/b.json'); +const unknownObject = random > 0.5 ? aJson : bJson; console.log(`Release ${release()}`); console.log(`Version ${version()}`); console.log(`Path segment separator is "${path.sep}"`); +// These are not available in ESM by default +import { fileURLToPath } from 'node:url'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + console.log(`Path to current file is ${__filename}`); console.log(`Path to current directory is ${__dirname}`); @@ -28,7 +34,4 @@ myServer.listen(PORT, () => { console.log('To terminate it, use Ctrl+C combination'); }); -module.exports = { - unknownObject, - myServer, -}; +export { unknownObject, myServer }; From 35892069e4ad59f6e14653c5adf7f9cfbe8a6ef9 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 20:26:26 +0500 Subject: [PATCH 10/28] Create esm.mjs for server and JSON imports Implement ESM module with server setup and JSON handling --- src/modules/esm.mjs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 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..381fd82742 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,37 @@ +import path from 'node:path'; +import { release, version } from 'node:os'; +import { createServer as createServerHttp } from 'node:http'; +import './files/c.cjs' assert { type: 'javascript' }; + +import aJson from './files/a.json' assert { type: 'json' }; +import bJson from './files/b.json' assert { type: 'json' }; + +const random = Math.random(); +const unknownObject = random > 0.5 ? aJson : bJson; + +console.log(`Release ${release()}`); +console.log(`Version ${version()}`); +console.log(`Path segment separator is "${path.sep}"`); + +// These are not available in ESM by default +import { fileURLToPath } from 'node:url'; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +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 fef1fafb8cf7df124f58c9ec6aa116d7adb35d46 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 20:30:07 +0500 Subject: [PATCH 11/28] Delete src/modules/cjsToEsm.cjs --- src/modules/cjsToEsm.cjs | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 src/modules/cjsToEsm.cjs diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.cjs deleted file mode 100644 index 381fd82742..0000000000 --- a/src/modules/cjsToEsm.cjs +++ /dev/null @@ -1,37 +0,0 @@ -import path from 'node:path'; -import { release, version } from 'node:os'; -import { createServer as createServerHttp } from 'node:http'; -import './files/c.cjs' assert { type: 'javascript' }; - -import aJson from './files/a.json' assert { type: 'json' }; -import bJson from './files/b.json' assert { type: 'json' }; - -const random = Math.random(); -const unknownObject = random > 0.5 ? aJson : bJson; - -console.log(`Release ${release()}`); -console.log(`Version ${version()}`); -console.log(`Path segment separator is "${path.sep}"`); - -// These are not available in ESM by default -import { fileURLToPath } from 'node:url'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - -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 63a479240d3b276cd2d6e22471d43f73d574452e Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:18:07 +0500 Subject: [PATCH 12/28] Implement SHA-256 hash calculation with streams Refactor hash calculation to use streams and handle file reading errors. --- src/hash/calcHash.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..6e20fe6c55 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,27 @@ -const calculateHash = async () => { - // Write your code here +import { createHash } from 'crypto'; +import { createReadStream } from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const calcHash = async () => { + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt'); + + const hash = createHash('sha256'); + const stream = createReadStream(filePath); + + stream.on('error', () => { + console.error('FS operation failed'); + }); + + stream.on('data', (chunk) => { + hash.update(chunk); + }); + + stream.on('end', () => { + console.log(hash.digest('hex')); + }); }; -await calculateHash(); +await calcHash(); From 98dc131d3e6a3325eb5d751a013b674e632a66dc Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:22:57 +0500 Subject: [PATCH 13/28] Add file reading functionality with error handling Implement file reading using streams and handle errors. --- src/streams/read.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..9ccce6920e 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,18 @@ +import { createReadStream } from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + const read = async () => { - // Write your code here + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + const filePath = path.join(__dirname, 'files', 'fileToRead.txt'); + + const stream = createReadStream(filePath, 'utf-8'); + stream.pipe(process.stdout); + + stream.on('error', () => { + console.error('FS operation failed'); + }); }; await read(); From fe96dfad7eb3aa53984e2abec152c544e5f97db1 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:23:24 +0500 Subject: [PATCH 14/28] Implement file writing using streams Refactor write function to handle file writing from stdin. --- src/streams/write.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..5bf6a8fc7c 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,18 @@ +import { createWriteStream } from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + const write = async () => { - // Write your code here + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + const filePath = path.join(__dirname, 'files', 'fileToWrite.txt'); + + const stream = createWriteStream(filePath); + process.stdin.pipe(stream); + + stream.on('error', () => { + console.error('FS operation failed'); + }); }; await write(); From 25a9839de867ccc294440e5ad886cca7cb996a84 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:23:49 +0500 Subject: [PATCH 15/28] Implement Transform stream to reverse input Added a Transform stream to reverse input data. --- src/streams/transform.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..2a57fdc4e5 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,14 @@ +import { Transform } from '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(); From c511c14e1d307be4fd042f2c095dee11421171c2 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:27:09 +0500 Subject: [PATCH 16/28] Add gzip compression functionality Implement file compression using gzip. --- src/zip/compress.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..f37669e6dc 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,23 @@ +import { createReadStream, createWriteStream } from 'fs'; +import { createGzip } from 'zlib'; +import path from 'path'; +import { fileURLToPath } from 'url'; + const compress = async () => { - // Write your code here + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + + const sourcePath = path.join(__dirname, 'files', 'fileToCompress.txt'); + const destinationPath = path.join(__dirname, 'files', 'archive.gz'); + + const readable = createReadStream(sourcePath); + const writable = createWriteStream(destinationPath); + const gzip = createGzip(); + + readable.pipe(gzip).pipe(writable); + + readable.on('error', () => console.error('FS operation failed')); + writable.on('error', () => console.error('FS operation failed')); }; await compress(); From 3d77154ab264a7cc73a44996694b7b9f98e84aa1 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:28:05 +0500 Subject: [PATCH 17/28] Add decompression functionality for .gz files Implement decompression of a .gz file to a text file. --- src/zip/decompress.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..07b79b3e09 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,23 @@ +import { createReadStream, createWriteStream } from 'fs'; +import { createGunzip } from 'zlib'; +import path from 'path'; +import { fileURLToPath } from 'url'; + const decompress = async () => { - // Write your code here + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + + const sourcePath = path.join(__dirname, 'files', 'archive.gz'); + const destinationPath = path.join(__dirname, 'files', 'fileToCompress.txt'); + + const readable = createReadStream(sourcePath); + const writable = createWriteStream(destinationPath); + const gunzip = createGunzip(); + + readable.pipe(gunzip).pipe(writable); + + readable.on('error', () => console.error('FS operation failed')); + writable.on('error', () => console.error('FS operation failed')); }; await decompress(); From 0b26dd2bf128af77715b8965322dae60847a27e0 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:30:23 +0500 Subject: [PATCH 18/28] Implement parallel calculations using worker threads Refactored code to use worker threads for parallel calculations based on the number of CPU cores. --- src/wt/main.js | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/wt/main.js b/src/wt/main.js index e2ef054d41..3d92b209bb 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,41 @@ -const performCalculations = async () => { - // Write your code here +import { Worker } from 'worker_threads'; +import os from 'os'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +const main = async () => { + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + const workerPath = path.join(__dirname, 'worker.js'); + + const numCores = os.cpus().length; + const startValue = 10; + + const promises = Array.from({ length: numCores }, (_, i) => { + return new Promise((resolve) => { + const worker = new Worker(workerPath); + const value = startValue + i; + + worker.postMessage(value); + + worker.on('message', (result) => { + resolve({ status: 'resolved', data: result }); + }); + + worker.on('error', () => { + resolve({ status: 'error', data: null }); + }); + + worker.on('exit', (code) => { + if (code !== 0) { + resolve({ status: 'error', data: null }); + } + }); + }); + }); + + const results = await Promise.all(promises); + console.log(results); }; -await performCalculations(); +await main(); From fc58ee0227189c8cf486dd314d3ed90d1d670a6f Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:33:14 +0500 Subject: [PATCH 19/28] Add message listener for Fibonacci computation results Implement message listener to handle Fibonacci requests from the main thread and send results back. --- src/wt/worker.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/wt/worker.js b/src/wt/worker.js index 405595394d..4c2b0f8fa4 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,18 @@ +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) => { + try { + const result = nthFibonacci(n); + parentPort.postMessage(result); + } catch { + parentPort.postMessage(null); + } + }); }; sendResult(); From fd3968e2eeeb8fa85d501b2249d8d2332002521c Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Mon, 20 Oct 2025 21:40:35 +0500 Subject: [PATCH 20/28] Refactor spawnChildProcess to use child_process Updated spawnChildProcess to utilize the child_process module for improved process management and added argument handling. --- src/cp/cp.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/cp/cp.js b/src/cp/cp.js index 72c6addc9c..6c0cb6835c 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,19 @@ -const spawnChildProcess = async (args) => { - // Write your code here +import { spawn } from 'child_process'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +export const spawnChildProcess = async (args) => { + const __filename = fileURLToPath(import.meta.url); + const __dirname = path.dirname(__filename); + const scriptPath = path.join(__dirname, 'files', 'script.js'); + + const child = spawn('node', [scriptPath, ...args], { + stdio: ['pipe', 'pipe', 'inherit'] + }); + + process.stdin.pipe(child.stdin); + child.stdout.pipe(process.stdout); }; // Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess(['hello', 'world']); From 28238e8bcab11d92600acdc1c320588aadfc0639 Mon Sep 17 00:00:00 2001 From: toby-28 Date: Tue, 21 Oct 2025 12:08:26 +0500 Subject: [PATCH 21/28] Refactor file operations to use __dirname for path resolution and standardize error messages --- src/fs/copy.js | 18 +++++++++++------- src/fs/create.js | 17 +++++++++++------ src/fs/delete.js | 14 +++++++++----- src/fs/list.js | 14 +++++++++----- src/fs/read.js | 16 ++++++++++------ src/fs/rename.js | 18 +++++++++++------- 6 files changed, 61 insertions(+), 36 deletions(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index 0f4fc8a69b..0025ed0573 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,17 +1,21 @@ -import { access, cp } from 'fs/promises'; -import { constants } from 'fs'; -import path from 'path'; +import { access, cp } from "fs/promises"; +import { constants } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const copy = async () => { - const source = path.join('files'); - const destination = path.join('files_copy'); + const dir = path.join(__dirname, "files"); + const source = path.join(dir); + const destination = path.join(__dirname, "files_copy"); try { await access(source, constants.F_OK); await access(destination, constants.F_OK); - throw new Error('FS operation failed'); + throw new Error("FS operation failed"); } catch (err) { - if (err.code === 'ENOENT') { + if (err.code === "ENOENT") { await cp(source, destination, { recursive: true }); } else { throw err; diff --git a/src/fs/create.js b/src/fs/create.js index bc31963d9a..7854fbdbc0 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,14 +1,19 @@ -import { writeFile, access } from 'fs/promises'; -import { constants } from 'fs'; -import path from 'path'; +import { writeFile, access } from "fs/promises"; +import { constants } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const create = async () => { - const filePath = path.join('files', 'fresh.txt'); + const dir = path.join(__dirname, "files"); + const filePath = path.join(dir, "fresh.txt"); + try { await access(filePath, constants.F_OK); - throw new Error('FS operation failed'); + throw new Error("FS operation failed"); } catch { - await writeFile(filePath, 'I am fresh and young', { flag: 'wx' }); + await writeFile(filePath, "I am fresh and young", { flag: "wx" }); } }; diff --git a/src/fs/delete.js b/src/fs/delete.js index e5547739bd..b5e8344d09 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,15 +1,19 @@ -import { unlink, access } from 'fs/promises'; -import { constants } from 'fs'; -import path from 'path'; +import { unlink, access } from "fs/promises"; +import { constants } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const remove = async () => { - const filePath = path.join('files', 'fileToRemove.txt'); + const dir = path.join(__dirname, "files"); + const filePath = path.join(dir, "fileToRemove.txt"); try { await access(filePath, constants.F_OK); await unlink(filePath); } catch { - throw new Error('FS operation failed'); + throw new Error("FS operation failed"); } }; diff --git a/src/fs/list.js b/src/fs/list.js index 322ca4c485..f2822d0b28 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,16 +1,20 @@ -import { readdir, access } from 'fs/promises'; -import { constants } from 'fs'; -import path from 'path'; +import { readdir, access } from "fs/promises"; +import { constants } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const list = async () => { - const dirPath = path.join('files'); + const dir = path.join(__dirname, "files"); + const dirPath = path.join(dir); try { await access(dirPath, constants.F_OK); const files = await readdir(dirPath); console.log(files); } catch { - throw new Error('FS operation failed'); + throw new Error("FS operation failed"); } }; diff --git a/src/fs/read.js b/src/fs/read.js index 43fc1716ce..240b086bbe 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,16 +1,20 @@ -import { readFile, access } from 'fs/promises'; -import { constants } from 'fs'; -import path from 'path'; +import { readFile, access } from "fs/promises"; +import { constants } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const read = async () => { - const filePath = path.join('files', 'fileToRead.txt'); + const dir = path.join(__dirname, "files"); + const filePath = path.join(dir, "fileToRead.txt"); try { await access(filePath, constants.F_OK); - const content = await readFile(filePath, 'utf-8'); + const content = await readFile(filePath, "utf-8"); console.log(content); } catch { - throw new Error('FS operation failed'); + throw new Error("FS operation failed"); } }; diff --git a/src/fs/rename.js b/src/fs/rename.js index bde182b4d2..dc5aca88b2 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,17 +1,21 @@ -import { rename, access } from 'fs/promises'; -import { constants } from 'fs'; -import path from 'path'; +import { rename, access } from "fs/promises"; +import { constants } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const renameFile = async () => { - const oldPath = path.join('files', 'wrongFilename.txt'); - const newPath = path.join('files', 'properFilename.md'); + const dir = path.join(__dirname, "files"); + const oldPath = path.join(dir, "wrongFilename.txt"); + const newPath = path.join(dir, "properFilename.md"); try { await access(oldPath, constants.F_OK); await access(newPath, constants.F_OK); - throw new Error('FS operation failed'); + throw new Error("FS operation failed"); } catch (err) { - if (err.code === 'ENOENT') { + if (err.code === "ENOENT") { await rename(oldPath, newPath); } else { throw err; From befa038b486b834982e4f9acc73a94cca0f83c98 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Tue, 21 Oct 2025 12:14:53 +0500 Subject: [PATCH 22/28] Refactor file path in create.js to use __dirname Update file path resolution to use __dirname for consistency. --- src/fs/create.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/fs/create.js b/src/fs/create.js index bc31963d9a..9a867f99b0 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,9 +1,13 @@ import { writeFile, access } from 'fs/promises'; import { constants } from 'fs'; import path from 'path'; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const create = async () => { - const filePath = path.join('files', 'fresh.txt'); + const dir = path.join(__dirname, "files"); + const filePath = path.join(dir, 'fresh.txt'); try { await access(filePath, constants.F_OK); throw new Error('FS operation failed'); From 07331fc5bc3adb546ad07fa91e02b2a7587f6303 Mon Sep 17 00:00:00 2001 From: toby-28 Date: Tue, 21 Oct 2025 12:23:43 +0500 Subject: [PATCH 23/28] Refactor create.js to use __dirname for file path resolution --- src/fs/create.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/fs/create.js b/src/fs/create.js index 691158d053..29f46ec3b6 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,9 +1,14 @@ -import { writeFile, access } from 'fs/promises'; -import { constants } from 'fs'; -import path from 'path'; +import { writeFile, access } from "fs/promises"; +import { constants } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const create = async () => { - const filePath = path.join('files', 'fresh.txt'); + const dir = path.join(__dirname, "files"); + const filePath = path.join(dir, "fresh.txt"); + try { await access(filePath, constants.F_OK); throw new Error("FS operation failed"); From bc121ade70432f439196bdac958a8c8d10c11073 Mon Sep 17 00:00:00 2001 From: toby-28 Date: Tue, 21 Oct 2025 12:58:03 +0500 Subject: [PATCH 24/28] Refactor string quotes in args.js, env.js, and esm.mjs for consistency --- src/cli/args.js | 2 +- src/cli/env.js | 4 ++-- src/modules/esm.mjs | 41 +++++++++++++++++++++++++++-------------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/cli/args.js b/src/cli/args.js index 3d269131d9..b14e97d799 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,7 +1,7 @@ const parseArgs = () => { const args = process.argv.slice(2); for (let i = 0; i < args.length; i += 2) { - const key = args[i].replace(/^--/, ''); + const key = args[i].replace(/^--/, ""); const value = args[i + 1]; console.log(`${key} is ${value}`); } diff --git a/src/cli/env.js b/src/cli/env.js index b6da7145e7..819c192fb7 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,8 +1,8 @@ const parseEnv = () => { const rssVars = Object.entries(process.env) - .filter(([key]) => key.startsWith('RSS_')) + .filter(([key]) => key.startsWith("RSS_")) .map(([key, value]) => `${key}=${value}`) - .join('; '); + .join("; "); console.log(rssVars); }; diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs index 381fd82742..958a168b01 100644 --- a/src/modules/esm.mjs +++ b/src/modules/esm.mjs @@ -1,37 +1,50 @@ -import path from 'node:path'; -import { release, version } from 'node:os'; -import { createServer as createServerHttp } from 'node:http'; -import './files/c.cjs' assert { type: 'javascript' }; +import path from "node:path"; +import { release, version } from "node:os"; +import { createServer as createServerHttp } from "node:http"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { readFile } from "node:fs/promises"; -import aJson from './files/a.json' assert { type: 'json' }; -import bJson from './files/b.json' assert { type: 'json' }; +// Reconstruct __filename and __dirname for ESM +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +// Dynamically import CommonJS module +await import(pathToFileURL(path.join(__dirname, "files", "c.cjs")).href); + +// Dynamically import JSON files +const aJson = JSON.parse( + await readFile(path.join(__dirname, "files", "a.json"), "utf-8") +); +const bJson = JSON.parse( + await readFile(path.join(__dirname, "files", "b.json"), "utf-8") +); +// Randomly select one JSON object const random = Math.random(); const unknownObject = random > 0.5 ? aJson : bJson; +// Log system info console.log(`Release ${release()}`); console.log(`Version ${version()}`); console.log(`Path segment separator is "${path.sep}"`); - -// These are not available in ESM by default -import { fileURLToPath } from 'node:url'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - console.log(`Path to current file is ${__filename}`); console.log(`Path to current directory is ${__dirname}`); +// Create HTTP server const myServer = createServerHttp((_, res) => { - res.end('Request accepted'); + res.end("Request accepted"); }); const PORT = 3000; +// Log selected JSON object console.log(unknownObject); +// Start server myServer.listen(PORT, () => { console.log(`Server is listening on port ${PORT}`); - console.log('To terminate it, use Ctrl+C combination'); + console.log("To terminate it, use Ctrl+C combination"); }); +// Export values export { unknownObject, myServer }; From f35b4dfdafe1ec465b59154b08fbebbc964cf090 Mon Sep 17 00:00:00 2001 From: toby-28 Date: Tue, 21 Oct 2025 13:10:38 +0500 Subject: [PATCH 25/28] Refactor string quotes in calcHash.js for consistency --- src/hash/calcHash.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 6e20fe6c55..86f137de12 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,26 +1,26 @@ -import { createHash } from 'crypto'; -import { createReadStream } from 'fs'; -import path from 'path'; -import { fileURLToPath } from 'url'; +import { createHash } from "crypto"; +import { createReadStream } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; const calcHash = async () => { const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); - const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt'); + const filePath = path.join(__dirname, "files", "fileToCalculateHashFor.txt"); - const hash = createHash('sha256'); + const hash = createHash("sha256"); const stream = createReadStream(filePath); - stream.on('error', () => { - console.error('FS operation failed'); + stream.on("error", () => { + console.error("FS operation failed"); }); - stream.on('data', (chunk) => { + stream.on("data", (chunk) => { hash.update(chunk); }); - stream.on('end', () => { - console.log(hash.digest('hex')); + stream.on("end", () => { + console.log(hash.digest("hex")); }); }; From 4c7ecf7a79d4524000dc96ffb07b0d86375762af Mon Sep 17 00:00:00 2001 From: toby-28 Date: Mon, 27 Oct 2025 23:30:45 +0500 Subject: [PATCH 26/28] fully implemented --- src/streams/read.js | 23 ++++++++++++++--------- src/streams/write.js | 12 ++++++------ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/streams/read.js b/src/streams/read.js index 9ccce6920e..e0b1319f98 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,18 +1,23 @@ -import { createReadStream } from 'fs'; -import path from 'path'; -import { fileURLToPath } from 'url'; +import { createReadStream } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; const read = async () => { const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); - const filePath = path.join(__dirname, 'files', 'fileToRead.txt'); + const filePath = path.join(__dirname, "files", "fileToRead.txt"); - const stream = createReadStream(filePath, 'utf-8'); - stream.pipe(process.stdout); + return new Promise((resolve, reject) => { + const stream = createReadStream(filePath, "utf-8"); - stream.on('error', () => { - console.error('FS operation failed'); + stream.pipe(process.stdout); + + stream.on("end", resolve); + stream.on("error", () => { + console.error("FS operation failed"); + reject(); + }); }); }; -await read(); +await read(); \ No newline at end of file diff --git a/src/streams/write.js b/src/streams/write.js index 5bf6a8fc7c..7fe0b6d62e 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,17 +1,17 @@ -import { createWriteStream } from 'fs'; -import path from 'path'; -import { fileURLToPath } from 'url'; +import { createWriteStream } from "fs"; +import path from "path"; +import { fileURLToPath } from "url"; const write = async () => { const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); - const filePath = path.join(__dirname, 'files', 'fileToWrite.txt'); + const filePath = path.join(__dirname, "files", "fileToWrite.txt"); const stream = createWriteStream(filePath); process.stdin.pipe(stream); - stream.on('error', () => { - console.error('FS operation failed'); + stream.on("error", () => { + console.error("FS operation failed"); }); }; From 42939c141293bc37e08860940148a9aa752c2ec6 Mon Sep 17 00:00:00 2001 From: toby-28 Date: Mon, 3 Nov 2025 11:51:40 +0500 Subject: [PATCH 27/28] Remove warning against submitting Pull Requests in Readme.md --- Readme.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/Readme.md b/Readme.md index 611a505f49..f01301e365 100644 --- a/Readme.md +++ b/Readme.md @@ -1,3 +1 @@ # Node.js basics - -## !!! Please don't submit Pull Requests to this repository !!! From b13f26286c3c1d41c1dd70d48b11abcdd1795370 Mon Sep 17 00:00:00 2001 From: hushnud <64332592+toby-28@users.noreply.github.com> Date: Sun, 9 Nov 2025 00:51:35 +0500 Subject: [PATCH 28/28] Revise README for clarity and project details This README provides an overview of the project, including goals, folder structure, setup instructions, testing, and contributing guidelines. --- Readme.md | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index f01301e365..6356507648 100644 --- a/Readme.md +++ b/Readme.md @@ -1 +1,116 @@ -# Node.js basics +# ๐Ÿง  Node.js Basics โ€“ Review Branch + +This repository is a **review-enhanced fork** of [AlreadyBored/node-nodejs-basics](https://github.com/AlreadyBored/node-nodejs-basics), structured to align with the [RS School Node.js 2025 curriculum](https://github.com/AlreadyBored/nodejs-assignments). It includes modular implementations of core Node.js concepts, with a focus on clarity, testability, and assignment compliance. + +> ๐Ÿ“Œ **Branch:** `review` โ€” 28 commits ahead, 1 behind upstream `main` + +--- + +## ๐Ÿ“š Project Goals + +- Build a solid foundation in Node.js core modules and APIs +- Practice asynchronous programming and stream handling +- Implement CLI tools, file operations, and compression +- Explore advanced topics like child processes and worker threads +- Prepare for horizontal scaling with the Cluster API (Section 8) + +--- + +## ๐Ÿ“ Folder Structure + +``` +src/ +โ”œโ”€โ”€ cli/ # Command-line interface logic and argument parsing +โ”œโ”€โ”€ cp/ # Child process operations +โ”œโ”€โ”€ fs/ # File system utilities +โ”œโ”€โ”€ hash/ # Hashing functionality +โ”œโ”€โ”€ modules/ # Node.js core modules +โ”œโ”€โ”€ streams/ # Stream-based operations +โ”œโ”€โ”€ wt/ # Worker threads +โ”œโ”€โ”€ zip/ # Compression and decompression logic +``` + +Each folder corresponds to a specific RS School assignment section. All modules are self-contained and can be executed independently. + +--- + +## โš™๏ธ Setup Instructions + +1. **Clone the repository** + ```bash + git clone https://github.com/toby-28/node-nodejs-basics.git + cd node-nodejs-basics + ``` + +2. **Install dependencies** + ```bash + npm install + ``` + +3. **Run a specific module** + ```bash + node src//.js + ``` + + Replace `` and `` with the appropriate path. For example: + ```bash + node src/fs/files/create.js + ``` + +--- + +## ๐Ÿงช Testing + +Some modules include test scripts or validation logic. To run tests (if available): + +```bash +npm test +``` + +> โœ… Ensure Node.js v18+ is installed to support modern APIs like `stream/promises` and `worker_threads`. + +--- + +## ๐Ÿงต Section 8: Horizontal Scaling (Coming Soon) + +The `review` branch is being actively updated to include: +- A custom load balancer using the **Cluster API** +- Round-robin request distribution +- Graceful worker lifecycle management + +Once implemented, this logic will live in a dedicated file (e.g., `src/cluster/multi.ts`) and be documented here. + +--- + +## ๐Ÿง  Learning Outcomes + +By completing this project, youโ€™ll gain hands-on experience with: + +- Node.js runtime and architecture +- CLI tool development +- File system and stream manipulation +- Cryptographic hashing +- Child processes and worker threads +- Compression and decompression +- Modular code organization +- Cluster-based horizontal scaling + +--- + +## ๐Ÿค Contributing + +This branch is maintained for review and educational purposes. Contributions are welcome via: + +- Pull requests +- Issue reports +- Suggestions for clarity or structure + +--- + +## ๐Ÿ“„ License + +This project inherits the license from the original repository. See [LICENSE](https://github.com/AlreadyBored/node-nodejs-basics/blob/main/LICENSE) for details. + +--- + +Made with ๐Ÿ’ก by [@toby-28](https://github.com/toby-28) โ€” based on the work of [@AlreadyBored](https://github.com/AlreadyBored)