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
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);

const result = [];
for (let i = 0; i < args.length; i += 2) {
const key = args[i].replace(/^--/, '');
const value = args[i + 1];
parts.push(`${key} is ${value}`);
}

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

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

console.log(envVariables);
};

parseEnv();
26 changes: 23 additions & 3 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
const spawnChildProcess = async (args) => {
// Write your code here
import { spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';

export const spawnChildProcess = async (args = []) => {
const scriptPath = fileURLToPath(new URL('./files/script.js', import.meta.url));

const child = spawn(process.execPath, [scriptPath, ...args], {
stdio: ['pipe', 'pipe', 'inherit'],
});

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

return new Promise((resolve, reject) => {
child.once('error', reject);
child.once('exit', (code) => {
process.stdin.unpipe(child.stdin);
child.stdout.unpipe(process.stdout);
if (code === 0) resolve();
else reject(new Error(`Child exited with code ${code}`));
});
});
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess( ["rs", "2025", "node.js"] );
11 changes: 10 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from 'fs/promises';

const copy = async () => {
// Write your code here
const source = new URL('./files/', import.meta.url);
const destination = new URL('./files_copy/', import.meta.url);

try {
await fs.cp(source, destination, { recursive: true, force: false, errorOnExist: true });
} catch {
throw new Error('FS operation failed');
}
};

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

const create = async () => {
// Write your code here
const filePath = new URL('./fresh.txt', import.meta.url)

try {
await fs.writeFile(filePath, 'I am fresh and young', { flag: 'wx'})
} catch {
throw new Error('FS operation failed');
}
};

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

const remove = async () => {
// Write your code here
const fileToRemove = new URL('./files/fileToRemove.txt', import.meta.url);

try {
await fs.unlink(fileToRemove);
} catch {
throw new Error('FS operation failed');
}
};

await remove();
File renamed without changes.
11 changes: 10 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from 'fs/promises';

const list = async () => {
// Write your code here
const folderToList = new URL('./files/', import.meta.url);

try {
const files = await fs.readdir(folderToList);
console.log(files);
} catch {
throw new Error('FS operation failed');
}
};

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

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

try {
const contentToRead = await fs.readFile(fileToRead, 'utf-8');
console.log(contentToRead);
} catch {
throw new Error('FS operation failed');
}
};

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

const rename = async () => {
// Write your code here
const source = new URL('./files/wrongFilename.txt', import.meta.url);
const destination = new URL('./files/properFilename.md', import.meta.url);

try {
try {
await fs.stat(destination);
throw new Error('FS operation failled');
} catch (e){
if (!e || e.code !== 'ENOENT')
throw new Error('FS operation failled');
}
await fs.rename(source, destination);
} catch {
throw new Error('FS operation failed');
}
};

await rename();
21 changes: 18 additions & 3 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
const calculateHash = async () => {
// Write your code here
import { createReadStream } from 'node:fs';
import { createHash } from 'node:crypto';
import { pipeline } from 'node:stream/promises';

const calcHash = async () => {
const filePath = new URL('./files/fileToCalculateHashFor.txt', import.meta.url);

const hash = createHash('sha256');

try {
await pipeline(createReadStream(filePath), hash);

console.log(hash.digest('hex'));
} catch {
throw new Error('FS operation failed');
}
};

await calculateHash();
await calcHash();

22 changes: 12 additions & 10 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const path = require('node:path');
const { release, version } = require('node:os');
const { createServer: createServerHttp } = require('node:http');
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { release, version } from 'node:os';
import { createServer as createServerHttp } from 'node:http';

require('./files/c.cjs');
import './files/c.cjs';

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

const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/b.json');
const random = Math.random();
const unknownObject = random > 0.5
? (await import('./files/a.json', { with: { type: 'json' } })).default
: (await import('./files/b.json', { with: { type: 'json' } })).default;

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

module.exports = {
unknownObject,
myServer,
};
export { unknownObject, myServer };
1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

13 changes: 12 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'node:fs';

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

const readStream = fs.createReadStream(filePath, { encoding: 'utf-8' });

readStream.pipe(process.stdout);

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

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 @@
import { Transform } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { Readable } from 'node:stream';

const transform = async () => {
// Write your code here
const mockData = process.stdin ? Readable.from('Hello world\n') : process.stdin;

const reverseTransform = new Transform({
transform(chunk, _enc, cb) {
const reversed = chunk.toString('utf8').split('').reverse().join('');
cb(null, reversed);
}
});

try {
await pipeline(mockData, reverseTransform, process.stdout);
} catch {
console.error('Stream operation failed');
}
};

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

const write = async () => {
// Write your code here
const filePath = new URL('./files/fileToWrite.txt', import.meta.url);
const writeStream = fs.createWriteStream(filePath, { encoding: 'utf-8' });
const mockData = process.stdin ? Readable.from('Hello world\n') : process.stdin;


try {
await pipeline(mockData, writeStream);
} catch {
throw new Error('FS operation failed');
}
};

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

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

const runWorker = (n) =>
new Promise((resolve) => {
const worker = new Worker(new URL('./worker.js', import.meta.url), {
workerData: n, type: 'module'
});

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

worker.once('error', (err) => {
console.error('worker error:', err)
resolve({ status: 'error', data: null });
});

worker.once('exit', (code) => {
if (code !== 0) {
resolve({ status: 'error', data: null });
}
});
});

const coreCount = cpus().length;
const start = 10;

const workerPromises = Array.from({ length: coreCount }, (_, i) =>
runWorker(start + i)
);

const results = await Promise.all(workerPromises);
console.log(results);
};

await performCalculations();
13 changes: 11 additions & 2 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { parentPort, workerData } from 'node:worker_threads';

// n should be received from main thread
const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2);

const sendResult = () => {
const sendResult = (n) => {
// This function sends result of nthFibonacci computations to main thread
const result = nthFibonacci(Number(n));
parentPort.postMessage(result);
};

sendResult();
if (workerData !== undefined) {
sendResult(workerData);
} else {
parentPort.once('message', sendResult);
}

18 changes: 17 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import fs from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { createGzip } from 'node:zlib';

const compress = async () => {
// Write your code here
const source = new URL('./files/fileToCompress.txt', import.meta.url);
const destination = new URL('./files/archive.gz', import.meta.url);

try {
await pipeline(
fs.createReadStream(source),
createGzip(),
fs.createWriteStream(destination)
);
} catch {

throw new Error('FS operation failed');
}
};

await compress();
Loading