Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (65 loc) · 1.54 KB
/
Copy pathindex.js
File metadata and controls
80 lines (65 loc) · 1.54 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
'use strict'
var Dr = require('docker-remote-api')
var Promise = require('native-or-bluebird')
var dkr
function drPromised (method, url, opts) {
opts = opts || {}
var json = false
// using the json flag on docker-remote-api makes things hard so we'll
// fix that
if (opts.json) {
json = true
delete opts.json
}
if (method === 'post' && !opts.body) {
opts.body = null
}
return new Promise(function (resolve, reject) {
dkr[method](url, opts, function (err, stream) {
var statusRange = 2
if (stream && stream.statusCode) {
statusRange = Math.floor(stream.statusCode / 100)
}
if (err || statusRange !== 2) {
return reject(err)
}
var data = []
if (!stream) {
return resolve(true)
}
stream
.on('data', function (chunk) {
data.push(chunk.toString())
})
.on('error', function (err) {
reject(err)
})
.on('end', function () {
var resp = data.join('')
if (json) {
resp = JSON.parse(resp)
}
resolve(resp)
})
})
})
}
module.exports = function (host) {
if (!dkr) {
dkr = new Dr(host)
}
return {
post: function (url, opts) {
return drPromised('post', url, opts)
},
get: function (url, opts) {
return drPromised('get', url, opts)
},
delete: function (url, opts) {
return drPromised('delete', url, opts)
},
put: function (url, opts) {
return drPromised('put', url, opts)
}
}
}