-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
vingester-update.js
303 lines (277 loc) · 12.2 KB
/
vingester-update.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
** Vingester ~ Ingest Web Contents as Video Streams
** Copyright (c) 2021-2022 Dr. Ralf S. Engelschall <rse@engelschall.com>
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
*/
/* external requirements */
const electron = require("electron")
const fs = require("fs")
const os = require("os")
const path = require("path")
const AdmZip = require("adm-zip")
const got = require("got")
const tmp = require("tmp")
const dayjs = require("dayjs")
const mkdirp = require("mkdirp")
const UpdateHelper = require("update-helper")
const DSIG = require("dsig")
/* internal requirements */
const pjson = require("./package.json")
/* export the API */
module.exports = class Update {
constructor (options = {}) {
/* determine options */
this.options = Object.assign({
urlDist: "https://github.oscdn.org/rse/vingester/%V/Vingester-%S-x64.zip",
urlVersion: "https://github.com/rse/vingester/raw/master/VERSION.md"
}, options)
/* determine absolute path to our own application file */
this.app = ""
if (electron.app.isPackaged) {
if (os.platform() === "win32") {
/* under Windows we are a portable app "Vingester.exe"
and Electron Builder provides us the direct path to it */
this.app = path.resolve(process.env.PORTABLE_EXECUTABLE_FILE)
}
else if (os.platform() === "darwin") {
/* under macOS we are a regular app "Vingester.app"
but we have to step up to the base directory from its
actual embedded executable "Contents/MacOS/Vingester" */
this.app = path.resolve(path.join(electron.app.getPath("exe"), "..", "..", ".."))
}
else if (os.platform() === "linux") {
/* under Linux we are an AppImage executable "Vingester"
and the AppImage stub provides us the direct path to it */
this.app = path.resolve(process.env.APPIMAGE)
}
}
/* initialize with unknown available versions */
this.versions = []
/* initialize with unknown target versions */
this.versionRunning = undefined
this.versionForthcoming = undefined
this.versionCurrent = undefined
}
/* check whether we are really updateable */
async updateable () {
if (!electron.app.isPackaged)
return false
if (this.app === "")
return false
if (!( (os.platform() === "win32" && this.app.match(/^(.+)\.exe/))
|| (os.platform() === "darwin" && this.app.match(/^(.+)\.app$/))
|| (os.platform() === "linux" && this.app.match(/^(.+)$/)) ))
return false
const accessible = await fs.promises.access(this.app, fs.constants.W_OK)
.then(() => true).catch(() => false)
if (!accessible)
return false
return true
}
/* check for available update versions */
async check (progress) {
/* determine available versions */
if (progress)
progress("downloading application version information", 0.0)
const req = got({
method: "GET",
url: this.options.urlVersion,
headers: { "User-Agent": `${pjson.name}/${pjson.version}` },
responseType: "text",
https: { rejectUnauthorized: false }
})
if (progress) {
req.on("downloadProgress", (p) => {
let completed = p.total > 0 ? p.transferred / p.total : 0
if (isNaN(completed))
completed = 0
progress("downloading application version information", completed)
})
}
const response = await req
const md = response.body
this.versions = []
md.replace(
/^\|\s+([0-9]+(?:(?:a|b|rc|\.)[0-9]+)*)\s+\|\s+(\d{4}-\d{2}-\d{2})\s+\|\s+(\S+)\s+\|\s*$/mg,
(_, version, date, type) => { this.versions.push({ version, date, type }) }
)
if (progress)
progress("downloading application version information", 1.0)
/* determine running version */
this.versionRunning = this.versions.find((v) => v.version === pjson.version)
/* determine latest current version */
this.versionCurrent = this.versions.find((v) => v.type === "current")
/* determine information about forthcoming version */
this.versionForthcoming = this.versions.find((v) => v.type === "forthcoming")
/* ensure the forthcoming version is newer or same than the current version */
if (this.versionForthcoming && this.versionCurrent) {
const d1 = dayjs(this.versionForthcoming.date)
const d2 = dayjs(this.versionCurrent.date)
if (d1.isBefore(d2))
this.versionForthcoming = undefined
}
/* return determined versions and whether we are updateable */
return {
running: this.versionRunning,
current: this.versionCurrent,
forthcoming: this.versionForthcoming
}
}
/* perform update */
async update (version, progress) {
/* sanity check situation */
const updateable = await this.updateable()
if (!updateable)
throw new Error("we are not able to update the application")
/* download application distribution ZIP archive */
let sys
if (os.platform() === "win32")
sys = "win"
else if (os.platform() === "darwin")
sys = "mac"
else if (os.platform() === "linux")
sys = "lnx"
let url = this.options.urlDist
.replace(/%V/g, version)
.replace(/%S/g, sys)
if (progress)
progress("downloading application distribution archive", 0.0)
let req = got({
method: "GET",
url,
headers: { "User-Agent": `${pjson.name}/${pjson.version}` },
responseType: "buffer",
https: { rejectUnauthorized: false }
})
if (progress) {
req.on("downloadProgress", (p) => {
let completed = p.total > 0 ? p.transferred / p.total : 0
if (isNaN(completed))
completed = 0
progress("downloading application distribution archive", completed)
})
}
let response = await req
const payload = response.body
const tmpfile = tmp.fileSync()
await fs.promises.writeFile(tmpfile.name, payload, { encoding: null })
if (progress)
progress("downloading application distribution archive", 1.0)
/* download signature */
url = url.replace(/\.zip$/, ".sig")
if (progress)
progress("downloading application distribution signature", 0.0)
req = got({
method: "GET",
url,
headers: { "User-Agent": `${pjson.name}/${pjson.version}` },
responseType: "buffer",
https: { rejectUnauthorized: false }
})
if (progress) {
req.on("downloadProgress", (p) => {
let completed = p.total > 0 ? p.transferred / p.total : 0
if (isNaN(completed))
completed = 0
progress("downloading application distribution signature", completed)
})
}
response = await req
const signature = response.body.toString()
if (progress)
progress("downloading application distribution signature", 1.0)
/* read public key and verify signature
(throws an exception if not valid) */
if (progress)
progress("verifying application distribution signature", 0.0)
const publicKey = await fs.promises.readFile(
path.join(__dirname, "npm-package.pk"), { encoding: "utf8" })
await DSIG.verify(payload, signature, publicKey)
if (progress)
progress("verifying application distribution signature", 1.0)
/* extract application distribution ZIP archive */
if (progress)
progress("extracting application distribution archive", 0.0)
const tmpdir = tmp.dirSync()
const zip = new AdmZip(tmpfile.name)
const dirCreated = {}
const entries = zip.getEntries()
const isPOSIX = os.platform() !== "win32"
for (let i = 0; i < entries.length; i++) {
const entry = entries[i]
if (progress)
progress("extracting application distribution archive", i / entries.length)
/* determine result file path on filesystem */
const filePath = path.join(tmpdir.name, entry.entryName)
/* determine directory path and automatically create missing directories */
const dirPath = entry.isDirectory ? filePath : path.dirname(filePath)
if (!dirCreated[dirPath]) {
const options = {}
if (isPOSIX)
options.mode = fs.constants.S_IRUSR | fs.constants.S_IWUSR | fs.constants.S_IXUSR |
fs.constants.S_IRGRP | fs.constants.S_IXGRP | fs.constants.S_IROTH | fs.constants.S_IXOTH
await mkdirp(dirPath, options)
dirCreated[dirPath] = true
}
/* create resulting entry */
if (((entry.attr >> 28) & 0x0F) === 10) {
/* case 1: symbolic link */
const target = zip.readFile(entry).toString()
await fs.promises.symlink(target, filePath, "file")
if (isPOSIX)
await fs.promises.lchmod(filePath, fs.constants.S_IRUSR | fs.constants.S_IWUSR |
fs.constants.S_IRGRP | fs.constants.S_IROTH)
}
else if (!entry.isDirectory) {
/* case 2: regular file */
const data = zip.readFile(entry)
const options = { encoding: null }
if (isPOSIX) {
options.mode = fs.constants.S_IRUSR | fs.constants.S_IWUSR |
fs.constants.S_IRGRP | fs.constants.S_IROTH
if ((entry.attr >> 16) & fs.constants.S_IXUSR)
options.mode |= fs.constants.S_IXUSR | fs.constants.S_IXGRP | fs.constants.S_IXOTH
}
await fs.promises.writeFile(filePath, data, options)
}
}
if (progress)
progress("extracting application distribution archive", 1.0)
tmpfile.removeCallback()
/* start background process to update application executable */
if (progress)
progress("updating application executable", 0.0)
/* final sanity check */
let from
if (os.platform() === "win32")
from = path.join(tmpdir.name, "Vingester.exe")
else if (os.platform() === "darwin")
from = path.join(tmpdir.name, "Vingester.app")
else if (os.platform() === "linux")
from = path.join(tmpdir.name, "Vingester")
const accessible = await fs.promises.access(from, fs.constants.F_OK | fs.constants.R_OK)
.then(() => true).catch(() => false)
if (!accessible)
throw new Error("cannot find application executable in distribution content")
/* kill/replace/restart ourself */
const updateHelper = new UpdateHelper({
kill: process.pid,
wait: 1000,
rename: true,
source: from,
target: this.app,
[os.platform() === "darwin" ? "open" : "execute"]: this.app,
cleanup: [ tmpdir.name ],
progress: (step, percent) => {
return progress(step, percent)
}
})
await updateHelper.update()
}
/* perform cleanup */
async cleanup () {
/* remove old update-helper after update */
const updateHelper = new UpdateHelper()
await updateHelper.cleanup()
}
}