Skip to content
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
16 changes: 14 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const parseArgs = () => {
// Write your code here
const clArgs = process.argv;
let result = [];
for (let i = 2; i < clArgs.length; i += 2) {
const propName = clArgs[i].replace("--", "");
const propValue = clArgs[i + 1];
if (i + 2 < clArgs.length) {
result.push(`${propName} is ${propValue},`);
} else {
result.push(`${propName} is ${propValue}`);
}
}

console.log(result.join(" "));
};

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 envVariables = Object.entries(process.env);
const rssEnvVariables = envVariables
.filter(([key]) => key.startsWith("RSS_"))
.map(([key, value]) => `${key}=${value}`)
.join("; ");

console.log(rssEnvVariables);
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const pathToFile = "src/cp/files/";
const fileName = "script.js";
const child = spawn("node", [pathToFile + fileName, ...args], {
stdio: ["pipe", "pipe", "inherit", "ipc"],
});
process.stdin.pipe(child.stdin);
child.stdout.pipe(process.stdout);

child.on("message", (data) => {
console.log(`Received from child process via IPC: ${data}`);
});
};
const someArgument1 = "Test";
const someArgument2 = 123;

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess([someArgument1, someArgument2]);
12 changes: 8 additions & 4 deletions src/cp/files/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ 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);

if (process.send) {
process.send(`Child process started with arguments: ${JSON.stringify(args)}`);
}
16 changes: 14 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fsPromises from "node:fs/promises";
import { throwError } from "../utils/customError.js";

const copy = async () => {
// Write your code here
try {
const src = "src/fs/files/";
const dest = "src/fs/files_copy/";
await fsPromises.cp(src, dest, {
recursive: true,
errorOnExist: true,
force: false,
});
} catch {
throwError();
}
};

await copy();
29 changes: 27 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import fsPromises from "node:fs/promises";
import { throwError } from "../utils/customError.js";

const create = async () => {
// Write your code here
try {
const pathToSave = "src/fs/files/";
const fileName = "fresh.txt";
const fileContent = "I am fresh and young";
const exists = await fileExists(pathToSave + fileName);
if (exists) {
throwError();
}
await fsPromises.writeFile(pathToSave + fileName, fileContent);
} catch {
throwError();
}
};

const fileExists = async (filePath) => {
try {
await fsPromises.access(filePath);
return true;
} catch (err) {
if (err.code === "ENOENT") {
return false;
}
}
};

await create();
create();
13 changes: 11 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fsPromises from "node:fs/promises";
import { throwError } from "../utils/customError.js";

const remove = async () => {
// Write your code here
try {
const pathToFile = "src/fs/files/";
const fileName = "fileToRemove.txt";
await fsPromises.rm(pathToFile + fileName);
} catch {
throwError();
}
};

await remove();
await remove();
13 changes: 11 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fsPromises from "node:fs/promises";
import { throwError } from "../utils/customError.js";

const list = async () => {
// Write your code here
try {
const path = "src/fs/files/";
const files = await fsPromises.readdir(path, { recursive: true });
for (const file of files) console.log(file);
} catch {
throwError();
}
};

await list();
await list();
16 changes: 14 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fsPromises from "node:fs/promises";
import { throwError } from "../utils/customError.js";

const read = async () => {
// Write your code here
try {
const pathToFile = "src/fs/files/";
const fileName = "fileToRead.txt";
const text = await fsPromises.readFile(pathToFile + fileName, {
encoding: "utf8",
});
console.log(text);
} catch {
throwError();
}
};

await read();
await read();
13 changes: 11 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fsPromises from "node:fs/promises";
import { throwError } from "../utils/customError.js";

const rename = async () => {
// Write your code here
try {
const oldPath = "src/fs/files/wrongFilename.txt";
const newPath = "src/fs/files/properFilename.md";
await fsPromises.rename(oldPath, newPath);
} catch {
throwError();
}
};

await rename();
await rename();
24 changes: 22 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { createReadStream } from "node:fs";
import { createHash } from "node:crypto";
import { throwError } from "../utils/customError.js";

const calculateHash = async () => {
// Write your code here
try {
const pathToFile = "src/hash/files/";
const fileName = "fileToCalculateHashFor.txt";
const hash = createHash("sha256");
const stream = createReadStream(pathToFile + fileName);
stream.on("data", (chunk) => hash.update(chunk));
stream.on("end", () => {
const hexHash = hash.digest("hex");
console.log(`hex: ${hexHash}`);
});

stream.on("error", () => {
throwError();
});
} catch {
throwError();
}
};

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

This file was deleted.

41 changes: 41 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path from "path";
import { fileURLToPath } from 'url';
import { release, version } from "os";
import { createServer as createServerHttp } from "http";
import * as a from "./files/a.json" with { type: "json" };
import * as b from "./files/b.json" with { type: "json" };
import * as c from "./files/c.js";

const random = Math.random();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

let randomData;

if (random > 0.5) {
randomData = a;
} else {
randomData = b;
}

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);

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(randomData);

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

export default {randomData, myServer};
23 changes: 21 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import fs from "fs";
import { throwError } from "../utils/customError.js";

const read = async () => {
// Write your code here
let data = "";
const pathToFile = "src/streams/files/";
const fileName = "fileToRead.txt";
const readerStream = fs.createReadStream(pathToFile + fileName);
readerStream.setEncoding("UTF8");

readerStream.on("data", function (chunk) {
data += chunk;
});

readerStream.on("end", function () {
console.log(data);
});

readerStream.on("error", function () {
throwError();
});
};

await read();
await read();
25 changes: 23 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { Transform } from "node:stream";
import { throwError } from "../utils/customError.js";

const transform = async () => {
// Write your code here
process.stdin
.pipe(reverseTransform)
.pipe(process.stdout)
.on("error", () => {
throwError();
});

process.stdin.on("error", () => {
throwError();
});
};

await transform();
const reverseTransform = new Transform({
transform(chunk, encoding, callback) {
const data =
encoding === "buffer" ? chunk.toString() : chunk.toString(encoding);
this.push(data.split("").reverse().join(""));
callback();
},
});

await transform();
17 changes: 15 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from "fs";
import { throwError } from "../utils/customError.js";

const write = async () => {
// Write your code here
const pathToFile = "src/streams/files/";
const fileName = "fileToWrite.txt";
const writeStream = fs.createWriteStream(pathToFile + fileName);
process.stdin.pipe(writeStream);
process.stdin.resume();
writeStream.on("finish", () => {
console.log("Data successfully written to the file.");
});
writeStream.on("error", () => {
throwError();
});
};

await write();
await write();
Loading