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
36 changes: 35 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
# Node.js basics
# Node.js basics

# For run each single task you should input follow lines into terminal:

File system (src/fs):
create.js: npm run fs:create
copy.js: npm run fs:copy
rename.js: npm run fs:rename
delete.js: npm run fs:delete
list.js: npm run fs:list
read.js: nmp run fs:read

Command line interface(src/cli):
env.js: $env:RSS_name-variable="value"; npm run cli:env
args.js: npm run cli:args -- --propName value --prop2Name value2

Modules(src/modules):
cjsToEsm.mjs: npm run modules

Hash (src/hash):
calcHash.js: npm run hash

Streams (src/streams):
read.js: npm run streams:read
write.js: npm run streams:write
transform: npm run streams:transform

Zlib (src/zip):
compress.js: npm run zip:compress
decompress.js: npm run zip:decompress

Worker Threads (src/wt):
main.js: npm run wt
ATTENTION: in worker.js file was added special condition for throw error!!!!!!!! JUST FOR AN EXAMPLE

19 changes: 18 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
"description": "This repository is the part of nodejs-assignments https://github.com/AlreadyBored/nodejs-assignments",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"fs:create": "node src/fs/create.js",
"fs:copy": "node src/fs/copy.js",
"fs:rename": "node src/fs/rename.js",
"fs:delete": "node src/fs/delete.js",
"fs:list": "node src/fs/list.js",
"fs:read": "node src/fs/read.js",
"cli:env": "node src/cli/env.js",
"cli:args": "node src/cli/args.js",
"modules": "node src/modules/cjsToEsm.mjs",
"hash": "node src/hash/calcHash.js",
"streams:read": "node src/streams/read.js",
"streams:write": "node src/streams/write.js",
"streams:transform": "node src/streams/transform.js",
"zip:compress": "node src/zip/compress.js",
"zip:decompress": "node src/zip/decompress.js",
"wt": "node src/wt/main.js",
"cp": "node src/cp/cp.js"
},
"repository": {
"type": "git",
Expand Down
33 changes: 31 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
export const parseArgs = () => {
// Write your code here
};
const args = process.argv;
const flags = {
prefix: "--",
propName: "propName",
prop2Name: "prop2Name",

getPropWithPrefix(name) {
const key = name.split(this.prefix)[1];
return this.prefix + this[key];
},

getOnlyProp(value) {
const key = value.split(this.prefix)[1];
return this[key];
},
};

const output = args
.reduce((acc, curr, index, arr) => {
if (curr === flags.getPropWithPrefix(curr)) {
const buffer = `${flags.getOnlyProp(curr)} is ${arr[index + 1]}`;
acc.push(buffer);
}
return acc;
}, [])
.join(", ");

console.log(output);
};

parseArgs();
18 changes: 16 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
export const parseEnv = () => {
// Write your code here
};
const envArr = Object.entries(process.env);

const rssOnly = envArr.filter((value) => {
return value[0].startsWith("RSS_");
});

const output = rssOnly
.map((value) => {
return `${value[0]}=${value[1]}`;
})
.join("; ");

console.log(output);
};

parseEnv();
10 changes: 8 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { fork } from "child_process";
import { __dirname } from "../globalPath.js";

export const spawnChildProcess = async (args) => {
// Write your code here
};
fork(`${__dirname(import.meta.url)}/files/script.js`, args);
};

const envArguments = process.argv;
spawnChildProcess(envArguments);
14 changes: 7 additions & 7 deletions src/cp/files/script.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const arguments = process.argv.slice(2);
const args = process.argv.slice(2);

console.log(`Total number of arguments is ${arguments.length}`);
console.log(`Arguments: ${JSON.stringify(arguments)}`);
console.log(`Total number of arguments is ${args.length}`);
console.log(`Arguments: ${JSON.stringify(args)}`);

const echoInput = (chunk) => {
const chunkStringified = chunk.toString();
if (chunkStringified.includes('CLOSE')) process.exit(0);
process.stdout.write(`Received from master process: ${chunk.toString()}\n`)
const chunkStringified = chunk.toString();
if (chunkStringified.includes("CLOSE")) process.exit(0);
process.stdout.write(`Received from master process: ${chunk.toString()}\n`);
};

process.stdin.on('data', echoInput);
process.stdin.on("data", echoInput);
24 changes: 22 additions & 2 deletions src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
import { readdir, copyFile, mkdir } from "fs";
import { __dirname } from "../globalPath.js";

