Skip to content

Commit 7a0c2b5

Browse files
fix: include the path in the manager ID
Previously, the following code: ```js const socket1 = io({ path: "/test1" }); const socket2 = io({ path: "/test2" }); ``` would result in one single Manager, with the "/test2" path being silently ignored. Two distinct Manager instances will now be created. Related: #1225
1 parent 61afc5d commit 7a0c2b5

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function lookup(
4747

4848
opts = opts || {};
4949

50-
const parsed = url(uri as string);
50+
const parsed = url(uri as string, opts.path);
5151
const source = parsed.source;
5252
const id = parsed.id;
5353
const path = parsed.path;

lib/url.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ type ParsedUrl = {
2929
* URL parser.
3030
*
3131
* @param uri - url
32+
* @param path - the request path of the connection
3233
* @param loc - An object meant to mimic window.location.
3334
* Defaults to window.location.
3435
* @public
3536
*/
3637

37-
export function url(uri: string | ParsedUrl, loc?: Location): ParsedUrl {
38+
export function url(
39+
uri: string | ParsedUrl,
40+
path: string = "",
41+
loc?: Location
42+
): ParsedUrl {
3843
let obj = uri as ParsedUrl;
3944

4045
// default to window.location
@@ -80,7 +85,7 @@ export function url(uri: string | ParsedUrl, loc?: Location): ParsedUrl {
8085
const host = ipv6 ? "[" + obj.host + "]" : obj.host;
8186

8287
// define unique id
83-
obj.id = obj.protocol + "://" + host + ":" + obj.port;
88+
obj.id = obj.protocol + "://" + host + ":" + obj.port + path;
8489
// define href
8590
obj.href =
8691
obj.protocol +

test/url.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe("url", () => {
99
loc.protocol = "https:";
1010
loc.port = 4005;
1111
loc.host = loc.hostname + ":" + loc.port;
12-
const parsed = url(undefined, loc);
12+
const parsed = url(undefined, undefined, loc);
1313
expect(parsed.host).to.be("woot.com");
1414
expect(parsed.protocol).to.be("https");
1515
expect(parsed.port).to.be("4005");
@@ -20,23 +20,23 @@ describe("url", () => {
2020
loc.protocol = "https:";
2121
loc.port = 3000;
2222
loc.host = loc.hostname + ":" + loc.port;
23-
const parsed = url("/test", loc);
23+
const parsed = url("/test", undefined, loc);
2424
expect(parsed.host).to.be("woot.com");
2525
expect(parsed.protocol).to.be("https");
2626
expect(parsed.port).to.be("3000");
2727
});
2828

2929
it("works with no protocol", () => {
3030
loc.protocol = "http:";
31-
const parsed = url("localhost:3000", loc);
31+
const parsed = url("localhost:3000", undefined, loc);
3232
expect(parsed.host).to.be("localhost");
3333
expect(parsed.port).to.be("3000");
3434
expect(parsed.protocol).to.be("http");
3535
});
3636

3737
it("works with no schema", () => {
3838
loc.protocol = "http:";
39-
const parsed = url("//localhost:3000", loc);
39+
const parsed = url("//localhost:3000", undefined, loc);
4040
expect(parsed.host).to.be("localhost");
4141
expect(parsed.port).to.be("3000");
4242
expect(parsed.protocol).to.be("http");
@@ -46,16 +46,18 @@ describe("url", () => {
4646
const id1 = url("http://google.com:80/");
4747
const id2 = url("http://google.com/");
4848
const id3 = url("https://google.com/");
49+
const id4 = url("http://google.com/", "/test");
4950
expect(id1.id).to.be(id2.id);
5051
expect(id1.id).to.not.be(id3.id);
5152
expect(id2.id).to.not.be(id3.id);
53+
expect(id2.id).to.not.be(id4.id);
5254
});
5355

5456
it("identifies the namespace", () => {
5557
loc.protocol = "http:";
5658
loc.hostname = "woot.com";
5759

58-
expect(url("/woot", loc).path).to.be("/woot");
60+
expect(url("/woot", undefined, loc).path).to.be("/woot");
5961
expect(url("http://google.com").path).to.be("/");
6062
expect(url("http://google.com/").path).to.be("/");
6163
});
@@ -74,10 +76,16 @@ describe("url", () => {
7476
loc.port = "";
7577
loc.host = loc.hostname + ":" + loc.port;
7678

77-
const parsed = url(undefined, loc);
79+
const parsed = url(undefined, undefined, loc);
7880
expect(parsed.protocol).to.be("http");
7981
expect(parsed.host).to.be("::1");
8082
expect(parsed.port).to.be("80");
8183
expect(parsed.id).to.be("http://[::1]:80");
8284
});
85+
86+
it("works with a custom path", function () {
87+
const parsed = url("https://woot.com/some-namespace", "/some-path");
88+
expect(parsed.id).to.be("https://woot.com:443/some-path");
89+
expect(parsed.path).to.be("/some-namespace");
90+
});
8391
});

0 commit comments

Comments
 (0)