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
console.log(process.argv.reduce((acc, cur, index, arr) => {
if (cur.startsWith('--')) {
return [...acc, `${cur.slice(2)} is ${arr[index + 1]}`];
}
return acc;
}, []).join(', '));
};

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

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 cp from 'child_process';
import path from 'path';
import getDirname from '../getDirname.js';

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

const script = path.join(__dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
const child = cp.spawn('node', [script, ...args]);

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['someArgument1', 'someArgument2']);
29 changes: 28 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import path from 'path';
import fs from 'fs/promises';
import getDirname from '../getDirname.js';

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

const startFolder = path.join(__dirname, 'files');
const endFolder = path.join(__dirname, 'copy-files');

const copy = async () => {
// Write your code here
try {
await fs.access(startFolder);
try {
await fs.access(endFolder);
throw new Error('FS operation failed');
} catch (err) {
if (err.code === 'ENOENT') {
fs.cp(startFolder, endFolder, { recursive: true });
} else {
throw err;
}
}
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');
} else {
throw err;
}
}
};

await copy();
19 changes: 18 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import path from 'path';
import fs from 'fs/promises';
import getDirname from '../getDirname.js';

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

const file = path.join(__dirname, 'files', 'fresh.txt');

const create = async () => {
// Write your code here
try {
await fs.access(file);
throw new Error('FS operation failed');
} catch (err) {
if (err.code === 'ENOENT') {
await fs.writeFile(file, 'I am fresh and young');
} else {
throw err;
}
}
};

await create();
17 changes: 16 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import path from 'path';
import fs from 'fs/promises';
import getDirname from '../getDirname.js';

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

const file = path.join(__dirname, 'files', 'fileToRemove.txt');

const remove = async () => {
// Write your code here
try {
await fs.access(file);
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');
}
}
await fs.unlink(file);
};

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 'path';
import fs from 'fs/promises';
import getDirname from '../getDirname.js';

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

const folder = path.join(__dirname, 'files');

const list = async () => {
// Write your code here
try {
const files = await fs.readdir(folder);
for (const file of files) {
console.log(file);
}
} catch (err) {
throw new Error('FS operation failed');
}
};

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

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

const file = path.join(__dirname, 'files', 'fileToRead.txt');

const read = async () => {
// Write your code here
try {
const text = await fs.readFile(file, { encoding: 'utf8' });
console.log(text);
} catch (err) {
throw new Error('FS operation failed');
}
};

await read();
29 changes: 28 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
import path from 'path';
import fs from 'fs/promises';
import getDirname from '../getDirname.js';

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

const startFile = path.join(__dirname, 'files', 'wrongFilename.txt');
const endFile = path.join(__dirname, 'files', 'properFilename.md');

const rename = async () => {
// Write your code here
try {
await fs.access(startFile);
try {
await fs.access(endFile);
throw new Error('FS operation failed');
} catch (err) {
if (err.code === 'ENOENT') {
fs.rename(startFile, endFile);
} else {
throw err;
}
}
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');
} else {
throw err;
}
}
};

await rename();
8 changes: 8 additions & 0 deletions src/getDirname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import path from 'path';
import { fileURLToPath } from 'url';

const getDirname = (url) => {
return path.dirname(fileURLToPath(url));
}

export default getDirname;
12 changes: 11 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import path from 'path';
import crypto from 'crypto';
import getDirname from '../getDirname.js';

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

const file = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');

const calculateHash = async () => {
// Write your code here
const hash = crypto.createHash('sha256');
hash.update(file);
console.log(hash.digest('hex'));
};

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

import {fileURLToPath} from 'url';

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

const random = Math.random();

let unknownObject;

const importJSON = async (fileName) => {
const file = path.join(__dirname, 'files', fileName);
return JSON.parse(await fs.readFile(file, {encoding: 'utf-8'}));
}

if (random > 0.5) {
unknownObject = require('./files/a.json');
unknownObject = await importJSON('a.json');
} else {
unknownObject = require('./files/b.json');
unknownObject = await importJSON('b.json');
}

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

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

13 changes: 12 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'fs'
import path from 'path';
import getDirname from '../getDirname.js';

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

const file = path.join(__dirname, 'files', 'fileToRead.txt');

const read = async () => {
// Write your code here
const readableStream = fs.createReadStream(file, 'utf-8');
readableStream.on('data', (chunk) => {
process.stdout.write(chunk);
});
};

await read();
9 changes: 8 additions & 1 deletion 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 reverse = new Transform({
transform(chunk, encoding, callback) {
callback(null, chunk.toString().split('').reverse().join(''));
},
})
process.stdin.pipe(reverse).pipe(process.stdout);
};

await transform();
11 changes: 10 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from 'fs';
import path from 'path';
import getDirname from '../getDirname.js';

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

const file = path.join(__dirname, 'files', 'fileToWrite.txt');

const write = async () => {
// Write your code here
const writableStream = fs.createWriteStream(file);
process.stdin.pipe(writableStream);
};

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

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

const workerFile = path.join(__dirname, 'worker.js');

const performCalculations = async () => {
// Write your code here
const cores = os.cpus().length;
const results = [];

for (let i = 0; i < cores; i++) {
const worker = new Worker(workerFile, { workerData: i + 10 });

const output = () => {
if (results.length === cores && !results.includes()) {
console.log(results);
}
}

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

worker.on('error', (error) => {
results[i] = { status: 'error', data: null };
output();
});
}
};

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

sendResult();
Loading