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
12 changes: 11 additions & 1 deletion src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import process from 'node:process';

const parseArgs = () => {
// Write your code here
// Write your code here
const [environmentPath, filepath, ...userArgv] = process.argv
let parts = []

for (let i = 0; i < userArgv.length; i += 2) {
parts.push(`${userArgv[i].slice(2)} is ${userArgv[i + 1]}`);
}

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

parseArgs();
8 changes: 8 additions & 0 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import proccess from 'node:process'

const parseEnv = () => {
// Write your code here
const filteredEnvRecords = Object
.entries(process.env)
.filter(([key, value]) => key.startsWith('RSS'))
.map(([key, value]) => `${key}=${value}`).join('; ')

console.log(filteredEnvRecords)
};

parseEnv();
4 changes: 4 additions & 0 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { fork } from "node:child_process";
import { resolve } from "node:path";

const spawnChildProcess = async (args) => {
// Write your code here
const child = fork(resolve(import.meta.dirname, 'files', 'script.js'), args)
};

spawnChildProcess();
20 changes: 18 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { cp, access } from 'fs/promises';
import { resolve } from 'node:path'

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

const pathFrom = resolve(import.meta.dirname, 'files')
const pathTo = resolve(import.meta.dirname, 'files_copy')

const isFromDirectoryExists = await access(pathFrom).then(() => true).catch(() => false);

if (!isFromDirectoryExists) throw new Error('FS operation failed')

try {
await cp(pathFrom, pathTo, { errorOnExist: true, recursive: true, force: false })
} catch(e) {
throw new Error('FS operation failed')
}
};

copy();
await copy();
16 changes: 16 additions & 0 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { resolve } from 'node:path';
import { access, writeFile } from 'fs/promises';

const create = async () => {
// Write your code here
const destinationFilename = resolve(import.meta.dirname, 'files', 'fresh.txt')

let fileExists = await access(destinationFilename).then(() => true).catch(() => false);

if (fileExists) {
throw new Error('FS operation failed')
}

try {
await writeFile(destinationFilename, 'I am fresh and young', { encoding: 'utf-8', flag: 'wx' })
} catch(e) {
throw new Error('FS operation failed')
}
};

await create();
11 changes: 11 additions & 0 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { rm } from 'fs/promises';
import { resolve } from 'node:path';

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

const pathToRemove = resolve(import.meta.dirname, 'files', 'fileToRemove.txt')

try {
await rm(pathToRemove)
} catch {
throw new Error('FS operation failed')
}
};

await remove();
11 changes: 11 additions & 0 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { resolve } from 'node:path';
import { readdir } from 'fs/promises';

const list = async () => {
// Write your code here
const sourcePath = resolve(import.meta.dirname, 'files')

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

await list();
11 changes: 11 additions & 0 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { readFile } from 'fs/promises';
import { resolve } from 'node:path';

const read = async () => {
// Write your code here
const sourcePath = resolve(import.meta.dirname, 'files', 'fileToRead.txt')

try {
const data = await readFile(sourcePath, { encoding: 'utf-8' })
console.log(data)
} catch(e) {
throw new Error('FS operation failed')
}
};

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

const rename = async () => {
// Write your code here
// Write your code here
const pathFrom = resolve(import.meta.dirname, 'files', 'wrongFilename.txt')
const pathTo = resolve(import.meta.dirname, 'files', 'properFilename.md')

const pathFromExists = await fs.access(pathFrom).then(() => true).catch(() => false);

if (!pathFromExists) throw new Error('FS operation failed')

const pathToExists = await fs.access(pathTo).then(() => true).catch(() => false);

if (pathToExists) throw new Error('FS operation failed')

try {
await fs.rename(pathFrom, pathTo)
} catch(e) {
throw new Error('FS operation failed')
}
};

await rename();
10 changes: 10 additions & 0 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { resolve } from 'node:path';
import { createReadStream } from 'node:fs';
import { createHash } from 'node:crypto';
import { stdout } from 'node:process';

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

const hash = createHash('sha256')
const streamData = createReadStream(resolve(import.meta.dirname, 'files', 'fileToCalculateHashFor.txt'))

streamData.pipe(hash).setEncoding('hex').pipe(stdout)
};

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

const require = Module.createRequire(import.meta.url)
import './files/c.js'

const random = Math.random();

Expand All @@ -17,8 +20,8 @@ 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.filename}`);
console.log(`Path to current directory is ${import.meta.dirname}`);

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

module.exports = {
export default {
unknownObject,
myServer,
};
Expand Down
7 changes: 7 additions & 0 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { stdout } from 'node:process';
import { resolve } from 'node:path';
import { createReadStream } from 'node:fs';

const read = async () => {
// Write your code here
const stream = createReadStream(resolve(import.meta.dirname, 'files', 'fileToRead.txt'))

stream.pipe(stdout)
};

await read();
17 changes: 16 additions & 1 deletion src/streams/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { stdin, stdout } from 'node:process';
import { Transform } from 'node:stream';
import { pipeline } from "node:stream/promises";


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

try {
await pipeline(stdin, transform, stdout)
} catch(e) {
console.error(e)
}
};

await transform();
7 changes: 7 additions & 0 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { createWriteStream } from 'node:fs'
import { resolve } from 'node:path';
import { stdin } from 'node:process';

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

const stream = createWriteStream(resolve(import.meta.dirname, 'files', 'fileToWrite.txt'))
stdin.pipe(stream);
};

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 { cpus } from 'node:os'
import { Worker } from 'node:worker_threads';
import path from 'node:path';

const performCalculations = async () => {
// Write your code here
const threadCount = cpus().length;
let increment = 10;
const promises = []

for (let i = 0; i < threadCount; i++) {
const promise = new Promise((resolve) => {
const worker = new Worker(path.resolve(import.meta.dirname, 'worker.js'), { workerData: increment++ })

worker.on('message', (data) => resolve({ status: 'resolved', data }))
worker.on('error', () => resolve({ status: 'error', data: null }))
})

promises.push(promise)
}

const result = await Promise.all(promises)

console.log(result);
};

await performCalculations();
7 changes: 7 additions & 0 deletions src/wt/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { workerData, parentPort } 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 function sends result of nthFibonacci computations to main thread
const result = nthFibonacci(workerData)
parentPort.postMessage(result)



};

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

const compress = async () => {
// Write your code here
const input = createReadStream(resolve(import.meta.dirname, 'files', 'fileToCompress.txt'))
const output = createWriteStream(resolve(import.meta.dirname, 'files', 'archive.gz'))
const gzip = createGzip();

try {
await pipeline(input, gzip, output)
} catch(e) {
console.error(e);
}

};

await compress();
14 changes: 14 additions & 0 deletions src/zip/decompress.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { createReadStream, createWriteStream } from 'node:fs'
import { resolve } from 'node:path';
import { createUnzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';

const decompress = async () => {
// Write your code here
const input = createReadStream(resolve(import.meta.dirname, 'files', 'archive.gz'))
const output = createWriteStream(resolve(import.meta.dirname, 'files', 'fileToCompress.txt'))
const unzip = createUnzip();

try {
await pipeline(input, unzip, output)
} catch(e) {
console.error(e)
}
};

await decompress();