diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000000..bf33159a50 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,17 @@ +{ + "name": "node-nodejs-basics", + "version": "1.0.0", + "lockfileVersion": 2, + "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/cli/args.js b/src/cli/args.js index 9e3622f791..6cf800da71 100644 --- a/src/cli/args.js +++ b/src/cli/args.js @@ -1,5 +1,10 @@ const parseArgs = () => { - // Write your code here + const args = Object.values(process.argv).slice(2); + const strArgs = args + .map((arg, index) => index % 2 === 0 ? `${arg} is ${args[index + 1]}`: null) + .filter(Boolean) + .join(', ') + console.log(strArgs) }; parseArgs(); diff --git a/src/cli/env.js b/src/cli/env.js index e3616dc8e7..a443c4c9b5 100644 --- a/src/cli/env.js +++ b/src/cli/env.js @@ -1,5 +1,10 @@ const parseEnv = () => { - // Write your code here + const envVariables = Object.entries(process.env) + const rssVariables = envVariables + .filter(([key, _]) => key.includes('RSS_')) + .map(([key, value]) => `${key}=${value}`); + const strRssVariables = rssVariables.join('; ') + console.log(strRssVariables); }; parseEnv(); diff --git a/src/fs/copy.js b/src/fs/copy.js index e226075b4c..47f5fd4174 100644 --- a/src/fs/copy.js +++ b/src/fs/copy.js @@ -1,5 +1,18 @@ +import { copyFile, mkdir, readdir } from 'node:fs/promises'; +import { join } from 'node:path'; + const copy = async () => { - // Write your code here + try { + const originalFolderPath = await join(import.meta.dirname, './files'); + const copyFolderPath = await join(import.meta.dirname, './files_copy'); + await mkdir(copyFolderPath); + const files = await readdir(originalFolderPath); + for ( const file of files ) { + copyFile(join(originalFolderPath, file), join(copyFolderPath, file)); + } + } catch { + throw new Error('FS operation failed'); + } }; await copy(); diff --git a/src/fs/create.js b/src/fs/create.js index 6ede285599..3177bc02a8 100644 --- a/src/fs/create.js +++ b/src/fs/create.js @@ -1,5 +1,13 @@ -const create = async () => { - // Write your code here -}; + import * as fs from 'node:fs/promises'; + import{ join } from 'node:path'; -await create(); + const create = async () => { + try { + const filePath = await join(import.meta.dirname, './files/fresh.txt'); + await fs.writeFile(filePath, 'I am fresh and young'); + } catch { + throw new Error('FS operation failed'); + } + }; + + await create(); diff --git a/src/fs/delete.js b/src/fs/delete.js index a70b13766c..f9dc5677f9 100644 --- a/src/fs/delete.js +++ b/src/fs/delete.js @@ -1,5 +1,13 @@ +import { rm } from 'node:fs/promises'; +import { join } from 'node:path' + const remove = async () => { - // Write your code here + try { + const path = await join(import.meta.dirname, './files/fileToRemove.txt'); + await rm(path); + } catch { + throw new Error('FS operation failed'); + } }; await remove(); diff --git a/src/fs/list.js b/src/fs/list.js index 0c0fa21f7e..fd88cf236a 100644 --- a/src/fs/list.js +++ b/src/fs/list.js @@ -1,5 +1,14 @@ +import { readdir } from 'node:fs/promises'; +import { join } from 'node:path' + const list = async () => { - // Write your code here + try { + const files = await readdir(join(import.meta.dirname, './files')); + console.log(files); + } catch { + throw new Error('FS operation failed'); + } + }; await list(); diff --git a/src/fs/read.js b/src/fs/read.js index e3938be563..1bc9ca65d9 100644 --- a/src/fs/read.js +++ b/src/fs/read.js @@ -1,5 +1,10 @@ +import { join } from 'node:path' +import { readFile } from 'node:fs/promises'; + const read = async () => { - // Write your code here + const path = await join(import.meta.dirname, './files/fileToRead.txt'); + const fileContent = await readFile(path, { encoding: 'utf-8' }); + console.log(fileContent); }; await read(); diff --git a/src/fs/rename.js b/src/fs/rename.js index b1d65b0c86..10e46e9cf6 100644 --- a/src/fs/rename.js +++ b/src/fs/rename.js @@ -1,5 +1,16 @@ +import * as fs from 'node:fs/promises'; +import { join } from 'node:path'; + const rename = async () => { - // Write your code here + try { + const path = import.meta.dirname + const oldNamePath = await join(import.meta.dirname, './files/wrongFilename.txt'); + const newNamePath = await join(import.meta.dirname, './files/properFilename.md'); + await fs.rename(oldNamePath, newNamePath); + } catch { + throw new Error('FS operation failed'); + } + }; await rename(); diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js index e37c17ed62..8e9ef575df 100644 --- a/src/hash/calcHash.js +++ b/src/hash/calcHash.js @@ -1,5 +1,14 @@ +import { createHash } from 'node:crypto'; +import { createReadStream } from 'node:fs'; +import { join } from 'node:path'; + const calculateHash = async () => { - // Write your code here + const hash = createHash('sha256'); + const read = createReadStream(join(import.meta.dirname, './files/fileToCalculateHashFor.txt'), { encoding: 'utf-8' }); + read.on('data', (chunk) => hash.update(chunk)); + read.on('end', () => { + console.log(hash.digest('hex')) + }); }; await calculateHash(); 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..136fa689c1 --- /dev/null +++ b/src/modules/esm.mjs @@ -0,0 +1,32 @@ +import * as path from 'node:path'; +import { release, version } from 'node:os'; +import { createServer as createServerHttp } from 'node:http'; + +import './files/c.cjs'; + + +const random = Math.random(); + +const { default: unknownObject} = random > 0.5 ? await import('./files/a.json', { with: {type: 'json' } }) : await import('./files/b.json', { with: { 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 ${import.meta.filename}`); +console.log(`Path to current directory is ${import.meta.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'); +}); + +export { myServer, unknownObject } diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt index e69de29bb2..8b13789179 100644 --- a/src/streams/files/fileToWrite.txt +++ b/src/streams/files/fileToWrite.txt @@ -0,0 +1 @@ + diff --git a/src/streams/read.js b/src/streams/read.js index e3938be563..455148e142 100644 --- a/src/streams/read.js +++ b/src/streams/read.js @@ -1,5 +1,11 @@ +import { createReadStream } from 'fs'; +import { stdout } from 'process'; + const read = async () => { - // Write your code here + const read = createReadStream('./src/streams/files/fileToRead.txt', { encoding: 'utf-8' }); + read.on('data', (chunk) => { + stdout.write(chunk); + }) }; await read(); diff --git a/src/streams/transform.js b/src/streams/transform.js index 9e6c15fe84..58f1bd92a1 100644 --- a/src/streams/transform.js +++ b/src/streams/transform.js @@ -1,5 +1,12 @@ +import { Transform } from 'stream'; const transform = async () => { - // Write your code here + const reverseData = new Transform({ + transform(chunk, encoding, callback) { + const reversedChunk = chunk.toString().split('').reverse().join(''); + callback(null, reversedChunk); + } + }) + process.stdin.pipe(reverseData).pipe(process.stdout); }; await transform(); diff --git a/src/streams/write.js b/src/streams/write.js index 84aa11e7cb..d4a3903ab7 100644 --- a/src/streams/write.js +++ b/src/streams/write.js @@ -1,5 +1,10 @@ +import { createWriteStream } from 'fs'; +import { stdin } from 'process'; +import { join } from 'path'; + const write = async () => { - // Write your code here + const output = createWriteStream(join(import.meta.dirname, './files/fileToWrite.txt')); + stdin.pipe(output); }; await write(); diff --git a/src/wt/main.js b/src/wt/main.js index e2ef054d41..32e02a6bf7 100644 --- a/src/wt/main.js +++ b/src/wt/main.js @@ -2,4 +2,4 @@ const performCalculations = async () => { // Write your code here }; -await performCalculations(); +await performCalculations(); \ No newline at end of file diff --git a/src/zip/compress.js b/src/zip/compress.js index d55209587e..b6a9689488 100644 --- a/src/zip/compress.js +++ b/src/zip/compress.js @@ -1,5 +1,15 @@ +import { + createReadStream, + createWriteStream, +} from 'node:fs'; +import { createGzip } from 'node:zlib'; +import { join } from 'path'; + const compress = async () => { - // Write your code here + const gzip = createGzip(); + const source = createReadStream(join(import.meta.dirname, './files/fileToCompress.txt')); + const destination = createWriteStream(join(import.meta.dirname, './files/archive.gz')); + source.pipe(gzip).pipe(destination); }; await compress(); diff --git a/src/zip/decompress.js b/src/zip/decompress.js index 8aaf26c8a4..0649647320 100644 --- a/src/zip/decompress.js +++ b/src/zip/decompress.js @@ -1,5 +1,15 @@ +import { + createReadStream, + createWriteStream, +} from 'node:fs'; +import { createGunzip } from 'node:zlib'; +import { join } from 'path'; + const decompress = async () => { - // Write your code here + const gunzip = createGunzip(); + const destination = createWriteStream(join(import.meta.dirname, './files/fileToCompress.txt')); + const source = createReadStream(join(import.meta.dirname, './files/archive.gz')); + source.pipe(gunzip).pipe(destination); }; await decompress();