Skip to content

Commit 266a4d3

Browse files
author
dfounderliu
committed
fix cos
1 parent c1d51cc commit 266a4d3

File tree

16 files changed

+3036
-26
lines changed

16 files changed

+3036
-26
lines changed

lib/client.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ class TencentCloudClient {
1818
this.service = service
1919
}
2020

21-
async sign(str, secretKey, signatureMethod = 'sha1') {
22-
var hmac = crypto.createHmac(signatureMethod, secretKey || '')
23-
return hmac.update(new Buffer.from(str, 'utf8')).digest('base64')
24-
}
25-
26-
async generateQueryString(data) {
21+
async cloudApiGenerateQueryString(data) {
2722
var param = assign(
2823
{
2924
Region: defaults.Region,
@@ -39,10 +34,9 @@ class TencentCloudClient {
3934
}
4035
param.SignatureMethod = defaults.signatureMethod
4136
param = dotQs.flatten(param)
42-
var { host, path } = this.service
37+
const { host, path } = this.service
4338
var keys = Object.keys(param)
4439
var qstr = ''
45-
var signStr
4640
keys.sort()
4741
keys.forEach(function(key) {
4842
var val = param[key]
@@ -57,17 +51,16 @@ class TencentCloudClient {
5751

5852
qstr = qstr.slice(1)
5953

60-
signStr = await this.sign(
61-
defaults.method.toUpperCase() + host + path + '?' + qstr,
62-
this.credentials.SecretKey
63-
)
64-
param.Signature = signStr
54+
const hmac = crypto.createHmac('sha1', this.credentials.SecretKey || '')
55+
param.Signature = hmac
56+
.update(new Buffer.from(defaults.method.toUpperCase() + host + path + '?' + qstr, 'utf8'))
57+
.digest('base64')
6558

6659
return qs.stringify(param)
6760
}
6861

69-
async doRequest(data) {
70-
const httpBody = await this.generateQueryString(data)
62+
async doCloudApiRequest(data) {
63+
const httpBody = await this.cloudApiGenerateQueryString(data)
7164
const options = {
7265
hostname: this.service.host,
7366
path: this.service.path + '?' + httpBody
@@ -96,7 +89,7 @@ class ScfClient {
9689
return await new TencentCloudClient(this.credentials, {
9790
host: 'scf.tencentcloudapi.com',
9891
path: '/'
99-
}).doRequest(data)
92+
}).doCloudApiRequest(data)
10093
}
10194
}
10295

@@ -108,7 +101,7 @@ class TagClient {
108101
return await new TencentCloudClient(this.credentials, {
109102
host: 'tag.tencentcloudapi.com',
110103
path: '/'
111-
}).doRequest(data)
104+
}).doCloudApiRequest(data)
112105
}
113106
}
114107

@@ -120,7 +113,7 @@ class ApigwClient {
120113
return await new TencentCloudClient(this.credentials, {
121114
host: 'apigateway.api.qcloud.com',
122115
path: '/v2/index.php'
123-
}).doRequest(data)
116+
}).doCloudApiRequest(data)
124117
}
125118
}
126119

@@ -132,7 +125,7 @@ class CamClient {
132125
return await new TencentCloudClient(this.credentials, {
133126
host: 'cam.tencentcloudapi.com',
134127
path: '/'
135-
}).doRequest(data)
128+
}).doCloudApiRequest(data)
136129
}
137130
}
138131

@@ -144,7 +137,7 @@ class CnsClient {
144137
return await new TencentCloudClient(this.credentials, {
145138
host: 'cns.api.qcloud.com',
146139
path: '/v2/index.php'
147-
}).doRequest(data)
140+
}).doCloudApiRequest(data)
148141
}
149142
}
150143

@@ -156,7 +149,7 @@ class DomainClient {
156149
return await new TencentCloudClient(this.credentials, {
157150
host: 'domain.tencentcloudapi.com',
158151
path: '/'
159-
}).doRequest(data)
152+
}).doCloudApiRequest(data)
160153
}
161154
}
162155

lib/cos/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var REQUEST = require('request')
1+
var REQUEST = require('../request/index')
22
var mime = require('mime-types')
33
var util = require('./util')
44
var fs = require('fs')

