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

for (let i = 0; i < args.length; i += 2) {
const prop = args[i].replace('--', '');
const value = args[i + 1];
result.push(`${prop} is ${value}`);
}

console.log(result.join(', '));
};

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

console.log(envVars);
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const child = spawn('node', [path.join(__dirname, 'files', 'script.js'), ...args]);

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['arg1', 'arg2', 'arg3']);
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 { cp } from 'fs/promises';
import { access } from 'fs/promises';

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

await copy();
8 changes: 7 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { writeFile } from 'fs/promises';

const create = async () => {
// Write your code here
try {
await writeFile('./src/fs/files/fresh.txt', 'I am fresh and young', { flag: 'wx' });
} catch (err) {
throw new Error('FS operation failed');
}
};

await create();
10 changes: 9 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { unlink } from 'fs/promises';
import { access } from 'fs/promises';

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

await remove();
1 change: 1 addition & 0 deletions src/fs/files/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions src/fs/files_copy/dontLookAtMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
What are you looking at?!
7 changes: 7 additions & 0 deletions src/fs/files_copy/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
My content
should
be
printed
into
console
!
1 change: 1 addition & 0 deletions src/fs/files_copy/fileToRemove.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
How dare you!
1 change: 1 addition & 0 deletions src/fs/files_copy/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
1 change: 1 addition & 0 deletions src/fs/files_copy/hello.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Node.js
3 changes: 3 additions & 0 deletions src/fs/files_copy/wrongFilename.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This is a file with a wrong filename

Hello from **markdown**!
11 changes: 10 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { readdir } from 'fs/promises';
import { access } from 'fs/promises';

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

await list();
11 changes: 10 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { readFile } from 'fs/promises';
import { access } from 'fs/promises';

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

await read();
18 changes: 17 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { rename as renameFile, access } from 'fs/promises';

const rename = async () => {
// Write your code here
try {
await access('./src/fs/files/wrongFilename.txt');
try {
await access('./src/fs/files/properFilename.md');
throw new Error('FS operation failed');
} catch (err) {
if (err.code === 'ENOENT') {
await renameFile('./src/fs/files/wrongFilename.txt', './src/fs/files/properFilename.md');
} else {
throw new Error('FS operation failed');
}
}
} catch (err) {
throw new Error('FS operation failed');
}
};

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

const calculateHash = async () => {
// Write your code here
const hash = createHash('sha256');
const stream = createReadStream('./src/hash/files/fileToCalculateHashFor.txt');

stream.on('data', (data) => {
hash.update(data);
});

stream.on('end', () => {
console.log(hash.digest('hex'));
});
};

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

This file was deleted.

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

import './files/c.cjs';

const random = Math.random();

const aJson = JSON.parse(await readFile('./src/modules/files/a.json', 'utf8'));
const bJson = JSON.parse(await readFile('./src/modules/files/b.json', 'utf8'));

const unknownObject = random > 0.5 ? aJson : bJson;

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

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

console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

const myServer = createServer((_, 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');
});

export {
unknownObject,
myServer,
};

5 changes: 4 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createReadStream } from 'fs';

const read = async () => {
// Write your code here
const stream = createReadStream('./src/streams/files/fileToRead.txt');
stream.pipe(process.stdout);
};

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 'stream';

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

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

await transform();
5 changes: 4 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createWriteStream } from 'fs';

const write = async () => {
// Write your code here
const stream = createWriteStream('./src/streams/files/fileToWrite.txt');
process.stdin.pipe(stream);
};

await write();
39 changes: 38 additions & 1 deletion src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
import { Worker } from 'worker_threads';
import { cpus } from 'os';
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 cpuCount = cpus().length;
console.log(`Number of CPU cores: ${cpuCount}`);

const workers = [];

for (let i = 0; i < cpuCount; i++) {
const worker = new Worker(path.join(__dirname, 'worker.js'));
workers.push(worker);
}

const promises = workers.map((worker, i) => {
return new Promise((resolve) => {
const data = 10 + i;
worker.postMessage(data);

worker.on('message', (message) => {
resolve({ status: 'resolved', data: message });
});

worker.on('error', () => {
resolve({ status: 'error', data: null });
});
});
});

const results = await Promise.all(promises);

workers.forEach(worker => worker.terminate());

console.log(results);
};

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

sendResult();
10 changes: 9 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { createReadStream, createWriteStream } from 'fs';
import { createGzip } from 'zlib';
import { pipeline } from 'stream/promises';

const compress = async () => {
// Write your code here
const source = createReadStream('./src/zip/files/fileToCompress.txt');
const destination = createWriteStream('./src/zip/files/archive.gz');
const gzip = createGzip();

await pipeline(source, gzip, destination);
};

await compress();
Loading