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
8 changes: 7 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const parseArgs = () => {
// Write your code here
const argsList = process.argv.slice(2);
const argsKeys = argsList.filter((_, key) => key % 2 === 0);
const argsValues = argsList.filter((_, key) => key % 2 === 1);

const res = argsKeys.map((value, key) => `${value.replace('--', '')} is ${argsValues[key]}`).join(', ');

console.log(res);
};

parseArgs();
8 changes: 7 additions & 1 deletion src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const parseEnv = () => {
// Write your code here
const keys = Object.keys(process.env).filter((key) => {
return key.startsWith('RSS_');
}).map((key) => {
return `${key}=${process.env[key]}`
});

console.log(keys.join('; '));
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const cpFilePath = path.join(folderPath, 'files', 'script.js');

const cp = spawn('node', [cpFilePath, ...args]);

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['1', 2]);
26 changes: 25 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import {readdir, mkdir} from 'fs/promises';
import {createReadStream, createWriteStream} from 'fs';
import path from 'path';

const copy = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);

const currentFolderPath = path.join(folderPath, 'files');
const newFolderPath = path.join(folderPath, 'files_copy');

mkdir(newFolderPath, {
flags: 'ax'
}).then(() => {
return readdir(currentFolderPath, {
flags: 'r'
});
}).then((fileList) => {
fileList.forEach((filename) => {
const readFile = createReadStream(path.join(currentFolderPath, filename));
const writeFile = createWriteStream(path.join(newFolderPath, filename));

readFile.pipe(writeFile);
})
}).catch(() => {
throw Error('FS operation failed');
});
};

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

const create = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const filePath = path.join(folderPath, 'files', 'fresh.txt');

const fileStream = createWriteStream(filePath, {
flags: 'ax'
});

fileStream.on('error', () => {
throw Error('FS operation failed');
})

fileStream.write('I am fresh and young');

fileStream.end();
};

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

const remove = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const currentFolderPath = path.join(folderPath, 'files', 'fileToRemove.txt');

const deleteFile = unlink(currentFolderPath);
deleteFile.catch(() => {
throw Error('FS operation failed');
})
};

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

const list = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const currentFolderPath = path.join(folderPath, 'files');

readdir(currentFolderPath, {
flags: 'r'
}).then((filenames) => {
console.log(filenames);
}).catch(() => {
throw Error('FS operation failed');
});
};

await list();
21 changes: 20 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import path from 'path';
import {createReadStream} from 'fs';

const read = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const currentFilePath = path.join(folderPath, 'files', 'fileToRead.txt');

let res = '';

const readFile = createReadStream(currentFilePath, 'utf8');
readFile.on('data', (chunk) => {
res += chunk.toString();
})

readFile.on('end', () => {
console.log(res);
});

readFile.on('error', () => {
throw Error('FS operation failed');
});
};

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 path from 'path';
import {rename as renameFile, readFile} from 'fs/promises';

const rename = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const filePath = path.join(folderPath, 'files', 'wrongFilename.txt');
const newFilePath = path.join(folderPath, 'files', 'properFilename.md');

readFile(newFilePath, {
flag: 'ax+'
}).then(() => {
renameFile(filePath, newFilePath);
}).catch(() => {
throw Error('FS operation failed');
})
};

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

const calculateHash = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const filePath = path.join(folderPath, 'files', 'fileToCalculateHashFor.txt');

const readFile = createReadStream(filePath);
const transformFile = new stream.Transform({
transform(chunk, encoding, callback) {
const res = createHash('sha256').update(chunk.toString()).digest('hex');
callback(null, res);
}
});

readFile.pipe(transformFile).pipe(process.stdout);
readFile.on('end', () => {process.stdout.write('\n')});
};

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

import { createRequire } from 'module';
import './files/c.js';

const importFile = createRequire(import.meta.url);
const __filename = path.basename(process.argv[1]);
const __dirname = path.dirname(process.argv[1]);

const random = Math.random();
let unknownObject;

if (random > 0.5) {
unknownObject = importFile('./files/a.json');
} else {
unknownObject = importFile('./files/b.json');
}

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');
});

const PORT = 3000;

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,
}
12 changes: 11 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import path from 'path';
import {createReadStream} from 'fs';

const read = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const filePath = path.join(folderPath, 'files', 'fileToRead.txt');

const readFile = createReadStream(filePath);

readFile.pipe(process.stdout);

readFile.on('end', () => {process.stdout.write('\n')});
};

await read();
11 changes: 10 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import stream from 'stream';
const transform = async () => {
// Write your code here
const transformText = new stream.Transform({
transform(chunk, encoding, callback) {
const res = chunk.toString().split('').filter((letter) => letter !== '\n').reverse();
res.push('\n');
callback(null, res.join(''));
}
});

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

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

const write = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const filePath = path.join(folderPath, 'files', 'fileToWrite.txt');
const writeStream = createWriteStream(filePath);
process.stdin.pipe(writeStream);
};

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} from 'worker_threads';
import {availableParallelism} from 'os';
import path from 'path';

const cpusLength = availableParallelism();

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

const startNumber = 10;
const folderPath = path.dirname(process.argv[1]);
const workerFilePath = path.join(folderPath, 'worker.js');

let nullsArray = (new Array(cpusLength)).fill(null);

Promise.all(nullsArray.map((_, k) => {
return new Promise((resolve) => {
const worker = new Worker(workerFilePath, {
workerData: startNumber + k
});

worker.on('message', data => {
resolve({
status: 'resolved',
data: data
})
})

worker.on('error', data => {
resolve({
status: 'error',
data: null
})
})
})
})).then(data => {
console.log(data);
});
};

await performCalculations();
5 changes: 3 additions & 2 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
import {workerData, parentPort} 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
parentPort.postMessage(nthFibonacci(workerData));
};

sendResult();
14 changes: 13 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import {createGzip} from 'zlib';
import {createWriteStream, createReadStream} from 'fs';
import path from 'path';

const compress = async () => {
// Write your code here
const folderPath = path.dirname(process.argv[1]);
const filePath = path.join(folderPath, 'files', 'fileToCompress.txt');
const zipFile = path.join(folderPath, 'files', 'archive.gz')

const fileRead = createReadStream(filePath);
const writeZip = createWriteStream(zipFile);
const gzip = createGzip();

fileRead.pipe(gzip).pipe(writeZip);
};

await compress();
Loading