Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a69baaa
Implement create method
Duude92 Apr 23, 2025
1e5c62b
Implement create method
Duude92 Apr 23, 2025
5312cb8
Implement copy method
Duude92 Apr 23, 2025
439f68d
Implement rename method
Duude92 Apr 23, 2025
8c9a770
Implement delete method
Duude92 Apr 23, 2025
859805a
Implement list method
Duude92 Apr 23, 2025
c6adb50
Implement read method
Duude92 Apr 23, 2025
5523cab
Implement env module
Duude92 Apr 23, 2025
09a6c73
Implement args module
Duude92 Apr 23, 2025
0ed472f
Implementation of cjm2esm module
Duude92 Apr 23, 2025
975dc6d
Implementation of calcHash module
Duude92 Apr 23, 2025
abd00f1
Implementation of readableStream task module
Duude92 Apr 23, 2025
96258f0
Implementation of writeableStream task module
Duude92 Apr 23, 2025
aab6f05
Implementation of transform task module
Duude92 Apr 23, 2025
d7ea771
Implementation of compress task
Duude92 Apr 23, 2025
e1f3224
Implementation of decompress task
Duude92 Apr 23, 2025
18a9d6a
Implementation of working thread task
Duude92 Apr 23, 2025
ea2ba5c
Implementation of child process task
Duude92 Apr 23, 2025
a24dfca
Fix create task //self check
Duude92 Apr 24, 2025
f62f657
//typo
Duude92 Apr 24, 2025
134d13d
fix module task reading json//possible fix, cuz there's no answer on …
Duude92 Apr 24, 2025
fa39519
Added comment with link reference to discord answer
Duude92 Apr 24, 2025
d139f22
Comment
Duude92 Apr 25, 2025
d771ace
Throw error instead console.error //previous code could cause error w…
Duude92 Apr 26, 2025
6611d53
fix: cjsToEsm.cjs, missing extension in require c.cjs, log message ty…
alexey-koran Apr 25, 2025
e0a2bb8
Implementation of cjm2esm module
Duude92 Apr 23, 2025
42615df
Rebase onto main with updated commit aa4d766a523fa45f19e1d2aae5031f1d…
Duude92 Apr 28, 2025
91b0e6b
Revert "Implementation of cjm2esm module"
Duude92 Apr 28, 2025
4bc3068
Rework of Hash function because of missed "using Streams API" part fo…
Duude92 Apr 28, 2025
df41e94
Added commented simulation of worker failure
Duude92 Apr 28, 2025
32e2a06
Added welcome message to transform.js/write.js, removed archive.gz
Duude92 Apr 28, 2025
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
// Write your code here
console.log(process.argv.slice(2)
.filter(value => value.startsWith('--'))
// map modified argument key (without '--') with argument value (find index of key and increment it to get next argv)
.map(value => `${value.replace('--', '')} is ${process.argv[process.argv.findIndex(value1 => value1 === value) + 1]}`)
.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
// Write your code here
console.log(Object.entries(process.env)
.filter(([key,value], index) => key.startsWith('RSS_'))
.map(([key,value],index) => `${key}=${value}` )
.join('; '));
};

