From a11e5fd77feb3d5e1ee86154127278cf1045dcfa Mon Sep 17 00:00:00 2001 From: Kyle Peacock Date: Fri, 25 Aug 2023 09:53:20 -0700 Subject: [PATCH 1/4] fix: subdomains on icp0.io correctly point to / --- docs/generated/changelog.html | 11 +++++++++++ packages/agent/src/agent/http/http.test.ts | 12 ++++++++++++ packages/agent/src/agent/http/index.ts | 6 ++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/generated/changelog.html b/docs/generated/changelog.html index 0aec23b41..3082cfcf9 100644 --- a/docs/generated/changelog.html +++ b/docs/generated/changelog.html @@ -11,6 +11,17 @@

Agent-JS Changelog

Version x.x.x

+ +

Version 0.19.2

+ +

Version 0.19.1

diff --git a/packages/agent/src/agent/http/http.test.ts b/packages/agent/src/agent/http/http.test.ts index 3adcda2e1..9a82f3ce2 100644 --- a/packages/agent/src/agent/http/http.test.ts +++ b/packages/agent/src/agent/http/http.test.ts @@ -783,4 +783,16 @@ describe('default host', () => { expect((agent as any)._host.hostname).toBe(host); } }); + it('should correctly handle subdomains on known hosts', () => { + const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1']; + for (const host of knownHosts) { + delete window.location; + window.location = { + hostname: `rrkah-fqaaa-aaaaa-aaaaq-cai.${host}`, + protocol: 'https:', + } as any; + const agent = new HttpAgent({ fetch: jest.fn() }); + expect((agent as any)._host.hostname).toBe(host); + } + }); }); diff --git a/packages/agent/src/agent/http/index.ts b/packages/agent/src/agent/http/index.ts index d1442f85b..4e6abbb4b 100644 --- a/packages/agent/src/agent/http/index.ts +++ b/packages/agent/src/agent/http/index.ts @@ -212,9 +212,11 @@ export class HttpAgent implements Agent { } // Mainnet and local will have the api route available const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1']; - if (location && knownHosts.includes(location.hostname)) { + + const knownHost = knownHosts.find(host => location?.hostname.endsWith(host)); + if (location && knownHost) { // If the user is on a boundary-node provided host, we can use the same host for the agent - this._host = new URL(location + ''); + this._host = new URL(`${location.protocol}//${knownHost}`); } else { this._host = new URL('https://icp-api.io'); console.warn( From 7d7a8c8bbed25937503b86efcc90535329c7dc96 Mon Sep 17 00:00:00 2001 From: Kyle Peacock Date: Fri, 25 Aug 2023 10:10:16 -0700 Subject: [PATCH 2/4] test cases for ports, verified on local deploy --- packages/agent/src/agent/http/http.test.ts | 16 ++++++++++++++++ packages/agent/src/agent/http/index.ts | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/agent/src/agent/http/http.test.ts b/packages/agent/src/agent/http/http.test.ts index 9a82f3ce2..7fa93b084 100644 --- a/packages/agent/src/agent/http/http.test.ts +++ b/packages/agent/src/agent/http/http.test.ts @@ -788,6 +788,7 @@ describe('default host', () => { for (const host of knownHosts) { delete window.location; window.location = { + host: `foo.${host}`, hostname: `rrkah-fqaaa-aaaaa-aaaaq-cai.${host}`, protocol: 'https:', } as any; @@ -795,4 +796,19 @@ describe('default host', () => { expect((agent as any)._host.hostname).toBe(host); } }); + it('should handle port numbers for localhost', () => { + const knownHosts = ['localhost', '127.0.0.1']; + for (const host of knownHosts) { + delete window.location; + // hostname is different from host when port is specified + window.location = { + host: `${host}:4943`, + hostname: `${host}`, + protocol: 'http:', + port: '4943', + } as any; + const agent = new HttpAgent({ fetch: jest.fn() }); + expect((agent as any)._host.hostname).toBe(host); + } + }); }); diff --git a/packages/agent/src/agent/http/index.ts b/packages/agent/src/agent/http/index.ts index 4e6abbb4b..41c26d03e 100644 --- a/packages/agent/src/agent/http/index.ts +++ b/packages/agent/src/agent/http/index.ts @@ -212,11 +212,15 @@ export class HttpAgent implements Agent { } // Mainnet and local will have the api route available const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1']; - const knownHost = knownHosts.find(host => location?.hostname.endsWith(host)); + location; if (location && knownHost) { // If the user is on a boundary-node provided host, we can use the same host for the agent - this._host = new URL(`${location.protocol}//${knownHost}`); + knownHost; + this._host = new URL( + `${location.protocol}//${knownHost}${location.port ? ':' + location.port : ''}`, + ); + this._host; //? } else { this._host = new URL('https://icp-api.io'); console.warn( From d41f5c395ddb3dcc4af77a3efc20ef1ab7eb71ae Mon Sep 17 00:00:00 2001 From: Kyle Peacock Date: Fri, 25 Aug 2023 10:11:04 -0700 Subject: [PATCH 3/4] cleaning up --- packages/agent/src/agent/http/http.test.ts | 1 - packages/agent/src/agent/http/index.ts | 3 --- 2 files changed, 4 deletions(-) diff --git a/packages/agent/src/agent/http/http.test.ts b/packages/agent/src/agent/http/http.test.ts index 7fa93b084..1df6284c7 100644 --- a/packages/agent/src/agent/http/http.test.ts +++ b/packages/agent/src/agent/http/http.test.ts @@ -763,7 +763,6 @@ test('should fetch with given call options and fetch options', async () => { describe('default host', () => { it('should use a default host of icp-api.io', () => { const agent = new HttpAgent({ fetch: jest.fn() }); - window.location.hostname; //? expect((agent as any)._host.hostname).toBe('icp-api.io'); }); it('should use a default of icp-api.io if location is not available', () => { diff --git a/packages/agent/src/agent/http/index.ts b/packages/agent/src/agent/http/index.ts index 41c26d03e..f00c9606e 100644 --- a/packages/agent/src/agent/http/index.ts +++ b/packages/agent/src/agent/http/index.ts @@ -213,14 +213,11 @@ export class HttpAgent implements Agent { // Mainnet and local will have the api route available const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1']; const knownHost = knownHosts.find(host => location?.hostname.endsWith(host)); - location; if (location && knownHost) { // If the user is on a boundary-node provided host, we can use the same host for the agent - knownHost; this._host = new URL( `${location.protocol}//${knownHost}${location.port ? ':' + location.port : ''}`, ); - this._host; //? } else { this._host = new URL('https://icp-api.io'); console.warn( From 664514b2381362bdf4f5693cb8edc5db8c3d545f Mon Sep 17 00:00:00 2001 From: Kyle Peacock Date: Fri, 25 Aug 2023 10:36:51 -0700 Subject: [PATCH 4/4] updating location mock in auth-client --- packages/agent/src/agent/http/index.ts | 7 ++++++- packages/auth-client/src/index.test.ts | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/agent/src/agent/http/index.ts b/packages/agent/src/agent/http/index.ts index f00c9606e..e4b5d1e30 100644 --- a/packages/agent/src/agent/http/index.ts +++ b/packages/agent/src/agent/http/index.ts @@ -212,7 +212,12 @@ export class HttpAgent implements Agent { } // Mainnet and local will have the api route available const knownHosts = ['ic0.app', 'icp0.io', 'localhost', '127.0.0.1']; - const knownHost = knownHosts.find(host => location?.hostname.endsWith(host)); + const hostname = location?.hostname; + let knownHost; + if (hostname && typeof hostname === 'string') { + knownHost = knownHosts.find(host => hostname.endsWith(host)); + } + if (location && knownHost) { // If the user is on a boundary-node provided host, we can use the same host for the agent this._host = new URL( diff --git a/packages/auth-client/src/index.test.ts b/packages/auth-client/src/index.test.ts index 56a78e003..4e249d1ba 100644 --- a/packages/auth-client/src/index.test.ts +++ b/packages/auth-client/src/index.test.ts @@ -84,6 +84,9 @@ describe('Auth Client', () => { (window as any).location = { reload: jest.fn(), fetch, + hostname: 'localhost', + protocol: 'http:', + port: '4943', toString: jest.fn(() => 'http://localhost:4943'), };