Skip to content

Commit 9ab623f

Browse files
committed
feat(browser): synchronize server-side and client-side time errors
1 parent 12fcfd1 commit 9ab623f

File tree

3 files changed

+53
-33
lines changed

3 files changed

+53
-33
lines changed

lib/browser/client.js

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function Client(options, ctx) {
4343
this.options = Client.initOptions(options);
4444
}
4545

46-
this.options.cancelFlag = false;// cancel flag: if true need to be cancelled, default false
46+
this.options.cancelFlag = false; // cancel flag: if true need to be cancelled, default false
4747

4848
// support custom agent and urllib client
4949
if (this.options.urllib) {
@@ -57,7 +57,7 @@ function Client(options, ctx) {
5757
this.stsTokenFreshTime = new Date();
5858

5959
// record the time difference between client and server
60-
this.options.amendTimeSkewed = 0;
60+
this.amendTimeSkewed = 0;
6161
}
6262

6363
/**
@@ -68,18 +68,22 @@ module.exports = Client;
6868

6969
Client.initOptions = function initOptions(options) {
7070
if (!options.stsToken) {
71-
console.warn('Please use STS Token for safety, see more details at https://help.aliyun.com/document_detail/32077.html');
71+
console.warn(
72+
'Please use STS Token for safety, see more details at https://help.aliyun.com/document_detail/32077.html'
73+
);
7274
}
73-
const opts = Object.assign({
74-
secure: isHttpsWebProtocol(),
75-
// for browser compatibility disable fetch.
76-
useFetch: false,
77-
}, options);
75+
const opts = Object.assign(
76+
{
77+
secure: isHttpsWebProtocol(),
78+
// for browser compatibility disable fetch.
79+
useFetch: false
80+
},
81+
options
82+
);
7883

7984
return _initOptions(opts);
8085
};
8186

82-
8387
/**
8488
* prototype
8589
*/
@@ -179,7 +183,12 @@ proto.authorization = function authorization(method, resource, subres, headers)
179183
parameters: subres
180184
});
181185

182-
return signUtils.authorization(this.options.accessKeyId, this.options.accessKeySecret, stringToSign, this.options.headerEncoding);
186+
return signUtils.authorization(
187+
this.options.accessKeyId,
188+
this.options.accessKeySecret,
189+
stringToSign,
190+
this.options.headerEncoding
191+
);
183192
};
184193

185194
/**
@@ -204,8 +213,8 @@ proto.authorization = function authorization(method, resource, subres, headers)
204213
proto.request = async function (params) {
205214
if (this.options.retryMax) {
206215
return await retry(request.bind(this), this.options.retryMax, {
207-
errorHandler: (err) => {
208-
const _errHandle = (_err) => {
216+
errorHandler: err => {
217+
const _errHandle = _err => {
209218
if (params.stream) return false;
210219
const statusErr = [-1, -2].includes(_err.status);
211220
const requestErrorRetryHandle = this.options.requestErrorRetryHandle || (() => true);
@@ -233,7 +242,14 @@ async function request(params) {
233242
const useStream = !!params.stream;
234243
try {
235244
result = await this.urllib.request(reqParams.url, reqParams.params);
236-
this.debug('response %s %s, got %s, headers: %j', params.method, reqParams.url, result.status, result.headers, 'info');
245+
this.debug(
246+
'response %s %s, got %s, headers: %j',
247+
params.method,
248+
reqParams.url,
249+
result.status,
250+
result.headers,
251+
'info'
252+
);
237253
} catch (err) {
238254
reqErr = err;
239255
}
@@ -242,7 +258,7 @@ async function request(params) {
242258
err = await this.requestError(result);
243259
// not use stream
244260
if (err.code === 'RequestTimeTooSkewed' && !useStream) {
245-
this.options.amendTimeSkewed = +new Date(err.serverTime) - new Date();
261+
this.amendTimeSkewed = +new Date(err.serverTime) - new Date();
246262
return await this.request(params);
247263
}
248264
err.params = params;
@@ -282,7 +298,7 @@ proto._escape = function _escape(name) {
282298
*/
283299

