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
const PREFIX = '--';
const ars = process.argv.reduce((acc,value, index, array) =>
value.startsWith(PREFIX) ? [...acc, `${value.replace(PREFIX, '')} is ${array[index + 1]}`] : acc, []).join(', ');

console.log(ars);
};

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
const PREFIX = 'RSS_';
const rssVariables = Object.entries(process.env).reduce((acc, [key, value]) =>
key.startsWith(PREFIX) ? [...acc, `${key}=${value}`] : acc, []).join('; ');

console.log(rssVariables);
};

parseEnv();
13 changes: 12 additions & 1 deletion src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { fork } from 'child_process';

const spawnChildProcess = async (args) => {
// Write your code here
const SCRIPT_PATH = './files/script.js';
const scriptUrl = new URL(SCRIPT_PATH, import.meta.url);
const childProcess = fork(scriptUrl, args, { silent: true });

process.stdin.pipe(childProcess.stdin);
childProcess.stdout.pipe(process.stdout)

childProcess.stdout.on('data', chunk => {
process.stdout.write(`Received from child process: ${chunk}`)
});
};

spawnChildProcess();
2 changes: 2 additions & 0 deletions src/fs/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ERROR_MSG = 'FS operation failed';
export const FILES_PATH = 'files';
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 { readdir, copyFile, mkdir } from 'fs/promises';
import { exists, getAbsUrl } from './utils.js';
import { ERROR_MSG, FILES_PATH } from './constants.js';

const copy = async () => {
// Write your code here
const COPY_FOLDER_PATH = 'files_copy';
const originalFolderUrl = getAbsUrl(FILES_PATH);
const copyFolderUrl = getAbsUrl(COPY_FOLDER_PATH);

if (await exists(copyFolderUrl) || !(await exists(originalFolderUrl))) {
throw Error(ERROR_MSG);
} else {
const [files] = await Promise.all([readdir(originalFolderUrl), mkdir(copyFolderUrl)]);
const promises = files.map((fileName) =>
copyFile(getAbsUrl(`${FILES_PATH}/${fileName}`), getAbsUrl(`${COPY_FOLDER_PATH}/${fileName}`)));

await Promise.all(promises);
}
};

copy();
14 changes: 13 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { writeFile } from 'fs/promises';
import { ERROR_MSG, FILES_PATH } from './constants.js';
import { exists, getAbsUrl } from './utils.js';

const create = async () => {
// Write your code here
const FILE_NAME = 'fresh.txt';
const CONTENT = 'I am fresh and young';
const url = getAbsUrl(`${FILES_PATH}/${FILE_NAME}`);

if (await exists(url)) {
throw Error(ERROR_MSG);
} else {
await writeFile(url, CONTENT);
}
};

await create();
13 changes: 12 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { rm } from 'fs/promises';
import { exists, getAbsUrl } from './utils.js';
import { FILES_PATH, ERROR_MSG } from './constants.js';

const remove = async () => {
// Write your code here
const FILE_NAME = 'fileToRemove.txt';
const fileUrl = getAbsUrl(`${FILES_PATH}/${FILE_NAME}`);

if (await exists(fileUrl)) {
await rm(fileUrl);
} else {
throw Error(ERROR_MSG);
}
};

await remove();
13 changes: 11 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { readdir } from 'fs/promises';
import { getAbsUrl, exists } from './utils.js';
import { FILES_PATH, ERROR_MSG } from './constants.js';

const list = async () => {
// Write your code here
};
const originalFolderUrl = getAbsUrl(FILES_PATH);

if (await exists(originalFolderUrl)) {
console.log(await readdir(originalFolderUrl));
} else {
throw Error(ERROR_MSG);
}};

await list();
16 changes: 15 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { readFile } from 'fs/promises';
import { exists, getAbsUrl } from './utils.js';
import { ERROR_MSG, FILES_PATH } from './constants.js';

const read = async () => {
// Write your code here
const FILE_NAME = 'fileToRead.txt';
const BUFFER_ENCODING = 'utf8';
const fileUrl = getAbsUrl(`${FILES_PATH}/${FILE_NAME}`);

if (await exists(fileUrl)) {
const content = await readFile(fileUrl, BUFFER_ENCODING);

console.log(content);
} else {
throw Error(ERROR_MSG);
}
};

await read();
15 changes: 14 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { rename as renameFile } from 'fs/promises';
import { exists, getAbsUrl } from './utils.js';
import { ERROR_MSG, FILES_PATH } from './constants.js';

