Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basics node #555

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 7 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const parseArgs = () => {
// Write your code here
// Write your code here
const argv = process.argv.slice(2);

for (let i = 0; i < argv.length; i++) {
if (argv[i].startsWith("--")) console.log(`${argv[i]} is ${argv[i + 1]}`);
}
};

parseArgs();
parseArgs();
7 changes: 5 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const parseEnv = () => {
// Write your code here
// Write your code here
for (const key in process.env) {
if (key.startsWith("RSS_")) console.log(`${key}=${process.env[key]}`);
}
};

parseEnv();
parseEnv();
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ERROR_FS = "FS operation failed";
24 changes: 22 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import cp from "child_process";

const spawnChildProcess = async (args) => {
// Write your code here
// Write your code here
const childp = cp.spawn("node", [
import.meta.dirname + "/files/script.js",
...args,
]);

// Pipe the stdin of the parent process to the child process
process.stdin.on("data", (data) => {
childp.stdin.write(data + "\n");
});
// or
// process.stdin.pipe(childp.stdin);

// Pipe the stdout of the child process to the stdout of the parent process
childp.stdout.on("data", (data) => {
process.stdout.write(data + "\n");
});
// or
// childp.stdout.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(process.argv.slice(2));
8 changes: 4 additions & 4 deletions src/cp/files/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ console.log(`Total number of arguments is ${args.length}`);
console.log(`Arguments: ${JSON.stringify(args)}`);

const echoInput = (chunk) => {
const chunkStringified = chunk.toString();
if (chunkStringified.includes('CLOSE')) process.exit(0);
process.stdout.write(`Received from master process: ${chunk.toString()}\n`)
const chunkStringified = chunk.toString();
if (chunkStringified.includes("CLOSE")) process.exit(0);
process.stdout.write(`Received from master process: ${chunk.toString()}\n`);
};

process.stdin.on('data', echoInput);
process.stdin.on("data", echoInput);
16 changes: 15 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from "fs";
import { ERROR_FS } from "../constants.js";
import { isFileExist } from "../shared.js";

const copy = async () => {
// Write your code here
// Write your code here
const copy = "./src/fs/files_copy";
const src = "./src/fs/files";

if (!(await isFileExist(src)) || (await isFileExist(copy)))
throw Error(ERROR_FS);
else {
fs.cp(src, copy, { recursive: true }, (err) => {
if (err) throw err;
});
}
};

await copy();
15 changes: 13 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from "fs";
import { ERROR_FS } from "../constants.js";
import { isFileExist } from "../shared.js";

const create = async () => {
// Write your code here
// Write your code here
const fresh = "./src/fs/files/fresh.txt";

if (!(await isFileExist(fresh)))
fs.appendFile(fresh, "I am fresh and young", (err) => {
if (err) throw err;
});
else throw Error(ERROR_FS); // file already exist
};

await create();
await create();
17 changes: 15 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from "fs";
import { ERROR_FS } from "../constants.js";
import { isFileExist } from "../shared.js";

const remove = async () => {
// Write your code here
// Write your code here
const fileToRemove = "./src/fs/files/fileToRemove.txt";

if (!(await isFileExist(fileToRemove)))
throw Error(ERROR_FS); // file doesn't exist
else {
fs.rm(fileToRemove, (err) => {
if (err) throw err;
});
}
};

await remove();
await remove();
1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt

This file was deleted.

File renamed without changes.
17 changes: 15 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from "fs";
import { ERROR_FS } from "../constants.js";
import { isFileExist } from "../shared.js";

const list = async () => {
// Write your code here
// Write your code here
const filesDir = "./src/fs/files";

if (!(await isFileExist(filesDir))) throw Error(ERROR_FS);
else {
fs.readdir(filesDir, (err, files) => {
if (err) throw err;
else console.log(files);
});
}
};

await list();
await list();
19 changes: 17 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from "fs";
import { ERROR_FS } from "../constants.js";
import { isFileExist } from "../shared.js";

const read = async () => {
// Write your code here
// Write your code here
const fileToRead = "./src/fs/files/fileToRead.txt";

const readFile = (file) => {
fs.readFile(file, "utf8", (err, data) => {
if (err) throw err;
else console.log(data);
});
};

if (!(await isFileExist(fileToRead))) throw Error(ERROR_FS);
else readFile(fileToRead);
};

await read();
await read();
18 changes: 16 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from "fs";
import { ERROR_FS } from "../constants.js";
import { isFileExist } from "../shared.js";

const rename = async () => {
// Write your code here
// Write your code here
const oldFile = "./src/fs/files/wrongFilename.txt";
const newFile = "./src/fs/files/properFilename.md";

if (!(await isFileExist(oldFile)) || (await isFileExist(newFile)))
throw Error(ERROR_FS);
else {
fs.rename(oldFile, newFile, (err) => {
if (err) throw err; // newFile is a directory
});
}
};

await rename();
await rename();
15 changes: 13 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from "fs";
import crypto from "crypto";

const calculateHash = async () => {
// Write your code here
// Write your code here
const file = fs.createReadStream(
"./src/hash/files/fileToCalculateHashFor.txt"
);
const hash = crypto.createHash("sha256");

file.pipe(hash); // Pipes the file data into the hash object

console.log(hash.digest("hex")); // transform to hex
};

await calculateHash();
await calculateHash();
40 changes: 0 additions & 40 deletions src/modules/cjsToEsm.cjs

This file was deleted.

36 changes: 36 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from "fs";
import path from "node:path";
import { release, version } from "node:os";
import { createServer } from "node:http";
import "./files/c.js";

const parsedData = (path) =>
JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));

const random = Math.random();

const unknownObject = parsedData(
random > 0.5 ? "./files/a.json" : "./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 ${import.meta.filename}`);
console.log(`Path to current directory is ${import.meta.dirname}`);

const myServer = createServer((_, 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 };
8 changes: 8 additions & 0 deletions src/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from "fs";

export const isFileExist = async (file) =>
new Promise((resolve) => {
fs.access(file, fs.constants.F_OK, (err) => {
resolve(!err); // resolves `true` if file exists, `false` if it doesn't
});
});
1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 10 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import fs from "fs";

const read = async () => {
// Write your code here
// Write your code here
const stream = fs.createReadStream("src/streams/files/fileToRead.txt");

stream.on("data", (chunk) => {
chunk = chunk.toString();
process.stdout.write(chunk + "\n");
});
};

await read();
await read();
15 changes: 13 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { Transform } from "stream";

const transform = async () => {
// Write your code here
// Write your code here

const reverseStream = new Transform({
transform(chunk, encoding, callback) {
// Reverse the chunk and pass it to the readable side
callback(null, chunk.toString().split("").reverse().join(""));
},
});

process.stdin.pipe(reverseStream).pipe(process.stdout);
};

await transform();
await transform();
14 changes: 12 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fs from "fs";

const write = async () => {
// Write your code here
// Write your code here
const stream = fs.createWriteStream("src/streams/files/fileToWrite.txt");

process.stdin.on("data", (chunk) => {
stream.write(chunk);
});

// or ?
// process.stdin.pipe(stream);
};

await write();
await write();
Loading