-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (100 loc) · 3.3 KB
/
index.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
const compareVersions = require('legal-versioning-compare')
const concat = require('simple-concat')
const https = require('https')
const isObject = require('is-object')
const isUpgrade = require('legal-versioning-upgrade')
const once = require('once')
const parse = require('json-parse-errback')
const predicate = require('commonform-predicate')
const runParallelLimit = require('run-parallel-limit')
module.exports = (form, { cache, parallel = 1 }, callback) => {
runParallelLimit(
findComponents(form).map(found => done => {
const {
path,
reference: { component: url, version: currentVersion }
} = found
if (cache && cache.get) {
cache.get(url, (error, versions) => {
if (error || !versions) downloadAndCache()
withVersions(null, versions)
})
} else downloadAndCache()
function downloadAndCache () {
downloadVersions(url, (error, versions) => {
if (error) return withVersions(error)
if (!versions) return withVersions(null, false)
if (cache && cache.put) cache.put(url, versions, () => finish())
else finish()
function finish (error) {
withVersions(error, versions)
}
})
}
function withVersions (error, versions) {
if (error) return callback(error)
if (!versions) return callback(new Error(`No versions for ${url}`))
// The versions array may contain invalid versions.
try {
versions.sort(compareVersions).reverse()
} catch (error) {
return callback(error)
}
const latest = versions.find(candidate => isUpgrade(currentVersion, candidate))
return done(null, {
path,
reference: found.reference,
version: currentVersion,
available: versions.reverse(),
latest
})
}
}),
parallel,
callback
)
}
function findComponents (form) {
const found = []
recurse([], form)
return found
function recurse (path, form) {
for (const [index, element] of form.content.entries()) {
if (predicate.child(element)) {
recurse(path.concat('content', index, 'form'), element.form)
} else if (predicate.component(element)) {
found.push({
reference: element,
path: path.concat('content', index)
})
}
}
}
}
function downloadVersions (component, callback) {
callback = once(callback)
const url = component + '.json'
https.request(url)
.once('error', callback)
.once('timeout', callback)
.once('response', function (response) {
const statusCode = response.statusCode
if (statusCode === 404) return callback(null, false)
if (statusCode !== 200) {
const statusError = new Error(`${url} responded ${statusCode}`)
statusError.statusCode = statusCode
return callback(statusError)
}
concat(response, function (error, buffer) {
if (error) return callback(error)
parse(buffer, function (error, parsed) {
if (error) return callback(error)
if (!isObject(parsed)) return callback(null, false)
const { versions } = parsed
if (!Array.isArray(versions)) return callback(null, false)
callback(null, versions)
})
})
})
.end()
}