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
19 changes: 17 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
const parseArgs = () => {
// Write your code here
let args = process.argv;
let argName = '';

if (args && args.length) {
for (const arg of args) {
if (arg.startsWith('--')) {
if (argName && argName !== '') {
console.log(`${argName.replace('--', '')} is`);
}
argName = arg;
} else if (argName && argName !== '') {
console.log(`${argName.replace('--', '')} is ${arg}`);
argName = '';
}
}
}
};

parseArgs();
parseArgs();
9 changes: 8 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// RSS_name1=value1 RSS_name2=value2 node env.js

const parseEnv = () => {
// Write your code here
let env = process.env;
if (env) {
for (const key in env) {
if (key.startsWith('RSS_')) console.log(`${key}=${env[key]}`);
}
}
};

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

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

const spawnChildProcess = async (args) => {
// Write your code here
const runChild = spawn('node', [path.join(__dirname, 'files', 'script.js'), ...args], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'], // Establish IPC channel
});

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

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

const copy = async () => {
// Write your code here
let sourcePath = path.resolve(__dirname, 'files');
let distPath = path.resolve(__dirname, 'files_copy');
let sourcePathStat;

try {
await fs.promises.readdir(sourcePath);
sourcePathStat = await fs.promises.mkdir(distPath);
} catch (err) {
throw new Error('FS operation failed');
}

if (sourcePathStat && sourcePathStat.length) {
await Promise.all(
sourcePathStat.map(file =>
fs.promises.copyFile(path.resolve(sourcePath, file), path.resolve(distPath, file))
)
);
}
};

await copy();
12 changes: 11 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fs from 'fs';
import path from 'path';
import { __filename, __dirname } from './helpers.js';

const create = async () => {
// Write your code here
const filePath = path.resolve(__dirname, 'files', 'fresh.txt');

try {
await fs.promises.writeFile(filePath, 'I am fresh and young', { flag: 'ax+' });
} catch (err) {
throw new Error('FS operation failed');
}
};

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

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

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

export const __filename = fileURLToPath(import.meta.url);
export const __dirname = path.dirname(__filename);
16 changes: 14 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fs from 'fs/promises';
import path from 'path';
import { __filename, __dirname } from './helpers.js';

const list = async () => {
// Write your code here
try {
const fileList = await fs.readdir(path.resolve(__dirname, 'files'));

if (fileList && fileList.length) {
for (let fileName of fileList) console.log(fileName);
}
} catch (error) {
throw new Error('FS operation failed');
}
};

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

const read = async () => {
// Write your code here
try {
let fileContent = (await fs.readFile(path.resolve(__dirname, 'files', 'fileToRead.txt'))).toString('utf-8');

console.log(fileContent);
} catch (err) {
throw new Error('FS operation failed');
}
};

await read();
await read();
15 changes: 13 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'fs';
import path from 'path';
import { __filename, __dirname } from './helpers.js';

const rename = async () => {
// Write your code here
try {
await fs.promises.rename(
path.resolve(__dirname, 'files', 'wrongFilename.txt'),
path.resolve(__dirname, 'files', 'properFilename.md')
);
} catch (err) {
throw new Error('FS operation failed');
}
};

await rename();
await rename();
14 changes: 12 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import crypto from 'crypto';
import fs from 'fs/promises';
import { fileURLToPath } from 'url';
import path from 'path';

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

const calculateHash = async () => {
// Write your code here
const fileBuffer = await fs.readFile(path.resolve(__dirname, 'files', 'fileToCalculateHashFor.txt'));
const hash = crypto.createHash('sha256').update(fileBuffer);
console.log(hash.digest('hex'));
};

await calculateHash();
await calculateHash();
17 changes: 11 additions & 6 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
import path from 'path';
import { fileURLToPath } from 'url';
import {release, version} from 'os';
import { createRequire } from 'node:module';
import { createServer as createServerHttp } from 'http';

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

import './files/c.js';
const random = Math.random();

let unknownObject;
Expand Down Expand Up @@ -33,8 +39,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

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

15 changes: 13 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { createReadStream } from 'fs';
import { fileURLToPath } from 'url';
import path from 'path';

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

const read = async () => {
// Write your code here
const readStream = createReadStream(path.join(__dirname, 'files', 'fileToRead.txt'));

readStream.on('data', chunk => {
process.stdout.write(chunk.toString());
});
};

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

const transformInput = new Transform({
transform(chunk, encoding, callback){
const reversed = chunk.toString().trim().split('').reverse().join('');
this.push(chunk.toString().trim().split('').reverse().join('') + '\n');
callback();
}
});

const transform = async () => {
// Write your code here
process.stdin.pipe(transformInput).pipe(process.stdout);
};

await transform();
await transform();
15 changes: 13 additions & 2 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { createWriteStream } from 'fs';
import { fileURLToPath } from 'url';
import path from 'path';

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

const write = async () => {
// Write your code here
const writeStream = createWriteStream(path.join(__dirname, 'files', 'fileToWrite.txt'));

process.stdin.on('data', data => {
writeStream.write(data.toString().trim() + '\n');
});
};

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

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

const performCalculations = async () => {
// Write your code here
if (isMainThread) {
let workersNum = os.cpus().length;
let dataArray = Array.from({ length: workersNum }, (_, i) => i + 10);
let resultArray = Array.from({ length: workersNum }, (_) => ({status:'pending', result: null}));

for (let i in dataArray) {
const worker = new Worker(__dirname + '/worker.js', { workerData: dataArray[i] });

worker.on('message', message => {
resultArray[i].status = 'resolved';
resultArray[i].result = message;
});

worker.on('error', () => {
resultArray[i].status = 'error';
});

worker.on('exit', () => {
workersNum--;

if (workersNum === 0) {
console.log(resultArray);
}
});
}
} else {

}
};

await performCalculations();
20 changes: 16 additions & 4 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);

// 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 sendResult = result => {
parentPort.postMessage(result);
};

sendResult();
if (isMainThread) {
const worker = new Worker(__filename, { workerData: 10 });
worker.on('message', (message) => {
console.log('nthFibonacci result:', message);
})
} else {
sendResult(nthFibonacci(workerData));
}
17 changes: 15 additions & 2 deletions src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { createGzip } from 'zlib';
import { pipeline } from 'stream/promises';
import { createReadStream, createWriteStream } from 'fs';
import { fileURLToPath } from 'url';
import path from 'path';

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

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

await compress();
await compress();
Loading