generated from DimitarNestorov/RepoOnVercel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
179 lines (146 loc) · 4.83 KB
/
loader.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const crypto = require('crypto')
const fs = require('fs')
const path = require('path')
const stream = require('stream')
const ar = require('ar')
const axios = require('axios')
const gunzipMaybe = require('gunzip-maybe')
const tar = require('tar-stream')
const { urlRegexp } = require('./utils')
// Create node_modules/.cache folder
const cacheFolder = path.join(__dirname, 'node_modules', '.cache')
if (!fs.existsSync(cacheFolder)) {
fs.mkdirSync(cacheFolder)
}
// Load node_modules/.cache/urlsLoader.json
const cacheVersion = 2
const cacheFile = path.join(__dirname, 'node_modules', '.cache', 'urlsLoader.json')
function getCacheFileContents() {
const emptyCache = {
version: cacheVersion,
cache: {},
}
if (fs.existsSync(cacheFile)) {
try {
const cacheFileContents = require(cacheFile)
if (cacheFileContents.version !== cacheVersion) return emptyCache
if (typeof cacheFileContents.cache !== 'object') return emptyCache
return cacheFileContents
} catch (error) {
return emptyCache
}
} else {
return emptyCache
}
}
const cache = getCacheFileContents().cache
let lastWrittenCache = JSON.stringify(cache)
const urlsToAssetId = {}
function getAssetIdForURL(url) {
const result = url.match(urlRegexp)
if (!result) throw new Error(`Bad URL: ${url}`)
const [, owner, repo, tag] = result
const options = {}
if (process.env.GITHUB_TOKEN) options.headers = { Authorization: `token ${process.env.GITHUB_TOKEN}` }
return axios
.get(`https://api.github.com/repos/${owner}/${repo}/releases/tags/${tag}`, options)
.then(({ data }) => (data.assets.filter((asset) => asset.browser_download_url === url)[0] || {}).id)
.catch(({ response }) => {
throw new Error(response.statusText)
})
}
function extractControlTarGunzipMaybe(data) {
return new Promise((resolve, reject) => {
const readableStream = new stream.Readable({
read() {
this.push(data)
this.push(null)
},
})
const extract = tar.extract()
extract.on('entry', function (header, stream, next) {
if (header.name === './control') {
const data = []
stream.on('data', (chunk) => {
data.push(chunk)
})
stream.on('end', function () {
resolve(Buffer.concat(data).toString())
})
stream.resume()
} else {
next()
}
})
extract.on('finish', function () {
reject(new Error('control file missing'))
})
readableStream.pipe(gunzipMaybe()).pipe(extract)
})
}
function convertControlToObject(control) {
const controlRegExp = /^([A-Za-z-]+): (.*)$/gm
const meta = {}
let result
while ((result = controlRegExp.exec(control))) {
meta[result[1]] = result[2]
}
delete meta.Icon
return meta
}
async function getMetaForURL(url) {
const { data } = await axios.get(url, { responseType: 'arraybuffer' })
const archive = new ar.Archive(data)
const meta = {}
for (const file of archive.getFiles()) {
const fileName = file.name()
if (fileName.startsWith('control.tar')) {
Object.assign(meta, convertControlToObject(await extractControlTarGunzipMaybe(file.fileData())))
} else if (fileName.startsWith('data.tar') || fileName === 'debian-binary') {
// Skip
} else {
console.warn('File', fileName, 'not supported; skipping')
}
}
delete meta.Depiction
delete meta.Icon
// Calculate Size [size of package]
meta.Size = data.length
// Calculate MD5sum of package
meta.MD5sum = crypto.createHash('md5').update(data).digest('hex')
meta.Filename = `api/deb/${meta.MD5sum}.deb`
// Calculate SHA1 of package
meta.SHA1 = crypto.createHash('sha1').update(data).digest('hex')
// Calculate SHA256 of package
meta.SHA256 = crypto.createHash('sha256').update(data).digest('hex')
return meta
}
module.exports = async function () {
const repo = require(this.resourcePath)
const packages = await Promise.all(
repo.packages.map(async (url) => {
const assetId = urlsToAssetId[url] || (urlsToAssetId[url] = await getAssetIdForURL(url))
if (!assetId) throw new Error(`Asset with URL ${url} not found`)
const meta = cache[assetId] || (cache[assetId] = await getMetaForURL(url))
return { meta, url }
}),
)
const currentCacheSerialized = JSON.stringify({ version: cacheVersion, cache })
if (lastWrittenCache !== currentCacheSerialized) {
fs.writeFileSync(cacheFile, currentCacheSerialized)
lastWrittenCache = currentCacheSerialized
}
const restructuredPackages = {}
const md5Table = {}
for (const p of packages) {
// package is a reserved variable name
if (!restructuredPackages[p.meta.Name]) restructuredPackages[p.meta.Name] = {}
restructuredPackages[p.meta.Name][p.meta.Version] = p
md5Table[p.meta.MD5sum] = p.url
}
return `export const packages = ${JSON.stringify(restructuredPackages)};
export const md5Table = ${JSON.stringify(md5Table)};
export const name = ${JSON.stringify(repo.name)};
export const description = ${JSON.stringify(repo.description)};
export const icons = ${JSON.stringify(repo.icons)};`
}