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 @@
import { argv } from 'node:process';

const parseArgs = () => {
// Write your code here
const msg = argv
.reduce((acc, item, i, arr) => item.startsWith('--') ? acc += `${item.slice(2)} is ${arr[i + 1]}, ` : acc, '')
.slice(0, -2);

console.log(msg);
};

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

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

console.log(msg);
};

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

import { getDirname } from '../utils.js';


const spawnChildProcess = async (args) => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/', 'script.js');

fork(myPath, args); // if this method little confuse you, please check screenshot on PR
};

spawnChildProcess();
spawnChildProcess(['this', 'arguments', 'for', 'test']);
19 changes: 17 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fsPromises from 'node:fs/promises';
import path from 'path';

import { getDirname, exists } from '../utils.js';

const copy = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/');
const myPathCopy = path.join(__dirname, '/files_copy/');
const isExistFiles = await exists(myPath);
const isExistFilesCopy = await exists(myPathCopy);

if (!isExistFiles || isExistFilesCopy) {
throw new Error('FS operation failed: files dir not exists or copy dir already exists');
}

await fsPromises.cp(myPath, myPathCopy, { recursive: true }); // if this method little confuse you, please check screenshot on PR
};

copy();
copy();
17 changes: 15 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fsPromises from 'node:fs/promises';
import path from 'path';

import { getDirname, exists } from '../utils.js';

const create = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/', 'fresh.txt');
const isExist = await exists(myPath);

if (isExist) {
throw new Error('FS operation failed: file already exists');
}

await fsPromises.writeFile(myPath, 'I am fresh and young');
};

await create();
await create();
17 changes: 15 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fsPromises from 'node:fs/promises';
import path from 'path';

import { getDirname, exists } from '../utils.js';

const remove = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/', 'fileToRemove.txt');
const isExist = await exists(myPath);

if (!isExist) {
throw new Error('FS operation failed: file not exists');
}

await fsPromises.rm(myPath);
};

await remove();
await remove();
17 changes: 15 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fsPromises from 'node:fs/promises';
import path from 'path';

import { getDirname, exists } from '../utils.js';

const list = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/');
const isExist = await exists(myPath);

if (!isExist) {
throw new Error('FS operation failed: dir not exists');
}

console.log(await fsPromises.readdir(myPath));
};

await list();
await list();
17 changes: 15 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fsPromises from 'node:fs/promises';
import path from 'path';

import { getDirname, exists } from '../utils.js';

const read = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/', 'fileToRead.txt');
const isExist = await exists(myPath);

if (!isExist) {
throw new Error('FS operation failed: file not exists');
}

console.log(await fsPromises.readFile(myPath, { encoding: 'utf-8'}));
};

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

import { getDirname, exists } from '../utils.js';

const rename = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/');
const pathWrongFile = path.join(myPath, 'wrongFilename.txt');
const pathProperFile = path.join(myPath, 'properFilename.md');

const isExistWrongFile = await exists(pathWrongFile);
const isExistProperFile = await exists(pathProperFile);

if (!isExistWrongFile || isExistProperFile) {
throw new Error('FS operation failed: wrong file not exists or proper file already exists');
}

await fsPromises.rename(pathWrongFile, pathProperFile);
};

await rename();
await rename();
16 changes: 14 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { createHash } from 'crypto'
import fsPromises from 'node:fs/promises';
import path from 'path';

import { getDirname } from '../utils.js';


const calculateHash = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/', 'fileToCalculateHashFor.txt');
const text = await fsPromises.readFile(myPath, { encoding: 'utf8' });
const hash = createHash('sha256').update(text).digest('hex');

console.log(hash);
};

await calculateHash();
await calculateHash();
23 changes: 15 additions & 8 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
import path from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';

import { getDirname } from '../utils.js';

import('./files/c.js');

const __filename = import.meta.url;
const __dirname = getDirname(__filename);

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = require('./files/a.json');
const obj = await import('./files/a.json', { assert: { type: 'json'}});
unknownObject = obj.default;
} else {
unknownObject = require('./files/b.json');
const obj = await import('./files/b.json', { assert: { type: 'json'}});
unknownObject = obj.default;
}

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

module.exports = {
export {
unknownObject,
myServer,
};

2 changes: 2 additions & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
regergergerger
regergergerger
20 changes: 18 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { stdout } from 'node:process';
import fs from 'node:fs';
import path from 'path';

import { getDirname } from '../utils.js';

const read = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/', 'fileToRead.txt');
const readableStream = fs.createReadStream(myPath, 'utf8');

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

readableStream.on('end', () => {
console.log('\nend');
});
};

await read();
await read();
12 changes: 10 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { stdin, stdout } from "process";
import { Transform } from "stream";

const transform = async () => {
// Write your code here
const reverse = new Transform({
transform(chunk, _, callback) {
callback(null, [...chunk.toString()].reverse().join(""));
},
});
stdin.pipe(reverse).pipe(stdout);
};

await transform();
await transform();
14 changes: 12 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { stdin } from 'node:process';
import fs from 'node:fs';
import path from 'path';

import { getDirname } from '../utils.js';

const write = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const myPath = path.join(__dirname, '/files/', 'fileToWrite.txt');
const writableStream = fs.createWriteStream(myPath);

stdin.pipe(writableStream);
};

await write();
await write();
14 changes: 14 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fsPromises from 'node:fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

export const getDirname = (url) => path.dirname(fileURLToPath(url));

export const exists = async (path) => {
try {
await fsPromises.access(path)
return true
} catch {
return false
}
}
38 changes: 36 additions & 2 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
import { Worker, isMainThread } from 'node:worker_threads';
import os from 'os';
import path from 'path';

import { getDirname } from '../utils.js';

const performCalculations = async () => {
// Write your code here
const __dirname = getDirname(import.meta.url);
const startNumberForFibonacci = 10;
const arrWithPromise = [];
const numOfCpus = os.cpus().length;

for (let i = 0; i < numOfCpus; i++) {
const worker = new Promise((resolve, _) => {
const worker = new Worker(path.join(__dirname, './worker.js'), {
workerData: startNumberForFibonacci + i
});
worker.on("message", result => {
resolve({
status: 'resolved',
data: result
});
});
worker.on('error', () => {
resolve({ //resolve is used rather than reject because we need a list of data with both successful workers and those that gave an error
status: 'error',
data: null
})
});
})
arrWithPromise.push(worker);
}

Promise.all(arrWithPromise).then(values => {
console.log(values);
});
};

await performCalculations();
await performCalculations();
8 changes: 7 additions & 1 deletion src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { workerData, 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
//if (workerData === 12) throw new Error('gergergerg'); // uncomment this for test if one of worker get error
const result = nthFibonacci(workerData);
parentPort.postMessage(result);
process.exit();
};

sendResult();
sendResult();
Loading