Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f430371
feat: add solution for create.js
LinderJK Oct 1, 2024
63e0705
feat: add solution for copy.js
LinderJK Oct 1, 2024
6f9da4c
feat: add solution for rename
LinderJK Oct 3, 2024
1efe470
feat: add solution for delete
LinderJK Oct 3, 2024
334a79d
feat: add solution for list
LinderJK Oct 3, 2024
e4df185
feat: add solution for read
LinderJK Oct 3, 2024
0abbe27
feat: add solution for cli:env
LinderJK Oct 3, 2024
7ea7987
feat: add solution for cli:args
LinderJK Oct 3, 2024
f6d490a
feat: change imports in esm.mjs
LinderJK Oct 3, 2024
102363f
feat: change import json, change export functions
LinderJK Oct 3, 2024
1e450c8
feat: add dirname and filename for esm
LinderJK Oct 3, 2024
0c31871
refactor: delete comments
LinderJK Oct 3, 2024
656d8ad
refactor: change paths to be used pathlib
LinderJK Oct 3, 2024
6f426f1
refactor: change use pipe instead of manual streaming
LinderJK Oct 7, 2024
2e4866e
feat: add tastl "steams read" solution
LinderJK Oct 7, 2024
b78138c
feat: add solution for stream write
LinderJK Oct 7, 2024
3ad3699
feat: add solution for stream tranform
LinderJK Oct 7, 2024
0df65fd
feat: add zlib tasks solution
LinderJK Oct 7, 2024
a5a2534
refactor: delete remove files after compress and decompress
LinderJK Oct 7, 2024
d81c05b
feat: add worker
LinderJK Oct 7, 2024
e2cfe51
feat: add imports
LinderJK Oct 7, 2024
63d1f92
feat: add start n value and os cpu number
LinderJK Oct 7, 2024
5f7b44e
feat: add create workers based on cpu count
LinderJK Oct 7, 2024
4d2014d
refactor: delete typo symbol
LinderJK Oct 7, 2024
94024d7
feat: add imports, create child process with fork
LinderJK Oct 7, 2024
16ea68f
feat: add child stdout send data to main stdout
LinderJK Oct 7, 2024
0ce4ac8
feat: add main process pipe to child process
LinderJK Oct 7, 2024
42635cf
feat: add children error message and exit
LinderJK Oct 7, 2024
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
9 changes: 7 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const parseArgs = () => {
// Write your code here
const args = process.argv.slice(2);
for (let i = 0; i < args.length; i+=2) {
const name = args[i].replace('--', '');
const value = args[i+1];
console.log(`${name} is ${value}`);
}
};

parseArgs();
parseArgs();
12 changes: 10 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const parseEnv = () => {
// Write your code here
const data = Object.entries(process.env)
.filter(([key])=> key.includes('RSS_'))
.map(([key, value])=> `${key}=${value}`)

if (data.length > 0) {
console.log(data.join('\n'));
} else {
console.log('No environment variables found');
}
};

parseEnv();
parseEnv();
30 changes: 28 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import {fork} from 'child_process';
import path from "path";

const __dirname = import.meta.dirname;
const filePath = path.join(__dirname, 'files', 'script.js');

const spawnChildProcess = async (args) => {
// Write your code here
try {
const child = fork(filePath, args, { stdio: ['pipe', 'pipe', 'pipe', 'ipc'] });

child.stdout.on('data', (data) => {
process.stdout.write(`From child: ${data.toString()}\n`);
})

process.stdin.pipe(child.stdin);

child.on('exit', (code) => {
if (code !== 0) {
process.exit(code);
}
});
child.on('error', (err) => {
console.error(err);
});

} catch (err) {
console.error(err);
}
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess( ['--some-arg', 'value1', '--other', '1337', '--arg2', '42'] );
32 changes: 31 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import fs from 'fs/promises';
import path from "path";

const copy = async () => {
// Write your code here
const __dirname = import.meta.dirname;
const filesPath = path.join(__dirname, 'files');
const filesCopyPath = path.join(__dirname, 'files_copy');
try {
await fs.access(filesPath)
await fs.access(filesCopyPath)

throw new Error('FS operation failed');

} catch (err) {
if (err.code === 'ENOENT') {
if (err.path === filesPath) {
throw new Error('FS operation failed');
}
if (err.path === filesCopyPath) {
await fs.mkdir(filesCopyPath);
}
} else {
console.error(err);
return;
}
}

try {
await fs.cp(filesPath, filesCopyPath, {recursive: true});
} catch (err) {
console.error(err);
}
};

await copy();
19 changes: 17 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from 'fs/promises';
import path from 'path';

const create = async () => {
// Write your code here
const __dirname = import.meta.dirname;
const freshFilePath = path.join(__dirname, 'files', 'fresh.txt');
const filesPath = path.join(__dirname, 'files');
try {
const files = await fs.readdir(filesPath);
for (const file of files) {
if (file === 'fresh.txt') throw new Error('FS operation failed');
}
await fs.writeFile(freshFilePath, 'I am fresh and young');
console.log('File created successfully');
} catch (err) {
console.error(err);
}
};

await create();
await create();
17 changes: 15 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from "fs/promises";
import path from "path";

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

try {
await fs.access(filePath);
await fs.rm(filePath);
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');
}
}
};

