Skip to content

Commit

Permalink
fix: distinguish remote dev environments from known hosts (#830)
Browse files Browse the repository at this point in the history
* Distinguish 'remoteHosts' from 'knownHosts'

* Update changelog

* Add new unit test for remote hosts

* Rename local variables
  • Loading branch information
rvanasa authored Jan 23, 2024
1 parent cc489d9 commit e0aea69
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
4 changes: 3 additions & 1 deletion docs/generated/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ <h1>Agent-JS Changelog</h1>

<section>
<h2>Version x.x.x</h2>
<ul></ul>
<ul>
<li>fix: distinguish remote dev environments from known hosts</li>
</ul>
<h2>Version 0.21.2</h2>
<ul></ul>
<h2>Version 0.21.1</h2>
Expand Down
22 changes: 20 additions & 2 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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) {
Expand Down
16 changes: 7 additions & 9 deletions packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e0aea69

Please sign in to comment.