Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: distinguish remote dev environments from known hosts #830

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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