33

I'm trying to create a socket based filetransfer system and because of that I came up with some trouble. I want to read files in chunks using node's fs.createReadStream() but I was unable to change the chunk size from 665536. I couldn't find anything about this.

random
  • 9,774
  • 10
  • 66
  • 83
Joe Thomas
  • 5,807
  • 6
  • 25
  • 36
  • Could you help with a related question: In this situation (socket-based filesharing), what's an ideal chunk size? And in general (aside from sockets), what's an ideal chunk size? – Morris Sep 13 '20 at 20:15

2 Answers2

72

According to the ReadStream code, you should be able to increase the highWaterMark by explicitly setting it in the ReadStream options:

var rs = fs.createReadStream('/foo/bar', { highWaterMark: 128 * 1024 });
mscdex
  • 104,356
  • 15
  • 192
  • 153
1

This is how you are going to change the change the size of the chunk with highWaterMark : 8 , you can set it to 8,16,24 all are in bytes, be sure if you make chunk size to much high then you are by passing the benefit of using readerstream1. Because benefit of readerstream or stream is that they work efficiently while consuming less memory.

let readerstream1 = fs.createReadStream('shamoon.txt', { highWaterMark: 8 });
Shamoon97
  • 332
  • 2
  • 12