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
Empty file added archive.gz
Empty file.
14 changes: 14 additions & 0 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { argv } from 'node:process';

const parseArgs = () => {
// Write your code here
const args = argv.slice(2);
const result = args.reduce((acc, curr, index) => {
if(index % 2 === 0) {
const key = curr.replace('--', '');
const value = args[index + 1];
acc.push(`${key} is ${value}`);
}
return acc;
}
, []);
console.log(result.join(', '));

};

parseArgs();
8 changes: 8 additions & 0 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { env } from 'node:process';

const parseEnv = () => {
// Write your code here
const result = Object.entries(env)
.filter(([key]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`)
.join('; ');

console.log(result);
};

parseEnv();
20 changes: 16 additions & 4 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
const spawnChildProcess = async (args) => {
// Write your code here
};
import { spawn } from "node:child_process";

const spawnChildProcess = async (args) => {

return new Promise((resolve) => {
const childProcess = spawn('node', ["./src/cp/files/script.js", ...args], {
stdio: ['inherit', 'inherit', 'inherit']
});

childProcess.on('close', (code) => {
console.log(`Child process exited with code ${code}`);
resolve();
});
});
};

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

const copy = async () => {
// Write your code here
const srcDirectory = "./src/fs/files";
const destDirectory = "./src/fs/files_copy";

try{
await cp(srcDirectory, destDirectory, {
errorOnExist: true,
recursive: true,
force: false,
});
} catch (error) {
throw new Error("FS operation failed");
}
};

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

const create = async () => {
// Write your code here
const path = "./src/fs/files/fresh.txt";
const content = "I am fresh and young";
const options = {
flag: "wx", // 'wx' flag to ensure the file is created only if it does not exist
}
try {
await writeFile(path, content, options);
} catch (error) {
throw new Error("FS operation failed");
}

};

await create();
7 changes: 7 additions & 0 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { unlink } from 'node:fs/promises';

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

await remove();
1 change: 1 addition & 0 deletions src/fs/files/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
3 changes: 3 additions & 0 deletions src/fs/files/properFilename.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is a file with a wrong filename

Hello from **markdown**!
1 change: 1 addition & 0 deletions src/fs/files_copy/dontLookAtMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
What are you looking at?!
7 changes: 7 additions & 0 deletions src/fs/files_copy/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
My content
should
be
printed
into
console
!
1 change: 1 addition & 0 deletions src/fs/files_copy/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
How dare you!
1 change: 1 addition & 0 deletions src/fs/files_copy/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions src/fs/files_copy/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Node.js
3 changes: 3 additions & 0 deletions src/fs/files_copy/wrongFilename.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is a file with a wrong filename

Hello from **markdown**!
8 changes: 8 additions & 0 deletions 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
try{
const files = await readdir("./src/fs/files");
console.log(files);
} catch(error) {
throw new Error("FS operation failed");
}
};

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

const read = async () => {
// Write your code here
try {

const data = await readFile("./src/fs/files/fileToRead.txt", { encoding: "utf8" });
console.log(data);
} catch (error) {
throw new Error("FS operation failed");
}
};

await read();
8 changes: 8 additions & 0 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { rename as renameFile } from "node:fs/promises";
const rename = async () => {
// Write your code here
const oldPath = "./src/fs/files/wrongFilename.txt";
const newPath = "./src/fs/files/properFilename.md";
try {
await renameFile(oldPath, newPath);
} catch (error) {
throw new Error("FS operation failed");
}
};

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

const calculateHash = async () => {
// Write your code here

return new Promise((resolve, reject) => {
const hash = createHash("sha256");
const inputFile = "./src/hash/files/fileToCalculateHashFor.txt";
const data = createReadStream(inputFile);

data.pipe(hash).setEncoding("hex").pipe(stdout);

data.on("end", () => {
stdout.write('\n');
resolve();
});
});

};

await calculateHash();
19 changes: 12 additions & 7 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c.cjs');
import path from "node:path";
import { release, version } from "node:os";
import { createServer as createServerHttp } from "node:http";
import './files/c.cjs';
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";

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

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = require('./files/a.json');
unknownObject = JSON.parse(readFileSync(new URL('./files/a.json', import.meta.url)));
} else {
unknownObject = require('./files/b.json');
unknownObject = JSON.parse(readFileSync(new URL('./files/b.json', import.meta.url)));
}

console.log(`Release ${release()}`);
Expand All @@ -33,7 +38,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export {
unknownObject,
myServer,
};
Expand Down
12 changes: 12 additions & 0 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { createReadStream } from 'node:fs';
import { stdout } from 'node:process';

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

stream.on('end', () => {
stdout.write('\n');
resolve();
});
});
};

await read();
18 changes: 18 additions & 0 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import { Transform } from "node:stream";
import { pipeline } from "node:stream/promises";
import { stdin, stdout } from "node:process";

const transform = async () => {
// Write your code here
const reverseStream = new Transform({
transform(chunk, encoding, cb) {
const inputStr = chunk.toString();
const reversedStr = [...inputStr].reverse().join("");;
cb(null, reversedStr + "\n".repeat(2));
},
});

try {
await pipeline(stdin, reverseStream, stdout);
}
catch (error) {
console.error("Pipeline failed.", error);
}
};

await transform();
10 changes: 10 additions & 0 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { createWriteStream } from "node:fs";
import { pipeline } from "node:stream/promises";
import { stdin } from "node:process";

const write = async () => {
// Write your code here
const writableStream = createWriteStream("./src/streams/files/fileToWrite.txt");
try {
await pipeline(stdin, writableStream);
} catch (error) {
console.error("Pipeline failed.");
}
};

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

const performCalculations = async () => {
// Write your code here
const numCPUCores = os.cpus().length;
const workers = [];
const result = [];
for(let i=0; i<numCPUCores; i++){
const worker = new Worker("./src/wt/worker.js");
workers.push(worker);

worker.postMessage(10 + i);

worker.on("message", (data) => {
result[i] = {
status: "resolved",
data
};

if(result.length === numCPUCores){
console.log(result);
}
});

worker.on("error", (error) => {
result[i] = {
status: "error",
data: null
};

if(result.length === numCPUCores){
console.log(result);
}
});
}
};

await performCalculations();
6 changes: 6 additions & 0 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
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 = () => {
// This function sends result of nthFibonacci computations to main thread
parentPort.on("message", (n) => {
const result = nthFibonacci(n);
parentPort.postMessage(result);
})
};

sendResult();
12 changes: 12 additions & 0 deletions src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import {
createReadStream,
createWriteStream,
} from "node:fs";
import { createGzip } from "node:zlib";
import { pipeline } from "node:stream/promises";

const compress = async () => {
// Write your code here
const gzip = createGzip();
const source = createReadStream('./src/zip/files/fileToCompress.txt');
const destination = createWriteStream('./src/zip/files/archive.gz');

await pipeline(source, gzip, destination);
};

await compress();
Loading