diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml
new file mode 100644
index 0000000000..d23208fbb7
--- /dev/null
+++ b/.idea/jsLibraryMappings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000000..35eb1ddfbb
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000000..91d8cea176
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,155 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {
+ "associatedIndex": 2
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1728136826558
+
+
+ 1728136826558
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/package-lock.json b/package-lock.json
new file mode 100644
index 0000000000..ff3865a27f
--- /dev/null
+++ b/package-lock.json
@@ -0,0 +1,16 @@
+{
+ "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": ">=20.0.0"
+ }
+ }
+ }
+}
diff --git a/src/cli/args.js b/src/cli/args.js
index 8283f7f7aa..a398aa58e1 100644
--- a/src/cli/args.js
+++ b/src/cli/args.js
@@ -1,5 +1,12 @@
+import process from "node:process";
+
const parseArgs = () => {
- // Write your code here
+ const args = process.argv.slice(2);
+ args.forEach((arg, idx) => {
+ if (arg.startsWith('--')) {
+ console.log(`${arg} is ${args[idx + 1]}`);
+ }
+ })
};
-parseArgs();
\ No newline at end of file
+parseArgs();
diff --git a/src/cli/env.js b/src/cli/env.js
index fe4aa4a8df..e9bf5744bd 100644
--- a/src/cli/env.js
+++ b/src/cli/env.js
@@ -1,5 +1,10 @@
+import process from "node:process";
+
const parseEnv = () => {
- // Write your code here
+ const keys = Object.keys(process.env).filter((key) => key.startsWith("RSS_"));
+ keys.forEach((key) => {
+ console.log(`${key}=${process.env[key]}`);
+ })
};
-parseEnv();
\ No newline at end of file
+parseEnv();
diff --git a/src/fs/copy.js b/src/fs/copy.js
index bd17fe3991..57ec2d64b2 100644
--- a/src/fs/copy.js
+++ b/src/fs/copy.js
@@ -1,5 +1,16 @@
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import {useFSVariables} from "./utils.js";
+
const copy = async () => {
- // Write your code here
+ try {
+ const { __dirname} = useFSVariables();
+ const sourcePath = path.resolve(__dirname, 'files');
+ const destPath = path.resolve(__dirname, 'files_copy');
+ await fs.cp(sourcePath, destPath, { recursive: true, errorOnExist: true, force: false });
+ } catch {
+ throw new Error('FS operation failed');
+ }
};
await copy();
diff --git a/src/fs/create.js b/src/fs/create.js
index 8d18cf9fc2..2664a33c65 100644
--- a/src/fs/create.js
+++ b/src/fs/create.js
@@ -1,5 +1,15 @@
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+import {useFSVariables} from "./utils.js";
+
const create = async () => {
- // Write your code here
+ try {
+ const { __dirname} = useFSVariables();
+ await fs.writeFile(path.resolve(__dirname, 'files', 'fresh.txt'), 'I am fresh and young', { flag: 'wx' });
+ } catch {
+ throw new Error('FS operation failed');
+ }
};
-await create();
\ No newline at end of file
+await create();
diff --git a/src/fs/delete.js b/src/fs/delete.js
index 4718dbc4c5..4b9c96ef01 100644
--- a/src/fs/delete.js
+++ b/src/fs/delete.js
@@ -1,5 +1,15 @@
+import {useFSVariables} from "./utils.js";
+import fs from "node:fs/promises";
+import path from "node:path";
+
const remove = async () => {
- // Write your code here
+ try {
+ const { __dirname } = useFSVariables();
+ const filePath = path.resolve(__dirname, 'files', 'fileToRemove.txt');
+ await fs.unlink(filePath);
+ } catch {
+ throw new Error('FS operation failed');
+ }
};
-await remove();
\ No newline at end of file
+await remove();
diff --git a/src/fs/list.js b/src/fs/list.js
index c0a83dea15..22726da833 100644
--- a/src/fs/list.js
+++ b/src/fs/list.js
@@ -1,5 +1,17 @@
+import {useFSVariables} from "./utils.js";
+import fs from "node:fs/promises";
+import path from "node:path";
+
const list = async () => {
- // Write your code here
+ try {
+ const { __dirname } = useFSVariables();
+ const dirPath = path.resolve(__dirname, 'files');
+ const files = await fs.readdir(dirPath, { withFileTypes: true, recursive: true });
+ console.log(files.map(({ name }) => name))
+ } catch {
+ throw new Error('FS operation failed');
+ }
+
};
-await list();
\ No newline at end of file
+await list();
diff --git a/src/fs/read.js b/src/fs/read.js
index 52c78cc6ee..533a87e992 100644
--- a/src/fs/read.js
+++ b/src/fs/read.js
@@ -1,5 +1,16 @@
+import {useFSVariables} from "./utils.js";
+import fs from "node:fs/promises";
+import path from "node:path";
+
const read = async () => {
- // Write your code here
+ try {
+ const { __dirname } = useFSVariables();
+ const filePath = path.resolve(__dirname, 'files', 'fileToRead.txt');
+ const fileData = await fs.readFile(filePath, { encoding: 'utf8' });
+ console.log(fileData);
+ } catch {
+ throw new Error('FS operation failed');
+ }
};
-await read();
\ No newline at end of file
+await read();
diff --git a/src/fs/rename.js b/src/fs/rename.js
index 2bb99ecdb5..e10c519fc8 100644
--- a/src/fs/rename.js
+++ b/src/fs/rename.js
@@ -1,5 +1,16 @@
+import fs from 'node:fs/promises';
+import path from 'node:path';
+import {useFSVariables} from "./utils.js";
+
const rename = async () => {
- // Write your code here
+ try {
+ const { __dirname } = useFSVariables();
+ const oldPath = path.resolve(__dirname, 'files', 'wrongFilename.txt');
+ const newPath = path.resolve(__dirname, 'files', 'properFilename.md');
+ await fs.rename(oldPath, newPath);
+ } catch {
+ throw new Error('FS operation failed');
+ }
};
-await rename();
\ No newline at end of file
+await rename();
diff --git a/src/fs/utils.js b/src/fs/utils.js
new file mode 100644
index 0000000000..725e889d9c
--- /dev/null
+++ b/src/fs/utils.js
@@ -0,0 +1,8 @@
+import {fileURLToPath} from "node:url";
+import path from "node:path";
+
+export const useFSVariables = () => {
+ const __filename = fileURLToPath(import.meta.url);
+ const __dirname = path.dirname(__filename);
+ return { __filename, __dirname };
+}
diff --git a/src/hash/calcHash.js b/src/hash/calcHash.js
index 450f8f72e2..0c82262b67 100644
--- a/src/hash/calcHash.js
+++ b/src/hash/calcHash.js
@@ -1,5 +1,18 @@
+import { createHash } from 'node:crypto';
+import {fileURLToPath} from "node:url";
+import path from "node:path";
+import {createReadStream} from "node:fs";
+
const calculateHash = async () => {
- // Write your code here
+ const __filename = fileURLToPath(import.meta.url);
+ const __dirname = path.dirname(__filename);
+ const filePath = path.resolve(__dirname, 'files', 'fileToCalculateHashFor.txt');
+ const readStream = createReadStream(filePath);
+ const hash = createHash('sha256');
+
+ readStream.pipe(hash).on('finish', () => {
+ console.log(hash.digest("hex"));
+ })
};
-await calculateHash();
\ No newline at end of file
+await calculateHash();
diff --git a/src/modules/cjsToEsm.cjs b/src/modules/esm.mjs
similarity index 58%
rename from src/modules/cjsToEsm.cjs
rename to src/modules/esm.mjs
index 8b7be2a48b..41813455dd 100644
--- a/src/modules/cjsToEsm.cjs
+++ b/src/modules/esm.mjs
@@ -1,22 +1,26 @@
-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 './files/c.js'
+import {fileURLToPath} from "node:url";
const random = Math.random();
let unknownObject;
if (random > 0.5) {
- unknownObject = require('./files/a.json');
+ unknownObject = await import('./files/a.json', { with: { type: 'json'}});
} else {
- unknownObject = require('./files/b.json');
+ unknownObject = await 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}`);
@@ -33,7 +37,7 @@ myServer.listen(PORT, () => {
console.log('To terminate it, use Ctrl+C combination');
});
-module.exports = {
+export {
unknownObject,
myServer,
};
diff --git a/src/streams/files/fileToWrite.txt b/src/streams/files/fileToWrite.txt
index e69de29bb2..2a65cfb9fa 100644
--- a/src/streams/files/fileToWrite.txt
+++ b/src/streams/files/fileToWrite.txt
@@ -0,0 +1,5 @@
+fjieifeisefiseifisef
+
+
+sfedeesjkfesse
+
diff --git a/src/streams/read.js b/src/streams/read.js
index 52c78cc6ee..f94598d6e3 100644
--- a/src/streams/read.js
+++ b/src/streams/read.js
@@ -1,5 +1,15 @@
+import { pipeline } from 'node:stream/promises'
+import { stdout } from "node:process";
+import {createReadStream} from 'node:fs';
+import path from "node:path";
+import {fileURLToPath} from "node:url";
+
const read = async () => {
- // Write your code here
+ const __filename = fileURLToPath(import.meta.url);
+ const __dirname = path.dirname(__filename);
+ const filePath = path.resolve(__dirname, 'files', 'fileToRead.txt');
+ const readStream = createReadStream(filePath);
+ await pipeline(readStream, stdout);
};
-await read();
\ No newline at end of file
+await read();
diff --git a/src/streams/transform.js b/src/streams/transform.js
index 315fc6597f..ee2cfcf6d6 100644
--- a/src/streams/transform.js
+++ b/src/streams/transform.js
@@ -1,5 +1,15 @@
+import { Transform } from "node:stream";
+import {pipeline} from "node:stream/promises";
+import {stdin, stdout} from "node:process";
+
const transform = async () => {
- // Write your code here
+ const reverseTransform = new Transform({
+ transform(chunk, encoding, callback) {
+ callback(null, `${chunk.toString().split('').reverse().join('')}\n`);
+ },
+ });
+
+ await pipeline(stdin, reverseTransform, stdout);
};
-await transform();
\ No newline at end of file
+await transform();
diff --git a/src/streams/write.js b/src/streams/write.js
index fc917160a2..06b5b6cbaf 100644
--- a/src/streams/write.js
+++ b/src/streams/write.js
@@ -1,5 +1,15 @@
+import {fileURLToPath} from "node:url";
+import path from "node:path";
+import {createWriteStream} from "node:fs";
+import {pipeline} from "node:stream/promises";
+import {stdin} from "node:process";
+
const write = async () => {
- // Write your code here
+ const __filename = fileURLToPath(import.meta.url);
+ const __dirname = path.dirname(__filename);
+ const destPath = path.resolve(__dirname, 'files', 'fileToWrite.txt');
+ const writeStream = createWriteStream(destPath);
+ await pipeline(stdin, writeStream);
};
-await write();
\ No newline at end of file
+await write();
diff --git a/src/zip/compress.js b/src/zip/compress.js
index bb328f43c6..587b112df4 100644
--- a/src/zip/compress.js
+++ b/src/zip/compress.js
@@ -1,5 +1,15 @@
+import { createGzip } from 'node:zlib';
+import path from 'node:path';
+import { pipeline } from 'node:stream/promises'
+import { createReadStream, createWriteStream } from 'node:fs';
+import {fileURLToPath} from "node:url";
+
const compress = async () => {
- // Write your code here
+ const __filename = fileURLToPath(import.meta.url);
+ const __dirname = path.dirname(__filename);
+ const srcPath = path.resolve(__dirname, 'files', 'fileToCompress.txt');
+ const destPath = path.resolve(__dirname, 'files', 'archive.gz');
+ await pipeline(createReadStream(srcPath), createGzip(), createWriteStream(destPath))
};
-await compress();
\ No newline at end of file
+await compress();
diff --git a/src/zip/decompress.js b/src/zip/decompress.js
index 69f6c345f8..16e4bad003 100644
--- a/src/zip/decompress.js
+++ b/src/zip/decompress.js
@@ -1,5 +1,15 @@
+import {fileURLToPath} from "node:url";
+import path from "node:path";
+import {pipeline} from "node:stream/promises";
+import {createReadStream, createWriteStream} from "node:fs";
+import {createGunzip} from "node:zlib";
+
const decompress = async () => {
- // Write your code here
+ const __filename = fileURLToPath(import.meta.url);
+ const __dirname = path.dirname(__filename);
+ const destPath = path.resolve(__dirname, 'files', 'fileToCompress.txt');
+ const srcPath = path.resolve(__dirname, 'files', 'archive.gz');
+ await pipeline(createReadStream(srcPath), createGunzip(), createWriteStream(destPath))
};
-await decompress();
\ No newline at end of file
+await decompress();