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
let listOfKeyValueArgs = [];
const args = process.argv.slice(2);
for (let i = 0; i < args.length; i += 2) {
listOfKeyValueArgs.push(`${args[i].slice(2)} is ${args[i + 1]}`);
}
const resString = listOfKeyValueArgs.join(', ');
console.log(resString);
};

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 PREFIX = 'RSS_';

const listOfPrefixedEnvs = Object.entries(process.env)
.filter(([key]) => key.startsWith(PREFIX))
.map(([key, value]) => `${key}=${value}`)
.join('; ');
console.log(listOfPrefixedEnvs);
};

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

const spawnChildProcess = async (args) => {
// Write your code here
const scriptPath = path.resolve("src", "cp", "files", "script.js");

const cp = spawn('node', [scriptPath, ...args], {
stdio: ['pipe', 'pipe', 'inherit']
});

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

};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['a', 'b', 'c', 1, 2, 3, null , undefined]);
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 path from 'path';
import { access, constants } from 'node:fs';
import { cp } from 'node:fs/promises';

const copy = async () => {
// Write your code here
const folderPath = path.join('src', 'fs');
const source = path.join(folderPath, `files`);
const destination = path.join(folderPath, `files_copy`);

access(source, constants.F_OK, (err) => {
const error = new Error('FS operation failed');
// Error if source does not exist.
if (err) {
throw error;
}
access(destination, constants.F_OK, (err) => {
// Error if destination exists.
if (!err) {
throw error;
}
cp(source, destination, { recursive: true })
.then(() => console.log('ok'))
.catch((error) => console.log('something went wrong', error))
});

});
};

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 path from 'path';
import { access, constants } from 'node:fs';
import { writeFile } from 'node:fs/promises';

const create = async () => {
// Write your code here
const folder = path.join('src', 'fs', 'files')
const file = path.join(folder, 'fresh.txt');

access(file, constants.F_OK, (err) => {
if (!err) {
throw new Error('FS operation failed');
}
const content = 'I am fresh and young';
writeFile(file, content)
.then(() => console.log('ok'))
.catch(err => console.error('something went wrong', err));
});
};

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

const remove = async () => {
// Write your code here
const folderPath = path.join('src', 'fs', 'files');
const target = path.join(folderPath, `fileToRemove.txt`);

access(target, constants.F_OK, (err) => {
const error = new Error('FS operation failed');
// Error if target does not exist.
if (err) {
throw error;
}
fs.unlink(target)
.then(() => console.log('ok'))
.catch((error) => console.error('something went wrong', error));
});
};

await remove();
22 changes: 21 additions & 1 deletion src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import path from 'path';
import { access, constants } from 'node:fs';
import fs from 'node:fs';

const list = async () => {
// Write your code here
const folderPath = path.join('src', 'fs', 'files');

access(folderPath, constants.F_OK, (err) => {
const error = new Error('FS operation failed');
// Error if target does not exist.
if (err) {
throw error;
}

fs.readdir(folderPath,(err, files) => {
if (err) {
console.error('something went wrong', err);
}
console.log('files:',files)
})

});
};

await list();
22 changes: 21 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import path from 'path';
import { access, constants } from 'node:fs';
import fs from 'node:fs/promises';

const read = async () => {
// Write your code here
const folderPath = path.join('src', 'fs', 'files');
const target = path.join(folderPath, 'fileToRead.txt');

access(target, constants.F_OK, async (err) => {
const error = new Error('FS operation failed');
// Error if target does not exist.
if (err) {
throw error;
}

try {
const data = await fs.readFile(target, { encoding: 'utf8' });
console.log(data);
} catch (err) {
console.error('something went wrong', err);
}
});
};

await read();
26 changes: 25 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import path from 'path';
import { access, constants } from 'node:fs';
import fs from 'node:fs/promises';

const rename = async () => {
// Write your code here
const folderPath = path.join('src', 'fs', 'files');
const source = path.join(folderPath, `wrongFilename.txt`);
const destination = path.join(folderPath, `properFilename.md`);

access(source, constants.F_OK, (err) => {
const error = new Error('FS operation failed');
// Error if source does not exist.
if (err) {
throw error;
}
access(destination, constants.F_OK, (err) => {
// Error if destination exists.
if (!err) {
throw error;
}
fs.rename(source, destination)
.then(() => console.log('ok'))
.catch((error) => console.error('something went wrong', error));
});

});
};

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

const calculateHash = async () => {
// Write your code here
const hash = createHash('sha256');
const filePath = path.join('src', 'hash', 'files', 'fileToCalculateHashFor.txt');
const readableStream = createReadStream(filePath);

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

readableStream.on('end', () => {
console.log(hash.digest('hex'));
})
};

await calculateHash();
26 changes: 19 additions & 7 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c.cjs');
import path from 'node:path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import { fileURLToPath, pathToFileURL } from 'url';
import './files/c.cjs';

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

async function importJSON(fileName) {
const fileURL = pathToFileURL(path.join('src', 'modules', fileName));
const module = await import(fileURL, {
with: { type: 'json' },
})
return module.default;
}

const random = Math.random();

let unknownObject;

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

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

module.exports = {
export {
unknownObject,
myServer,
};
Expand Down
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';
import path from 'path';

const read = async () => {
// Write your code here
const filePath = path.join('src', 'streams', 'files', 'fileToRead.txt');

const stream = createReadStream(filePath);

stream.on('open', () => {
stream.pipe(process.stdout);
});

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

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

const transform = async () => {
// Write your code here
const reverseStringStream = new Transform({
transform(chunk, encoding, callback) {
callback(null, String(chunk).split('').reverse().join('') + "\n");
},
});
await pipeline(process.stdin, reverseStringStream, 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";
import path from 'path';

const write = async () => {
// Write your code here
const filePath = path.join('src', 'streams', 'files', 'fileToWrite.txt');

const stream = createWriteStream(filePath);
await pipeline(process.stdin, stream);
};

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

const performCalculations = async () => {
// Write your code here
const workerPath = path.resolve('src', 'wt', 'worker.js');

let workers = new Array(cpus().length).fill().map(() => new Worker(workerPath));

const promises = workers.map((worker, i) => {
worker.postMessage(10 + i);
return new Promise((resolve) => {
worker.once('message', (data) => {
resolve({ status: 'resolved', data })
});
worker.once('error', () => {
resolve({ status: 'error', data: null })
});
});
});
console.log(await Promise.all(promises));
await Promise.all(workers.map(worker => worker.terminate()));
};

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.once("message", (message) => {
return parentPort.postMessage(nthFibonacci(message));

});
};

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



const compress = async () => {
// Write your code here
const fileToCompress = path.join('src', 'zip', 'files', 'fileToCompress.txt');
const compressedFile = path.join('src', 'zip', 'files', 'archive.gz');


await pipeline(
createReadStream(fileToCompress),
createGzip(),
createWriteStream(compressedFile)
)
};

await compress();
Loading