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
12 changes: 10 additions & 2 deletions src/cli/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const parseArgs = () => {
// Write your code here
const args = process.argv
.reduce((acc, arg, i) => {
if (arg.startsWith("--")) {
acc.push(`${arg.substring(2)} is ${process.argv[i + 1]}`);
}
return acc;
}, [])
.join(", ");
console.log(args);
};

parseArgs();
parseArgs();
8 changes: 6 additions & 2 deletions src/cli/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const parseEnv = () => {
// Write your code here
const env = Object.entries(process.env)
.filter(([key]) => key.startsWith("RSS_"))
.map(([key, value]) => `${key}=${value}`)
.join("; ");
console.log(env);
};

parseEnv();
parseEnv();
13 changes: 11 additions & 2 deletions src/cp/cp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { spawn } from 'node:child_process';
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const childPath = path.join(rootPath, 'files', "script.js");

const spawnChildProcess = async (args) => {
// Write your code here
const child = spawn('node', [childPath, ...args]);
process.stdin.pipe(child.stdin);
child.stdout.pipe(process.stdout);
};

// Put your arguments in function call to test this functionality
spawnChildProcess( /* [someArgument1, someArgument2, ...] */);
spawnChildProcess(['fgs', 'fds']);
27 changes: 26 additions & 1 deletion src/fs/copy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { mkdir, access, constants, readdir, copyFile } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const sourcePath = path.join(rootPath, "files");
const destPath = path.join(rootPath, "files_copy");

const errorMessage = "FS operation failed";

const copy = async () => {
// Write your code here
access(destPath, constants.F_OK, (error) => {
if (!error) throw new Error(errorMessage);
});
mkdir(destPath, (error) => {
if (error) throw new Error(error);
});
readdir(sourcePath, (error, files) => {
if (error) throw new Error(error);
files.forEach((name) => {
const src = path.join(sourcePath, name);
const dest = path.join(destPath, name);
copyFile(src, dest, (error) => {
if (error) throw new Error(error);
});
});
});
};

await copy();
19 changes: 17 additions & 2 deletions src/fs/create.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { writeFile, access, constants } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const filePath = path.join(rootPath, "files", "fresh.txt");

const text = "I am fresh and young";
const errorMessage = "FS operation failed";

const create = async () => {
// Write your code here
access(filePath, constants.F_OK, (error) => {
if (!error) throw new Error(errorMessage);
});
writeFile(filePath, text, (error) => {
if (error) throw new Error(error);
});
};

await create();
await create();
15 changes: 13 additions & 2 deletions src/fs/delete.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { rm } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const filePath = path.join(rootPath, "files", "fileToRemove.txt");

const errorMessage = "FS operation failed";

const remove = async () => {
// Write your code here
rm(filePath, (error) => {
if (error) throw new Error(errorMessage);
});
};

await remove();
await remove();
16 changes: 14 additions & 2 deletions src/fs/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { mkdir, access, constants, readdir, copyFile } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const sourcePath = path.join(rootPath, "files");

const errorMessage = "FS operation failed";

const list = async () => {
// Write your code here
readdir(sourcePath, (error, files) => {
if (error) throw new Error(errorMessage);
console.log(files);
});
};

await list();
await list();
16 changes: 14 additions & 2 deletions src/fs/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { readFile } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const sourcePath = path.join(rootPath, "files", "fileToRead.txt");

const errorMessage = "FS operation failed";

const read = async () => {
// Write your code here
readFile(sourcePath, (error, text) => {
if (error) throw new Error(errorMessage);
console.log(text.toString());
});
};

await read();
await read();
19 changes: 17 additions & 2 deletions src/fs/rename.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { access, constants, rename as nodeRename } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const sourcePath = path.join(rootPath, "files", "wrongFilename.txt");
const destPath = path.join(rootPath, "files", "properFilename.md");

const errorMessage = "FS operation failed";

const rename = async () => {
// Write your code here
access(sourcePath, constants.F_OK, (error) => {
if (error) throw new Error(errorMessage);
});
nodeRename(sourcePath, destPath, (error) => {
if (error) throw new Error(errorMessage);
});
};

await rename();
await rename();
16 changes: 14 additions & 2 deletions src/hash/calcHash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { createHash } from "node:crypto";
import { createReadStream } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const sourcePath = path.join(rootPath, "files", "fileToCalculateHashFor.txt");

const calculateHash = async () => {
// Write your code here
const readableStream = createReadStream(sourcePath);
readableStream.on("data", (data) => {
const hash = createHash("sha256").update(data).digest("hex");
console.log(hash);
});
};

await calculateHash();
await calculateHash();
40 changes: 0 additions & 40 deletions src/modules/cjsToEsm.cjs

This file was deleted.

42 changes: 42 additions & 0 deletions src/modules/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { sep } from 'path';
import { release, version } from 'os';
import { createServer as createServerHttp } from 'http';
import './files/c.js';
import * as a from './files/a.json' with { type: "json" };
import * as b from './files/b.json' with { type: "json" };

const random = Math.random();

let unknownObject;

if (random > 0.5) {
unknownObject = a;
} else {
unknownObject = b;
}

console.log(`Release ${release()}`);
console.log(`Version ${version()}`);
console.log(`Path segment separator is "${sep}"`);

console.log(`Path to current file is ${process.env.__filename}`);
console.log(`Path to current directory is ${process.env.__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 default {
unknownObject,
myServer,
};

14 changes: 12 additions & 2 deletions src/streams/read.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { createReadStream } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const sourcePath = path.join(rootPath, "files", "fileToRead.txt");

const read = async () => {
// Write your code here
const readableStream = createReadStream(sourcePath);
readableStream.on("data", (data) => {
process.stdout.write(data + "\n");
});
};

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

class textReverse extends Transform {
constructor () {
super();
};

_transform(chunc, enc, done) {
this.push(chunc.toString().split('').reverse().join('') + '\n');
done();
}
}

const transform = async () => {
// Write your code here
process.stdin
.pipe(new textReverse())
.pipe(process.stdout);
};

await transform();
await transform();
10 changes: 9 additions & 1 deletion src/streams/write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { createWriteStream } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const destPath = path.join(rootPath, "files", "fileToWrite.txt");

const write = async () => {
// Write your code here
const writableStream = createWriteStream(destPath);
process.stdin.pipe(writableStream);
};

await write();
24 changes: 22 additions & 2 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 "worker_threads";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";

const rootPath = dirname(fileURLToPath(import.meta.url));
const workerPath = path.join(rootPath, "worker.js");

const performCalculations = async () => {
// Write your code here
const cores = cpus().length;
const workers = Array.from({ length: cores }, (_, i) => i).map(
(core) => new Worker(workerPath, { workerData: core + 10 })
);

let count = 0;
workers.forEach((worker) =>
worker.on("message", (msg) => {
worker.resultData = msg;
count++;
if (count === cores) console.log(workers.map((w) => w.resultData));
})
);
};

await performCalculations();
await performCalculations();
Loading