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
5 changes: 4 additions & 1 deletion 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 values = process.argv.slice(2);
for (let i = 0; i < values.length; i += 2) {
console.log(`${values[i].slice(2)} is ${values[i + 1]}`);
}
};

parseArgs();
6 changes: 5 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const parseEnv = () => {
// Write your code here
for (let [key, value] of Object.entries(process.env)) {
if (key.startsWith("RSS_")) {
console.log(`${key}=${value}`);
}
}
};

parseEnv();
12 changes: 10 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { fork } from "child_process";
import { getDirName, getFileName } from "../utils/utils.js";
import path from "path";

const __filename = getFileName(import.meta.url);
const __dirname = getDirName(__filename);
const childUrl = path.join(__dirname, "./files/script.js");

const spawnChildProcess = async (args) => {
// Write your code here
const child = fork(childUrl, args);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess([]);
15 changes: 14 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { cp } from "node:fs/promises";
import { existsSync } from "node:fs";

const copy = async () => {
// Write your code here
if (existsSync("./src/fs/files_copy")) {
throw Error("FS operation failed");
}

cp("./src/fs/files", "./src/fs/files_copy", {
recursive: true,
errorOnExist: true,
force: false,
}).catch(() => {
throw Error("FS operation failed");
});
};

await copy();
8 changes: 7 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { appendFile } from "node:fs/promises";

const create = async () => {
// Write your code here
appendFile("./src/fs/files/fresh.txt", "I am fresh and young", {
flag: "ax",
}).catch(() => {
throw Error("FS operation failed");
});
};

await create();
6 changes: 5 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { unlink } from "node:fs/promises";

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

await remove();
10 changes: 9 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { readdir } from "node:fs/promises";

const list = async () => {
// Write your code here
readdir("./src/fs/files")
.then((list) => {
console.log(list);
})
.catch(() => {
throw Error("FS operation failed");
});
};

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

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

await read();
14 changes: 13 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fs from "node:fs/promises";
import { existsSync } from "node:fs";

const rename = async () => {
// Write your code here
if (existsSync("./src/fs/files/properFileName.md")) {
throw Error("FS operation failed");
}

fs.rename(
"./src/fs/files/wrongFilename.txt",
"./src/fs/files/properFileName.md"
).catch(() => {
throw Error("FS operation failed");
});
};

await rename();
20 changes: 19 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { createReadStream } from "fs";
import { createHash } from "crypto";
import { stdout } from "process";
import path from "path";
import { fileURLToPath } from "url";
import os from "os";
import { pipeline } from "stream/promises";

const calculateHash = async () => {
// Write your code here
let __filename = fileURLToPath(import.meta.url);
let __dirname = path.dirname(__filename);
const hash = createHash("sha256");
const input = createReadStream(
path.join(__dirname, "files/fileToCalculateHashFor.txt")
);

await pipeline(input, hash.setEncoding("hex"), stdout, {
end: false,
});
console.log(os.EOL);
};

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

This file was deleted.

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

const random = Math.random();

export const unknownObject =
random > 0.5
? await import("./files/a.json", { with: { type: "json" } })
: await import("./files/b.json", { with: { type: "json" } });

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

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

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.default);

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log("To terminate it, use Ctrl+C combination");
});
16 changes: 15 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { getDirName, getFileName } from "../utils/utils.js";
import path from "path";
import { createReadStream } from "fs";
import { pipeline } from "stream/promises";
import os from "os";

const read = async () => {
// Write your code here
const __filename = getFileName(import.meta.url);
const __dirname = getDirName(__filename);

await pipeline(
createReadStream(path.join(__dirname, "files/fileToRead.txt")),
process.stdout,
{ end: false }
);
console.log(os.EOL);
};

await read();
16 changes: 15 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { pipeline } from "stream/promises";
import { Transform } from "stream";

const transform = async () => {
// Write your code here
const transformReverse = new Transform({
transform(chunk, encoding, cb) {
const chunkString = chunk.toString().trim();
const reverseChunk = chunkString.split("").reverse().join("");

this.push(reverseChunk + "\n");

cb();
},
});

await pipeline(process.stdin, transformReverse, process.stdout);
};

await transform();
13 changes: 12 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { getDirName, getFileName } from "../utils/utils.js";
import { pipeline } from "stream/promises";
import { createWriteStream } from "fs";
import path from "path";

const write = async () => {
// Write your code here
const __filename = getFileName(import.meta.url);
const __dirname = getDirName(__filename);

await pipeline(
process.stdin,
createWriteStream(path.join(__dirname, "files/fileToWrite.txt"))
);
};

await write();
10 changes: 10 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { fileURLToPath } from "url";
import { dirname } from "path";

export const getFileName = (metaUrl) => {
return fileURLToPath(metaUrl);
};

export const getDirName = (fileName) => {
return dirname(fileName);
};
36 changes: 35 additions & 1 deletion src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
import { Worker } from "worker_threads";
import os from "os";
import { getDirName, getFileName } from "../utils/utils.js";
import path from "path";

const performCalculations = async () => {
// Write your code here
const START_NUMBER = 10;
const STATUS_RESOLVED = "resolved";
const STATUS_ERROR = "error";
const __filename = getFileName(import.meta.url);
const __dirname = getDirName(__filename);
const workerUrl = path.join(__dirname, "./worker.js");
const cpusAmount = os.cpus().length;
const results = [];

for (let i = 0; i < cpusAmount; i++) {
const workerData = START_NUMBER + i;
const result = () => {
return new Promise((resolve) => {
const worker = new Worker(workerUrl, {
workerData,
});

worker.on("message", (data) =>
resolve({ status: STATUS_RESOLVED, data })
);
worker.on("error", () => resolve({ status: STATUS_ERROR, data: null }));
});
};

results.push(result());
}

const data = await Promise.all(results);

console.log(data);
};

await performCalculations();
10 changes: 8 additions & 2 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
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);

// Get error in worker
// if (Math.random() > 0.5) throw new Error("Fatal error!!");

const sendResult = () => {
// This function sends result of nthFibonacci computations to main thread
parentPort.postMessage(nthFibonacci(workerData));
};

sendResult();
16 changes: 15 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { createGzip } from "zlib";
import { pipeline } from "stream/promises";
import { createReadStream, createWriteStream } from "fs";
import { getDirName, getFileName } from "../utils/utils.js";
import { join } from "path";

const compress = async () => {
// Write your code here
const gzip = createGzip();
const __filename = getFileName(import.meta.url);
const __dirname = getDirName(__filename);

await pipeline(
createReadStream(join(__dirname, "./files/fileToCompress.txt")),
gzip,
createWriteStream(join(__dirname, "./files/archive.gz"))
);
};

await compress();
16 changes: 15 additions & 1 deletion src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { pipeline } from "stream/promises";
import { createReadStream, createWriteStream } from "fs";
import { getDirName, getFileName } from "../utils/utils.js";
import { join } from "path";
import { createUnzip } from "zlib";

const decompress = async () => {
// Write your code here
const unzip = createUnzip();
const __filename = getFileName(import.meta.url);
const __dirname = getDirName(__filename);

await pipeline(
createReadStream(join(__dirname, "./files/archive.gz")),
unzip,
createWriteStream(join(__dirname, "./files/fileToCompress.txt"))
);
};

await decompress();