Skip to content

Commit 90a0fd4

Browse files
authored
feat: implement parser based on devalue (#1)
Reference: https://github.com/Rich-Harris/devalue#readme
1 parent 9f76ea2 commit 90a0fd4

18 files changed

+6456
-5331
lines changed

.github/workflows/ci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ jobs:
1717
strategy:
1818
matrix:
1919
node-version:
20-
- 12
2120
- 20
2221

2322
steps:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist/
33
build/
4+
.idea/

Readme.md

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11

22
# socket.io-parser
33

4-
[![Build Status](https://github.com/socketio/socket.io-parser/workflows/CI/badge.svg)](https://github.com/socketio/socket.io-parser/actions)
5-
[![NPM version](https://badge.fury.io/js/socket.io-parser.svg)](http://badge.fury.io/js/socket.io-parser)
4+
[![Build Status](https://github.com/socketio/socket.io-devalue-parser/workflows/CI/badge.svg)](https://github.com/socketio/socket.io-devalue-parser/actions)
65

7-
A socket.io encoder and decoder written in JavaScript complying with version `5`
8-
of [socket.io-protocol](https://github.com/socketio/socket.io-protocol).
9-
Used by [socket.io](https://github.com/automattic/socket.io) and
10-
[socket.io-client](https://github.com/automattic/socket.io-client).
11-
12-
Compatibility table:
13-
14-
| Parser version | Socket.IO server version | Protocol revision |
15-
|----------------| ------------------------ | ----------------- |
16-
| 3.x | 1.x / 2.x | 4 |
17-
| 4.x | 3.x | 5 |
6+
A fork of the default socket.io encoder and decoder, which uses
7+
[devalue](https://github.com/Rich-Harris/devalue) over the vanilla
8+
[JSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) object,
9+
for stringifying and parsing JavaScript objects.
10+
This offers many improvements over the default parser, such as support for
11+
dates, maps, sets, regular expressions, `undefined`, custom classes and more.
12+
Check out [devalue](https://github.com/Rich-Harris/devalue) for details.
1813

1914

2015
## Parser API
@@ -28,7 +23,7 @@ Compatibility table:
2823
### Encoding and decoding a packet
2924

3025
```js
31-
var parser = require('socket.io-parser');
26+
var parser = require('socket.io-devalue-parser');
3227
var encoder = new parser.Encoder();
3328
var packet = {
3429
type: parser.EVENT,
@@ -52,7 +47,7 @@ encoder.encode(packet, function(encodedPackets) {
5247
### Encoding and decoding a packet with binary data
5348

5449
```js
55-
var parser = require('socket.io-parser');
50+
var parser = require('socket.io-devalue-parser');
5651
var encoder = new parser.Encoder();
5752
var packet = {
5853
type: parser.BINARY_EVENT,
@@ -73,7 +68,7 @@ encoder.encode(packet, function(encodedPackets) {
7368
}
7469
});
7570
```
76-
See the test suite for more examples of how socket.io-parser is used.
71+
See the test suite for more examples of how socket.io-devalue-parser is used.
7772

7873

7974
## License

lib/index.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Emitter } from "@socket.io/component-emitter";
22
import { deconstructPacket, reconstructPacket } from "./binary.js";
3-
import { isBinary, hasBinary } from "./is-binary.js";
3+
import { hasBinary, isBinary } from "./is-binary.js";
44
import debugModule from "debug"; // debug()
5+
import * as devalue from "devalue";
56

67
const debug = debugModule("socket.io-parser"); // debug()
78

@@ -51,9 +52,10 @@ export class Encoder {
5152
/**
5253
* Encoder constructor
5354
*
54-
* @param {function} replacer - custom replacer to pass down to JSON.parse
55+
* @param {Object?} reducers - custom reducers to pass down to `devalue.stringify`
5556
*/
56-
constructor(private replacer?: (this: any, key: string, value: any) => any) {}
57+
constructor(private reducers?: Record<string, (value: any) => any>) {}
58+
5759
/**
5860
* Encode a packet as a single string if non-binary, or as a
5961
* buffer sequence, depending on packet type.
@@ -108,7 +110,7 @@ export class Encoder {
108110

109111
// json data
110112
if (null != obj.data) {
111-
str += JSON.stringify(obj.data, this.replacer);
113+
str += devalue.stringify(obj.data, this.reducers);
112114
}
113115

114116
debug("encoded %j as %s", obj, str);
@@ -146,9 +148,9 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
146148
/**
147149
* Decoder constructor
148150
*
149-
* @param {function} reviver - custom reviver to pass down to JSON.stringify
151+
* @param {Object?} revivers - custom revivers to pass down to `devalue.parse`
150152
*/
151-
constructor(private reviver?: (this: any, key: string, value: any) => any) {
153+
constructor(private revivers?: Record<string, (value: any) => any>) {
152154
super();
153155
}
154156

@@ -157,7 +159,6 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
157159
*
158160
* @param {String} obj - encoded packet
159161
*/
160-
161162
public add(obj: any) {
162163
let packet;
163164
if (typeof obj === "string") {
@@ -271,7 +272,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
271272

272273
private tryParse(str) {
273274
try {
274-
return JSON.parse(str, this.reviver);
275+
return devalue.parse(str, this.revivers);
275276
} catch (e) {
276277
return false;
277278
}

0 commit comments

Comments
 (0)