Skip to content

Commit

Permalink
支持 after-receive 事件和 Request 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Nov 10, 2020
1 parent f94ac32 commit f3eab19
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 65 deletions.
65 changes: 33 additions & 32 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.4';
COS.version = '1.1.0';

module.exports = COS;

Expand Down Expand Up @@ -5227,17 +5227,8 @@ function getBucketLocation(params, callback) {
}

function putBucketPolicy(params, callback) {
var Policy = params['Policy'];
var PolicyStr = Policy;
try {
if (typeof Policy === 'string') {
Policy = JSON.parse(PolicyStr);
} else {
PolicyStr = JSON.stringify(Policy);
}
} catch (e) {
callback({ error: 'Policy format error' });
}
var PolicyStr = params['Policy'];
if (typeof PolicyStr !== 'string') PolicyStr = JSON.stringify(PolicyStr);

var headers = params.Headers;
headers['Content-Type'] = 'application/json';
Expand All @@ -5250,8 +5241,7 @@ function putBucketPolicy(params, callback) {
Region: params.Region,
action: 'policy',
body: PolicyStr,
headers: headers,
json: true
headers: headers
}, function (err, data) {
if (err && err.statusCode === 204) {
return callback(null, { statusCode: err.statusCode });
Expand Down Expand Up @@ -6405,7 +6395,6 @@ function putBucketAccelerate(params, callback) {
headers['Content-MD5'] = util.binaryBase64(util.md5(xml));

submitRequest.call(this, {
Interface: 'putBucketAccelerate',
Action: 'name/cos:PutBucketAccelerate',
method: 'PUT',
Bucket: params.Bucket,
Expand All @@ -6424,7 +6413,6 @@ function putBucketAccelerate(params, callback) {

function getBucketAccelerate(params, callback) {
submitRequest.call(this, {
Interface: 'getBucketAccelerate',
Action: 'name/cos:GetBucketAccelerate',
method: 'GET',
Bucket: params.Bucket,
Expand Down Expand Up @@ -6885,6 +6873,7 @@ function putObjectCopy(params, callback) {
var SourceKey = decodeURIComponent(m[4]);

submitRequest.call(this, {
Interface: 'PutObjectCopy',
Scope: [{
action: 'name/cos:GetObject',
bucket: SourceBucket,
Expand Down Expand Up @@ -6927,6 +6916,7 @@ function uploadPartCopy(params, callback) {
var SourceKey = decodeURIComponent(m[4]);

submitRequest.call(this, {
Interface: 'UploadPartCopy',
Scope: [{
action: 'name/cos:GetObject',
bucket: SourceBucket,
Expand Down Expand Up @@ -6980,6 +6970,7 @@ function deleteMultipleObject(params, callback) {
});

submitRequest.call(this, {
Interface: 'DeleteMultipleObjects',
Scope: Scope,
method: 'POST',
Bucket: params.Bucket,
Expand Down Expand Up @@ -7057,7 +7048,6 @@ function putObjectTagging(params, callback) {
headers['Content-MD5'] = util.binaryBase64(util.md5(xml));

submitRequest.call(this, {
Interface: 'putObjectTagging',
Action: 'name/cos:PutObjectTagging',
method: 'PUT',
Bucket: params.Bucket,
Expand Down Expand Up @@ -7092,7 +7082,6 @@ function putObjectTagging(params, callback) {
function getObjectTagging(params, callback) {

submitRequest.call(this, {
Interface: 'getObjectTagging',
Action: 'name/cos:GetObjectTagging',
method: 'GET',
Key: params.Key,
Expand Down Expand Up @@ -7139,7 +7128,6 @@ function getObjectTagging(params, callback) {
*/
function deleteObjectTagging(params, callback) {
submitRequest.call(this, {
Interface: 'deleteObjectTagging',
Action: 'name/cos:DeleteObjectTagging',
method: 'DELETE',
Bucket: params.Bucket,
Expand Down Expand Up @@ -7183,7 +7171,7 @@ function selectObjectContent(params, callback) {
headers['Content-MD5'] = util.binaryBase64(util.md5(xml));

submitRequest.call(this, {
Interface: 'selectObjectContent',
Interface: 'SelectObjectContent',
Action: 'name/cos:GetObject',
method: 'POST',
Bucket: params.Bucket,
Expand Down Expand Up @@ -7981,7 +7969,6 @@ function _submitRequest(params, callback) {
var method = params.method || 'GET';
var url = params.url;
var body = params.body;
var json = params.json;
var rawBody = params.rawBody;

// url
Expand All @@ -8002,8 +7989,7 @@ function _submitRequest(params, callback) {
url: url,
headers: params.headers,
qs: params.qs,
body: body,
json: json
body: body
};

// 获取签名
Expand Down Expand Up @@ -8033,10 +8019,23 @@ function _submitRequest(params, callback) {
opt.timeout = this.options.Timeout;
}

// 整理 cosInterface 用于 before-send 使用
if (params.Interface) {
opt.cosInterface = params.Interface;
} else if (params.Action) {
opt.cosInterface = params.Action.replace(/^name\/cos:/, '');
}

self.options.ForcePathStyle && (opt.pathStyle = self.options.ForcePathStyle);
self.emit('before-send', opt);
var sender = REQUEST(opt, function (err, response, body) {
if (err === 'abort') return;
var sender = (self.options.Request || REQUEST)(opt, function (r) {
if (r.error === 'abort') return;

// 抛出事件,允许修改返回值的 error、statusCode、statusMessage、body
self.emit('after-receive', r);
var response = { statusCode: r.statusCode, statusMessage: r.statusMessage, headers: r.headers };
var err = r.error;
var body = r.body;

// 返回内容添加 状态码 和 headers
var hasReturned;
Expand Down Expand Up @@ -8242,7 +8241,7 @@ var queryStringify = function (obj, sep, eq, name) {
return encodeURIComponent(stringifyPrimitive(name)) + eq + encodeURIComponent(stringifyPrimitive(obj));
};

var xhrRes = function (xhr) {
var xhrRes = function (err, xhr, body) {
var headers = {};
xhr.getAllResponseHeaders().trim().split('\n').forEach(function (item) {
if (item) {
Expand All @@ -8253,9 +8252,11 @@ var xhrRes = function (xhr) {
}
});
return {
error: err,
statusCode: xhr.status,
statusMessage: xhr.statusText,
headers: headers
headers: headers,
body: body
};
};

Expand Down Expand Up @@ -8297,20 +8298,20 @@ var request = function (opt, callback) {

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

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

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

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var queryStringify = function(obj, sep, eq, name) {
encodeURIComponent(stringifyPrimitive(obj));
};

var xhrRes = function (xhr) {
var xhrRes = function (err, xhr, body) {
var headers = {};
xhr.getAllResponseHeaders().trim().split('\n').forEach(function (item) {
if (item) {
Expand All @@ -46,9 +46,11 @@ var xhrRes = function (xhr) {
}
});
return {
error: err,
statusCode: xhr.status,
statusMessage: xhr.statusText,
headers: headers
headers: headers,
body: body,
};
};

Expand Down Expand Up @@ -94,18 +96,18 @@ var request = function (opt, callback) {

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

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

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.4",
"version": "1.1.0",
"description": "JavaScript SDK for [腾讯云对象存储](https://cloud.tencent.com/product/cos)",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit f3eab19

Please sign in to comment.