From ee1fd06c3ebc1664e4d30aea63be1660cff45286 Mon Sep 17 00:00:00 2001 From: taotao7 Date: Mon, 29 Aug 2022 15:41:22 +0800 Subject: [PATCH] feat(browser): synchronize server-side and client-side time errors --- lib/browser/object/put.js | 2 +- lib/common/client/_createRequest.js | 4 ++-- lib/common/client/initOptions.js | 1 - lib/common/client/request.js | 2 +- lib/setConfig.d.ts | 1 + lib/setConfig.js | 1 + src/browser/object/put.ts | 2 +- src/common/client/_createRequest.ts | 4 ++-- src/common/client/initOptions.ts | 1 - src/common/client/request.ts | 2 +- src/setConfig.ts | 10 ++++++---- 11 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/browser/object/put.js b/lib/browser/object/put.js index d0cf95832..d303f8c15 100644 --- a/lib/browser/object/put.js +++ b/lib/browser/object/put.js @@ -56,7 +56,7 @@ async function put(name, file, options = {}) { } catch (err) { if (err.code === 'RequestTimeTooSkewed') { - this.options.amendTimeSkewed = +new Date(err.serverTime) - new Date().valueOf(); + this.amendTimeSkewed = +new Date(err.serverTime) - new Date().valueOf(); return await put.call(this, name, file, options); } else { diff --git a/lib/common/client/_createRequest.js b/lib/common/client/_createRequest.js index fe102d987..990f55d80 100644 --- a/lib/common/client/_createRequest.js +++ b/lib/common/client/_createRequest.js @@ -27,8 +27,8 @@ function delHeader(headers, name) { } function _createRequest(params) { let date = new Date(); - if (this.options.amendTimeSkewed) { - date = +new Date() + this.options.amendTimeSkewed; + if (this.amendTimeSkewed) { + date = +new Date() + this.amendTimeSkewed; } const headers = { 'x-oss-date': dateformat_1.default(date, "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'") diff --git a/lib/common/client/initOptions.js b/lib/common/client/initOptions.js index 8afca3441..e14c29d43 100644 --- a/lib/common/client/initOptions.js +++ b/lib/common/client/initOptions.js @@ -63,7 +63,6 @@ function initOptions(options) { sldEnable: false, useFetch: false, headerEncoding: 'utf-8', - amendTimeSkewed: 0, refreshSTSTokenInterval: 60000 * 5, refreshSTSToken: null, enableProxy: false, diff --git a/lib/common/client/request.js b/lib/common/client/request.js index efa43fc10..70cef6fea 100644 --- a/lib/common/client/request.js +++ b/lib/common/client/request.js @@ -55,7 +55,7 @@ async function _request(params) { err = await this.requestError(result); // not use stream if (err.code === 'RequestTimeTooSkewed' && !useStream && !isNode) { - this.options.amendTimeSkewed = +new Date(err.serverTime) - new Date().valueOf(); + this.amendTimeSkewed = +new Date(err.serverTime) - new Date().valueOf(); return await this.request(params); } err.params = params; diff --git a/lib/setConfig.d.ts b/lib/setConfig.d.ts index d12674a09..82ed8063d 100644 --- a/lib/setConfig.d.ts +++ b/lib/setConfig.d.ts @@ -7,6 +7,7 @@ declare class Client { userAgent: any; _createStream: any; stsTokenFreshTime: any; + amendTimeSkewed: any; constructor(options: any, ctx: any); static use(...fn: any): typeof Client; setConfig(options: any, ctx: any): void; diff --git a/lib/setConfig.js b/lib/setConfig.js index 10dc6c85c..abfd269cc 100644 --- a/lib/setConfig.js +++ b/lib/setConfig.js @@ -26,6 +26,7 @@ class Client { Object.keys(client_1.default).forEach(prop => { Client.prototype[prop] = client_1.default[prop]; }); + this.amendTimeSkewed = 0; this.setConfig(options, ctx); } static use(...fn) { diff --git a/src/browser/object/put.ts b/src/browser/object/put.ts index b73536112..3aeb6f1a0 100644 --- a/src/browser/object/put.ts +++ b/src/browser/object/put.ts @@ -50,7 +50,7 @@ export async function put(this: any, name: string, file: any, options: PutObject return result; } catch (err) { if (err.code === 'RequestTimeTooSkewed') { - this.options.amendTimeSkewed = +new Date(err.serverTime) - new Date().valueOf(); + this.amendTimeSkewed = +new Date(err.serverTime) - new Date().valueOf(); return await put.call(this, name, file, options); } else { throw err; diff --git a/src/common/client/_createRequest.ts b/src/common/client/_createRequest.ts index 1d380806e..c08a844cb 100644 --- a/src/common/client/_createRequest.ts +++ b/src/common/client/_createRequest.ts @@ -35,8 +35,8 @@ function delHeader(headers: Headers, name: string) { export function _createRequest(this: any, params) { let date = new Date(); - if (this.options.amendTimeSkewed) { - date = +new Date() + this.options.amendTimeSkewed; + if (this.amendTimeSkewed) { + date = +new Date() + this.amendTimeSkewed; } const headers: Headers = { 'x-oss-date': dateFormat(date, "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'") diff --git a/src/common/client/initOptions.ts b/src/common/client/initOptions.ts index 7c95dc81c..cb4925560 100644 --- a/src/common/client/initOptions.ts +++ b/src/common/client/initOptions.ts @@ -68,7 +68,6 @@ export function initOptions(options) { sldEnable: false, useFetch: false, headerEncoding: 'utf-8', - amendTimeSkewed: 0, // record the time difference between client and server refreshSTSTokenInterval: 60000 * 5, refreshSTSToken: null, // auto set sts config enableProxy: false, diff --git a/src/common/client/request.ts b/src/common/client/request.ts index c5e46fe95..c49ddaf4e 100644 --- a/src/common/client/request.ts +++ b/src/common/client/request.ts @@ -52,7 +52,7 @@ export async function _request(this: any, params) { err = await this.requestError(result); // not use stream if (err.code === 'RequestTimeTooSkewed' && !useStream && !isNode) { - this.options.amendTimeSkewed = +new Date(err.serverTime) - new Date().valueOf(); + this.amendTimeSkewed = +new Date(err.serverTime) - new Date().valueOf(); return await this.request(params); } err.params = params; diff --git a/src/setConfig.ts b/src/setConfig.ts index bb19ced9e..e8792f566 100644 --- a/src/setConfig.ts +++ b/src/setConfig.ts @@ -24,8 +24,11 @@ class Client { public userAgent; public _createStream; + public stsTokenFreshTime; + public amendTimeSkewed; + public constructor(options, ctx) { if (!(this instanceof Client)) { return new Client(options, ctx); @@ -40,6 +43,8 @@ class Client { Client.prototype[prop] = base[prop]; }); + this.amendTimeSkewed = 0; + this.setConfig(options, ctx); } @@ -74,11 +79,8 @@ class Client { } } - export const initClient = (options, ctx) => { return new Client(options, ctx); }; -export { - Client -}; +export { Client };