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
22 changes: 20 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { parseArgs as nodeParseArgs } from "node:util";

const parseArgs = () => {
// Write your code here
const options = {
"some-arg": {
type: "string",
},
other: {
type: "string",
},
arg2: {
type: "string",
},
};
const { tokens } = nodeParseArgs({ options, tokens: true });
const formattedOptions = tokens
.filter((token) => token.kind === "option")
.map((token) => `${token.name} is ${token.value}`)
.join(", ");
console.log(formattedOptions);
};

parseArgs();
parseArgs();
2 changes: 1 addition & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const parseEnv = () => {
// Write your code here
console.log(Object.entries(process.env).filter(prop => prop[0].startsWith('RSS')).map(([key, value]) => `${key}=${value}`).join(', '));
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = spawn("node", ["src/cp/files/script.js", ...args]);

childProcess.stdout.on("data", (data) => {
console.log(data.toString());
});

childProcess.stdin.write("main-message-stdio");
setTimeout(() => {
childProcess.stdin.write("CLOSE");
}, 100)

childProcess.on("exit", (code) => {
console.log(`Child exited with code ${code}`);
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(["cos", "jeszcze", "innego"]);
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 srcPath = "./src/fs/files";
const destPath = "./src/fs/files_copy";
const errorMessage = "FS operation failed";

const copy = async () => {
// Write your code here
try {
await cp(srcPath, destPath, {force: false, errorOnExist: true, recursive: true});
} catch {
throw new Error(errorMessage);
}
};

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

//TODO: check how to do it with relative file path
const path = "./src/fs/files";
const fileName = "message.txt";
const errorMessage = "FS operation failed";

const create = async () => {
// Write your code here
try {
await stat(`${path}/${fileName}`);
throw new Error(errorMessage);
} catch (err) {
if (err.code === "ENOENT") {
try {
await writeFile(`${path}/${fileName}`, "I am fresh and young", "utf8");
} catch (err) {
throw new Error(errorMessage);
}
} else {
throw new Error(errorMessage);
}
}
};

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 { unlink } from "node:fs/promises";

const path = "./src/fs/files/fileToRemove.txt";
const errorMessage = "FS operation failed";

const remove = async () => {
// Write your code here
try {
await unlink(path);
} catch {
throw new Error(errorMessage);
}
};

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 folderPath = "./src/fs/files";
const errorMessage = "FS operation failed";

const list = async () => {
// Write your code here
try {
const files = await readdir(folderPath);
console.log(files);
} catch {
throw new Error(errorMessage);
}
};

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

const path = "./src/fs/files/fileToRead.txt";
const errorMessage = "FS operation failed";

const read = async () => {
// Write your code here
try {
const fileContent = await readFile(path, { encoding: 'utf8' });
console.log(fileContent);
} catch {
throw new Error(errorMessage);
}
};

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

const oldPath = "./src/fs/files/wrongFilename.txt";
const newPath = "./src/fs/files/properFilename.md";
const errorMessage = "FS operation failed";

const rename = async () => {
// Write your code here
try {
await stat(newPath);
throw new Error(errorMessage);
} catch (err) {
if (err.code === "ENOENT") {
try {
await fsRename(oldPath, newPath);
} catch(err) {
throw new Error(errorMessage);
}
} else {
throw new Error(errorMessage);
}
}
};

await rename();
await rename();
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 { createReadStream } from "node:fs";
const { createHash } = await import("node:crypto");

const filename = "./src/hash/files/fileToCalculateHashFor.txt";

const calculateHash = async () => {
// Write your code here
const hash = createHash("sha256");
const input = createReadStream(filename);
input.on("readable", () => {
const data = input.read();
if (data) hash.update(data);
else {
console.log(`${hash.digest("hex")} ${filename}`);
}
});
};

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

const random = Math.random();

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 "${path.sep}"`);

console.log(`Path to current file is ${import.meta.filename}`);
console.log(`Path to current directory is ${import.meta.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 };
14 changes: 12 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { createReadStream } from "node:fs";
import { stdout } from 'node:process';

const filePath = "./src/streams/files/fileToRead.txt";

const read = async () => {
// Write your code here
try {
const stream = await createReadStream(filePath);
stream.pipe(stdout);
} catch(err) {
throw new Error(err);
}
};

await read();
await read();
10 changes: 9 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { compose, Transform } from 'node:stream';

const reversesText = new Transform({
transform(chunk, encoding, callback) {
callback(null, String(chunk).split("").reverse().join(""));
},
});

const transform = async () => {
// Write your code here
compose(process.stdin, reversesText).pipe(process.stdout)
};

await transform();
14 changes: 13 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import process from 'node:process';
import { createWriteStream } from "node:fs";

const filePath = "./src/streams/files/fileToWrite.txt";

const write = async () => {
// Write your code here
try {
const writable = createWriteStream(filePath);
process.stdin.on('data', data => {
writable.write(data);
})
} catch(err) {
throw new Error(err);
}
};

await write();
25 changes: 23 additions & 2 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import os from "os";
import { Worker } from "node:worker_threads";
import { ReadableStreamDefaultController } from "node:stream/web";

const cpus = os.cpus();
const filePath = "./src/wt/worker.js";

const performCalculations = async () => {
// Write your code here
const workersLength = cpus.length;
let workersCompletedLength = 0;
const resultArr = [];
cpus.forEach((cpu, i) => {
const worker = new Worker(filePath);
const data = i + 10;
worker.postMessage(data);
worker.on("message", (result) => {
resultArr[i] = result;
workersCompletedLength++;
if(workersLength === workersCompletedLength) {
console.log(resultArr);
}
});
});
};

await performCalculations();
await performCalculations();
11 changes: 11 additions & 0 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { parentPort } from "node:worker_threads";

// n should be received from main thread
const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2);

const sendResult = () => {
parentPort.on("message", (data) => {
try {
const result = nthFibonacci(data);
parentPort.postMessage({ status: 'resolved', data: result});
} catch {
parentPort.postMessage({ status: 'error', data: null});
}
parentPort.close();
})
// This function sends result of nthFibonacci computations to main thread
};

Expand Down
Loading