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

Fix/browser test case #920

Merged
merged 6 commits into from
Jan 11, 2021
Merged
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [6.13.1](https://github.com/aliyun/oss-nodejs-sdk/compare/v6.13.0...v6.13.1) (2021-01-07)


### Bug Fixes

* responseCacheControl in Node.js ([#919](https://github.com/aliyun/oss-nodejs-sdk/issues/919)) ([7ca7055](https://github.com/aliyun/oss-nodejs-sdk/commit/7ca7055a667604f3f653621b9f2974e28e9d2190))

## [6.13.0](https://github.com/aliyun/oss-nodejs-sdk/compare/v6.12.0...v6.13.0) (2021-01-06)


Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"package.json"
],
"main": "dist/aliyun-oss-sdk.js",
"version": "6.13.0"
"version": "6.13.1"
}
750 changes: 437 additions & 313 deletions dist/aliyun-oss-sdk.js

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions dist/aliyun-oss-sdk.min.js

Large diffs are not rendered by default.

61 changes: 0 additions & 61 deletions lib/browser/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const fs = require('fs');
const copy = require('copy-to');
const path = require('path');
const mime = require('mime');
const is = require('is-type-of');
const callback = require('../common/callback');
const merge = require('merge-descriptors');
const { isBlob } = require('../common/utils/isBlob');
Expand Down Expand Up @@ -371,63 +370,3 @@ proto._deleteFileSafe = function _deleteFileSafe(filepath) {
});
});
};

/**
* get
* @param {String} name - object name
* @param {String | Stream} file
* @param {Object} options
* @param {{res}}
*/
proto.get = async function get(name, file, options = {}) {
let writeStream = null;
let needDestroy = false;

if (is.writableStream(file)) {
writeStream = file;
} else if (is.string(file)) {
writeStream = fs.createWriteStream(file);
needDestroy = true;
} else {
// get(name, options)
options = file;
}

options = options || {};
const responseCacheControl = options.responseCacheControl === null ? '' : 'no-cache';
options.subres = Object.assign(
responseCacheControl ? { 'response-cache-control': responseCacheControl } : {},
options.subres
);
if (options.versionId) {
options.subres.versionId = options.versionId;
}
if (options.process) {
options.subres['x-oss-process'] = options.process;
}

let result;
try {
const params = this._objectRequestParams('GET', name, options);
params.writeStream = writeStream;
params.successStatuses = [200, 206, 304];

result = await this.request(params);

if (needDestroy) {
writeStream.destroy();
}
} catch (err) {
if (needDestroy) {
writeStream.destroy();
// should delete the exists file before throw error
await this._deleteFileSafe(file);
}
throw err;
}

return {
res: result.res,
content: result.data
};
};
2 changes: 1 addition & 1 deletion lib/browser/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
exports.version="6.12.0"
exports.version="6.13.1"
6 changes: 5 additions & 1 deletion lib/common/object/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ proto.get = async function get(name, file, options = {}) {
}

options = options || {};
const isBrowserEnv = process && process.browser;
const responseCacheControl = options.responseCacheControl === null ? '' : 'no-cache';
const defaultSubresOptions =
isBrowserEnv && responseCacheControl ? { 'response-cache-control': responseCacheControl } : {};
options.subres = Object.assign(defaultSubresOptions, options.subres);

