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
7 changes: 6 additions & 1 deletion 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.splice(2);
const result = [];
for (let i = 0; i < args.length; i += 2) {
result.push(`${args[i].replace('--', '')} is ${args[i + 1]}`);
}
console.log(result.join(', '));
};

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

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

const spawnChildProcess = async (args) => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const childPath = path.join(__dirname, 'files', 'script.js');
fork(childPath, args);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess( ['someArgument1', 'someArgument2']);
19 changes: 18 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { access, mkdir, cp } from 'node:fs/promises';

const copy = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const folderPath = path.join(__dirname, 'files');
const folderCopyPath = path.join(__dirname, 'files_copy');
try {
await access(folderPath);
await mkdir(folderCopyPath);
await cp(folderPath, folderCopyPath, { recursive: true });
}
catch (error) {
if (error.code === 'EEXIST' || error.code === 'ENOENT') {
throw new Error ('FS operation failed');
}
else { throw error };
}
};

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

const create = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const folderPath = path.join(__dirname, 'files');
const filePath = path.join(folderPath, 'fresh.txt');
const content = 'I am fresh and young';
try {
await access(filePath);
throw new Error ('FS operation failed');
}
catch (error) {
if (error.code === 'ENOENT') {
await writeFile(filePath, content);
}
else { throw error };
}
};

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

const remove = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToRemove.txt');
try {
await unlink(filePath);
}
catch (error) {
if (error.code === 'ENOENT') {
throw new Error ('FS operation failed');
}
else { throw error };
}
};

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 path from 'node:path';
import { fileURLToPath } from 'node:url';
import { readdir } from 'node:fs/promises';

const list = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const folderPath = path.join(__dirname, 'files');
try {
const folderContent = await readdir(folderPath);
console.log(folderContent);
}
catch (error) {
if (error.code === 'ENOENT') {
throw new Error ('FS operation failed');
}
else { throw error };
}
};

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 path from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFile } from 'node:fs/promises';

const read = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');
try {
const content = await readFile(filePath, { encoding: 'utf8' });
console.log(content);
}
catch (error) {
if (error.code === 'ENOENT') {
throw new Error ('FS operation failed');
}
else { throw error };
}
};

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 path from 'node:path';
import { fileURLToPath } from 'node:url';
import { rename as renameFile } from 'node:fs/promises';

const rename = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const folderPath = path.join(__dirname, 'files');
try {
await renameFile(path.join(folderPath, 'wrongFilename.txt'), path.join(folderPath, 'properFilename.md'));
}
catch (error) {
if (error.code === 'ENOENT') {
throw new Error ('FS operation failed');
}
else { throw error };
}
};

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

const calculateHash = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');
const readStream = createReadStream(filePath, { encoding: 'utf8' });
const hash = createHash('sha256');
readStream.pipe(hash).setEncoding('hex').pipe(process.stdout);
readStream.on('end', () => process.stdout.write(os.EOL));
};

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

This file was deleted.

52 changes: 52 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { release, version } from 'node:os';
import { createServer as createServerHttp } from 'node:http';
import { readFile } from 'node:fs/promises';
import './files/c.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const pathToFolderFiles = path.join(__dirname, 'files');

const readJson = async (file) => {
try {
const pathToFile = path.join(pathToFolderFiles, file);
return JSON.parse(await readFile(pathToFile, { encoding: 'utf8' }));
}
catch (err) {
throw err;
}
}

const aJson = await readJson('a.json');
const bJson = await readJson('b.json');

const random = Math.random();

export let unknownObject;

if (random > 0.5) {
unknownObject = aJson;
} else {
unknownObject = bJson;
}

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

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

export 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');
});
11 changes: 10 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createReadStream } from 'node:fs';
import os from 'node:os';

const read = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');
const readStream = createReadStream(filePath, { encoding: 'utf8' });
readStream.pipe(process.stdout);
readStream.on('end', () => process.stdout.write(os.EOL));
};

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

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

const reverseTransform = new Transform({
transform(chunk, encoding, callback) {
const reverseInput = chunk.toString().split('').reverse().join('');
callback(null, reverseInput);
},
});
process.stdin.pipe(reverseTransform).pipe(process.stdout);
};

await transform();
12 changes: 11 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { createWriteStream } from 'node:fs';
import { createInterface } from 'node:readline/promises';
import os from 'node:os';

const write = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToWrite.txt');
const writeStream = createWriteStream(filePath);
const rl = createInterface(process.stdin);
rl.on('line', (data) => writeStream.write(`${data}${os.EOL}`));
};

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

const performCalculations = async () => {
// Write your code here
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const numberOfHostMachine = os.cpus().length;
const filePath = path.join(__dirname, 'worker.js');
const workers = [];
for (let i = 0; i < numberOfHostMachine; i++) {
const worker = new Worker(filePath, {
workerData: i + 10
})

workers.push(new Promise((resolve, reject) => {
worker.on('message', (message) => resolve({status: 'resolved', data: message}));
worker.on('error', () => reject({status: 'error', data: null}));
}))
}
const result = await Promise.allSettled(workers);
console.log(result.map((el) => el.value || el.reason));
};

await performCalculations();
6 changes: 4 additions & 2 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// n should be received from main thread
import { parentPort, workerData } from 'node: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 result = nthFibonacci(workerData);
parentPort.postMessage(result);
};

sendResult();
Loading