1
1
import { Emitter } from "@socket.io/component-emitter" ;
2
2
import { deconstructPacket , reconstructPacket } from "./binary.js" ;
3
- import { isBinary , hasBinary } from "./is-binary.js" ;
3
+ import { hasBinary , isBinary } from "./is-binary.js" ;
4
4
import debugModule from "debug" ; // debug()
5
+ import * as devalue from "devalue" ;
5
6
6
7
const debug = debugModule ( "socket.io-parser" ) ; // debug()
7
8
@@ -51,9 +52,10 @@ export class Encoder {
51
52
/**
52
53
* Encoder constructor
53
54
*
54
- * @param {function } replacer - custom replacer to pass down to JSON.parse
55
+ * @param {Object? } reducers - custom reducers to pass down to `devalue.stringify`
55
56
*/
56
- constructor ( private replacer ?: ( this : any , key : string , value : any ) => any ) { }
57
+ constructor ( private reducers ?: Record < string , ( value : any ) => any > ) { }
58
+
57
59
/**
58
60
* Encode a packet as a single string if non-binary, or as a
59
61
* buffer sequence, depending on packet type.
@@ -108,7 +110,7 @@ export class Encoder {
108
110
109
111
// json data
110
112
if ( null != obj . data ) {
111
- str += JSON . stringify ( obj . data , this . replacer ) ;
113
+ str += devalue . stringify ( obj . data , this . reducers ) ;
112
114
}
113
115
114
116
debug ( "encoded %j as %s" , obj , str ) ;
@@ -146,9 +148,9 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
146
148
/**
147
149
* Decoder constructor
148
150
*
149
- * @param {function } reviver - custom reviver to pass down to JSON.stringify
151
+ * @param {Object? } revivers - custom revivers to pass down to `devalue.parse`
150
152
*/
151
- constructor ( private reviver ?: ( this : any , key : string , value : any ) => any ) {
153
+ constructor ( private revivers ?: Record < string , ( value : any ) => any > ) {
152
154
super ( ) ;
153
155
}
154
156
@@ -157,7 +159,6 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
157
159
*
158
160
* @param {String } obj - encoded packet
159
161
*/
160
-
161
162
public add ( obj : any ) {
162
163
let packet ;
163
164
if ( typeof obj === "string" ) {
@@ -271,7 +272,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
271
272
272
273
private tryParse ( str ) {
273
274
try {
274
- return JSON . parse ( str , this . reviver ) ;
275
+ return devalue . parse ( str , this . revivers ) ;
275
276
} catch ( e ) {
276
277
return false ;
277
278
}
0 commit comments