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
12 changes: 11 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
const result = [];
for (let i = 0; i < args.length; i += 2) {
if (args[i].startsWith('--')) {
const propName = args[i].slice(2);
const value = args[i + 1];
result.push(`${propName} is ${value}`);
}
}

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

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 @@
import readline from 'readline';

const parseEnv = () => {
// Write your code here
const envVars = process.env;
const prefix = 'RSS_';
const rssVars = Object.keys(envVars).filter(key => key.startsWith(prefix));
const rssValues = rssVars.map(key => `${key}=${envVars[key]}`);
const rssString = rssValues.join('; ');
console.log(rssString);
};

parseEnv();
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/promises';

const copy = async () => {
// Write your code here
const sourceDir = 'files';
const destinationDir = 'files_copy';

const sourceDirExists = await fs.access(sourceDir)
.then(() => true)
.catch(() => false);
if (!sourceDirExists) {
throw new Error('FS operation failed');
}
const destinationDirExists = await fs.access(destinationDir)
.then(() => true)
.catch(() => false);
if (!destinationDirExists) {
await fs.mkdir(destinationDir);
} else {
throw new Error('FS operation failed');
}

await fs.cp(sourceDir, destinationDir, { recursive: true });

};

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

const create = async () => {
// Write your code here
const content = 'I am fresh and young';
const filePath = path.join('files', 'fresh.txt');
const fileExists = await fs.access(filePath)
.then(() => true)
.catch(() => false);
if (fileExists) {
throw new Error('FS operation failed');
}
await fs.writeFile(filePath, content, (err) => {
if (err) throw err;
});
};

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

const remove = async () => {
// Write your code here
const fileExist = await fs.access('./files/fileToRemove.txt').then(() => true).catch(() => false);
if (!fileExist) {
throw new Error('FS operation failed');
}
await fs.unlink('./files/fileToRemove.txt');
};

await remove();
9 changes: 8 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from 'fs/promises'

const list = async () => {
// Write your code here
const folderExist = await fs.access('./files').then(() => true).catch(() => false);
if (!folderExist) {
throw new Error('FS operation failed');
}
const files = await fs.readdir('./files');
console.log(files);
};

await list();
9 changes: 8 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { readFile, access } from 'node:fs/promises';
const read = async () => {
// Write your code here
const fileExist = await access('./files/fileToRead.txt').then(() => true).catch(() => false);
if (!fileExist) {
throw new Error('FS operation failed');
}
const content = await readFile('./files/fileToRead.txt', 'utf8');
console.log(content);

};

await read();
9 changes: 8 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from 'fs/promises';

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

const fileExist = await fs.access('./files/wrongFilename.txt').then(() => true).catch(() => false);
if (!fileExist) {
throw new Error('FS operation failed');
}
await fs.rename('./files/wrongFilename.txt', './files/properFilename.md');
};

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

const calculateHash = async () => {
// Write your code here
return new Promise((resolve, reject) => {
const hash = createHash('sha256');
const rs = createReadStream('./files/fileToCalculateHashFor.txt');

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

rs.on('end', () => {
const hashDigest = hash.digest('hex');
console.log(hashDigest);
resolve(hashDigest);
});

rs.on('error', (err) => {
reject(err);
});
});
};

await calculateHash();
20 changes: 10 additions & 10 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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 { fileURLToPath } from 'url';
import { createServer as createServerHttp} from 'http';
import './files/c.mjs';

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');
unknownObject = await import('./files/a.json', {assert: {type: "json"}});
} else {
unknownObject = require('./files/b.json');
unknownObject = await import('./files/b.json', {assert: {type: "json"}});
}

console.log(`Release ${release()}`);
Expand All @@ -33,8 +36,5 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

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

File renamed without changes.
15 changes: 14 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { createReadStream } from 'fs';

const read = async () => {
// Write your code here
return new Promise((resolve, reject) => {
const rs = createReadStream('./files/fileToRead.txt', 'utf8');
rs.on('data', (chunk) => {
process.stdout.write(chunk);
});
rs.on('end', () => {
resolve();
});
rs.on('error', (err) => {
reject(err);
});
});
};

await read();
19 changes: 18 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const transform = async () => {
// Write your code here
return new Promise((resolve, reject) => {
process.stdin.on('data', (chunk) => {
try {
const reversedChunk = chunk.toString().split('').reverse().join('');
process.stdout.write(reversedChunk);
} catch (error) {
reject(error);
}
});

process.stdin.on('end', () => {
resolve();
});

process.stdin.on('error', (err) => {
reject(err);
});
});
};

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

const write = async () => {
// Write your code here
return new Promise((resolve, reject) => {
const ws = createWriteStream('./files/fileToWrite.txt');
process.stdin.on('data', (chunk) => {
ws.write(chunk);
});
process.stdin.on('end', () => {
ws.end();
resolve();
});
process.stdin.on('error', (err) => {
reject(err);
});

ws.on('error', (err) => {
reject(err);
});
});
};

await write();
23 changes: 22 additions & 1 deletion 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 numCores = cpus().length;
console.log(`Number of CPU cores: ${numCores}`);
const workerPath = './worker.js';
const workerPromises = [];
for (let i = 0; i < numCores; i++) {
const workerData = 10 + i;
const worker = new Worker(workerPath, {workerData});
workerPromises.push(new Promise((resolve, reject) => {
worker.on('message', (result) => {
resolve({ status: 'resolved', data: result });
});
worker.on('error', (error) => {
reject({ status: 'error', data: null });
});
}));
}
const workerResults = await Promise.allSettled(workerPromises);
console.log(workerResults);
return workerResults;
};

await performCalculations();
11 changes: 9 additions & 2 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// n should be received from main thread
import { parentPort, workerData } 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
try {

const result = nthFibonacci(workerData);
parentPort.postMessage(result);
} catch (error){
throw error;
}

};

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

const compress = async () => {
// Write your code here
return new Promise((resolve, reject) => {
const gzip = createGzip();
const source = createReadStream("./files/fileToCompress.txt");
const destination = createWriteStream("./files/archive.gz");

pipeline(source, gzip, destination, (err) => {
if (err) {
console.error('An error occurred:', err);
process.exitCode = 1;
reject(err);
} else {
resolve();
}
});
});
};

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

const decompress = async () => {
// Write your code here
try {
const gunzip = createGunzip();
const source = createReadStream("./files/archive.gz");
const destination = createWriteStream("./files/fileToCompress.txt");
await pipeline(source, gunzip, destination);
} catch (err) {
console.error('An error occurred during decompression:', err);
process.exitCode = 1;
throw err;
}
};

await decompress();