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
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseArgs = () => {
// Write your code here
const allArgs = process.argv.slice(2);
if (allArgs.length === 0) return;
let output = "";
allArgs.forEach((item, index, arr) => {
if (index === 0) {
output += `${item.slice(2)} is ${arr[index + 1]}`;
} else if (index % 2 === 0) {
output += `, ${item.slice(2)} is ${arr[index + 1]}`;
} else return;
});
console.log(output);
};

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 allEnvKeys = Object.keys(process.env);
const rssKeys = allEnvKeys.filter((key) => key.startsWith("RSS_"));
if (rssKeys.length === 0) return;
let output = "";
rssKeys.forEach((key, index) => {
if (index === 0) {
output += `${key}=${process.env[key]}`;
} else {
output += `; ${key}=${process.env[key]}`;
}
});
console.log(output);
};

parseEnv();
parseEnv();
11 changes: 8 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { fork } from "node:child_process";
import * as url from "url";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

const spawnChildProcess = async (args) => {
// Write your code here
const child = fork(__dirname + "/files/script.js", args, { silent: true });
process.stdin.pipe(child.stdin);
child.stdout.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["one", "two", "three"]);
21 changes: 20 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { readdir, mkdir, copyFile } from "node:fs/promises";

const copy = async () => {
// Write your code here
const dirСonsistency = await readdir("./src/fs/");
const isFilesDir = dirСonsistency.includes("files");
const isFilesCopyDir = dirСonsistency.includes("files_copy");

if (!isFilesDir || isFilesCopyDir) {
throw new Error("FS operation failed");
} else {
await mkdir("./src/fs/files_copy");
const dirСonsistency = await readdir("./src/fs/files");
dirСonsistency
.filter((item) => item.includes("."))
.forEach((fileName) => {
copyFile(
`./src/fs/files/${fileName}`,
`./src/fs/files_copy/${fileName}`
);
});
}
};

await copy();
11 changes: 9 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { writeFile, readdir } from "node:fs/promises";

const create = async () => {
// Write your code here
const dirFiles = await readdir("./src/fs/files/");
if (dirFiles.includes("fresh.txt")) {
throw new Error("FS operation failed");
} else {
writeFile("./src/fs/files/fresh.txt", "I am fresh and young");
}
};

await create();
await 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 { readdir, rm } from "node:fs/promises";

const remove = async () => {
// Write your code here
const dirСonsistency = await readdir("./src/fs/files");
const isfileToRemove = dirСonsistency.includes("fileToRemove.txt");

if (!isfileToRemove) {
throw new Error("FS operation failed");
} else {
await rm("./src/fs/files/fileToRemove.txt");
}
};

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

const list = async () => {
// Write your code here
const dirСonsistency = await readdir("./src/fs/");
const isFilesDir = dirСonsistency.includes("files");

if (!isFilesDir) {
throw new Error("FS operation failed");
} else {
const dirСonsistency = await readdir("./src/fs/files");
dirСonsistency.forEach((fileName) => console.log(fileName));
}
};

await list();
await list();
14 changes: 12 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { readdir, readFile } from "node:fs/promises";

const read = async () => {
// Write your code here
const dirСonsistency = await readdir("./src/fs/files");
const isfileToRead = dirСonsistency.includes("fileToRead.txt");

if (!isfileToRead) {
throw new Error("FS operation failed");
} else {
const content = await readFile("./src/fs/files/fileToRead.txt");
console.log(content.toString());
}
};

await read();
await read();
17 changes: 15 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { readdir, rename as nodeRename } from "node:fs/promises";

const rename = async () => {
// Write your code here
const dirСonsistency = await readdir("./src/fs/files");
const isWrongFilename = dirСonsistency.includes("wrongFilename.txt");
const isProperFilename = dirСonsistency.includes("properFilename.md");

if (!isWrongFilename || isProperFilename) {
throw new Error("FS operation failed");
} else {
await nodeRename(
"./src/fs/files/wrongFilename.txt",
"./src/fs/files/properFilename.md"
);
}
};

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

const calculateHash = async () => {
// Write your code here
const readStream = createReadStream(
"./src/hash/files/fileToCalculateHashFor.txt",
{ encoding: "utf8" }
);
const hash = createHash("sha256");

readStream.on("readable", () => {
const data = readStream.read();
if (data) hash.update(data);
else {
console.log(`${hash.digest("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 path from "path";
import { release, version } from "os";
import { createServer as createServerHttp } from "http";
import "./files/c.js";
import * as url from "url";
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

const random = Math.random();

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 "${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(unknownObject);

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log("To terminate it, use Ctrl+C combination");
});
10 changes: 8 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createReadStream } from "node:fs";
import { stdout } from "node:process";

const read = async () => {
// Write your code here
const stream = createReadStream("./src/streams/files/fileToRead.txt", {
encoding: "utf8",
});
stream.pipe(stdout);
};

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 "node:stream";

const transform = async () => {
// Write your code here
const transformStream = new Transform({
transform(chunk, encoding, callback) {
this.push(("\n" + chunk.toString()).split("").reverse().join(""));
callback();
},
});
process.stdout.write(
"Hello!\nYou can write some data below\nPlease press Ctrl + C to exit\n"
);
process.stdin.pipe(transformStream).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 { createWriteStream } from "node:fs";

const write = async () => {
// Write your code here
const stream = createWriteStream("./src/streams/files/fileToWrite.txt", {
encoding: "utf8",
});

process.stdout.write(
"Hello!\nYou can write some data below\nPlease press Ctrl + C to exit\n"
);
process.stdin.pipe(stream);
process.stdin.resume();
};

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 { cpus } from "os";
import { Worker } from "worker_threads";
import * as url from "url";
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));

const performCalculations = async () => {
// Write your code here
const cpusInfoArr = cpus();
const resultsArr = await Promise.all(
cpusInfoArr.map((_cpu, index) => {
return new Promise((res) => {
const worker = new Worker(__dirname + "worker.js", {
workerData: index + 10,
});
worker.on("message", (calcResult) => {
res({ status: "resolved", data: calcResult });
});
worker.on("error", (calcResult) => {
res({ status: "error", data: null });
});
worker.on("exit", (code) => {
if (code !== 0)
throw new Error(`Worker stopped with exit code ${code}`);
});
});
})
);
console.log(resultsArr);
};

await performCalculations();
await performCalculations();
11 changes: 8 additions & 3 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { workerData, parentPort } 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
const data = nthFibonacci(workerData);
parentPort.postMessage(data);
// This function sends result of nthFibonacci computations to main thread
};

sendResult();
sendResult();
Loading