Skip to content

Commit 091d25e

Browse files
chore: add dist
1 parent ccadd5a commit 091d25e

File tree

7 files changed

+476
-1
lines changed

7 files changed

+476
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
node_modules
2-
dist/

dist/binary.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Replaces every Buffer | ArrayBuffer | Blob | File in packet with a numbered placeholder.
3+
*
4+
* @param {Object} packet - socket.io event packet
5+
* @return {Object} with deconstructed packet and list of buffers
6+
* @public
7+
*/
8+
export declare function deconstructPacket(packet: any): {
9+
packet: any;
10+
buffers: any[];
11+
};
12+
/**
13+
* Reconstructs a binary packet from its placeholder packet and buffers
14+
*
15+
* @param {Object} packet - event packet with placeholders
16+
* @param {Array} buffers - binary buffers to put in placeholder positions
17+
* @return {Object} reconstructed packet
18+
* @public
19+
*/
20+
export declare function reconstructPacket(packet: any, buffers: any): any;

dist/binary.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

dist/index.d.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import Emitter from "component-emitter";
2+
/**
3+
* Protocol version.
4+
*
5+
* @public
6+
*/
7+
export declare const protocol: number;
8+
export declare enum PacketType {
9+
CONNECT = 0,
10+
DISCONNECT = 1,
11+
EVENT = 2,
12+
ACK = 3,
13+
ERROR = 4,
14+
BINARY_EVENT = 5,
15+
BINARY_ACK = 6
16+
}
17+
export interface Packet {
18+
type: PacketType;
19+
nsp: string;
20+
data?: any;
21+
id?: number;
22+
attachments?: number;
23+
}
24+
/**
25+
* A socket.io Encoder instance
26+
*/
27+
export declare class Encoder {
28+
/**
29+
* Encode a packet as a single string if non-binary, or as a
30+
* buffer sequence, depending on packet type.
31+
*
32+
* @param {Object} obj - packet object
33+
*/
34+
encode(obj: Packet): any[];
35+
/**
36+
* Encode packet as string.
37+
*/
38+
private encodeAsString;
39+
/**
40+
* Encode packet as 'buffer sequence' by removing blobs, and
41+
* deconstructing packet into object with placeholders and
42+
* a list of buffers.
43+
*/
44+
private encodeAsBinary;
45+
}
46+
/**
47+
* A socket.io Decoder instance
48+
*
49+
* @return {Object} decoder
50+
*/
51+
export declare class Decoder extends Emitter {
52+
private reconstructor;
53+
constructor();
54+
/**
55+
* Decodes an encoded packet string into packet JSON.
56+
*
57+
* @param {String} obj - encoded packet
58+
*/
59+
add(obj: any): void;
60+
/**
61+
* Decode a packet String (JSON data)
62+
*
63+
* @param {String} str
64+
* @return {Object} packet
65+
*/
66+
private decodeString;
67+
/**
68+
* Deallocates a parser's resources
69+
*/
70+
destroy(): void;
71+
}

0 commit comments

Comments
 (0)