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