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

feat: deprecate HttpAgent constructor in favor of new create #873

Merged
merged 7 commits into from
Jun 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
- docs: adds instructions on how to run unit and e2e tests to the README
- chore: adds required `npm audit` check to PRs
- new `HttpAgent` option: `backoffStrategy` - allows you to set a custom delay strategy for retries. The default is a newly exported `exponentialBackoff`, but you can pass your own function to customize the delay between retries.
- feat!: deprecate `HttpAgent` constructor in favor of new `create` and `createSync` methods.
- `create` is async and returns a promise. It will sync time with the replica and fetch the root key if the host is not `https://icp-api.io`
- Replaces `source` option with a `from` and `fromSync` methods, similar to `Principal.from`

### Changed

Expand All @@ -42,7 +45,7 @@

- feat: make `IdbStorage` `get/set` methods generic
- chore: add context to errors thrown when failing to decode CBOR values.
- chore: replaces globle npm install with setup-node for size-limit action
- chore: replaces global npm install with setup-node for size-limit action

## [1.2.0] - 2024-03-25

Expand Down
24 changes: 12 additions & 12 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ test('readState should not call transformers if request is passed', async () =>
test('redirect avoid', async () => {
function checkUrl(base: string, result: string) {
const httpAgent = new HttpAgent({ host: base });
expect(httpAgent['_host'].hostname).toBe(result);
expect(httpAgent.host.hostname).toBe(result);
}

checkUrl('https://ic0.app', 'ic0.app');
Expand Down Expand Up @@ -714,12 +714,12 @@ 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() });
expect((agent as any)._host.hostname).toBe('icp-api.io');
expect((agent as any).host.hostname).toBe('icp-api.io');
});
it('should use a default of icp-api.io if location is not available', () => {
delete (global as any).window;
const agent = new HttpAgent({ fetch: jest.fn() });
expect((agent as any)._host.hostname).toBe('icp-api.io');
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'];
Expand All @@ -729,8 +729,8 @@ describe('default host', () => {
hostname: host,
protocol: 'https:',
} as any;
const agent = new HttpAgent({ fetch: jest.fn(), host });
expect((agent as any)._host.hostname).toBe(host);
const agent = HttpAgent.createSync({ fetch: jest.fn(), host });
expect((agent as any).host.hostname).toBe(host);
}
});
it('should correctly handle subdomains on known hosts', () => {
Expand All @@ -743,10 +743,10 @@ describe('default host', () => {
protocol: 'https:',
} as any;
const agent = new HttpAgent({ fetch: jest.fn() });
expect((agent as any)._host.hostname).toBe(host);
expect(agent.host.hostname).toBe(host);
}
});
it('should correctly handle subdomains on remote hosts', () => {
it('should correctly handle subdomains on remote hosts', async () => {
const remoteHosts = [
'000.gitpod.io',
'000.github.dev',
Expand All @@ -760,11 +760,11 @@ describe('default host', () => {
hostname: host,
protocol: 'https:',
} as any;
const agent = new HttpAgent({ fetch: jest.fn() });
expect((agent as any)._host.hostname).toBe(host);
const agent = await HttpAgent.createSync({ fetch: jest.fn() });
expect(agent.host.toString()).toBe(`https://${host}/`);
}
});
it('should handle port numbers for 127.0.0.1 and localhost', () => {
it('should handle port numbers for 127.0.0.1 and localhost', async () => {
const knownHosts = ['127.0.0.1', 'localhost'];
for (const host of knownHosts) {
delete (window as any).location;
Expand All @@ -775,8 +775,8 @@ describe('default host', () => {
protocol: 'http:',
port: '4943',
} as any;
const agent = new HttpAgent({ fetch: jest.fn() });
expect((agent as any)._host.hostname).toBe(host);
const agent = await HttpAgent.createSync({ fetch: jest.fn() });
expect(agent.host.toString()).toBe(`http://${host}:4943/`);
}
});
});
Expand Down
Loading
Loading