lib/request/index.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright 2010-2012 Mikeal Rogers
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
var extend = require('extend')
16+
var cookies = require('./lib/cookies')
17+
var helpers = require('./lib/helpers')
18+
19+
var paramsHaveRequestBody = helpers.paramsHaveRequestBody
20+
21+
// organize params for patch, post, put, head, del
22+
function initParams(uri, options, callback) {
23+
if (typeof options === 'function') {
24+
callback = options
25+
}
26+
27+
var params = {}
28+
if (options !== null && typeof options === 'object') {
29+
extend(params, options, { uri: uri })
30+
} else if (typeof uri === 'string') {
31+
extend(params, { uri: uri })
32+
} else {
33+
extend(params, uri)
34+
}
35+
36+
params.callback = callback || params.callback
37+
return params
38+
}
39+
40+
function request(uri, options, callback) {
41+
if (typeof uri === 'undefined') {
42+
throw new Error('undefined is not a valid uri or options object.')
43+
}
44+
45+
var params = initParams(uri, options, callback)
46+
47+
if (params.method === 'HEAD' && paramsHaveRequestBody(params)) {
48+
throw new Error('HTTP HEAD requests MUST NOT include a request body.')
49+
}
50+
51+
return new request.Request(params)
52+
}
53+
54+
function verbFunc(verb) {
55+
var method = verb.toUpperCase()
56+
return function(uri, options, callback) {
57+
var params = initParams(uri, options, callback)
58+
params.method = method
59+
return request(params, params.callback)
60+
}
61+
}
62+
63+
// define like this to please codeintel/intellisense IDEs
64+
request.get = verbFunc('get')
65+
request.head = verbFunc('head')
66+
request.options = verbFunc('options')
67+
request.post = verbFunc('post')
68+
request.put = verbFunc('put')
69+
request.patch = verbFunc('patch')
70+
request.del = verbFunc('delete')
71+
request['delete'] = verbFunc('delete')
72+
73+
request.jar = function(store) {
74+
return cookies.jar(store)
75+
}
76+
77+
request.cookie = function(str) {
78+
return cookies.parse(str)
79+
}
80+
81+
function wrapRequestMethod(method, options, requester, verb) {
82+
return function(uri, opts, callback) {
83+
var params = initParams(uri, opts, callback)
84+
85+
var target = {}
86+
extend(true, target, options, params)
87+
88+
target.pool = params.pool || options.pool
89+
90+
if (verb) {
91+
target.method = verb.toUpperCase()
92+
}
93+
94+
if (typeof requester === 'function') {
95+
method = requester
96+
}
97+
98+
return method(target, target.callback)
99+
}
100+
}
101+
102+
request.defaults = function(options, requester) {
103+
var self = this
104+
105+
options = options || {}
106+
107+
if (typeof options === 'function') {
108+
requester = options
109+
options = {}
110+
}
111+
112+
var defaults = wrapRequestMethod(self, options, requester)
113+
114+
var verbs = ['get', 'head', 'post', 'put', 'patch', 'del', 'delete']
115+
verbs.forEach(function(verb) {
116+
defaults[verb] = wrapRequestMethod(self[verb], options, requester, verb)
117+
})
118+
119+
defaults.cookie = wrapRequestMethod(self.cookie, options, requester)
120+
defaults.jar = self.jar
121+
defaults.defaults = self.defaults
122+
return defaults
123+
}
124+
125+
request.forever = function(agentOptions, optionsArg) {
126+
var options = {}
127+
if (optionsArg) {
128+
extend(options, optionsArg)
129+
}
130+
if (agentOptions) {
131+
options.agentOptions = agentOptions
132+
}
133+
134+
options.forever = true
135+
return request.defaults(options)
136+
}
137+
138+
// Exports
139+
140+
module.exports = request
141+
request.Request = require('./request')
142+
request.initParams = initParams
143+
144+
// Backwards compatibility for request.debug
145+
Object.defineProperty(request, 'debug', {
146+
enumerable: true,
147+
get: function() {
148+
return request.Request.debug
149+
},
150+
set: function(debug) {
151+
request.Request.debug = debug
152+
}
153+
})

0 commit comments

Comments
 (0)