Let's look at the following code snippet:
const fs = require('fs');
const server = require('http').createServer();
server.on('request', (req, res) => {
const src = fs.createReadStream('./big.file');
src.pipe(res);
});
server.listen(8000);
fs.createReadStream divides the content of big.file to chunks and reads the file "chunk after chunk".
My question is what is the size of every chunk, where it's defined and how I can change the chunk size?
A chunk is a fragment of the data that is sent by the client to server all chunks concepts to each other to make a buffer of the stream then the buffer is converted into meaningful data.
Return Value: This method returns the fs. ReadStream object.
createReadStream() allows you to open up a readable stream in a very simple manner. All you have to do is pass the path of the file to start streaming in. It turns out that the response (as well as the request) objects are streams. So we will use this fact to create a http server that streams the files to the client.
PassThrough. This Stream is a trivial implementation of a Transform stream that simply passes the input bytes across to the output. This is mainly for testing and some other trivial use cases. Here is an example of Passthrough Stream where it is piping from readable stream to writable stream.
here is how to change the chunk size
var rs = fs.createReadStream('/foo/bar', { highWaterMark: 128 * 1024 });
Check the documentation for createReadStream
here.
It takes in an options
object as its second parameter.
highWaterMark
is the option that you are looking for, and it defaults to 64 * 1024
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With