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
10 changes: 8 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const parseArgs = () => {
// Write your code here
const parsedArgs = process.argv.slice(2);
const result = [];
for (let i = 0; i < parsedArgs.length; i += 2) {
const propName = parsedArgs[i].replace("--", "");
result.push(propName + " is " + parsedArgs[i + 1]);
}
console.log(result.join(", "));
};

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 rssVariables = Object.entries(process.env).filter(([key, _]) =>
key.startsWith("RSS_"),
);

const formattedString = rssVariables
.map(([key, value]) => key + "=" + value)
.join("; ");

console.log(formattedString);
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const filePath = path.join(import.meta.dirname + "/files/script.js");
const child = spawn("node", [filePath, ...args]);

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

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

const copy = async () => {
// Write your code here
const oldFolderPath = path.join(import.meta.dirname + "/files");
const newFolderPath = path.join(import.meta.dirname + "/copy_files");
try {
fs.mkdirSync(newFolderPath);
fs.readdirSync(oldFolderPath).forEach((file) => {
const oldFilePath = path.join(oldFolderPath + "/" + file);
const newFilePath = path.join(newFolderPath + "/" + file);
fs.copyFileSync(oldFilePath, newFilePath);
});
} catch (err) {
throw Error("FS operation failed");
}
};

await copy();
18 changes: 16 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from "fs";
import path from "node:path";

const create = async () => {
// Write your code here
const content = "I am fresh and young";
const filePath = path.join(import.meta.dirname + "/files/fresh.txt");

if (fs.existsSync(filePath)) {
throw Error("FS operation failed");
}

try {
fs.writeFileSync(filePath, content, { flag: "w+" });
} catch (err) {
throw Error("FS operation failed");
}
};

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 fs from "fs";
import path from "node:path";
const remove = async () => {
// Write your code here
const filePath = path.join(import.meta.dirname + "/files/fileToRemove.txt");

if (!fs.existsSync(filePath)) {
throw Error("FS operation failed");
}

fs.rmSync(filePath);
};

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 fs from "fs";
import path from "node:path";

const list = async () => {
// Write your code here
const folderPath = path.join(import.meta.dirname + "/files");
try {
fs.readdirSync(folderPath).forEach((file) => {
console.log(file);
});
} catch (err) {
throw Error("FS operation failed");
}
};

await list();
await list();
13 changes: 11 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from "fs";
import path from "node:path";

const read = async () => {
// Write your code here
const filePath = path.join(import.meta.dirname + "/files/fileToRead.txt");
try {
const content = fs.readFileSync(filePath).toString();
console.log(content);
} catch (err) {
throw Error("FS operation failed");
}
};

await read();
await read();
18 changes: 16 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from "fs";
import path from "node:path";

const rename = async () => {
// Write your code here
const oldFilePath = path.join(
import.meta.dirname + "/files/wrongFilename.txt",
);
const newFilePath = path.join(
import.meta.dirname + "/files/properFilename.md",
);

if (!fs.existsSync(oldFilePath) || fs.existsSync(newFilePath)) {
throw Error("FS operation failed");
}

fs.renameSync(oldFilePath, newFilePath);
};

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

const calculateHash = async () => {
// Write your code here
const hash = crypto.createHash("SHA256");
const filePath = path.join(
import.meta.dirname + "/files/fileToCalculateHashFor.txt",
);
const readStream = fs.createReadStream(filePath);
readStream.on("data", (chunk) => {
hash.update(chunk);
});

readStream.on("end", () => {
const fileHash = hash.digest("hex");
console.log("\nHash is:", fileHash);
});
};

await calculateHash();
await calculateHash();
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 { release, version } from "os";
import { createServer as createServerHttp } from "http";
import { fileURLToPath } from "url";
import jsonA from "./files/a.json" with { type: "json" };
import jsonB from "./files/b.json" with { type: "json" };
import "./files/c.js";

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

let unknownObject;

if (random > 0.5) {
unknownObject = jsonA;
} else {
unknownObject = jsonB;
}

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");
});

export { unknownObject, myServer };
11 changes: 9 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from "node:fs";
import path from "node:path";

const read = async () => {
// Write your code here
const filePath = path.join(import.meta.dirname + "/files/fileToRead.txt");
const readStream = fs.createReadStream(filePath);
readStream.on("data", (chunk) => {
process.stdout.write(chunk);
});
};

await read();
await read();
11 changes: 9 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Transform } from "node:stream";
const transform = async () => {
// Write your code here
const reverseTransform = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().split("").reverse().join("") + "\n");
callback();
},
});
process.stdin.pipe(reverseTransform).pipe(process.stdout);
};

await transform();
await transform();
11 changes: 9 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from "fs";
import path from "node:path";
const write = async () => {
// Write your code here
const filePath = path.join(import.meta.dirname + "/files/fileToWrite.txt");
const writableStream = fs.createWriteStream(filePath);

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

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

const performCalculations = async () => {
// Write your code here
const numOfCores = os.cpus().length;
let numberOfDataReceived = 0;
const results = new Array(numOfCores).fill({ status: null, data: null });
const filePath = path.join(import.meta.dirname + "/worker.js");

for (let i = 0; i < numOfCores; i++) {
const worker = new Worker(filePath);
const numberToSend = 10 + i;
worker.postMessage(numberToSend);

worker.on("message", (message) => {
results[i] = message;
if (++numberOfDataReceived === numOfCores) {
console.log(results);
}
});

worker.on("error", (message) => {
results[i].status = "error";
if (++numberOfDataReceived === numOfCores) {
console.log(results);
}
});
}
};

await performCalculations();
await performCalculations();
10 changes: 7 additions & 3 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
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
parentPort.on("message", (message) =>
parentPort.postMessage({ status: "resolved", data: nthFibonacci(message) }),
);
};

sendResult();
sendResult();
14 changes: 12 additions & 2 deletions src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import zlib from "node:zlib";
import path from "node:path";
import fs from "node:fs";

const compress = async () => {
// Write your code here
const filePath = path.join(import.meta.dirname + "/files/fileToCompress.txt");
const outputPath = path.join(import.meta.dirname + "/files/archive.gz");
const readStream = fs.createReadStream(filePath);
const writeStream = fs.createWriteStream(outputPath);
const gzip = zlib.createGzip();

readStream.pipe(gzip).pipe(writeStream);
};

await compress();
await compress();
16 changes: 14 additions & 2 deletions src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import zlib from "node:zlib";
import path from "node:path";
import fs from "node:fs";

const decompress = async () => {
// Write your code here
const filePath = path.join(import.meta.dirname + "/files/archive.gz");
const outputPath = path.join(
import.meta.dirname + "/files/fileToCompress.txt",
);
const readStream = fs.createReadStream(filePath);
const writeStream = fs.createWriteStream(outputPath);
const gunzip = zlib.createGunzip();

readStream.pipe(gunzip).pipe(writeStream);
};

await decompress();
await decompress();