Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(browser): synchronize server-side and client-side time errors #1152

Open
wants to merge 1 commit into
base: 7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/browser/object/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions lib/common/client/_createRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
Expand Down
1 change: 0 additions & 1 deletion lib/common/client/initOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function initOptions(options) {
sldEnable: false,
useFetch: false,
headerEncoding: 'utf-8',
amendTimeSkewed: 0,
refreshSTSTokenInterval: 60000 * 5,
refreshSTSToken: null,
enableProxy: false,
Expand Down
2 changes: 1 addition & 1 deletion lib/common/client/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/setConfig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/setConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/browser/object/put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/common/client/_createRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
Expand Down
1 change: 0 additions & 1 deletion src/common/client/initOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/common/client/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/setConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -40,6 +43,8 @@ class Client {
Client.prototype[prop] = base[prop];
});

this.amendTimeSkewed = 0;

this.setConfig(options, ctx);
}

Expand Down Expand Up @@ -74,11 +79,8 @@ class Client {
}
}


export const initClient = (options, ctx) => {
return new Client(options, ctx);
};

export {
Client
};
export { Client };