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
18 changes: 16 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
const parseArgs = () => {
// Write your code here
const filteredArguments = {};

for (let i = 0; i < process.argv.length; i += 1) {
const arg = process.argv[i];
if (arg.startsWith("--")) {
filteredArguments[arg] = process.argv[i + 1];
i += 1;
}
}

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

console.log(resString);
};

parseArgs();
parseArgs();
12 changes: 10 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const parseEnv = () => {
// Write your code here
const filteredEnvs = Object.entries(process.env).filter(([key]) =>
key.startsWith("RSS_")
);

const envsResultString = filteredEnvs.map(
([key, value]) => `${key}=${value}`
);

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

parseEnv();
parseEnv();
20 changes: 17 additions & 3 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";
import path from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = fileURLToPath(new URL(".", import.meta.url));
const scriptPath = path.join(__dirname, "files/script.js");

const spawnChildProcess = async (args) => {
// Write your code here
const child = spawn("node", [scriptPath, ...args], {
stdin: ["inherit", "inherit", "inherit", "ipc"],
});

process.stdin.pipe(child.stdin);

child.stdout.on("data", (data) => {
process.stdout.write(data);
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["a", "b", "c"]);
10 changes: 6 additions & 4 deletions src/cp/files/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ 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);
12 changes: 11 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { cp } from "node:fs/promises";

const copy = async () => {
// Write your code here
try {
await cp("src/fs/files", "src/fs/files_copy", {
recursive: true,
errorOnExist: true,
force: false,
});
} catch (error) {
throw Error("FS operation failed");
}
};

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

const create = async () => {
// Write your code here
try {
try {
await writeFile("src/fs/files/fresh.txt", "I am fresh and young", {
flag: "wx",
});
} catch (error) {
throw Error("FS operation failed");
}
} catch (error) {
console.log(error);
}
};

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

const remove = async () => {
// Write your code here
try {
await rm("src/fs/files/fileToRemove.txt").catch(() => {
throw Error("FS operation failed");
});
} catch (error) {
console.log(error);
}
};

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

const list = async () => {
// Write your code here
const allFilesFolderContent = await readdir("src/fs/files", {
withFileTypes: true,
}).catch(() => {
throw Error("FS operation failed");
});

const onlyFiles = allFilesFolderContent.filter((file) => file.isFile());

const onlyFilesNames = onlyFiles.map((file) => file.name.split(".")[0]);

console.log(onlyFilesNames);
};

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

const read = async () => {
// Write your code here
try {
const content = await readFile("src/fs/files/fileToRead.txt", "utf8").catch(
() => {
throw Error("FS operation failed");
}
);
console.log(content);
} catch (error) {
console.log(error);
}
};

await read();
await read();
21 changes: 19 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { stat, rename as fsRename, open } from "node:fs/promises";

const rename = async () => {
// Write your code here
try {
const stats = await stat("src/fs/files/properFilename.md").catch(() => {});

if (stats) {
throw Error("FS operation failed");
}

await fsRename(
"src/fs/files/wrongFilename.txt",
"src/fs/files/properFilename.md"
).catch(() => {
throw Error("FS operation failed");
});
} catch (error) {
console.log(error);
}
};

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

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

readStream
.on("data", (chunk) => {
hash.update(chunk);
})
.on("end", () => {
console.log(hash.digest("hex"));
});
};

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/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { sep } from "node:path";
import { release, version } from "node:os";
import { createServer as createServerHttp } from "node:http";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = fileURLToPath(new URL(".", import.meta.url));

await import("./files/c.js");

const random = Math.random();

export let unknownObject;

if (random > 0.5) {
unknownObject = await import("./files/a.json", { with: { type: "json" } });
} else {
unknownObject = await import("./files/b.json", { with: { 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");
});
13 changes: 11 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { createReadStream } from "node:fs";
import { EOL } from "node:os";

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

readStream.on("data", (chunk) => {
process.stdout.write(`${chunk}${EOL}`);
});
};

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

const transform = async () => {
// Write your code here
const reverseTransformStream = new Transform({
transform: (chunk, _, callback) => {
callback(null, `${chunk.toString().split("").reverse().join("")}${EOL}`);
},
});

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

await transform();
await transform();
10 changes: 8 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createWriteStream } from "node:fs";

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

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

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

const __dirname = fileURLToPath(new URL(".", import.meta.url));

const performCalculations = async () => {
// Write your code here
const coresCount = cpus().length;
const pathToWorker = path.join(__dirname, "worker.js");
const arrayFromCores = Array.from({ length: coresCount });

const workerPromises = arrayFromCores.map((_, i) => {
return new Promise((resolve) => {
const workerData = 10 + i;
const worker = new Worker(pathToWorker, { workerData });

worker.on("message", (result) => {
resolve(result);
});
});
});

const workerResults = await Promise.all(workerPromises);

console.log("workerResults", workerResults);
};

await performCalculations();
await performCalculations();
Loading