-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { HttpClient } = require('urllib4'); | ||
const ms = require('humanize-ms'); | ||
|
||
class HttpClient4 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 = HttpClient4; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const assert = require('assert'); | ||
|
||
module.exports = app => { | ||
class CustomHttpClient extends app.HttpClient4 { | ||
request(url, opt) { | ||
return new Promise(resolve => { | ||
assert(/^http/.test(url), 'url should start with http, but got ' + url); | ||
resolve(); | ||
}).then(() => { | ||
return super.request(url, opt); | ||
}); | ||
} | ||
|
||
curl(url, opt) { | ||
return this.request(url, opt); | ||
} | ||
} | ||
app.HttpClient4 = CustomHttpClient; | ||
}; |
6 changes: 6 additions & 0 deletions
6
test/fixtures/apps/httpclient-allowH2/config/config.default.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
exports.httpclient = { | ||
allowH2: true, | ||
request: { | ||
timeout: 99, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "httpclient-overwrite" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters