Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

binary-data-chunking

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binary-data-chunking - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

125

browser/binary-data-chunking.js

@@ -955,49 +955,47 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.BinaryDataChunking = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

function BDC(metadata) {
function BDC(md) {
var totalChunks, payloadSize, checksum;
if (typeof metadata !== 'object') { throw new Error('metadata object must be passed in'); }
if (! metadata.name) { throw new Error('no name specified'); }
if (! metadata.mimeType) { throw new Error('no mimeType specified'); }
if (! metadata.chunkSize) { throw new Error('no chunkSize specified'); }
if ((metadata.arrayBuffer) &&
((typeof metadata.arrayBuffer.toString !== 'function') || (metadata.arrayBuffer.toString() !== '[object ArrayBuffer]'))) {
if (typeof md !== 'object') { throw new Error('metadata object must be passed in'); }
if (! md.name) { throw new Error('no name specified'); }
if (! md.mimeType) { throw new Error('no mimeType specified'); }
if (! md.chunkSize) { throw new Error('no chunkSize specified'); }
if ((md.arrayBuffer) &&
((typeof md.arrayBuffer.toString !== 'function') || (md.arrayBuffer.toString() !== '[object ArrayBuffer]'))) {
throw new Error('arrayBuffer must be an actual ArrayBuffer object');
} else if ((! metadata.arrayBuffer) && (! metadata.uid)) {
} else if ((! md.arrayBuffer) && (! md.uid)) {
throw new Error('arrayBuffer or uid must be specified');
} else if ((metadata.arrayBuffer) && (metadata.uid)) {
} else if ((md.arrayBuffer) && (md.uid)) {
throw new Error('cannot manually assign uid');
}
if (! metadata.uid) {
if (! md.uid) {
// we have an arrayBuffer, so we create a UID
metadata.uid = genUID();
md.uid = genUID();
}
if (metadata.arrayBuffer) {
if (md.arrayBuffer) {
// this is a sender
// we have an arrayBuffer, so let's calc total number of chunks
payloadSize = (metadata.chunkSize - RESERVED_BYTES)
totalChunks = Math.ceil(metadata.arrayBuffer.byteLength / payloadSize);
checksum = SparkMD5.ArrayBuffer.hash(metadata.arrayBuffer);
payloadSize = (md.chunkSize - RESERVED_BYTES)
totalChunks = Math.ceil(md.arrayBuffer.byteLength / payloadSize);
checksum = SparkMD5.ArrayBuffer.hash(md.arrayBuffer);
} else {
checksum = metadata.checksum;
checksum = md.checksum;
}
this.uid = metadata.uid;
this._metadata = {
uid: metadata.uid,
name: metadata.name,
mimeType: metadata.mimeType,
chunkSize: metadata.chunkSize,
payloadSize: payloadSize,
reservedSize: RESERVED_BYTES,
fileSize: (metadata.arrayBuffer) ? metadata.arrayBuffer.byteLength : 0,
totalChunks: (totalChunks) ? totalChunks : (metadata.totalChunks) ? metadata.totalChunks : 0,
checksum: checksum
// TODO calc checksums for every chunk
};
this.uid = md.uid;
this.name = md.name;
this.mimeType = md.mimeType;
this.chunkSize = md.chunkSize;
this.payloadSize = payloadSize;
this.reservedSize = RESERVED_BYTES;
this.fileSize = (md.arrayBuffer) ? md.arrayBuffer.byteLength : 0;
this.totalChunks = (totalChunks) ? totalChunks : (md.totalChunks) ? md.totalChunks : 0;
this.checksum = checksum;
this.generatedChecksum;
this.chunksProcessed = 0;
// TODO calc checksums for every chunk
this._arrayBuffer = (metadata.arrayBuffer) ? metadata.arrayBuffer : null,
this._chunksProcessed = 0;
this._arrayBuffer = (md.arrayBuffer) ? md.arrayBuffer : null,
this._chunks = [];
if (! metadata.arrayBuffer) {
if (! md.arrayBuffer) {
fileChunks.addRecord(this);

@@ -1008,25 +1006,15 @@ }

BDC.prototype.getMetadata = function () {
return this._metadata;
return {
uid: this.uid,
name: this.name,
mimeType: this.mimeType,
chunkSize: this.chunkSize,
payloadSize: this.payloadSize,
reservedSize: this.reservedSize,
fileSize: this.fileSize,
totalChunks: this.totalChunks,
checksum: this.checksum
};
};
BDC.prototype.getUID = function () {
return this.uid;
};
BDC.prototype.getFileSize = function () {
return this._metadata.fileSize;
};
BDC.prototype.getTotalChunks = function () {
return this._metadata.totalChunks;
};
BDC.prototype.getChecksum = function () {
return this._metadata.checksum;
};
BDC.prototype.getGeneratedChecksum = function () {
return this._metadata.generatedChecksum;
};
BDC.prototype.getFile = function (cb) {

@@ -1038,4 +1026,4 @@ this._arrayBuffer = this._chunks[0];

}
this._metadata.generatedChecksum = SparkMD5.ArrayBuffer.hash(this._arrayBuffer);
cb(this._arrayBuffer, this._metadata.generatedChecksum);
this.generatedChecksum = SparkMD5.ArrayBuffer.hash(this._arrayBuffer);
cb(this._arrayBuffer, this.generatedChecksum);
};

@@ -1047,11 +1035,7 @@

BDC.prototype.getPosition = function () {
return this._chunksProcessed;
};
BDC.prototype.forEachChunk = function (cb, end) {
var chunk;
for (var i = 0; i < this._metadata.totalChunks; i += 1) {
for (var i = 0; i < this.totalChunks; i += 1) {
if (chunk = this.getChunk()) {
cb(chunk, this.getPosition());
cb(chunk, this.chunksProcessed);
} else {

@@ -1064,3 +1048,3 @@ end();

BDC.prototype.getChunk = function (num) {
var _adjustedPosition = this._chunksProcessed + 1
var _adjustedPosition = this.chunksProcessed + 1
if (typeof num === 'number') {

@@ -1076,14 +1060,14 @@ if (num <= 0) {

var start = (this._metadata.payloadSize * _adjustedPosition) - this._metadata.payloadSize;
if (start >= this._metadata.fileSize) {
var start = (this.payloadSize * _adjustedPosition) - this.payloadSize;
if (start >= this.fileSize) {
return undefined;
}
var end = start + this._metadata.payloadSize;
end = (end > this._metadata.fileSize) ? this._metadata.fileSize : end;
var end = start + this.payloadSize;
end = (end > this.fileSize) ? this.fileSize : end;
var payload = this._arrayBuffer.slice(start, end);
var chunk = BDC.__pack(this._metadata.uid, _adjustedPosition, payload);
var chunk = BDC.__pack(this.uid, _adjustedPosition, payload);
if (! num) {
this._chunksProcessed += 1;
this.chunksProcessed += 1;
}

@@ -1097,3 +1081,2 @@

delete this._arrayBuffer;
delete this._metadata;
delete this;

@@ -1135,6 +1118,6 @@ };

file._chunks[pos - 1] = ab;
file._chunksProcessed += 1;
file.chunksProcessed += 1;
file.onChunkReceived(ab, pos);
if (file.getPosition() === file.getTotalChunks()) {
if (file.chunksProcessed === file.totalChunks) {
// TODO

@@ -1141,0 +1124,0 @@ // - add merged ArrayBuffer

@@ -1,1 +0,1 @@

!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.BinaryDataChunking=t()}}(function(){var t;return function e(t,r,n){function i(f,s){if(!r[f]){if(!t[f]){var a="function"==typeof require&&require;if(!s&&a)return a(f,!0);if(o)return o(f,!0);var u=new Error("Cannot find module '"+f+"'");throw u.code="MODULE_NOT_FOUND",u}var h=r[f]={exports:{}};t[f][0].call(h.exports,function(e){var r=t[f][1][e];return i(r?r:e)},h,h.exports,e,t,r,n)}return r[f].exports}for(var o="function"==typeof require&&require,f=0;f<n.length;f++)i(n[f]);return i}({1:[function(t,e,r){function n(t){"object"!=typeof t&&(t={}),this._identifier=t.identifier||"id",this._store=[],this._idx=[],t.emitEvents?(this._emitEvents=!0,this.events=new i):this._emitEvents=!1}var i=t("tiny-emitter");n.prototype.emitEvent=function(t,e,r){this._emitEvents&&!r&&this.events.emit(t,e)},n.prototype.getIdentifiers=function(){for(var t=[],e=this._store.length-1;e>=0;e-=1)t[t.length]=this._store[e][this._identifier];return t},n.prototype.getRecord=function(t){for(var e=this._store.length-1;e>=0;e-=1)if(this._store[e][this._identifier]===t)return this._store[e]},n.prototype.exists=function(t){return this.getIndex(t)>=0},n.prototype.getIndex=function(t){for(var e=this._idx.length-1;e>=0;e-=1)if(this._idx[e]===t)return e;return-1},n.prototype.addRecord=function(t){if("object"!=typeof t)throw new Error("cannot add non-object records.");if(!t[this._identifier])throw new Error("cannot add a record with no `"+this._identifier+"` property specified.");var e=this.removeRecord(t[this._identifier],!0);return this._idx[this._idx.length]=t[this._identifier],this._store[this._store.length]=t,setTimeout(function(){e?setTimeout(this.emitEvent.bind(this,"update",t),0):setTimeout(this.emitEvent.bind(this,"add",t),0)}.bind(this),0),!0},n.prototype.removeRecord=function(t,e){var r=this.getIndex(t);if(0>r)return!1;for(var n=r;n>=0;n-=1)if(this._store[n]&&this._store[n][this._identifier]===t)return this._store.splice(n,1),this._idx.splice(r,1),setTimeout(this.emitEvent.bind(this,"remove",t,e),0),!0;for(var i=this._store.length-1;i>=r;i-=1)if(this._store[n]&&this._store[i][this._identifier]===t)return this._store.splice(i,1),this._idx.splice(r,1),setTimeout(this.emitEvent.bind(this,"remove",t,e),0),!0;return!1},n.prototype.forEachRecord=function(t){var e=0,r=this,n=function(){};return setTimeout(function(){for(var i=r._store.length-1;i>=0;i-=1)e+=1,setTimeout(t(r._store[i]),0);setTimeout(n(e),0)},0),{"finally":function(t){n=t}}},n.prototype.getCount=function(){return this._store.length},n.prototype.removeAll=function(){for(var t=this._store.length-1;t>=0;t-=1)delete this._store[t];this._store=[]},e.exports=n},{"tiny-emitter":3}],2:[function(e,r,n){!function(e){if("object"==typeof n)r.exports=e();else if("function"==typeof t&&t.amd)t(e);else{var i;try{i=window}catch(o){i=self}i.SparkMD5=e()}}(function(t){"use strict";function e(t,e,r,n,i,o){return e=v(v(e,t),v(n,o)),v(e<<i|e>>>32-i,r)}function r(t,r,n,i,o,f,s){return e(r&n|~r&i,t,r,o,f,s)}function n(t,r,n,i,o,f,s){return e(r&i|n&~i,t,r,o,f,s)}function i(t,r,n,i,o,f,s){return e(r^n^i,t,r,o,f,s)}function o(t,r,n,i,o,f,s){return e(n^(r|~i),t,r,o,f,s)}function f(t,e){var f=t[0],s=t[1],a=t[2],u=t[3];f=r(f,s,a,u,e[0],7,-680876936),u=r(u,f,s,a,e[1],12,-389564586),a=r(a,u,f,s,e[2],17,606105819),s=r(s,a,u,f,e[3],22,-1044525330),f=r(f,s,a,u,e[4],7,-176418897),u=r(u,f,s,a,e[5],12,1200080426),a=r(a,u,f,s,e[6],17,-1473231341),s=r(s,a,u,f,e[7],22,-45705983),f=r(f,s,a,u,e[8],7,1770035416),u=r(u,f,s,a,e[9],12,-1958414417),a=r(a,u,f,s,e[10],17,-42063),s=r(s,a,u,f,e[11],22,-1990404162),f=r(f,s,a,u,e[12],7,1804603682),u=r(u,f,s,a,e[13],12,-40341101),a=r(a,u,f,s,e[14],17,-1502002290),s=r(s,a,u,f,e[15],22,1236535329),f=n(f,s,a,u,e[1],5,-165796510),u=n(u,f,s,a,e[6],9,-1069501632),a=n(a,u,f,s,e[11],14,643717713),s=n(s,a,u,f,e[0],20,-373897302),f=n(f,s,a,u,e[5],5,-701558691),u=n(u,f,s,a,e[10],9,38016083),a=n(a,u,f,s,e[15],14,-660478335),s=n(s,a,u,f,e[4],20,-405537848),f=n(f,s,a,u,e[9],5,568446438),u=n(u,f,s,a,e[14],9,-1019803690),a=n(a,u,f,s,e[3],14,-187363961),s=n(s,a,u,f,e[8],20,1163531501),f=n(f,s,a,u,e[13],5,-1444681467),u=n(u,f,s,a,e[2],9,-51403784),a=n(a,u,f,s,e[7],14,1735328473),s=n(s,a,u,f,e[12],20,-1926607734),f=i(f,s,a,u,e[5],4,-378558),u=i(u,f,s,a,e[8],11,-2022574463),a=i(a,u,f,s,e[11],16,1839030562),s=i(s,a,u,f,e[14],23,-35309556),f=i(f,s,a,u,e[1],4,-1530992060),u=i(u,f,s,a,e[4],11,1272893353),a=i(a,u,f,s,e[7],16,-155497632),s=i(s,a,u,f,e[10],23,-1094730640),f=i(f,s,a,u,e[13],4,681279174),u=i(u,f,s,a,e[0],11,-358537222),a=i(a,u,f,s,e[3],16,-722521979),s=i(s,a,u,f,e[6],23,76029189),f=i(f,s,a,u,e[9],4,-640364487),u=i(u,f,s,a,e[12],11,-421815835),a=i(a,u,f,s,e[15],16,530742520),s=i(s,a,u,f,e[2],23,-995338651),f=o(f,s,a,u,e[0],6,-198630844),u=o(u,f,s,a,e[7],10,1126891415),a=o(a,u,f,s,e[14],15,-1416354905),s=o(s,a,u,f,e[5],21,-57434055),f=o(f,s,a,u,e[12],6,1700485571),u=o(u,f,s,a,e[3],10,-1894986606),a=o(a,u,f,s,e[10],15,-1051523),s=o(s,a,u,f,e[1],21,-2054922799),f=o(f,s,a,u,e[8],6,1873313359),u=o(u,f,s,a,e[15],10,-30611744),a=o(a,u,f,s,e[6],15,-1560198380),s=o(s,a,u,f,e[13],21,1309151649),f=o(f,s,a,u,e[4],6,-145523070),u=o(u,f,s,a,e[11],10,-1120210379),a=o(a,u,f,s,e[2],15,718787259),s=o(s,a,u,f,e[9],21,-343485551),t[0]=v(f,t[0]),t[1]=v(s,t[1]),t[2]=v(a,t[2]),t[3]=v(u,t[3])}function s(t){var e,r=[];for(e=0;64>e;e+=4)r[e>>2]=t.charCodeAt(e)+(t.charCodeAt(e+1)<<8)+(t.charCodeAt(e+2)<<16)+(t.charCodeAt(e+3)<<24);return r}function a(t){var e,r=[];for(e=0;64>e;e+=4)r[e>>2]=t[e]+(t[e+1]<<8)+(t[e+2]<<16)+(t[e+3]<<24);return r}function u(t){var e,r,n,i,o,a,u=t.length,h=[1732584193,-271733879,-1732584194,271733878];for(e=64;u>=e;e+=64)f(h,s(t.substring(e-64,e)));for(t=t.substring(e-64),r=t.length,n=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],e=0;r>e;e+=1)n[e>>2]|=t.charCodeAt(e)<<(e%4<<3);if(n[e>>2]|=128<<(e%4<<3),e>55)for(f(h,n),e=0;16>e;e+=1)n[e]=0;return i=8*u,i=i.toString(16).match(/(.*?)(.{0,8})$/),o=parseInt(i[2],16),a=parseInt(i[1],16)||0,n[14]=o,n[15]=a,f(h,n),h}function h(t){var e,r,n,i,o,s,u=t.length,h=[1732584193,-271733879,-1732584194,271733878];for(e=64;u>=e;e+=64)f(h,a(t.subarray(e-64,e)));for(t=u>e-64?t.subarray(e-64):new Uint8Array(0),r=t.length,n=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],e=0;r>e;e+=1)n[e>>2]|=t[e]<<(e%4<<3);if(n[e>>2]|=128<<(e%4<<3),e>55)for(f(h,n),e=0;16>e;e+=1)n[e]=0;return i=8*u,i=i.toString(16).match(/(.*?)(.{0,8})$/),o=parseInt(i[2],16),s=parseInt(i[1],16)||0,n[14]=o,n[15]=s,f(h,n),h}function c(t){var e,r="";for(e=0;4>e;e+=1)r+=b[t>>8*e+4&15]+b[t>>8*e&15];return r}function p(t){var e;for(e=0;e<t.length;e+=1)t[e]=c(t[e]);return t.join("")}function y(t){return/[\u0080-\uFFFF]/.test(t)&&(t=unescape(encodeURIComponent(t))),t}function d(t,e){var r,n=t.length,i=new ArrayBuffer(n),o=new Uint8Array(i);for(r=0;n>r;r+=1)o[r]=t.charCodeAt(r);return e?o:i}function _(t){return String.fromCharCode.apply(null,new Uint8Array(t))}function l(t,e,r){var n=new Uint8Array(t.byteLength+e.byteLength);return n.set(new Uint8Array(t)),n.set(new Uint8Array(e),t.byteLength),r?n:n.buffer}function g(t){var e,r=[],n=t.length;for(e=0;n-1>e;e+=2)r.push(parseInt(t.substr(e,2),16));return String.fromCharCode.apply(String,r)}function m(){this.reset()}var v=function(t,e){return t+e&4294967295},b=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];return"5d41402abc4b2a76b9719d911017c592"!==p(u("hello"))&&(v=function(t,e){var r=(65535&t)+(65535&e),n=(t>>16)+(e>>16)+(r>>16);return n<<16|65535&r}),"undefined"==typeof ArrayBuffer||ArrayBuffer.prototype.slice||!function(){function e(t,e){return t=0|t||0,0>t?Math.max(t+e,0):Math.min(t,e)}ArrayBuffer.prototype.slice=function(r,n){var i,o,f,s,a=this.byteLength,u=e(r,a),h=a;return n!==t&&(h=e(n,a)),u>h?new ArrayBuffer(0):(i=h-u,o=new ArrayBuffer(i),f=new Uint8Array(o),s=new Uint8Array(this,u,i),f.set(s),o)}}(),m.prototype.append=function(t){return this.appendBinary(y(t)),this},m.prototype.appendBinary=function(t){this._buff+=t,this._length+=t.length;var e,r=this._buff.length;for(e=64;r>=e;e+=64)f(this._hash,s(this._buff.substring(e-64,e)));return this._buff=this._buff.substring(e-64),this},m.prototype.end=function(t){var e,r,n=this._buff,i=n.length,o=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(e=0;i>e;e+=1)o[e>>2]|=n.charCodeAt(e)<<(e%4<<3);return this._finish(o,i),r=p(this._hash),t&&(r=g(r)),this.reset(),r},m.prototype.reset=function(){return this._buff="",this._length=0,this._hash=[1732584193,-271733879,-1732584194,271733878],this},m.prototype.getState=function(){return{buff:this._buff,length:this._length,hash:this._hash}},m.prototype.setState=function(t){return this._buff=t.buff,this._length=t.length,this._hash=t.hash,this},m.prototype.destroy=function(){delete this._hash,delete this._buff,delete this._length},m.prototype._finish=function(t,e){var r,n,i,o=e;if(t[o>>2]|=128<<(o%4<<3),o>55)for(f(this._hash,t),o=0;16>o;o+=1)t[o]=0;r=8*this._length,r=r.toString(16).match(/(.*?)(.{0,8})$/),n=parseInt(r[2],16),i=parseInt(r[1],16)||0,t[14]=n,t[15]=i,f(this._hash,t)},m.hash=function(t,e){return m.hashBinary(y(t),e)},m.hashBinary=function(t,e){var r=u(t),n=p(r);return e?g(n):n},m.ArrayBuffer=function(){this.reset()},m.ArrayBuffer.prototype.append=function(t){var e,r=l(this._buff.buffer,t,!0),n=r.length;for(this._length+=t.byteLength,e=64;n>=e;e+=64)f(this._hash,a(r.subarray(e-64,e)));return this._buff=n>e-64?new Uint8Array(r.buffer.slice(e-64)):new Uint8Array(0),this},m.ArrayBuffer.prototype.end=function(t){var e,r,n=this._buff,i=n.length,o=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(e=0;i>e;e+=1)o[e>>2]|=n[e]<<(e%4<<3);return this._finish(o,i),r=p(this._hash),t&&(r=g(r)),this.reset(),r},m.ArrayBuffer.prototype.reset=function(){return this._buff=new Uint8Array(0),this._length=0,this._hash=[1732584193,-271733879,-1732584194,271733878],this},m.ArrayBuffer.prototype.getState=function(){var t=m.prototype.getState.call(this);return t.buff=_(t.buff),t},m.ArrayBuffer.prototype.setState=function(t){return t.buff=d(t.buff,!0),m.prototype.setState.call(this,t)},m.ArrayBuffer.prototype.destroy=m.prototype.destroy,m.ArrayBuffer.prototype._finish=m.prototype._finish,m.ArrayBuffer.hash=function(t,e){var r=h(new Uint8Array(t)),n=p(r);return e?g(n):n},m})},{}],3:[function(t,e,r){function n(){}n.prototype={on:function(t,e,r){var n=this.e||(this.e={});return(n[t]||(n[t]=[])).push({fn:e,ctx:r}),this},once:function(t,e,r){function n(){i.off(t,n),e.apply(r,arguments)}var i=this;return n._=e,this.on(t,n,r)},emit:function(t){var e=[].slice.call(arguments,1),r=((this.e||(this.e={}))[t]||[]).slice(),n=0,i=r.length;for(n;i>n;n++)r[n].fn.apply(r[n].ctx,e);return this},off:function(t,e){var r=this.e||(this.e={}),n=r[t],i=[];if(n&&e)for(var o=0,f=n.length;f>o;o++)n[o].fn!==e&&n[o].fn._!==e&&i.push(n[o]);return i.length?r[t]=i:delete r[t],this}},e.exports=n},{}],4:[function(t,e,r){function n(){return Math.floor(65535*Math.random()+1)}function i(t,e){var r=new Uint8Array(t.byteLength+e.byteLength);return r.set(new Uint8Array(t),0),r.set(new Uint8Array(e),t.byteLength),r.buffer}function o(t){var e,r,i;if("object"!=typeof t)throw new Error("metadata object must be passed in");if(!t.name)throw new Error("no name specified");if(!t.mimeType)throw new Error("no mimeType specified");if(!t.chunkSize)throw new Error("no chunkSize specified");if(t.arrayBuffer&&("function"!=typeof t.arrayBuffer.toString||"[object ArrayBuffer]"!==t.arrayBuffer.toString()))throw new Error("arrayBuffer must be an actual ArrayBuffer object");if(!t.arrayBuffer&&!t.uid)throw new Error("arrayBuffer or uid must be specified");if(t.arrayBuffer&&t.uid)throw new Error("cannot manually assign uid");t.uid||(t.uid=n()),t.arrayBuffer?(r=t.chunkSize-a,e=Math.ceil(t.arrayBuffer.byteLength/r),i=s.ArrayBuffer.hash(t.arrayBuffer)):i=t.checksum,this.uid=t.uid,this._metadata={uid:t.uid,name:t.name,mimeType:t.mimeType,chunkSize:t.chunkSize,payloadSize:r,reservedSize:a,fileSize:t.arrayBuffer?t.arrayBuffer.byteLength:0,totalChunks:e?e:t.totalChunks?t.totalChunks:0,checksum:i},this._arrayBuffer=t.arrayBuffer?t.arrayBuffer:null,this._chunksProcessed=0,this._chunks=[],t.arrayBuffer||c.addRecord(this)}var f=t("array-keys"),s=t("spark-md5"),a=8,u=0,h=4,c=new f({identifier:"uid"});o.prototype.getMetadata=function(){return this._metadata},o.prototype.getUID=function(){return this.uid},o.prototype.getFileSize=function(){return this._metadata.fileSize},o.prototype.getTotalChunks=function(){return this._metadata.totalChunks},o.prototype.getChecksum=function(){return this._metadata.checksum},o.prototype.getGeneratedChecksum=function(){return this._metadata.generatedChecksum},o.prototype.getFile=function(t){this._arrayBuffer=this._chunks[0];for(var e=1;e<this._chunks.length;e+=1)this._arrayBuffer=i(this._arrayBuffer,this._chunks[e]);this._metadata.generatedChecksum=s.ArrayBuffer.hash(this._arrayBuffer),t(this._arrayBuffer,this._metadata.generatedChecksum)},o.prototype.onChunkReceived=function(){},o.prototype.onCompleted=function(){},o.prototype.getPosition=function(){return this._chunksProcessed},o.prototype.forEachChunk=function(t,e){for(var r,n=0;n<this._metadata.totalChunks;n+=1)(r=this.getChunk())?t(r,this.getPosition()):e()},o.prototype.getChunk=function(t){var e=this._chunksProcessed+1;if("number"==typeof t){if(0>=t)return;e=t}else t=void 0;var r=this._metadata.payloadSize*e-this._metadata.payloadSize;if(!(r>=this._metadata.fileSize)){var n=r+this._metadata.payloadSize;n=n>this._metadata.fileSize?this._metadata.fileSize:n;var i=this._arrayBuffer.slice(r,n),f=o.__pack(this._metadata.uid,e,i);return t||(this._chunksProcessed+=1),f}},o.prototype.clearData=function(){c.removeRecord(this.uid),delete this._arrayBuffer,delete this._metadata,delete this},o.__pack=function(t,e,r){var n=new ArrayBuffer(r.byteLength+a),i=new DataView(n);return i.setInt32(u,t),i.setInt32(h,e),new Uint8Array(n,0,n.byteLength).set(new Uint8Array(r),a),n},o.__unpack=function(t){var e=new DataView(t),r=e.getInt32(u),n=e.getInt32(h),i=new Uint8Array(t.slice(a));return[r,n,i]},o.submitChunk=function(t){var e=this.__unpack(t),r=e[0],n=e[1],i=e[2],o=c.getRecord(r);return o?(o._chunks[n-1]=i,o._chunksProcessed+=1,o.onChunkReceived(i,n),o.getPosition()===o.getTotalChunks()&&o.onCompleted(),c.addRecord(o),!0):!1},e.exports=o},{"array-keys":1,"spark-md5":2}]},{},[4])(4)});
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.BinaryDataChunking=t()}}(function(){var t;return function e(t,r,n){function i(s,f){if(!r[s]){if(!t[s]){var h="function"==typeof require&&require;if(!f&&h)return h(s,!0);if(o)return o(s,!0);var u=new Error("Cannot find module '"+s+"'");throw u.code="MODULE_NOT_FOUND",u}var a=r[s]={exports:{}};t[s][0].call(a.exports,function(e){var r=t[s][1][e];return i(r?r:e)},a,a.exports,e,t,r,n)}return r[s].exports}for(var o="function"==typeof require&&require,s=0;s<n.length;s++)i(n[s]);return i}({1:[function(t,e,r){function n(t){"object"!=typeof t&&(t={}),this._identifier=t.identifier||"id",this._store=[],this._idx=[],t.emitEvents?(this._emitEvents=!0,this.events=new i):this._emitEvents=!1}var i=t("tiny-emitter");n.prototype.emitEvent=function(t,e,r){this._emitEvents&&!r&&this.events.emit(t,e)},n.prototype.getIdentifiers=function(){for(var t=[],e=this._store.length-1;e>=0;e-=1)t[t.length]=this._store[e][this._identifier];return t},n.prototype.getRecord=function(t){for(var e=this._store.length-1;e>=0;e-=1)if(this._store[e][this._identifier]===t)return this._store[e]},n.prototype.exists=function(t){return this.getIndex(t)>=0},n.prototype.getIndex=function(t){for(var e=this._idx.length-1;e>=0;e-=1)if(this._idx[e]===t)return e;return-1},n.prototype.addRecord=function(t){if("object"!=typeof t)throw new Error("cannot add non-object records.");if(!t[this._identifier])throw new Error("cannot add a record with no `"+this._identifier+"` property specified.");var e=this.removeRecord(t[this._identifier],!0);return this._idx[this._idx.length]=t[this._identifier],this._store[this._store.length]=t,setTimeout(function(){e?setTimeout(this.emitEvent.bind(this,"update",t),0):setTimeout(this.emitEvent.bind(this,"add",t),0)}.bind(this),0),!0},n.prototype.removeRecord=function(t,e){var r=this.getIndex(t);if(0>r)return!1;for(var n=r;n>=0;n-=1)if(this._store[n]&&this._store[n][this._identifier]===t)return this._store.splice(n,1),this._idx.splice(r,1),setTimeout(this.emitEvent.bind(this,"remove",t,e),0),!0;for(var i=this._store.length-1;i>=r;i-=1)if(this._store[n]&&this._store[i][this._identifier]===t)return this._store.splice(i,1),this._idx.splice(r,1),setTimeout(this.emitEvent.bind(this,"remove",t,e),0),!0;return!1},n.prototype.forEachRecord=function(t){var e=0,r=this,n=function(){};return setTimeout(function(){for(var i=r._store.length-1;i>=0;i-=1)e+=1,setTimeout(t(r._store[i]),0);setTimeout(n(e),0)},0),{"finally":function(t){n=t}}},n.prototype.getCount=function(){return this._store.length},n.prototype.removeAll=function(){for(var t=this._store.length-1;t>=0;t-=1)delete this._store[t];this._store=[]},e.exports=n},{"tiny-emitter":3}],2:[function(e,r,n){!function(e){if("object"==typeof n)r.exports=e();else if("function"==typeof t&&t.amd)t(e);else{var i;try{i=window}catch(o){i=self}i.SparkMD5=e()}}(function(t){"use strict";function e(t,e,r,n,i,o){return e=m(m(e,t),m(n,o)),m(e<<i|e>>>32-i,r)}function r(t,r,n,i,o,s,f){return e(r&n|~r&i,t,r,o,s,f)}function n(t,r,n,i,o,s,f){return e(r&i|n&~i,t,r,o,s,f)}function i(t,r,n,i,o,s,f){return e(r^n^i,t,r,o,s,f)}function o(t,r,n,i,o,s,f){return e(n^(r|~i),t,r,o,s,f)}function s(t,e){var s=t[0],f=t[1],h=t[2],u=t[3];s=r(s,f,h,u,e[0],7,-680876936),u=r(u,s,f,h,e[1],12,-389564586),h=r(h,u,s,f,e[2],17,606105819),f=r(f,h,u,s,e[3],22,-1044525330),s=r(s,f,h,u,e[4],7,-176418897),u=r(u,s,f,h,e[5],12,1200080426),h=r(h,u,s,f,e[6],17,-1473231341),f=r(f,h,u,s,e[7],22,-45705983),s=r(s,f,h,u,e[8],7,1770035416),u=r(u,s,f,h,e[9],12,-1958414417),h=r(h,u,s,f,e[10],17,-42063),f=r(f,h,u,s,e[11],22,-1990404162),s=r(s,f,h,u,e[12],7,1804603682),u=r(u,s,f,h,e[13],12,-40341101),h=r(h,u,s,f,e[14],17,-1502002290),f=r(f,h,u,s,e[15],22,1236535329),s=n(s,f,h,u,e[1],5,-165796510),u=n(u,s,f,h,e[6],9,-1069501632),h=n(h,u,s,f,e[11],14,643717713),f=n(f,h,u,s,e[0],20,-373897302),s=n(s,f,h,u,e[5],5,-701558691),u=n(u,s,f,h,e[10],9,38016083),h=n(h,u,s,f,e[15],14,-660478335),f=n(f,h,u,s,e[4],20,-405537848),s=n(s,f,h,u,e[9],5,568446438),u=n(u,s,f,h,e[14],9,-1019803690),h=n(h,u,s,f,e[3],14,-187363961),f=n(f,h,u,s,e[8],20,1163531501),s=n(s,f,h,u,e[13],5,-1444681467),u=n(u,s,f,h,e[2],9,-51403784),h=n(h,u,s,f,e[7],14,1735328473),f=n(f,h,u,s,e[12],20,-1926607734),s=i(s,f,h,u,e[5],4,-378558),u=i(u,s,f,h,e[8],11,-2022574463),h=i(h,u,s,f,e[11],16,1839030562),f=i(f,h,u,s,e[14],23,-35309556),s=i(s,f,h,u,e[1],4,-1530992060),u=i(u,s,f,h,e[4],11,1272893353),h=i(h,u,s,f,e[7],16,-155497632),f=i(f,h,u,s,e[10],23,-1094730640),s=i(s,f,h,u,e[13],4,681279174),u=i(u,s,f,h,e[0],11,-358537222),h=i(h,u,s,f,e[3],16,-722521979),f=i(f,h,u,s,e[6],23,76029189),s=i(s,f,h,u,e[9],4,-640364487),u=i(u,s,f,h,e[12],11,-421815835),h=i(h,u,s,f,e[15],16,530742520),f=i(f,h,u,s,e[2],23,-995338651),s=o(s,f,h,u,e[0],6,-198630844),u=o(u,s,f,h,e[7],10,1126891415),h=o(h,u,s,f,e[14],15,-1416354905),f=o(f,h,u,s,e[5],21,-57434055),s=o(s,f,h,u,e[12],6,1700485571),u=o(u,s,f,h,e[3],10,-1894986606),h=o(h,u,s,f,e[10],15,-1051523),f=o(f,h,u,s,e[1],21,-2054922799),s=o(s,f,h,u,e[8],6,1873313359),u=o(u,s,f,h,e[15],10,-30611744),h=o(h,u,s,f,e[6],15,-1560198380),f=o(f,h,u,s,e[13],21,1309151649),s=o(s,f,h,u,e[4],6,-145523070),u=o(u,s,f,h,e[11],10,-1120210379),h=o(h,u,s,f,e[2],15,718787259),f=o(f,h,u,s,e[9],21,-343485551),t[0]=m(s,t[0]),t[1]=m(f,t[1]),t[2]=m(h,t[2]),t[3]=m(u,t[3])}function f(t){var e,r=[];for(e=0;64>e;e+=4)r[e>>2]=t.charCodeAt(e)+(t.charCodeAt(e+1)<<8)+(t.charCodeAt(e+2)<<16)+(t.charCodeAt(e+3)<<24);return r}function h(t){var e,r=[];for(e=0;64>e;e+=4)r[e>>2]=t[e]+(t[e+1]<<8)+(t[e+2]<<16)+(t[e+3]<<24);return r}function u(t){var e,r,n,i,o,h,u=t.length,a=[1732584193,-271733879,-1732584194,271733878];for(e=64;u>=e;e+=64)s(a,f(t.substring(e-64,e)));for(t=t.substring(e-64),r=t.length,n=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],e=0;r>e;e+=1)n[e>>2]|=t.charCodeAt(e)<<(e%4<<3);if(n[e>>2]|=128<<(e%4<<3),e>55)for(s(a,n),e=0;16>e;e+=1)n[e]=0;return i=8*u,i=i.toString(16).match(/(.*?)(.{0,8})$/),o=parseInt(i[2],16),h=parseInt(i[1],16)||0,n[14]=o,n[15]=h,s(a,n),a}function a(t){var e,r,n,i,o,f,u=t.length,a=[1732584193,-271733879,-1732584194,271733878];for(e=64;u>=e;e+=64)s(a,h(t.subarray(e-64,e)));for(t=u>e-64?t.subarray(e-64):new Uint8Array(0),r=t.length,n=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],e=0;r>e;e+=1)n[e>>2]|=t[e]<<(e%4<<3);if(n[e>>2]|=128<<(e%4<<3),e>55)for(s(a,n),e=0;16>e;e+=1)n[e]=0;return i=8*u,i=i.toString(16).match(/(.*?)(.{0,8})$/),o=parseInt(i[2],16),f=parseInt(i[1],16)||0,n[14]=o,n[15]=f,s(a,n),a}function c(t){var e,r="";for(e=0;4>e;e+=1)r+=b[t>>8*e+4&15]+b[t>>8*e&15];return r}function p(t){var e;for(e=0;e<t.length;e+=1)t[e]=c(t[e]);return t.join("")}function y(t){return/[\u0080-\uFFFF]/.test(t)&&(t=unescape(encodeURIComponent(t))),t}function d(t,e){var r,n=t.length,i=new ArrayBuffer(n),o=new Uint8Array(i);for(r=0;n>r;r+=1)o[r]=t.charCodeAt(r);return e?o:i}function l(t){return String.fromCharCode.apply(null,new Uint8Array(t))}function _(t,e,r){var n=new Uint8Array(t.byteLength+e.byteLength);return n.set(new Uint8Array(t)),n.set(new Uint8Array(e),t.byteLength),r?n:n.buffer}function g(t){var e,r=[],n=t.length;for(e=0;n-1>e;e+=2)r.push(parseInt(t.substr(e,2),16));return String.fromCharCode.apply(String,r)}function v(){this.reset()}var m=function(t,e){return t+e&4294967295},b=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];return"5d41402abc4b2a76b9719d911017c592"!==p(u("hello"))&&(m=function(t,e){var r=(65535&t)+(65535&e),n=(t>>16)+(e>>16)+(r>>16);return n<<16|65535&r}),"undefined"==typeof ArrayBuffer||ArrayBuffer.prototype.slice||!function(){function e(t,e){return t=0|t||0,0>t?Math.max(t+e,0):Math.min(t,e)}ArrayBuffer.prototype.slice=function(r,n){var i,o,s,f,h=this.byteLength,u=e(r,h),a=h;return n!==t&&(a=e(n,h)),u>a?new ArrayBuffer(0):(i=a-u,o=new ArrayBuffer(i),s=new Uint8Array(o),f=new Uint8Array(this,u,i),s.set(f),o)}}(),v.prototype.append=function(t){return this.appendBinary(y(t)),this},v.prototype.appendBinary=function(t){this._buff+=t,this._length+=t.length;var e,r=this._buff.length;for(e=64;r>=e;e+=64)s(this._hash,f(this._buff.substring(e-64,e)));return this._buff=this._buff.substring(e-64),this},v.prototype.end=function(t){var e,r,n=this._buff,i=n.length,o=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(e=0;i>e;e+=1)o[e>>2]|=n.charCodeAt(e)<<(e%4<<3);return this._finish(o,i),r=p(this._hash),t&&(r=g(r)),this.reset(),r},v.prototype.reset=function(){return this._buff="",this._length=0,this._hash=[1732584193,-271733879,-1732584194,271733878],this},v.prototype.getState=function(){return{buff:this._buff,length:this._length,hash:this._hash}},v.prototype.setState=function(t){return this._buff=t.buff,this._length=t.length,this._hash=t.hash,this},v.prototype.destroy=function(){delete this._hash,delete this._buff,delete this._length},v.prototype._finish=function(t,e){var r,n,i,o=e;if(t[o>>2]|=128<<(o%4<<3),o>55)for(s(this._hash,t),o=0;16>o;o+=1)t[o]=0;r=8*this._length,r=r.toString(16).match(/(.*?)(.{0,8})$/),n=parseInt(r[2],16),i=parseInt(r[1],16)||0,t[14]=n,t[15]=i,s(this._hash,t)},v.hash=function(t,e){return v.hashBinary(y(t),e)},v.hashBinary=function(t,e){var r=u(t),n=p(r);return e?g(n):n},v.ArrayBuffer=function(){this.reset()},v.ArrayBuffer.prototype.append=function(t){var e,r=_(this._buff.buffer,t,!0),n=r.length;for(this._length+=t.byteLength,e=64;n>=e;e+=64)s(this._hash,h(r.subarray(e-64,e)));return this._buff=n>e-64?new Uint8Array(r.buffer.slice(e-64)):new Uint8Array(0),this},v.ArrayBuffer.prototype.end=function(t){var e,r,n=this._buff,i=n.length,o=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(e=0;i>e;e+=1)o[e>>2]|=n[e]<<(e%4<<3);return this._finish(o,i),r=p(this._hash),t&&(r=g(r)),this.reset(),r},v.ArrayBuffer.prototype.reset=function(){return this._buff=new Uint8Array(0),this._length=0,this._hash=[1732584193,-271733879,-1732584194,271733878],this},v.ArrayBuffer.prototype.getState=function(){var t=v.prototype.getState.call(this);return t.buff=l(t.buff),t},v.ArrayBuffer.prototype.setState=function(t){return t.buff=d(t.buff,!0),v.prototype.setState.call(this,t)},v.ArrayBuffer.prototype.destroy=v.prototype.destroy,v.ArrayBuffer.prototype._finish=v.prototype._finish,v.ArrayBuffer.hash=function(t,e){var r=a(new Uint8Array(t)),n=p(r);return e?g(n):n},v})},{}],3:[function(t,e,r){function n(){}n.prototype={on:function(t,e,r){var n=this.e||(this.e={});return(n[t]||(n[t]=[])).push({fn:e,ctx:r}),this},once:function(t,e,r){function n(){i.off(t,n),e.apply(r,arguments)}var i=this;return n._=e,this.on(t,n,r)},emit:function(t){var e=[].slice.call(arguments,1),r=((this.e||(this.e={}))[t]||[]).slice(),n=0,i=r.length;for(n;i>n;n++)r[n].fn.apply(r[n].ctx,e);return this},off:function(t,e){var r=this.e||(this.e={}),n=r[t],i=[];if(n&&e)for(var o=0,s=n.length;s>o;o++)n[o].fn!==e&&n[o].fn._!==e&&i.push(n[o]);return i.length?r[t]=i:delete r[t],this}},e.exports=n},{}],4:[function(t,e,r){function n(){return Math.floor(65535*Math.random()+1)}function i(t,e){var r=new Uint8Array(t.byteLength+e.byteLength);return r.set(new Uint8Array(t),0),r.set(new Uint8Array(e),t.byteLength),r.buffer}function o(t){var e,r,i;if("object"!=typeof t)throw new Error("metadata object must be passed in");if(!t.name)throw new Error("no name specified");if(!t.mimeType)throw new Error("no mimeType specified");if(!t.chunkSize)throw new Error("no chunkSize specified");if(t.arrayBuffer&&("function"!=typeof t.arrayBuffer.toString||"[object ArrayBuffer]"!==t.arrayBuffer.toString()))throw new Error("arrayBuffer must be an actual ArrayBuffer object");if(!t.arrayBuffer&&!t.uid)throw new Error("arrayBuffer or uid must be specified");if(t.arrayBuffer&&t.uid)throw new Error("cannot manually assign uid");t.uid||(t.uid=n()),t.arrayBuffer?(r=t.chunkSize-h,e=Math.ceil(t.arrayBuffer.byteLength/r),i=f.ArrayBuffer.hash(t.arrayBuffer)):i=t.checksum,this.uid=t.uid,this.name=t.name,this.mimeType=t.mimeType,this.chunkSize=t.chunkSize,this.payloadSize=r,this.reservedSize=h,this.fileSize=t.arrayBuffer?t.arrayBuffer.byteLength:0,this.totalChunks=e?e:t.totalChunks?t.totalChunks:0,this.checksum=i,this.generatedChecksum,this.chunksProcessed=0,this._arrayBuffer=t.arrayBuffer?t.arrayBuffer:null,this._chunks=[],t.arrayBuffer||c.addRecord(this)}var s=t("array-keys"),f=t("spark-md5"),h=8,u=0,a=4,c=new s({identifier:"uid"});o.prototype.getMetadata=function(){return{uid:this.uid,name:this.name,mimeType:this.mimeType,chunkSize:this.chunkSize,payloadSize:this.payloadSize,reservedSize:this.reservedSize,fileSize:this.fileSize,totalChunks:this.totalChunks,checksum:this.checksum}},o.prototype.getFile=function(t){this._arrayBuffer=this._chunks[0];for(var e=1;e<this._chunks.length;e+=1)this._arrayBuffer=i(this._arrayBuffer,this._chunks[e]);this.generatedChecksum=f.ArrayBuffer.hash(this._arrayBuffer),t(this._arrayBuffer,this.generatedChecksum)},o.prototype.onChunkReceived=function(){},o.prototype.onCompleted=function(){},o.prototype.forEachChunk=function(t,e){for(var r,n=0;n<this.totalChunks;n+=1)(r=this.getChunk())?t(r,this.chunksProcessed):e()},o.prototype.getChunk=function(t){var e=this.chunksProcessed+1;if("number"==typeof t){if(0>=t)return;e=t}else t=void 0;var r=this.payloadSize*e-this.payloadSize;if(!(r>=this.fileSize)){var n=r+this.payloadSize;n=n>this.fileSize?this.fileSize:n;var i=this._arrayBuffer.slice(r,n),s=o.__pack(this.uid,e,i);return t||(this.chunksProcessed+=1),s}},o.prototype.clearData=function(){c.removeRecord(this.uid),delete this._arrayBuffer,delete this},o.__pack=function(t,e,r){var n=new ArrayBuffer(r.byteLength+h),i=new DataView(n);return i.setInt32(u,t),i.setInt32(a,e),new Uint8Array(n,0,n.byteLength).set(new Uint8Array(r),h),n},o.__unpack=function(t){var e=new DataView(t),r=e.getInt32(u),n=e.getInt32(a),i=new Uint8Array(t.slice(h));return[r,n,i]},o.submitChunk=function(t){var e=this.__unpack(t),r=e[0],n=e[1],i=e[2],o=c.getRecord(r);return o?(o._chunks[n-1]=i,o.chunksProcessed+=1,o.onChunkReceived(i,n),o.chunksProcessed===o.totalChunks&&o.onCompleted(),c.addRecord(o),!0):!1},e.exports=o},{"array-keys":1,"spark-md5":2}]},{},[4])(4)});
{
"name": "binary-data-chunking",
"version": "1.2.0",
"version": "2.0.0",
"description": "A small library that simplifies sending large amounts of chunked binary data",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -38,5 +38,5 @@ # binary-data-chunking

file.getFileSize(); // returns actual file size based on ArrayBuffer
file.getTotalChunks(); // total number of chunks that will be sent (based on file size and chunk size)
file.getChecksum(); // checksum based on ArrayBuffer
file.fileSize; // returns actual file size based on ArrayBuffer
file.totalChunks; // total number of chunks that will be sent (based on file size and chunk size)
file.checksum; // checksum based on ArrayBuffer

@@ -48,3 +48,3 @@ file.getMetadata(); // to be sent to other end to initialize the transfer, used to initialize a BDC instance on the receiving end.

// must also have an instance created based on the information returned from `file.getMetadata()`
file.getPosition(); // returns the current number if chunks given from `getChunk()`
file.chunksProcessed; // returns the current number if chunks given from `getChunk()`
file.clearData(); // removes arrayBuffer data and all stored information

@@ -70,9 +70,10 @@ ```

BDC.submitChunk(chunk);
file.generatedChecksum; // contains then generated checksum once a transfer is complete
```
With your `file` object instance, you can access the methods like `getNumChunks()`, `getFileSize()`, `getMetadata()`, `getChunk()` and once completed `getChecksum()`.
With your `file` object instance, you can access the methods like `totalChunks`, `fileSize`, `getMetadata()`, `getChunk()` and once completed `getFile()`.
## 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).

@@ -24,49 +24,47 @@ var ArrayKeys = require('array-keys');

function BDC(metadata) {
function BDC(md) {
var totalChunks, payloadSize, checksum;
if (typeof metadata !== 'object') { throw new Error('metadata object must be passed in'); }
if (! metadata.name) { throw new Error('no name specified'); }
if (! metadata.mimeType) { throw new Error('no mimeType specified'); }
if (! metadata.chunkSize) { throw new Error('no chunkSize specified'); }
if ((metadata.arrayBuffer) &&
((typeof metadata.arrayBuffer.toString !== 'function') || (metadata.arrayBuffer.toString() !== '[object ArrayBuffer]'))) {
if (typeof md !== 'object') { throw new Error('metadata object must be passed in'); }
if (! md.name) { throw new Error('no name specified'); }
if (! md.mimeType) { throw new Error('no mimeType specified'); }
if (! md.chunkSize) { throw new Error('no chunkSize specified'); }
if ((md.arrayBuffer) &&
((typeof md.arrayBuffer.toString !== 'function') || (md.arrayBuffer.toString() !== '[object ArrayBuffer]'))) {
throw new Error('arrayBuffer must be an actual ArrayBuffer object');
} else if ((! metadata.arrayBuffer) && (! metadata.uid)) {
} else if ((! md.arrayBuffer) && (! md.uid)) {
throw new Error('arrayBuffer or uid must be specified');
} else if ((metadata.arrayBuffer) && (metadata.uid)) {
} else if ((md.arrayBuffer) && (md.uid)) {
throw new Error('cannot manually assign uid');
}
if (! metadata.uid) {
if (! md.uid) {
// we have an arrayBuffer, so we create a UID
metadata.uid = genUID();
md.uid = genUID();
}
if (metadata.arrayBuffer) {
if (md.arrayBuffer) {
// this is a sender
// we have an arrayBuffer, so let's calc total number of chunks
payloadSize = (metadata.chunkSize - RESERVED_BYTES)
totalChunks = Math.ceil(metadata.arrayBuffer.byteLength / payloadSize);
checksum = SparkMD5.ArrayBuffer.hash(metadata.arrayBuffer);
payloadSize = (md.chunkSize - RESERVED_BYTES)
totalChunks = Math.ceil(md.arrayBuffer.byteLength / payloadSize);
checksum = SparkMD5.ArrayBuffer.hash(md.arrayBuffer);
} else {
checksum = metadata.checksum;
checksum = md.checksum;
}
this.uid = metadata.uid;
this._metadata = {
uid: metadata.uid,
name: metadata.name,
mimeType: metadata.mimeType,
chunkSize: metadata.chunkSize,
payloadSize: payloadSize,
reservedSize: RESERVED_BYTES,
fileSize: (metadata.arrayBuffer) ? metadata.arrayBuffer.byteLength : 0,
totalChunks: (totalChunks) ? totalChunks : (metadata.totalChunks) ? metadata.totalChunks : 0,
checksum: checksum
// TODO calc checksums for every chunk
};
this.uid = md.uid;
this.name = md.name;
this.mimeType = md.mimeType;
this.chunkSize = md.chunkSize;
this.payloadSize = payloadSize;
this.reservedSize = RESERVED_BYTES;
this.fileSize = (md.arrayBuffer) ? md.arrayBuffer.byteLength : 0;
this.totalChunks = (totalChunks) ? totalChunks : (md.totalChunks) ? md.totalChunks : 0;
this.checksum = checksum;
this.generatedChecksum;
this.chunksProcessed = 0;
// TODO calc checksums for every chunk
this._arrayBuffer = (metadata.arrayBuffer) ? metadata.arrayBuffer : null,
this._chunksProcessed = 0;
this._arrayBuffer = (md.arrayBuffer) ? md.arrayBuffer : null,
this._chunks = [];
if (! metadata.arrayBuffer) {
if (! md.arrayBuffer) {
fileChunks.addRecord(this);

@@ -77,25 +75,15 @@ }

BDC.prototype.getMetadata = function () {
return this._metadata;
return {
uid: this.uid,
name: this.name,
mimeType: this.mimeType,
chunkSize: this.chunkSize,
payloadSize: this.payloadSize,
reservedSize: this.reservedSize,
fileSize: this.fileSize,
totalChunks: this.totalChunks,
checksum: this.checksum
};
};
BDC.prototype.getUID = function () {
return this.uid;
};
BDC.prototype.getFileSize = function () {
return this._metadata.fileSize;
};
BDC.prototype.getTotalChunks = function () {
return this._metadata.totalChunks;
};
BDC.prototype.getChecksum = function () {
return this._metadata.checksum;
};
BDC.prototype.getGeneratedChecksum = function () {
return this._metadata.generatedChecksum;
};
BDC.prototype.getFile = function (cb) {

@@ -107,4 +95,4 @@ this._arrayBuffer = this._chunks[0];

}
this._metadata.generatedChecksum = SparkMD5.ArrayBuffer.hash(this._arrayBuffer);
cb(this._arrayBuffer, this._metadata.generatedChecksum);
this.generatedChecksum = SparkMD5.ArrayBuffer.hash(this._arrayBuffer);
cb(this._arrayBuffer, this.generatedChecksum);
};

@@ -116,11 +104,7 @@

BDC.prototype.getPosition = function () {
return this._chunksProcessed;
};
BDC.prototype.forEachChunk = function (cb, end) {
var chunk;
for (var i = 0; i < this._metadata.totalChunks; i += 1) {
for (var i = 0; i < this.totalChunks; i += 1) {
if (chunk = this.getChunk()) {
cb(chunk, this.getPosition());
cb(chunk, this.chunksProcessed);
} else {

@@ -133,3 +117,3 @@ end();

BDC.prototype.getChunk = function (num) {
var _adjustedPosition = this._chunksProcessed + 1
var _adjustedPosition = this.chunksProcessed + 1
if (typeof num === 'number') {

@@ -145,14 +129,14 @@ if (num <= 0) {

var start = (this._metadata.payloadSize * _adjustedPosition) - this._metadata.payloadSize;
if (start >= this._metadata.fileSize) {
var start = (this.payloadSize * _adjustedPosition) - this.payloadSize;
if (start >= this.fileSize) {
return undefined;
}
var end = start + this._metadata.payloadSize;
end = (end > this._metadata.fileSize) ? this._metadata.fileSize : end;
var end = start + this.payloadSize;
end = (end > this.fileSize) ? this.fileSize : end;
var payload = this._arrayBuffer.slice(start, end);
var chunk = BDC.__pack(this._metadata.uid, _adjustedPosition, payload);
var chunk = BDC.__pack(this.uid, _adjustedPosition, payload);
if (! num) {
this._chunksProcessed += 1;
this.chunksProcessed += 1;
}

@@ -166,3 +150,2 @@

delete this._arrayBuffer;
delete this._metadata;
delete this;

@@ -204,6 +187,6 @@ };

file._chunks[pos - 1] = ab;
file._chunksProcessed += 1;
file.chunksProcessed += 1;
file.onChunkReceived(ab, pos);
if (file.getPosition() === file.getTotalChunks()) {
if (file.chunksProcessed === file.totalChunks) {
// TODO

@@ -210,0 +193,0 @@ // - add merged ArrayBuffer

@@ -24,3 +24,3 @@ if (typeof define !== 'function') {

env.totalChunks = 67;
test.assertType(env.sender.getMetadata, 'function');
test.assertType(env.sender.getFile, 'function');
});

@@ -32,7 +32,7 @@ },

run: function (env, test) {
test.assertAnd((env.sender.getUID() < 65536) && (env.sender.getUID() > 0), true);
test.assertAnd((env.sender.uid < 65536) && (env.sender.uid > 0), true);
// console.log('UID: ' + env.sender.getUID());
test.assertAnd(env.sender.getChecksum(), 'd55bddf8d62910879ed9f605522149a8');
test.assertAnd(env.sender.getFileSize(), 1055736);
test.assert(env.sender.getTotalChunks(), env.totalChunks);
test.assertAnd(env.sender.checksum, 'd55bddf8d62910879ed9f605522149a8');
test.assertAnd(env.sender.fileSize, 1055736);
test.assert(env.sender.totalChunks, env.totalChunks);
}

@@ -44,3 +44,3 @@ },

env.receiver = new env.BDC(env.sender.getMetadata());
test.assert(env.receiver.getTotalChunks(), env.totalChunks);
test.assert(env.receiver.totalChunks, env.totalChunks);
}

@@ -54,3 +54,3 @@ },

env.BDC.submitChunk(chunk);
test.assert(env.sender.getPosition(), 1);
test.assert(env.sender.chunksProcessed, 1);
}

@@ -61,3 +61,3 @@ },

run: function (env, test) {
test.assert(env.receiver.getPosition(), 1);
test.assert(env.receiver.chunksProcessed, 1);
}

@@ -75,3 +75,3 @@ },

// done
test.assert(env.sender.getPosition(), env.totalChunks);
test.assert(env.sender.chunksProcessed, env.totalChunks);
});

@@ -83,12 +83,6 @@ }

run: function (env, test) {
test.assert(env.receiver.getPosition(), 67);
test.assert(env.receiver.chunksProcessed, 67);
}
},
{
desc: '# check receiver position number',
run: function (env, test) {
test.assert(env.receiver.getPosition(), 67);
}
},
{
desc: '# get file and get md5sum',

@@ -98,3 +92,3 @@ run: function (env, test) {

test.assertAnd(file.byteLength, env.sender._arrayBuffer.byteLength);
test.assert(checksum, env.sender.getChecksum());
test.assert(checksum, env.sender.checksum);
});

@@ -101,0 +95,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc