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 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const argsLength = args.length;
const arrArgs = [];

for (let i = 0; i < argsLength; i += 2) arrArgs.push(`${args[i].slice(2)} is ${args[i + 1]}`);

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

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 rssEnvs = Object.entries(process.env).filter(([key]) => key.startsWith('RSS_'));
console.log(rssEnvs.map(([key, value]) => `${key}=${value}`).join('; '));
};

parseEnv();
parseEnv();
11 changes: 9 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { fork } from 'child_process';
import { pipeline } from 'stream/promises';

const spawnChildProcess = async (args) => {
// Write your code here
const cp = fork('./src/cp/files/script.js', [...args], { stdio: ['pipe', 'pipe', 'inherit', 'ipc'] });

try {
await Promise.all([pipeline(process.stdin, cp.stdin), pipeline(cp.stdout, process.stdout)]);
} catch {}
};

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

const copy = async () => {
// Write your code here
const files = './src/fs/files';
const filesCopy = './src/fs/files_copy';
const error = new Error('FS operation failed');

try {
await access(files);
} catch {
throw error;
}

try {
await access(filesCopy);

throw error;
} catch (err) {
if (err.code === 'ENOENT') {
await cp(files, filesCopy, { recursive: true });
} else throw error;
}
};

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

const create = async () => {
// Write your code here
const file = './src/fs/files/fresh.txt';
const content = 'I am fresh and young';
const error = new Error('FS operation failed');

try {
await access(file);

throw error;
} catch (err) {
if (err.code === 'ENOENT') {
await writeFile(file, content);
} else throw error;
}
};

await create();
await create();
10 changes: 8 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { rm } from 'fs/promises';

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

await remove();
await remove();
12 changes: 10 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { readdir } from 'fs/promises';
import { parse } from 'path';

const list = async () => {
// Write your code here
try {
const files = await readdir('./src/fs/files', { withFileTypes: true });
console.log(files.map(({ name }) => parse(name).name));
} catch {
throw new Error('FS operation failed');
}
};

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

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

await read();
await read();
14 changes: 12 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { access, constants, copyFile } from 'fs/promises';

const rename = async () => {
// Write your code here
const path = './src/fs/files';
const wrongFilename = `${path}/wrongFilename.txt`;

try {
await access(wrongFilename);
await copyFile(wrongFilename, `${path}/properFilename.md`, constants.COPYFILE_EXCL);
} catch {
throw new Error('FS operation failed');
}
};

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

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

for await (const slice of stream) hash.update(slice);

console.log(hash.digest('hex'));
};

await calculateHash();
await calculateHash();
36 changes: 36 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createServer as createServerHttp } from 'http';
import { release, version } from 'os';
import path from 'path';
import './files/c.cjs';

const random = Math.random();

let unknownObject;

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

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

console.log(`Path to current file is ${import.meta.filename}`);
console.log(`Path to current directory is ${import.meta.dirname}`);

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');
});

export { myServer, unknownObject };
11 changes: 9 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { createReadStream } from 'fs';
import { EOL } from 'os';

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

for await (const slice of stream) process.stdout.write(slice);

process.stdout.write(EOL);
};

await read();
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';
import { pipeline } from 'stream/promises';

const transform = async () => {
// Write your code here
const transformStream = new Transform({
transform: (chunk, _, cb) => cb(null, chunk.toString().split('').reverse().join('')),
});

pipeline(process.stdin, transformStream, process.stdout);
};

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

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

await write();
await write();
25 changes: 23 additions & 2 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { cpus } from 'os';
import { Worker } from 'worker_threads';

const performCalculations = async () => {
// Write your code here
const workers = Array.from(
{ length: cpus().length },
(_, i) =>
new Promise((resolve) => {
const worker = new Worker('./src/wt/worker.js', { workerData: 10 + i });

worker.on('message', (msg) => resolve({ status: 'resolved', data: msg }));
worker.on('error', () => resolve({ status: 'error', data: null }));
worker.on('exit', (code) => {
if (code !== 0) {
return resolve({ status: 'error', data: null });
}
});
})
);

const result = await Promise.all(workers);

console.log(result);
};

await performCalculations();
await performCalculations();
10 changes: 7 additions & 3 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { parentPort, workerData } from 'worker_threads';

// n should be received from main thread
const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2);
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 n = workerData;
const result = nthFibonacci(n);
parentPort.postMessage(result);
};

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

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

await compress();
await compress();
12 changes: 10 additions & 2 deletions src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { createReadStream, createWriteStream } from 'fs';
import { pipeline } from 'stream/promises';
import { createUnzip } from 'zlib';

const decompress = async () => {
// Write your code here
pipeline(
createReadStream('./src/zip/files/archive.gz'),
createUnzip(),
createWriteStream('./src/zip/files/fileToCompress.txt')
);
};

await decompress();
await decompress();