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
6 changes: 5 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const parseArgs = () => {
// Write your code here
process.argv.slice(2).forEach((arg, i, arr) => {
if (arg.startsWith('--')) {
console.log(`${arg.replace('--', '')} is ${arr[i + 1]}`);
}
});
};

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
for (const [key, value] of Object.entries(process.env)) {
if(key.startsWith('RSS_')) {
console.log(`${key}=${value}`);
}
}
};

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

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const scriptPath = `${dirname}/files/script.js`;
const spawnChildProcess = async (args) => {
// Write your code here
const childProcess = fork(scriptPath, args, {silent: true});
process.stdin.pipe(childProcess.stdin);
childProcess.stdout.pipe(process.stdout);
};

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

const currentFilename = fileURLToPath(import.meta.url);
const currentDirectory = path.dirname(currentFilename);
const filesDirectory = path.join(currentDirectory, '/files');
const copyFilesDirectory = path.join(currentDirectory, '/files_copy');
const errorMsg = 'FS operation failed';
const copy = async () => {
// Write your code here
if (!fs.existsSync(filesDirectory) || fs.existsSync(copyFilesDirectory)) throw new Error(errorMsg);

const filenames = fs.readdirSync(filesDirectory);
fs.mkdirSync(copyFilesDirectory);

for (let filename of filenames) {
fs.copyFile(path.join(filesDirectory, `/${filename}`), path.join(copyFilesDirectory, `/${filename}`), (e)=>{})
}
};

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

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const filePath = `${dirname}/files/fresh.txt`;
const create = async () => {
// Write your code here
try {
await fs.writeFile(filePath, 'I am fresh and young', {flag: 'wx'});
} catch (e) {
throw new Error('FS operation failed');
}
};

await create();
14 changes: 13 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fs from 'fs/promises';
import {fileURLToPath} from 'url';
import path from 'path';

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const fileToRemove = path.join(dirname, '/files/fileToRemove.txt');
const errorMsg = 'FS operation failed';
const remove = async () => {
// Write your code here
try {
await fs.rm(fileToRemove)
} catch (e) {
throw new Error(errorMsg);
}
};

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

const currentFilename = fileURLToPath(import.meta.url);
const filesDirectory = path.join(path.dirname(currentFilename), '/files')
const list = async () => {
// Write your code here
try {
const files = await fs.readdir(filesDirectory);
for (const file of files) {
console.log(file);
}
} catch (err) {
throw new Error('FS operation failed')
}
};

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

const currentFilename = fileURLToPath(import.meta.url);
const fileToRead = path.join(path.dirname(currentFilename), '/files/fileToRea3d.txt');
const read = async () => {
// Write your code here
try {
const content = await fs.readFile(fileToRead, {encoding: 'utf8'});
console.log(content);
} catch (e) {
throw new Error('FS operation failed');
}
};

await read();
20 changes: 19 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import fs from 'fs/promises';
import {fileURLToPath} from "url";
import path from "path";

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const oldFilePath = path.join(dirname, '/files/wrongFilename.txt');
const newFilePath = path.join(dirname, '/files/properFilename.md');
const errorMsg = 'FS operation failed';

const rename = async () => {
// Write your code here
const fileExists = await fs.access(newFilePath, fs.constants.F_OK).then(() => true, () => false);
if (fileExists) throw new Error(errorMsg);

try {
await fs.rename(oldFilePath, newFilePath);
} catch (e) {
console.log(e);
throw new Error(errorMsg);
}
};

await rename();
20 changes: 19 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
import crypto from 'crypto';
import fs from 'fs';
import {fileURLToPath} from "url";
import path from "path";

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const hashFilePath = `${dirname}/files/fileToCalculateHashFor.txt`;

const calculateHash = async () => {
// Write your code here
const fd = fs.createReadStream(hashFilePath);
const hash = crypto.createHash('sha256');
hash.setEncoding('hex');

fd.on('end', function () {
hash.end();
console.log(hash.read());
});

fd.pipe(hash);
};

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

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

let unknownObject;

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


console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${path.sep}"`);
Expand All @@ -33,7 +37,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export {
unknownObject,
myServer,
};
Expand Down
2 changes: 2 additions & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test write
test yhdfbd
9 changes: 8 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const filePath = `${dirname}/files/fileToRead.txt`;
const read = async () => {
// Write your code here
fs.createReadStream(filePath).pipe(process.stdout);
};

await read();
10 changes: 9 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Transform } from 'stream';

const reverseTransform = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().split("").reverse().join(""));
callback();
}
})
const transform = async () => {
// Write your code here
process.stdin.pipe(reverseTransform).pipe(process.stdout);
};

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

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const filePath = `${dirname}/files/fileToWrite.txt`;
const write = async () => {
// Write your code here
const file = fs.createWriteStream(filePath, {flags: 'a'});
process.stdin.pipe(file);
};

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


const filename = fileURLToPath(import.meta.url);
const workerPath = `${path.dirname(filename)}/worker.js`;
const performCalculations = async () => {
// Write your code here
};
const cpuNumber = cpus().length;
const workersArray = [];
for (let i = 0; i < cpuNumber; i++) {
const promise = new Promise((resolve) => {
const worker = new Worker(workerPath, {workerData: i + 10});
worker.on('message', (res) => resolve({status: 'resolved', data: res}));
worker.on('error', () => resolve({status: 'error', data: null}));
});
workersArray.push(promise);
}
const resul = await Promise.all(workersArray);
console.log(resul);
}

await performCalculations();
3 changes: 2 additions & 1 deletion src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {workerData, parentPort} from 'worker_threads';
// n should be received from main thread
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();
16 changes: 15 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import {createGzip} from 'zlib';
import {pipeline} from 'stream/promises';
import {createReadStream, createWriteStream} from 'fs';
import {fileURLToPath} from "url";
import path from "path";

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const compressFile = `${dirname}/files/fileToCompress.txt`;
const compressedFile = `${dirname}/files/archive.gz`;

const compress = async () => {
// Write your code here
const gzip = createGzip();
const source = createReadStream(compressFile);
const destination = createWriteStream(compressedFile);
await pipeline(source, gzip, destination);
};

await compress();
16 changes: 15 additions & 1 deletion src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import {createGunzip} from 'zlib';
import {pipeline} from 'stream/promises';
import {createReadStream, createWriteStream} from 'fs';
import {fileURLToPath} from "url";
import path from "path";

const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const decompressedFile = `${dirname}/files/fileToDecompress.txt`;
const compressedFile = `${dirname}/files/archive.gz`;

const decompress = async () => {
// Write your code here
const gzip = createGunzip();
const source = createReadStream(compressedFile);
const destination = createWriteStream(decompressedFile);
await pipeline(source, gzip, destination);
};

await decompress();