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
7 changes: 6 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const parseArgs = () => {
// Write your code here
let args = process.argv.slice(2);
var output = [];
for (let i = 0; i < args.length; i += 2) {
output.push(`${args[i].substring(2)} is ${args[i + 1]}`);
}
console.log(output.join(", "));
};

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
let result = Object.entries(process.env)
.filter(([key, value]) => key.startsWith('RSS_'))
.map(([key, value]) => `${key}=${value}`)
.join("; ");
console.log(result);
};

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

const spawnChildProcess = async (args) => {
// Write your code here
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const scriptPath = path.join(__dirname, 'files', 'script.js');
const child = spawn('node', [scriptPath, ...args], { stdio: ['pipe', 'pipe', 'inherit', 'ipc'] });
process.stdin.pipe(child.stdin);
child.stdout.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
Expand Down
42 changes: 41 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
import { existsSync } from 'fs';
import fs from 'fs/promises';
import path from 'path';
import url from 'url';

async function copyFolderRecursive(sourcePath, destPath) {
await fs.mkdir(destPath);
const files = await fs.readdir(sourcePath);
for (const file of files) {
const sourceFilePath = path.join(sourcePath, file);
const destFilePath = path.join(destPath, file);
const stats = await fs.stat(sourceFilePath);
if (stats.isDirectory()) {
await copyFolderRecursive(sourceFilePath, destFilePath);
} else {
fs.copyFile(sourceFilePath, destFilePath);
}
}
}

const copy = async () => {
// Write your code here
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const sourcePath = path.join(__dirname, 'files');
const destPath = path.join(__dirname, 'files_copy');

if (!existsSync(sourcePath) || existsSync(destPath)) {
throw new Error('FS operation failed');
}

await fs.mkdir(destPath);
const files = await fs.readdir(sourcePath);

for (const file of files) {
const sourceFilePath = path.join(sourcePath, file);
const destFilePath = path.join(destPath, file);
const stats = await fs.stat(sourceFilePath);
if (stats.isDirectory()) {
await copyFolderRecursive(sourceFilePath, destFilePath);
} else {
fs.copyFile(sourceFilePath, destFilePath);
}
}
};

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

const create = async () => {
// Write your code here
const filename = 'fresh.txt';
const content = 'I am fresh and young';

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

if (existsSync(filePath)) {
throw new Error('FS operation failed');
};

await fs.writeFile(filePath, content);
};

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 { existsSync } from 'fs';
import fs from 'fs/promises';
import path from 'path';
import url from 'url';

const remove = async () => {
// Write your code here
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToRemove.txt');

if (!existsSync(filePath)) {
throw new Error('FS operation failed');
}

await fs.unlink(filePath);
};

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 { existsSync } from 'fs';
import fs from 'fs/promises';
import path from 'path';
import url from 'url';

const list = async () => {
// Write your code here
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const dirPath = path.join(__dirname, 'files');

if (!existsSync(dirPath)) {
throw new Error('FS operation failed');
}

const files = await fs.readdir(dirPath);
console.log(files);
};

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

const read = async () => {
// Write your code here
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');

if (!existsSync(filePath)) {
throw new Error('FS operation failed');
}

const contents = await fs.readFile(filePath, 'utf8');
console.log(contents);
};

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 { existsSync } from 'fs';
import fs from 'fs/promises';
import path from 'path';
import url from 'url';

const rename = async () => {
// Write your code here
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const sourcePath = path.join(__dirname, 'files', 'wrongFilename.txt');
const destPath = path.join(__dirname, 'files', 'properFilename.md');

if (!existsSync(sourcePath) || existsSync(destPath)) {
throw new Error('FS operation failed');
}

await fs.rename(sourcePath, destPath);
};

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

const calculateHash = async () => {
// Write your code here
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');

if (!existsSync(filePath)) {
throw new Error('FS operation failed');
}

const fileContents = await fs.readFile(filePath);
const hash = crypto.createHash('sha256').update(fileContents).digest('hex');
console.log(hash);
};

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

This file was deleted.

36 changes: 36 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'path';
import { release, version } from 'os';
import { createServer } from 'http';
import './files/c.js';

const random = Math.random();

let unknownObject;

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

console.log(`Path to current file is ${import.meta.url}`);
console.log(`Path to current directory is ${path.dirname(import.meta.url)}`);

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

const PORT = 3000;

console.log(unknownObject.default);

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

export { unknownObject, myServer };
10 changes: 9 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import fs from 'fs';
import path from 'path';
import url from 'url';

const read = async () => {
// Write your code here
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');

const readStream = fs.createReadStream(filePath);
readStream.pipe(process.stdout);
};

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 { Transform } from 'stream';

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

process.stdin.pipe(reverseStream).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 url from 'url';

const write = async () => {
// Write your code here
let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, 'files', 'fileToWrite.txt');

const writeStream = fs.createWriteStream(filePath);
process.stdin.pipe(writeStream);
};

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

const performCalculations = async () => {
// Write your code here
const numCores = os.cpus().length;
const workers = [];

let __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const workerPath = path.join(__dirname, 'worker.js');

for (let i = 0; i < numCores; i++) {
const worker = new Promise((resolve, reject) => {
const worker = new Worker (workerPath, i);
worker.once('message', (data) => {
resolve({status: 'resolved', data});
worker.terminate();
});
worker.once('error', (_) => {
resolve({status: 'error', data: null});
worker.terminate();
});
worker.postMessage(i + 10);
});
workers.push(worker);
}

await Promise.all(workers).then((data) => {
console.log(data);
});
};

await performCalculations();
7 changes: 6 additions & 1 deletion src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { 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.on('message', (data) => {
const result = nthFibonacci(data);
parentPort.postMessage(result);
});
};

sendResult();
Loading