How to use Ronomon Reed-Solomon Library to split file in chunks

374 views Asked by At

I am trying to use Ronomon/Reed-Solomon JavaScript Library, I want to chunk files based on the number of data shards and parity shards. Any example demonstrating the use of ReedSolomon.encode will help me. My code is as below:

Encode.js

var ReedSolomon = require('@ronomon/reed-solomon');
const fs = require('fs');

/* rsencode function
   blob           : file object // Blob object
   options        : set of options(key, value)
   {
    data_shards   : # of data shards(symbols) // integer
    parity_shards : # of parity shards // integer
    buffer_offset : # of bytes attached in front of file // integer (optional)
    header_msg    : non-reedsolomon header (optional)
    footer_msg    : non-reedsolomon footer (optional)
   }
   callback       : to handle encoded buffer
*/


fileinfo = function (file){
 var fsize = fs.statSync(file)
 var st = fsize.size
 console.log(fsize)
 console.log("sizeeee", st)
};

rsencode = function (file, options, callback){
    // console.log("inside")
  var fsize = fs.statSync(file)
  var fileSize = fsize.size;
  console.log(fileSize)
  var dataShards = options.data_shards;
  var parityShards = options.parity_shards;
  var bufferOffset = ( options.buffer_offset || 0 );
  var header = ( options.header_msg || '' );
  var footer = ( options.footer_msg || '' );
  var shardOffset = 1; // for index of shards

  // if(!(blob instanceof Blob)) throw new Error('blob must be a Blob object');

  var rs = ReedSolomon.create(dataShards, parityShards); // including assertion

  var totalShards = dataShards + parityShards;
  var shardLength = Math.floor(fileSize / (dataShards - 1));
  var remainder = fileSize % (dataShards - 1);

  if(remainder > shardLength){
    shardLength = shardLength + Math.floor(remainder / shardLength);
    remainder = remainder % shardLength;
  }

  var paddingLength = shardLength - remainder;
  var paddingString = "";

  for(i = 0; i < paddingLength; i++){
    paddingString = paddingString + "1";
  }

  var reader = fs.readFile(file,function(result) {
    console.log(result);
})
console.log(reader)

var bufferSize = (shardLength + shardOffset) * totalShards;
var shardSize = shardLength;

      ReedSolomon.encode(
        rs,
        bufferOffset,
        bufferSize,
        shardLength+shardOffset,
        shardOffset,
        shardSize,
        (error) => {
          if(error) throw error;
          callback(buffer, fileSize, bufferOffset, bufferSize, totalShards, shardLength, shardOffset);
        },
      );

};

Client.js

var sd = require('./encode')

file = "big.txt"
// console.log(file)

rsencode(file,{data_shards:4,parity_shards:2}, function(result) {
    console.log(result);
})

I am a newbie to JavaScript and hence cannot split files, kindly help.

0

There are 0 answers