export const copy = async () => {
// Write your code here
};
const sourcePath = `${__dirname(import.meta.url)}/files`;
const copy = `${__dirname(import.meta.url)}/files_copy`;

readdir(sourcePath, (err, files) => {
if (err) {
throw new Error("FS operation failed");
}
mkdir(copy, (err) => {
if (err) {
throw new Error("FS operation failed");
}
});
files.forEach((file) => {
copyFile(`${sourcePath}/${file}`, `${copy}/${file}`, () => {});
});
});
};

copy();
19 changes: 17 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import { stat, writeFile } from "fs";
import { __dirname } from "../globalPath.js";

export const create = async () => {
// Write your code here
};
const newFileName = "fresh.txt";
const fullPath = `${__dirname(import.meta.url)}/files/${newFileName}`;
const fileContent = "I am fresh and young";

stat(fullPath, (err, stats) => {
if (!stats) {
writeFile(fullPath, fileContent, () => {});
} else {
throw Error("FS operation failed");
}
});
};

create();
15 changes: 13 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { unlink } from "fs";
import { __dirname } from "../globalPath.js";

export const remove = async () => {
// Write your code here
};
const targetPath = `${__dirname(import.meta.url)}/files/fileToRemove.txt`;

unlink(targetPath, (err) => {
if (err) {
throw new Error("FS operation failed");
}
});
};

remove();
16 changes: 14 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import { readdir } from "fs";
import { __dirname } from "../globalPath.js";

export const list = async () => {
// Write your code here
};
const sourcePath = `${__dirname(import.meta.url)}/files`;

readdir(sourcePath, (err, files) => {
if (err) {
throw new Error("FS operation failed");
}
files.forEach((file) => console.log(file));
});
};

list();
16 changes: 14 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import { readFile } from "fs";
import { __dirname } from "../globalPath.js";

export const read = async () => {
// Write your code here
};
const path = `${__dirname(import.meta.url)}/files/fileToRead.txt`;

readFile(path, "utf8", (err, data) => {
if (err) {
throw new Error("FS operation failed");
}
console.log(data);
});
};

read();
23 changes: 20 additions & 3 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
export const rename = async () => {
// Write your code here
};
import { rename } from "fs";
import { __dirname } from "../globalPath.js";

export const fileRename = async () => {
const sourcePath = `${__dirname(import.meta.url)}/files`;
const targetFile = "wrongFilename.txt";
const finalFileName = "properFilename.md";

rename(
`${sourcePath}/${targetFile}`,
`${sourcePath}/${finalFileName}`,
(err) => {
if (err) {
throw new Error("FS operation failed");
}
}
);
};

fileRename();
13 changes: 13 additions & 0 deletions src/globalPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { fileURLToPath } from "url";
import { dirname } from "path";

function __filename(metaUrl) {
return fileURLToPath(metaUrl);
}

function __dirname(metaUrl) {
const fileName = fileURLToPath(metaUrl);
return dirname(fileName);
}

export { __filename, __dirname };
20 changes: 18 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
import { readFileSync } from "fs";
import crypto from "crypto";
import { __dirname } from "../globalPath.js";

export const calculateHash = async () => {
// Write your code here
};
const sourcePath = `${__dirname(
import.meta.url
)}/files/fileToCalculateHashFor.txt`;

const data = readFileSync(sourcePath, "utf8");
return crypto.createHash("sha256").update(data).digest("hex");
};

async function printHash() {
const hash = await calculateHash();
console.log(hash);
}

printHash();
31 changes: 0 additions & 31 deletions src/modules/cjsToEsm.cjs

This file was deleted.

41 changes: 41 additions & 0 deletions src/modules/cjsToEsm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path from "path";
import { release, version } from "os";
import { createServer as createServerHttp } from "http";
import "./files/c.js";

import { readFile } from "fs/promises";
import { fileURLToPath } from "url";
import { dirname } from "path";

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

const aJSON = JSON.parse(
await readFile(new URL("./files/a.json", import.meta.url))
);
const bJSON = JSON.parse(
await readFile(new URL("./files/b.json", import.meta.url))
);

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = aJSON;
} else {
unknownObject = bJSON;
}

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 createMyServer = createServerHttp((_, res) => {
res.end("Request accepted");
});

export { unknownObject, createMyServer };
Loading