Thursday, September 19, 2024

File dealing with in server-side JavaScript


const fs = require('fs');

const filename="binary.bin";

fs.readFile(filename, (err, information) => {
  if (err) {
    console.error('Error studying file:', err);
    return;
  }
  console.log(information);

  // course of the Buffer information utilizing Buffer strategies (e.g., slice, copy)
});

Streaming recordsdata in JavaScript

One other side of coping with recordsdata is streaming in chunks of information, which turns into a necessity when coping with giant recordsdata. Right here’s a contrived instance of writing out in streaming chunks:


const fs = require('fs');

const filename="large_file.txt"; 
const chunkSize = 1024 * 1024; // (1)
const content material="That is some content material to be written in chunks."; // (2)
const fileSizeLimit = 5 * 1024 * 1024; // // (3)

let writtenBytes = 0; // (4)

const writeStream = fs.createWriteStream(filename, { highWaterMark: chunkSize }); // (5)

perform writeChunk() { // (6)
  const chunk = content material.repeat(Math.ceil(chunkSize / content material.size)); // (7)

  if (writtenBytes + chunk.size  fileSizeLimit) {
      console.error('File measurement restrict reached');
      writeStream.finish(); 
      return;
    }
    console.log(`Wrote chunk of measurement: ${chunk.size}, Whole written: ${writtenBytes}`);
  }
}

writeStream.on('error', (err) => { // (10)
  console.error('Error writing file:', err);
});

writeStream.on('end', () => { // (10)
  console.log('Completed writing file');
});

writeChunk(); 

Streaming offers you extra energy, however you’ll discover it entails extra work. The work you might be doing is in setting chunk sizes after which responding to occasions primarily based on the chunks. That is the essence of avoiding placing an excessive amount of of an enormous file into reminiscence without delay. As an alternative, you break it into chunks and take care of each. Listed below are my notes concerning the attention-grabbing components of the above write instance:

  1. We specify a bit measurement in kilobytes. On this case, we have now a 1MB chunk, which is how a lot content material shall be written at a time.
  2. Right here’s some faux content material to put in writing.
  3. Now, we create a file-size restrict, on this case, 5MB.
  4. This variable tracks what number of bytes we’ve written (so we will cease writing after 5MB).
  5. We create the precise writeStream object. The highWaterMark ingredient tells it how huge the chunks are that it’ll settle for.
  6. The writeChunk() perform is recursive. At any time when a bit must be dealt with, it calls itself. It does this except the file restrict has been reached, wherein case it exits.
  7. Right here, we’re simply repeating the pattern textual content till it reaches the 1MB measurement.
  8. Right here’s the attention-grabbing half. If the file measurement isn’t exceeded, then we name writeStream.write(chunk):
    1. writeStream.write(chunk) returns false if the buffer measurement is exceeded. Meaning we will’t match extra within the buffer given the scale restrict.
    2. When the buffer is exceeded, the drain occasion happens, dealt with by the primary handler, which we outline right here with writeStream.as soon as('drain', writeChunk);. Discover that it is a recursive callback to writeChunk.
  9. This retains monitor of how a lot we’ve written.
  10. This handles the case the place we’re carried out writing and finishes the stream author with writeStream.finish();.
  11. This demonstrates including occasion handlers for error and end.

And to learn it again off the disk, we will use the same strategy:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles