Skip to content

Commit

Permalink
feat: 支持上传回调参数; bump: 1.0.0-rc.42
Browse files Browse the repository at this point in the history
  • Loading branch information
lurunze1226 committed Oct 26, 2023
1 parent 724b843 commit ae21010
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 44 deletions.
51 changes: 51 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
# CHANGELOG

## 1.0.0-rc.42

_published on 2023-10-26_

- BosClient: support callback parameter in options;

Approach 1:

> Use the callback parameter, the SDK will help you process the parameter and add it to the request header.
```javascript
try {
const res = await client.putObjectFromString('bucketName', 'fileName', 'demo-string', {
callback: {
urls: ["https://www.test.com/callback"],
vars: {name: 'baidu'},
encrypt: 'config',
key: 'callback1'
}
});

/* callback result */
console.log(res.body.callback.result);
} catch (e) {
/* callback error code */
console.error(res.body.callback.code);
/* callback error message */
console.error(res.body.callback.message);
}
```

Approach 2:

> Directly pass the "x-bce-process" parameter to headers.
```javascript
try {
const res = await client.putObjectFromString('bucketName', 'fileName', 'demo-string', {
'x-bce-process': 'callback/callback,u_WyJodHRwczovL3d3dy50ZXN0LmNvbS9jYWxsYmFjayJd,m_sync,v_eyJuYW1lIjoiYmFpZHUifQ'
});

/* callback result */
console.log(res.body.callback.result);
} catch (e) {
/* callback error code */
console.error(res.body.callback.code);
/* callback error message */
console.error(res.body.callback.message);
}
```

## 1.0.0-rc.41

_published on 2023-10-26_
Expand Down
164 changes: 127 additions & 37 deletions dist/baidubce-sdk.bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/baidubce-sdk.bundle.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exports.strings = require('./src/strings');
exports.STS = require('./src/sts');
exports.Auth = require('./src/auth');
exports.MimeType = require('./src/mime.types');
exports.Base64 = require('./src/base64');

exports.HttpClient = require('./src/http_client');
exports.BceBaseClient = require('./src/bce_base_client');
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@baiducloud/sdk",
"version": "1.0.0-rc.41",
"version": "1.0.0-rc.42",
"description": "Baidu Cloud Engine JavaScript SDK",
"main": "./index.js",
"browser": {
Expand Down
57 changes: 57 additions & 0 deletions src/base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* @file src/base64.js
* @author lurunze
*/

function getEnv() {
if (typeof global !== 'undefined') {
return 'Node.js';
} else if (typeof window !== 'undefined') {
return 'Browser';
} else {
return 'Unknown environment';
}
}

exports.urlEncode = function urlEncode(inputStr) {
const env = getEnv();
let base64Str = inputStr && typeof inputStr ==='string'? inputStr : JSON.stringify(inputStr);

if (env === 'Node.js') {
const buffer = Buffer.from(base64Str, 'utf8');
base64Str = buffer.toString('base64');
}
else if (env === 'Browser') {
base64Str = window.btoa(base64Str);
}

return base64Str
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}

exports.urlDecode = function urlDecode(inputStr) {
const env = getEnv();
let result = (inputStr && typeof inputStr === 'string' ? inputStr : '').replace(/\-/g, '+').replace(/\_/g, '/');

if (env === 'Node.js') {
result = Buffer.from(result, 'base64').toString('utf-8');
}
else if (env === 'Browser') {
result = window.atob(str);
}

return result;
}
28 changes: 28 additions & 0 deletions src/bos_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var BceBaseClient = require('./bce_base_client');
var MimeType = require('./mime.types');
var WMStream = require('./wm_stream');
var Multipart = require('./multipart');
var Base64 = require('./base64');

// var MIN_PART_SIZE = 1048576; // 1M
// var THREAD = 2;
Expand Down Expand Up @@ -1722,6 +1723,33 @@ BosClient.prototype._checkOptions = function (options, allowedParams) {
rv.headers = this._prepareObjectHeaders(options);
rv.params = u.pick(options, allowedParams || []);

/** 如果使用callback参数格式传入,将参数处理为字符串格式 */
if (!u.has(options, H.X_BCE_PROCESS) && u.has(options, 'callback')) {
const callbackParams = u.extend(
{
/** urls, 缩写为u */
u: Base64.urlEncode(options.callback.urls),
/** mode, 缩写为u */
m: 'sync',
v: Base64.urlEncode(options.callback.vars),
},
/** encrypt, 缩写为e */
options.callback.encrypt && options.callback.encrypt === 'config' ? {e: 'config'} : {},
/** key, 缩写为k */
options.callback.key ? {k: options.callback.key} : {}
);
let callbackStr = '';
const callbackKeys = Object.keys(callbackParams);

callbackKeys.forEach((key, index) => {
callbackStr += key + '_' + callbackParams[key] + (index === callbackKeys.length - 1 ? '' : ',')
})

if (callbackStr) {
rv.headers[H.X_BCE_PROCESS] = 'callback/callback,' + callbackStr;
}
}

return rv;
};

Expand Down
12 changes: 9 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@

const BosClient = require('../').BosClient;
const config = require('./config');

console.log(config.bos);
const client = new BosClient(config.bos);

(async function () {})()
(async function () {
/** callback demo */
const res = await client.putObjectFromString('amis-mock', 'str1', 'lurunze', {
callback: {
urls: ["https://www.test.com/callback"],
vars: {name: 'baidu'}
}
});
})()

0 comments on commit ae21010

Please sign in to comment.