Skip to content

Commit e2b51c8

Browse files
committed
feat: use flatted instead of JSON for parse/stringify
1 parent 2908669 commit e2b51c8

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lib/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Emitter } from "@socket.io/component-emitter";
2+
import { parse, stringify } from "flatted";
23
import { deconstructPacket, reconstructPacket } from "./binary.js";
34
import { isBinary, hasBinary } from "./is-binary.js";
45
import debugModule from "debug"; // debug()
@@ -39,7 +40,7 @@ export class Encoder {
3940
/**
4041
* Encoder constructor
4142
*
42-
* @param {function} replacer - custom replacer to pass down to JSON.parse
43+
* @param {function} replacer - custom replacer to pass down to JSON.stringify
4344
*/
4445
constructor(private replacer?: (this: any, key: string, value: any) => any) {}
4546
/**
@@ -92,7 +93,7 @@ export class Encoder {
9293

9394
// json data
9495
if (null != obj.data) {
95-
str += JSON.stringify(obj.data, this.replacer);
96+
str += stringify(obj.data, this.replacer);
9697
}
9798

9899
debug("encoded %j as %s", obj, str);
@@ -130,7 +131,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
130131
/**
131132
* Decoder constructor
132133
*
133-
* @param {function} reviver - custom reviver to pass down to JSON.stringify
134+
* @param {function} reviver - custom reviver to pass down to JSON.parse
134135
*/
135136
constructor(private reviver?: (this: any, key: string, value: any) => any) {
136137
super();
@@ -256,7 +257,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
256257

257258
private tryParse(str) {
258259
try {
259-
return JSON.parse(str, this.reviver);
260+
return parse(str, this.reviver);
260261
} catch (e) {
261262
return false;
262263
}

0 commit comments

Comments
 (0)