Initial working version
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
declare const PACKET_TYPES: any;
|
||||
declare const PACKET_TYPES_REVERSE: any;
|
||||
declare const ERROR_PACKET: {
|
||||
type: string;
|
||||
data: string;
|
||||
};
|
||||
export { PACKET_TYPES, PACKET_TYPES_REVERSE, ERROR_PACKET };
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
const PACKET_TYPES = Object.create(null); // no Map = no polyfill
|
||||
PACKET_TYPES["open"] = "0";
|
||||
PACKET_TYPES["close"] = "1";
|
||||
PACKET_TYPES["ping"] = "2";
|
||||
PACKET_TYPES["pong"] = "3";
|
||||
PACKET_TYPES["message"] = "4";
|
||||
PACKET_TYPES["upgrade"] = "5";
|
||||
PACKET_TYPES["noop"] = "6";
|
||||
const PACKET_TYPES_REVERSE = Object.create(null);
|
||||
Object.keys(PACKET_TYPES).forEach(key => {
|
||||
PACKET_TYPES_REVERSE[PACKET_TYPES[key]] = key;
|
||||
});
|
||||
const ERROR_PACKET = { type: "error", data: "parser error" };
|
||||
export { PACKET_TYPES, PACKET_TYPES_REVERSE, ERROR_PACKET };
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
declare const decodePacket: (encodedPacket: any, binaryType: any) => {
|
||||
type: string;
|
||||
data: any;
|
||||
} | {
|
||||
type: any;
|
||||
data: string;
|
||||
} | {
|
||||
type: any;
|
||||
data?: undefined;
|
||||
};
|
||||
export default decodePacket;
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
import { ERROR_PACKET, PACKET_TYPES_REVERSE } from "./commons.js";
|
||||
import { decode } from "base64-arraybuffer";
|
||||
const withNativeArrayBuffer = typeof ArrayBuffer === "function";
|
||||
const decodePacket = (encodedPacket, binaryType) => {
|
||||
if (typeof encodedPacket !== "string") {
|
||||
return {
|
||||
type: "message",
|
||||
data: mapBinary(encodedPacket, binaryType)
|
||||
};
|
||||
}
|
||||
const type = encodedPacket.charAt(0);
|
||||
if (type === "b") {
|
||||
return {
|
||||
type: "message",
|
||||
data: decodeBase64Packet(encodedPacket.substring(1), binaryType)
|
||||
};
|
||||
}
|
||||
const packetType = PACKET_TYPES_REVERSE[type];
|
||||
if (!packetType) {
|
||||
return ERROR_PACKET;
|
||||
}
|
||||
return encodedPacket.length > 1
|
||||
? {
|
||||
type: PACKET_TYPES_REVERSE[type],
|
||||
data: encodedPacket.substring(1)
|
||||
}
|
||||
: {
|
||||
type: PACKET_TYPES_REVERSE[type]
|
||||
};
|
||||
};
|
||||
const decodeBase64Packet = (data, binaryType) => {
|
||||
if (withNativeArrayBuffer) {
|
||||
const decoded = decode(data);
|
||||
return mapBinary(decoded, binaryType);
|
||||
}
|
||||
else {
|
||||
return { base64: true, data }; // fallback for old browsers
|
||||
}
|
||||
};
|
||||
const mapBinary = (data, binaryType) => {
|
||||
switch (binaryType) {
|
||||
case "blob":
|
||||
return data instanceof ArrayBuffer ? new Blob([data]) : data;
|
||||
case "arraybuffer":
|
||||
default:
|
||||
return data; // assuming the data is already an ArrayBuffer
|
||||
}
|
||||
};
|
||||
export default decodePacket;
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
declare const decodePacket: (encodedPacket: any, binaryType?: any) => {
|
||||
type: string;
|
||||
data: any;
|
||||
} | {
|
||||
type: any;
|
||||
data: string;
|
||||
} | {
|
||||
type: any;
|
||||
data?: undefined;
|
||||
};
|
||||
export default decodePacket;
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
import { ERROR_PACKET, PACKET_TYPES_REVERSE } from "./commons.js";
|
||||
const decodePacket = (encodedPacket, binaryType) => {
|
||||
if (typeof encodedPacket !== "string") {
|
||||
return {
|
||||
type: "message",
|
||||
data: mapBinary(encodedPacket, binaryType)
|
||||
};
|
||||
}
|
||||
const type = encodedPacket.charAt(0);
|
||||
if (type === "b") {
|
||||
const buffer = Buffer.from(encodedPacket.substring(1), "base64");
|
||||
return {
|
||||
type: "message",
|
||||
data: mapBinary(buffer, binaryType)
|
||||
};
|
||||
}
|
||||
if (!PACKET_TYPES_REVERSE[type]) {
|
||||
return ERROR_PACKET;
|
||||
}
|
||||
return encodedPacket.length > 1
|
||||
? {
|
||||
type: PACKET_TYPES_REVERSE[type],
|
||||
data: encodedPacket.substring(1)
|
||||
}
|
||||
: {
|
||||
type: PACKET_TYPES_REVERSE[type]
|
||||
};
|
||||
};
|
||||
const mapBinary = (data, binaryType) => {
|
||||
const isBuffer = Buffer.isBuffer(data);
|
||||
switch (binaryType) {
|
||||
case "arraybuffer":
|
||||
return isBuffer ? toArrayBuffer(data) : data;
|
||||
case "nodebuffer":
|
||||
default:
|
||||
return data; // assuming the data is already a Buffer
|
||||
}
|
||||
};
|
||||
const toArrayBuffer = buffer => {
|
||||
const arrayBuffer = new ArrayBuffer(buffer.length);
|
||||
const view = new Uint8Array(arrayBuffer);
|
||||
for (let i = 0; i < buffer.length; i++) {
|
||||
view[i] = buffer[i];
|
||||
}
|
||||
return arrayBuffer;
|
||||
};
|
||||
export default decodePacket;
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
declare const encodePacket: ({ type, data }: {
|
||||
type: any;
|
||||
data: any;
|
||||
}, supportsBinary: any, callback: any) => any;
|
||||
export default encodePacket;
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
import { PACKET_TYPES } from "./commons.js";
|
||||
const withNativeBlob = typeof Blob === "function" ||
|
||||
(typeof Blob !== "undefined" &&
|
||||
Object.prototype.toString.call(Blob) === "[object BlobConstructor]");
|
||||
const withNativeArrayBuffer = typeof ArrayBuffer === "function";
|
||||
// ArrayBuffer.isView method is not defined in IE10
|
||||
const isView = obj => {
|
||||
return typeof ArrayBuffer.isView === "function"
|
||||
? ArrayBuffer.isView(obj)
|
||||
: obj && obj.buffer instanceof ArrayBuffer;
|
||||
};
|
||||
const encodePacket = ({ type, data }, supportsBinary, callback) => {
|
||||
if (withNativeBlob && data instanceof Blob) {
|
||||
if (supportsBinary) {
|
||||
return callback(data);
|
||||
}
|
||||
else {
|
||||
return encodeBlobAsBase64(data, callback);
|
||||
}
|
||||
}
|
||||
else if (withNativeArrayBuffer &&
|
||||
(data instanceof ArrayBuffer || isView(data))) {
|
||||
if (supportsBinary) {
|
||||
return callback(data);
|
||||
}
|
||||
else {
|
||||
return encodeBlobAsBase64(new Blob([data]), callback);
|
||||
}
|
||||
}
|
||||
// plain string
|
||||
return callback(PACKET_TYPES[type] + (data || ""));
|
||||
};
|
||||
const encodeBlobAsBase64 = (data, callback) => {
|
||||
const fileReader = new FileReader();
|
||||
fileReader.onload = function () {
|
||||
const content = fileReader.result.split(",")[1];
|
||||
callback("b" + content);
|
||||
};
|
||||
return fileReader.readAsDataURL(data);
|
||||
};
|
||||
export default encodePacket;
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
declare const encodePacket: ({ type, data }: {
|
||||
type: any;
|
||||
data: any;
|
||||
}, supportsBinary: any, callback: any) => any;
|
||||
export default encodePacket;
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import { PACKET_TYPES } from "./commons.js";
|
||||
const encodePacket = ({ type, data }, supportsBinary, callback) => {
|
||||
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
|
||||
const buffer = toBuffer(data);
|
||||
return callback(encodeBuffer(buffer, supportsBinary));
|
||||
}
|
||||
// plain string
|
||||
return callback(PACKET_TYPES[type] + (data || ""));
|
||||
};
|
||||
const toBuffer = data => {
|
||||
if (Buffer.isBuffer(data)) {
|
||||
return data;
|
||||
}
|
||||
else if (data instanceof ArrayBuffer) {
|
||||
return Buffer.from(data);
|
||||
}
|
||||
else {
|
||||
return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
||||
}
|
||||
};
|
||||
// only 'message' packets can contain binary, so the type prefix is not needed
|
||||
const encodeBuffer = (data, supportsBinary) => {
|
||||
return supportsBinary ? data : "b" + data.toString("base64");
|
||||
};
|
||||
export default encodePacket;
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import encodePacket from "./encodePacket.js";
|
||||
import decodePacket from "./decodePacket.js";
|
||||
declare const encodePayload: (packets: any, callback: any) => void;
|
||||
declare const decodePayload: (encodedPayload: any, binaryType?: any) => any[];
|
||||
export declare const protocol = 4;
|
||||
export { encodePacket, encodePayload, decodePacket, decodePayload };
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import encodePacket from "./encodePacket.js";
|
||||
import decodePacket from "./decodePacket.js";
|
||||
const SEPARATOR = String.fromCharCode(30); // see https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
|
||||
const encodePayload = (packets, callback) => {
|
||||
// some packets may be added to the array while encoding, so the initial length must be saved
|
||||
const length = packets.length;
|
||||
const encodedPackets = new Array(length);
|
||||
let count = 0;
|
||||
packets.forEach((packet, i) => {
|
||||
// force base64 encoding for binary packets
|
||||
encodePacket(packet, false, encodedPacket => {
|
||||
encodedPackets[i] = encodedPacket;
|
||||
if (++count === length) {
|
||||
callback(encodedPackets.join(SEPARATOR));
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
const decodePayload = (encodedPayload, binaryType) => {
|
||||
const encodedPackets = encodedPayload.split(SEPARATOR);
|
||||
const packets = [];
|
||||
for (let i = 0; i < encodedPackets.length; i++) {
|
||||
const decodedPacket = decodePacket(encodedPackets[i], binaryType);
|
||||
packets.push(decodedPacket);
|
||||
if (decodedPacket.type === "error") {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return packets;
|
||||
};
|
||||
export const protocol = 4;
|
||||
export { encodePacket, encodePayload, decodePacket, decodePayload };
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"type": "module",
|
||||
"browser": {
|
||||
"./encodePacket.js": "./encodePacket.browser.js",
|
||||
"./decodePacket.js": "./decodePacket.browser.js"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user