await remove();
await remove();
18 changes: 16 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from "fs/promises";
import path from "path";

const list = async () => {
// Write your code here
const __dirname = import.meta.dirname;
const filesPath = path.join(__dirname, 'files');
try {
await fs.access(filesPath);
const files = await fs.readdir(filesPath);
console.log(files);
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');
}
console.error(err);
}
};

await list();
await list();
18 changes: 16 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from "fs/promises";
import path from "path";

const read = async () => {
// Write your code here
const __dirname = import.meta.dirname;
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');
try {
await fs.access(filePath);
const data = await fs.readFile(filePath, 'utf8');
console.log(data);
} catch (err) {
if (err.code === 'ENOENT') {
throw new Error('FS operation failed');
}
console.error(err);
}
};

await read();
await read();
33 changes: 31 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import fs from "fs/promises";
import path from "path";

const rename = async () => {
// Write your code here
const __dirname = import.meta.dirname;
const wrongFilenamePath = path.join(__dirname, 'files', 'wrongFilename.txt');
const properFilenamePath = path.join(__dirname, 'files', 'wrongFilename.txt');

try {
await fs.access(wrongFilenamePath)
await fs.access(properFilenamePath)

throw new Error('FS operation failed');

} catch (err) {
if (err.code === 'ENOENT') {
if (err.path === wrongFilenamePath) {
throw new Error('FS operation failed');
}
if (err.path === properFilenamePath) {
try {
await fs.rename(wrongFilenamePath, properFilenamePath);
console.log('File renamed successfully!');
} catch (renameErr) {
console.error('Error during renaming:', renameErr);
}
}
} else {
console.error(err);
}
}
};

await rename();
await rename();
24 changes: 22 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { createHash } from 'crypto';
import { createReadStream } from 'fs';
import path from 'path';
const calculateHash = async () => {
// Write your code here
try {
const __dirname = import.meta.dirname;
const filePath = path.join(__dirname, 'files', 'fileToCalculateHashFor.txt');
if (!filePath) {
throw new Error('FS operation failed');
}
const hash = createHash('sha256');
const stream = createReadStream(filePath);

stream.pipe(hash);
hash.on('finish', () => {
console.log(hash.read().toString('hex'));
});


} catch (err) {
console.error(err);
}
};

await calculateHash();
await calculateHash();
24 changes: 10 additions & 14 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const path = require('path');
const { release, version } = require('os');
const { createServer: createServerHttp } = require('http');
require('./files/c');
import path from 'path';
import {release, version} from 'os';
import {createServer as createServerHttp} from 'http';
import './files/c.js'

const random = Math.random();

let unknownObject;
const __dirname = import.meta.dirname;
const __filename = import.meta.filename;
export let unknownObject;

if (random > 0.5) {
unknownObject = require('./files/a.json');
unknownObject = await import('./files/a.json', {assert: {type: 'json'}});
} else {
unknownObject = require('./files/b.json');
unknownObject = await import('./files/b.json', {assert: {type: 'json'}});
}

console.log(`Release ${release()}`);
Expand All @@ -20,7 +21,7 @@ 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) => {
export const myServer = createServerHttp((_, res) => {
res.end('Request accepted');
});

Expand All @@ -33,8 +34,3 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
unknownObject,
myServer,
};

19 changes: 18 additions & 1 deletion src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { createReadStream } from 'fs';
import path from 'path';
const read = async () => {
// Write your code here
const __dirname = import.meta.dirname;
const filePath = path.join(__dirname, "files", "fileToRead.txt");
try {
const stream = createReadStream(filePath, {encoding: 'utf8'});
stream.pipe(process.stdout);
stream.on("end", () => {
console.log("\n");
});

stream.on("error", (err) => {
console.error("Error reading file", err);
});

} catch (err) {
console.error(err);
}
};

await read();
18 changes: 16 additions & 2 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { Transform } from 'stream';
const transform = async () => {
// Write your code here
try {
const transformStream = new Transform({
transform(chunk, _, callback) {
const reversed = chunk.toString().split('').reverse().join('') + '\n';
callback(null, reversed);
}
});
process.stdin.pipe(transformStream).pipe(process.stdout);
transformStream.on('error', (err) => {
console.error(err);
})
} catch (err) {
console.error(err);
}
};

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

const write = async () => {
// Write your code here
const __dirname = import.meta.dirname;
const filePath = path.join(__dirname, 'files', 'fileToWrite.txt');
try{
const stream = createWriteStream(filePath , {encoding: 'utf8'});
process.stdin.pipe(stream);
stream.on("open", () => {
console.log("Use CTRL + D to stop writing to file");
});
stream.on("error", (err) => {
console.error("Error writing file", err);
})
stream.on("finish", () => {
console.log("\n File written successfully");
});

} catch (err) {
console.error(err);
}
};

await write();
Loading