Skip to content

Commit

Permalink
fix: include the path in the manager ID
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Feb 3, 2021
1 parent 61afc5d commit 7a0c2b5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/index.ts
Expand Up @@ -47,7 +47,7 @@ function lookup(

opts = opts || {};

const parsed = url(uri as string);
const parsed = url(uri as string, opts.path);
const source = parsed.source;
const id = parsed.id;
const path = parsed.path;
Expand Down
9 changes: 7 additions & 2 deletions lib/url.ts
Expand Up @@ -29,12 +29,17 @@ type ParsedUrl = {
* URL parser.
*
* @param uri - url
* @param path - the request path of the connection
* @param loc - An object meant to mimic window.location.
* Defaults to window.location.
* @public
*/

export function url(uri: string | ParsedUrl, loc?: Location): ParsedUrl {
export function url(
uri: string | ParsedUrl,
path: string = "",
loc?: Location
): ParsedUrl {
let obj = uri as ParsedUrl;

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

// define unique id
obj.id = obj.protocol + "://" + host + ":" + obj.port;
obj.id = obj.protocol + "://" + host + ":" + obj.port + path;
// define href
obj.href =
obj.protocol +
Expand Down
20 changes: 14 additions & 6 deletions test/url.ts
Expand Up @@ -9,7 +9,7 @@ describe("url", () => {
loc.protocol = "https:";
loc.port = 4005;
loc.host = loc.hostname + ":" + loc.port;
const parsed = url(undefined, loc);
const parsed = url(undefined, undefined, loc);
expect(parsed.host).to.be("woot.com");
expect(parsed.protocol).to.be("https");
expect(parsed.port).to.be("4005");
Expand All @@ -20,23 +20,23 @@ describe("url", () => {
loc.protocol = "https:";
loc.port = 3000;
loc.host = loc.hostname + ":" + loc.port;
const parsed = url("/test", loc);
const parsed = url("/test", undefined, loc);
expect(parsed.host).to.be("woot.com");
expect(parsed.protocol).to.be("https");
expect(parsed.port).to.be("3000");
});

it("works with no protocol", () => {
loc.protocol = "http:";
const parsed = url("localhost:3000", loc);
const parsed = url("localhost:3000", undefined, loc);
expect(parsed.host).to.be("localhost");
expect(parsed.port).to.be("3000");
expect(parsed.protocol).to.be("http");
});

it("works with no schema", () => {
loc.protocol = "http:";
const parsed = url("//localhost:3000", loc);
const parsed = url("//localhost:3000", undefined, loc);
expect(parsed.host).to.be("localhost");
expect(parsed.port).to.be("3000");
expect(parsed.protocol).to.be("http");
Expand All @@ -46,16 +46,18 @@ describe("url", () => {
const id1 = url("http://google.com:80/");
const id2 = url("http://google.com/");
const id3 = url("https://google.com/");
const id4 = url("http://google.com/", "/test");
expect(id1.id).to.be(id2.id);
expect(id1.id).to.not.be(id3.id);
expect(id2.id).to.not.be(id3.id);
expect(id2.id).to.not.be(id4.id);
});

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

expect(url("/woot", loc).path).to.be("/woot");
expect(url("/woot", undefined, loc).path).to.be("/woot");
expect(url("http://google.com").path).to.be("/");
expect(url("http://google.com/").path).to.be("/");
});
Expand All @@ -74,10 +76,16 @@ describe("url", () => {
loc.port = "";
loc.host = loc.hostname + ":" + loc.port;

const parsed = url(undefined, loc);
const parsed = url(undefined, undefined, loc);
expect(parsed.protocol).to.be("http");
expect(parsed.host).to.be("::1");
expect(parsed.port).to.be("80");
expect(parsed.id).to.be("http://[::1]:80");
});

it("works with a custom path", function () {
const parsed = url("https://woot.com/some-namespace", "/some-path");
expect(parsed.id).to.be("https://woot.com:443/some-path");
expect(parsed.path).to.be("/some-namespace");
});
});

0 comments on commit 7a0c2b5

Please sign in to comment.