egg/lib/core/httpclient_next.js
fengmk2 ceded0b1c9
feat: allow to httpClient use HTTP2 first (#5332)
base on https://github.com/node-modules/urllib/pull/516


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced the ability to enable HTTP/2 support through the `allowH2`
property when using the next-generation HTTP client.
  
- **Bug Fixes**
- Enhanced clarity and accuracy of comments for HTTP client
configuration properties.

- **Improvements**
- Updated `urllib-next` dependency to version `^3.26.0` for better
performance and stability.
- Added tests to verify HTTP/2 functionality and custom HTTP client
behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-06-27 10:17:46 +08:00

39 lines
905 B
JavaScript

const { HttpClient } = require('urllib-next');
const ms = require('humanize-ms');
class HttpClientNext extends HttpClient {
constructor(app) {
normalizeConfig(app);
const config = app.config.httpclient;
super({
app,
defaultArgs: config.request,
allowH2: config.allowH2,
});
this.app = app;
}
async request(url, options) {
options = options || {};
if (options.ctx && options.ctx.tracer) {
options.tracer = options.ctx.tracer;
} else {
options.tracer = options.tracer || this.app.tracer;
}
return await super.request(url, options);
}
async curl(...args) {
return await this.request(...args);
}
}
function normalizeConfig(app) {
const config = app.config.httpclient;
if (typeof config.request.timeout === 'string') {
config.request.timeout = ms(config.request.timeout);
}
}
module.exports = HttpClientNext;