Skip to content

Commit 6d58a75

Browse files
fix: properly track events with acknowledgement
"event_received" events did include the acknowledgement function, which would break during encoding. Related: - #48 - #54
1 parent a8ce526 commit 6d58a75

File tree

4 files changed

+182
-1
lines changed

4 files changed

+182
-1
lines changed

lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ const registerVerboseListeners = (
368368
if (nsp !== adminNamespace) {
369369
if (typeof socket.onAny === "function") {
370370
socket.onAny((...args: any[]) => {
371+
const withAck = typeof args[args.length - 1] === "function";
372+
if (withAck) {
373+
args = args.slice(0, -1);
374+
}
371375
adminNamespace.emit(
372376
"event_received",
373377
nsp.name,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"scripts": {
1212
"compile": "tsc",
1313
"test": "npm run format:check && npm run compile && npm run test:unit",
14-
"test:unit": "nyc mocha --require ts-node/register --timeout 5000 test/index.ts --quit",
14+
"test:unit": "nyc mocha --require ts-node/register --timeout 5000 test/*.ts --quit",
1515
"format:check": "prettier --parser typescript --check 'lib/**/*.ts' 'test/**/*.ts'",
1616
"format:fix": "prettier --parser typescript --write 'lib/**/*.ts' 'test/**/*.ts'",
1717
"prepack": "npm run compile"

test/events.ts

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import { Server } from "socket.io";
2+
import { createServer } from "http";
3+
import { AddressInfo } from "net";
4+
import { instrument } from "../lib";
5+
import { io as ioc } from "socket.io-client";
6+
import expect = require("expect.js");
7+
import { createPartialDone } from "./util";
8+
9+
describe("events", () => {
10+
let port: number, io: Server;
11+
12+
beforeEach((done) => {
13+
const httpServer = createServer();
14+
io = new Server(httpServer);
15+
16+
httpServer.listen(() => {
17+
port = (httpServer.address() as AddressInfo).port;
18+
done();
19+
});
20+
});
21+
22+
afterEach(() => {
23+
io.close();
24+
});
25+
26+
it("should track events sent to the client", (done) => {
27+
instrument(io, {
28+
auth: false,
29+
});
30+
31+
const adminSocket = ioc(`http://localhost:${port}/admin`);
32+
const clientSocket = ioc(`http://localhost:${port}`, {
33+
forceNew: true,
34+
});
35+
36+
const partialDone = createPartialDone(2, () => {
37+
adminSocket.disconnect();
38+
clientSocket.disconnect();
39+
done();
40+
});
41+
42+
io.on("connection", (socket) => {
43+
socket.emit("hello", 1, "2", Buffer.from([3]));
44+
});
45+
46+
clientSocket.on("hello", partialDone);
47+
48+
adminSocket.on("event_sent", (arg1, arg2, arg3, arg4) => {
49+
expect(arg1).to.eql("/");
50+
expect(arg2).to.eql(clientSocket.id);
51+
expect(arg3).to.eql(["hello", 1, "2", Buffer.from([3])]);
52+
expect(new Date(arg4).toISOString()).to.eql(arg4);
53+
54+
partialDone();
55+
});
56+
});
57+
58+
it("should track events sent to the client (with ack)", (done) => {
59+
instrument(io, {
60+
auth: false,
61+
});
62+
63+
const adminSocket = ioc(`http://localhost:${port}/admin`);
64+
const clientSocket = ioc(`http://localhost:${port}`, {
65+
forceNew: true,
66+
});
67+
68+
const partialDone = createPartialDone(2, () => {
69+
adminSocket.disconnect();
70+
clientSocket.disconnect();
71+
done();
72+
});
73+
74+
io.on("connection", (socket) => {
75+
socket.emit("hello", (arg: string) => {
76+
expect(arg).to.eql("world");
77+
78+
partialDone();
79+
});
80+
});
81+
82+
clientSocket.on("hello", (cb) => {
83+
cb("world");
84+
});
85+
86+
adminSocket.on("event_sent", (arg1, arg2, arg3, arg4) => {
87+
expect(arg1).to.eql("/");
88+
expect(arg2).to.eql(clientSocket.id);
89+
expect(arg3).to.eql(["hello"]);
90+
expect(new Date(arg4).toISOString()).to.eql(arg4);
91+
92+
partialDone();
93+
});
94+
});
95+
96+
it("should track events received from the client", (done) => {
97+
instrument(io, {
98+
auth: false,
99+
});
100+
101+
const adminSocket = ioc(`http://localhost:${port}/admin`);
102+
const clientSocket = ioc(`http://localhost:${port}`, {
103+
forceNew: true,
104+
});
105+
106+
const partialDone = createPartialDone(2, () => {
107+
adminSocket.disconnect();
108+
clientSocket.disconnect();
109+
done();
110+
});
111+
112+
io.on("connection", (socket) => {
113+
socket.on("hello", partialDone);
114+
});
115+
116+
adminSocket.on("event_received", (arg1, arg2, arg3, arg4) => {
117+
expect(arg1).to.eql("/");
118+
expect(arg2).to.eql(clientSocket.id);
119+
expect(arg3).to.eql(["hello", 1, "2", Buffer.from([3])]);
120+
expect(new Date(arg4).toISOString()).to.eql(arg4);
121+
122+
partialDone();
123+
});
124+
125+
clientSocket.emit("hello", 1, "2", Buffer.from([3]));
126+
});
127+
128+
it("should track events received from the client (with ack)", (done) => {
129+
instrument(io, {
130+
auth: false,
131+
});
132+
133+
const adminSocket = ioc(`http://localhost:${port}/admin`);
134+
const clientSocket = ioc(`http://localhost:${port}`, {
135+
forceNew: true,
136+
});
137+
138+
const partialDone = createPartialDone(2, () => {
139+
adminSocket.disconnect();
140+
clientSocket.disconnect();
141+
done();
142+
});
143+
144+
io.on("connection", (socket) => {
145+
socket.on("hello", (arg, cb) => {
146+
expect(arg).to.eql("world");
147+
148+
cb("123");
149+
});
150+
});
151+
152+
adminSocket.on("event_received", (arg1, arg2, arg3, arg4) => {
153+
expect(arg1).to.eql("/");
154+
expect(arg2).to.eql(clientSocket.id);
155+
expect(arg3).to.eql(["hello", "world"]);
156+
expect(new Date(arg4).toISOString()).to.eql(arg4);
157+
158+
partialDone();
159+
});
160+
161+
clientSocket.emit("hello", "world", (arg: string) => {
162+
expect(arg).to.eql("123");
163+
164+
partialDone();
165+
});
166+
});
167+
});

test/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function createPartialDone(count: number, done: (err?: Error) => void) {
2+
let i = 0;
3+
return () => {
4+
if (++i === count) {
5+
done();
6+
} else if (i > count) {
7+
done(new Error(`partialDone() called too many times: ${i} > ${count}`));
8+
}
9+
};
10+
}

0 commit comments

Comments
 (0)