diff --git a/lib/browser/client.js b/lib/browser/client.js index fcd2f49c4..a0391cbbf 100644 --- a/lib/browser/client.js +++ b/lib/browser/client.js @@ -242,6 +242,7 @@ proto.request = async function request(params) { } let result; let reqErr; + const useStream = !!params.stream; try { result = await this.urllib.request(reqParams.url, reqParams.params); this.debug('response %s %s, got %s, headers: %j', params.method, reqParams.url, result.status, result.headers, 'info'); @@ -251,7 +252,8 @@ proto.request = async function request(params) { let err; if (result && params.successStatuses && params.successStatuses.indexOf(result.status) === -1) { err = await this.requestError(result); - if (err.code === 'RequestTimeTooSkewed') { + // not use stream + if (err.code === 'RequestTimeTooSkewed' && !useStream) { this.options.amendTimeSkewed = +new Date(err.serverTime) - new Date(); return await this.request(params); } diff --git a/lib/browser/object.js b/lib/browser/object.js index 98d7410de..c581a3b21 100644 --- a/lib/browser/object.js +++ b/lib/browser/object.js @@ -74,8 +74,15 @@ proto.put = async function put(name, file, options) { const stream = this._createStream(file, 0, file.size); options.contentLength = await this._getFileSize(file); - const result = await this.putStream(name, stream, options); - return result; + try { + const result = await this.putStream(name, stream, options); + return result; + } catch (err) { + if (err.code === 'RequestTimeTooSkewed') { + this.options.amendTimeSkewed = +new Date(err.serverTime) - new Date(); + return await this.put(name, file, options); + } + } } else { throw new TypeError('Must provide Buffer/Blob for put.'); } diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index 9065bfed7..82b63a42b 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -1151,11 +1151,8 @@ describe('browser', () => { }); describe('request time is skew', () => { - let store; - before(() => { - store = oss(ossConfig); - }); it('When the client\'s date is skew, the request will calibration time and retry', async () => { + const store = oss(ossConfig); const name = `${prefix}put/skew_date`; const body = new Buffer('body'); const requestSpy = sinon.spy(store, 'request'); @@ -1178,6 +1175,37 @@ describe('browser', () => { const resultDel = await store.delete(name); assert.equal(resultDel.res.status, 204); + + timemachine.reset(); + }); + + + it('date is skew, put file will retry', async () => { + const store = oss(ossConfig); + const name = `${prefix}put/skew_date_file`; + const requestSpy = sinon.spy(store, 'request'); + const requestErrorSpy = sinon.spy(store, 'requestError'); + const fileContent = Array(1024 * 1024).fill('a').join(''); + const file = new File([fileContent], 'skew_date_file'); + + timemachine.config({ + dateString: 'December 25, 1991 13:12:59', + tick: true + }); + const resultPut = await store.put(name, file); + assert.equal(resultPut.res.status, 200); + + assert.equal(requestSpy.callCount, 2); + assert.equal(requestErrorSpy.callCount, 1); + + const resultGet = await store.get(name); + assert.equal(resultGet.res.status, 200); + + assert.equal(resultGet.content.toString(), fileContent); + + const resultDel = await store.delete(name); + assert.equal(resultDel.res.status, 204); + timemachine.reset(); }); }); diff --git a/test/node/object.test.js b/test/node/object.test.js index 4b5a08f30..b26d14546 100644 --- a/test/node/object.test.js +++ b/test/node/object.test.js @@ -564,7 +564,6 @@ describe('test/object.test.js', () => { it('should return Etag and Content-Length', async () => { const info = await store.getObjectMeta(name); - console.log(resHeaders, info); assert.equal(info.status, 200); assert.equal(info.res.headers.etag, resHeaders.etag); assert.equal(info.res.headers['content-length'], fileSize);