File: source/data-process.js

  1. /**
  2. * <blockquote class="info">
  3. * Currently, we do not support Blob or ArrayBuffer data type of transfers, but we
  4. * will look into implementing it in the future.
  5. * </blockquote>
  6. * The list of supported data transfer data types.
  7. * @attribute DATA_TRANSFER_DATA_TYPE
  8. * @param {String} BINARY_STRING <small>Value <code>"binaryString"</code></small>
  9. * The value of the transfer type that sends all data packets as string
  10. * (or converts them into string) when transferring data over the Datachannel connection.
  11. * @type JSON
  12. * @readOnly
  13. * @for Skylink
  14. * @since 0.1.0
  15. */
  16. Skylink.prototype.DATA_TRANSFER_DATA_TYPE = {
  17. BINARY_STRING: 'binaryString',
  18. ARRAY_BUFFER: 'arrayBuffer',
  19. BLOB: 'blob'
  20. };
  21.  
  22. /**
  23. * Stores the data chunk size for Blob transfers.
  24. * @attribute _CHUNK_FILE_SIZE
  25. * @type Number
  26. * @private
  27. * @readOnly
  28. * @for Skylink
  29. * @since 0.5.2
  30. */
  31. Skylink.prototype._CHUNK_FILE_SIZE = 49152;
  32.  
  33. /**
  34. * Stores the data chunk size for Blob transfers transferring from/to
  35. * Firefox browsers due to limitation tested in the past in some PCs (linx predominatly).
  36. * @attribute _MOZ_CHUNK_FILE_SIZE
  37. * @type Number
  38. * @private
  39. * @readOnly
  40. * @for Skylink
  41. * @since 0.5.2
  42. */
  43. Skylink.prototype._MOZ_CHUNK_FILE_SIZE = 12288;
  44.  
  45. /**
  46. * Stores the data chunk size for data URI string transfers.
  47. * @attribute _CHUNK_DATAURL_SIZE
  48. * @type Number
  49. * @private
  50. * @readOnly
  51. * @for Skylink
  52. * @since 0.5.2
  53. */
  54. Skylink.prototype._CHUNK_DATAURL_SIZE = 1212;
  55.  
  56. /**
  57. * Function that converts Base64 string into Blob object.
  58. * This is referenced from devnull69@stackoverflow.com #6850276.
  59. * @method _base64ToBlob
  60. * @private
  61. * @for Skylink
  62. * @since 0.1.0
  63. */
  64. Skylink.prototype._base64ToBlob = function(dataURL) {
  65. var byteString = atob(dataURL.replace(/\s\r\n/g, ''));
  66. // write the bytes of the string to an ArrayBuffer
  67. var ab = new ArrayBuffer(byteString.length);
  68. var ia = new Uint8Array(ab);
  69. for (var j = 0; j < byteString.length; j++) {
  70. ia[j] = byteString.charCodeAt(j);
  71. }
  72. // write the ArrayBuffer to a blob, and you're done
  73. return new Blob([ab]);
  74. };
  75.  
  76. /**
  77. * Function that converts a Blob object into Base64 string.
  78. * @method _blobToBase64
  79. * @private
  80. * @for Skylink
  81. * @since 0.1.0
  82. */
  83. Skylink.prototype._blobToBase64 = function(data, callback) {
  84. var fileReader = new FileReader();
  85. fileReader.onload = function() {
  86. // Load Blob as dataurl base64 string
  87. var base64BinaryString = fileReader.result.split(',')[1];
  88. callback(base64BinaryString);
  89. };
  90. fileReader.readAsDataURL(data);
  91. };
  92.  
  93. /**
  94. * Function that chunks Blob object based on the data chunk size provided.
  95. * If provided Blob object size is lesser than or equals to the chunk size, it should return an array
  96. * of length of <code>1</code>.
  97. * @method _chunkBlobData
  98. * @private
  99. * @for Skylink
  100. * @since 0.5.2
  101. */
  102. Skylink.prototype._chunkBlobData = function(blob, chunkSize) {
  103. var chunksArray = [];
  104. var startCount = 0;
  105. var endCount = 0;
  106. var blobByteSize = blob.size;
  107.  
  108. if (blobByteSize > chunkSize) {
  109. // File Size greater than Chunk size
  110. while ((blobByteSize - 1) > endCount) {
  111. endCount = startCount + chunkSize;
  112. chunksArray.push(blob.slice(startCount, endCount));
  113. startCount += chunkSize;
  114. }
  115. if ((blobByteSize - (startCount + 1)) > 0) {
  116. chunksArray.push(blob.slice(startCount, blobByteSize - 1));
  117. }
  118. } else {
  119. // File Size below Chunk size
  120. chunksArray.push(blob);
  121. }
  122. return chunksArray;
  123. };
  124.  
  125. /**
  126. * Function that chunks large string into string chunks based on the data chunk size provided.
  127. * If provided string length is lesser than or equals to the chunk size, it should return an array
  128. * of length of <code>1</code>.
  129. * @method _chunkDataURL
  130. * @private
  131. * @for Skylink
  132. * @since 0.6.1
  133. */
  134. Skylink.prototype._chunkDataURL = function(dataURL, chunkSize) {
  135. var outputStr = dataURL; //encodeURIComponent(dataURL);
  136. var dataURLArray = [];
  137. var startCount = 0;
  138. var endCount = 0;
  139. var dataByteSize = dataURL.size || dataURL.length;
  140.  
  141. if (dataByteSize > chunkSize) {
  142. // File Size greater than Chunk size
  143. while ((dataByteSize - 1) > endCount) {
  144. endCount = startCount + chunkSize;
  145. dataURLArray.push(outputStr.slice(startCount, endCount));
  146. startCount += chunkSize;
  147. }
  148. if ((dataByteSize - (startCount + 1)) > 0) {
  149. chunksArray.push(outputStr.slice(startCount, dataByteSize - 1));
  150. }
  151. } else {
  152. // File Size below Chunk size
  153. dataURLArray.push(outputStr);
  154. }
  155.  
  156. return dataURLArray;
  157. };
  158.  
  159. /**
  160. * Function that assembles the data string chunks into a large string.
  161. * @method _assembleDataURL
  162. * @private
  163. * @for Skylink
  164. * @since 0.6.1
  165. */
  166. Skylink.prototype._assembleDataURL = function(dataURLArray) {
  167. var outputStr = '';
  168.  
  169. for (var i = 0; i < dataURLArray.length; i++) {
  170. try {
  171. outputStr += dataURLArray[i];
  172. } catch (error) {
  173. console.error('Malformed', i, dataURLArray[i]);
  174. }
  175. }
  176.  
  177. return outputStr;
  178. };