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
16 changes: 16 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: 11 additions & 0 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const parseArgs = () => {
// Write your code here

const args = process.argv.slice(2);
const result = [];
args.forEach((arg, index) => {
if(arg.startsWith('--')) {
// console.log(`${arg.slice(2)}: ${args[index + 1]}`);
result.push(`${arg.slice(2)}: ${args[index + 1]}`);
}
});

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

parseArgs();
7 changes: 7 additions & 0 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const parseEnv = () => {
// Write your code here
// console.log(process.env);

for(const key in process.env) {
if (key.startsWith('RSS_')) {
console.log(`${key}=${process.env[key]};`);
}
}
};

parseEnv();
11 changes: 11 additions & 0 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import fs from 'fs/promises';
import path from 'path';
import url from 'url';



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

};

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



const create = async () => {
// Write your code here
// Write your code here
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const file = path.join(__dirname, 'files', 'fresh.txt');;
const dataToWrite = 'I am fresh and young';

try {
await fs.access(file);

throw new Error('FS operation failed');
} catch (error) {
if (error.code === 'ENOENT') {
await fs.writeFile(file, dataToWrite);
} else {
throw error;
}
}
};

await create();
1 change: 1 addition & 0 deletions src/fs/files/fresh.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am fresh and young
19 changes: 19 additions & 0 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { createHash } from 'crypto';
import fs from 'fs';
import path from 'path';
import url from 'url';

const calculateHash = async () => {
// Write your code here
const __filename = url.fileURLToPath(import.meta.url);
const file = path.join(path.dirname(__filename), 'files', 'fileToCalculateHashFor.txt');
const fileReadStream = fs.createReadStream(file);

const hash = createHash('sha256');

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

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

await calculateHash();
18 changes: 9 additions & 9 deletions src/modules/cjsToEsm.cjs → src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
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;

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()}`);
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}`);
console.log(`Path to current file is ${import.meta.url}`);
console.log(`Path to current directory is ${import.meta.url}`);

const myServer = createServerHttp((_, res) => {
res.end('Request accepted');
Expand All @@ -33,7 +33,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});

module.exports = {
export {
unknownObject,
myServer,
};
Expand Down
1 change: 1 addition & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, STREAM!
14 changes: 14 additions & 0 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import fs from 'fs';
import path from 'path';
import url from 'url';

const read = async () => {
// Write your code here
// console.log(process.cwd());
const __filename = url.fileURLToPath(import.meta.url);
const file = path.join(path.dirname(__filename), 'files', 'fileToRead.txt');

const readStream = fs.createReadStream(file, 'utf-8');
readStream.pipe(process.stdout);

readStream.on('error', (error) => {
console.error('Error while reading file:', error);
});
};

await read();
13 changes: 13 additions & 0 deletions src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import fs from 'fs';
import { Transform } from 'stream';

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

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

await transform();
21 changes: 21 additions & 0 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import fs from 'fs';
import path from 'path';
import url from 'url';

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

const writeStream = fs.createWriteStream(file);

process.stdin.once('data', (userInput) => {
writeStream.write(userInput, () => {
console.log('Your input was succesfully written');

writeStream.end();
process.exit();
});
});

writeStream.on('error', (error) => {
console.error('Error while writing to file:', error);
});
};

await write();
24 changes: 24 additions & 0 deletions src/zip/compress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import fs from 'fs';
import path from 'path';
import url from 'url';
import zlib from 'zlib';

const compress = async () => {
// Write your code here
const __filename = url.fileURLToPath(import.meta.url);
const file = path.join(path.dirname(__filename), 'files', 'fileToCompress.txt');
const output = path.join(path.dirname(__filename), 'files', 'archive.gz');

const gzip = zlib.createGzip();
const readStream = fs.createReadStream(file);
const writeStream = fs.createWriteStream(output);

writeStream.on('finish', () => {
console.log('Compressed.');
});
readStream.on('error', (err) => {
console.error('Error while reading file:', err);
});
writeStream.on('error', (err) => {
console.error('Error while writing compressed file:', err);
});

readStream.pipe(gzip).pipe(writeStream);
};

await compress();
24 changes: 24 additions & 0 deletions src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import fs from 'fs';
import path from 'path';
import url from 'url';
import zlib from 'zlib';

const decompress = async () => {
// Write your code here
const __filename = url.fileURLToPath(import.meta.url);
const file = path.join(path.dirname(__filename), 'files', 'archive.gz');
const output = path.join(path.dirname(__filename), 'files', 'fileToCompress.txt');

const gzip = zlib.createGunzip();
const readStream = fs.createReadStream(file);
const writeStream = fs.createWriteStream(output);

writeStream.on('finish', () => {
console.log('Decompressed.');
});
readStream.on('error', (err) => {
console.error('Error while reading file:', err);
});
writeStream.on('error', (err) => {
console.error('Error while writing decompressed file:', err);
});

readStream.pipe(gzip).pipe(writeStream);
};

await decompress();
Binary file added src/zip/files/archive.gz
Binary file not shown.