binary-data-chunking
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.
Environment
This can be used both in a node.js
or browser environment (targeted browsers are the latest: Chrome, Firefox and eventually Edge)
Usage
Include
Node
var BDC = require('binary-data-chunking');
Browser
Include the either the minified, or non-minified files in the dist/
directory.
dist/binary-data-chunking.js
dist/binary-data-chunking.min.js
Initialize
You create a new BDC
instance for each file you wish to transfer. The initialization parameters give the basic information about your file.
Sending client
var file = new BDC({
name: 'sample.mp4',
mimeType: 'video/mp4',
arrayBuffer: ab,
chunkSize: 16000
});
file.getFileSize();
file.getTotalChunks();
file.getChecksum();
file.getMetadata();
file.getChunk([pos]);
file.getPosition();
file.clearData();
Receiving client
When you receive the metadata
sent from the sending client, you can initialize a new BDC
instance
var file = new BDC(metadata);
file.onChunkReceived(function (ab, pos) {
});
file.onComplete(function (ab, metadata) {
});
BDC.submitChunk(chunk);
With your file
object instance, you can access the methods like getNumChunks()
, getFileSize()
, getMetadata()
, getChunk()
and once completed getChecksum()
.
Known Issues
The generation of a uid
does have some collision potential, we we are limited in space, and also we do not know what uid
s have already been generated on the receiving end(s) of the file transfer(s).