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
3.7 KiB

7 years ago
  1. /*global Blob,File*/
  2. /**
  3. * Module requirements
  4. */
  5. var isArray = require('isarray');
  6. var isBuf = require('./is-buffer');
  7. /**
  8. * Replaces every Buffer | ArrayBuffer in packet with a numbered placeholder.
  9. * Anything with blobs or files should be fed through removeBlobs before coming
  10. * here.
  11. *
  12. * @param {Object} packet - socket.io event packet
  13. * @return {Object} with deconstructed packet and list of buffers
  14. * @api public
  15. */
  16. exports.deconstructPacket = function(packet){
  17. var buffers = [];
  18. var packetData = packet.data;
  19. function _deconstructPacket(data) {
  20. if (!data) return data;
  21. if (isBuf(data)) {
  22. var placeholder = { _placeholder: true, num: buffers.length };
  23. buffers.push(data);
  24. return placeholder;
  25. } else if (isArray(data)) {
  26. var newData = new Array(data.length);
  27. for (var i = 0; i < data.length; i++) {
  28. newData[i] = _deconstructPacket(data[i]);
  29. }
  30. return newData;
  31. } else if ('object' == typeof data && !(data instanceof Date)) {
  32. var newData = {};
  33. for (var key in data) {
  34. newData[key] = _deconstructPacket(data[key]);
  35. }
  36. return newData;
  37. }
  38. return data;
  39. }
  40. var pack = packet;
  41. pack.data = _deconstructPacket(packetData);
  42. pack.attachments = buffers.length; // number of binary 'attachments'
  43. return {packet: pack, buffers: buffers};
  44. };
  45. /**
  46. * Reconstructs a binary packet from its placeholder packet and buffers
  47. *
  48. * @param {Object} packet - event packet with placeholders
  49. * @param {Array} buffers - binary buffers to put in placeholder positions
  50. * @return {Object} reconstructed packet
  51. * @api public
  52. */
  53. exports.reconstructPacket = function(packet, buffers) {
  54. var curPlaceHolder = 0;
  55. function _reconstructPacket(data) {
  56. if (data && data._placeholder) {
  57. var buf = buffers[data.num]; // appropriate buffer (should be natural order anyway)
  58. return buf;
  59. } else if (isArray(data)) {
  60. for (var i = 0; i < data.length; i++) {
  61. data[i] = _reconstructPacket(data[i]);
  62. }
  63. return data;
  64. } else if (data && 'object' == typeof data) {
  65. for (var key in data) {
  66. data[key] = _reconstructPacket(data[key]);
  67. }
  68. return data;
  69. }
  70. return data;
  71. }
  72. packet.data = _reconstructPacket(packet.data);
  73. packet.attachments = undefined; // no longer useful
  74. return packet;
  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 ((global.Blob && obj instanceof Blob) ||
  90. (global.File && 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 (obj && 'object' == typeof obj && !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. };