engine.io-parser
Advanced tools
Comparing version 2.2.0 to 4.0.0-alpha.0
@@ -1,14 +0,9 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
const hasBinary = require("has-binary2"); | ||
const sliceBuffer = require("arraybuffer.slice"); | ||
const after = require("after"); | ||
const utf8 = require("./utf8"); | ||
var keys = require('./keys'); | ||
var hasBinary = require('has-binary2'); | ||
var sliceBuffer = require('arraybuffer.slice'); | ||
var after = require('after'); | ||
var utf8 = require('./utf8'); | ||
var base64encoder; | ||
if (typeof ArrayBuffer !== 'undefined') { | ||
base64encoder = require('base64-arraybuffer'); | ||
let base64encoder; | ||
if (typeof ArrayBuffer !== "undefined") { | ||
base64encoder = require("base64-arraybuffer"); | ||
} | ||
@@ -22,5 +17,5 @@ | ||
*/ | ||
const isAndroid = | ||
typeof navigator !== "undefined" && /Android/i.test(navigator.userAgent); | ||
var isAndroid = typeof navigator !== 'undefined' && /Android/i.test(navigator.userAgent); | ||
/** | ||
@@ -32,3 +27,4 @@ * Check if we are running in PhantomJS. | ||
*/ | ||
var isPhantomJS = typeof navigator !== 'undefined' && /PhantomJS/i.test(navigator.userAgent); | ||
const isPhantomJS = | ||
typeof navigator !== "undefined" && /PhantomJS/i.test(navigator.userAgent); | ||
@@ -39,3 +35,3 @@ /** | ||
*/ | ||
var dontSendBlobs = isAndroid || isPhantomJS; | ||
const dontSendBlobs = isAndroid || isPhantomJS; | ||
@@ -46,3 +42,3 @@ /** | ||
exports.protocol = 3; | ||
exports.protocol = 4; | ||
@@ -52,27 +48,24 @@ /** | ||
*/ | ||
const packets = (exports.packets = { | ||
open: 0, // non-ws | ||
close: 1, // non-ws | ||
ping: 2, | ||
pong: 3, | ||
message: 4, | ||
upgrade: 5, | ||
noop: 6 | ||
}); | ||
var packets = exports.packets = { | ||
open: 0 // non-ws | ||
, close: 1 // non-ws | ||
, ping: 2 | ||
, pong: 3 | ||
, message: 4 | ||
, upgrade: 5 | ||
, noop: 6 | ||
}; | ||
const packetslist = Object.keys(packets); | ||
var packetslist = keys(packets); | ||
/** | ||
* Premade error packet. | ||
*/ | ||
const err = { type: "error", data: "parser error" }; | ||
var err = { type: 'error', data: 'parser error' }; | ||
/** | ||
* Create a blob api even for blob builder when vendor prefixes exist | ||
*/ | ||
const Blob = require("blob"); | ||
var Blob = require('blob'); | ||
/** | ||
@@ -94,4 +87,4 @@ * Encodes a packet. | ||
exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) { | ||
if (typeof supportsBinary === 'function') { | ||
exports.encodePacket = function(packet, supportsBinary, utf8encode, callback) { | ||
if (typeof supportsBinary === "function") { | ||
callback = supportsBinary; | ||
@@ -101,3 +94,3 @@ supportsBinary = false; | ||
if (typeof utf8encode === 'function') { | ||
if (typeof utf8encode === "function") { | ||
callback = utf8encode; | ||
@@ -107,9 +100,8 @@ utf8encode = null; | ||
var data = (packet.data === undefined) | ||
? undefined | ||
: packet.data.buffer || packet.data; | ||
const data = | ||
packet.data === undefined ? undefined : packet.data.buffer || packet.data; | ||
if (typeof ArrayBuffer !== 'undefined' && data instanceof ArrayBuffer) { | ||
if (typeof ArrayBuffer !== "undefined" && data instanceof ArrayBuffer) { | ||
return encodeArrayBuffer(packet, supportsBinary, callback); | ||
} else if (typeof Blob !== 'undefined' && data instanceof Blob) { | ||
} else if (typeof Blob !== "undefined" && data instanceof Blob) { | ||
return encodeBlob(packet, supportsBinary, callback); | ||
@@ -124,11 +116,12 @@ } | ||
// Sending data as a utf-8 string | ||
var encoded = packets[packet.type]; | ||
let encoded = packets[packet.type]; | ||
// data fragment is optional | ||
if (undefined !== packet.data) { | ||
encoded += utf8encode ? utf8.encode(String(packet.data), { strict: false }) : String(packet.data); | ||
encoded += utf8encode | ||
? utf8.encode(String(packet.data), { strict: false }) | ||
: String(packet.data); | ||
} | ||
return callback('' + encoded); | ||
return callback("" + encoded); | ||
}; | ||
@@ -138,3 +131,3 @@ | ||
// packet data is an object { base64: true, data: dataAsBase64String } | ||
var message = 'b' + exports.packets[packet.type] + packet.data.data; | ||
const message = "b" + exports.packets[packet.type] + packet.data.data; | ||
return callback(message); | ||
@@ -152,12 +145,3 @@ } | ||
var data = packet.data; | ||
var contentArray = new Uint8Array(data); | ||
var resultBuffer = new Uint8Array(1 + data.byteLength); | ||
resultBuffer[0] = packets[packet.type]; | ||
for (var i = 0; i < contentArray.length; i++) { | ||
resultBuffer[i+1] = contentArray[i]; | ||
} | ||
return callback(resultBuffer.buffer); | ||
return callback(packet.data); | ||
} | ||
@@ -170,5 +154,10 @@ | ||
var fr = new FileReader(); | ||
const fr = new FileReader(); | ||
fr.onload = function() { | ||
exports.encodePacket({ type: packet.type, data: fr.result }, supportsBinary, true, callback); | ||
exports.encodePacket( | ||
{ type: packet.type, data: fr.result }, | ||
supportsBinary, | ||
true, | ||
callback | ||
); | ||
}; | ||
@@ -187,7 +176,3 @@ return fr.readAsArrayBuffer(packet.data); | ||
var length = new Uint8Array(1); | ||
length[0] = packets[packet.type]; | ||
var blob = new Blob([length.buffer, packet.data]); | ||
return callback(blob); | ||
return callback(packet.data); | ||
} | ||
@@ -203,7 +188,7 @@ | ||
exports.encodeBase64Packet = function(packet, callback) { | ||
var message = 'b' + exports.packets[packet.type]; | ||
if (typeof Blob !== 'undefined' && packet.data instanceof Blob) { | ||
var fr = new FileReader(); | ||
let message = "b" + exports.packets[packet.type]; | ||
if (typeof Blob !== "undefined" && packet.data instanceof Blob) { | ||
const fr = new FileReader(); | ||
fr.onload = function() { | ||
var b64 = fr.result.split(',')[1]; | ||
const b64 = fr.result.split(",")[1]; | ||
callback(message + b64); | ||
@@ -214,3 +199,3 @@ }; | ||
var b64data; | ||
let b64data; | ||
try { | ||
@@ -220,5 +205,5 @@ b64data = String.fromCharCode.apply(null, new Uint8Array(packet.data)); | ||
// iPhone Safari doesn't let you apply with typed arrays | ||
var typed = new Uint8Array(packet.data); | ||
var basic = new Array(typed.length); | ||
for (var i = 0; i < typed.length; i++) { | ||
const typed = new Uint8Array(packet.data); | ||
const basic = new Array(typed.length); | ||
for (let i = 0; i < typed.length; i++) { | ||
basic[i] = typed[i]; | ||
@@ -239,3 +224,3 @@ } | ||
exports.decodePacket = function (data, binaryType, utf8decode) { | ||
exports.decodePacket = function(data, binaryType, utf8decode) { | ||
if (data === undefined) { | ||
@@ -245,4 +230,4 @@ return err; | ||
// String data | ||
if (typeof data === 'string') { | ||
if (data.charAt(0) === 'b') { | ||
if (typeof data === "string") { | ||
if (data.charAt(0) === "b") { | ||
return exports.decodeBase64Packet(data.substr(1), binaryType); | ||
@@ -257,3 +242,3 @@ } | ||
} | ||
var type = data.charAt(0); | ||
const type = data.charAt(0); | ||
@@ -271,9 +256,7 @@ if (Number(type) != type || !packetslist[type]) { | ||
var asArray = new Uint8Array(data); | ||
var type = asArray[0]; | ||
var rest = sliceBuffer(data, 1); | ||
if (Blob && binaryType === 'blob') { | ||
rest = new Blob([rest]); | ||
} | ||
return { type: packetslist[type], data: rest }; | ||
// only 'message' packets can contain binary data | ||
return { | ||
type: "message", | ||
data: Blob && binaryType === "blob" ? new Blob([data]) : data | ||
}; | ||
}; | ||
@@ -298,3 +281,3 @@ | ||
exports.decodeBase64Packet = function(msg, binaryType) { | ||
var type = packetslist[msg.charAt(0)]; | ||
const type = packetslist[msg.charAt(0)]; | ||
if (!base64encoder) { | ||
@@ -304,5 +287,5 @@ return { type: type, data: { base64: true, data: msg.substr(1) } }; | ||
var data = base64encoder.decode(msg.substr(1)); | ||
let data = base64encoder.decode(msg.substr(1)); | ||
if (binaryType === 'blob' && Blob) { | ||
if (binaryType === "blob" && Blob) { | ||
data = new Blob([data]); | ||
@@ -330,4 +313,4 @@ } | ||
exports.encodePayload = function (packets, supportsBinary, callback) { | ||
if (typeof supportsBinary === 'function') { | ||
exports.encodePayload = function(packets, supportsBinary, callback) { | ||
if (typeof supportsBinary === "function") { | ||
callback = supportsBinary; | ||
@@ -337,3 +320,3 @@ supportsBinary = null; | ||
var isBinary = hasBinary(packets); | ||
const isBinary = hasBinary(packets); | ||
@@ -349,17 +332,22 @@ if (supportsBinary && isBinary) { | ||
if (!packets.length) { | ||
return callback('0:'); | ||
return callback("0:"); | ||
} | ||
function setLengthHeader(message) { | ||
return message.length + ':' + message; | ||
return message.length + ":" + message; | ||
} | ||
function encodeOne(packet, doneCallback) { | ||
exports.encodePacket(packet, !isBinary ? false : supportsBinary, false, function(message) { | ||
doneCallback(null, setLengthHeader(message)); | ||
}); | ||
exports.encodePacket( | ||
packet, | ||
!isBinary ? false : supportsBinary, | ||
false, | ||
function(message) { | ||
doneCallback(null, setLengthHeader(message)); | ||
} | ||
); | ||
} | ||
map(packets, encodeOne, function(err, results) { | ||
return callback(results.join('')); | ||
return callback(results.join("")); | ||
}); | ||
@@ -373,6 +361,6 @@ }; | ||
function map(ary, each, done) { | ||
var result = new Array(ary.length); | ||
var next = after(ary.length, done); | ||
const result = new Array(ary.length); | ||
const next = after(ary.length, done); | ||
var eachWithIndex = function(i, el, cb) { | ||
const eachWithIndex = function(i, el, cb) { | ||
each(el, function(error, msg) { | ||
@@ -384,3 +372,3 @@ result[i] = msg; | ||
for (var i = 0; i < ary.length; i++) { | ||
for (let i = 0; i < ary.length; i++) { | ||
eachWithIndex(i, ary[i], next); | ||
@@ -398,8 +386,8 @@ } | ||
exports.decodePayload = function (data, binaryType, callback) { | ||
if (typeof data !== 'string') { | ||
exports.decodePayload = function(data, binaryType, callback) { | ||
if (typeof data !== "string") { | ||
return exports.decodePayloadAsBinary(data, binaryType, callback); | ||
} | ||
if (typeof binaryType === 'function') { | ||
if (typeof binaryType === "function") { | ||
callback = binaryType; | ||
@@ -409,4 +397,4 @@ binaryType = null; | ||
var packet; | ||
if (data === '') { | ||
let packet; | ||
if (data === "") { | ||
// parser error - ignoring payload | ||
@@ -416,8 +404,12 @@ return callback(err, 0, 1); | ||
var length = '', n, msg; | ||
let length = "", | ||
n, | ||
msg; | ||
for (var i = 0, l = data.length; i < l; i++) { | ||
var chr = data.charAt(i); | ||
let i = 0; | ||
const l = data.length; | ||
for (; i < l; i++) { | ||
const chr = data.charAt(i); | ||
if (chr !== ':') { | ||
if (chr !== ":") { | ||
length += chr; | ||
@@ -427,3 +419,3 @@ continue; | ||
if (length === '' || (length != (n = Number(length)))) { | ||
if (length === "" || length != (n = Number(length))) { | ||
// parser error - ignoring payload | ||
@@ -448,3 +440,3 @@ return callback(err, 0, 1); | ||
var ret = callback(packet, i + n, l); | ||
const ret = callback(packet, i + n, l); | ||
if (false === ret) return; | ||
@@ -455,10 +447,9 @@ } | ||
i += n; | ||
length = ''; | ||
length = ""; | ||
} | ||
if (length !== '') { | ||
if (length !== "") { | ||
// parser error - ignoring payload | ||
return callback(err, 0, 1); | ||
} | ||
}; | ||
@@ -492,5 +483,5 @@ | ||
map(packets, encodeOne, function(err, encodedPackets) { | ||
var totalLength = encodedPackets.reduce(function(acc, p) { | ||
var len; | ||
if (typeof p === 'string'){ | ||
const totalLength = encodedPackets.reduce(function(acc, p) { | ||
let len; | ||
if (typeof p === "string") { | ||
len = p.length; | ||
@@ -503,11 +494,11 @@ } else { | ||
var resultArray = new Uint8Array(totalLength); | ||
const resultArray = new Uint8Array(totalLength); | ||
var bufferIndex = 0; | ||
let bufferIndex = 0; | ||
encodedPackets.forEach(function(p) { | ||
var isString = typeof p === 'string'; | ||
var ab = p; | ||
const isString = typeof p === "string"; | ||
let ab = p; | ||
if (isString) { | ||
var view = new Uint8Array(p.length); | ||
for (var i = 0; i < p.length; i++) { | ||
for (let i = 0; i < p.length; i++) { | ||
view[i] = p.charCodeAt(i); | ||
@@ -518,10 +509,12 @@ } | ||
if (isString) { // not true binary | ||
if (isString) { | ||
// not true binary | ||
resultArray[bufferIndex++] = 0; | ||
} else { // true binary | ||
} else { | ||
// true binary | ||
resultArray[bufferIndex++] = 1; | ||
} | ||
var lenStr = ab.byteLength.toString(); | ||
for (var i = 0; i < lenStr.length; i++) { | ||
const lenStr = ab.byteLength.toString(); | ||
for (let i = 0; i < lenStr.length; i++) { | ||
resultArray[bufferIndex++] = parseInt(lenStr[i]); | ||
@@ -532,3 +525,3 @@ } | ||
var view = new Uint8Array(ab); | ||
for (var i = 0; i < view.length; i++) { | ||
for (let i = 0; i < view.length; i++) { | ||
resultArray[bufferIndex++] = view[i]; | ||
@@ -549,7 +542,7 @@ } | ||
exports.encodePacket(packet, true, true, function(encoded) { | ||
var binaryIdentifier = new Uint8Array(1); | ||
const binaryIdentifier = new Uint8Array(1); | ||
binaryIdentifier[0] = 1; | ||
if (typeof encoded === 'string') { | ||
var view = new Uint8Array(encoded.length); | ||
for (var i = 0; i < encoded.length; i++) { | ||
if (typeof encoded === "string") { | ||
const view = new Uint8Array(encoded.length); | ||
for (let i = 0; i < encoded.length; i++) { | ||
view[i] = encoded.charCodeAt(i); | ||
@@ -561,9 +554,8 @@ } | ||
var len = (encoded instanceof ArrayBuffer) | ||
? encoded.byteLength | ||
: encoded.size; | ||
const len = | ||
encoded instanceof ArrayBuffer ? encoded.byteLength : encoded.size; | ||
var lenStr = len.toString(); | ||
var lengthAry = new Uint8Array(lenStr.length + 1); | ||
for (var i = 0; i < lenStr.length; i++) { | ||
const lenStr = len.toString(); | ||
const lengthAry = new Uint8Array(lenStr.length + 1); | ||
for (let i = 0; i < lenStr.length; i++) { | ||
lengthAry[i] = parseInt(lenStr[i]); | ||
@@ -574,3 +566,7 @@ } | ||
if (Blob) { | ||
var blob = new Blob([binaryIdentifier.buffer, lengthAry.buffer, encoded]); | ||
const blob = new Blob([ | ||
binaryIdentifier.buffer, | ||
lengthAry.buffer, | ||
encoded | ||
]); | ||
doneCallback(null, blob); | ||
@@ -595,4 +591,4 @@ } | ||
exports.decodePayloadAsBinary = function (data, binaryType, callback) { | ||
if (typeof binaryType === 'function') { | ||
exports.decodePayloadAsBinary = function(data, binaryType, callback) { | ||
if (typeof binaryType === "function") { | ||
callback = binaryType; | ||
@@ -602,11 +598,11 @@ binaryType = null; | ||
var bufferTail = data; | ||
var buffers = []; | ||
let bufferTail = data; | ||
const buffers = []; | ||
while (bufferTail.byteLength > 0) { | ||
var tailArray = new Uint8Array(bufferTail); | ||
var isString = tailArray[0] === 0; | ||
var msgLength = ''; | ||
const tailArray = new Uint8Array(bufferTail); | ||
const isString = tailArray[0] === 0; | ||
let msgLength = ""; | ||
for (var i = 1; ; i++) { | ||
for (let i = 1; ; i++) { | ||
if (tailArray[i] === 255) break; | ||
@@ -625,3 +621,3 @@ | ||
var msg = sliceBuffer(bufferTail, 0, msgLength); | ||
let msg = sliceBuffer(bufferTail, 0, msgLength); | ||
if (isString) { | ||
@@ -632,5 +628,5 @@ try { | ||
// iPhone Safari doesn't let you apply to typed arrays | ||
var typed = new Uint8Array(msg); | ||
msg = ''; | ||
for (var i = 0; i < typed.length; i++) { | ||
const typed = new Uint8Array(msg); | ||
msg = ""; | ||
for (let i = 0; i < typed.length; i++) { | ||
msg += String.fromCharCode(typed[i]); | ||
@@ -645,3 +641,3 @@ } | ||
var total = buffers.length; | ||
const total = buffers.length; | ||
buffers.forEach(function(buffer, i) { | ||
@@ -648,0 +644,0 @@ callback(exports.decodePacket(buffer, binaryType, true), i, total); |
217
lib/index.js
@@ -1,14 +0,9 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
const utf8 = require("./utf8"); | ||
const hasBinary = require("has-binary2"); | ||
const after = require("after"); | ||
var utf8 = require('./utf8'); | ||
var hasBinary = require('has-binary2'); | ||
var after = require('after'); | ||
var keys = require('./keys'); | ||
/** | ||
* Current protocol version. | ||
*/ | ||
exports.protocol = 3; | ||
exports.protocol = 4; | ||
@@ -18,21 +13,19 @@ /** | ||
*/ | ||
const packets = (exports.packets = { | ||
open: 0, // non-ws | ||
close: 1, // non-ws | ||
ping: 2, | ||
pong: 3, | ||
message: 4, | ||
upgrade: 5, | ||
noop: 6 | ||
}); | ||
var packets = exports.packets = { | ||
open: 0 // non-ws | ||
, close: 1 // non-ws | ||
, ping: 2 | ||
, pong: 3 | ||
, message: 4 | ||
, upgrade: 5 | ||
, noop: 6 | ||
}; | ||
const packetslist = Object.keys(packets); | ||
var packetslist = keys(packets); | ||
/** | ||
* Premade error packet. | ||
*/ | ||
const err = { type: "error", data: "parser error" }; | ||
var err = { type: 'error', data: 'parser error' }; | ||
const EMPTY_BUFFER = Buffer.concat([]); | ||
@@ -56,4 +49,4 @@ | ||
exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) { | ||
if (typeof supportsBinary === 'function') { | ||
exports.encodePacket = function(packet, supportsBinary, utf8encode, callback) { | ||
if (typeof supportsBinary === "function") { | ||
callback = supportsBinary; | ||
@@ -63,3 +56,3 @@ supportsBinary = null; | ||
if (typeof utf8encode === 'function') { | ||
if (typeof utf8encode === "function") { | ||
callback = utf8encode; | ||
@@ -71,15 +64,24 @@ utf8encode = null; | ||
return encodeBuffer(packet, supportsBinary, callback); | ||
} else if (packet.data && (packet.data.buffer || packet.data) instanceof ArrayBuffer) { | ||
return encodeBuffer({ type: packet.type, data: arrayBufferToBuffer(packet.data) }, supportsBinary, callback); | ||
} else if ( | ||
packet.data && | ||
(packet.data.buffer || packet.data) instanceof ArrayBuffer | ||
) { | ||
return encodeBuffer( | ||
{ type: packet.type, data: arrayBufferToBuffer(packet.data) }, | ||
supportsBinary, | ||
callback | ||
); | ||
} | ||
// Sending data as a utf-8 string | ||
var encoded = packets[packet.type]; | ||
let encoded = packets[packet.type]; | ||
// data fragment is optional | ||
if (undefined !== packet.data) { | ||
encoded += utf8encode ? utf8.encode(String(packet.data), { strict: false }) : String(packet.data); | ||
encoded += utf8encode | ||
? utf8.encode(String(packet.data), { strict: false }) | ||
: String(packet.data); | ||
} | ||
return callback('' + encoded); | ||
return callback("" + encoded); | ||
}; | ||
@@ -95,7 +97,3 @@ | ||
} | ||
var data = packet.data; | ||
var typeBuffer = Buffer.allocUnsafe(1); | ||
typeBuffer[0] = packets[packet.type]; | ||
return callback(Buffer.concat([typeBuffer, data])); | ||
return callback(packet.data); | ||
} | ||
@@ -110,6 +108,8 @@ | ||
exports.encodeBase64Packet = function(packet, callback){ | ||
var data = Buffer.isBuffer(packet.data) ? packet.data : arrayBufferToBuffer(packet.data); | ||
var message = 'b' + packets[packet.type]; | ||
message += data.toString('base64'); | ||
exports.encodeBase64Packet = function(packet, callback) { | ||
const data = Buffer.isBuffer(packet.data) | ||
? packet.data | ||
: arrayBufferToBuffer(packet.data); | ||
let message = "b" + packets[packet.type]; | ||
message += data.toString("base64"); | ||
return callback(message); | ||
@@ -125,3 +125,3 @@ }; | ||
exports.decodePacket = function (data, binaryType, utf8decode) { | ||
exports.decodePacket = function(data, binaryType, utf8decode) { | ||
if (data === undefined) { | ||
@@ -131,10 +131,9 @@ return err; | ||
var type; | ||
let type; | ||
// String data | ||
if (typeof data === 'string') { | ||
if (typeof data === "string") { | ||
type = data.charAt(0); | ||
if (type === 'b') { | ||
if (type === "b") { | ||
return exports.decodeBase64Packet(data.substr(1), binaryType); | ||
@@ -162,7 +161,6 @@ } | ||
// Binary data | ||
if (binaryType === 'arraybuffer') { | ||
if (binaryType === "arraybuffer") { | ||
// wrap Buffer/ArrayBuffer data into an Uint8Array | ||
var intArray = new Uint8Array(data); | ||
type = intArray[0]; | ||
return { type: packetslist[type], data: intArray.buffer.slice(1) }; | ||
const intArray = new Uint8Array(data); | ||
return { type: "message", data: intArray.buffer }; | ||
} | ||
@@ -173,4 +171,4 @@ | ||
} | ||
type = data[0]; | ||
return { type: packetslist[type], data: data.slice(1) }; | ||
// only 'message' packets can contain binary data | ||
return { type: "message", data }; | ||
}; | ||
@@ -195,7 +193,7 @@ | ||
exports.decodeBase64Packet = function(msg, binaryType) { | ||
var type = packetslist[msg.charAt(0)]; | ||
var data = Buffer.from(msg.substr(1), 'base64'); | ||
if (binaryType === 'arraybuffer') { | ||
var abv = new Uint8Array(data.length); | ||
for (var i = 0; i < abv.length; i++){ | ||
const type = packetslist[msg.charAt(0)]; | ||
let data = Buffer.from(msg.substr(1), "base64"); | ||
if (binaryType === "arraybuffer") { | ||
const abv = new Uint8Array(data.length); | ||
for (let i = 0; i < abv.length; i++) { | ||
abv[i] = data[i]; | ||
@@ -224,4 +222,4 @@ } | ||
exports.encodePayload = function (packets, supportsBinary, callback) { | ||
if (typeof supportsBinary === 'function') { | ||
exports.encodePayload = function(packets, supportsBinary, callback) { | ||
if (typeof supportsBinary === "function") { | ||
callback = supportsBinary; | ||
@@ -236,3 +234,3 @@ supportsBinary = null; | ||
if (!packets.length) { | ||
return callback('0:'); | ||
return callback("0:"); | ||
} | ||
@@ -247,3 +245,3 @@ | ||
map(packets, encodeOne, function(err, results) { | ||
return callback(results.join('')); | ||
return callback(results.join("")); | ||
}); | ||
@@ -253,3 +251,3 @@ }; | ||
function setLengthHeader(message) { | ||
return message.length + ':' + message; | ||
return message.length + ":" + message; | ||
} | ||
@@ -262,6 +260,6 @@ | ||
function map(ary, each, done) { | ||
var result = new Array(ary.length); | ||
var next = after(ary.length, done); | ||
const result = new Array(ary.length); | ||
const next = after(ary.length, done); | ||
for (var i = 0; i < ary.length; i++) { | ||
for (let i = 0; i < ary.length; i++) { | ||
each(ary[i], function(error, msg) { | ||
@@ -282,8 +280,8 @@ result[i] = msg; | ||
exports.decodePayload = function (data, binaryType, callback) { | ||
if (typeof data !== 'string') { | ||
exports.decodePayload = function(data, binaryType, callback) { | ||
if (typeof data !== "string") { | ||
return exports.decodePayloadAsBinary(data, binaryType, callback); | ||
} | ||
if (typeof binaryType === 'function') { | ||
if (typeof binaryType === "function") { | ||
callback = binaryType; | ||
@@ -293,3 +291,3 @@ binaryType = null; | ||
if (data === '') { | ||
if (data === "") { | ||
// parser error - ignoring payload | ||
@@ -299,8 +297,13 @@ return callback(err, 0, 1); | ||
var length = '', n, msg, packet; | ||
let length = "", | ||
n, | ||
msg, | ||
packet; | ||
for (var i = 0, l = data.length; i < l; i++) { | ||
var chr = data.charAt(i); | ||
let i = 0; | ||
const l = data.length; | ||
for (; i < l; i++) { | ||
const chr = data.charAt(i); | ||
if (chr !== ':') { | ||
if (chr !== ":") { | ||
length += chr; | ||
@@ -310,3 +313,3 @@ continue; | ||
if (length === '' || (length != (n = Number(length)))) { | ||
if (length === "" || length != (n = Number(length))) { | ||
// parser error - ignoring payload | ||
@@ -331,3 +334,3 @@ return callback(err, 0, 1); | ||
var more = callback(packet, i + n, l); | ||
const more = callback(packet, i + n, l); | ||
if (false === more) return; | ||
@@ -338,10 +341,9 @@ } | ||
i += n; | ||
length = ''; | ||
length = ""; | ||
} | ||
if (length !== '') { | ||
if (length !== "") { | ||
// parser error - ignoring payload | ||
return callback(err, 0, 1); | ||
} | ||
}; | ||
@@ -357,4 +359,6 @@ | ||
function bufferToString(buffer) { | ||
var str = ''; | ||
for (var i = 0, l = buffer.length; i < l; i++) { | ||
let str = ""; | ||
let i = 0; | ||
const l = buffer.length; | ||
for (; i < l; i++) { | ||
str += String.fromCharCode(buffer[i]); | ||
@@ -373,4 +377,6 @@ } | ||
function stringToBuffer(string) { | ||
var buf = Buffer.allocUnsafe(string.length); | ||
for (var i = 0, l = string.length; i < l; i++) { | ||
const buf = Buffer.allocUnsafe(string.length); | ||
let i = 0; | ||
const l = string.length; | ||
for (; i < l; i++) { | ||
buf.writeUInt8(string.charCodeAt(i), i); | ||
@@ -390,4 +396,4 @@ } | ||
// data is either an ArrayBuffer or ArrayBufferView. | ||
var length = data.byteLength || data.length; | ||
var offset = data.byteOffset || 0; | ||
const length = data.byteLength || data.length; | ||
const offset = data.byteOffset || 0; | ||
@@ -411,3 +417,3 @@ return Buffer.from(data.buffer || data, offset, length); | ||
exports.encodePayloadAsBinary = function (packets, callback) { | ||
exports.encodePayloadAsBinary = function(packets, callback) { | ||
if (!packets.length) { | ||
@@ -423,16 +429,17 @@ return callback(EMPTY_BUFFER); | ||
function encodeOneBinaryPacket(p, doneCallback) { | ||
function onBinaryPacketEncode(packet) { | ||
const encodingLength = "" + packet.length; | ||
let sizeBuffer; | ||
var encodingLength = '' + packet.length; | ||
var sizeBuffer; | ||
if (typeof packet === 'string') { | ||
if (typeof packet === "string") { | ||
sizeBuffer = Buffer.allocUnsafe(encodingLength.length + 2); | ||
sizeBuffer[0] = 0; // is a string (not true binary = 0) | ||
for (var i = 0; i < encodingLength.length; i++) { | ||
for (let i = 0; i < encodingLength.length; i++) { | ||
sizeBuffer[i + 1] = parseInt(encodingLength[i], 10); | ||
} | ||
sizeBuffer[sizeBuffer.length - 1] = 255; | ||
return doneCallback(null, Buffer.concat([sizeBuffer, stringToBuffer(packet)])); | ||
return doneCallback( | ||
null, | ||
Buffer.concat([sizeBuffer, stringToBuffer(packet)]) | ||
); | ||
} | ||
@@ -442,3 +449,3 @@ | ||
sizeBuffer[0] = 1; // is binary (true binary = 1) | ||
for (var i = 0; i < encodingLength.length; i++) { | ||
for (let i = 0; i < encodingLength.length; i++) { | ||
sizeBuffer[i + 1] = parseInt(encodingLength[i], 10); | ||
@@ -452,6 +459,4 @@ } | ||
exports.encodePacket(p, true, true, onBinaryPacketEncode); | ||
} | ||
/* | ||
@@ -466,4 +471,4 @@ * Decodes data when a payload is maybe expected. Strings are decoded by | ||
exports.decodePayloadAsBinary = function (data, binaryType, callback) { | ||
if (typeof binaryType === 'function') { | ||
exports.decodePayloadAsBinary = function(data, binaryType, callback) { | ||
if (typeof binaryType === "function") { | ||
callback = binaryType; | ||
@@ -473,11 +478,11 @@ binaryType = null; | ||
var bufferTail = data; | ||
var buffers = []; | ||
var i; | ||
let bufferTail = data; | ||
const buffers = []; | ||
let i; | ||
while (bufferTail.length > 0) { | ||
var strLen = ''; | ||
var isString = bufferTail[0] === 0; | ||
let strLen = ""; | ||
const isString = bufferTail[0] === 0; | ||
for (i = 1; ; i++) { | ||
if (bufferTail[i] === 255) break; | ||
if (bufferTail[i] === 255) break; | ||
// 310 = char length of Number.MAX_VALUE | ||
@@ -487,9 +492,9 @@ if (strLen.length > 310) { | ||
} | ||
strLen += '' + bufferTail[i]; | ||
strLen += "" + bufferTail[i]; | ||
} | ||
bufferTail = bufferTail.slice(strLen.length + 1); | ||
var msgLength = parseInt(strLen, 10); | ||
const msgLength = parseInt(strLen, 10); | ||
var msg = bufferTail.slice(1, msgLength + 1); | ||
let msg = bufferTail.slice(1, msgLength + 1); | ||
if (isString) msg = bufferToString(msg); | ||
@@ -500,7 +505,7 @@ buffers.push(msg); | ||
var total = buffers.length; | ||
const total = buffers.length; | ||
for (i = 0; i < total; i++) { | ||
var buffer = buffers[i]; | ||
const buffer = buffers[i]; | ||
callback(exports.decodePacket(buffer, binaryType, true), i, total); | ||
} | ||
}; |
316
lib/utf8.js
@@ -7,25 +7,26 @@ /*! https://mths.be/utf8js v2.1.2 by @mathias */ | ||
function ucs2decode(string) { | ||
var output = []; | ||
var counter = 0; | ||
var length = string.length; | ||
var value; | ||
var extra; | ||
while (counter < length) { | ||
value = string.charCodeAt(counter++); | ||
if (value >= 0xD800 && value <= 0xDBFF && counter < length) { | ||
// high surrogate, and there is a next character | ||
extra = string.charCodeAt(counter++); | ||
if ((extra & 0xFC00) == 0xDC00) { // low surrogate | ||
output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); | ||
} else { | ||
// unmatched surrogate; only append this code unit, in case the next | ||
// code unit is the high surrogate of a surrogate pair | ||
output.push(value); | ||
counter--; | ||
} | ||
} else { | ||
output.push(value); | ||
} | ||
} | ||
return output; | ||
var output = []; | ||
var counter = 0; | ||
var length = string.length; | ||
var value; | ||
var extra; | ||
while (counter < length) { | ||
value = string.charCodeAt(counter++); | ||
if (value >= 0xd800 && value <= 0xdbff && counter < length) { | ||
// high surrogate, and there is a next character | ||
extra = string.charCodeAt(counter++); | ||
if ((extra & 0xfc00) == 0xdc00) { | ||
// low surrogate | ||
output.push(((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000); | ||
} else { | ||
// unmatched surrogate; only append this code unit, in case the next | ||
// code unit is the high surrogate of a surrogate pair | ||
output.push(value); | ||
counter--; | ||
} | ||
} else { | ||
output.push(value); | ||
} | ||
} | ||
return output; | ||
} | ||
@@ -35,29 +36,30 @@ | ||
function ucs2encode(array) { | ||
var length = array.length; | ||
var index = -1; | ||
var value; | ||
var output = ''; | ||
while (++index < length) { | ||
value = array[index]; | ||
if (value > 0xFFFF) { | ||
value -= 0x10000; | ||
output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800); | ||
value = 0xDC00 | value & 0x3FF; | ||
} | ||
output += stringFromCharCode(value); | ||
} | ||
return output; | ||
var length = array.length; | ||
var index = -1; | ||
var value; | ||
var output = ""; | ||
while (++index < length) { | ||
value = array[index]; | ||
if (value > 0xffff) { | ||
value -= 0x10000; | ||
output += stringFromCharCode(((value >>> 10) & 0x3ff) | 0xd800); | ||
value = 0xdc00 | (value & 0x3ff); | ||
} | ||
output += stringFromCharCode(value); | ||
} | ||
return output; | ||
} | ||
function checkScalarValue(codePoint, strict) { | ||
if (codePoint >= 0xD800 && codePoint <= 0xDFFF) { | ||
if (strict) { | ||
throw Error( | ||
'Lone surrogate U+' + codePoint.toString(16).toUpperCase() + | ||
' is not a scalar value' | ||
); | ||
} | ||
return false; | ||
} | ||
return true; | ||
if (codePoint >= 0xd800 && codePoint <= 0xdfff) { | ||
if (strict) { | ||
throw Error( | ||
"Lone surrogate U+" + | ||
codePoint.toString(16).toUpperCase() + | ||
" is not a scalar value" | ||
); | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
@@ -67,43 +69,45 @@ /*--------------------------------------------------------------------------*/ | ||
function createByte(codePoint, shift) { | ||
return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80); | ||
return stringFromCharCode(((codePoint >> shift) & 0x3f) | 0x80); | ||
} | ||
function encodeCodePoint(codePoint, strict) { | ||
if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence | ||
return stringFromCharCode(codePoint); | ||
} | ||
var symbol = ''; | ||
if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence | ||
symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0); | ||
} | ||
else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence | ||
if (!checkScalarValue(codePoint, strict)) { | ||
codePoint = 0xFFFD; | ||
} | ||
symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0); | ||
symbol += createByte(codePoint, 6); | ||
} | ||
else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence | ||
symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0); | ||
symbol += createByte(codePoint, 12); | ||
symbol += createByte(codePoint, 6); | ||
} | ||
symbol += stringFromCharCode((codePoint & 0x3F) | 0x80); | ||
return symbol; | ||
if ((codePoint & 0xffffff80) == 0) { | ||
// 1-byte sequence | ||
return stringFromCharCode(codePoint); | ||
} | ||
var symbol = ""; | ||
if ((codePoint & 0xfffff800) == 0) { | ||
// 2-byte sequence | ||
symbol = stringFromCharCode(((codePoint >> 6) & 0x1f) | 0xc0); | ||
} else if ((codePoint & 0xffff0000) == 0) { | ||
// 3-byte sequence | ||
if (!checkScalarValue(codePoint, strict)) { | ||
codePoint = 0xfffd; | ||
} | ||
symbol = stringFromCharCode(((codePoint >> 12) & 0x0f) | 0xe0); | ||
symbol += createByte(codePoint, 6); | ||
} else if ((codePoint & 0xffe00000) == 0) { | ||
// 4-byte sequence | ||
symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xf0); | ||
symbol += createByte(codePoint, 12); | ||
symbol += createByte(codePoint, 6); | ||
} | ||
symbol += stringFromCharCode((codePoint & 0x3f) | 0x80); | ||
return symbol; | ||
} | ||
function utf8encode(string, opts) { | ||
opts = opts || {}; | ||
var strict = false !== opts.strict; | ||
opts = opts || {}; | ||
var strict = false !== opts.strict; | ||
var codePoints = ucs2decode(string); | ||
var length = codePoints.length; | ||
var index = -1; | ||
var codePoint; | ||
var byteString = ''; | ||
while (++index < length) { | ||
codePoint = codePoints[index]; | ||
byteString += encodeCodePoint(codePoint, strict); | ||
} | ||
return byteString; | ||
var codePoints = ucs2decode(string); | ||
var length = codePoints.length; | ||
var index = -1; | ||
var codePoint; | ||
var byteString = ""; | ||
while (++index < length) { | ||
codePoint = codePoints[index]; | ||
byteString += encodeCodePoint(codePoint, strict); | ||
} | ||
return byteString; | ||
} | ||
@@ -114,77 +118,77 @@ | ||
function readContinuationByte() { | ||
if (byteIndex >= byteCount) { | ||
throw Error('Invalid byte index'); | ||
} | ||
if (byteIndex >= byteCount) { | ||
throw Error("Invalid byte index"); | ||
} | ||
var continuationByte = byteArray[byteIndex] & 0xFF; | ||
byteIndex++; | ||
var continuationByte = byteArray[byteIndex] & 0xff; | ||
byteIndex++; | ||
if ((continuationByte & 0xC0) == 0x80) { | ||
return continuationByte & 0x3F; | ||
} | ||
if ((continuationByte & 0xc0) == 0x80) { | ||
return continuationByte & 0x3f; | ||
} | ||
// If we end up here, it’s not a continuation byte | ||
throw Error('Invalid continuation byte'); | ||
// If we end up here, it’s not a continuation byte | ||
throw Error("Invalid continuation byte"); | ||
} | ||
function decodeSymbol(strict) { | ||
var byte1; | ||
var byte2; | ||
var byte3; | ||
var byte4; | ||
var codePoint; | ||
var byte1; | ||
var byte2; | ||
var byte3; | ||
var byte4; | ||
var codePoint; | ||
if (byteIndex > byteCount) { | ||
throw Error('Invalid byte index'); | ||
} | ||
if (byteIndex > byteCount) { | ||
throw Error("Invalid byte index"); | ||
} | ||
if (byteIndex == byteCount) { | ||
return false; | ||
} | ||
if (byteIndex == byteCount) { | ||
return false; | ||
} | ||
// Read first byte | ||
byte1 = byteArray[byteIndex] & 0xFF; | ||
byteIndex++; | ||
// Read first byte | ||
byte1 = byteArray[byteIndex] & 0xff; | ||
byteIndex++; | ||
// 1-byte sequence (no continuation bytes) | ||
if ((byte1 & 0x80) == 0) { | ||
return byte1; | ||
} | ||
// 1-byte sequence (no continuation bytes) | ||
if ((byte1 & 0x80) == 0) { | ||
return byte1; | ||
} | ||
// 2-byte sequence | ||
if ((byte1 & 0xE0) == 0xC0) { | ||
byte2 = readContinuationByte(); | ||
codePoint = ((byte1 & 0x1F) << 6) | byte2; | ||
if (codePoint >= 0x80) { | ||
return codePoint; | ||
} else { | ||
throw Error('Invalid continuation byte'); | ||
} | ||
} | ||
// 2-byte sequence | ||
if ((byte1 & 0xe0) == 0xc0) { | ||
byte2 = readContinuationByte(); | ||
codePoint = ((byte1 & 0x1f) << 6) | byte2; | ||
if (codePoint >= 0x80) { | ||
return codePoint; | ||
} else { | ||
throw Error("Invalid continuation byte"); | ||
} | ||
} | ||
// 3-byte sequence (may include unpaired surrogates) | ||
if ((byte1 & 0xF0) == 0xE0) { | ||
byte2 = readContinuationByte(); | ||
byte3 = readContinuationByte(); | ||
codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3; | ||
if (codePoint >= 0x0800) { | ||
return checkScalarValue(codePoint, strict) ? codePoint : 0xFFFD; | ||
} else { | ||
throw Error('Invalid continuation byte'); | ||
} | ||
} | ||
// 3-byte sequence (may include unpaired surrogates) | ||
if ((byte1 & 0xf0) == 0xe0) { | ||
byte2 = readContinuationByte(); | ||
byte3 = readContinuationByte(); | ||
codePoint = ((byte1 & 0x0f) << 12) | (byte2 << 6) | byte3; | ||
if (codePoint >= 0x0800) { | ||
return checkScalarValue(codePoint, strict) ? codePoint : 0xfffd; | ||
} else { | ||
throw Error("Invalid continuation byte"); | ||
} | ||
} | ||
// 4-byte sequence | ||
if ((byte1 & 0xF8) == 0xF0) { | ||
byte2 = readContinuationByte(); | ||
byte3 = readContinuationByte(); | ||
byte4 = readContinuationByte(); | ||
codePoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0C) | | ||
(byte3 << 0x06) | byte4; | ||
if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) { | ||
return codePoint; | ||
} | ||
} | ||
// 4-byte sequence | ||
if ((byte1 & 0xf8) == 0xf0) { | ||
byte2 = readContinuationByte(); | ||
byte3 = readContinuationByte(); | ||
byte4 = readContinuationByte(); | ||
codePoint = | ||
((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4; | ||
if (codePoint >= 0x010000 && codePoint <= 0x10ffff) { | ||
return codePoint; | ||
} | ||
} | ||
throw Error('Invalid UTF-8 detected'); | ||
throw Error("Invalid UTF-8 detected"); | ||
} | ||
@@ -196,20 +200,20 @@ | ||
function utf8decode(byteString, opts) { | ||
opts = opts || {}; | ||
var strict = false !== opts.strict; | ||
opts = opts || {}; | ||
var strict = false !== opts.strict; | ||
byteArray = ucs2decode(byteString); | ||
byteCount = byteArray.length; | ||
byteIndex = 0; | ||
var codePoints = []; | ||
var tmp; | ||
while ((tmp = decodeSymbol(strict)) !== false) { | ||
codePoints.push(tmp); | ||
} | ||
return ucs2encode(codePoints); | ||
byteArray = ucs2decode(byteString); | ||
byteCount = byteArray.length; | ||
byteIndex = 0; | ||
var codePoints = []; | ||
var tmp; | ||
while ((tmp = decodeSymbol(strict)) !== false) { | ||
codePoints.push(tmp); | ||
} | ||
return ucs2encode(codePoints); | ||
} | ||
module.exports = { | ||
version: '2.1.2', | ||
encode: utf8encode, | ||
decode: utf8decode | ||
version: "2.1.2", | ||
encode: utf8encode, | ||
decode: utf8decode | ||
}; |
@@ -5,10 +5,17 @@ { | ||
"license": "MIT", | ||
"version": "2.2.0", | ||
"version": "4.0.0-alpha.0", | ||
"main": "lib/index.js", | ||
"homepage": "https://github.com/socketio/engine.io-parser", | ||
"devDependencies": { | ||
"@babel/core": "^7.8.3", | ||
"@babel/preset-env": "^7.8.3", | ||
"babel-eslint": "^10.0.3", | ||
"babelify": "^10.0.0", | ||
"benchmark": "^2.1.4", | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.9.0", | ||
"expect.js": "0.3.1", | ||
"mocha": "^5.2.0", | ||
"socket.io-browsers": "^1.0.2", | ||
"prettier": "^1.19.1", | ||
"socket.io-browsers": "^1.0.4", | ||
"zuul": "3.11.1", | ||
@@ -25,3 +32,6 @@ "zuul-ngrok": "4.0.0" | ||
"scripts": { | ||
"test": "make test" | ||
"test": "npm run lint && npm run format:check && make test", | ||
"format:check": "prettier --check 'lib/**/*.js' 'test/**/*.js'", | ||
"format:fix": "prettier --write 'lib/**/*.js' 'test/**/*.js'", | ||
"lint": "eslint 'lib/**/*.js' 'test/**/*.js'" | ||
}, | ||
@@ -35,3 +45,6 @@ "repository": { | ||
], | ||
"browser": "./lib/browser.js" | ||
"browser": "./lib/browser.js", | ||
"engines": { | ||
"node": ">=8.0.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39213
1093
13
6
1