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.

17 changes: 14 additions & 3 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import process from 'node:process'

const parseArgs = () => {
// Write your code here
};
for (let idx = 2; idx < process.argv.length; idx++) {
const el = process.argv[idx]
if (idx === process.argv.length - 1) {
process.stdout.write(`${el}\n`)
} else if (idx % 2) {
process.stdout.write(`${el}, `)
} else {
process.stdout.write(`${el} is `)
}
}
}

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

const parseEnv = () => {
// Write your code here
};
console.log(Object.keys(process.env).filter(el => /^RSS_/.test(el)).join('; '))
}

parseEnv();
parseEnv()
18 changes: 14 additions & 4 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import cp from 'node:child_process'
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'

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

const spawnChildProcess = async (args) => {
// Write your code here
};
const child = cp.fork(path.join(__dirname, 'files', 'script.js'), args, {
stdio: ['pipe', 'pipe', 'inherit', 'ipc']
})
process.stdin.pipe(child.stdin)
child.stdout.pipe(process.stdout)
}

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['asd', 1, { 'zxc': 'zxc' }])
28 changes: 25 additions & 3 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import fs from 'node:fs/promises'
import getDirname from './dirname.js'
import path from 'node:path'

const copy = async () => {
// Write your code here
};
const newFilesPath = path.join(getDirname(), 'files_copy')
const oldFilesPath = path.join(getDirname(), 'files')
try {
const pathStat = await fs.stat(newFilesPath).catch(() => {})
if (pathStat && pathStat.isDirectory()) {
throw Error('FS operation failed')
}
await fs.mkdir(newFilesPath, { recursive: false })
const files = await fs.readdir(oldFilesPath, { withFileTypes: true })
for (const file of files) {
await fs.copyFile(path.join(oldFilesPath, file.name), path.join(newFilesPath, file.name))
}
} catch (error) {
if (error.code === 'ENOENT') {
throw Error('FS operation failed')
} else {
throw error
}
}
}

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

const create = async () => {
// Write your code here
};
const filePath = path.join(getDirname(), 'files', 'fresh.txt')
try {
await fs.access(filePath, fs.constants.R_OK | fs.constants.W_OK)
throw Error('FS operation failed')
} catch (error) {
if (error.code === 'ENOENT') {
await fs.writeFile(filePath, 'I am fresh and young')
} else {
throw error
}
}
}

await create();
await create()
15 changes: 12 additions & 3 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from 'node:fs/promises'
import getDirname from './dirname.js'
import path from 'node:path'

const remove = async () => {
// Write your code here
};
const filePath = path.join(getDirname(), 'files', 'fileToRemove.txt')
try {
await fs.unlink(filePath)
} catch (e) {
throw Error('FS operation failed')
}
}

await remove();
await remove()
7 changes: 7 additions & 0 deletions src/fs/dirname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import path from 'node:path'
import { fileURLToPath } from 'node:url'

export default function getDirname(caller = import.meta.url) {
return path.dirname(fileURLToPath(caller))
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# This is a file with a wrong filename

# This is a file with a wrong filename
Hello from **markdown**!
26 changes: 23 additions & 3 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import fs from 'node:fs/promises'
import getDirname from './dirname.js'
import path from 'node:path'

const list = async () => {
// Write your code here
};
const filesPath = path.join(getDirname(), 'files')
try {
const pathStat = await fs.stat(filesPath).catch(() => {})
if (!pathStat || !pathStat.isDirectory()) {
throw Error('FS operation failed')
}
const files = await fs.readdir(filesPath, { withFileTypes: true })
for (const file of files) {
console.log(file.name)
}
} catch (error) {
if (error.code === 'ENOENT') {
throw Error('FS operation failed')
} else {
throw error
}
}
}

await list();
await list()
14 changes: 12 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fs from 'node:fs/promises'
import getDirname from './dirname.js'
import path from 'node:path'

