Skip to content

Commit

Permalink
reorder query region hosts and retore basic request methods (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 committed Jul 28, 2023
1 parent 2240114 commit 84d6f5b
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 52 deletions.
21 changes: 15 additions & 6 deletions qiniu/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,31 @@ exports.FormMimeJson = 'application/json';
exports.FormMimeRaw = 'application/octet-stream';
exports.RS_HOST = 'rs.qiniu.com';
exports.RPC_TIMEOUT = 600000; // 600s
let UC_BACKUP_HOSTS = [
'kodo-config.qiniuapi.com',
let QUERY_REGION_BACKUP_HOSTS = [
'uc.qbox.me',
'api.qiniu.com'
];
Object.defineProperty(exports, 'UC_BACKUP_HOSTS', {
get: () => UC_BACKUP_HOSTS,
Object.defineProperty(exports, 'QUERY_REGION_BACKUP_HOSTS', {
get: () => QUERY_REGION_BACKUP_HOSTS,
set: v => {
UC_BACKUP_HOSTS = v;
QUERY_REGION_BACKUP_HOSTS = v;
}
});
let QUERY_REGION_HOST = 'kodo-config.qiniuapi.com';
Object.defineProperty(exports, 'QUERY_REGION_HOST', {
get: () => QUERY_REGION_HOST,
set: v => {
QUERY_REGION_HOST = v;
QUERY_REGION_BACKUP_HOSTS = [];
}
});
let UC_HOST = 'uc.qbox.me';
Object.defineProperty(exports, 'UC_HOST', {
get: () => UC_HOST,
set: v => {
UC_HOST = v;
UC_BACKUP_HOSTS = [];
QUERY_REGION_HOST = v;
QUERY_REGION_BACKUP_HOSTS = [];
}
});

Expand Down
75 changes: 55 additions & 20 deletions qiniu/rpc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const urllib = require('urllib');

const pkg = require('../package.json');
const conf = require('./conf');
const digest = require('./auth/digest');
Expand Down Expand Up @@ -131,9 +133,16 @@ function postWithoutForm (requestURI, token, callbackFunc) {
}

function get (requestUrl, headers, callbackFunc) {
headers = headers || {};
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
headers.Connection = 'keep-alive';

const data = {
method: 'GET',
headers: headers,
dataType: 'json',
timeout: conf.RPC_TIMEOUT
timeout: conf.RPC_TIMEOUT,
gzip: true
};

if (conf.RPC_HTTP_AGENT) {
Expand All @@ -144,16 +153,22 @@ function get (requestUrl, headers, callbackFunc) {
data.httpsAgent = conf.RPC_HTTPS_AGENT;
}

return exports.qnHttpClient.get({
url: requestUrl,
headers: headers,
callback: callbackFunc
}, data);
return urllib.request(
requestUrl,
data,
callbackFunc
);
}

function post (requestURL, requestForm, headers, callbackFunc) {
function post (requestUrl, requestForm, headers, callbackFunc) {
// var start = parseInt(Date.now() / 1000);
headers = headers || {};
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
headers.Connection = 'keep-alive';

const data = {
headers: headers,
method: 'POST',
dataType: 'json',
timeout: conf.RPC_TIMEOUT,
gzip: true
Expand All @@ -168,17 +183,30 @@ function post (requestURL, requestForm, headers, callbackFunc) {
data.httpsAgent = conf.RPC_HTTPS_AGENT;
}

return exports.qnHttpClient.post({
url: requestURL,
data: requestForm,
headers: headers,
callback: callbackFunc
}, data);
if (Buffer.isBuffer(requestForm) || typeof requestForm === 'string') {
data.content = requestForm;
} else if (requestForm) {
data.stream = requestForm;
} else {
data.headers['Content-Length'] = 0;
}

return urllib.request(
requestUrl,
data,
callbackFunc
);
}

function put (requestURL, requestForm, headers, callbackFunc) {
function put (requestUrl, requestForm, headers, callbackFunc) {
// var start = parseInt(Date.now() / 1000);
headers = headers || {};
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
headers.Connection = 'keep-alive';

const data = {
headers: headers,
method: 'PUT',
dataType: 'json',
timeout: conf.RPC_TIMEOUT,
gzip: true
Expand All @@ -193,10 +221,17 @@ function put (requestURL, requestForm, headers, callbackFunc) {
data.httpsAgent = conf.RPC_HTTPS_AGENT;
}

return exports.qnHttpClient.put({
url: requestURL,
data: requestForm,
headers: headers,
callback: callbackFunc
}, data);
if (Buffer.isBuffer(requestForm) || typeof requestForm === 'string') {
data.content = requestForm;
} else if (requestForm) {
data.stream = requestForm;
} else {
data.headers['Content-Length'] = 0;
}

return urllib.request(
requestUrl,
data,
callbackFunc
);
}
4 changes: 2 additions & 2 deletions qiniu/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ exports.Zone_as0 = new conf.Zone([
'api-as0.qiniuapi.com');

exports.getZoneInfo = function (accessKey, bucket, callbackFunc) {
const apiAddr = 'https://' + conf.UC_HOST + '/v4/query';
const apiAddr = 'https://' + conf.QUERY_REGION_HOST + '/v4/query';

rpc.qnHttpClient.get({
url: apiAddr,
Expand All @@ -73,7 +73,7 @@ exports.getZoneInfo = function (accessKey, bucket, callbackFunc) {
},
middlewares: [
new RetryDomainsMiddleware({
backupDomains: conf.UC_BACKUP_HOSTS
backupDomains: conf.QUERY_REGION_BACKUP_HOSTS
})
],
callback: function (respErr, respData, respInfo) {
Expand Down
86 changes: 62 additions & 24 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,6 @@ describe('test util functions', function () {
});
});

it('test prepareZone with backup domains', function (done) {
before(function () {
qiniu.conf.UC_HOST = 'fake-uc.nodejssdk.qiniu.com';
qiniu.conf.UC_BACKUP_HOSTS = [
'unavailable-uc.nodejssdk.qiniu.com',
'uc.qbox.me'
];
});

after(function () {
qiniu.conf.UC_HOST = 'uc.qbox.me';
qiniu.conf.UC_BACKUP_HOSTS = [
'kodo-config.qiniuapi.com',
'api.qiniu.com'
];
});

qiniu.util.prepareZone(bucketManager, bucketManager.mac.accessKey, bucket, function (err, ctx) {
should.not.exist(err);
should.equal(bucketManager, ctx);
done();
});
});

it('test formatDateUTC', function () {
const caseList = [
{
Expand Down Expand Up @@ -395,4 +371,66 @@ describe('test util functions', function () {
}
});
});

describe('test prepareZone with change hosts config', function () {
let bucketManagerNoCtxCache = new qiniu.rs.BucketManager(mac, config);

beforeEach(function () {
const noCacheConfig = new qiniu.conf.Config();
bucketManagerNoCtxCache = new qiniu.rs.BucketManager(mac, noCacheConfig);
});

afterEach(function () {
qiniu.conf.UC_HOST = 'uc.qbox.me';
qiniu.conf.QUERY_REGION_HOST = 'kodo-config.qiniuapi.com';
qiniu.conf.QUERY_REGION_BACKUP_HOSTS = [
'uc.qbox.me',
'api.qiniu.com'
];
});

it('test prepareZone with custom query domain', function (done) {
should.not.exist(bucketManagerNoCtxCache.config.zone);

qiniu.conf.QUERY_REGION_HOST = 'uc.qbox.me';

qiniu.util.prepareZone(bucketManagerNoCtxCache, bucketManagerNoCtxCache.mac.accessKey, bucket, function (err, ctx) {
should.not.exist(err);
should.exist(ctx.config.zone);
done();
});
});

it('test prepareZone with backup domain', function (done) {
should.not.exist(bucketManagerNoCtxCache.config.zone);

qiniu.conf.QUERY_REGION_HOST = 'fake-uc.csharp.qiniu.com';
qiniu.conf.QUERY_REGION_BACKUP_HOSTS = [
'unavailable-uc.csharp.qiniu.com',
'uc.qbox.me'
];

qiniu.util.prepareZone(bucketManagerNoCtxCache, bucketManagerNoCtxCache.mac.accessKey, bucket, function (err, ctx) {
should.not.exist(err);
should.exist(ctx.config.zone);
done();
});
});

it('test prepareZone with uc and backup domains', function (done) {
should.not.exist(bucketManagerNoCtxCache.config.zone);

qiniu.conf.UC_HOST = 'fake-uc.csharp.qiniu.com';
qiniu.conf.QUERY_REGION_BACKUP_HOSTS = [
'unavailable-uc.csharp.qiniu.com',
'uc.qbox.me'
];

qiniu.util.prepareZone(bucketManagerNoCtxCache, bucketManagerNoCtxCache.mac.accessKey, bucket, function (err, ctx) {
should.not.exist(err);
should.exist(ctx.config.zone);
done();
});
});
});
});

0 comments on commit 84d6f5b

Please sign in to comment.