UNPKG

2.32 kBJavaScriptView Raw
1'use strict'
2
3function sslCertsPromise (app, heroku) {
4 return heroku.request({
5 path: `/apps/${app}/ssl-endpoints`,
6 headers: {'Accept': 'application/vnd.heroku+json; version=3.ssl_cert'}
7 }).then(function (data) {
8 return data
9 })
10}
11
12function sniCertsPromise (app, heroku) {
13 return heroku.request({
14 path: `/apps/${app}/sni-endpoints`,
15 headers: {'Accept': 'application/vnd.heroku+json; version=3.sni_ssl_cert'}
16 }).then(function (data) {
17 return data
18 })
19}
20
21function meta (app, t, name) {
22 var path, type, variant
23 if (t === 'sni') {
24 type = 'SNI'
25 path = `/apps/${app}/sni-endpoints`
26 variant = 'sni_ssl_cert'
27 } else if (t === 'ssl') {
28 type = 'Endpoint'
29 path = `/apps/${app}/ssl-endpoints`
30 variant = 'ssl_cert'
31 } else {
32 throw Error('Unknown type ' + type)
33 }
34 if (name) {
35 path = `${path}/${name}`
36 }
37 return {path, type, variant}
38}
39
40function tagAndSort (app, allCerts) {
41 allCerts.sni_certs.forEach(function (cert) {
42 cert._meta = meta(app, 'sni', cert.name)
43 })
44
45 allCerts.ssl_certs.forEach(function (cert) {
46 cert._meta = meta(app, 'ssl', cert.name)
47 })
48
49 return allCerts.ssl_certs.concat(allCerts.sni_certs).sort(function (a, b) {
50 return a.name < b.name
51 })
52}
53
54function * all (app, heroku) {
55 let allCerts = yield {
56 ssl_certs: sslCertsPromise(app, heroku),
57 sni_certs: sniCertsPromise(app, heroku)
58 }
59
60 return tagAndSort(app, allCerts)
61}
62
63function * certsAndDomains (app, heroku) {
64 let requests = yield {
65 ssl_certs: sslCertsPromise(app, heroku),
66 sni_certs: sniCertsPromise(app, heroku),
67 domains: heroku.request({path: `/apps/${app}/domains`})
68 }
69
70 return {certs: tagAndSort(app, requests), domains: requests.domains}
71}
72
73function * hasAddon (app, heroku) {
74 return yield heroku.request({
75 path: `/apps/${app}/addons/ssl%3Aendpoint`
76 }).then(function () {
77 return true
78 }).catch(function (err) {
79 if (err.statusCode === 404 && err.body && err.body.id === 'not_found' && err.body.resource === 'addon') {
80 return false
81 } else {
82 throw err
83 }
84 })
85}
86
87function * hasSpace (app, heroku) {
88 return yield heroku.request({
89 path: `/apps/${app}`
90 }).then(function (data) {
91 return !!data.space
92 })
93}
94
95module.exports = {
96 hasSpace,
97 hasAddon,
98 meta,
99 all,
100 certsAndDomains
101}