region_config.js
2.53 KB
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
var util = require('./util');
var regionConfig = require('./region_config_data.json');
function generateRegionPrefix(region) {
if (!region) return null;
var parts = region.split('-');
if (parts.length < 3) return null;
return parts.slice(0, parts.length - 2).join('-') + '-*';
}
function derivedKeys(service) {
var region = service.config.region;
var regionPrefix = generateRegionPrefix(region);
var endpointPrefix = service.api.endpointPrefix;
return [
[region, endpointPrefix],
[regionPrefix, endpointPrefix],
[region, '*'],
[regionPrefix, '*'],
['*', endpointPrefix],
['*', '*']
].map(function(item) {
return item[0] && item[1] ? item.join('/') : null;
});
}
function applyConfig(service, config) {
util.each(config, function(key, value) {
if (key === 'globalEndpoint') return;
if (service.config[key] === undefined || service.config[key] === null) {
service.config[key] = value;
}
});
}
function configureEndpoint(service) {
var keys = derivedKeys(service);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (!key) continue;
if (Object.prototype.hasOwnProperty.call(regionConfig.rules, key)) {
var config = regionConfig.rules[key];
if (typeof config === 'string') {
config = regionConfig.patterns[config];
}
// set dualstack endpoint
if (service.config.useDualstack && util.isDualstackAvailable(service)) {
config = util.copy(config);
config.endpoint = '{service}.dualstack.{region}.amazonaws.com';
}
// set global endpoint
service.isGlobalEndpoint = !!config.globalEndpoint;
// signature version
if (!config.signatureVersion) config.signatureVersion = 'v4';
// merge config
applyConfig(service, config);
return;
}
}
}
function getEndpointSuffix(region) {
var regionRegexes = {
'^(us|eu|ap|sa|ca|me)\\-\\w+\\-\\d+$': 'amazonaws.com',
'^cn\\-\\w+\\-\\d+$': 'amazonaws.com.cn',
'^us\\-gov\\-\\w+\\-\\d+$': 'amazonaws.com',
'^us\\-iso\\-\\w+\\-\\d+$': 'c2s.ic.gov',
'^us\\-isob\\-\\w+\\-\\d+$': 'sc2s.sgov.gov'
};
var defaultSuffix = 'amazonaws.com';
var regexes = Object.keys(regionRegexes);
for (var i = 0; i < regexes.length; i++) {
var regionPattern = RegExp(regexes[i]);
var dnsSuffix = regionRegexes[regexes[i]];
if (regionPattern.test(region)) return dnsSuffix;
}
return defaultSuffix;
}
/**
* @api private
*/
module.exports = {
configureEndpoint: configureEndpoint,
getEndpointSuffix: getEndpointSuffix
};