pinggy 0.6.9 → 0.6.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +21 -8
- package/CLAUDE.md +7 -3
- package/README.md +12 -0
- package/dist/{chunk-MHQ3CVEB.js → chunk-P54K7NPF.js} +5 -1
- package/dist/chunk-RYO5T2JT.js +1947 -0
- package/dist/{chunk-LF6WMFWT.js → chunk-W47AYOH2.js} +6800 -2025
- package/dist/{daemonChild-WTZ7X6SZ.js → daemonChild-GGG34AOT.js} +7 -5
- package/dist/index.cjs +3234 -432
- package/dist/index.d.cts +75 -2
- package/dist/index.d.ts +75 -2
- package/dist/index.js +21 -5
- package/dist/{ipcClient-IZ5R6TPN.js → ipcClient-6QESURCH.js} +1 -1
- package/dist/{main-INPCUGAN.js → main-FCIOVX5I.js} +5 -8
- package/dist/{updateChild-BPWBQ3WR.js → updateChild-RNCLN24C.js} +12 -14
- package/package.json +1 -1
- package/scripts/install/install.ps1 +3 -2
- package/scripts/install/install.sh +13 -7
- package/dist/chunk-HJECJ7LC.js +0 -889
- package/dist/chunk-OQVFQ5VG.js +0 -1041
- package/dist/chunk-Q5RYSWFK.js +0 -2204
package/dist/chunk-HJECJ7LC.js
DELETED
|
@@ -1,889 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
InstallKind,
|
|
3
|
-
getDaemonInfo,
|
|
4
|
-
isProcessAlive,
|
|
5
|
-
moveDeclinedInstallToVersion,
|
|
6
|
-
readServiceEntryText,
|
|
7
|
-
readUpdateState,
|
|
8
|
-
unixLayoutPaths,
|
|
9
|
-
writeUpdateState
|
|
10
|
-
} from "./chunk-OQVFQ5VG.js";
|
|
11
|
-
import {
|
|
12
|
-
errorMessage
|
|
13
|
-
} from "./chunk-HTIWY3O5.js";
|
|
14
|
-
import {
|
|
15
|
-
logger
|
|
16
|
-
} from "./chunk-DGAH7UVX.js";
|
|
17
|
-
import {
|
|
18
|
-
ensurePinggyConfigDir,
|
|
19
|
-
getUpdateLockPath
|
|
20
|
-
} from "./chunk-WFHMERT3.js";
|
|
21
|
-
|
|
22
|
-
// src/update/latestRelease.ts
|
|
23
|
-
import http from "http";
|
|
24
|
-
import https from "https";
|
|
25
|
-
var DEFAULT_RELEASES_URL = "https://github.com/Pinggy-io/cli-js/releases";
|
|
26
|
-
var RELEASES_URL_OVERRIDE_ENV = "PINGGY_UPDATE_RELEASES_URL";
|
|
27
|
-
var LOOPBACK_HOSTS = /* @__PURE__ */ new Set(["127.0.0.1", "localhost"]);
|
|
28
|
-
var TAG_PATTERN = /^v\d+\.\d+\.\d+$/;
|
|
29
|
-
var VERSION_PATTERN = /^(\d+)\.(\d+)\.(\d+)$/;
|
|
30
|
-
var USER_AGENT = "pinggy-cli";
|
|
31
|
-
var NPM_REGISTRY_LATEST_URL = "https://registry.npmjs.org/pinggy/latest";
|
|
32
|
-
var SHA256SUMS_FILE = "SHA256SUMS";
|
|
33
|
-
function loopbackReleasesOverride() {
|
|
34
|
-
const value = process.env[RELEASES_URL_OVERRIDE_ENV];
|
|
35
|
-
if (!value) return null;
|
|
36
|
-
try {
|
|
37
|
-
const url = new URL(value);
|
|
38
|
-
if (url.protocol === "http:" && LOOPBACK_HOSTS.has(url.hostname)) return value.replace(/\/+$/, "");
|
|
39
|
-
} catch {
|
|
40
|
-
}
|
|
41
|
-
logger.debug(`Ignoring ${RELEASES_URL_OVERRIDE_ENV}. It accepts a loopback http URL only`);
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
function getReleasesUrl() {
|
|
45
|
-
return loopbackReleasesOverride() ?? DEFAULT_RELEASES_URL;
|
|
46
|
-
}
|
|
47
|
-
function isAllowedRequestUrl(url) {
|
|
48
|
-
if (url.protocol === "https:") return true;
|
|
49
|
-
return url.protocol === "http:" && LOOPBACK_HOSTS.has(url.hostname) && loopbackReleasesOverride() !== null;
|
|
50
|
-
}
|
|
51
|
-
function requestModuleFor(url) {
|
|
52
|
-
if (!isAllowedRequestUrl(url)) throw new Error(`Refused a request to ${url.protocol}//${url.host}`);
|
|
53
|
-
return url.protocol === "https:" ? https : http;
|
|
54
|
-
}
|
|
55
|
-
function headWithoutRedirect(target, timeoutMillis) {
|
|
56
|
-
return new Promise((resolve, reject) => {
|
|
57
|
-
const url = new URL(target);
|
|
58
|
-
const request = requestModuleFor(url).request(
|
|
59
|
-
url,
|
|
60
|
-
{ method: "HEAD", headers: { "User-Agent": USER_AGENT } },
|
|
61
|
-
(response) => {
|
|
62
|
-
clearTimeout(timer);
|
|
63
|
-
response.resume();
|
|
64
|
-
resolve({ status: response.statusCode ?? 0, location: response.headers.location });
|
|
65
|
-
}
|
|
66
|
-
);
|
|
67
|
-
const timer = setTimeout(
|
|
68
|
-
() => request.destroy(new Error(`No answer within ${timeoutMillis} ms`)),
|
|
69
|
-
timeoutMillis
|
|
70
|
-
);
|
|
71
|
-
request.on("error", (error) => {
|
|
72
|
-
clearTimeout(timer);
|
|
73
|
-
reject(error);
|
|
74
|
-
});
|
|
75
|
-
request.end();
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
async function resolveLatestTag({ timeoutMillis }) {
|
|
79
|
-
const { status, location } = await headWithoutRedirect(`${getReleasesUrl()}/latest`, timeoutMillis);
|
|
80
|
-
const tag = location?.split("?")[0].split("/").filter(Boolean).pop();
|
|
81
|
-
if (status < 300 || status >= 400 || !tag || !TAG_PATTERN.test(tag)) {
|
|
82
|
-
throw new Error(`Unexpected answer for the latest release: status ${status}`);
|
|
83
|
-
}
|
|
84
|
-
return tag;
|
|
85
|
-
}
|
|
86
|
-
async function isReleaseComplete(tag, { timeoutMillis }) {
|
|
87
|
-
const { status } = await headWithoutRedirect(`${getReleasesUrl()}/download/${tag}/${SHA256SUMS_FILE}`, timeoutMillis);
|
|
88
|
-
if (status >= 200 && status < 400) return true;
|
|
89
|
-
if (status === 404) return false;
|
|
90
|
-
throw new Error(`Unexpected answer for ${SHA256SUMS_FILE}: status ${status}`);
|
|
91
|
-
}
|
|
92
|
-
function getJson(target, timeoutMillis) {
|
|
93
|
-
return new Promise((resolve, reject) => {
|
|
94
|
-
const url = new URL(target);
|
|
95
|
-
const request = requestModuleFor(url).get(url, { headers: { "User-Agent": USER_AGENT } }, (response) => {
|
|
96
|
-
const chunks = [];
|
|
97
|
-
response.on("data", (chunk) => chunks.push(chunk));
|
|
98
|
-
response.on("error", reject);
|
|
99
|
-
response.on("end", () => {
|
|
100
|
-
clearTimeout(timer);
|
|
101
|
-
try {
|
|
102
|
-
resolve(JSON.parse(Buffer.concat(chunks).toString("utf-8")));
|
|
103
|
-
} catch (error) {
|
|
104
|
-
reject(error instanceof Error ? error : new Error(String(error)));
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
const timer = setTimeout(
|
|
109
|
-
() => request.destroy(new Error(`No answer within ${timeoutMillis} ms`)),
|
|
110
|
-
timeoutMillis
|
|
111
|
-
);
|
|
112
|
-
request.on("error", (error) => {
|
|
113
|
-
clearTimeout(timer);
|
|
114
|
-
reject(error);
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
async function resolveLatestNpmVersion({ timeoutMillis }, registryUrl = NPM_REGISTRY_LATEST_URL) {
|
|
119
|
-
const version = (await getJson(registryUrl, timeoutMillis))?.version;
|
|
120
|
-
if (typeof version !== "string" || parseVersion(version) === null) {
|
|
121
|
-
throw new Error("Unexpected answer from the npm registry");
|
|
122
|
-
}
|
|
123
|
-
return version;
|
|
124
|
-
}
|
|
125
|
-
function versionFromTag(tag) {
|
|
126
|
-
return tag.replace(/^v/, "");
|
|
127
|
-
}
|
|
128
|
-
function parseVersion(text) {
|
|
129
|
-
const match = VERSION_PATTERN.exec(text);
|
|
130
|
-
return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : null;
|
|
131
|
-
}
|
|
132
|
-
function isNewerVersion(candidate, current) {
|
|
133
|
-
const candidateParts = parseVersion(candidate);
|
|
134
|
-
const currentParts = parseVersion(current);
|
|
135
|
-
if (!candidateParts || !currentParts) return false;
|
|
136
|
-
for (let index = 0; index < candidateParts.length; index++) {
|
|
137
|
-
if (candidateParts[index] !== currentParts[index]) return candidateParts[index] > currentParts[index];
|
|
138
|
-
}
|
|
139
|
-
return false;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// src/update/binaryUpdater.ts
|
|
143
|
-
import crypto from "crypto";
|
|
144
|
-
import { execFile } from "child_process";
|
|
145
|
-
import fs from "fs";
|
|
146
|
-
import path from "path";
|
|
147
|
-
var UpdateErrorCode = {
|
|
148
|
-
NotReady: "not-ready",
|
|
149
|
-
Network: "network",
|
|
150
|
-
NoDiskSpace: "no-disk-space",
|
|
151
|
-
Incomplete: "incomplete",
|
|
152
|
-
Checksum: "checksum",
|
|
153
|
-
BadBinary: "bad-binary",
|
|
154
|
-
RenameFailed: "rename-failed",
|
|
155
|
-
PostSwapFailed: "post-swap-failed",
|
|
156
|
-
RestoreFailed: "restore-failed",
|
|
157
|
-
NothingToRollBack: "nothing-to-roll-back"
|
|
158
|
-
};
|
|
159
|
-
var BAD_RELEASE_CODES = /* @__PURE__ */ new Set([
|
|
160
|
-
UpdateErrorCode.Incomplete,
|
|
161
|
-
UpdateErrorCode.Checksum,
|
|
162
|
-
UpdateErrorCode.BadBinary,
|
|
163
|
-
UpdateErrorCode.PostSwapFailed
|
|
164
|
-
]);
|
|
165
|
-
var UpdateError = class extends Error {
|
|
166
|
-
constructor(code, message, options = {}) {
|
|
167
|
-
super(message);
|
|
168
|
-
this.name = "UpdateError";
|
|
169
|
-
this.code = code;
|
|
170
|
-
this.countsAsBadRelease = options.countsAsBadRelease ?? BAD_RELEASE_CODES.has(code);
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
var BINARY_RUN_TIMEOUT_MILLIS = 3e4;
|
|
174
|
-
var DEVICE_TOKEN_ENV = "DEVICE_AGENT_TOKEN";
|
|
175
|
-
var VERSION_LINE = /^Pinggy CLI version: (\d+\.\d+\.\d+)\s*$/m;
|
|
176
|
-
function environmentWithoutDeviceToken() {
|
|
177
|
-
const environment = { ...process.env };
|
|
178
|
-
delete environment[DEVICE_TOKEN_ENV];
|
|
179
|
-
return environment;
|
|
180
|
-
}
|
|
181
|
-
function pinggyBinaryRunOptions() {
|
|
182
|
-
return {
|
|
183
|
-
timeout: BINARY_RUN_TIMEOUT_MILLIS,
|
|
184
|
-
windowsHide: true,
|
|
185
|
-
env: { ...environmentWithoutDeviceToken(), PKG_EXECPATH: "" }
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
var runPinggyBinary = (binaryPath, args) => new Promise((resolve, reject) => {
|
|
189
|
-
execFile(binaryPath, args, pinggyBinaryRunOptions(), (error, stdout) => {
|
|
190
|
-
if (error) reject(error);
|
|
191
|
-
else resolve(String(stdout));
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
async function readBinaryVersion(binaryPath, runBinary) {
|
|
195
|
-
const match = VERSION_LINE.exec(await runBinary(binaryPath, ["--version"]));
|
|
196
|
-
if (!match) throw new Error(`${binaryPath} printed no version`);
|
|
197
|
-
return match[1];
|
|
198
|
-
}
|
|
199
|
-
async function readBinaryIpcVersion(binaryPath, runBinary) {
|
|
200
|
-
const version = Number((await runBinary(binaryPath, ["--_ipc-version"])).trim());
|
|
201
|
-
if (!Number.isInteger(version)) throw new UpdateError(UpdateErrorCode.BadBinary, "The new binary reported no IPC version.");
|
|
202
|
-
return version;
|
|
203
|
-
}
|
|
204
|
-
var ASSET_NAMES = {
|
|
205
|
-
"linux-x64": "pinggy-linux-x64",
|
|
206
|
-
"linux-arm64": "pinggy-linux-arm64",
|
|
207
|
-
"darwin-x64": "pinggy-macos-x64",
|
|
208
|
-
"darwin-arm64": "pinggy-macos-arm64",
|
|
209
|
-
"win32-x64": "pinggy-win-x64.exe",
|
|
210
|
-
"win32-arm64": "pinggy-win-arm64.exe"
|
|
211
|
-
};
|
|
212
|
-
function assetNameFor(platform, architecture) {
|
|
213
|
-
const name = ASSET_NAMES[`${platform}-${architecture}`];
|
|
214
|
-
if (!name) throw new Error(`No release asset for ${platform}-${architecture}`);
|
|
215
|
-
return name;
|
|
216
|
-
}
|
|
217
|
-
var MAX_REDIRECTS = 5;
|
|
218
|
-
var IDLE_TIMEOUT_MILLIS = 3e4;
|
|
219
|
-
var FREE_SPACE_MARGIN_BYTES = 50 * 1024 * 1024;
|
|
220
|
-
var WINDOWS_FREE_SPACE_FACTOR = 2;
|
|
221
|
-
var UNIX_FREE_SPACE_FACTOR = 1;
|
|
222
|
-
var EXECUTABLE_MODE = 493;
|
|
223
|
-
var USER_AGENT2 = "pinggy-cli";
|
|
224
|
-
var SUMS_LINE = /^([0-9a-f]{64}) {2}(.+)$/;
|
|
225
|
-
function realFreeBytes(directory) {
|
|
226
|
-
try {
|
|
227
|
-
const stats = fs.statfsSync(directory);
|
|
228
|
-
return Number(stats.bavail) * Number(stats.bsize);
|
|
229
|
-
} catch {
|
|
230
|
-
return null;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
var defaultDownloadDependencies = {
|
|
234
|
-
runBinary: runPinggyBinary,
|
|
235
|
-
freeBytes: realFreeBytes,
|
|
236
|
-
idleTimeoutMillis: IDLE_TIMEOUT_MILLIS
|
|
237
|
-
};
|
|
238
|
-
function getFollowingRedirects(target, idleTimeoutMillis, hopsLeft = MAX_REDIRECTS) {
|
|
239
|
-
return new Promise((resolve, reject) => {
|
|
240
|
-
const url = new URL(target);
|
|
241
|
-
const request = requestModuleFor(url).get(url, { headers: { "User-Agent": USER_AGENT2 } }, (response) => {
|
|
242
|
-
const status = response.statusCode ?? 0;
|
|
243
|
-
const location = response.headers.location;
|
|
244
|
-
if (status < 300 || status >= 400 || !location) {
|
|
245
|
-
resolve(response);
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
response.resume();
|
|
249
|
-
if (hopsLeft === 0) {
|
|
250
|
-
reject(new Error("too many redirects"));
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
getFollowingRedirects(new URL(location, url).href, idleTimeoutMillis, hopsLeft - 1).then(resolve, reject);
|
|
254
|
-
});
|
|
255
|
-
request.setTimeout(idleTimeoutMillis, () => request.destroy(new Error("no data arrived in time")));
|
|
256
|
-
request.on("error", reject);
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
function unchanged(running) {
|
|
260
|
-
return `pinggy ${running} is unchanged.`;
|
|
261
|
-
}
|
|
262
|
-
function networkError(reason, running) {
|
|
263
|
-
return new UpdateError(UpdateErrorCode.Network, `The download failed: ${reason}. ${unchanged(running)}`);
|
|
264
|
-
}
|
|
265
|
-
function notReady(tag) {
|
|
266
|
-
return new UpdateError(UpdateErrorCode.NotReady, `Release ${tag} is not ready. Try again later.`);
|
|
267
|
-
}
|
|
268
|
-
async function open(url, running, idleTimeoutMillis) {
|
|
269
|
-
try {
|
|
270
|
-
return await getFollowingRedirects(url, idleTimeoutMillis);
|
|
271
|
-
} catch (error) {
|
|
272
|
-
throw networkError(errorMessage(error), running);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
function readBody(response) {
|
|
276
|
-
return new Promise((resolve, reject) => {
|
|
277
|
-
const chunks = [];
|
|
278
|
-
response.on("data", (chunk) => chunks.push(chunk));
|
|
279
|
-
response.on("end", () => resolve(Buffer.concat(chunks)));
|
|
280
|
-
response.on("error", reject);
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
function hashForAsset(sums, assetName) {
|
|
284
|
-
for (const line of sums.split("\n")) {
|
|
285
|
-
const match = SUMS_LINE.exec(line.trim());
|
|
286
|
-
if (match && match[2] === assetName) return match[1];
|
|
287
|
-
}
|
|
288
|
-
return null;
|
|
289
|
-
}
|
|
290
|
-
async function readExpectedHash(request, releaseUrl, dependencies) {
|
|
291
|
-
const { tag, assetName, runningVersion } = request;
|
|
292
|
-
const response = await open(`${releaseUrl}/${SHA256SUMS_FILE}`, runningVersion, dependencies.idleTimeoutMillis);
|
|
293
|
-
if (response.statusCode === 404) {
|
|
294
|
-
response.resume();
|
|
295
|
-
throw notReady(tag);
|
|
296
|
-
}
|
|
297
|
-
if (response.statusCode !== 200) {
|
|
298
|
-
response.resume();
|
|
299
|
-
throw networkError(`status ${response.statusCode}`, runningVersion);
|
|
300
|
-
}
|
|
301
|
-
let sumsBytes;
|
|
302
|
-
try {
|
|
303
|
-
sumsBytes = await readBody(response);
|
|
304
|
-
} catch (error) {
|
|
305
|
-
throw networkError(errorMessage(error), runningVersion);
|
|
306
|
-
}
|
|
307
|
-
const expectedHash = hashForAsset(sumsBytes.toString("utf-8"), assetName);
|
|
308
|
-
if (!expectedHash) throw notReady(tag);
|
|
309
|
-
return expectedHash;
|
|
310
|
-
}
|
|
311
|
-
function writeFully(fileDescriptor, chunk) {
|
|
312
|
-
let written = 0;
|
|
313
|
-
while (written < chunk.length) {
|
|
314
|
-
written += fs.writeSync(fileDescriptor, chunk, written);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
function receiveBody(response, fileDescriptor) {
|
|
318
|
-
return new Promise((resolve, reject) => {
|
|
319
|
-
const hash = crypto.createHash("sha256");
|
|
320
|
-
let byteCount = 0;
|
|
321
|
-
response.on("data", (chunk) => {
|
|
322
|
-
try {
|
|
323
|
-
writeFully(fileDescriptor, chunk);
|
|
324
|
-
} catch (error) {
|
|
325
|
-
response.destroy();
|
|
326
|
-
reject(error);
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
hash.update(chunk);
|
|
330
|
-
byteCount += chunk.length;
|
|
331
|
-
});
|
|
332
|
-
response.on("end", () => resolve({ byteCount, sha256: hash.digest("hex") }));
|
|
333
|
-
response.on("error", reject);
|
|
334
|
-
response.on("close", () => reject(new Error("the connection closed early")));
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
function noDiskSpace(running) {
|
|
338
|
-
return new UpdateError(UpdateErrorCode.NoDiskSpace, `Not enough free disk space for the update. ${unchanged(running)}`);
|
|
339
|
-
}
|
|
340
|
-
function ensureFreeSpace(request, contentLength, dependencies) {
|
|
341
|
-
const factor = request.assetName.endsWith(".exe") ? WINDOWS_FREE_SPACE_FACTOR : UNIX_FREE_SPACE_FACTOR;
|
|
342
|
-
const freeBytes = dependencies.freeBytes(path.dirname(request.tempPath));
|
|
343
|
-
if (freeBytes !== null && freeBytes < contentLength * factor + FREE_SPACE_MARGIN_BYTES) {
|
|
344
|
-
throw noDiskSpace(request.runningVersion);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
function bodyFailure(error, running) {
|
|
348
|
-
if (error.code === "ENOSPC") return noDiskSpace(running);
|
|
349
|
-
return new UpdateError(
|
|
350
|
-
UpdateErrorCode.Incomplete,
|
|
351
|
-
`The download was incomplete. ${unchanged(running)}`,
|
|
352
|
-
{ countsAsBadRelease: false }
|
|
353
|
-
);
|
|
354
|
-
}
|
|
355
|
-
async function receiveVerifiedFile(response, request, expectedHash, contentLength) {
|
|
356
|
-
const { tempPath, runningVersion } = request;
|
|
357
|
-
const fileDescriptor = fs.openSync(tempPath, "w", EXECUTABLE_MODE);
|
|
358
|
-
try {
|
|
359
|
-
const body = await receiveBody(response, fileDescriptor).catch((error) => {
|
|
360
|
-
throw bodyFailure(error, runningVersion);
|
|
361
|
-
});
|
|
362
|
-
if (body.byteCount !== contentLength) {
|
|
363
|
-
throw new UpdateError(UpdateErrorCode.Incomplete, `The download was incomplete. ${unchanged(runningVersion)}`);
|
|
364
|
-
}
|
|
365
|
-
if (body.sha256 !== expectedHash) {
|
|
366
|
-
throw new UpdateError(UpdateErrorCode.Checksum, `The download failed its checksum. ${unchanged(runningVersion)}`);
|
|
367
|
-
}
|
|
368
|
-
fs.fsyncSync(fileDescriptor);
|
|
369
|
-
if (process.platform !== "win32") fs.fchmodSync(fileDescriptor, EXECUTABLE_MODE);
|
|
370
|
-
} finally {
|
|
371
|
-
fs.closeSync(fileDescriptor);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
async function downloadAsset(request, releaseUrl, expectedHash, dependencies) {
|
|
375
|
-
const { assetName, runningVersion } = request;
|
|
376
|
-
const response = await open(`${releaseUrl}/${assetName}`, runningVersion, dependencies.idleTimeoutMillis);
|
|
377
|
-
if (response.statusCode !== 200) {
|
|
378
|
-
response.resume();
|
|
379
|
-
throw networkError(`status ${response.statusCode}`, runningVersion);
|
|
380
|
-
}
|
|
381
|
-
const contentLength = Number(response.headers["content-length"]);
|
|
382
|
-
if (!Number.isInteger(contentLength)) {
|
|
383
|
-
response.destroy();
|
|
384
|
-
throw new UpdateError(UpdateErrorCode.Incomplete, `The download was incomplete. ${unchanged(runningVersion)}`);
|
|
385
|
-
}
|
|
386
|
-
try {
|
|
387
|
-
ensureFreeSpace(request, contentLength, dependencies);
|
|
388
|
-
} catch (error) {
|
|
389
|
-
response.destroy();
|
|
390
|
-
throw error;
|
|
391
|
-
}
|
|
392
|
-
await receiveVerifiedFile(response, request, expectedHash, contentLength);
|
|
393
|
-
}
|
|
394
|
-
async function ensureBinaryRuns(request, dependencies) {
|
|
395
|
-
const expected = versionFromTag(request.tag);
|
|
396
|
-
const reported = await readBinaryVersion(request.tempPath, dependencies.runBinary).catch(() => null);
|
|
397
|
-
if (reported !== expected) {
|
|
398
|
-
throw new UpdateError(
|
|
399
|
-
UpdateErrorCode.BadBinary,
|
|
400
|
-
`The new binary did not run correctly. ${unchanged(request.runningVersion)}`
|
|
401
|
-
);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
function removeQuietly(filePath) {
|
|
405
|
-
fs.rmSync(filePath, { force: true });
|
|
406
|
-
}
|
|
407
|
-
async function downloadAndVerify(request, dependencies = defaultDownloadDependencies) {
|
|
408
|
-
const releaseUrl = `${getReleasesUrl()}/download/${request.tag}`;
|
|
409
|
-
try {
|
|
410
|
-
const expectedHash = await readExpectedHash(request, releaseUrl, dependencies);
|
|
411
|
-
await downloadAsset(request, releaseUrl, expectedHash, dependencies);
|
|
412
|
-
await ensureBinaryRuns(request, dependencies);
|
|
413
|
-
} catch (error) {
|
|
414
|
-
removeQuietly(request.tempPath);
|
|
415
|
-
throw error;
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
// src/update/windowsSwap.ts
|
|
420
|
-
import fs2 from "fs";
|
|
421
|
-
import path2 from "path";
|
|
422
|
-
var WINDOWS_PATH = path2.win32;
|
|
423
|
-
var BACKUP_MARKER = ".prev";
|
|
424
|
-
var NEW_FILE_SUFFIX = ".new.exe";
|
|
425
|
-
var OLD_FILE_SUFFIX = ".old";
|
|
426
|
-
var RETRYABLE_RENAME_CODES = /* @__PURE__ */ new Set(["EPERM", "EBUSY", "EACCES"]);
|
|
427
|
-
var RENAME_RETRY_INTERVAL_MILLIS = 250;
|
|
428
|
-
var RENAME_RETRY_LIMIT_MILLIS = 1e4;
|
|
429
|
-
var POST_SWAP_WAIT_MILLIS = 2e3;
|
|
430
|
-
var INSTALL_COMMAND = "irm https://pinggy.io/install.ps1 | iex";
|
|
431
|
-
var realWindowsFileOperations = {
|
|
432
|
-
exists: (filePath) => fs2.existsSync(filePath),
|
|
433
|
-
copyFile: (from, to) => fs2.copyFileSync(from, to),
|
|
434
|
-
rename: (from, to) => fs2.renameSync(from, to),
|
|
435
|
-
// Synchronous, so no other JavaScript runs between the 2 renames of a swap.
|
|
436
|
-
sleepMillis: (millis) => {
|
|
437
|
-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, millis);
|
|
438
|
-
},
|
|
439
|
-
nowMillis: () => Date.now()
|
|
440
|
-
};
|
|
441
|
-
function windowsInstallPaths(execPath) {
|
|
442
|
-
const directory = WINDOWS_PATH.dirname(execPath);
|
|
443
|
-
const { name, ext, base } = WINDOWS_PATH.parse(execPath);
|
|
444
|
-
if (name.endsWith(BACKUP_MARKER)) {
|
|
445
|
-
const liveName = `${name.slice(0, -BACKUP_MARKER.length)}${ext}`;
|
|
446
|
-
return { directory, liveName, livePath: WINDOWS_PATH.join(directory, liveName), backupPath: execPath, runningFromBackup: true };
|
|
447
|
-
}
|
|
448
|
-
const backupName = `${name}${BACKUP_MARKER}${ext}`;
|
|
449
|
-
return { directory, liveName: base, livePath: execPath, backupPath: WINDOWS_PATH.join(directory, backupName), runningFromBackup: false };
|
|
450
|
-
}
|
|
451
|
-
function windowsDownloadPath(execPath) {
|
|
452
|
-
return `${execPath}.${process.pid}${NEW_FILE_SUFFIX}`;
|
|
453
|
-
}
|
|
454
|
-
function renameWithRetry(operations, from, to) {
|
|
455
|
-
const attempts = Math.ceil(RENAME_RETRY_LIMIT_MILLIS / RENAME_RETRY_INTERVAL_MILLIS);
|
|
456
|
-
for (let attempt = 0; ; attempt++) {
|
|
457
|
-
try {
|
|
458
|
-
operations.rename(from, to);
|
|
459
|
-
return;
|
|
460
|
-
} catch (error) {
|
|
461
|
-
const retryable = RETRYABLE_RENAME_CODES.has(error.code ?? "");
|
|
462
|
-
if (!retryable || attempt >= attempts) throw error;
|
|
463
|
-
operations.sleepMillis(RENAME_RETRY_INTERVAL_MILLIS);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
function restoreLiveFile(operations, paths, oldPath) {
|
|
468
|
-
try {
|
|
469
|
-
renameWithRetry(operations, oldPath, paths.livePath);
|
|
470
|
-
return;
|
|
471
|
-
} catch {
|
|
472
|
-
}
|
|
473
|
-
try {
|
|
474
|
-
operations.copyFile(paths.backupPath, paths.livePath);
|
|
475
|
-
} catch {
|
|
476
|
-
throw new UpdateError(
|
|
477
|
-
UpdateErrorCode.RestoreFailed,
|
|
478
|
-
`Could not restore ${paths.livePath}. The old binary is at ${oldPath}. Run: ${INSTALL_COMMAND}`
|
|
479
|
-
);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
function renameFailure(livePath, error, running) {
|
|
483
|
-
return new UpdateError(
|
|
484
|
-
UpdateErrorCode.RenameFailed,
|
|
485
|
-
`Could not replace ${livePath}: ${errorMessage(error)}. pinggy ${running} is unchanged.`
|
|
486
|
-
);
|
|
487
|
-
}
|
|
488
|
-
function oldFilePath(livePath, operations) {
|
|
489
|
-
return `${livePath}.${operations.nowMillis()}${OLD_FILE_SUFFIX}`;
|
|
490
|
-
}
|
|
491
|
-
async function swapWindowsBinary(request, operations, runBinary) {
|
|
492
|
-
const { downloadPath, newVersion, runningVersion } = request;
|
|
493
|
-
const paths = windowsInstallPaths(request.execPath);
|
|
494
|
-
const oldPath = oldFilePath(paths.livePath, operations);
|
|
495
|
-
try {
|
|
496
|
-
operations.copyFile(paths.livePath, paths.backupPath);
|
|
497
|
-
renameWithRetry(operations, paths.livePath, oldPath);
|
|
498
|
-
} catch (error) {
|
|
499
|
-
throw renameFailure(paths.livePath, error, runningVersion);
|
|
500
|
-
}
|
|
501
|
-
try {
|
|
502
|
-
renameWithRetry(operations, downloadPath, paths.livePath);
|
|
503
|
-
} catch (error) {
|
|
504
|
-
restoreLiveFile(operations, paths, oldPath);
|
|
505
|
-
throw renameFailure(paths.livePath, error, runningVersion);
|
|
506
|
-
}
|
|
507
|
-
operations.sleepMillis(POST_SWAP_WAIT_MILLIS);
|
|
508
|
-
const reported = await readBinaryVersion(paths.livePath, runBinary).catch(() => null);
|
|
509
|
-
if (reported !== newVersion) {
|
|
510
|
-
restoreLiveFile(operations, paths, oldPath);
|
|
511
|
-
throw new UpdateError(
|
|
512
|
-
UpdateErrorCode.PostSwapFailed,
|
|
513
|
-
`The new binary did not run after the update. pinggy ${runningVersion} is restored.`
|
|
514
|
-
);
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
async function versionBeingLeft(request, paths, runBinary) {
|
|
518
|
-
if (!paths.runningFromBackup) return request.runningVersion;
|
|
519
|
-
const reported = await readBinaryVersion(paths.livePath, runBinary).catch(() => null);
|
|
520
|
-
return reported ?? request.latestVersion ?? "the newer version";
|
|
521
|
-
}
|
|
522
|
-
function nothingToRollBack() {
|
|
523
|
-
return new UpdateError(UpdateErrorCode.NothingToRollBack, "No previous version to go back to.");
|
|
524
|
-
}
|
|
525
|
-
async function rollbackWindowsBinary(request, operations, runBinary) {
|
|
526
|
-
const paths = windowsInstallPaths(request.execPath);
|
|
527
|
-
if (!operations.exists(paths.backupPath)) throw nothingToRollBack();
|
|
528
|
-
const to = await readBinaryVersion(paths.backupPath, runBinary).catch(() => null);
|
|
529
|
-
if (to === null) throw nothingToRollBack();
|
|
530
|
-
const from = await versionBeingLeft(request, paths, runBinary);
|
|
531
|
-
const oldPath = oldFilePath(paths.livePath, operations);
|
|
532
|
-
const liveExists = operations.exists(paths.livePath);
|
|
533
|
-
try {
|
|
534
|
-
if (liveExists) renameWithRetry(operations, paths.livePath, oldPath);
|
|
535
|
-
} catch (error) {
|
|
536
|
-
throw renameFailure(paths.livePath, error, from);
|
|
537
|
-
}
|
|
538
|
-
try {
|
|
539
|
-
renameWithRetry(operations, paths.backupPath, paths.livePath);
|
|
540
|
-
} catch (error) {
|
|
541
|
-
if (liveExists) renameWithRetry(operations, oldPath, paths.livePath);
|
|
542
|
-
throw renameFailure(paths.livePath, error, from);
|
|
543
|
-
}
|
|
544
|
-
return { from, to };
|
|
545
|
-
}
|
|
546
|
-
var realStaleFileOperations = {
|
|
547
|
-
list: (directory) => fs2.readdirSync(directory),
|
|
548
|
-
remove: (filePath) => fs2.rmSync(filePath, { force: true }),
|
|
549
|
-
isAlive: isProcessAlive
|
|
550
|
-
};
|
|
551
|
-
function isRemovableStaleFile(fileName, liveName, operations) {
|
|
552
|
-
const prefix = `${liveName}.`;
|
|
553
|
-
if (!fileName.startsWith(prefix)) return false;
|
|
554
|
-
if (fileName.endsWith(OLD_FILE_SUFFIX)) return true;
|
|
555
|
-
if (!fileName.endsWith(NEW_FILE_SUFFIX)) return false;
|
|
556
|
-
const pidText = fileName.slice(prefix.length, -NEW_FILE_SUFFIX.length);
|
|
557
|
-
return /^\d+$/.test(pidText) && !operations.isAlive(Number(pidText));
|
|
558
|
-
}
|
|
559
|
-
function removeStaleUpdateFiles(execPath = process.execPath, operations = realStaleFileOperations) {
|
|
560
|
-
const directory = WINDOWS_PATH.dirname(execPath);
|
|
561
|
-
const liveName = WINDOWS_PATH.basename(execPath);
|
|
562
|
-
let fileNames;
|
|
563
|
-
try {
|
|
564
|
-
fileNames = operations.list(directory);
|
|
565
|
-
} catch {
|
|
566
|
-
return;
|
|
567
|
-
}
|
|
568
|
-
for (const fileName of fileNames) {
|
|
569
|
-
try {
|
|
570
|
-
if (isRemovableStaleFile(fileName, liveName, operations)) operations.remove(WINDOWS_PATH.join(directory, fileName));
|
|
571
|
-
} catch {
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
// src/update/updateLock.ts
|
|
577
|
-
import fs3 from "fs";
|
|
578
|
-
var STALE_AFTER_MILLIS = 30 * 60 * 1e3;
|
|
579
|
-
var REPLACEMENT_SETTLE_MILLIS = 200;
|
|
580
|
-
var LockState = {
|
|
581
|
-
Absent: "absent",
|
|
582
|
-
Held: "held",
|
|
583
|
-
Stale: "stale"
|
|
584
|
-
};
|
|
585
|
-
function readLockPid(lockPath) {
|
|
586
|
-
try {
|
|
587
|
-
const parsed = JSON.parse(fs3.readFileSync(lockPath, "utf-8"));
|
|
588
|
-
return typeof parsed.pid === "number" && parsed.pid > 0 ? parsed.pid : null;
|
|
589
|
-
} catch {
|
|
590
|
-
return null;
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
function lockState(lockPath) {
|
|
594
|
-
let modifiedMillis;
|
|
595
|
-
try {
|
|
596
|
-
modifiedMillis = fs3.statSync(lockPath).mtimeMs;
|
|
597
|
-
} catch {
|
|
598
|
-
return LockState.Absent;
|
|
599
|
-
}
|
|
600
|
-
if (Date.now() - modifiedMillis >= STALE_AFTER_MILLIS) return LockState.Stale;
|
|
601
|
-
const pid = readLockPid(lockPath);
|
|
602
|
-
return pid === null || isProcessAlive(pid) ? LockState.Held : LockState.Stale;
|
|
603
|
-
}
|
|
604
|
-
function lockContents() {
|
|
605
|
-
return JSON.stringify({ pid: process.pid, startedAt: Date.now() });
|
|
606
|
-
}
|
|
607
|
-
function createLock(lockPath) {
|
|
608
|
-
try {
|
|
609
|
-
const fileDescriptor = fs3.openSync(lockPath, "wx");
|
|
610
|
-
fs3.writeSync(fileDescriptor, lockContents());
|
|
611
|
-
fs3.closeSync(fileDescriptor);
|
|
612
|
-
return true;
|
|
613
|
-
} catch (error) {
|
|
614
|
-
if (error.code === "EEXIST") return false;
|
|
615
|
-
throw error;
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
function wait(millis) {
|
|
619
|
-
return new Promise((resolve) => setTimeout(resolve, millis));
|
|
620
|
-
}
|
|
621
|
-
async function replaceStaleLock(lockPath) {
|
|
622
|
-
const temporaryPath = `${lockPath}.${process.pid}`;
|
|
623
|
-
fs3.writeFileSync(temporaryPath, lockContents());
|
|
624
|
-
fs3.renameSync(temporaryPath, lockPath);
|
|
625
|
-
await wait(REPLACEMENT_SETTLE_MILLIS);
|
|
626
|
-
return readLockPid(lockPath) === process.pid;
|
|
627
|
-
}
|
|
628
|
-
async function acquireUpdateLock() {
|
|
629
|
-
ensurePinggyConfigDir();
|
|
630
|
-
const lockPath = getUpdateLockPath();
|
|
631
|
-
if (createLock(lockPath)) return true;
|
|
632
|
-
switch (lockState(lockPath)) {
|
|
633
|
-
case LockState.Held:
|
|
634
|
-
return false;
|
|
635
|
-
case LockState.Absent:
|
|
636
|
-
return createLock(lockPath);
|
|
637
|
-
case LockState.Stale:
|
|
638
|
-
return replaceStaleLock(lockPath);
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
function isUpdateLockHeld() {
|
|
642
|
-
return lockState(getUpdateLockPath()) === LockState.Held;
|
|
643
|
-
}
|
|
644
|
-
function isUpdateLockOurs() {
|
|
645
|
-
return readLockPid(getUpdateLockPath()) === process.pid;
|
|
646
|
-
}
|
|
647
|
-
function releaseUpdateLock() {
|
|
648
|
-
if (isUpdateLockOurs()) fs3.rmSync(getUpdateLockPath(), { force: true });
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
// src/update/unixLinkSwap.ts
|
|
652
|
-
import fs4 from "fs";
|
|
653
|
-
import path3 from "path";
|
|
654
|
-
var BINARY_NAME = "pinggy";
|
|
655
|
-
var DOWNLOAD_PREFIX = "pinggy.download.";
|
|
656
|
-
var CONVERT_PREFIX = "pinggy.convert.";
|
|
657
|
-
var LINK_TEMP_PREFIX = "pinggy.link.";
|
|
658
|
-
var EXECUTABLE_MODE2 = 493;
|
|
659
|
-
function fsyncPath(target, flags) {
|
|
660
|
-
const fileDescriptor = fs4.openSync(target, flags);
|
|
661
|
-
try {
|
|
662
|
-
fs4.fsyncSync(fileDescriptor);
|
|
663
|
-
} finally {
|
|
664
|
-
fs4.closeSync(fileDescriptor);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
var realUnixSwapEnvironment = {
|
|
668
|
-
fsyncFile: (filePath) => fsyncPath(filePath, "r+"),
|
|
669
|
-
fsyncDirectory: (directory) => fsyncPath(directory, "r"),
|
|
670
|
-
daemonVersion: () => getDaemonInfo()?.cliVersion,
|
|
671
|
-
serviceEntryText: readServiceEntryText,
|
|
672
|
-
isAlive: isProcessAlive
|
|
673
|
-
};
|
|
674
|
-
function unixDownloadPath(layout) {
|
|
675
|
-
return path3.posix.join(layout.versionsDirectory, `${DOWNLOAD_PREFIX}${process.pid}`);
|
|
676
|
-
}
|
|
677
|
-
function versionDirectory(layout, version) {
|
|
678
|
-
return path3.posix.join(layout.versionsDirectory, version);
|
|
679
|
-
}
|
|
680
|
-
function versionBinaryPath(layout, version) {
|
|
681
|
-
return path3.posix.join(versionDirectory(layout, version), BINARY_NAME);
|
|
682
|
-
}
|
|
683
|
-
function readLinkVersion(layout) {
|
|
684
|
-
try {
|
|
685
|
-
const version = path3.posix.basename(path3.posix.dirname(fs4.readlinkSync(layout.link)));
|
|
686
|
-
return parseVersion(version) ? version : null;
|
|
687
|
-
} catch {
|
|
688
|
-
return null;
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
function installedVersions(layout) {
|
|
692
|
-
return fs4.readdirSync(layout.versionsDirectory).filter((name) => parseVersion(name) !== null);
|
|
693
|
-
}
|
|
694
|
-
function highestVersionBelow(layout, ceiling) {
|
|
695
|
-
const lower = installedVersions(layout).filter((version) => isNewerVersion(ceiling, version));
|
|
696
|
-
return lower.reduce((best, version) => best === null || isNewerVersion(version, best) ? version : best, null);
|
|
697
|
-
}
|
|
698
|
-
function moveLink(layout, version, environment) {
|
|
699
|
-
fs4.mkdirSync(layout.binDirectory, { recursive: true });
|
|
700
|
-
const temporaryLink = path3.posix.join(layout.binDirectory, `${LINK_TEMP_PREFIX}${process.pid}`);
|
|
701
|
-
fs4.rmSync(temporaryLink, { force: true });
|
|
702
|
-
fs4.symlinkSync(versionBinaryPath(layout, version), temporaryLink);
|
|
703
|
-
fs4.renameSync(temporaryLink, layout.link);
|
|
704
|
-
environment.fsyncDirectory(layout.binDirectory);
|
|
705
|
-
}
|
|
706
|
-
function convertPlainFile(layout, execPath, runningVersion, environment) {
|
|
707
|
-
const temporaryPath = path3.posix.join(layout.versionsDirectory, `${CONVERT_PREFIX}${process.pid}`);
|
|
708
|
-
fs4.copyFileSync(execPath, temporaryPath);
|
|
709
|
-
fs4.chmodSync(temporaryPath, EXECUTABLE_MODE2);
|
|
710
|
-
environment.fsyncFile(temporaryPath);
|
|
711
|
-
fs4.mkdirSync(versionDirectory(layout, runningVersion), { recursive: true });
|
|
712
|
-
fs4.renameSync(temporaryPath, versionBinaryPath(layout, runningVersion));
|
|
713
|
-
environment.fsyncDirectory(versionDirectory(layout, runningVersion));
|
|
714
|
-
}
|
|
715
|
-
function isNamedByService(layout, version, environment) {
|
|
716
|
-
return environment.serviceEntryText().includes(versionBinaryPath(layout, version));
|
|
717
|
-
}
|
|
718
|
-
function removeDeadProcessTemp(directory, prefix, environment) {
|
|
719
|
-
for (const name of fs4.readdirSync(directory)) {
|
|
720
|
-
const pidText = name.startsWith(prefix) ? name.slice(prefix.length) : "";
|
|
721
|
-
if (/^\d+$/.test(pidText) && !environment.isAlive(Number(pidText))) {
|
|
722
|
-
fs4.rmSync(path3.posix.join(directory, name), { force: true });
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
function removeStaleUnixFiles(layout, environment = realUnixSwapEnvironment) {
|
|
727
|
-
const locations = [
|
|
728
|
-
[layout.versionsDirectory, DOWNLOAD_PREFIX],
|
|
729
|
-
[layout.versionsDirectory, CONVERT_PREFIX],
|
|
730
|
-
[layout.binDirectory, LINK_TEMP_PREFIX]
|
|
731
|
-
];
|
|
732
|
-
for (const [directory, prefix] of locations) {
|
|
733
|
-
try {
|
|
734
|
-
removeDeadProcessTemp(directory, prefix, environment);
|
|
735
|
-
} catch {
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
function pruneVersions(layout, environment = realUnixSwapEnvironment) {
|
|
740
|
-
removeStaleUnixFiles(layout, environment);
|
|
741
|
-
const target = readLinkVersion(layout);
|
|
742
|
-
if (target === null) return;
|
|
743
|
-
const keep = /* @__PURE__ */ new Set([target, highestVersionBelow(layout, target), environment.daemonVersion()]);
|
|
744
|
-
for (const version of installedVersions(layout)) {
|
|
745
|
-
if (keep.has(version) || isNamedByService(layout, version, environment)) continue;
|
|
746
|
-
fs4.rmSync(versionDirectory(layout, version), { recursive: true, force: true });
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
function replaceFailure(layout, error, running) {
|
|
750
|
-
return new UpdateError(
|
|
751
|
-
UpdateErrorCode.RenameFailed,
|
|
752
|
-
`Could not replace ${layout.link}: ${errorMessage(error)}. pinggy ${running} is unchanged.`
|
|
753
|
-
);
|
|
754
|
-
}
|
|
755
|
-
function installVersion(request, environment = realUnixSwapEnvironment) {
|
|
756
|
-
const { layout, plainFileToConvert, runningVersion, newVersion } = request;
|
|
757
|
-
try {
|
|
758
|
-
fs4.mkdirSync(layout.versionsDirectory, { recursive: true });
|
|
759
|
-
if (plainFileToConvert !== null) convertPlainFile(layout, plainFileToConvert, runningVersion, environment);
|
|
760
|
-
fs4.mkdirSync(versionDirectory(layout, newVersion), { recursive: true });
|
|
761
|
-
fs4.renameSync(unixDownloadPath(layout), versionBinaryPath(layout, newVersion));
|
|
762
|
-
environment.fsyncDirectory(versionDirectory(layout, newVersion));
|
|
763
|
-
moveLink(layout, newVersion, environment);
|
|
764
|
-
} catch (error) {
|
|
765
|
-
throw replaceFailure(layout, error, runningVersion);
|
|
766
|
-
}
|
|
767
|
-
try {
|
|
768
|
-
pruneVersions(layout, environment);
|
|
769
|
-
} catch {
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
function nothingToRollBack2() {
|
|
773
|
-
return new UpdateError(UpdateErrorCode.NothingToRollBack, "No previous version to go back to.");
|
|
774
|
-
}
|
|
775
|
-
function previousVersion(layout, current) {
|
|
776
|
-
try {
|
|
777
|
-
const version = highestVersionBelow(layout, current);
|
|
778
|
-
if (version !== null) return version;
|
|
779
|
-
} catch {
|
|
780
|
-
}
|
|
781
|
-
throw nothingToRollBack2();
|
|
782
|
-
}
|
|
783
|
-
function rollbackUnixVersion(layout, environment = realUnixSwapEnvironment) {
|
|
784
|
-
const current = readLinkVersion(layout);
|
|
785
|
-
if (current === null) throw nothingToRollBack2();
|
|
786
|
-
const previous = previousVersion(layout, current);
|
|
787
|
-
try {
|
|
788
|
-
moveLink(layout, previous, environment);
|
|
789
|
-
} catch (error) {
|
|
790
|
-
throw replaceFailure(layout, error, current);
|
|
791
|
-
}
|
|
792
|
-
writeUpdateState({ rolledBackFromVersion: current });
|
|
793
|
-
if (environment.daemonVersion() !== current && !isNamedByService(layout, current, environment)) {
|
|
794
|
-
fs4.rmSync(versionDirectory(layout, current), { recursive: true, force: true });
|
|
795
|
-
}
|
|
796
|
-
return { from: current, to: previous };
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
// src/update/applyUpdate.ts
|
|
800
|
-
import fs5 from "fs";
|
|
801
|
-
function windowsUpdateTarget(execPath, runBinary) {
|
|
802
|
-
return {
|
|
803
|
-
downloadPath: windowsDownloadPath(execPath),
|
|
804
|
-
rollbackBinaryPath: () => windowsInstallPaths(execPath).livePath,
|
|
805
|
-
installedVersion: (runningVersion) => runningVersion,
|
|
806
|
-
prepareDownload: () => void 0,
|
|
807
|
-
install: async ({ newVersion, runningVersion }) => {
|
|
808
|
-
await swapWindowsBinary(
|
|
809
|
-
{ execPath, downloadPath: windowsDownloadPath(execPath), newVersion, runningVersion },
|
|
810
|
-
realWindowsFileOperations,
|
|
811
|
-
runBinary
|
|
812
|
-
);
|
|
813
|
-
moveDeclinedInstallToVersion(execPath, newVersion);
|
|
814
|
-
},
|
|
815
|
-
rollback: async (runningVersion) => {
|
|
816
|
-
const result = await rollbackWindowsBinary(
|
|
817
|
-
{ execPath, runningVersion, latestVersion: readUpdateState().latestVersion },
|
|
818
|
-
realWindowsFileOperations,
|
|
819
|
-
runBinary
|
|
820
|
-
);
|
|
821
|
-
writeUpdateState({ rolledBackFromVersion: result.from });
|
|
822
|
-
return result;
|
|
823
|
-
}
|
|
824
|
-
};
|
|
825
|
-
}
|
|
826
|
-
function unixUpdateTarget(kind, probe) {
|
|
827
|
-
const layout = unixLayoutPaths(probe.homeRealPath);
|
|
828
|
-
const isPlainFile = kind === InstallKind.UnixPlainFile;
|
|
829
|
-
return {
|
|
830
|
-
downloadPath: unixDownloadPath(layout),
|
|
831
|
-
rollbackBinaryPath: (version) => versionBinaryPath(layout, version),
|
|
832
|
-
installedVersion: (runningVersion) => readLinkVersion(layout) ?? runningVersion,
|
|
833
|
-
prepareDownload: () => {
|
|
834
|
-
fs5.mkdirSync(layout.versionsDirectory, { recursive: true });
|
|
835
|
-
},
|
|
836
|
-
install: ({ newVersion, runningVersion }) => installVersion({
|
|
837
|
-
layout,
|
|
838
|
-
plainFileToConvert: isPlainFile ? probe.execPath : null,
|
|
839
|
-
newVersion,
|
|
840
|
-
runningVersion
|
|
841
|
-
}),
|
|
842
|
-
rollback: () => {
|
|
843
|
-
if (isPlainFile) throw nothingToRollBack2();
|
|
844
|
-
return rollbackUnixVersion(layout);
|
|
845
|
-
}
|
|
846
|
-
};
|
|
847
|
-
}
|
|
848
|
-
function createUpdateTarget(kind, probe, runBinary = runPinggyBinary) {
|
|
849
|
-
switch (kind) {
|
|
850
|
-
case InstallKind.WindowsWritable:
|
|
851
|
-
return windowsUpdateTarget(probe.execPath, runBinary);
|
|
852
|
-
case InstallKind.UnixLayout:
|
|
853
|
-
case InstallKind.UnixPlainFile:
|
|
854
|
-
return unixUpdateTarget(kind, probe);
|
|
855
|
-
default:
|
|
856
|
-
return null;
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
function downloadRelease(target, tag, assetName, runningVersion, dependencies = defaultDownloadDependencies) {
|
|
860
|
-
target.prepareDownload();
|
|
861
|
-
return downloadAndVerify({ tag, assetName, tempPath: target.downloadPath, runningVersion }, dependencies);
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
export {
|
|
865
|
-
resolveLatestTag,
|
|
866
|
-
isReleaseComplete,
|
|
867
|
-
resolveLatestNpmVersion,
|
|
868
|
-
versionFromTag,
|
|
869
|
-
parseVersion,
|
|
870
|
-
isNewerVersion,
|
|
871
|
-
UpdateError,
|
|
872
|
-
environmentWithoutDeviceToken,
|
|
873
|
-
pinggyBinaryRunOptions,
|
|
874
|
-
runPinggyBinary,
|
|
875
|
-
readBinaryVersion,
|
|
876
|
-
readBinaryIpcVersion,
|
|
877
|
-
assetNameFor,
|
|
878
|
-
removeQuietly,
|
|
879
|
-
windowsInstallPaths,
|
|
880
|
-
removeStaleUpdateFiles,
|
|
881
|
-
acquireUpdateLock,
|
|
882
|
-
isUpdateLockHeld,
|
|
883
|
-
isUpdateLockOurs,
|
|
884
|
-
releaseUpdateLock,
|
|
885
|
-
readLinkVersion,
|
|
886
|
-
removeStaleUnixFiles,
|
|
887
|
-
createUpdateTarget,
|
|
888
|
-
downloadRelease
|
|
889
|
-
};
|