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: allow to httpClient use HTTP2 first #5332

Merged
merged 1 commit 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
2 changes: 2 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ module.exports = appInfo => {
* @property {Number} httpsAgent.maxSockets - https agent max socket number of one host, default is `Number.MAX_SAFE_INTEGER` @ses https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
* @property {Number} httpsAgent.maxFreeSockets - https agent max free socket number of one host, default is 256.
* @property {Boolean} useHttpClientNext - use urllib@3 HttpClient
* @property {Boolean} allowH2 - Allow to use HTTP2 first, only work on `useHttpClientNext = true`
*/
config.httpclient = {
enableDNSCache: false,
Expand All @@ -326,6 +327,7 @@ module.exports = appInfo => {
maxFreeSockets: 256,
},
useHttpClientNext: false,
// allowH2: false,
};

/**
Expand Down
8 changes: 5 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,18 @@ declare module 'egg' {
request?: HttpClientRequestOptions | RequestOptionsOld;
/** Whether enable dns cache */
enableDNSCache?: boolean;
/** Enable proxy request, default is false. */
/** Enable proxy request. Default is `false`. */
enableProxy?: boolean;
/** proxy agent uri or options, default is null. */
/** proxy agent uri or options. Default is `null`. */
proxy?: string | { [key: string]: any };
/** DNS cache lookup interval */
dnsCacheLookupInterval?: number;
/** DNS cache max age */
dnsCacheMaxLength?: number;
/** use urllib@3 HttpClient */
/** use urllib@3 HttpClient. Default is `false` */
useHttpClientNext?: boolean;
/** Allow to use HTTP2 first, only work on `useHttpClientNext = true`. Default is `false` */
allowH2?: boolean;
}

export interface EggAppConfig {
Expand Down
1 change: 1 addition & 0 deletions lib/core/httpclient_next.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class HttpClientNext extends HttpClient {
super({
app,
defaultArgs: config.request,
allowH2: config.allowH2,
});
this.app = app;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"onelogger": "^1.0.0",
"sendmessage": "^2.0.0",
"urllib": "^2.33.0",
"urllib-next": "npm:urllib@^3.22.4",
"urllib-next": "npm:urllib@^3.26.0",
"utility": "^2.1.0",
"ylru": "^1.3.2"
},
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/apps/httpclient-http2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove redundant 'use strict' directive.

Since ES6 modules are strict by default, the 'use strict' directive is unnecessary.

- 'use strict';
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
'use strict';
Tools
Biome

[error] 1-1: Redundant use strict directive.

The entire contents of JavaScript modules are automatically in strict mode, with no statement needed to initiate it.
Safe fix: Remove the redundant use strict directive.

(lint/suspicious/noRedundantUseStrict)


const assert = require('assert');

module.exports = app => {
class CustomHttpClient extends app.HttpClientNext {
request(url, opt) {
return new Promise(resolve => {
assert(/^http/.test(url), 'url should start with http, but got ' + url);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve URL validation regex.

The current regex ^http matches any string starting with "http" which could include invalid protocols like "httpx". Consider refining it to match only "http" or "https".

- assert(/^http/.test(url), 'url should start with http, but got ' + url);
+ assert(/^(http|https):/.test(url), 'url should start with http or https, but got ' + url);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
assert(/^http/.test(url), 'url should start with http, but got ' + url);
assert(/^(http|https):/.test(url), 'url should start with http or https, but got ' + url);

resolve();
}).then(() => {
return super.request(url, opt);
});
}

curl(url, opt) {
return this.request(url, opt);
}
}
app.HttpClientNext = CustomHttpClient;
};
4 changes: 4 additions & 0 deletions test/fixtures/apps/httpclient-http2/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.httpclient = {
useHttpClientNext: true,
allowH2: true,
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/httpclient-http2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "httpclient-overwrite"
}
37 changes: 37 additions & 0 deletions test/lib/core/httpclient.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const assert = require('node:assert');
const { sensitiveHeaders } = require('node:http2');
const mm = require('egg-mock');
const urllib = require('urllib');
const Httpclient = require('../../../lib/core/httpclient');
Expand Down Expand Up @@ -237,6 +238,12 @@ describe('test/lib/core/httpclient.test.js', () => {
});
after(() => app.close());

it('should work', async () => {
const res = await app.httpclient.request(url);
assert.equal(res.status, 200);
assert.equal(res.data.toString(), 'GET /');
});

it('should set request default global timeout to 99ms', () => {
return app.httpclient.curl(`${url}/timeout`)
.catch(err => {
Expand All @@ -255,6 +262,36 @@ describe('test/lib/core/httpclient.test.js', () => {
});
});

describe('overwrite httpclient support allowH2=true', () => {
let app;
before(() => {
app = utils.app('apps/httpclient-http2');
return app.ready();
});
after(() => app.close());

it('should work on http2', async () => {
const res = await app.httpclient.request(url);
assert.equal(res.status, 200);
assert.equal(res.data.toString(), 'GET /');
assert.equal(sensitiveHeaders in res.headers, false);
const res2 = await app.httpclient.request('https://registry.npmmirror.com/urllib/latest', {
dataType: 'json',
});
assert.equal(res2.status, 200);
assert.equal(res2.data.name, 'urllib');
assert.equal(sensitiveHeaders in res2.headers, true);
});

it('should assert url', () => {
return app.httpclient.curl('unknown url')
.catch(err => {
assert(err);
assert(err.message.includes('url should start with http, but got unknown url'));
});
});
});

describe('httpclient tracer', () => {
let app;
before(() => {
Expand Down
Loading