-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (45 loc) · 1.22 KB
/
index.js
File metadata and controls
53 lines (45 loc) · 1.22 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
var Q = require('kew')
var http = require('http')
var getAvailabilityZone = exports.getAvailabilityZone = function getAvailabilityZone(callback, callbackScope) {
var defer = Q.defer()
var options = {
host: '169.254.169.254',
port: 80,
path: '/latest/meta-data/placement/availability-zone'
}
var req = http.get(options, function (res) {
var data = ''
res.on('data', function (chunk) {
data += chunk
})
res.on('end', function () {
defer.resolve(data)
})
}).on('error', function (e) {
defer.reject(e)
}).on('socket', function (socket) {
socket.setTimeout(2000);
socket.on('timeout', function() {
req.abort();
});
})
var promise = defer.promise
if (callback) {
promise = promise
.then(callback.bind(callbackScope, null))
.fail(callback.bind(callbackScope))
}
return promise
}
var getRegion = exports.getRegion = function getRegion(callback, callbackScope) {
var promise = getAvailabilityZone()
.then(function (zone) {
return zone.replace(/([0-9][0-9]*)[a-z]*$/, "$1")
})
if (callback) {
promise = promise
.then(callback.bind(callbackScope, null))
.fail(callback.bind(callbackScope))
}
return promise
}