Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3655cff
Add create function to write fresh.txt
toby-28 Oct 20, 2025
1bb6c34
Add copy function to copy files recursively
toby-28 Oct 20, 2025
183d96b
Implement file renaming with error handling
toby-28 Oct 20, 2025
18df431
Implement file deletion using fs/promises
toby-28 Oct 20, 2025
8151277
Implement file listing in src/fs/list.js
toby-28 Oct 20, 2025
9c5ef39
Implement file reading with error handling
toby-28 Oct 20, 2025
e94641a
Implement parsing of RSS environment variables
toby-28 Oct 20, 2025
74f9144
Add argument parsing functionality to args.js
toby-28 Oct 20, 2025
6c600d4
Transform cjsToEsm to fully utilize ES Module syntax
toby-28 Oct 20, 2025
3589206
Create esm.mjs for server and JSON imports
toby-28 Oct 20, 2025
fef1faf
Delete src/modules/cjsToEsm.cjs
toby-28 Oct 20, 2025
63a4792
Implement SHA-256 hash calculation with streams
toby-28 Oct 20, 2025
98dc131
Add file reading functionality with error handling
toby-28 Oct 20, 2025
fe96dfa
Implement file writing using streams
toby-28 Oct 20, 2025
25a9839
Implement Transform stream to reverse input
toby-28 Oct 20, 2025
c511c14
Add gzip compression functionality
toby-28 Oct 20, 2025
3d77154
Add decompression functionality for .gz files
toby-28 Oct 20, 2025
0b26dd2
Implement parallel calculations using worker threads
toby-28 Oct 20, 2025
fc58ee0
Add message listener for Fibonacci computation results
toby-28 Oct 20, 2025
fd3968e
Refactor spawnChildProcess to use child_process
toby-28 Oct 20, 2025
28238e8
Refactor file operations to use __dirname for path resolution and sta…
toby-28 Oct 21, 2025
befa038
Refactor file path in create.js to use __dirname
toby-28 Oct 21, 2025
c09049d
Merge branch 'main' of https://github.com/toby-28/node-nodejs-basics
toby-28 Oct 21, 2025
07331fc
Refactor create.js to use __dirname for file path resolution
toby-28 Oct 21, 2025
bc121ad
Refactor string quotes in args.js, env.js, and esm.mjs for consistency
toby-28 Oct 21, 2025
f35b4df
Refactor string quotes in calcHash.js for consistency
toby-28 Oct 21, 2025
4c7ecf7
fully implemented
toby-28 Oct 27, 2025
42939c1
Remove warning against submitting Pull Requests in Readme.md
toby-28 Nov 3, 2025
b13f262
Revise README for clarity and project details
toby-28 Nov 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 115 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,116 @@
# Node.js basics
# 🧠 Node.js Basics – Review Branch

## !!! Please don't submit Pull Requests to this repository !!!
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/<folder>/<file>.js
```

Replace `<folder>` and `<file>` 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)
7 changes: 6 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -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();
7 changes: 6 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -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();
19 changes: 16 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -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']);
23 changes: 22 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
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 () => {
// Write your code here
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");
} catch (err) {
if (err.code === "ENOENT") {
await cp(source, destination, { recursive: true });
} else {
throw err;
}
}
};

await copy();
17 changes: 16 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
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 () => {
// Write your code here
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");
} catch {
await writeFile(filePath, "I am fresh and young", { flag: "wx" });
}
};

await create();
17 changes: 16 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
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 () => {
// Write your code here
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");
}
};

await remove();
18 changes: 17 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
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 () => {
// Write your code here
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");
}
};

await list();
18 changes: 17 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
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 () => {
// Write your code here
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");
console.log(content);
} catch {
throw new Error("FS operation failed");
}
};

await read();
27 changes: 24 additions & 3 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
const rename = async () => {
// Write your code here
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 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");
} catch (err) {
if (err.code === "ENOENT") {
await rename(oldPath, newPath);
} else {
throw err;
}
}
};

await rename();
await renameFile();
28 changes: 25 additions & 3 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -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();
34 changes: 0 additions & 34 deletions src/modules/cjsToEsm.cjs

This file was deleted.

Loading