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
13 changes: 11 additions & 2 deletions 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 array = process.argv.slice(2);

let value = array.reduce(function (accumulator, item, index, array) {
if (index % 2 === 0) {
accumulator.push(`${item.slice(2)} is ${array[index + 1]}`);
}
return accumulator;
}, []);
const joinedStr = value.join(", ");
console.log(joinedStr);
};

parseArgs();
parseArgs();
8 changes: 6 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const parseEnv = () => {
// Write your code here
const filteredObject = Object.entries(process.env).filter((el) =>
el[0].includes("RSS_")
);
const variables = filteredObject.map(el => el.join("=")).join('; ');
console.log(variables)
};

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

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

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

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

const __dirname = dirname(fileURLToPath(import.meta.url));
const filesFolder = path.join(__dirname, 'files');
const filesFolderCopy = path.join(__dirname, 'files-copy');

const copy = async () => {
// Write your code here
try {
await cp(filesFolder, filesFolderCopy, { recursive: true, force: false, errorOnExist: true });
} catch {
throw new Error('FS operation failed');
}
};

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

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

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

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

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

const remove = async () => {
// Write your code here
try {
await unlink(path.join(filesFolder, 'fileToRemove.txt'));
} catch {
throw new Error('FS operation failed');
}
};

await remove();
await remove();
17 changes: 15 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { access, constants, readdir, unlink } from 'node:fs/promises';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

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

await list();
await list();
18 changes: 17 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { readFile } from 'node:fs/promises';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

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

try {
const fileContent = await readFile(path.join(filesFolder, 'fileToRead.txt'),'utf8')
console.log(fileContent);
} catch (error) {

throw new Error('FS operation failed')
}


};

await read();
16 changes: 14 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { rename as renameFile } from 'node:fs/promises';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const target = path.join(__dirname, 'files', 'wrongFilename.txt');
const outputFile = path.join(__dirname, 'files', 'properFilename.md');

const rename = async () => {
// Write your code here
try {
await renameFile(target, outputFile);
} catch {
throw new Error('FS operation failed');
}
};

await rename();
await rename();
23 changes: 21 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
const calculateHash = async () => {
// Write your code here

import { createHash } from 'node:crypto';
import { createReadStream } from 'node:fs';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

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

const hash = createHash('sha256');

const calculateHash = async () => {
const stream = createReadStream(fileForHash);
stream.on('readable', () => {
const data = stream.read();
if (data)
hash.update(data);
else {
console.log(`${hash.digest('hex')}`);
}
});
};

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

const require = createRequire(import.meta.url);
import './files/c.js'

const random = Math.random();

let unknownObject;
export let unknownObject;

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

if (random > 0.5) {
unknownObject = require('./files/a.json');
Expand All @@ -20,7 +27,7 @@ console.log(`Path segment separator is "${path.sep}"`);
console.log(`Path to current file is ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

const myServer = createServerHttp((_, res) => {
export const myServer = createServerHttp((_, res) => {
res.end('Request accepted');
});

Expand All @@ -33,8 +40,3 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
unknownObject,
myServer,
};

1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dfddfdfdff defddfjDDDD F F F Fd
13 changes: 11 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { createReadStream } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

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

const read = async () => {
// Write your code here
const readableStream = createReadStream(fileToRead);
readableStream.pipe(process.stdout);
};

await read();
await read();
18 changes: 16 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { Transform, pipeline } from 'stream';
import { stdin, stdout } from 'process';

const input = stdin;
const output = stdout;

const transform = async () => {
// Write your code here
const transformStream = new Transform({
transform(chunk, enc, cb) {
const reversed = chunk.toString().trim().split('').reverse().join('');
cb(null, reversed + '\n');
},
});
pipeline(input, transformStream, output, (err) => {
throw new Error('Error:', err);
});
};

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

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

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

await write();
await write();
47 changes: 45 additions & 2 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
import { Worker, isMainThread, workerData } from 'node:worker_threads';
import os from 'node:os';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const workerModule = path.join(__dirname, 'worker.js');
const incrementalNumber = 10;
const numberOfCores = os.availableParallelism();

const performCalculations = async () => {
// Write your code here
if (isMainThread) {
const workers = [];
const output = [];

for (let i = 0; i < numberOfCores; i++) {
const worker = new Worker(workerModule, { workerData: i + incrementalNumber });
workers.push(worker);
}

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

results.length === numberOfCores ? console.log(results) : null;
});

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

workers.forEach((worker) => {
worker.postMessage(workerData);
});
} else {
const data = workerData;
parentPort.postMessage(data);
}
};

await performCalculations();
await performCalculations();
9 changes: 5 additions & 4 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
const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2);
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
parentPort.postMessage(nthFibonacci(workerData));
};

sendResult();
sendResult();
18 changes: 16 additions & 2 deletions src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { createReadStream, createWriteStream } from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { createGzip } from "zlib";

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

const compress = async () => {
// Write your code here
const gzip = createGzip();
const fileToCompress = createReadStream(
path.join(filesFolder, "fileToCompress.txt")
);
const archive = createWriteStream(path.join(filesFolder, "archive.gz"));

fileToCompress.pipe(gzip).pipe(archive);
};

await compress();
await compress();
Loading