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
11 changes: 9 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const data = new Array(Math.ceil(args.length / 2))
.fill()
.map((_) => args.splice(0, 2))
.map(([key, value]) => `${key} is ${value}`)
.join(", ");

console.log(data);
};

parseArgs();
parseArgs();
15 changes: 13 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const parseEnv = () => {
// Write your code here
const data = Object.keys(process.env)
.filter((key) => key.startsWith("RSS_"))
.reduce((obj, key) => {
obj[key] = process.env[key];
return obj;
}, {});

const output = Object.entries(data)
.map(([key, value]) => `${key}=${value}`)
.join("; ");

console.log(output);
};

parseEnv();
parseEnv();
20 changes: 18 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { fork } from "child_process";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const source = join(
dirname(fileURLToPath(import.meta.url)),
"./files/script.js"
);

const spawnChildProcess = async (args) => {
// Write your code here
const child = fork(source, args);
child.on("data", function (message) {
console.log(`Message from child.js: ${message}`);
});
child.on("close", function (code) {
console.log("child process exited with code " + code);
});
child.send(args);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["--cat", "--dog", "--parrot"]);
18 changes: 17 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { access, cp, mkdir } from "fs/promises";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const source = join(dirname(fileURLToPath(import.meta.url)), "./files");
const destination = join(
dirname(fileURLToPath(import.meta.url)),
"./files_copy"
);

const copy = async () => {
// Write your code here
try {
await access(source);
await mkdir(destination);
await cp(source, destination, { recursive: true });
} catch (error) {
throw new Error("FS operation failed");
}
};

await copy();
14 changes: 12 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { writeFile } from "fs/promises";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const path = join(dirname(fileURLToPath(import.meta.url)), "./files/fresh.txt");

const create = async () => {
// Write your code here
try {
await writeFile(path, "I am fresh and young", { flag: "wx" });
} catch (err) {
throw new Error("FS operation failed");
}
};

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 { rm } from "fs/promises";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const path = join(
dirname(fileURLToPath(import.meta.url)),
"./files/fileToRemove.txt"
);

const remove = async () => {
// Write your code here
try {
await rm(path);
} catch (error) {
throw new Error("FS operation failed");
}
};

await remove();
await remove();
1 change: 0 additions & 1 deletion src/fs/files/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
How dare you!
15 changes: 13 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { readdir } from "fs/promises";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const path = join(dirname(fileURLToPath(import.meta.url)), "./files");

const list = async () => {
// Write your code here
try {
const fileList = await readdir(path);
console.log(fileList);
} catch (error) {
throw new Error("FS operation failed");
}
};

await list();
await list();
18 changes: 16 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { readFile } from "fs/promises";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const path = join(
dirname(fileURLToPath(import.meta.url)),
"./files/fileToRead.txt"
);

const read = async () => {
// Write your code here
try {
const data = await readFile(path, "utf-8");
console.log(data);
} catch (error) {
throw new Error("FS operation failed");
}
};

await read();
await read();
23 changes: 20 additions & 3 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const rename = async () => {
// Write your code here
import { rename } from "fs/promises";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const wrongFilename = join(
dirname(fileURLToPath(import.meta.url)),
"./files/wrongFilename.txt"
);
const properFilename = join(
dirname(fileURLToPath(import.meta.url)),
"./files/properFilename.md"
);

const renameF = async () => {
try {
await rename(wrongFilename, properFilename);
} catch (error) {
throw new Error("FS operation failed");
}
};

await rename();
await renameF();
17 changes: 15 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { readFile } from "fs/promises";
import { createHash } from "node:crypto";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const path = join(dirname(fileURLToPath(import.meta.url)), "./files/fileToCalculateHashFor.txt");

const calculateHash = async () => {
// Write your code here
try {
const data = await readFile(path, "utf-8");
const hex = createHash("sha256").update(data).digest("hex");
console.log(hex);
} catch (error) {
throw new Error(error);
}
};

await calculateHash();
await calculateHash();
38 changes: 38 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { sep, dirname } from "path";
import { fileURLToPath } from "url";
import { release, version } from "os";
import { createServer as createServerHttp } from "http";
import "./files/c.js";

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

const random = Math.random();

export let unknownObject;

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

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

console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

export 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");
});
22 changes: 20 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { createReadStream } from "fs";
import { stdout } from "process";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const source = join(
dirname(fileURLToPath(import.meta.url)),
"./files/fileToRead.txt"
);
const readableStream = createReadStream(source);

const read = async () => {
// Write your code here
try {
readableStream.on("open", () => {
rs.pipe(stdout);
});
readableStream.on("end", () => {});
} catch (error) {
throw new Error();
}
};

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

async function reverseStream() {
return Transform({
transform: function (str, _, next) {
next(null, str.toString().split("").reverse().join("") + "\n");
},
});
}

const transform = async () => {
// Write your code here
try {
stdin.pipe(await reverseStream()).pipe(stdout);
} catch (error) {
throw new Error(error);
}
};

await transform();
await transform();
20 changes: 18 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { createWriteStream } from "fs";
import { stdin } from "process";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const source = join(
dirname(fileURLToPath(import.meta.url)),
"./files/fileToWrite.txt"
);

const writableStream = createWriteStream(source, { flags: "a" });

const write = async () => {
// Write your code here
try {
stdin.pipe(writableStream, { end: false });
} catch (error) {
throw new Error();
}
};

await write();
await write();
29 changes: 27 additions & 2 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { Worker } from "worker_threads";
import { cpus } from "os";
import { fileURLToPath } from "url";
import { join, dirname } from "path";

const workerFile = join(dirname(fileURLToPath(import.meta.url)), "./worker.js");

const THREAD_COUNT = cpus().length;

function createWorker(num) {
return new Promise(function (resolve, reject) {
const worker = new Worker(workerFile, { workerData: { thread: num } });
worker.on("message", (data) => resolve({ status: "resolved", data }));
worker.on("error", () => resolve({ status: "error", data: null }));
});
}

const performCalculations = async () => {
// Write your code here
const workerPromises = [];
const threadResults = [];
for (let i = 0; i < THREAD_COUNT; i++) {
workerPromises.push(createWorker(10 + i));
}
await Promise.allSettled(workerPromises).then((results) =>
results.forEach((result) => threadResults.push(result))
);
console.log(threadResults);
};

await performCalculations();
await performCalculations();
9 changes: 6 additions & 3 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { parentPort, workerData } from "worker_threads";

// n should be received from main thread
const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2);
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.postMessage(nthFibonacci(workerData.thread));
};

sendResult();
sendResult();
Loading