-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
MacUpdater.ts
163 lines (143 loc) · 6.05 KB
/
MacUpdater.ts
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
import { AllPublishOptions, newError, safeStringifyJson } from "builder-util-runtime"
import { stat } from "fs-extra"
import { createReadStream } from "fs"
import { createServer, IncomingMessage, ServerResponse } from "http"
import { AddressInfo } from "net"
import { AppAdapter } from "./AppAdapter"
import { AppUpdater, DownloadUpdateOptions } from "./AppUpdater"
import { ResolvedUpdateFileInfo } from "./main"
import { findFile } from "./providers/Provider"
import AutoUpdater = Electron.AutoUpdater
import { execFileSync } from "child_process"
export class MacUpdater extends AppUpdater {
private readonly nativeUpdater: AutoUpdater = require("electron").autoUpdater
private squirrelDownloadedUpdate = false
constructor(options?: AllPublishOptions, app?: AppAdapter) {
super(options, app)
this.nativeUpdater.on("error", it => {
this._logger.warn(it)
this.emit("error", it)
})
this.nativeUpdater.on("update-downloaded", () => {
this.squirrelDownloadedUpdate = true
})
}
protected async doDownloadUpdate(downloadUpdateOptions: DownloadUpdateOptions): Promise<Array<string>> {
let files = downloadUpdateOptions.updateInfoAndProvider.provider.resolveFiles(downloadUpdateOptions.updateInfoAndProvider.info)
// Detect if we are running inside Rosetta emulation
const sysctlRosettaInfoKey = "sysctl.proc_translated"
let isRosetta: boolean
try {
const result = execFileSync("sysctl", [sysctlRosettaInfoKey], { encoding: "utf8" })
isRosetta = result.includes(`${sysctlRosettaInfoKey}: 1`)
} catch (e) {
this._logger.info(`sysctl shell command to check for macOS Rosetta environment failed: ${e}`)
}
// Allow arm64 macs to install universal or rosetta2(x64) - https://github.com/electron-userland/electron-builder/pull/5524
const isArm64 = (file: ResolvedUpdateFileInfo) => file.url.pathname.includes("arm64")
if (files.some(isArm64)) {
files = files.filter(file => (process.arch === "arm64" || isRosetta) === isArm64(file))
}
const zipFileInfo = findFile(files, "zip", ["pkg", "dmg"])
if (zipFileInfo == null) {
throw newError(`ZIP file not provided: ${safeStringifyJson(files)}`, "ERR_UPDATER_ZIP_FILE_NOT_FOUND")
}
const server = createServer()
server.on("close", () => {
this._logger.info(`Proxy server for native Squirrel.Mac is closed (was started to download ${zipFileInfo.url.href})`)
})
function getServerUrl(): string {
const address = server.address() as AddressInfo
return `http://127.0.0.1:${address.port}`
}
return this.executeDownload({
fileExtension: "zip",
fileInfo: zipFileInfo,
downloadUpdateOptions,
task: (destinationFile, downloadOptions) => {
return this.httpExecutor.download(zipFileInfo.url, destinationFile, downloadOptions)
},
done: async event => {
const downloadedFile = event.downloadedFile
let updateFileSize = zipFileInfo.info.size
if (updateFileSize == null) {
updateFileSize = (await stat(downloadedFile)).size
}
return await new Promise<Array<string>>((resolve, reject) => {
// insecure random is ok
const fileUrl = `/${Date.now()}-${Math.floor(Math.random() * 9999)}.zip`
server.on("request", (request: IncomingMessage, response: ServerResponse) => {
const requestUrl = request.url!
this._logger.info(`${requestUrl} requested`)
if (requestUrl === "/") {
const data = Buffer.from(`{ "url": "${getServerUrl()}${fileUrl}" }`)
response.writeHead(200, { "Content-Type": "application/json", "Content-Length": data.length })
response.end(data)
return
}
if (!requestUrl.startsWith(fileUrl)) {
this._logger.warn(`${requestUrl} requested, but not supported`)
response.writeHead(404)
response.end()
return
}
this._logger.info(`${fileUrl} requested by Squirrel.Mac, pipe ${downloadedFile}`)
let errorOccurred = false
response.on("finish", () => {
try {
setImmediate(() => server.close())
} finally {
if (!errorOccurred) {
this.nativeUpdater.removeListener("error", reject)
resolve([])
}
}
})
const readStream = createReadStream(downloadedFile)
readStream.on("error", error => {
try {
response.end()
} catch (e) {
this._logger.warn(`cannot end response: ${e}`)
}
errorOccurred = true
this.nativeUpdater.removeListener("error", reject)
reject(new Error(`Cannot pipe "${downloadedFile}": ${error}`))
})
response.writeHead(200, {
"Content-Type": "application/zip",
"Content-Length": updateFileSize,
})
readStream.pipe(response)
})
server.listen(0, "127.0.0.1", () => {
this.nativeUpdater.setFeedURL({
url: getServerUrl(),
headers: { "Cache-Control": "no-cache" },
})
// The update has been downloaded and is ready to be served to Squirrel
this.dispatchUpdateDownloaded(event)
if (this.autoInstallOnAppQuit) {
this.nativeUpdater.once("error", reject)
// This will trigger fetching and installing the file on Squirrel side
this.nativeUpdater.checkForUpdates()
} else {
resolve([])
}
})
})
},
})
}
quitAndInstall(): void {
if (this.squirrelDownloadedUpdate) {
// Update already fetched by Squirrel, it's ready to install
this.nativeUpdater.quitAndInstall()
} else {
// Quit and install as soon as Squirrel get the update
this.nativeUpdater.on("update-downloaded", () => {
this.nativeUpdater.quitAndInstall()
})
}
}
}