socket.io-parser
Advanced tools
Comparing version
@@ -30,2 +30,10 @@ "use strict"; | ||
/** | ||
* Encoder constructor | ||
* | ||
* @param {function} replacer - custom replacer to pass down to JSON.parse | ||
*/ | ||
constructor(replacer) { | ||
this.replacer = replacer; | ||
} | ||
/** | ||
* Encode a packet as a single string if non-binary, or as a | ||
@@ -71,3 +79,3 @@ * buffer sequence, depending on packet type. | ||
if (null != obj.data) { | ||
str += JSON.stringify(obj.data); | ||
str += JSON.stringify(obj.data, this.replacer); | ||
} | ||
@@ -97,4 +105,10 @@ debug("encoded %j as %s", obj, str); | ||
class Decoder extends component_emitter_1.Emitter { | ||
constructor() { | ||
/** | ||
* Decoder constructor | ||
* | ||
* @param {function} reviver - custom reviver to pass down to JSON.stringify | ||
*/ | ||
constructor(reviver) { | ||
super(); | ||
this.reviver = reviver; | ||
} | ||
@@ -200,3 +214,3 @@ /** | ||
if (str.charAt(++i)) { | ||
const payload = tryParse(str.substr(i)); | ||
const payload = this.tryParse(str.substr(i)); | ||
if (Decoder.isPayloadValid(p.type, payload)) { | ||
@@ -212,2 +226,10 @@ p.data = payload; | ||
} | ||
tryParse(str) { | ||
try { | ||
return JSON.parse(str, this.reviver); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
static isPayloadValid(type, payload) { | ||
@@ -239,10 +261,2 @@ switch (type) { | ||
exports.Decoder = Decoder; | ||
function tryParse(str) { | ||
try { | ||
return JSON.parse(str); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
/** | ||
@@ -249,0 +263,0 @@ * A manager of a binary event's 'buffer sequence'. Should |
@@ -28,3 +28,10 @@ import { Emitter } from "@socket.io/component-emitter"; | ||
export declare class Encoder { | ||
private replacer?; | ||
/** | ||
* Encoder constructor | ||
* | ||
* @param {function} replacer - custom replacer to pass down to JSON.parse | ||
*/ | ||
constructor(replacer?: (this: any, key: string, value: any) => any); | ||
/** | ||
* Encode a packet as a single string if non-binary, or as a | ||
@@ -56,5 +63,11 @@ * buffer sequence, depending on packet type. | ||
export declare class Decoder extends Emitter<{}, {}, DecoderReservedEvents> { | ||
private reviver?; | ||
private reconstructor; | ||
constructor(); | ||
/** | ||
* Decoder constructor | ||
* | ||
* @param {function} reviver - custom reviver to pass down to JSON.stringify | ||
*/ | ||
constructor(reviver?: (this: any, key: string, value: any) => any); | ||
/** | ||
* Decodes an encoded packet string into packet JSON. | ||
@@ -72,2 +85,3 @@ * | ||
private decodeString; | ||
private tryParse; | ||
private static isPayloadValid; | ||
@@ -74,0 +88,0 @@ /** |
@@ -27,2 +27,10 @@ import { Emitter } from "@socket.io/component-emitter"; | ||
/** | ||
* Encoder constructor | ||
* | ||
* @param {function} replacer - custom replacer to pass down to JSON.parse | ||
*/ | ||
constructor(replacer) { | ||
this.replacer = replacer; | ||
} | ||
/** | ||
* Encode a packet as a single string if non-binary, or as a | ||
@@ -68,3 +76,3 @@ * buffer sequence, depending on packet type. | ||
if (null != obj.data) { | ||
str += JSON.stringify(obj.data); | ||
str += JSON.stringify(obj.data, this.replacer); | ||
} | ||
@@ -93,4 +101,10 @@ debug("encoded %j as %s", obj, str); | ||
export class Decoder extends Emitter { | ||
constructor() { | ||
/** | ||
* Decoder constructor | ||
* | ||
* @param {function} reviver - custom reviver to pass down to JSON.stringify | ||
*/ | ||
constructor(reviver) { | ||
super(); | ||
this.reviver = reviver; | ||
} | ||
@@ -196,3 +210,3 @@ /** | ||
if (str.charAt(++i)) { | ||
const payload = tryParse(str.substr(i)); | ||
const payload = this.tryParse(str.substr(i)); | ||
if (Decoder.isPayloadValid(p.type, payload)) { | ||
@@ -208,2 +222,10 @@ p.data = payload; | ||
} | ||
tryParse(str) { | ||
try { | ||
return JSON.parse(str, this.reviver); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
static isPayloadValid(type, payload) { | ||
@@ -234,10 +256,2 @@ switch (type) { | ||
} | ||
function tryParse(str) { | ||
try { | ||
return JSON.parse(str); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
/** | ||
@@ -244,0 +258,0 @@ * A manager of a binary event's 'buffer sequence'. Should |
@@ -28,3 +28,10 @@ import { Emitter } from "@socket.io/component-emitter"; | ||
export declare class Encoder { | ||
private replacer?; | ||
/** | ||
* Encoder constructor | ||
* | ||
* @param {function} replacer - custom replacer to pass down to JSON.parse | ||
*/ | ||
constructor(replacer?: (this: any, key: string, value: any) => any); | ||
/** | ||
* Encode a packet as a single string if non-binary, or as a | ||
@@ -56,5 +63,11 @@ * buffer sequence, depending on packet type. | ||
export declare class Decoder extends Emitter<{}, {}, DecoderReservedEvents> { | ||
private reviver?; | ||
private reconstructor; | ||
constructor(); | ||
/** | ||
* Decoder constructor | ||
* | ||
* @param {function} reviver - custom reviver to pass down to JSON.stringify | ||
*/ | ||
constructor(reviver?: (this: any, key: string, value: any) => any); | ||
/** | ||
* Decodes an encoded packet string into packet JSON. | ||
@@ -72,2 +85,3 @@ * | ||
private decodeString; | ||
private tryParse; | ||
private static isPayloadValid; | ||
@@ -74,0 +88,0 @@ /** |
@@ -25,2 +25,10 @@ import { Emitter } from "@socket.io/component-emitter"; | ||
/** | ||
* Encoder constructor | ||
* | ||
* @param {function} replacer - custom replacer to pass down to JSON.parse | ||
*/ | ||
constructor(replacer) { | ||
this.replacer = replacer; | ||
} | ||
/** | ||
* Encode a packet as a single string if non-binary, or as a | ||
@@ -65,3 +73,3 @@ * buffer sequence, depending on packet type. | ||
if (null != obj.data) { | ||
str += JSON.stringify(obj.data); | ||
str += JSON.stringify(obj.data, this.replacer); | ||
} | ||
@@ -89,4 +97,10 @@ return str; | ||
export class Decoder extends Emitter { | ||
constructor() { | ||
/** | ||
* Decoder constructor | ||
* | ||
* @param {function} reviver - custom reviver to pass down to JSON.stringify | ||
*/ | ||
constructor(reviver) { | ||
super(); | ||
this.reviver = reviver; | ||
} | ||
@@ -192,3 +206,3 @@ /** | ||
if (str.charAt(++i)) { | ||
const payload = tryParse(str.substr(i)); | ||
const payload = this.tryParse(str.substr(i)); | ||
if (Decoder.isPayloadValid(p.type, payload)) { | ||
@@ -203,2 +217,10 @@ p.data = payload; | ||
} | ||
tryParse(str) { | ||
try { | ||
return JSON.parse(str, this.reviver); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
static isPayloadValid(type, payload) { | ||
@@ -229,10 +251,2 @@ switch (type) { | ||
} | ||
function tryParse(str) { | ||
try { | ||
return JSON.parse(str); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
} | ||
/** | ||
@@ -239,0 +253,0 @@ * A manager of a binary event's 'buffer sequence'. Should |
{ | ||
"name": "socket.io-parser", | ||
"version": "4.1.2", | ||
"version": "4.2.0", | ||
"description": "socket.io protocol parser", | ||
@@ -23,3 +23,3 @@ "repository": { | ||
"dependencies": { | ||
"@socket.io/component-emitter": "~3.0.0", | ||
"@socket.io/component-emitter": "~3.1.0", | ||
"debug": "~4.3.1" | ||
@@ -26,0 +26,0 @@ }, |
51547
4.41%1489
4.93%+ Added
- Removed