284300
proto._getUserAgent = function _getUserAgent() {
285-
const agent = (process && process.browser) ? 'js' : 'nodejs';
301+
const agent = process && process.browser ? 'js' : 'nodejs';
286302
const sdk = `aliyun-sdk-${agent}/${pkg.version}`;
287303
let plat = platform.description;
288304
if (!plat && process) {
@@ -306,7 +322,7 @@ proto._checkUserAgent = function _checkUserAgent(ua) {
306322
*/
307323

308324
proto.checkBrowserAndVersion = function checkBrowserAndVersion(name, version) {
309-
return ((bowser.name === name) && (bowser.version.split('.')[0] === version));
325+
return bowser.name === name && bowser.version.split('.')[0] === version;
310326
};
311327

312328
/**
@@ -321,16 +337,20 @@ proto.parseXML = function parseXMLThunk(str) {
321337
if (Buffer.isBuffer(str)) {
322338
str = str.toString();
323339
}
324-
xml.parseString(str, {
325-
explicitRoot: false,
326-
explicitArray: false
327-
}, (err, result) => {
328-
if (err) {
329-
reject(err);
330-
} else {
331-
resolve(result);
340+
xml.parseString(
341+
str,
342+
{
343+
explicitRoot: false,
344+
explicitArray: false
345+
},
346+
(err, result) => {
347+
if (err) {
348+
reject(err);
349+
} else {
350+
resolve(result);
351+
}
332352
}
333-
});
353+
);
334354
});
335355
};
336356

@@ -344,7 +364,8 @@ proto.parseXML = function parseXMLThunk(str) {
344364
proto.requestError = async function requestError(result) {
345365
let err = null;
346366
if (!result.data || !result.data.length) {
347-
if (result.status === -1 || result.status === -2) { // -1 is net error , -2 is timeout
367+
if (result.status === -1 || result.status === -2) {
368+
// -1 is net error , -2 is timeout
348369
err = new Error(result.message);
349370
err.name = result.name;
350371
err.status = result.status;
@@ -375,7 +396,7 @@ proto.requestError = async function requestError(result) {
375396

376397
let info;
377398
try {
378-
info = await this.parseXML(message) || {};
399+
info = (await this.parseXML(message)) || {};
379400
} catch (error) {
380401
this.debug(message, 'error');
381402
error.message += `\nraw xml: ${message}`;
@@ -384,7 +405,7 @@ proto.requestError = async function requestError(result) {
384405
return error;
385406
}
386407

387-
let msg = info.Message || (`unknow request error, status: ${result.status}`);
408+
let msg = info.Message || `unknow request error, status: ${result.status}`;
388409
if (info.Condition) {
389410
msg += ` (condition: ${info.Condition})`;
390411
}
@@ -400,4 +421,3 @@ proto.requestError = async function requestError(result) {
400421
this.debug('generate error %j', err, 'error');
401422
return err;
402423
};
403-

lib/common/utils/createRequest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ function delHeader(headers, name) {
2020
}
2121
function createRequest(params) {
2222
let date = new Date();
23-
if (this.options.amendTimeSkewed) {
24-
date = +new Date() + this.options.amendTimeSkewed;
23+
if (this.amendTimeSkewed) {
24+
date = +new Date() + this.amendTimeSkewed;
2525
}
2626
const headers = {
2727
'x-oss-date': dateFormat(date, "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'")

lib/common/utils/createRequest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function delHeader(headers: Headers, name: string) {
3030

3131
export function createRequest(this: any, params) {
3232
let date = new Date();
33-
if (this.options.amendTimeSkewed) {
34-
date = +new Date() + this.options.amendTimeSkewed;
33+
if (this.amendTimeSkewed) {
34+
date = +new Date() + this.amendTimeSkewed;
3535
}
3636
const headers: Headers = {
3737
'x-oss-date': dateFormat(date, "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'")

0 commit comments

Comments
 (0)