Skip to content

Commit

Permalink
feat: support HTTP2.0
Browse files Browse the repository at this point in the history
closes #474
  • Loading branch information
fengmk2 committed Jun 26, 2024
1 parent b414140 commit 4c4a4c4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/HttpAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type HttpAgentOptions = {
lookup?: LookupFunction;
checkAddress?: CheckAddressFunction;
connect?: buildConnector.BuildOptions,
allowH2?: boolean;
};

class IllegalAddressError extends Error {
Expand Down Expand Up @@ -62,7 +63,7 @@ export class HttpAgent extends Agent {
});
};
super({
connect: { ...options.connect, lookup },
connect: { ...options.connect, lookup, allowH2: options.allowH2 },
});
this.#checkAddress = options.checkAddress;
}
Expand Down
9 changes: 9 additions & 0 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const isNode14Or16 = /v1[46]\./.test(process.version);

export type ClientOptions = {
defaultArgs?: RequestOptions;
/** Allow to use HTTP2 first. Default is `false` */
allowH2?: boolean;
/**
* Custom DNS lookup function, default is `dns.lookup`.
*/
Expand Down Expand Up @@ -187,10 +189,17 @@ export class HttpClient extends EventEmitter {
lookup: clientOptions.lookup,
checkAddress: clientOptions.checkAddress,
connect: clientOptions.connect,
allowH2: clientOptions.allowH2,
});
} else if (clientOptions?.connect) {
this.#dispatcher = new Agent({
connect: clientOptions.connect,
allowH2: clientOptions.allowH2,
});
} else if (clientOptions?.allowH2) {
// Support HTTP2
this.#dispatcher = new Agent({
allowH2: clientOptions.allowH2,
});
}
initDiagnosticsChannel();
Expand Down
17 changes: 16 additions & 1 deletion test/HttpClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { strict as assert } from 'node:assert';
import dns from 'node:dns';
import { sensitiveHeaders } from 'node:http2';
import { describe, it, beforeAll, afterAll } from 'vitest';
import { HttpClient } from '../src';
import { HttpClient, getGlobalDispatcher } from '../src';
import { RawResponseWithMeta } from '../src/Response';
import { startServer } from './fixtures/server';

Expand All @@ -28,6 +29,20 @@ describe('HttpClient.test.ts', () => {
});
});

describe('clientOptions.allowH2', () => {
it('should work with allowH2 = true', async () => {
const httpclient = new HttpClient({
allowH2: true,
});
assert(httpclient);
const response = await httpclient.request('https://registry.npmmirror.com/urllib');
assert.equal(response.status, 200);
assert(sensitiveHeaders in response.headers);
assert.equal(response.headers['content-type'], 'application/json; charset=utf-8');
assert.notEqual(httpclient.getDispatcher(), getGlobalDispatcher());
});
});

describe('clientOptions.defaultArgs', () => {
it('should work with custom defaultArgs', async () => {
const httpclient = new HttpClient({ defaultArgs: { timeout: 1000 } });
Expand Down

0 comments on commit 4c4a4c4

Please sign in to comment.