From f2145039a9afd6cc82841d0cec71cf00bc8e2b2e Mon Sep 17 00:00:00 2001 From: chrisftian Date: Tue, 6 Dec 2022 11:32:57 +0800 Subject: [PATCH] Feat/2.11.17 beta (#158) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 支持动态秘钥Cred * upd version * fix: 修改字段 --- index.d.ts | 11 ++++++++--- package.json | 2 +- sdk/cos.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3d1f259..bd643c1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -194,6 +194,11 @@ declare namespace COS { params: GetAuthorizationCallbackParams ) => void ) => void, + /** 支持动态Credentials */ + Credentials?: { + secretId?: string; + secretKey?: string; + }; } type StringOrBuffer = Buffer | String; @@ -2294,13 +2299,13 @@ declare class COS { /** 获取上传任务列表 */ getTaskList(): COS.TaskList; - /** 判断上传队列是否有未完成的任务 */ + /** 暂停任务 */ pauseTask(taskId: COS.TaskId): void; - /** 判断上传队列是否有未完成的任务 */ + /** 重启任务 */ restartTask(taskId: COS.TaskId): void; - /** 判断上传队列是否有未完成的任务 */ + /** 取消任务 */ cancelTask(taskId: COS.TaskId): void; /** 判断上传队列是否有未完成的任务 */ diff --git a/package.json b/package.json index dcc3c8e..85e9e5e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cos-nodejs-sdk-v5", - "version": "2.11.16", + "version": "2.11.17", "description": "cos nodejs sdk v5", "main": "index.js", "types": "index.d.ts", diff --git a/sdk/cos.js b/sdk/cos.js index 8454095..575fae4 100644 --- a/sdk/cos.js +++ b/sdk/cos.js @@ -44,6 +44,24 @@ var defaultOptions = { UserAgent: '', ConfCwd: '', ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true + // 动态秘钥,优先级Credentials > SecretId/SecretKey。注意Cred内是小写的secretId、secretKey + Credentials: { + secretId: '', + secretKey: '', + }, +}; + +const watch = (obj, name, callback) => { + let value = obj[name]; + Object.defineProperty(obj, name, { + get() { + return value; + }, + set(newValue) { + value = newValue; + callback(); + } + }); }; // 对外暴露的类 @@ -66,6 +84,11 @@ var COS = function (options) { if (this.options.secretId && !this.options.SecretId) this.options.SecretId = this.options.secretId; if (this.options.secretKey && !this.options.SecretKey) this.options.SecretKey = this.options.secretKey; console.warn('warning: Please change options secretId/secretKey to SecretId/SecretKey.'); + } + // 支持外部传入Cred动态秘钥 + if (this.options.Credentials) { + this.options.SecretId = this.options.Credentials.secretId || ''; + this.options.SecretKey = this.options.Credentials.secretKey || ''; } if (this.options.SecretId && this.options.SecretId.indexOf(' ') > -1) { console.error('error: SecretId格式错误,请检查'); @@ -81,6 +104,16 @@ var COS = function (options) { } event.init(this); task.init(this); + + // 支持动态秘钥,监听到cred里secretId、secretKey变化时,主动给cos替换秘钥 + watch(this.options.Credentials, 'secretId', () => { + console.log('Credentials secretId changed'); + this.options.SecretId = this.options.Credentials.secretId; + }); + watch(this.options.Credentials, 'secretKey', () => { + console.log('Credentials secretKey changed'); + this.options.SecretKey = this.options.Credentials.secretKey; + }); }; base.init(COS, task);