const rename = async () => {
// Write your code here
const ORIGINAL_FILE_NAME = 'wrongFilename.txt';
const NEW_FILE_NAME = 'properFilename.md';
const originalFileUrl = getAbsUrl(`${FILES_PATH}/${ORIGINAL_FILE_NAME}`);
const newFileUrl = getAbsUrl(`${FILES_PATH}/${NEW_FILE_NAME}`);

if (await exists(newFileUrl) || !(await exists(originalFileUrl))) {
throw Error(ERROR_MSG);
} else {
await renameFile(originalFileUrl, newFileUrl);
}
};

await rename();
4 changes: 4 additions & 0 deletions src/fs/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { stat } from 'fs/promises';

export const exists = async (path) => !!await stat(path).catch(() => false);
export const getAbsUrl = (path) => new URL(path, import.meta.url);
13 changes: 12 additions & 1 deletion src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { createHash } from 'crypto';
import { readFile } from 'fs/promises';

const calculateHash = async () => {
// Write your code here
const FILE_PATH = './files/fileToCalculateHashFor.txt';
const ALGORITHM = 'sha256';
const ENCODING = 'hex';
const fileUrl = new URL(FILE_PATH, import.meta.url);

const content = await readFile(fileUrl);
const data = createHash(ALGORITHM).update(content);

console.log(data.digest(ENCODING));
};

await calculateHash();
40 changes: 0 additions & 40 deletions src/modules/cjsToEsm.cjs

This file was deleted.

37 changes: 37 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import path from 'path';
import { fileURLToPath } from 'url';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';

import a from './files/a.json' assert { type: 'json' };
import b from './files/b.json' assert { type: 'json' };
import './files/c.js';

const PORT = 3000;
const unknownObject = Math.random() > 0.5 ? a : b;
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
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) => {
res.end('Request accepted');
});

console.log(unknownObject);

myServer.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log('To terminate it, use Ctrl+C combination');
});

export {
unknownObject,
myServer,
};

8 changes: 7 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createReadStream } from 'fs';
import { pipeline } from 'stream/promises';

const read = async () => {
// Write your code here
const FILE_PATH = './files/fileToRead.txt';
const fileUrl = new URL(FILE_PATH, import.meta.url);

await pipeline(createReadStream(fileUrl), process.stdout);
};

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

const transform = async () => {
// Write your code here
const reverseString = (str) => str.split("").reverse().join('');
const transform = new Transform({
transform(chunk, encoding, callback) {
callback(null, reverseString(chunk.toString()));
},
});

await pipeline(process.stdin, transform, process.stdout);
};

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

const write = async () => {
// Write your code here
const FILE_PATH = './files/fileToWrite.txt';
const fileUrl = new URL(FILE_PATH, import.meta.url);
const writableStream = createWriteStream(fileUrl);

await pipeline(process.stdin, writableStream);
};

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

const performCalculations = async () => {
// Write your code here
const START_NUMBER = 10;
const STATUS_RESOLVED = 'resolved';
const STATUS_ERROR = 'error';
const WORKER_PATH = './worker.js';
const workerUrl = new URL(WORKER_PATH, import.meta.url);

const calculateNthFibonacci = (workerData) => new Promise((resolve) => {
const worker = new Worker(workerUrl, { workerData });

worker.on('message', (data) => resolve({ status: STATUS_RESOLVED, data }));
worker.on('error', () => resolve({ status: STATUS_ERROR, data: null }));
});

const calculation = new Array(cpus().length).fill(null).map((value, index) => calculateNthFibonacci(index + START_NUMBER));
const data = await Promise.all(calculation);

console.log(data);
};

await performCalculations();
4 changes: 3 additions & 1 deletion src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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();
7 changes: 6 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { pipeline } from 'stream/promises';
import { createGzip } from 'zlib';
import { createReadStream, createWriteStream } from 'fs';
import { fileUrl, archiveUrl } from './constants.js';

const compress = async () => {
// Write your code here
await pipeline(createReadStream(fileUrl), createGzip(), createWriteStream(archiveUrl));
};

await compress();
5 changes: 5 additions & 0 deletions src/zip/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const FOLDER_PATH = 'files';
const FILE_NAME = 'fileToCompress.txt';
const ARCHIVE_NAME = 'archive.gz';
export const fileUrl = new URL(`./${FOLDER_PATH}/${FILE_NAME}`, import.meta.url);
export const archiveUrl = new URL(`./${FOLDER_PATH}/${ARCHIVE_NAME}`, import.meta.url);
7 changes: 6 additions & 1 deletion src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { pipeline } from 'stream/promises';
import { createGunzip } from 'zlib';
import { createReadStream, createWriteStream } from 'fs';
import { archiveUrl, fileUrl } from './constants.js';

const decompress = async () => {
// Write your code here
await pipeline(createReadStream(archiveUrl), createGunzip(), createWriteStream(fileUrl));
};

await decompress();