Skip to content

Commit

Permalink
fix: put stream content empty when date is skew (#679)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterRao authored and luozhang002 committed Nov 12, 2019
1 parent 84cf170 commit 48d9546
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/browser/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions lib/browser/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down
36 changes: 32 additions & 4 deletions test/browser/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
});
});
Expand Down
1 change: 0 additions & 1 deletion test/node/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 48d9546

Please sign in to comment.