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
12 changes: 11 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);

const argsAsArray = [];

for (let i = 0; i < args.length; i += 2) {
argsAsArray.push([args[i].slice(2), args[i + 1]]);
}

const result = argsAsArray.map((ar => `${ar[0]} is ${ar[1]}`)).join(', ');

console.log(result);
};

parseArgs();
9 changes: 8 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseEnv = () => {
// Write your code here
const prefix = 'RSS_';

const result = Object.keys(process.env)
.filter(key => key.startsWith(prefix))
.map(key => `${key}=${process.env[key]}`)
.join('; ');

console.log(result)
};

parseEnv();
16 changes: 13 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { spawn } from 'node:child_process';
import path from 'path';
import { fileURLToPath } from 'url';

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

const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = spawn('node', [`${__dirname}/files/script.js`, ...args]);

process.stdin.pipe(childProcess.stdin);

childProcess.stdout.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['firstArg', 'secArg']);
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 { existsSync } from 'fs';
import { cp } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

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

const copy = async () => {
// Write your code here
const sourceFolder = `${__dirname}/files`;
const destinationFolder = `${__dirname}/files_copy`;

if (!existsSync(sourceFolder) || existsSync(destinationFolder))
throw new Error('FS operation failed');

await cp(sourceFolder, destinationFolder, { recursive: true });
};

await copy();
15 changes: 14 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { existsSync } from 'fs';
import { writeFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

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

const create = async () => {
// Write your code here
const filePath = `${__dirname}/files/fresh.txt`;

if (existsSync(filePath))
throw new Error('FS operation failed');

await writeFile(filePath, 'I am fresh and young');
};

await create();
15 changes: 14 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { unlink } from 'fs/promises';
import { existsSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const remove = async () => {
// Write your code here
const deleteFilePath = `${__dirname}/files/fileToRemove.txt`;

if (!existsSync(deleteFilePath))
throw new Error('FS operation failed');

await unlink(deleteFilePath);
};

await remove();
17 changes: 16 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { readdir } from 'fs/promises';
import { existsSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const list = async () => {
// Write your code here
const sourceFolder = `${__dirname}/files`;

if (!existsSync(sourceFolder))
throw new Error('FS operation failed');

const files = await readdir(sourceFolder);

console.log(files);
};

await list();
17 changes: 16 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { existsSync } from 'fs';
import { readFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

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

const read = async () => {
// Write your code here
const fileToRead = `${__dirname}/files/fileToRead.txt`;

if (!existsSync(fileToRead))
throw new Error('FS operation failed');

const data = await readFile(fileToRead, 'utf8');

console.log(data);
};

await read();
16 changes: 15 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'fs/promises';
import { existsSync } from 'fs';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';

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

const rename = async () => {
// Write your code here
const filePath = `${__dirname}/files/wrongFilename.txt`;
const targetFilePath = `${__dirname}/files/properFilename.md`;

if (!existsSync(filePath) || existsSync(targetFilePath))
throw new Error('FS operation failed');

await fs.rename(filePath, targetFilePath);
};

await rename();
18 changes: 17 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { createHash } from 'node:crypto'
import { readFile } from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

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

const calculateHash = async () => {
// Write your code here
const filePath = `${__dirname}/files/fileToCalculateHashFor.txt`;

const fileData = await readFile(filePath);

const hash = createHash('sha256')
.update(fileData)
.digest('hex');

console.log(hash);
};

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

This file was deleted.

40 changes: 40 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import path from "path";
import os from "os";
import { createServer } from "http";
import "./files/c.js";

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = import("./files/a.json", {
assert: { type: "json" }
});
} else {
unknownObject = import("./files/b.json", {
assert: { type: "json" }
});
}

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

console.log(`Path to current file is ${import.meta.url}`);
console.log(`Path to current directory is ${path.dirname(import.meta.url)}`);

const myServer = createServer((_, res) => {
res.end("Request accepted");
});

const PORT = 3000;

console.log((await unknownObject).default);

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log("To terminate it, use Ctrl+C combination");
});

export { unknownObject, myServer };
15 changes: 14 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from 'fs'
import path from 'path';
import { fileURLToPath } from 'url';

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

const read = async () => {
// Write your code here
const filePath = `${__dirname}/files/fileToRead.txt`;

const readableStream = fs.createReadStream(filePath);

readableStream.on('data', (chunk) => {
process.stdout.write(chunk);
});
};

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 { Transform } from 'stream';

const transform = async () => {
// Write your code here
const reverse = new Transform({
transform(chunk, encoding, callback) {
callback(null, chunk.toString().split('').reverse().join(''));
}
});

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

await transform();
15 changes: 14 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from 'fs'
import path from 'path';
import { fileURLToPath } from 'url';

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

const write = async () => {
// Write your code here
const filePath = `${__dirname}/files/fileToWrite.txt`;

const writableStream = fs.createWriteStream(filePath);

process.stdin.pipe(writableStream);

console.log('data to write:')
};

await write();
30 changes: 29 additions & 1 deletion src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import os from 'os';
import { Worker } from 'worker_threads';
import path from 'path';
import { fileURLToPath } from 'url';

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

const performCalculations = async () => {
// Write your code here
const workerPath = `${__dirname}/worker.js`;
const cpuCount= os.cpus().length

const processes = [];

const n = 10;

for (let i = n; i < n + cpuCount; i++) {
processes.push(
new Promise((resolve) => {
const worker = new Worker(workerPath);
worker.postMessage(i);
worker.on('message', (result) => resolve({ status: 'resolved', data: result }));
worker.on('error', (error) => resolve({ status: error, data: null }));
})
);
}

const result = await Promise.all(processes);

console.log(result);
};

await performCalculations();
7 changes: 6 additions & 1 deletion src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { parentPort } from '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();
Loading