Node-Red configuration
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.

binary.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.reconstructPacket = exports.deconstructPacket = void 0;
  4. const is_binary_js_1 = require("./is-binary.js");
  5. /**
  6. * Replaces every Buffer | ArrayBuffer | Blob | File in packet with a numbered placeholder.
  7. *
  8. * @param {Object} packet - socket.io event packet
  9. * @return {Object} with deconstructed packet and list of buffers
  10. * @public
  11. */
  12. function deconstructPacket(packet) {
  13. const buffers = [];
  14. const packetData = packet.data;
  15. const pack = packet;
  16. pack.data = _deconstructPacket(packetData, buffers);
  17. pack.attachments = buffers.length; // number of binary 'attachments'
  18. return { packet: pack, buffers: buffers };
  19. }
  20. exports.deconstructPacket = deconstructPacket;
  21. function _deconstructPacket(data, buffers) {
  22. if (!data)
  23. return data;
  24. if ((0, is_binary_js_1.isBinary)(data)) {
  25. const placeholder = { _placeholder: true, num: buffers.length };
  26. buffers.push(data);
  27. return placeholder;
  28. }
  29. else if (Array.isArray(data)) {
  30. const newData = new Array(data.length);
  31. for (let i = 0; i < data.length; i++) {
  32. newData[i] = _deconstructPacket(data[i], buffers);
  33. }
  34. return newData;
  35. }
  36. else if (typeof data === "object" && !(data instanceof Date)) {
  37. const newData = {};
  38. for (const key in data) {
  39. if (Object.prototype.hasOwnProperty.call(data, key)) {
  40. newData[key] = _deconstructPacket(data[key], buffers);
  41. }
  42. }
  43. return newData;
  44. }
  45. return data;
  46. }
  47. /**
  48. * Reconstructs a binary packet from its placeholder packet and buffers
  49. *
  50. * @param {Object} packet - event packet with placeholders
  51. * @param {Array} buffers - binary buffers to put in placeholder positions
  52. * @return {Object} reconstructed packet
  53. * @public
  54. */
  55. function reconstructPacket(packet, buffers) {
  56. packet.data = _reconstructPacket(packet.data, buffers);
  57. delete packet.attachments; // no longer useful
  58. return packet;
  59. }
  60. exports.reconstructPacket = reconstructPacket;
  61. function _reconstructPacket(data, buffers) {
  62. if (!data)
  63. return data;
  64. if (data && data._placeholder === true) {
  65. const isIndexValid = typeof data.num === "number" &&
  66. data.num >= 0 &&
  67. data.num < buffers.length;
  68. if (isIndexValid) {
  69. return buffers[data.num]; // appropriate buffer (should be natural order anyway)
  70. }
  71. else {
  72. throw new Error("illegal attachments");
  73. }
  74. }
  75. else if (Array.isArray(data)) {
  76. for (let i = 0; i < data.length; i++) {
  77. data[i] = _reconstructPacket(data[i], buffers);
  78. }
  79. }
  80. else if (typeof data === "object") {
  81. for (const key in data) {
  82. if (Object.prototype.hasOwnProperty.call(data, key)) {
  83. data[key] = _reconstructPacket(data[key], buffers);
  84. }
  85. }
  86. }
  87. return data;
  88. }