Skip to content

Commit 3a6d94d

Browse files
papandreoudarrachequesne
authored andcommitted
feat: add support for the toJSON() method when encoding (#113)
Example with BigInt values: ```js BigInt.prototype.toJSON = function () { return String(this); }; emitter.emit("foo", 42n); ``` Important note! There is a non backward-compatible change regarding Date objects, which means that the adapter may not be able to properly decode them. Reference: https://github.com/darrachequesne/notepack/releases/tag/3.0.0 Diff: darrachequesne/notepack@2.3.0...3.0.1
1 parent d62af2c commit 3a6d94d

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"dependencies": {
2222
"debug": "~4.3.1",
23-
"notepack.io": "~2.1.0",
23+
"notepack.io": "~3.0.1",
2424
"socket.io-parser": "~4.0.4"
2525
},
2626
"devDependencies": {

test/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,34 @@ describe("emitter", () => {
9696
});
9797
});
9898

99+
it("should support the toJSON() method", (done) => {
100+
// @ts-ignore
101+
BigInt.prototype.toJSON = function () {
102+
return String(this);
103+
};
104+
105+
// @ts-ignore
106+
Set.prototype.toJSON = function () {
107+
return [...this];
108+
};
109+
110+
class MyClass {
111+
toJSON() {
112+
return 4;
113+
}
114+
}
115+
116+
// @ts-ignore
117+
emitter.emit("payload", 1n, new Set(["2", 3]), new MyClass());
118+
119+
clientSockets[0].on("payload", (a, b, c) => {
120+
expect(a).to.eql("1");
121+
expect(b).to.eql(["2", 3]);
122+
expect(c).to.eql(4);
123+
done();
124+
});
125+
});
126+
99127
it("should support all broadcast modifiers", () => {
100128
emitter.in(["room1", "room2"]).emit("test");
101129
emitter.except(["room4", "room5"]).emit("test");

0 commit comments

Comments
 (0)