mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
The binary detection was moved from the parser to the client/server in
[1], in order to allow the user to skip the binary detection for huge
JSON payloads.
```js
socket.binary(false).emit(...);
```
The binary detection is needed in the default parser, because the
payload is encoded with JSON.stringify(), which does not support binary
content (ArrayBuffer, Blob, ...).
But other parsers (like [2] or [3]) do not need this check, so we'll
move the binary detection back here and remove the socket.binary()
method, as this use case is now covered by the ability to provide your
own parser.
Note: the hasBinary method was copied from [4].
[1]: f44256c523
[2]: https://github.com/darrachequesne/socket.io-msgpack-parser
[3]: https://github.com/darrachequesne/socket.io-json-parser
[4]: https://github.com/darrachequesne/has-binary
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { isBinary } from "./is-binary";
|
|
|
|
/**
|
|
* Replaces every Buffer | ArrayBuffer | Blob | File in packet with a numbered placeholder.
|
|
*
|
|
* @param {Object} packet - socket.io event packet
|
|
* @return {Object} with deconstructed packet and list of buffers
|
|
* @public
|
|
*/
|
|
|
|
export function deconstructPacket(packet) {
|
|
const buffers = [];
|
|
const packetData = packet.data;
|
|
const pack = packet;
|
|
pack.data = _deconstructPacket(packetData, buffers);
|
|
pack.attachments = buffers.length; // number of binary 'attachments'
|
|
return { packet: pack, buffers: buffers };
|
|
}
|
|
|
|
function _deconstructPacket(data, buffers) {
|
|
if (!data) return data;
|
|
|
|
if (isBinary(data)) {
|
|
const placeholder = { _placeholder: true, num: buffers.length };
|
|
buffers.push(data);
|
|
return placeholder;
|
|
} else if (Array.isArray(data)) {
|
|
const newData = new Array(data.length);
|
|
for (let i = 0; i < data.length; i++) {
|
|
newData[i] = _deconstructPacket(data[i], buffers);
|
|
}
|
|
return newData;
|
|
} else if (typeof data === "object" && !(data instanceof Date)) {
|
|
const newData = {};
|
|
for (const key in data) {
|
|
if (data.hasOwnProperty(key)) {
|
|
newData[key] = _deconstructPacket(data[key], buffers);
|
|
}
|
|
}
|
|
return newData;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
/**
|
|
* Reconstructs a binary packet from its placeholder packet and buffers
|
|
*
|
|
* @param {Object} packet - event packet with placeholders
|
|
* @param {Array} buffers - binary buffers to put in placeholder positions
|
|
* @return {Object} reconstructed packet
|
|
* @public
|
|
*/
|
|
|
|
export function reconstructPacket(packet, buffers) {
|
|
packet.data = _reconstructPacket(packet.data, buffers);
|
|
packet.attachments = undefined; // no longer useful
|
|
return packet;
|
|
}
|
|
|
|
function _reconstructPacket(data, buffers) {
|
|
if (!data) return data;
|
|
|
|
if (data && data._placeholder) {
|
|
return buffers[data.num]; // appropriate buffer (should be natural order anyway)
|
|
} else if (Array.isArray(data)) {
|
|
for (let i = 0; i < data.length; i++) {
|
|
data[i] = _reconstructPacket(data[i], buffers);
|
|
}
|
|
} else if (typeof data === "object") {
|
|
for (const key in data) {
|
|
if (data.hasOwnProperty(key)) {
|
|
data[key] = _reconstructPacket(data[key], buffers);
|
|
}
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|