From b8aabb632364ec6c77e827900fe2b9e2511e0599 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 26 Oct 2025 22:31:19 +0300 Subject: [PATCH 01/11] feat: implement fs/create function --- src/fs/create.js | 21 ++++++++++++++++++--- src/fs/files/fresh.txt | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 src/fs/files/fresh.txt diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..27c6932391 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,20 @@ +import { promises as fs } from 'fs' +import path from 'path' + const create = async () => { - // Write your code here -}; + const folderPath = path.join(process.cwd(), 'src', 'fs', 'files') + const filePath = path.join(folderPath, 'fresh.txt') + + const fileExists = await fs + .access(filePath) + .then(() => true) + .catch(() => false) + + if (fileExists) { + throw new Error('FS operation failed') + } else { + await fs.writeFile(filePath, 'I am fresh and young') + } +} -await create(); +await create() diff --git a/src/fs/files/fresh.txt b/src/fs/files/fresh.txt new file mode 100644 index 0000000000..205d704cb7 --- /dev/null +++ b/src/fs/files/fresh.txt @@ -0,0 +1 @@ +I am fresh and young \ No newline at end of file From 15d05bc6dde4f9259ce69c79f4d22ad8b63c25f2 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 26 Oct 2025 23:10:37 +0300 Subject: [PATCH 02/11] feat: implement fs/copy function --- src/fs/copy.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..492fd14816 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,28 @@ +import { promises as fs } from 'fs' +import path from 'path' + const copy = async () => { - // Write your code here -}; + const srcFolderPath = path.join(process.cwd(), 'src', 'fs', 'files') + const destFolderPath = path.join(process.cwd(), 'src', 'fs', 'files_copy') + + const isSrcFolderPathExists = await fs + .access(srcFolderPath) + .then(() => true) + .catch(() => false) + + if (!isSrcFolderPathExists) { + throw new Error('FS operation failed') + } else { + await fs + .cp(srcFolderPath, destFolderPath, { + recursive: true, + errorOnExist: true, + force: false, + }) + .catch(() => { + throw new Error('FS operation failed') + }) + } +} -await copy(); +await copy() From 450baffe0bb2c60c8e1ef8a13cda3f894b4b503e Mon Sep 17 00:00:00 2001 From: user Date: Sun, 26 Oct 2025 23:27:44 +0300 Subject: [PATCH 03/11] feat: implement fs/rename function --- .../{wrongFilename.txt => properFilename.md} | 0 src/fs/rename.js | 23 ++++++++++++++++--- src/fs/utils.js | 8 +++++++ 3 files changed, 28 insertions(+), 3 deletions(-) rename src/fs/files/{wrongFilename.txt => properFilename.md} (100%) create mode 100644 src/fs/utils.js diff --git a/src/fs/files/wrongFilename.txt b/src/fs/files/properFilename.md similarity index 100% rename from src/fs/files/wrongFilename.txt rename to src/fs/files/properFilename.md diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..117667d0e0 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,22 @@ +import { promises as fs } from 'fs' +import path from 'path' + +import { isExists } from './utils.js' + const rename = async () => { - // Write your code here -}; + const oldFileName = path.resolve('src', 'fs', 'files', 'wrongFilename.txt') + const newFileName = path.resolve('src', 'fs', 'files', 'properFilename.md') + + const isOldFileNameExists = await isExists(oldFileName) + const isNewFileNameExists = await isExists(newFileName) + + if (!isOldFileNameExists || isNewFileNameExists) { + throw new Error('FS operation failed') + } else { + await fs.rename(oldFileName, newFileName).catch(() => { + throw new Error('FS operation failed') + }) + } +} -await rename(); +await rename() diff --git a/src/fs/utils.js b/src/fs/utils.js new file mode 100644 index 0000000000..d1f0704198 --- /dev/null +++ b/src/fs/utils.js @@ -0,0 +1,8 @@ +import { promises as fs } from 'fs' + +export async function isExists(folderPath) { + return await fs + .access(folderPath) + .then(() => true) + .catch(() => false) +} From ff388b39dd201f318cc64bbaa84d88f71a2390b8 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 27 Oct 2025 07:14:27 +0300 Subject: [PATCH 04/11] refactor: add function isExists to create and copy fn --- src/fs/copy.js | 6 ++---- src/fs/create.js | 6 ++---- src/fs/files_copy/dontLookAtMe.txt | 1 + src/fs/files_copy/fileToRead.txt | 7 +++++++ src/fs/files_copy/fileToRemove.txt | 1 + src/fs/files_copy/fresh.txt | 1 + src/fs/files_copy/hello.txt | 1 + src/fs/files_copy/properFilename.md | 3 +++ 8 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 src/fs/files_copy/dontLookAtMe.txt create mode 100644 src/fs/files_copy/fileToRead.txt create mode 100644 src/fs/files_copy/fileToRemove.txt create mode 100644 src/fs/files_copy/fresh.txt create mode 100644 src/fs/files_copy/hello.txt create mode 100644 src/fs/files_copy/properFilename.md diff --git a/src/fs/copy.js b/src/fs/copy.js index 492fd14816..3786dbe5c5 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,14 +1,12 @@ import { promises as fs } from 'fs' import path from 'path' +import { isExists } from './utils.js' const copy = async () => { const srcFolderPath = path.join(process.cwd(), 'src', 'fs', 'files') const destFolderPath = path.join(process.cwd(), 'src', 'fs', 'files_copy') - const isSrcFolderPathExists = await fs - .access(srcFolderPath) - .then(() => true) - .catch(() => false) + const isSrcFolderPathExists = isExists(srcFolderPath) if (!isSrcFolderPathExists) { throw new Error('FS operation failed') diff --git a/src/fs/create.js b/src/fs/create.js index 27c6932391..c58d4a096e 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,14 +1,12 @@ import { promises as fs } from 'fs' import path from 'path' +import { isExists } from './utils.js' const create = async () => { const folderPath = path.join(process.cwd(), 'src', 'fs', 'files') const filePath = path.join(folderPath, 'fresh.txt') - const fileExists = await fs - .access(filePath) - .then(() => true) - .catch(() => false) + const fileExists = isExists(filePath) if (fileExists) { throw new Error('FS operation failed') diff --git a/src/fs/files_copy/dontLookAtMe.txt b/src/fs/files_copy/dontLookAtMe.txt new file mode 100644 index 0000000000..8979bab743 --- /dev/null +++ b/src/fs/files_copy/dontLookAtMe.txt @@ -0,0 +1 @@ +What are you looking at?! \ No newline at end of file diff --git a/src/fs/files_copy/fileToRead.txt b/src/fs/files_copy/fileToRead.txt new file mode 100644 index 0000000000..5d66c332d6 --- /dev/null +++ b/src/fs/files_copy/fileToRead.txt @@ -0,0 +1,7 @@ +My content +should +be +printed +into +console +! \ No newline at end of file diff --git a/src/fs/files_copy/fileToRemove.txt b/src/fs/files_copy/fileToRemove.txt new file mode 100644 index 0000000000..43e64cd45c --- /dev/null +++ b/src/fs/files_copy/fileToRemove.txt @@ -0,0 +1 @@ +How dare you! \ No newline at end of file diff --git a/src/fs/files_copy/fresh.txt b/src/fs/files_copy/fresh.txt new file mode 100644 index 0000000000..205d704cb7 --- /dev/null +++ b/src/fs/files_copy/fresh.txt @@ -0,0 +1 @@ +I am fresh and young \ No newline at end of file diff --git a/src/fs/files_copy/hello.txt b/src/fs/files_copy/hello.txt new file mode 100644 index 0000000000..4e65f7775f --- /dev/null +++ b/src/fs/files_copy/hello.txt @@ -0,0 +1 @@ +Hello Node.js \ No newline at end of file diff --git a/src/fs/files_copy/properFilename.md b/src/fs/files_copy/properFilename.md new file mode 100644 index 0000000000..38cca5db19 --- /dev/null +++ b/src/fs/files_copy/properFilename.md @@ -0,0 +1,3 @@ +# This is a file with a wrong filename + +Hello from **markdown**! \ No newline at end of file From cfe9fd8cd5ad54a8892cf8eff5f11aae537099bd Mon Sep 17 00:00:00 2001 From: user Date: Mon, 27 Oct 2025 07:38:12 +0300 Subject: [PATCH 05/11] feat: implement functions - fs/delete fs/list fs/read --- src/fs/delete.js | 20 +++++++++++++++++--- src/fs/list.js | 19 ++++++++++++++++--- src/fs/read.js | 18 +++++++++++++++--- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..6b8a3c896b 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,19 @@ +import { promises as fs } from 'fs' +import path from 'path' + +import { isExists } from './utils.js' + const remove = async () => { - // Write your code here -}; + const filePath = path.resolve('src', 'fs', 'files', 'fileToRemove.txt') + const isFileExists = await isExists(filePath) + + if (isFileExists) { + await fs.rm(filePath).catch(() => { + throw new Error('FS operation failed') + }) + } else { + throw new Error('FS operation failed') + } +} -await remove(); +await remove() diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..06cc9c5fcb 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,18 @@ +import { promises as fs } from 'fs' +import path from 'path' + +import { isExists } from './utils.js' + const list = async () => { - // Write your code here -}; + const folderPath = path.resolve('src', 'fs', 'files') + const isFolderPath = isExists(folderPath) + + if (isFolderPath) { + const listFiles = await fs.readdir(folderPath) + console.log(listFiles) + } else { + throw new Error('FS operation failed') + } +} -await list(); +await list() diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..374dee81a3 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,17 @@ +import { promises as fs } from 'fs' +import path from 'path' + +import { isExists } from './utils.js' + const read = async () => { - // Write your code here -}; + const filePath = path.resolve('src', 'fs', 'files', 'fileToRead.txt') + const isFileExists = await isExists(filePath) + if (isFileExists) { + const fileContent = await fs.readFile(filePath, 'utf-8') + console.log(fileContent) + } else { + throw new Error('FS operation failed') + } +} -await read(); +await read() From 1ab90140f12b6dc12ce6214513f0c8805502d548 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 27 Oct 2025 10:23:33 +0300 Subject: [PATCH 06/11] feat: implement cli/args and cli/env --- src/cli/args.js | 17 ++++++++++++++--- src/cli/env.js | 22 +++++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/cli/args.js b/src/cli/args.js index 9e3622f791..bb79ddab43 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,16 @@ +const PREFIX = '--' + const parseArgs = () => { - // Write your code here -}; + const formatArguments = process.argv.reduce((acc, value, index, array) => { + if (value.startsWith(PREFIX)) { + const argument = `${value.replace(PREFIX, '')} is ${array[index + 1]}` + return [...acc, argument] + } + return acc + }, []) + + const strFormatArguments = formatArguments.join(', ') + console.log(strFormatArguments) +} -parseArgs(); +parseArgs() diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..a11d310339 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,21 @@ +const PREFIX = 'RSS_' + const parseEnv = () => { - // Write your code here -}; + const envVariables = Object.entries(process.env).reduce( + (acc, value, index, array) => { + const [nameVar, valueVar] = value + + if (nameVar.startsWith(PREFIX)) { + const variable = `${nameVar}=${valueVar}` + return [...acc, variable] + } + return acc + }, + [] + ) + + const strFormatVariables = envVariables.join(', ') + console.log(strFormatVariables) +} -parseEnv(); +parseEnv() From 62efa4bddf294d0816fbe9cabc82fab1cb3dff6f Mon Sep 17 00:00:00 2001 From: user Date: Mon, 27 Oct 2025 12:25:59 +0300 Subject: [PATCH 07/11] refactor: convert cjsToEsm.cjs to ECMASript module esm.mjs --- src/modules/cjsToEsm.cjs | 34 ---------------------------------- src/modules/esm.mjs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 34 deletions(-) delete mode 100644 src/modules/cjsToEsm.cjs create mode 100644 src/modules/esm.mjs diff --git a/src/modules/cjsToEsm.cjs b/src/modules/cjsToEsm.cjs deleted file mode 100644 index 089bd2db13..0000000000 --- a/src/modules/cjsToEsm.cjs +++ /dev/null @@ -1,34 +0,0 @@ -const path = require('node:path'); -const { release, version } = require('node:os'); -const { createServer: createServerHttp } = require('node:http'); - -require('./files/c.cjs'); - -const random = Math.random(); - -const unknownObject = random > 0.5 ? require('./files/a.json') : require('./files/b.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; - -console.log(unknownObject); - -myServer.listen(PORT, () => { - console.log(`Server is listening on port ${PORT}`); - console.log('To terminate it, use Ctrl+C combination'); -}); - -module.exports = { - unknownObject, - myServer, -}; diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs new file mode 100644 index 0000000000..1308f8f1f3 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,36 @@ +import path from 'path' +import { release, version } from 'node:os' +import { createServer } from 'node:http' +import { fileURLToPath } from 'node:url' + +import './files/c.cjs' + +const random = Math.random() + +const unknownObject = await (random > 0.5 + ? import('./files/a.json', { with: { type: 'json' } }) + : import('./files/b.json', { with: { type: 'json' } })) + +console.log(`Release ${release()}`) +console.log(`Version ${version()}`) +console.log(`Path segment separator is "${path.sep}"`) + +const fileName = fileURLToPath(import.meta.url) +const dirName = path.dirname(fileName) +console.log(`Path to current file is ${fileName}`) +console.log(`Path to current directory is ${dirName}`) + +const myServer = createServer((_, res) => { + res.end('Request accepted') +}) + +const PORT = 3000 + +console.log(unknownObject) + +myServer.listen(PORT, () => { + console.log(`Server is listening on port ${PORT}`) + console.log('To terminate it, use Ctrl+C combination') +}) + +export { unknownObject, myServer } From 5c6e59e53ebc7238f67df0d5d2d07c1a3d6a841c Mon Sep 17 00:00:00 2001 From: user Date: Mon, 27 Oct 2025 13:23:19 +0300 Subject: [PATCH 08/11] feat: implement hash/calcHash --- src/hash/calcHash.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..60b27d98d7 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,18 @@ +import { promises as fs } from 'fs' +import { createHash } from 'crypto' +import path from 'path' + const calculateHash = async () => { - // Write your code here -}; + const filePath = path.resolve( + 'src', + 'hash', + 'files', + 'fileToCalculateHashFor.txt' + ) + + const textFromFile = await fs.readFile(filePath) + const hash = createHash('sha256').update(textFromFile) + console.log(hash.digest('hex')) +} -await calculateHash(); +await calculateHash() From 0e12ba236ad444eb043daf475952bcee96f974c9 Mon Sep 17 00:00:00 2001 From: user Date: Tue, 28 Oct 2025 21:09:45 +0300 Subject: [PATCH 09/11] feat: implement streams --- src/cli/args.js | 20 ++++++++++--------- src/fs/copy.js | 30 ++++++++++++++--------------- src/fs/create.js | 17 +++++++--------- src/fs/delete.js | 16 ++++++--------- src/fs/files/fileToRemove.txt | 1 - src/fs/files_copy/dontLookAtMe.txt | 1 - src/fs/files_copy/fileToRead.txt | 7 ------- src/fs/files_copy/fileToRemove.txt | 1 - src/fs/files_copy/fresh.txt | 1 - src/fs/files_copy/hello.txt | 1 - src/fs/files_copy/properFilename.md | 3 --- src/fs/list.js | 14 ++++++-------- src/fs/read.js | 9 ++++----- src/fs/rename.js | 20 ++++++++----------- src/hash/calcHash.js | 16 +++++++-------- src/modules/esm.mjs | 4 ++-- src/streams/files/fileToWrite.txt | 12 ++++++++++++ src/streams/read.js | 12 +++++++++--- src/streams/transform.js | 28 ++++++++++++++++++++++++--- src/streams/write.js | 13 ++++++++++--- src/fs/utils.js => utils.js | 2 +- 21 files changed, 122 insertions(+), 106 deletions(-) delete mode 100644 src/fs/files_copy/dontLookAtMe.txt delete mode 100644 src/fs/files_copy/fileToRead.txt delete mode 100644 src/fs/files_copy/fileToRemove.txt delete mode 100644 src/fs/files_copy/fresh.txt delete mode 100644 src/fs/files_copy/hello.txt delete mode 100644 src/fs/files_copy/properFilename.md rename src/fs/utils.js => utils.js (71%) diff --git a/src/cli/args.js b/src/cli/args.js index bb79ddab43..829a8dca99 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,16 +1,18 @@ const PREFIX = '--' const parseArgs = () => { - const formatArguments = process.argv.reduce((acc, value, index, array) => { - if (value.startsWith(PREFIX)) { - const argument = `${value.replace(PREFIX, '')} is ${array[index + 1]}` - return [...acc, argument] - } - return acc - }, []) + const formatArgumentsArr = process.argv + .slice(2) + .reduce((acc, value, index, array) => { + if (value.startsWith(PREFIX)) { + const argument = `${value.replace(PREFIX, '')} is ${array[index + 1]}` + return [...acc, argument] + } + return acc + }, []) - const strFormatArguments = formatArguments.join(', ') - console.log(strFormatArguments) + const formatArgumentsStr = formatArgumentsArr.join(', ') + console.log(formatArgumentsStr) } parseArgs() diff --git a/src/fs/copy.js b/src/fs/copy.js index 3786dbe5c5..64feab0c9b 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,25 +1,23 @@ -import { promises as fs } from 'fs' +import { cp } from 'fs/promises' import path from 'path' -import { isExists } from './utils.js' +import { isFileExists } from '../../utils.js' -const copy = async () => { - const srcFolderPath = path.join(process.cwd(), 'src', 'fs', 'files') - const destFolderPath = path.join(process.cwd(), 'src', 'fs', 'files_copy') - - const isSrcFolderPathExists = isExists(srcFolderPath) +const srcFolderPath = path.resolve(import.meta.dirname, 'files') +const destFolderPath = path.resolve(import.meta.dirname, 'files_copy') +const isSrcFolderPathExists = await isFileExists(srcFolderPath) +const copy = async () => { if (!isSrcFolderPathExists) { throw new Error('FS operation failed') } else { - await fs - .cp(srcFolderPath, destFolderPath, { - recursive: true, - errorOnExist: true, - force: false, - }) - .catch(() => { - throw new Error('FS operation failed') - }) + await cp(srcFolderPath, destFolderPath, { + recursive: true, + errorOnExist: true, + force: false, + }).catch(error => { + console.log(error) + throw new Error('FS operation failed') + }) } } diff --git a/src/fs/create.js b/src/fs/create.js index c58d4a096e..fe18b09f11 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,17 +1,14 @@ -import { promises as fs } from 'fs' +import { writeFile } from 'fs/promises' import path from 'path' -import { isExists } from './utils.js' -const create = async () => { - const folderPath = path.join(process.cwd(), 'src', 'fs', 'files') - const filePath = path.join(folderPath, 'fresh.txt') - - const fileExists = isExists(filePath) +const fileUrl = path.resolve(import.meta.dirname, 'files/fresh.txt') - if (fileExists) { +const create = async () => { + try { + await writeFile(fileUrl, 'I am fresh and young', { flag: 'wx' }) + } catch (err) { + console.log(err) throw new Error('FS operation failed') - } else { - await fs.writeFile(filePath, 'I am fresh and young') } } diff --git a/src/fs/delete.js b/src/fs/delete.js index 6b8a3c896b..55f5ca86f9 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,17 +1,13 @@ -import { promises as fs } from 'fs' +import { rm } from 'fs/promises' import path from 'path' -import { isExists } from './utils.js' +const filePath = path.resolve(import.meta.dirname, 'files/fileToRemove.txt') const remove = async () => { - const filePath = path.resolve('src', 'fs', 'files', 'fileToRemove.txt') - const isFileExists = await isExists(filePath) - - if (isFileExists) { - await fs.rm(filePath).catch(() => { - throw new Error('FS operation failed') - }) - } else { + try { + await rm(filePath) + } catch (error) { + console.log(error) throw new Error('FS operation failed') } } diff --git a/src/fs/files/fileToRemove.txt b/src/fs/files/fileToRemove.txt index 43e64cd45c..e69de29bb2 100644 --- a/src/fs/files/fileToRemove.txt +++ b/src/fs/files/fileToRemove.txt @@ -1 +0,0 @@ -How dare you! \ No newline at end of file diff --git a/src/fs/files_copy/dontLookAtMe.txt b/src/fs/files_copy/dontLookAtMe.txt deleted file mode 100644 index 8979bab743..0000000000 --- a/src/fs/files_copy/dontLookAtMe.txt +++ /dev/null @@ -1 +0,0 @@ -What are you looking at?! \ No newline at end of file diff --git a/src/fs/files_copy/fileToRead.txt b/src/fs/files_copy/fileToRead.txt deleted file mode 100644 index 5d66c332d6..0000000000 --- a/src/fs/files_copy/fileToRead.txt +++ /dev/null @@ -1,7 +0,0 @@ -My content -should -be -printed -into -console -! \ No newline at end of file diff --git a/src/fs/files_copy/fileToRemove.txt b/src/fs/files_copy/fileToRemove.txt deleted file mode 100644 index 43e64cd45c..0000000000 --- a/src/fs/files_copy/fileToRemove.txt +++ /dev/null @@ -1 +0,0 @@ -How dare you! \ No newline at end of file diff --git a/src/fs/files_copy/fresh.txt b/src/fs/files_copy/fresh.txt deleted file mode 100644 index 205d704cb7..0000000000 --- a/src/fs/files_copy/fresh.txt +++ /dev/null @@ -1 +0,0 @@ -I am fresh and young \ No newline at end of file diff --git a/src/fs/files_copy/hello.txt b/src/fs/files_copy/hello.txt deleted file mode 100644 index 4e65f7775f..0000000000 --- a/src/fs/files_copy/hello.txt +++ /dev/null @@ -1 +0,0 @@ -Hello Node.js \ No newline at end of file diff --git a/src/fs/files_copy/properFilename.md b/src/fs/files_copy/properFilename.md deleted file mode 100644 index 38cca5db19..0000000000 --- a/src/fs/files_copy/properFilename.md +++ /dev/null @@ -1,3 +0,0 @@ -# This is a file with a wrong filename - -Hello from **markdown**! \ No newline at end of file diff --git a/src/fs/list.js b/src/fs/list.js index 06cc9c5fcb..01e373040d 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,16 +1,14 @@ -import { promises as fs } from 'fs' +import { readdir } from 'fs/promises' import path from 'path' -import { isExists } from './utils.js' +const folderPath = path.resolve(import.meta.dirname, 'files') const list = async () => { - const folderPath = path.resolve('src', 'fs', 'files') - const isFolderPath = isExists(folderPath) - - if (isFolderPath) { - const listFiles = await fs.readdir(folderPath) + try { + const listFiles = await readdir(folderPath) console.log(listFiles) - } else { + } catch (error) { + console.log(error) throw new Error('FS operation failed') } } diff --git a/src/fs/read.js b/src/fs/read.js index 374dee81a3..5575debdec 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,15 +1,14 @@ import { promises as fs } from 'fs' import path from 'path' -import { isExists } from './utils.js' +const filePath = path.resolve(import.meta.dirname, 'files/fileToRead.txt') const read = async () => { - const filePath = path.resolve('src', 'fs', 'files', 'fileToRead.txt') - const isFileExists = await isExists(filePath) - if (isFileExists) { + try { const fileContent = await fs.readFile(filePath, 'utf-8') console.log(fileContent) - } else { + } catch (error) { + console.log(error) throw new Error('FS operation failed') } } diff --git a/src/fs/rename.js b/src/fs/rename.js index 117667d0e0..b3b0f59601 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,21 +1,17 @@ -import { promises as fs } from 'fs' +import { rename } from 'fs/promises' import path from 'path' -import { isExists } from './utils.js' +import { isFileExists } from '../../utils.js' -const rename = async () => { - const oldFileName = path.resolve('src', 'fs', 'files', 'wrongFilename.txt') - const newFileName = path.resolve('src', 'fs', 'files', 'properFilename.md') - - const isOldFileNameExists = await isExists(oldFileName) - const isNewFileNameExists = await isExists(newFileName) +const oldFileName = path.resolve(import.meta.dirname, 'wrongFilename.txt') +const newFileName = path.resolve(import.meta.dirname, 'properFilename.md') +const isNewFileNameExists = await isFileExists(newFileName) - if (!isOldFileNameExists || isNewFileNameExists) { +const rename = async () => { + if (isNewFileNameExists) { throw new Error('FS operation failed') } else { - await fs.rename(oldFileName, newFileName).catch(() => { - throw new Error('FS operation failed') - }) + await rename(oldFileName, newFileName) } } diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index 60b27d98d7..e15553fc33 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,16 +1,14 @@ -import { promises as fs } from 'fs' +import { readFile } from 'fs/promises' import { createHash } from 'crypto' import path from 'path' -const calculateHash = async () => { - const filePath = path.resolve( - 'src', - 'hash', - 'files', - 'fileToCalculateHashFor.txt' - ) +const filePath = path.resolve( + import.meta.dirname, + 'files/fileToCalculateHashFor.txt' +) - const textFromFile = await fs.readFile(filePath) +const calculateHash = async () => { + const textFromFile = await readFile(filePath) const hash = createHash('sha256').update(textFromFile) console.log(hash.digest('hex')) } diff --git a/src/modules/esm.mjs b/src/modules/esm.mjs index 1308f8f1f3..9726dda5ee 100644 --- a/src/modules/esm.mjs +++ b/src/modules/esm.mjs @@ -1,6 +1,6 @@ import path from 'path' import { release, version } from 'node:os' -import { createServer } from 'node:http' +import { createServer as createServerHttp } from 'node:http' import { fileURLToPath } from 'node:url' import './files/c.cjs' @@ -20,7 +20,7 @@ const dirName = path.dirname(fileName) console.log(`Path to current file is ${fileName}`) console.log(`Path to current directory is ${dirName}`) -const myServer = createServer((_, res) => { +const myServer = createServerHttp((_, res) => { res.end('Request accepted') }) diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt index e69de29bb2..e659235805 100644 --- a/src/streams/files/fileToWrite.txt +++ b/src/streams/files/fileToWrite.txt @@ -0,0 +1,12 @@ +rfgdfgdfgfd +gdf +gfd +gfd +g +fdg +fd +g +fdg + + + diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..45b70c60d5 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,11 @@ +import { createReadStream } from 'fs' +import path from 'path' + +const filePath = path.resolve(import.meta.dirname, 'files/fileToRead.txt') + const read = async () => { - // Write your code here -}; + const readStream = createReadStream(filePath, 'utf8') + readStream.pipe(process.stdout) +} -await read(); +await read() diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..e3d53627f8 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,27 @@ +import { Transform } from 'stream' + const transform = async () => { - // Write your code here -}; + const upperCaseTransform = new Transform({ + transform(chunk, encoding, callback) { + callback(null, chunk.toString().toUpperCase()) + }, + }) + + process.stdin.pipe(upperCaseTransform).pipe(process.stdout) +} + +await transform() + +// class UpperCaseTransform extends Transform { +// _transform(chunk, encoding, callback) { +// callback(null, chunk.toString().toUpperCase()) +// } +// } + +// const transform = async () => { +// const transformUpperStream = new UpperCaseTransform() + +// process.stdin.pipe(transformUpperStream).pipe(process.stdout) +// } -await transform(); +// await transform() diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..3bd892bbf3 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,12 @@ +import { createWriteStream } from 'fs' +import path from 'path' + +const filePath = path.resolve(import.meta.dirname, 'files/fileToWrite.txt') + const write = async () => { - // Write your code here -}; + const writeStream = createWriteStream(filePath, 'utf8', { flags: 'a' }) + process.stdin.setEncoding('utf8') + process.stdin.pipe(writeStream) +} -await write(); +await write() diff --git a/src/fs/utils.js b/utils.js similarity index 71% rename from src/fs/utils.js rename to utils.js index d1f0704198..85dacbbfe3 100644 --- a/src/fs/utils.js +++ b/utils.js @@ -1,6 +1,6 @@ import { promises as fs } from 'fs' -export async function isExists(folderPath) { +export async function isFileExists(folderPath) { return await fs .access(folderPath) .then(() => true) From ed99498bf21c17d7735f0500ea51bd6bdc5b2c75 Mon Sep 17 00:00:00 2001 From: user Date: Thu, 30 Oct 2025 18:37:31 +0300 Subject: [PATCH 10/11] feat: implement zlib and worker threads functions --- .vscode/settings.json | 3 +++ node_modules/.package-lock.json | 7 +++++++ package-lock.json | 17 +++++++++++++++ src/cp/cp.js | 14 +++++++++---- src/cp/files/script.js | 26 +++++++++++------------ src/wt/main.js | 36 +++++++++++++++++++++++++++++--- src/wt/worker.js | 11 ++++++---- src/zip/compress.js | 19 ++++++++++++++--- src/zip/decompress.js | 32 +++++++++++++++++++++++++--- src/zip/files/archive.gz | Bin 0 -> 32 bytes 10 files changed, 135 insertions(+), 30 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 node_modules/.package-lock.json create mode 100644 package-lock.json create mode 100644 src/zip/files/archive.gz diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..27c2b2d5fe --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cSpell.words": ["asar"] +} diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json new file mode 100644 index 0000000000..b9684068e3 --- /dev/null +++ b/node_modules/.package-lock.json @@ -0,0 +1,7 @@ +{ + "name": "node-nodejs-basics", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": {} +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000000..34b7366dcf --- /dev/null +++ b/package-lock.json @@ -0,0 +1,17 @@ +{ + "name": "node-nodejs-basics", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "node-nodejs-basics", + "version": "1.0.0", + "license": "ISC", + "engines": { + "node": ">=24.10.0", + "npm": ">=10.9.2" + } + } + } +} diff --git a/src/cp/cp.js b/src/cp/cp.js index 72c6addc9c..d2d425c203 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -1,6 +1,12 @@ -const spawnChildProcess = async (args) => { - // Write your code here -}; +import path from 'node:path' +import { fork } from 'node:child_process' + +const pathFileFork = path.resolve(import.meta.dirname, 'files/script.js') +const childFork = fork(pathFileFork) + +const spawnChildProcess = async args => { + // Write your code here +} // Put your arguments in function call to test this functionality -spawnChildProcess( /* [someArgument1, someArgument2, ...] */); +spawnChildProcess(/* [someArgument1, someArgument2, ...] */) diff --git a/src/cp/files/script.js b/src/cp/files/script.js index 0c6654f12f..8280ed7067 100644 --- a/src/cp/files/script.js +++ b/src/cp/files/script.js @@ -1,19 +1,19 @@ -import { EOL } from 'node:os'; -import { argv, stdout, stdin, exit } from 'node:process'; +import { EOL } from 'node:os' +import { argv, stdout, stdin, exit } from 'node:process' -const args = argv.slice(2); +const args = argv.slice(2) -console.log(`Total number of arguments is ${args.length}`); -console.log(`Arguments: ${JSON.stringify(args)}${EOL}`); +console.log(`Total number of arguments is ${args.length}`) +console.log(`Arguments: ${JSON.stringify(args)}${EOL}`) -const echoInput = (chunk) => { - const chunkStringified = chunk.toString(); +const echoInput = chunk => { + const chunkStringified = chunk.toString() - if (chunkStringified.includes('CLOSE')) { - exit(0); - } + if (chunkStringified.includes('CLOSE')) { + exit(0) + } - stdout.write(`Received from master process: ${chunk.toString()}${EOL}`); -}; + stdout.write(`Received from master process: ${chunk.toString()}${EOL}`) +} -stdin.on('data', echoInput); +stdin.on('data', echoInput) diff --git a/src/wt/main.js b/src/wt/main.js index e2ef054d41..861ed9baa3 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -1,5 +1,35 @@ +import path from 'path' +import { cpus } from 'os' +import { Worker } from 'worker_threads' + +const workerPath = path.resolve(import.meta.dirname, 'worker.js') +const START_INDEX = 10 + const performCalculations = async () => { - // Write your code here -}; + const countCPU = cpus().length + const workerPromises = [] + + for (let i = 0; i < countCPU; i++) { + const workerData = START_INDEX + i + + const workerPromise = new Promise(resolve => { + const worker = new Worker(workerPath, { workerData }) + + worker.on('message', data => { + resolve({ status: 'resolved', data }) + worker.terminate() + }) + + worker.on('error', error => { + resolve({ status: 'resolved', data: null }) + }) + }) + + workerPromises.push(workerPromise) + } + + const result = await Promise.all(workerPromises) + console.log(result) +} -await performCalculations(); +await performCalculations() diff --git a/src/wt/worker.js b/src/wt/worker.js index 405595394d..bdc6956949 100644 --- a/src/wt/worker.js +++ b/src/wt/worker.js @@ -1,8 +1,11 @@ +import { parentPort, workerData } from 'worker_threads' + // n should be received from main thread -const nthFibonacci = (n) => n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2); +const nthFibonacci = n => + n < 2 ? n : nthFibonacci(n - 1) + nthFibonacci(n - 2) const sendResult = () => { - // This function sends result of nthFibonacci computations to main thread -}; + parentPort.postMessage(nthFibonacci(workerData)) +} -sendResult(); +sendResult() diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..041908d5c7 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,18 @@ +import path from 'path' +import zlib from 'zlib' +import { createReadStream, createWriteStream } from 'fs' + +const filePathInput = path.resolve( + import.meta.dirname, + 'files/fileToCompress.txt' +) +const filePathOutput = path.resolve(import.meta.dirname, 'files/archive.gz') + const compress = async () => { - // Write your code here -}; + const readStream = createReadStream(filePathInput) + const writeStream = createWriteStream(filePathOutput) + + readStream.pipe(zlib.createGzip()).pipe(writeStream) +} -await compress(); +await compress() diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..1125f699d7 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,31 @@ +import path from 'path' +import zlib from 'zlib' +import { createReadStream, createWriteStream } from 'fs' +import { pipeline } from 'stream' + +const archivePathInput = path.resolve(import.meta.dirname, 'files/archive.gz') +const archivePathOutput = path.resolve( + import.meta.dirname, + 'files/fileToCompress.txt' +) + const decompress = async () => { - // Write your code here -}; + // const readStream = createReadStream(archivePathInput) + // const writeStream = createWriteStream(archivePathOutput) + // readStream.pipe(zlib.createGunzip()).pipe(writeStream) + + pipeline( + createReadStream(archivePathInput), + zlib.createGunzip(), + createWriteStream(archivePathOutput), + err => { + if (err) { + console.error('Ошибка при распаковке:', err.message) + } else { + console.log('Извлечение завершено успешно') + } + } + ) +} -await decompress(); +await decompress() diff --git a/src/zip/files/archive.gz b/src/zip/files/archive.gz new file mode 100644 index 0000000000000000000000000000000000000000..87e36da4f0b95f5488a28cc58c6c716430c823a8 GIT binary patch literal 32 jcmb2|=3oE==Hhebbv3+n^} Date: Thu, 30 Oct 2025 20:56:49 +0300 Subject: [PATCH 11/11] feat: implement cp function --- src/cp/cp.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cp/cp.js b/src/cp/cp.js index d2d425c203..ceabe102d2 100644 --- a/src/cp/cp.js +++ b/src/cp/cp.js @@ -2,11 +2,9 @@ import path from 'node:path' import { fork } from 'node:child_process' const pathFileFork = path.resolve(import.meta.dirname, 'files/script.js') -const childFork = fork(pathFileFork) const spawnChildProcess = async args => { - // Write your code here + fork(pathFileFork, args) } -// Put your arguments in function call to test this functionality -spawnChildProcess(/* [someArgument1, someArgument2, ...] */) +spawnChildProcess(['someArgument1', 'someArgument2'])