Skip to content

Commit

Permalink
feat: implement parser based on devalue (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rChaoz authored Mar 22, 2024
1 parent 9f76ea2 commit 90a0fd4
Show file tree
Hide file tree
Showing 18 changed files with 6,456 additions and 5,331 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
strategy:
matrix:
node-version:
- 12
- 20

steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist/
build/
.idea/
27 changes: 11 additions & 16 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@

# socket.io-parser

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

A socket.io encoder and decoder written in JavaScript complying with version `5`
of [socket.io-protocol](https://github.com/socketio/socket.io-protocol).
Used by [socket.io](https://github.com/automattic/socket.io) and
[socket.io-client](https://github.com/automattic/socket.io-client).

Compatibility table:

| Parser version | Socket.IO server version | Protocol revision |
|----------------| ------------------------ | ----------------- |
| 3.x | 1.x / 2.x | 4 |
| 4.x | 3.x | 5 |
A fork of the default socket.io encoder and decoder, which uses
[devalue](https://github.com/Rich-Harris/devalue) over the vanilla
[JSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) object,
for stringifying and parsing JavaScript objects.
This offers many improvements over the default parser, such as support for
dates, maps, sets, regular expressions, `undefined`, custom classes and more.
Check out [devalue](https://github.com/Rich-Harris/devalue) for details.


## Parser API
Expand All @@ -28,7 +23,7 @@ Compatibility table:
### Encoding and decoding a packet

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

```js
var parser = require('socket.io-parser');
var parser = require('socket.io-devalue-parser');
var encoder = new parser.Encoder();
var packet = {
type: parser.BINARY_EVENT,
Expand All @@ -73,7 +68,7 @@ encoder.encode(packet, function(encodedPackets) {
}
});
```
See the test suite for more examples of how socket.io-parser is used.
See the test suite for more examples of how socket.io-devalue-parser is used.


## License
Expand Down
17 changes: 9 additions & 8 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Emitter } from "@socket.io/component-emitter";
import { deconstructPacket, reconstructPacket } from "./binary.js";
import { isBinary, hasBinary } from "./is-binary.js";
import { hasBinary, isBinary } from "./is-binary.js";
import debugModule from "debug"; // debug()
import * as devalue from "devalue";

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

Expand Down Expand Up @@ -51,9 +52,10 @@ export class Encoder {
/**
* Encoder constructor
*
* @param {function} replacer - custom replacer to pass down to JSON.parse
* @param {Object?} reducers - custom reducers to pass down to `devalue.stringify`
*/
constructor(private replacer?: (this: any, key: string, value: any) => any) {}
constructor(private reducers?: Record<string, (value: any) => any>) {}

/**
* Encode a packet as a single string if non-binary, or as a
* buffer sequence, depending on packet type.
Expand Down Expand Up @@ -108,7 +110,7 @@ export class Encoder {

// json data
if (null != obj.data) {
str += JSON.stringify(obj.data, this.replacer);
str += devalue.stringify(obj.data, this.reducers);
}

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

Expand All @@ -157,7 +159,6 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
*
* @param {String} obj - encoded packet
*/

public add(obj: any) {
let packet;
if (typeof obj === "string") {
Expand Down Expand Up @@ -271,7 +272,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {

private tryParse(str) {
try {
return JSON.parse(str, this.reviver);
return devalue.parse(str, this.revivers);
} catch (e) {
return false;
}
Expand Down
Loading

0 comments on commit 90a0fd4

Please sign in to comment.