A simple binary data chunking library that simplifies sending large amounts of chunked binary data. For example via. the WebRTC DataChannel.
This is useful in the case that you would like to received chunks of data instead of one large file Blob at the end of the transfer (as in Firefox), and avoid the issues with sending large files in Chrome.
Although this was designed for use in WebRTC, it could be used in other environments where you need a custom way to send chunks binary of information.
This can be used both in a
node.js or browser environment (targeted browsers are the latest: Chrome, Firefox and eventually Edge)
var BDC = ;
Include the either the minified, or non-minified files in the
You create a new
BDC instance for each file you wish to transfer. The initialization parameters give the basic information about your file.
var file =name: 'sample.mp4'mimeType: 'video/mp4'arrayBuffer: ab // must be in the ArrayBuffer formatchunkSize: 16000 // chunkSize to use *before* meta-data additions.;filefileSize; // returns actual file size based on ArrayBufferfiletotalChunks; // total number of chunks that will be sent (based on file size and chunk size)filechecksum; // checksum based on ArrayBufferfile; // to be sent to other end to initialize the transfer, used to initialize a BDC instance on the receiving end.file; // removes arrayBuffer data and all stored informationvar to = file; // unique instance to track the transfering of each chunkto; // will get next chunk, or optionally get the chunk based on the number (position) specific.// note, the chunk will have header info embedded in the first few bytes, to receiving client// must also have an instance created based on the information returned from `file.getMetadata()`tocurrentIndex; // returns the current number if chunks given from `getChunk()`
When you receive the
metadata sent from the sending client, you can initialize a new
// when metadata is receivedvar file = metadata;file;file;var to = file;var payload = to; // next ordered arrayBuffer without the binary metadata bits// when a binary chunk is received, we don't know yet which file object it belongs to, so we submit it to the Factory object and wait for the handler to be calledBDC;filegeneratedChecksum; // contains then generated checksum once a transfer is complete
file object instance, you can access the methods like
getChunk() and once completed
The generation of a
uid does have some collision potential, we we are limited in space, and also we do not know what
uids have already been generated on the receiving end(s) of the file transfer(s).