From e0aea699ff06b4cee345a49da130c1418280c270 Mon Sep 17 00:00:00 2001 From: Ryan Vandersmith Date: Tue, 23 Jan 2024 12:52:37 -0700 Subject: [PATCH] fix: distinguish remote dev environments from known hosts (#830) * Distinguish 'remoteHosts' from 'knownHosts' * Update changelog * Add new unit test for remote hosts * Rename local variables --- docs/generated/changelog.html | 4 +++- packages/agent/src/agent/http/http.test.ts | 22 ++++++++++++++++++++-- packages/agent/src/agent/http/index.ts | 16 +++++++--------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/docs/generated/changelog.html b/docs/generated/changelog.html index 4e6cc9bc..5c4b5226 100644 --- a/docs/generated/changelog.html +++ b/docs/generated/changelog.html @@ -11,7 +11,9 @@

Agent-JS Changelog

Version x.x.x

- +

Version 0.21.2

Version 0.21.1

diff --git a/packages/agent/src/agent/http/http.test.ts b/packages/agent/src/agent/http/http.test.ts index 0cd60949..ed323c46 100644 --- a/packages/agent/src/agent/http/http.test.ts +++ b/packages/agent/src/agent/http/http.test.ts @@ -733,7 +733,7 @@ describe('default host', () => { expect((agent as any)._host.hostname).toBe('icp-api.io'); }); it('should use the existing host if the agent is used on a known hostname', () => { - const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', 'localhost', 'github.dev', 'gitpod.io']; + const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', 'localhost']; for (const host of knownHosts) { delete (window as any).location; (window as any).location = { @@ -745,7 +745,7 @@ describe('default host', () => { } }); it('should correctly handle subdomains on known hosts', () => { - const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', 'localhost', 'github.dev', 'gitpod.io']; + const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', 'localhost']; for (const host of knownHosts) { delete (window as any).location; (window as any).location = { @@ -757,6 +757,24 @@ describe('default host', () => { expect((agent as any)._host.hostname).toBe(host); } }); + it('should correctly handle subdomains on remote hosts', () => { + const remoteHosts = [ + '000.gitpod.io', + '000.github.dev', + '4943-dfinity-candid-6715adkgujw.ws-us107.gitpod.io', + 'sturdy-space-rotary-phone-674vv99gxf4x9j-4943.app.github.dev', + ]; + for (const host of remoteHosts) { + delete (window as any).location; + (window as any).location = { + host: host, + hostname: host, + protocol: 'https:', + } as any; + const agent = new HttpAgent({ fetch: jest.fn() }); + expect((agent as any)._host.hostname).toBe(host); + } + }); it('should handle port numbers for 127.0.0.1 and localhost', () => { const knownHosts = ['127.0.0.1', 'localhost']; for (const host of knownHosts) { diff --git a/packages/agent/src/agent/http/index.ts b/packages/agent/src/agent/http/index.ts index 47cf7daf..36b6df53 100644 --- a/packages/agent/src/agent/http/index.ts +++ b/packages/agent/src/agent/http/index.ts @@ -224,18 +224,16 @@ export class HttpAgent implements Agent { ); } // Mainnet, local, and remote environments will have the api route available - const knownHosts = [ - 'ic0.app', - 'icp0.io', - '127.0.0.1', - 'localhost', - 'github.dev', - 'gitpod.io', - ]; + const knownHosts = ['ic0.app', 'icp0.io', '127.0.0.1', 'localhost']; + const remoteHosts = ['.github.dev', '.gitpod.io']; const hostname = location?.hostname; let knownHost; if (hostname && typeof hostname === 'string') { - knownHost = knownHosts.find(host => hostname.endsWith(host)); + if (remoteHosts.some(host => hostname.endsWith(host))) { + knownHost = hostname; + } else { + knownHost = knownHosts.find(host => hostname.endsWith(host)); + } } if (location && knownHost) {