Skip to content

Commit 84d6f5b

Browse files
authored
reorder query region hosts and retore basic request methods (#417)
1 parent 2240114 commit 84d6f5b

File tree

4 files changed

+134
-52
lines changed

4 files changed

+134
-52
lines changed

qiniu/conf.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,31 @@ exports.FormMimeJson = 'application/json';
1818
exports.FormMimeRaw = 'application/octet-stream';
1919
exports.RS_HOST = 'rs.qiniu.com';
2020
exports.RPC_TIMEOUT = 600000; // 600s
21-
let UC_BACKUP_HOSTS = [
22-
'kodo-config.qiniuapi.com',
21+
let QUERY_REGION_BACKUP_HOSTS = [
22+
'uc.qbox.me',
2323
'api.qiniu.com'
2424
];
25-
Object.defineProperty(exports, 'UC_BACKUP_HOSTS', {
26-
get: () => UC_BACKUP_HOSTS,
25+
Object.defineProperty(exports, 'QUERY_REGION_BACKUP_HOSTS', {
26+
get: () => QUERY_REGION_BACKUP_HOSTS,
2727
set: v => {
28-
UC_BACKUP_HOSTS = v;
28+
QUERY_REGION_BACKUP_HOSTS = v;
29+
}
30+
});
31+
let QUERY_REGION_HOST = 'kodo-config.qiniuapi.com';
32+
Object.defineProperty(exports, 'QUERY_REGION_HOST', {
33+
get: () => QUERY_REGION_HOST,
34+
set: v => {
35+
QUERY_REGION_HOST = v;
36+
QUERY_REGION_BACKUP_HOSTS = [];
2937
}
3038
});
3139
let UC_HOST = 'uc.qbox.me';
3240
Object.defineProperty(exports, 'UC_HOST', {
3341
get: () => UC_HOST,
3442
set: v => {
3543
UC_HOST = v;
36-
UC_BACKUP_HOSTS = [];
44+
QUERY_REGION_HOST = v;
45+
QUERY_REGION_BACKUP_HOSTS = [];
3746
}
3847
});
3948

qiniu/rpc.js

+55-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const urllib = require('urllib');
2+
13
const pkg = require('../package.json');
24
const conf = require('./conf');
35
const digest = require('./auth/digest');
@@ -131,9 +133,16 @@ function postWithoutForm (requestURI, token, callbackFunc) {
131133
}
132134

133135
function get (requestUrl, headers, callbackFunc) {
136+
headers = headers || {};
137+
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
138+
headers.Connection = 'keep-alive';
139+
134140
const data = {
141+
method: 'GET',
142+
headers: headers,
135143
dataType: 'json',
136-
timeout: conf.RPC_TIMEOUT
144+
timeout: conf.RPC_TIMEOUT,
145+
gzip: true
137146
};
138147

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

147-
return exports.qnHttpClient.get({
148-
url: requestUrl,
149-
headers: headers,
150-
callback: callbackFunc
151-
}, data);
156+
return urllib.request(
157+
requestUrl,
158+
data,
159+
callbackFunc
160+
);
152161
}
153162

154-
function post (requestURL, requestForm, headers, callbackFunc) {
163+
function post (requestUrl, requestForm, headers, callbackFunc) {
155164
// var start = parseInt(Date.now() / 1000);
165+
headers = headers || {};
166+
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
167+
headers.Connection = 'keep-alive';
168+
156169
const data = {
170+
headers: headers,
171+
method: 'POST',
157172
dataType: 'json',
158173
timeout: conf.RPC_TIMEOUT,
159174
gzip: true
@@ -168,17 +183,30 @@ function post (requestURL, requestForm, headers, callbackFunc) {
168183
data.httpsAgent = conf.RPC_HTTPS_AGENT;
169184
}
170185

171-
return exports.qnHttpClient.post({
172-
url: requestURL,
173-
data: requestForm,
174-
headers: headers,
175-
callback: callbackFunc
176-
}, data);
186+
if (Buffer.isBuffer(requestForm) || typeof requestForm === 'string') {
187+
data.content = requestForm;
188+
} else if (requestForm) {
189+
data.stream = requestForm;
190+
} else {
191+
data.headers['Content-Length'] = 0;
192+
}
193+
194+
return urllib.request(
195+
requestUrl,
196+
data,
197+
callbackFunc
198+
);
177199
}
178200

179-
function put (requestURL, requestForm, headers, callbackFunc) {
201+
function put (requestUrl, requestForm, headers, callbackFunc) {
180202
// var start = parseInt(Date.now() / 1000);
203+
headers = headers || {};
204+
headers['User-Agent'] = headers['User-Agent'] || conf.USER_AGENT;
205+
headers.Connection = 'keep-alive';
206+
181207
const data = {
208+
headers: headers,
209+
method: 'PUT',
182210
dataType: 'json',
183211
timeout: conf.RPC_TIMEOUT,
184212
gzip: true
@@ -193,10 +221,17 @@ function put (requestURL, requestForm, headers, callbackFunc) {
193221
data.httpsAgent = conf.RPC_HTTPS_AGENT;
194222
}
195223

196-
return exports.qnHttpClient.put({
197-
url: requestURL,
198-
data: requestForm,
199-
headers: headers,
200-
callback: callbackFunc
201-
}, data);
224+
if (Buffer.isBuffer(requestForm) || typeof requestForm === 'string') {
225+
data.content = requestForm;
226+
} else if (requestForm) {
227+
data.stream = requestForm;
228+
} else {
229+
data.headers['Content-Length'] = 0;
230+
}
231+
232+
return urllib.request(
233+
requestUrl,
234+
data,
235+
callbackFunc
236+
);
202237
}

qiniu/zone.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ exports.Zone_as0 = new conf.Zone([
6363
'api-as0.qiniuapi.com');
6464

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

6868
rpc.qnHttpClient.get({
6969
url: apiAddr,
@@ -73,7 +73,7 @@ exports.getZoneInfo = function (accessKey, bucket, callbackFunc) {
7373
},
7474
middlewares: [
7575
new RetryDomainsMiddleware({
76-
backupDomains: conf.UC_BACKUP_HOSTS
76+
backupDomains: conf.QUERY_REGION_BACKUP_HOSTS
7777
})
7878
],
7979
callback: function (respErr, respData, respInfo) {

test/util.test.js

+62-24
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,6 @@ describe('test util functions', function () {
7474
});
7575
});
7676

77-
it('test prepareZone with backup domains', function (done) {
78-
before(function () {
79-
qiniu.conf.UC_HOST = 'fake-uc.nodejssdk.qiniu.com';
80-
qiniu.conf.UC_BACKUP_HOSTS = [
81-
'unavailable-uc.nodejssdk.qiniu.com',
82-
'uc.qbox.me'
83-
];
84-
});
85-
86-
after(function () {
87-
qiniu.conf.UC_HOST = 'uc.qbox.me';
88-
qiniu.conf.UC_BACKUP_HOSTS = [
89-
'kodo-config.qiniuapi.com',
90-
'api.qiniu.com'
91-
];
92-
});
93-
94-
qiniu.util.prepareZone(bucketManager, bucketManager.mac.accessKey, bucket, function (err, ctx) {
95-
should.not.exist(err);
96-
should.equal(bucketManager, ctx);
97-
done();
98-
});
99-
});
100-
10177
it('test formatDateUTC', function () {
10278
const caseList = [
10379
{
@@ -395,4 +371,66 @@ describe('test util functions', function () {
395371
}
396372
});
397373
});
374+
375+
describe('test prepareZone with change hosts config', function () {
376+
let bucketManagerNoCtxCache = new qiniu.rs.BucketManager(mac, config);
377+
378+
beforeEach(function () {
379+
const noCacheConfig = new qiniu.conf.Config();
380+
bucketManagerNoCtxCache = new qiniu.rs.BucketManager(mac, noCacheConfig);
381+
});
382+
383+
afterEach(function () {
384+
qiniu.conf.UC_HOST = 'uc.qbox.me';
385+
qiniu.conf.QUERY_REGION_HOST = 'kodo-config.qiniuapi.com';
386+
qiniu.conf.QUERY_REGION_BACKUP_HOSTS = [
387+
'uc.qbox.me',
388+
'api.qiniu.com'
389+
];
390+
});
391+
392+
it('test prepareZone with custom query domain', function (done) {
393+
should.not.exist(bucketManagerNoCtxCache.config.zone);
394+
395+
qiniu.conf.QUERY_REGION_HOST = 'uc.qbox.me';
396+
397+
qiniu.util.prepareZone(bucketManagerNoCtxCache, bucketManagerNoCtxCache.mac.accessKey, bucket, function (err, ctx) {
398+
should.not.exist(err);
399+
should.exist(ctx.config.zone);
400+
done();
401+
});
402+
});
403+
404+
it('test prepareZone with backup domain', function (done) {
405+
should.not.exist(bucketManagerNoCtxCache.config.zone);
406+
407+
qiniu.conf.QUERY_REGION_HOST = 'fake-uc.csharp.qiniu.com';
408+
qiniu.conf.QUERY_REGION_BACKUP_HOSTS = [
409+
'unavailable-uc.csharp.qiniu.com',
410+
'uc.qbox.me'
411+
];
412+
413+
qiniu.util.prepareZone(bucketManagerNoCtxCache, bucketManagerNoCtxCache.mac.accessKey, bucket, function (err, ctx) {
414+
should.not.exist(err);
415+
should.exist(ctx.config.zone);
416+
done();
417+
});
418+
});
419+
420+
it('test prepareZone with uc and backup domains', function (done) {
421+
should.not.exist(bucketManagerNoCtxCache.config.zone);
422+
423+
qiniu.conf.UC_HOST = 'fake-uc.csharp.qiniu.com';
424+
qiniu.conf.QUERY_REGION_BACKUP_HOSTS = [
425+
'unavailable-uc.csharp.qiniu.com',
426+
'uc.qbox.me'
427+
];
428+
429+
qiniu.util.prepareZone(bucketManagerNoCtxCache, bucketManagerNoCtxCache.mac.accessKey, bucket, function (err, ctx) {
430+
should.not.exist(err);
431+
should.exist(ctx.config.zone);
432+
done();
433+
});
434+
});
435+
});
398436
});

0 commit comments

Comments
 (0)