binary.js (1385B)
1 /* 2 * A socket.io encoder and decoder written in JavaScript complying with version 4 3 * of socket.io-protocol. Used by socket.io and socket.io-client. 4 * 5 * Copyright (c) 2014 Guillermo Rauch <guillermo@learnboost.com> 6 * 7 * This source code is licensed under the MIT license found in the 8 * LICENSE file in the root directory of the library source tree. 9 * 10 * https://github.com/socketio/socket.io-parser 11 */ 12 13 "use strict"; 14 15 /** 16 * Reconstructs a binary packet from its placeholder packet and buffers 17 * 18 * @param {object} packet - event packet with placeholders 19 * @param {Array} buffers - binary buffers to put in placeholder positions 20 * @return {object} reconstructed packet 21 * @public 22 */ 23 24 exports.reconstructPacket = function (packet, buffers) { 25 packet.data = _reconstructPacket(packet.data, buffers); 26 packet.attachments = undefined; // no longer useful 27 return packet; 28 }; 29 30 function _reconstructPacket(data, buffers) { 31 if (!data) { 32 return data; 33 } 34 35 if (data && data._placeholder) { 36 return buffers[data.num]; // appropriate buffer (should be natural order anyway) 37 } else if (Array.isArray(data)) { 38 for (let i = 0; i < data.length; i++) { 39 data[i] = _reconstructPacket(data[i], buffers); 40 } 41 } else if (typeof data === "object") { 42 for (const key in data) { 43 data[key] = _reconstructPacket(data[key], buffers); 44 } 45 } 46 47 return data; 48 }