Skip to content

Commit

Permalink
getObject 增加 DataType 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Oct 13, 2020
1 parent 54f2d9d commit 62cdcb4
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 13 deletions.
19 changes: 14 additions & 5 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.0.2';
COS.version = '1.0.3';

module.exports = COS;

Expand Down Expand Up @@ -6556,6 +6556,7 @@ function getObject(params, callback) {
Region: params.Region,
Key: params.Key,
VersionId: params.VersionId,
DataType: params.DataType,
headers: params.Headers,
qs: reqParams,
rawBody: true
Expand Down Expand Up @@ -8025,6 +8026,9 @@ function _submitRequest(params, callback) {
params.onProgress({ loaded: loaded, total: contentLength });
};
}
if (params.DataType) {
opt.dataType = params.DataType;
}
if (this.options.Timeout) {
opt.timeout = this.options.Timeout;
}
Expand Down Expand Up @@ -8255,6 +8259,10 @@ var xhrRes = function (xhr) {
};
};

var xhrBody = function (xhr, dataType) {
return !dataType && dataType === 'text' ? xhr.responseText : xhr.response;
};

var request = function (opt, callback) {

// method
Expand Down Expand Up @@ -8289,19 +8297,20 @@ var request = function (opt, callback) {

// success 2xx/3xx/4xx
xhr.onload = function () {
callback(null, xhrRes(xhr), xhr.responseText);
callback(null, xhrRes(xhr), xhrBody(xhr, opt.dataType));
};

// error 5xx/0 (网络错误、跨域报错、Https connect-src 限制的报错时 statusCode 为 0)
xhr.onerror = function (err) {
if (xhr.responseText) {
var res = xhrBody(xhr, opt.dataType);
if (res) {
// 5xx
callback(null, xhrRes(xhr), xhr.responseText);
callback(null, xhrRes(xhr), res);
} else {
// 0
var error = xhr.statusText;
if (!error && xhr.status === 0) error = 'CORS blocked or network error';
callback(error, xhrRes(xhr), xhr.responseText);
callback(error, xhrRes(xhr), res);
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ var xhrRes = function (xhr) {
};
};

var xhrBody = function (xhr, dataType) {
return !dataType && dataType === 'text' ? xhr.responseText : xhr.response;
};

var request = function (opt, callback) {

// method
Expand Down Expand Up @@ -90,17 +94,18 @@ var request = function (opt, callback) {

// success 2xx/3xx/4xx
xhr.onload = function () {
callback(null, xhrRes(xhr), xhr.responseText);
callback(null, xhrRes(xhr), xhrBody(xhr, opt.dataType));
};

// error 5xx/0 (网络错误、跨域报错、Https connect-src 限制的报错时 statusCode 为 0)
xhr.onerror = function (err) {
if (xhr.responseText) { // 5xx
callback(null, xhrRes(xhr), xhr.responseText);
var res = xhrBody(xhr, opt.dataType);
if (res) { // 5xx
callback(null, xhrRes(xhr), res);
} else { // 0
var error = xhr.statusText;
if (!error && xhr.status === 0) error = 'CORS blocked or network error';
callback(error, xhrRes(xhr), xhr.responseText);
callback(error, xhrRes(xhr), res);
}
};

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": "cos-js-sdk-v5",
"version": "1.0.2",
"version": "1.0.3",
"description": "JavaScript SDK for [腾讯云对象存储](https://cloud.tencent.com/product/cos)",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,7 @@ function getObject(params, callback) {
Region: params.Region,
Key: params.Key,
VersionId: params.VersionId,
DataType: params.DataType,
headers: params.Headers,
qs: reqParams,
rawBody: true,
Expand Down Expand Up @@ -3295,6 +3296,9 @@ function _submitRequest(params, callback) {
params.onProgress({loaded: loaded, total: contentLength});
};
}
if (params.DataType) {
opt.dataType = params.DataType;
}
if (this.options.Timeout) {
opt.timeout = this.options.Timeout;
}
Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.0.2';
COS.version = '1.0.3';

module.exports = COS;
47 changes: 46 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,50 @@ group('getObject()', function () {
});
});
});
test('getObject() DataType blob', function (done, assert) {
var key = '1.txt';
var content = Date.now().toString();
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
Body: content,
}, function (err, data) {
cos.getObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
DataType: 'blob',
}, function (err, data) {
if (err) throw err;
assert.ok(data.Body instanceof Blob);
assert.ok(data.headers['content-length'] === '' + content.length);
done();
});
});
});
test('getObject() DataType arraybuffer', function (done, assert) {
var key = '1.txt';
var content = Date.now().toString();
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
Body: content,
}, function (err, data) {
cos.getObject({
Bucket: config.Bucket,
Region: config.Region,
Key: key,
DataType: 'arraybuffer',
}, function (err, data) {
if (err) throw err;
assert.ok(data.Body instanceof ArrayBuffer);
assert.ok(data.headers['content-length'] === '' + content.length);
done();
});
});
});
});

group('Key 特殊字符', function () {
Expand Down Expand Up @@ -2536,6 +2580,7 @@ group('upload Content-Type', function () {
group('Cache-Control', function (val) {
var isNormalCacheControl = function (val) {
return val === undefined
|| val === 'no-cache'
|| val === 'max-age=259200'
// || val === 'no-cache, max-age=259200' // IE 10
// || val === 'no-cache, max-age=7200' // firefox
Expand Down Expand Up @@ -2589,7 +2634,7 @@ group('Cache-Control', function (val) {
Region: config.Region,
Key: '1mb.zip',
}, function (err, data) {
assert.ok(isNormalCacheControl(data.headers['cache-control']), 'cache-control 正确');
assert.ok(data.headers['cache-control'] === 'no-cache' || data.headers['cache-control'] === 'no-cache, max-age=259200', 'cache-control 正确');
done();
});
});
Expand Down

0 comments on commit 62cdcb4

Please sign in to comment.