parseEnv();
16 changes: 15 additions & 1 deletion src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import * as child_process from "node:child_process";
import {fileURLToPath} from "url";
import path from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fileName = path.join(__dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
let child = child_process.fork(fileName, args, {
stdio: ["pipe", "pipe", "pipe", "ipc"],
});

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

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
await spawnChildProcess(['qwerty', 3]);
22 changes: 22 additions & 0 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import {existsSync} from 'node:fs';
import {cp} from 'node:fs/promises';
import {fileURLToPath} from 'url';
import {dirname, join} from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const sourceDir = join(__dirname, 'files');
const destDir = join(__dirname, 'files_copy');
const sourceExists = existsSync(sourceDir);
const destExists = existsSync(destDir);

const copy = async () => {
// Write your code here
if(!sourceExists || destExists) {
throw new Error('FS operation failed');
}
cp(sourceDir, destDir, {recursive: true})
.then(() => {
console.log('Files copied successfully!');
})
.catch((error) => {
console.error('Error copying files:', error);
});
};

await copy();
23 changes: 22 additions & 1 deletion src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import {existsSync, constants, writeFileSync, access} from 'node:fs';
import {mkdir} from 'node:fs/promises';
import {fileURLToPath} from 'url';
import {dirname, join} from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const fileDirname = join(__dirname, 'files');
const filePath = join(fileDirname, 'fresh.txt');

const create = async () => {
// Write your code here
// Write your code here
access(filePath, constants.F_OK, async (err) => {
// If file exists, throw an error
if (!err)
throw new Error('FS operation failed');
if (!existsSync(fileDirname)) await mkdir(fileDirname, {recursive: true});
writeFileSync(filePath, 'I am fresh and young', (error) => {
if (error) throw error;
});
console.log('fresh.txt filled successfully!');

});
};

await create();
15 changes: 14 additions & 1 deletion src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import {fileURLToPath} from "url";
import {dirname, join} from "path";
import {existsSync, unlink} from "node:fs"

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const filePath = join(__dirname, 'files', 'fileToRemove.txt');

const remove = async () => {
// Write your code here
// Write your code here
if (!existsSync(filePath)) throw new Error('FS operation failed');
await unlink(filePath, (error) => {
if (error) throw error;
console.log('File successfully removed');
});
};

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 {fileURLToPath} from "url";
import {dirname, join} from "path";
import fs from "node:fs"

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const fileDirname = join(__dirname, 'files');

const list = async () => {
// Write your code here
// Write your code here
if(!fs.existsSync(fileDirname)) throw new Error('FS operation failed');
fs.readdir(fileDirname, (err, files) => {
if(err) throw err;
console.log(files);
})
};

await list();
17 changes: 16 additions & 1 deletion src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import {fileURLToPath} from "url";
import {dirname, join} from "path";
import fs from "node:fs";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const fileName = join(__dirname, 'files', 'fileToRead.txt');

const read = async () => {
// Write your code here
// Write your code here
if (!fs.existsSync(fileName)) throw new Error('FS operation failed');
fs.readFile(fileName, 'utf8', (err, buffer) => {
if (err) throw err;
if (buffer) {
console.log(buffer);
}
})
};

await read();
23 changes: 22 additions & 1 deletion src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import fs, {existsSync} from 'node:fs'
import {fileURLToPath} from "url";
import {dirname, join} from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const fileDirname = join(__dirname, 'files');
const srcPath = join(fileDirname, 'wrongFilename.txt');
const destPath = join(fileDirname, 'properFilename.md');
const sourceExists = existsSync(srcPath);
const destExists = existsSync(destPath);

const rename = async () => {
// Write your code here
// Write your code here
if(!sourceExists || destExists) {
throw new Error('FS operation failed');
}
await fs.rename(srcPath, destPath, (err) => {
if(err) {
throw err;
}
console.log('File renamed successfully!');
} )
};

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 crypto from 'node:crypto'
import fs from "node:fs";
import {fileURLToPath} from "url";
import path from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fileName = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');

const calculateHash = async () => {
// Write your code here
// Write your code here
const hash = crypto.createHash('sha256');
const input = fs.createReadStream(fileName);
const hashSum = input.pipe(hash);
hashSum.on('end', () =>process.stdout.write('\n'));
hashSum.setEncoding('hex').pipe(process.stdout);
};

await calculateHash();
2 changes: 1 addition & 1 deletion src/modules/cjsToEsm.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
require('./files/c.cjs');

const random = Math.random();

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

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

let unknownObject;
let fileName;

if (random > 0.5) {
fileName = 'a.json';
} else {
fileName = 'b.json';
}
// Both variants are viable https://discord.com/channels/755676888680366081/1363206860160631114/1365003228994998403
// unknownObject = {default: JSON.parse(fs.readFileSync(path.join(__dirname, 'files', fileName), 'utf-8'))};
unknownObject = await import(`./files/${fileName}`, { with: { 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 ${__filename}`);
console.log(`Path to current directory is ${__dirname}`);

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

const PORT = 3000;
// Assign to emp object prototype to get rid of [Module prototype null] or something
console.log(Object.assign({}, unknownObject));

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


2 changes: 1 addition & 1 deletion src/modules/files/c.cjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('Hello from c.js!');
console.log('Hello from c.cjs!');
14 changes: 13 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import fs from 'node:fs';
import {fileURLToPath} from "url";
import path from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fileName = path.join(__dirname, 'files', 'fileToRead.txt');

const read = async () => {
// Write your code here
// Write your code here
let fileStream = fs.createReadStream(fileName);
fileStream.pipe(process.stdout);
// Added on end event stdout write new line to ensure debug info would be written on new line + ensure to flush stdout buffer
fileStream.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 {Transform} from 'stream';
const transform = async () => {
// Write your code here
// Write your code here
console.log("Terminal is ready for input.");
const dataTransformer = new Transform({
transform(chunk, encoding, callback) {
this.push(`${chunk.toString().split('').reverse().join('')}\n`);
callback();
}
})
process.stdin.pipe(dataTransformer).pipe(process.stdout);
};

await transform();
13 changes: 12 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import {fileURLToPath} from "url";
import path from "path";
import fs from 'node:fs';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fileName = path.join(__dirname, 'files', 'fileToWrite.txt');

const write = async () => {
// Write your code here
// Write your code here
console.log("Terminal is ready for input.");
let fileStream = fs.createWriteStream(fileName);
process.stdin.pipe(fileStream);
};

await write();
22 changes: 22 additions & 0 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import os from "node:os";
import {Worker, workerData} from "node:worker_threads";
import {fileURLToPath} from "url";
import path from "path";

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

const performCalculations = async () => {
// Write your code here
let cpuCoreCount = os.cpus().length;
let workers = []
for (let i = 0; i < cpuCoreCount; i++) {
workers.push( new Promise((resolve) => {
const worker = new Worker(fileName, {workerData: (10 + i)})
worker.on('message', message => {
resolve({status: 'resolved', data: message});
})
worker.on('error', err => resolve({status: 'error', data: null}));
}));
}
let values = await Promise.all(workers);
console.log(values);
};

await performCalculations();
5 changes: 5 additions & 0 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import worker 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 = () => {
// This code is for simulation of Error behaviour
// let rand = Math.random()
// if(rand > 0.5) throw new Error('');
// This function sends result of nthFibonacci computations to main thread
worker.parentPort.postMessage(nthFibonacci(worker.workerData));
};

sendResult();
15 changes: 14 additions & 1 deletion src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import {fileURLToPath} from "url";
import path from "path";
import zlib from "zlib";
import fs from "node:fs";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fileName = path.join(__dirname, 'files', 'fileToCompress.txt');
const destName = path.join(__dirname, 'files', 'archive.gz');

const compress = async () => {
// Write your code here
// Write your code here
let fileStream = fs.createReadStream(fileName);
let destStream = fs.createWriteStream(destName);
fileStream.pipe(zlib.createGzip()).pipe(destStream).on("finish", () => console.log("File compressed."));
};

await compress();
Loading