Skip to content

Commit

Permalink
chore: add dist
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Oct 5, 2020
1 parent ccadd5a commit 091d25e
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node_modules
dist/
20 changes: 20 additions & 0 deletions dist/binary.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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 declare function deconstructPacket(packet: any): {
packet: any;
buffers: any[];
};
/**
* 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 declare function reconstructPacket(packet: any, buffers: any): any;
83 changes: 83 additions & 0 deletions dist/binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.reconstructPacket = exports.deconstructPacket = void 0;
const is_binary_1 = __importDefault(require("./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
*/
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 };
}
exports.deconstructPacket = deconstructPacket;
function _deconstructPacket(data, buffers) {
if (!data)
return data;
if (is_binary_1.default(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
*/
function reconstructPacket(packet, buffers) {
packet.data = _reconstructPacket(packet.data, buffers);
packet.attachments = undefined; // no longer useful
return packet;
}
exports.reconstructPacket = reconstructPacket;
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;
}
71 changes: 71 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Emitter from "component-emitter";
/**
* Protocol version.
*
* @public
*/
export declare const protocol: number;
export declare enum PacketType {
CONNECT = 0,
DISCONNECT = 1,
EVENT = 2,
ACK = 3,
ERROR = 4,
BINARY_EVENT = 5,
BINARY_ACK = 6
}
export interface Packet {
type: PacketType;
nsp: string;
data?: any;
id?: number;
attachments?: number;
}
/**
* A socket.io Encoder instance
*/
export declare class Encoder {
/**
* Encode a packet as a single string if non-binary, or as a
* buffer sequence, depending on packet type.
*
* @param {Object} obj - packet object
*/
encode(obj: Packet): any[];
/**
* Encode packet as string.
*/
private encodeAsString;
/**
* Encode packet as 'buffer sequence' by removing blobs, and
* deconstructing packet into object with placeholders and
* a list of buffers.
*/
private encodeAsBinary;
}
/**
* A socket.io Decoder instance
*
* @return {Object} decoder
*/
export declare class Decoder extends Emitter {
private reconstructor;
constructor();
/**
* Decodes an encoded packet string into packet JSON.
*
* @param {String} obj - encoded packet
*/
add(obj: any): void;
/**
* Decode a packet String (JSON data)
*
* @param {String} str
* @return {Object} packet
*/
private decodeString;
/**
* Deallocates a parser's resources
*/
destroy(): void;
}
Loading

0 comments on commit 091d25e

Please sign in to comment.