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
9 changes: 7 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2)
args.forEach((elem, index) => {
if(elem.includes('--')){
console.log(`${elem.slice(2)} is ${args[index + 1]}`)
}
});
};

parseArgs();
parseArgs();
5 changes: 3 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const parseEnv = () => {
// Write your code here
const keys = Object.keys(process.env).filter((el) => el.includes('RSS_'))
keys.forEach((el) => console.log(`${el}=${process.env[el]}`))
};

parseEnv();
parseEnv();
26 changes: 24 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import { spawn } from 'child_process';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const spawnChildProcess = async (args) => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const scriptFile = __dirname + '/files/script.js';
const childProcess = spawn('node', [scriptFile, args], { stdio: 'pipe' });

childProcess.stdout.on('data', (data) => {
console.log(`Received from child process: ${data.toString()}`);
});

childProcess.stderr.on('data', (data) => {
console.error(`Error from child process: ${data.toString()}`);
});

process.stdin.pipe(childProcess.stdin);

process.stdin.on('data', (data) => {
if (data.toString().includes('CLOSE')) {
childProcess.kill();
process.exit(0);
}
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['1', '2']);
25 changes: 24 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import * as fs from 'fs'
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const copy = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const folderFiles = __dirname + '/files';
const folderFilesCopy = __dirname + '/files_copy';
if (!fs.existsSync(folderFiles)) {
throw new Error('FS operation failed');
}

if (fs.existsSync(folderFilesCopy)) {
throw new Error('FS operation failed');
}

fs.mkdirSync(folderFilesCopy);
const files = fs.readdirSync(folderFiles);

for(const file of files){
const sourceFile = `${folderFiles}/${file}`
const copyFile = `${folderFilesCopy}/${file}`

fs.copyFileSync(sourceFile, copyFile);
}
};

await copy();
20 changes: 18 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import fs from 'fs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';

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

fs.access(path, fs.constants.F_OK, (err) => {
if (!err) {
throw new Error('FS operation failed');
} else {
fs.writeFile(path, content, (err) => {
if (err) throw err;
});
}
});
};

await create();
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 * as fs from 'fs'
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const remove = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const fileToRemove = __dirname + '/files/fileToRemove.txt';
if (!fs.existsSync(fileToRemove)) {
throw new Error('FS operation failed');
}
fs.unlinkSync(fileToRemove)
};

await remove();
await remove();
File renamed without changes.
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 * as fs from 'fs'
import { dirname } from 'path';
import { fileURLToPath } from 'url';

const list = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const dirFiles = __dirname + '/files';
if (!fs.existsSync(dirFiles)) {
throw new Error('FS operation failed');
}
fs.readdir(dirFiles, (err, files) => {
if (err) {
throw new Error('FS operation failed');
} else {
console.log(files);
}
});
};

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

const read = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const fileToRead = __dirname + '/files/fileToRead.txt';
if (!fs.existsSync(fileToRead)) {
throw new Error('FS operation failed');
}
fs.readFile(fileToRead, function(error,data){
if(error) {
return console.log(error);
}
console.log(data.toString());
});
};

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

const rename = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const wrongFilename = __dirname + '/files/wrongFilename.txt';
const properFilename = __dirname + '/files/properFilename.md';

if (!fs.existsSync(wrongFilename)) {
throw new Error('FS operation failed');
}
if (fs.existsSync(properFilename)) {
throw new Error('FS operation failed');
}
fs.rename(wrongFilename, properFilename, (err) => {
if (err) {
throw new Error('FS operation failed');
}
});
};

await rename();
15 changes: 13 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import * as fs from 'fs'
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import crypto from 'crypto';
const calculateHash = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const fileToCalculateHashFor = __dirname + '/files/fileToCalculateHashFor.txt';
const readStream = fs.createReadStream(fileToCalculateHashFor);
const hash = crypto.createHash('sha256');

readStream.on('data', (chunk) => {
console.log(hash.update(chunk).digest('hex'));
});
};

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

This file was deleted.

45 changes: 45 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as fs from 'fs'
import { fileURLToPath } from 'url';
import path from 'path';
import { release, version } from 'os';
import http from 'http';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const __filename = new URL('', import.meta.url).pathname;
const fileC = __dirname + '/files/c.js';
const fileA = __dirname + '/files/a.json';
const fileB = __dirname + '/files/b.json';

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = fs.readFileSync(fileA).toString()
} else {
unknownObject = fs.readFileSync(fileB).toString()
}
console.log(fs.readFileSync(fileC).toString());
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 = http.createServer((__, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
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 };

8 changes: 7 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as fs from 'fs'
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const read = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const fileToCalculateHashFor = __dirname + '/files/fileToRead.txt';
const readStream = fs.createReadStream(fileToCalculateHashFor);
readStream.on('data', (chunk) => process.stdout.write(chunk));
};

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 'stream';
const transform = async () => {
// Write your code here
const transformStream = new Transform({
transform(chunk, _encoding, callback) {
const reversedChunk = chunk.toString().split('').reverse().join('');
callback(null, reversedChunk);
}
});
process.stdin.pipe(transformStream).pipe(process.stdout);
};

await transform();
await transform();
9 changes: 8 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import * as fs from 'fs'
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const write = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = __dirname + '/files/fileToWrite.txt';
const writeStream = fs.createWriteStream(file);
process.stdin.on('data', (chunk) => writeStream.write(chunk));

};

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

const performCalculations = async () => {
// Write your code here
const __dirname = dirname(fileURLToPath(import.meta.url));
const file = __dirname + '/worker.js';
const cpusLength = os.cpus().length;
const results = [];

for (let i = 0; i < cpusLength; i++) {
const workerResult = await new Promise((resolve) => {
const worker = new Worker(file, { workerData: 10 + i });
worker.on("message", (message) =>
resolve({ status: "resolved", data: message })
);
worker.on('error', () => {
resolve({ status: 'error', data: null });
});
})
results.push(workerResult);
}
console.log(results)
};

await performCalculations();
11 changes: 7 additions & 4 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// n should be received from main thread
import { parentPort, workerData } from 'worker_threads';
const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2);

const sendResult = () => {
// This function sends result of nthFibonacci computations to main thread
const sendResult = (number) => {
const result = nthFibonacci(number);
if (parentPort !== null) {
parentPort.postMessage(result);
}
};

sendResult();
sendResult(workerData);
Loading