const read = async () => {
// Write your code here
try {
const filePath = path.join(getDirname(), 'files', 'fileToRead.txt')
const contents = await fs.readFile(filePath, { encoding: 'utf8' });
console.log(contents);
} catch (err) {
throw Error('FS operation failed')
}
};

await read();
await read();
23 changes: 20 additions & 3 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import fs from 'node:fs/promises'
import getDirname from './dirname.js'
import path from 'node:path'

const rename = async () => {
// Write your code here
};
const oldFilePath = path.join(getDirname(), 'files', 'wrongFilename.txt')
const newFilePath = path.join(getDirname(), 'files', 'properFilename.md')
try {
await fs.access(newFilePath, fs.constants.R_OK | fs.constants.W_OK)
throw Error('FS operation failed')
} catch (error) {
if (error.code === 'ENOENT') {
await fs.rename(oldFilePath, newFilePath).catch(() => {
throw Error('FS operation failed')
})
} else {
throw error
}
}
}

await rename();
await rename()
21 changes: 18 additions & 3 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import fs from 'node:fs'
import getDirname from '../fs/dirname.js'
import path from 'node:path'
import crypto from 'node:crypto'

const calculateHash = async () => {
// Write your code here
};
try {
const filePath = path.join(getDirname(import.meta.url), 'files', 'fileToCalculateHashFor.txt')
const stream = fs.createReadStream(filePath)
const hash = crypto.createHash('sha256')

stream.on('data', chunk => hash.update(chunk))
stream.on('end', () => console.log(hash.digest('hex')))
stream.on('error', err => console.error(err))
} catch (err) {
throw Error('FS operation failed')
}
}

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

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

const random = Math.random();

let unknownObject;

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

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

module.exports = {
unknownObject,
myServer,
};

export { myServer, unknownObject };
1 change: 0 additions & 1 deletion src/streams/files/fileToRead.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
This file should be read using Streams API
2 changes: 2 additions & 0 deletions src/streams/files/fileToWrite.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
asd
qwe
14 changes: 11 additions & 3 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import fs from 'node:fs'
import getDirname from '../fs/dirname.js'
import path from 'node:path'
import { EOL } from 'node:os'

const read = async () => {
// Write your code here
};
const filePath = path.join(getDirname(import.meta.url), 'files', 'fileToRead.txt')
const stream = fs.createReadStream(filePath)
stream.pipe(process.stdout)
stream.on('end', () => process.stdout.write(EOL))
}

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

const transform = async () => {
// Write your code here
const transformer = new Transform({
transform(chunk, encoding, callback) {
const newChunk = chunk.toString().split('').reverse().join('')
callback(null, newChunk)
}
})
process.stdin.pipe(transformer).pipe(process.stdout)
};

await transform();
await transform();
12 changes: 9 additions & 3 deletions src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import fs from 'node:fs'
import getDirname from '../fs/dirname.js'
import path from 'node:path'

const write = async () => {
// Write your code here
};
const filePath = path.join(getDirname(import.meta.url), 'files', 'fileToWrite.txt')
const stream = fs.createWriteStream(filePath)
process.stdin.pipe(stream)
}

await write();
await write()
26 changes: 23 additions & 3 deletions src/wt/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import { cpus } from 'node:os'
import { Worker } from 'node:worker_threads'
import path from 'node:path'
import getDirname from '../fs/dirname.js'

const performCalculations = async () => {
// Write your code here
};
const howMany = cpus().length
const res = await Promise.allSettled([...Array(howMany)].map((el, idx) => {
return new Promise((resolve) => {
const worker = new Worker(path.join(getDirname(import.meta.url), 'worker.js'))
worker.postMessage(10 + idx)
worker.on('message', (data) => {
worker.terminate()
resolve({ status: 'resolved', data})
})
worker.on('error', () => {
worker.terminate()
resolve({ status: 'resolved', data: null})
})
})
}))
console.log(res)
}

await performCalculations();
await performCalculations()
Loading