-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.js
185 lines (149 loc) · 4.83 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
const url = require('url');
const httpx = require('httpx');
const kitx = require('kitx');
const actions = require('./actions');
const ots2 = require('./ots2');
const OTSError = ots2.Error;
const debug = require('debug')('ots');
var getCanonicalHeaders = function (headers) {
var keys = Object.keys(headers);
var selectedKeys = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (key.indexOf('x-ots-') !== -1 && key !== 'x-ots-signature') {
selectedKeys.push(key);
}
}
selectedKeys.sort();
var str = '';
for (var j = 0; j < selectedKeys.length; j++) {
var name = selectedKeys[j];
str += name + ':' + headers[name].trim() + '\n';
}
return str;
};
/**
* OTS Client
* @param {Object} opts
* - instance {String} instance
* - region {String} region
* - internal
* - endpoint
* - accessKeyID
* - accessKeySecret
* - keepAliveMsecs {Number} http(s) agent keep socket conn time, unit ms
* - timeout
*/
class Client {
constructor(opts) {
if (!opts || typeof opts !== 'object') {
throw new TypeError('must pass in opts as Object');
}
this.accessKeyID = opts.accessKeyID;
this.accessKeySecret = opts.accessKeySecret;
if (!this.accessKeyID || !this.accessKeySecret) {
throw new Error('must pass in accessKeyID and accessKeySecret');
}
this.instance = opts.instance;
if (!this.instance) {
throw new Error('must pass in instance');
}
if (opts.endpoint) {
this.endpoint = opts.endpoint;
} else {
this.region = opts.region;
if (!this.region) {
throw new Error('must pass in region');
}
var env = opts.internal ? 'ots-internal' : 'ots';
this.endpoint = `http://${opts.instance}.${opts.region}.${env}.aliyuncs.com/`;
}
var urlobj = url.parse(this.endpoint);
var httplib = urlobj.protocol === 'http:' ? require('http') : require('https');
this.keepAliveAgent = httplib.Agent({
keepAlive: true,
keepAliveMsecs: opts.keepAliveMsecs
});
if (typeof opts.timeout === 'number') {
this.timeout = opts.timeout;
}
}
sign(input) {
return kitx.sha1(input, this.accessKeySecret, 'base64');
}
buildHeaders(canonicalURI, content) {
var headers = {
'x-ots-date': (new Date()).toISOString(),
'x-ots-apiversion': '2015-12-31',
'x-ots-accesskeyid': this.accessKeyID,
'x-ots-instancename': this.instance,
'x-ots-contentmd5': kitx.md5(content, 'base64'),
'content-length': Buffer.byteLength(content)
// 'User-Agent': 'aliyun-tablestore-sdk/'
};
var canonicalHeaders = getCanonicalHeaders(headers);
var method = 'POST';
var stringToSign = `${canonicalURI}\n${method}\n\n${canonicalHeaders}`;
debug('basestring is %s', stringToSign);
headers['x-ots-signature'] = this.sign(stringToSign);
return headers;
}
async request(operation, data) {
var url = this.endpoint + operation;
// get body
var Request = ots2[operation + 'Request'];
var body = new Request(data).toBuffer();
var headers = this.buildHeaders('/' + operation, body);
var opts = {
method: 'POST',
data: body,
headers: headers,
agent: this.keepAliveAgent
};
if (this.timeout) {
opts.timeout = this.timeout;
}
debug('url is %s', url);
var res = await httpx.request(url, opts);
debug('response headers is %j', res.headers);
var buff = await httpx.read(res);
if (res.statusCode < 200 || res.statusCode >= 400) {
var error = OTSError.decode(buff);
var err = new Error(error.message);
err.name = error.code + 'Error';
err.data = data;
throw err;
}
if (!this.checkResponseSign(operation, res.headers)) {
throw new Error('响应签名不对');
}
if (!this.checkResponseDate(res.headers)) {
throw new Error('响应时间不对');
}
if (!this.checkResponseContent(res.headers, buff)) {
throw new Error('响应内容不对');
}
return ots2[operation + 'Response'].decode(buff);
}
checkResponseSign(operation, headers) {
var canonicalURI = '/' + operation;
var canonicalHeaders = getCanonicalHeaders(headers);
var stringToSign = canonicalHeaders + canonicalURI;
debug('basestring is %s', stringToSign);
var signature = this.sign(stringToSign);
var auth = 'OTS ' + this.accessKeyID + ':' + signature;
return headers['authorization'] === auth;
}
checkResponseDate(headers) {
var now = new Date().getTime();
var date = Date.parse(headers['x-ots-date']);
return Math.abs(now - date) < 15 * 60 * 1000; // 对比时间,不能相差超过15分钟
}
checkResponseContent(headers, content) {
var hash = headers['x-ots-contentmd5'];
return kitx.md5(content, 'base64') === hash;
}
}
Object.assign(Client.prototype, actions);
module.exports = Client;