-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
225 lines (196 loc) · 7.1 KB
/
utils.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
var crypto = require('crypto');
var _ = require('lodash');
var ACCOUNT_STRING_KEYS = ['TableEndpoint', 'AccountName', 'AccountKey'];
var ACCOUNT_STRING_SETTINGS_KEYS = ['accountUrl', 'accountName', 'accountKey'];
exports.parseAccountString = function parseAccountString(accountString) {
if (typeof accountString !== 'string') {
return null;
}
var trimmedAS = accountString.trim();
if (trimmedAS.length < 30) {
return null;
}
var splittedAS = trimmedAS.split(';');
if (splittedAS.length < 3) {
return null;
}
var entry, i, j, retrievedValues = {};
for (i = 0; i < splittedAS.length; ++i) {
entry = splittedAS[i].split('=');
if (entry.length < 2) {
return null;
}
// get back if within string
if (entry.length > 2) {
for (j = 2; j < entry.length; ++j) {
entry[1] += '='+entry[j];
}
}
if (ACCOUNT_STRING_KEYS.indexOf(entry[0]) !== -1) {
retrievedValues[entry[0]] = entry[1];
}
}
if (Object.keys(retrievedValues).length !== ACCOUNT_STRING_KEYS.length) {
return null;
}
// convert to settings keys
var finalValues = {};
for (i = 0; i < ACCOUNT_STRING_SETTINGS_KEYS.length; ++i) {
finalValues[ACCOUNT_STRING_SETTINGS_KEYS[i]] = retrievedValues[ACCOUNT_STRING_KEYS[i]];
}
return finalValues;
};
exports.base64Decode = function base64Decode(base64String) {
return new Buffer(base64String, 'base64');
};
exports.hmacSha256 = function hmacSha256(keyBuffer, stringToSign) {
return crypto.createHmac('sha256', keyBuffer).update(stringToSign, 'utf-8').digest('base64');
};
exports.prepareEntityPath = function prepareEntityPath(table, partitionKey, rowKey) {
return table + '(PartitionKey=\'' + encodeURIComponent(partitionKey.replace(/'/g, '\'\'')) + '\',RowKey=\'' + encodeURIComponent(rowKey.replace(/'/g, '\'\'')) + '\')';
};
exports.prepareSelectQS = function prepareSelectQS(fields) {
return fields.join(',');
};
var _guidRegExp = /^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$/;
exports.isGuid = function isGuid(value) {
return _guidRegExp.test(value);
};
exports.serializeEntity = function serializeEntity(entity) {
var finalObject = {}, value;
for (var key in entity) {
// __etag will be provided in different way, so skip it here too
if (!entity.hasOwnProperty(key) || key === '__etag' || key === 'Timestamp') {
continue;
}
if (entity[key] == null) { // this includes undefined
continue;
}
// they do not require any special handling
if (key === 'PartitionKey' || key === 'RowKey' ) {
finalObject[key] = entity[key];
continue;
}
value = entity[key];
if (typeof value === 'string') {
if (value.length === 36 && _guidRegExp.test(value)) {
finalObject[key] = value;
finalObject[key+'@odata.type'] = 'Edm.Guid';
} else {
finalObject[key] = value; // normal string, no need to pass type info
}
} else if (typeof value === 'number') {
if (value % 1 === 0) { // checks if it is integer
if (Math.abs(value) < 2147483648) {
finalObject[key] = value; // 32 bit can be passed as int without type info
} else {
finalObject[key] = value.toString();
finalObject[key+'@odata.type'] = 'Edm.Int64';
}
} else {
finalObject[key] = value; // float can be passed without type info
}
} else if (_.isDate(value)) {
finalObject[key] = value.toISOString();
finalObject[key+'@odata.type'] = 'Edm.DateTime';
} else if (_.isBoolean(value)) {
finalObject[key] = value;
} else if (value instanceof Buffer) {
finalObject[key] = value.toString('base64');
finalObject[key+'@odata.type'] = 'Edm.Binary';
} else { // to be on a safe side, convert anything else to string
finalObject[key] = value.toString();
}
}
return finalObject;
};
exports.deserializeEntity = function deserializeEntity(entity) {
var finalObject = {}, value, type;
for (var key in entity) {
// skip all the odata info keys too
if (!entity.hasOwnProperty(key) || key.indexOf('odata.') >= 0) {
continue;
}
value = entity[key];
// handle always returned values first
if (key === 'PartitionKey' || key === 'RowKey') {
finalObject[key] = value;
continue;
} else if (key === 'Timestamp') {
finalObject[key] = new Date(value);
continue;
}
// handle other properties but check if they need converting
type = entity[key+'@odata.type'];
if (!type) {
finalObject[key] = value;
} else {
if (type === 'Edm.Int64') {
finalObject[key] = parseInt(value, 10);
} else if (type === 'Edm.DateTime') {
finalObject[key] = new Date(value);
} else if (type === 'Edm.Binary') {
finalObject[key] = new Buffer(value, 'base64');
} else { // for all other do not convert
finalObject[key] = value;
}
}
}
if ('odata.etag' in entity) {
finalObject.__etag = entity['odata.etag'];
}
return finalObject;
};
var _defaultRetryOptions = {
retries: 3,
firstDelay: 2000,
nextDelayMult: 2,
variability: 0.2,
transientErrors: [500, 501, 502, 503, 'ETIMEDOUT', 'ECONNRESET', 'EADDRINUSE', 'ESOCKETTIMEDOUT', 'ECONNREFUSED']
};
function _retryLogic(requestOptions, nextReq) {
// requestOptions needs to be edited in place (it is safe to do so)
// but retry does not change it in any way
var retry = 0, delay, settings = this;
function retryFn(err, resp, nextResp) {
if (err) {
if (retry < settings.retries && (settings.transientErrors.indexOf(err.code) !== -1 || settings.transientErrors.indexOf(err.statusCode) !== -1)) {
retry += 1;
if (retry === 1) {
delay = settings.firstDelay;
} else {
delay *= settings.nextDelayMult;
}
delay += (Math.random() - 0.5) * settings.variability * delay;
delay = Math.floor(delay);
setTimeout(function() {
nextReq(retryFn);
}, delay);
} else {
err.retriesMade = retry;
nextResp(err, resp);
}
} else {
nextResp(err, resp);
}
}
nextReq(retryFn);
}
exports.generateRetryFunction = function generateRetryFunction(options) {
var finalOptions = !options ? _defaultRetryOptions : {
retries: options.retries > 0 ? options.retries : _defaultRetryOptions.retries,
firstDelay: options.firstDelay > 0 ? options.firstDelay : _defaultRetryOptions.firstDelay,
nextDelayMult: options.nextDelayMult > 0 ? options.nextDelayMult : _defaultRetryOptions.nextDelayMult,
variability: typeof options.variability === 'number' ? options.variability : _defaultRetryOptions.variability,
transientErrors: Array.isArray(options.transientErrors) ? options.transientErrors : _defaultRetryOptions.transientErrors
};
return _retryLogic.bind(finalOptions);
};
exports.makeSignature = function makeSignature(keyBuffer, values) {
return exports.hmacSha256(keyBuffer, values.join('\n'));
};
exports.isoDateWithoutMiliseconds = function isoDateWithoutMiliseconds(date) {
var raw = date.toJSON();
return raw.substr(0, raw.lastIndexOf('.')) + 'Z';
};