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

Nodejs basics assignment #565

Open
wants to merge 10 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
4 changes: 1 addition & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# Node.js basics

## !!! Please don't submit Pull Requests to this repository !!!
## Node.js basics
7 changes: 5 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const parseArgs = () => {
// Write your code here
const variables = Object.keys(process.env);
variables.forEach((v) => {
console.log(`${v} is ${process.env[v]},`);
});
};

parseArgs();
parseArgs();
10 changes: 8 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const parseEnv = () => {
// Write your code here
const variables = Object.keys(process.env);
const prefix = "NVM";
variables.forEach((v) => {
if (v.startsWith(prefix)) {
console.log(`${v}=${process.env[v]};`);
}
});
};

parseEnv();
parseEnv();
11 changes: 9 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { fork, spawn } from "child_process";

const spawnChildProcess = async (args) => {
// Write your code here
const child = fork("./files/script.js", args, {
stdio: ["pipe", "pipe", "pipe", "ipc"],
});

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["someArgument1", "someArgument2"]);
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 fs from "fs";

const copy = async () => {
// Write your code here
fs.access("./files", function (error) {
if (error) {
// files does not exist.
throw new Error("FS operation failed");
}
});

fs.access("./files_copy", function (error) {
if (error) {
fs.cp("./files", "./files_copy", { recursive: true }, (err) => {
if (err) {
console.error(err);
}
console.log("Copied");
});
return;
}
// files_copy exists
throw new Error("FS operation failed");
});
};

await copy();
20 changes: 18 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import fs from "fs";

const create = async () => {
// Write your code here
fs.access("./files/fresh.txt", fs.constants.F_OK, (error) => {
if (error) {
fs.appendFile(
"./files/fresh.txt",
"I am fresh and young",
function (err) {
if (err) throw err;
console.log("Saved!");
}
);
return;
}

throw new Error("FS operation failed");
});
};

await create();
await create();
18 changes: 16 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from "fs";
const remove = async () => {
// Write your code here
fs.access("./files/fileToRemove.txt", (error) => {
if (error) {
// fileToRemove does not exist.
throw new Error("FS operation failed");
}
fs.unlink("./files/fileToRemove.txt", (err) => {
if (err) {
console.error(`Error removing file: ${err}`);
return;
}

console.log(`successfully removed.`);
});
});
};

await remove();
await remove();
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";
const list = async () => {
// Write your code here
fs.access("./files", (error) => {
if (error) {
// files does not exist.
throw new Error("FS operation failed");
}
fs.readdir("./files", (err, files) => {
let fileNames = [];
files.forEach((file) => {
fileNames.push(file);
});
console.log(fileNames);
});
});
};

await list();
await list();
17 changes: 15 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from "fs";
const read = async () => {
// Write your code here
fs.access("./files/fileToRead.txt", (error) => {
if (error) {
// fileToRead does not exist.
throw new Error("FS operation failed");
}
fs.readFile("./files/fileToRead.txt", "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});
});
};

await read();
await read();
26 changes: 24 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import fs from "fs";

const rename = async () => {
// Write your code here
fs.access("./files/wrongFilename.txt", function (error) {
if (error) {
// wrongFilename does not exist.
throw new Error("FS operation failed");
}
});

fs.access("./files/properFilename.md", function (error) {
if (error) {
fs.rename(
"./files/wrongFilename.txt",
"./files/properFilename.md",
() => {
console.log("File Renamed!");
}
);
return;
}
// properFilename exists
throw new Error("FS operation failed");
});
};

await rename();
await rename();
27 changes: 25 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { Writable } from "stream";
import { createHash } from "crypto";
import fs from "fs";

const calculateHash = async () => {
// Write your code here
fs.readFile("./files/fileToCalculateHashFor.txt", "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}

let hashed = createHash("sha256").update("diyora").digest();
// one way
let toLog = Buffer.from(hashed).toString("hex");
console.log(toLog);

// second way
const hexStream = new Writable({
write(chunk, encoding, callback) {
console.log(chunk.toString("hex"));
callback();
},
});
hexStream.write(hashed);
});
};

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

This file was deleted.

39 changes: 39 additions & 0 deletions src/modules/cjsToEsm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import "./files/c.js";

import { release, version } from "os";

import { createServer as createServerHttp } from "http";
import path from "path";

const random = Math.random();
let unknownObject;

(async () => {
if (random > 0.5) {
unknownObject = await import("./files/a.json", { with: { type: "json" } });
console.log("unknownObject", unknownObject);
} else {
unknownObject = await import("./files/b.json", { with: { type: "json" } });
console.log("unknownObject", unknownObject);
}
})();

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 = createServerHttp((_, res) => {
res.end("Request accepted");
});

const PORT = 3000;

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log("To terminate it, use Ctrl+C combination");
});

export { unknownObject, myServer };
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
const readableStream = fs.createReadStream("./files/fileToRead.txt", "utf8");

readableStream.on("error", function (error) {
console.log(`error: ${error.message}`);
});

readableStream.pipe(process.stdout);
};

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

const transform = async () => {
// Write your code here
function reverseInputText() {
const reverseStream = new Transform({
transform(chunk, encoding, callback) {
const reversedChunk = chunk.toString().split("").reverse().join("");
this.push(reversedChunk);
callback();
},
});
}
process.stdin.pipe(reverseInputText).pipe(process.stdout);
};

await transform();
await transform();

// I couldn't do it((
11 changes: 9 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from "fs";

const write = async () => {
// Write your code here
const writeStream = fs.createWriteStream("./files/fileToWrite.txt", "utf8");

process.stdin.on("data", (data) => {
writeStream.write(data);
process.exit();
});
};

await write();
await write();
Loading