Skip to content

Commit 6abfa1f

Browse files
KC Erbdarrachequesne
KC Erb
authored andcommitted
feat: add autoUnref option
With autoUnref set to true (default: false), the Socket.IO client will allow the program to exit if there is no other active timer/socket in the event system. ```js const socket = io({ autoUnref: true }); ``` Note: this option only applies to Node.js clients. Related: #1446
1 parent 5902365 commit 6abfa1f

File tree

10 files changed

+118
-13
lines changed

10 files changed

+118
-13
lines changed

lib/manager.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ export interface ManagerOptions extends EngineOptions {
256256
*/
257257
autoConnect: boolean;
258258

259+
/**
260+
* weather we should unref the reconnect timer when it is
261+
* create automatically
262+
* @default false
263+
*/
264+
autoUnref: boolean;
265+
259266
/**
260267
* the parser to use. Defaults to an instance of the Parser that ships with socket.io.
261268
*/
@@ -531,6 +538,10 @@ export class Manager<
531538
socket.emit("error", new Error("timeout"));
532539
}, timeout);
533540

541+
if (this.opts.autoUnref) {
542+
timer.unref();
543+
}
544+
534545
this.subs.push(function subDestroy(): void {
535546
clearTimeout(timer);
536547
});
@@ -769,6 +780,10 @@ export class Manager<
769780
});
770781
}, delay);
771782

783+
if (this.opts.autoUnref) {
784+
timer.unref();
785+
}
786+
772787
this.subs.push(function subDestroy() {
773788
clearTimeout(timer);
774789
});

package-lock.json

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

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"backo2": "~1.0.2",
3232
"component-emitter": "~1.3.0",
3333
"debug": "~4.3.1",
34-
"engine.io-client": "~4.1.0",
34+
"engine.io-client": "~5.0.0",
3535
"parseuri": "0.0.6",
3636
"socket.io-parser": "~4.0.4"
3737
},
@@ -108,5 +108,8 @@
108108
},
109109
"tsd": {
110110
"directory": "test"
111+
},
112+
"browser": {
113+
"./test/node.ts": false
111114
}
112115
}

test/fixtures/no-unref.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const io = require("../..");
2+
const socket = io("http://localhost:3211", {
3+
autoUnref: false,
4+
});
5+
6+
setTimeout(() => {
7+
console.log("process should not exit");
8+
}, 500);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const io = require("../..");
2+
const socket = io("http://localhost:3211", {
3+
autoUnref: true,
4+
});
5+
6+
socket.on("open", () => {
7+
console.log("open");
8+
});
9+
10+
setTimeout(() => {
11+
console.log("process should exit now");
12+
}, 500);

test/fixtures/unref-polling-only.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const io = require("../..");
2+
const socket = io("http://localhost:3210", {
3+
autoUnref: true,
4+
transports: ["polling"],
5+
});
6+
7+
socket.on("open", () => {
8+
console.log("open");
9+
});
10+
11+
setTimeout(() => {
12+
console.log("process should exit now");
13+
}, 500);

test/fixtures/unref-websocket-only.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const io = require("../..");
2+
const socket = io("http://localhost:3210", {
3+
autoUnref: true,
4+
transports: ["websocket"],
5+
});
6+
7+
socket.on("open", () => {
8+
console.log("open");
9+
});
10+
11+
setTimeout(() => {
12+
console.log("process should exit now");
13+
}, 500);

test/fixtures/unref.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const io = require("../..");
2+
const socket = io("http://localhost:3210", {
3+
autoUnref: true,
4+
});
5+
6+
socket.on("open", () => {
7+
console.log("open");
8+
});
9+
10+
setTimeout(() => {
11+
console.log("process should exit now");
12+
}, 500);

test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const { browser } = require("./support/env");
33
// whitelist some globals to avoid warnings
44
if (browser) {
55
window.mocha.globals(["___eio", "eio_iframe_*"]);
6+
} else {
7+
require("./node.ts");
68
}
79

810
require("./url.ts");

test/node.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const path = require("path");
2+
const { exec } = require("child_process");
3+
4+
describe("autoRef option", () => {
5+
const fixture = (filename) =>
6+
process.execPath + " " + path.join(__dirname, "fixtures", filename);
7+
8+
it("should stop once the timer is triggered", (done) => {
9+
exec(fixture("unref.ts"), done);
10+
});
11+
12+
it("should stop once the timer is triggered (even when trying to reconnect)", (done) => {
13+
exec(fixture("unref-during-reconnection.ts"), done);
14+
});
15+
16+
it("should stop once the timer is triggered (polling)", (done) => {
17+
exec(fixture("unref-polling-only.ts"), done);
18+
});
19+
20+
it("should stop once the timer is triggered (websocket)", (done) => {
21+
exec(fixture("unref-websocket-only.ts"), done);
22+
});
23+
24+
it("should not stop with autoUnref set to false", (done) => {
25+
const process = exec(fixture("no-unref.ts"), () => {
26+
done(new Error("should not happen"));
27+
});
28+
setTimeout(() => {
29+
process.kill();
30+
done();
31+
}, 1000);
32+
});
33+
});

0 commit comments

Comments
 (0)