You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.0 KiB

4 years ago
  1. /*global Blob,File*/
  2. /**
  3. * Module requirements
  4. */
  5. var isArray = require('isarray');
  6. var isBuf = require('./is-buffer');
  7. var toString = Object.prototype.toString;
  8. var withNativeBlob = typeof Blob === 'function' || (typeof Blob !== 'undefined' && toString.call(Blob) === '[object BlobConstructor]');
  9. var withNativeFile = typeof File === 'function' || (typeof File !== 'undefined' && toString.call(File) === '[object FileConstructor]');
  10. /**
  11. * Replaces every Buffer | ArrayBuffer in packet with a numbered placeholder.
  12. * Anything with blobs or files should be fed through removeBlobs before coming
  13. * here.
  14. *
  15. * @param {Object} packet - socket.io event packet
  16. * @return {Object} with deconstructed packet and list of buffers
  17. * @api public
  18. */
  19. exports.deconstructPacket = function(packet) {
  20. var buffers = [];
  21. var packetData = packet.data;
  22. var pack = packet;
  23. pack.data = _deconstructPacket(packetData, buffers);
  24. pack.attachments = buffers.length; // number of binary 'attachments'
  25. return {packet: pack, buffers: buffers};
  26. };
  27. function _deconstructPacket(data, buffers) {
  28. if (!data) return data;
  29. if (isBuf(data)) {
  30. var placeholder = { _placeholder: true, num: buffers.length };
  31. buffers.push(data);
  32. return placeholder;
  33. } else if (isArray(data)) {
  34. var newData = new Array(data.length);
  35. for (var i = 0; i < data.length; i++) {
  36. newData[i] = _deconstructPacket(data[i], buffers);
  37. }
  38. return newData;
  39. } else if (typeof data === 'object' && !(data instanceof Date)) {
  40. var newData = {};
  41. for (var key in data) {
  42. newData[key] = _deconstructPacket(data[key], buffers);
  43. }
  44. return newData;
  45. }
  46. return data;
  47. }
  48. /**
  49. * Reconstructs a binary packet from its placeholder packet and buffers
  50. *
  51. * @param {Object} packet - event packet with placeholders
  52. * @param {Array} buffers - binary buffers to put in placeholder positions
  53. * @return {Object} reconstructed packet
  54. * @api public
  55. */
  56. exports.reconstructPacket = function(packet, buffers) {
  57. packet.data = _reconstructPacket(packet.data, buffers);
  58. packet.attachments = undefined; // no longer useful
  59. return packet;
  60. };
  61. function _reconstructPacket(data, buffers) {
  62. if (!data) return data;
  63. if (data && data._placeholder) {
  64. return buffers[data.num]; // appropriate buffer (should be natural order anyway)
  65. } else if (isArray(data)) {
  66. for (var i = 0; i < data.length; i++) {
  67. data[i] = _reconstructPacket(data[i], buffers);
  68. }
  69. } else if (typeof data === 'object') {
  70. for (var key in data) {
  71. data[key] = _reconstructPacket(data[key], buffers);
  72. }
  73. }
  74. return data;
  75. }
  76. /**
  77. * Asynchronously removes Blobs or Files from data via
  78. * FileReader's readAsArrayBuffer method. Used before encoding
  79. * data as msgpack. Calls callback with the blobless data.
  80. *
  81. * @param {Object} data
  82. * @param {Function} callback
  83. * @api private
  84. */
  85. exports.removeBlobs = function(data, callback) {
  86. function _removeBlobs(obj, curKey, containingObject) {
  87. if (!obj) return obj;
  88. // convert any blob
  89. if ((withNativeBlob && obj instanceof Blob) ||
  90. (withNativeFile && obj instanceof File)) {
  91. pendingBlobs++;
  92. // async filereader
  93. var fileReader = new FileReader();
  94. fileReader.onload = function() { // this.result == arraybuffer
  95. if (containingObject) {
  96. containingObject[curKey] = this.result;
  97. }
  98. else {
  99. bloblessData = this.result;
  100. }
  101. // if nothing pending its callback time
  102. if(! --pendingBlobs) {
  103. callback(bloblessData);
  104. }
  105. };
  106. fileReader.readAsArrayBuffer(obj); // blob -> arraybuffer
  107. } else if (isArray(obj)) { // handle array
  108. for (var i = 0; i < obj.length; i++) {
  109. _removeBlobs(obj[i], i, obj);
  110. }
  111. } else if (typeof obj === 'object' && !isBuf(obj)) { // and object
  112. for (var key in obj) {
  113. _removeBlobs(obj[key], key, obj);
  114. }
  115. }
  116. }
  117. var pendingBlobs = 0;
  118. var bloblessData = data;
  119. _removeBlobs(bloblessData);
  120. if (!pendingBlobs) {
  121. callback(bloblessData);
  122. }
  123. };