Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
feat: impl accelerate request (#1637)
Browse files Browse the repository at this point in the history
* feat: impl accelerate request
  • Loading branch information
killagu authored May 6, 2021
1 parent 3d34058 commit 2245dc2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
23 changes: 23 additions & 0 deletions common/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var urllib = require('urllib');
var HttpAgent = require('agentkeepalive');
var HttpsAgent = require('agentkeepalive').HttpsAgent;
var config = require('../config');
var url = require('url');
var URL = require('url').URL;

var httpAgent;
var httpsAgent;
Expand Down Expand Up @@ -57,5 +59,26 @@ var client = urllib.create({
httpsAgent: httpsAgent
});

var request = urllib.HttpClient.prototype.request;

function getAccelerateUrl(url) {
const urlObj = typeof url === 'string' ? new URL(url) : url;
const newHost = config.accelerateHostMap && config.accelerateHostMap[urlObj.host];
if (newHost) {
urlObj.host = newHost;
}
return urlObj.toString();
}

client.request = function (requestUrl, options) {
const accelerateUrl = getAccelerateUrl(requestUrl);
options = Object.assign({}, options, {
formatRedirectUrl: function (from, to) {
return getAccelerateUrl(url.resolve(from, to));
}
});
return Reflect.apply(request, client, [ accelerateUrl, options ]);
};

module.exports = client;
module.exports.USER_AGENT = urllib.USER_AGENT;
2 changes: 2 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ var config = {
formatCustomOnePackageVersion: (ctx, packageVersion) => {
return packageVersion;
},
// registry download accelerate map
accelerateHostMap: {},
};

if (process.env.NODE_ENV === 'test') {
Expand Down
1 change: 1 addition & 0 deletions docs/dockerize/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ var config = {
// global hook function: function* (envelope) {}
// envelope format please see https://github.com/npm/registry/blob/master/docs/hooks/hooks-payload.md#payload
globalHook: null,
accelerateHostMap: {},
};

if (process.env.NODE_ENV !== 'test') {
Expand Down
39 changes: 39 additions & 0 deletions test/common/urllib.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const assert = require('assert');
const mm = require('mm');
const urllib = require('../../common/urllib');
const config = require('../../config');

describe('test/common/urllib.test.js', () => {
describe('accelerate request', () => {
beforeEach(() => {
mm(config, 'accelerateHostMap', {
'www.alipay.com': 'www.antgroup.com'
});
});

describe('direct', () => {
it('should work', function* () {
const res = yield urllib.request('https://www.alipay.com', {
followRedirect: true,
});
assert.deepStrictEqual(res.res.requestUrls, [
'https://www.antgroup.com/',
]);
});
});

describe('redirect', () => {
it('should work', function* () {
const res = yield urllib.request('http://alipay.com', {
followRedirect: true,
});
assert.deepStrictEqual(res.res.requestUrls, [
'http://alipay.com/',
'https://www.antgroup.com/',
]);
});
});
});
});

0 comments on commit 2245dc2

Please sign in to comment.