|
1 | 1 | import { Emitter } from "@socket.io/component-emitter"; |
| 2 | +import { parse, stringify } from "flatted"; |
2 | 3 | import { deconstructPacket, reconstructPacket } from "./binary.js"; |
3 | 4 | import { isBinary, hasBinary } from "./is-binary.js"; |
4 | 5 | import debugModule from "debug"; // debug() |
@@ -39,7 +40,7 @@ export class Encoder { |
39 | 40 | /** |
40 | 41 | * Encoder constructor |
41 | 42 | * |
42 | | - * @param {function} replacer - custom replacer to pass down to JSON.parse |
| 43 | + * @param {function} replacer - custom replacer to pass down to JSON.stringify |
43 | 44 | */ |
44 | 45 | constructor(private replacer?: (this: any, key: string, value: any) => any) {} |
45 | 46 | /** |
@@ -92,7 +93,7 @@ export class Encoder { |
92 | 93 |
|
93 | 94 | // json data |
94 | 95 | if (null != obj.data) { |
95 | | - str += JSON.stringify(obj.data, this.replacer); |
| 96 | + str += stringify(obj.data, this.replacer); |
96 | 97 | } |
97 | 98 |
|
98 | 99 | debug("encoded %j as %s", obj, str); |
@@ -130,7 +131,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> { |
130 | 131 | /** |
131 | 132 | * Decoder constructor |
132 | 133 | * |
133 | | - * @param {function} reviver - custom reviver to pass down to JSON.stringify |
| 134 | + * @param {function} reviver - custom reviver to pass down to JSON.parse |
134 | 135 | */ |
135 | 136 | constructor(private reviver?: (this: any, key: string, value: any) => any) { |
136 | 137 | super(); |
@@ -256,7 +257,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> { |
256 | 257 |
|
257 | 258 | private tryParse(str) { |
258 | 259 | try { |
259 | | - return JSON.parse(str, this.reviver); |
| 260 | + return parse(str, this.reviver); |
260 | 261 | } catch (e) { |
261 | 262 | return false; |
262 | 263 | } |
|
0 commit comments