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
56 changes: 56 additions & 0 deletions Joseph Heidari/Streams/read-many/1 - ReadBig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const fs = require("fs/promises");

async function createReadStream_1(){
/** Reading from the source file using a read stream **/

const fileHandleRead = await fs.open("source.txt", "r");
// below is the default HighWatermark value for the read-stream.
const readStream = fileHandleRead.createReadStream({ highWaterMark: 64 * 1024 });

/** Writing to the destination file using a write stream **/

const fileHandleWrite = await fs.open("destination.txt", "w");
const writeStream = fileHandleWrite.createWriteStream();

/** The following will work but still not a good practice as will cause something called Back-Pressure issues.

Backpressure in the context of Node.js streams refers to the scenario where the writable stream cannot process the incoming data as quickly as the readable stream is providing it. This can lead to a buildup of data in the memory buffer, potentially causing memory leaks, degraded performance, or even application crashes if not properly managed.
**/

readStream.on("data", (chunk) => {
writeStream.write(chunk);
});
};

// createReadStream_1();

async function createReadStream_2(){
/** Reading from the source file using a read stream **/

const fileHandleRead = await fs.open("source.txt", "r");
// below is the default HighWatermark value for the read-stream.
const readStream = fileHandleRead.createReadStream({ highWaterMark: 64 * 1024 });

/** Writing to the destination file using a write stream **/

const fileHandleWrite = await fs.open("destination.txt", "w");
const writeStream = fileHandleWrite.createWriteStream();

/**
The real benefit of this shows when the file size is massive. The function_1 will probably just crash the machine. While this function will make sure that the task is done, while keeping the memory usage under check.

This is the neat method, to check if during the writing, writeStream.write(data) returns false, it means that the internal buffer of the write stream is full and we'll need to pause writing until it's ready again.
**/

readStream.on("data", (chunk) => {
if(!writeStream.write(chunk)){
readStream.pause();
}
});

writeStream.on('drain', () => {
readStream.resume();
});
}

// createReadStream_2();
56 changes: 56 additions & 0 deletions Joseph Heidari/Streams/read-many/2 - ReadOnlyEvenNums.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const fs = require("fs/promises");

async function readOnlyEvenNumsUsingStreams(){
/** Reading from the source file using a read stream **/

const fileHandleRead = await fs.open("source.txt", "r");
// below is the default HighWatermark value for the read-stream.
const readStream = fileHandleRead.createReadStream({ highWaterMark: 64 * 1024 });

/** Writing to the destination file using a write stream **/

const fileHandleWrite = await fs.open("destination.txt", "w");
const writeStream = fileHandleWrite.createWriteStream();

/**
We're going to use the readStream and check for data values and only allow the even values to be written.

Now while trying to convert chunks from the readStream into numbers, we run into issues where our numbers might be split into weird places, for example the last element from a chunk might be "2432344" but since computer only understand this as some text being read from a .txt file, the buffer might not contain the complete number with itself. Think about it. Hence, we have made some additions to the code logic below for handling this unintentional anamoly.
**/

let splitNum = ''; // this string will contain the splitted-number (if any) so that we can remove it from this chunk and put it at the starting of the next chunk

readStream.on("data", (chunk) => {

// convert the chunk buffer into a utf-8 string separated by space
const numbers = chunk.toString('utf-8').split(' ');

// if we have a split issue at the starting of this chunk, let's fix it
if(Number(numbers[0]) !== Number(numbers[1]) - 1){
if(splitNum)
numbers[0] = splitNum.trim() + numbers[0].trim();
}

// if we have a split issue at the end of this chunk, let's prepare 'splitNum' for the next chunk
if(Number(numbers[numbers.length - 2]) + 1 !== Number(numbers[numbers.length - 1])){
splitNum = numbers.pop();
}

// writing even numbers from this chunk
numbers.forEach((number) => {
let n = Number(number);
if(n % 2 == 0){
if(!writeStream.write(" " + n + " ")){
readStream.pause();
}
}
});

});

writeStream.on('drain', () => {
readStream.resume();
});
};

readOnlyEvenNumsUsingStreams();
1 change: 1 addition & 0 deletions Joseph Heidari/Streams/read-many/destination.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Joseph Heidari/Streams/read-many/source.txt

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions Joseph Heidari/UNIX/10-stdin-err-out/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const {stdin, stderr, stdout} = require('process');

// stdin, stderr & stdout are just data-STREAMS, like regular streams
stdin.on("data", (data) => {
console.log("Got this data from standard in: ", data.toString("utf-8"));
});

/* By default, stdout and stderr are configured to terminal. But that can be changed.*/

// writing to stdout
stdout.write("This is some text that I want! ");
// writing to stderr
stderr.write("This is some text that I may not want.");

/*
Also, we can configure stdout on the fly by running something like "node playground.js 1>output.txt", here 1 stands for stdout.
*/
13 changes: 13 additions & 0 deletions Joseph Heidari/UNIX/10-stdin-err-out/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//** Trying to connect my node app to the C-App by accessing stdout & stderr of the C App **//

const {spawn, exect} = require("child_process");

const subprocess = spawn("./play");

subprocess.stdout.on("data", (data) => {
console.log("Got this stdout from the C App: ", data.toString("utf-8"));
});

subprocess.stderr.on("data", (data) => {
console.log("Got this stderr from the C App: ", data.toString("utf-8"));
});
Binary file added Joseph Heidari/UNIX/10-stdin-err-out/play
Binary file not shown.
11 changes: 11 additions & 0 deletions Joseph Heidari/UNIX/10-stdin-err-out/play.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main(int argc, char *argv[]){

fprintf(stdout, "some text for the stdout -> coming from the c-app \n");
fprintf(stderr, "some text for the stderr -> coming from the c-app \n");

return 0;
}
17 changes: 17 additions & 0 deletions Joseph Heidari/UNIX/11-Piping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const {stdin, stdout} = require('process');

// we can utilise this via something like -- echo "hey oh" | node 11-Piping.js --
stdin.on('data', (data) => {
stdout.write(`Got this data from the stdin ---> ${data}`);
});

/* We can further chain commands on top of the above command using the | (pipe) operator. Like we can use the tr (translate) command to make all the letters uppercase.

echo "hey oh" | node 11-Piping.js | tr 'a-z' 'A-Z'
*/

/*
node playground.js 2>text.txt --> This will over-write the file.
node playground.js 2>>text.txt --> This will append to the file.
*/

2 changes: 1 addition & 1 deletion Joseph Heidari/UNIX/9-Path-Lecture/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require("./file.js");
// Also logging the cwd value which will go as the reference to the below relative path
console.log(`Node's app.js process's CWD -> ${process.cwd()}`);

const content = fs.readFileSync("./text.txt", "utf-8");
const content = fs.readFileSync("./file.txt", "utf-8");
console.log("logging the content from the text file: ", content);

/*
Expand Down
1 change: 1 addition & 0 deletions Joseph Heidari/UNIX/9-Path-Lecture/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is some string in the text file.