options.subres = Object.assign({ 'response-cache-control': 'no-cache' }, options.subres);
if (options.versionId) {
options.subres.versionId = options.versionId;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/common/utils/deepCopy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports.deepCopyWith = (obj, customizer) => {
return value;
}
if (isBuffer_1.isBuffer(value)) {
return Buffer.from(value);
return value.slice();
}
const copy = Array.isArray(value) ? [] : {};
Object.keys(value).forEach((k) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/common/utils/deepCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const deepCopyWith = (obj: any, customizer?: (v: any, k: string, o: any)
}

if (isBuffer(value)) {
return obj.slice();
return value.slice();
}

const copy = Array.isArray(value) ? [] : {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ali-oss",
"version": "6.13.0",
"version": "6.13.1",
"description": "aliyun oss(object storage service) node client",
"main": "lib/client.js",
"files": [
Expand Down
43 changes: 22 additions & 21 deletions test/browser/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const cleanBucket = async store => {
const uploads = result.uploads || [];
await Promise.all(uploads.map(_ => store.abortMultipartUpload(_.name, _.uploadId)));
};

describe('browser', () => {
/* eslint require-yield: [0] */
before(() => {
Expand All @@ -48,7 +47,6 @@ describe('browser', () => {
stsToken: stsConfig.Credentials.SecurityToken,
bucket: stsConfig.bucket
};

// this.store = oss({
// region: stsConfig.region,
// accessKeyId: creds.AccessKeyId,
Expand Down Expand Up @@ -639,20 +637,22 @@ describe('browser', () => {

describe('get()', () => {
const name = `${prefix}ali-sdk/get/${Date.now()}-oss.jpg`
const store = new OSS(ossConfig);
before(() => {
await store.put('name', Buffer.from('oss.jpg'));
});
it('should get with disableCache option', async () => {
const originRequest = store.urllib.request;
let requestUrl;
store.urllib.request = (url, params) => {
requestUrl = url;
return originRequest.call(store.urllib, url, params);
};
await store.get(name);
store.urllib.request = originRequest;
assert(requestUrl.includes('response-cache-control=no-cache'));
let store;
before(async () => {
store = new OSS(ossConfig);
await store.put(name, Buffer.from('oss.jpg'));
});
it('should get with default responseCacheControl option', async () => {
const {
res: { requestUrls }
} = await store.get(name);
assert(requestUrls[0].includes('response-cache-control=no-cache'));
const {
res: { requestUrls: requestUrls2 }
} = await store.get(name, {
responseCacheControl: null
});
assert(!requestUrls2[0].includes('response-cache-control=no-cache'));
});
});

Expand All @@ -675,7 +675,7 @@ describe('browser', () => {
assert.equal(resultDel.res.status, 204);
});
it('GETs and PUTs blob to a bucket', async () => {
const name = `${prefix}put/test`;
const name = `${prefix}put/test1`;
const body = new Blob(['blobBody'], { type: 'text/plain' });
const resultPut = await store.put(name, body);
assert.equal(resultPut.res.status, 200);
Expand Down Expand Up @@ -731,7 +731,7 @@ describe('browser', () => {

it('should set custom Content-MD5 and ignore case', async () => {
const name = `${prefix}put/test-md5`;
const content = Array(1024 * 1024 * 10)
const content = Array(1024 * 1024 * 2)
.fill(1)
.join('');
const body = new Blob([content], { type: 'text/plain' });
Expand Down Expand Up @@ -905,7 +905,8 @@ describe('browser', () => {
assert.equal(typeof result.data.etag, 'string');
assert.equal(typeof result.data.lastModified, 'string');
let info = await store.head(originname);
assert(!info.res.headers['cache-control']);
// It should be 'no-cache' in the real browser environment, but other environments are undefined
assert(!info.res.headers['cache-control'] || info.res.headers['cache-control'] === 'no-cache');

// add Cache-Control header to a exists object
result = await store.copy(originname, originname, {
Expand Down Expand Up @@ -2078,12 +2079,12 @@ describe('browser', () => {

it('putMeta() should return 200', async () => {
const result = await store.putMeta(name, {
b: utf8_content
a: utf8_content
});
assert.equal(result.res.status, 200);
const info = await store.head(name);
assert.equal(info.status, 200);
assert.equal(info.meta.b, latin1_content);
assert.equal(info.meta.a, latin1_content);
});
});

Expand Down
1 change: 1 addition & 0 deletions test/node/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ describe('test/object.test.js', () => {
const savepath = path.join(tmpdir, name.replace(/\//g, '-'));
const result = await store.get(name, savepath);
assert.equal(result.res.status, 200);
assert(!result.res.requestUrls[0].includes('response-cache-control=no-cache'));
assert.equal(fs.statSync(savepath).size, fs.statSync(__filename).size);
});

Expand Down