farming-code 2.2.29 → 2.2.30
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/README.md +25 -66
- package/README.zh_cn.md +15 -66
- package/backend/acp-runtime.js +1 -15
- package/backend/acp-session-state.js +78 -88
- package/backend/agent-session-history.js +45 -12
- package/backend/codex-session-history.js +1 -1
- package/backend/codex-transcript.js +0 -202
- package/backend/config-manager.js +3 -19
- package/backend/farming-app-cli.js +1 -1
- package/backend/farming-cli.js +3 -57
- package/backend/index-html.js +21 -3
- package/backend/main-agent-skills.js +1 -18
- package/backend/server.js +6 -48
- package/backend/storage-layout.js +15 -0
- package/backend/update-service.js +55 -935
- package/config/farming.deploy.env.example +1 -1
- package/config/farming.install.env.example +1 -1
- package/dist/acp/codex-acp-1.1.4.mjs +387 -7
- package/dist/assets/App-Z1N7T0eM.js +695 -0
- package/dist/assets/{ProjectFilesSection-Cf2BEcAN.js → ProjectFilesSection-K3GHuQ4U.js} +2 -2
- package/dist/assets/code-dark-kLsQLvpf.css +1 -0
- package/dist/assets/{index-CQNaZc3l.js → index-D-zetSol.js} +2 -2
- package/dist/assets/main-DUTz-Rd2.css +1 -0
- package/dist/index.html +2 -1
- package/extensions/browser/backend/browser-resource-manager.js +27 -5
- package/extensions/browser/backend/browser-router.js +8 -0
- package/extensions/browser/backend/executable-discovery.js +26 -12
- package/extensions/browser/backend/managed-chromium-installer.js +593 -0
- package/extensions/browser/frontend/types.ts +8 -1
- package/extensions/browser/frontend/useBrowserResources.ts +8 -0
- package/index.html +1 -0
- package/package.json +1 -1
- package/backend/agent-memory-report.js +0 -583
- package/backend/bundle-update-helper.js +0 -113
- package/backend/codex-rollout-follower.js +0 -326
- package/dist/assets/App-Ex2oVVXO.js +0 -693
- package/dist/assets/code-dark-BEZTQwYZ.css +0 -1
- package/dist/assets/main-DK9Sy7-c.css +0 -1
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
const childProcess = require('child_process');
|
|
2
|
-
const crypto = require('crypto');
|
|
3
2
|
const fs = require('fs');
|
|
4
3
|
const http = require('http');
|
|
5
4
|
const https = require('https');
|
|
6
5
|
const os = require('os');
|
|
7
6
|
const path = require('path');
|
|
8
|
-
const { Transform } = require('stream');
|
|
9
|
-
const { pipeline } = require('stream/promises');
|
|
10
7
|
const { readServerProcessIdentity } = require('./server-process-identity');
|
|
11
8
|
const storageLayout = require('./storage-layout');
|
|
12
9
|
|
|
13
10
|
const CACHE_TTL_MS = 5 * 60 * 1000;
|
|
14
|
-
const UPDATE_SOURCE_UNCONFIGURED_REASON = 'Update source is empty. Save an Update URL in Settings or restore the default source.';
|
|
15
11
|
const NPM_PACKAGE_NAME = 'farming-code';
|
|
16
12
|
const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org';
|
|
17
13
|
|
|
@@ -66,102 +62,18 @@ function detectInstallMethod(rootDir, options = {}) {
|
|
|
66
62
|
return 'source';
|
|
67
63
|
}
|
|
68
64
|
|
|
69
|
-
function installMethodAllowsBundleUpdate(method) {
|
|
70
|
-
return method === 'app-bundle' || method === 'source-deploy';
|
|
71
|
-
}
|
|
72
|
-
|
|
73
65
|
function installMethodBlockedReason(method) {
|
|
74
66
|
if (method === 'source') return 'Source checkouts update through Git, not the in-app updater';
|
|
67
|
+
if (method === 'source-deploy') return 'Source deployments update through their deployment workflow, not the in-app updater';
|
|
68
|
+
if (method === 'app-bundle') return 'App bundles update by reinstalling a release package or switching to npm';
|
|
75
69
|
if (method === 'standalone-cli') return 'Standalone CLI updates must reinstall the matching release asset';
|
|
76
|
-
return `In-app updates
|
|
70
|
+
return `In-app updates require an npm installation; reinstall ${method || 'this installation'} through npm`;
|
|
77
71
|
}
|
|
78
72
|
|
|
79
73
|
function hasComparableVersion(value) {
|
|
80
74
|
return versionParts(value).length > 0;
|
|
81
75
|
}
|
|
82
76
|
|
|
83
|
-
function releaseVersionFromAsset(asset) {
|
|
84
|
-
return normalizeVersion(asset && (
|
|
85
|
-
asset.releaseVersion ||
|
|
86
|
-
asset.version ||
|
|
87
|
-
asset.file ||
|
|
88
|
-
asset.name ||
|
|
89
|
-
basenameFromUrl(asset.browser_download_url || asset.downloadUrl || asset.url)
|
|
90
|
-
));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function releaseVersionFromRelease(release) {
|
|
94
|
-
const taggedVersion = normalizeVersion(release && (release.tag_name || release.name));
|
|
95
|
-
if (hasComparableVersion(taggedVersion)) return taggedVersion;
|
|
96
|
-
const assetVersion = releaseVersionFromAsset(selectReleaseAsset(release));
|
|
97
|
-
return hasComparableVersion(assetVersion) ? assetVersion : taggedVersion;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function releaseVersionFromManifest(manifest) {
|
|
101
|
-
if (!manifest) return '';
|
|
102
|
-
if (manifest.version) return normalizeVersion(manifest.version);
|
|
103
|
-
if (manifest.releaseVersion) return normalizeVersion(manifest.releaseVersion);
|
|
104
|
-
if (manifest.tag) return normalizeVersion(manifest.tag);
|
|
105
|
-
const assets = manifestAssets(manifest);
|
|
106
|
-
const appBundle = assets.find(asset => asset && asset.type === 'app-bundle') || assets[0];
|
|
107
|
-
return releaseVersionFromAsset(appBundle);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function releaseTagFromManifest(manifest) {
|
|
111
|
-
const tag = String(manifest && manifest.tag || '').trim();
|
|
112
|
-
if (tag) return tag;
|
|
113
|
-
const version = releaseVersionFromManifest(manifest);
|
|
114
|
-
return version ? `v${version}` : '';
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function isAbsoluteHttpUrl(value) {
|
|
118
|
-
return /^https?:\/\//i.test(String(value || ''));
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function resolveUpdateUrl(value, baseUrl = '') {
|
|
122
|
-
const raw = String(value || '').trim();
|
|
123
|
-
if (!raw) return '';
|
|
124
|
-
if (isAbsoluteHttpUrl(raw)) return raw;
|
|
125
|
-
if (!baseUrl) return '';
|
|
126
|
-
try {
|
|
127
|
-
return new URL(raw, baseUrl).toString();
|
|
128
|
-
} catch {
|
|
129
|
-
return '';
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function basenameFromUrl(value) {
|
|
134
|
-
const raw = String(value || '').trim();
|
|
135
|
-
if (!raw) return '';
|
|
136
|
-
try {
|
|
137
|
-
const parsed = isAbsoluteHttpUrl(raw) ? new URL(raw) : new URL(raw, 'http://farming.local/');
|
|
138
|
-
const pathname = parsed.pathname || '';
|
|
139
|
-
return pathname.split('/').filter(Boolean).pop() || '';
|
|
140
|
-
} catch {
|
|
141
|
-
return raw.split('/').filter(Boolean).pop() || '';
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function manifestAssets(manifest) {
|
|
146
|
-
if (Array.isArray(manifest && manifest.assets)) return manifest.assets;
|
|
147
|
-
const tarUrl = manifest && (manifest.tarUrl || manifest.downloadUrl || manifest.url);
|
|
148
|
-
const file = manifest && (manifest.file || manifest.name || basenameFromUrl(tarUrl));
|
|
149
|
-
if (!tarUrl && !file) return [];
|
|
150
|
-
return [{
|
|
151
|
-
type: 'app-bundle',
|
|
152
|
-
file,
|
|
153
|
-
url: tarUrl,
|
|
154
|
-
size: manifest.size || manifest.bytes || 0,
|
|
155
|
-
sha256: manifest.sha256 || '',
|
|
156
|
-
releaseVersion: manifest.releaseVersion || manifest.version || '',
|
|
157
|
-
}];
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function normalizeSha256(value) {
|
|
161
|
-
const normalized = String(value || '').trim().toLowerCase();
|
|
162
|
-
return /^[a-f0-9]{64}$/.test(normalized) ? normalized : '';
|
|
163
|
-
}
|
|
164
|
-
|
|
165
77
|
function normalizePlatform(value) {
|
|
166
78
|
const raw = String(value || '').trim().toLowerCase();
|
|
167
79
|
if (raw === 'darwin' || raw === 'macos' || raw === 'macosx' || raw === 'osx') return 'darwin';
|
|
@@ -176,174 +88,6 @@ function normalizeArch(value) {
|
|
|
176
88
|
return raw;
|
|
177
89
|
}
|
|
178
90
|
|
|
179
|
-
function assetMatchesRuntime(asset, runtime) {
|
|
180
|
-
if (!runtime) return true;
|
|
181
|
-
return normalizePlatform(asset && asset.platform) === runtime.platform
|
|
182
|
-
&& normalizeArch(asset && asset.arch) === runtime.arch;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function assetCompatibilityPreference(asset, preferredProfile = '') {
|
|
186
|
-
const profile = String(asset && asset.compatibilityProfile || '').trim();
|
|
187
|
-
const preferred = String(preferredProfile || '').trim();
|
|
188
|
-
if (preferred) return profile === preferred ? 0 : 1;
|
|
189
|
-
return profile ? 1 : 0;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function runtimeFromBundleName(value) {
|
|
193
|
-
const match = /-(darwin|linux)-(x64|arm64)\.tar\.gz$/i.exec(String(value || ''));
|
|
194
|
-
return match ? { platform: normalizePlatform(match[1]), arch: normalizeArch(match[2]) } : null;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function releaseFromAssetListing(release) {
|
|
198
|
-
const rawAssets = Array.isArray(release && release.assets) ? release.assets : [];
|
|
199
|
-
const checksumAsset = rawAssets.find(asset => /(?:checksums?|sha256)\.(?:txt|sha256)$/i.test(String(asset && asset.name || '')));
|
|
200
|
-
return {
|
|
201
|
-
...release,
|
|
202
|
-
__directAssets: true,
|
|
203
|
-
assets: rawAssets.map(asset => {
|
|
204
|
-
const runtime = runtimeFromBundleName(asset && asset.name);
|
|
205
|
-
const isBundle = /^farming[-_].*\.tar\.gz$/i.test(String(asset && asset.name || ''));
|
|
206
|
-
return {
|
|
207
|
-
...asset,
|
|
208
|
-
...(isBundle ? { type: 'app-bundle' } : {}),
|
|
209
|
-
...(runtime || {}),
|
|
210
|
-
...(checksumAsset?.browser_download_url ? { checksum_url: checksumAsset.browser_download_url } : {}),
|
|
211
|
-
};
|
|
212
|
-
}),
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function releaseFromManifest(manifest, options = {}) {
|
|
217
|
-
const tag = releaseTagFromManifest(manifest);
|
|
218
|
-
const assetBaseUrl = options.assetBaseUrl || options.manifestUrl || '';
|
|
219
|
-
const assets = manifestAssets(manifest);
|
|
220
|
-
return {
|
|
221
|
-
__manifest: manifest,
|
|
222
|
-
tag_name: tag,
|
|
223
|
-
name: manifest && manifest.name ? String(manifest.name) : tag,
|
|
224
|
-
published_at: manifest && (manifest.publishedAt || manifest.builtAt) ? String(manifest.publishedAt || manifest.builtAt) : '',
|
|
225
|
-
assets: assets
|
|
226
|
-
.map(asset => {
|
|
227
|
-
const file = String(asset && (
|
|
228
|
-
asset.file ||
|
|
229
|
-
asset.name ||
|
|
230
|
-
basenameFromUrl(asset.browser_download_url || asset.downloadUrl || asset.url)
|
|
231
|
-
) || '');
|
|
232
|
-
if (!file) return null;
|
|
233
|
-
return {
|
|
234
|
-
...asset,
|
|
235
|
-
name: file,
|
|
236
|
-
size: asset.size || asset.bytes || 0,
|
|
237
|
-
browser_download_url: asset.browser_download_url
|
|
238
|
-
|| resolveUpdateUrl(asset.downloadUrl || asset.url || file, assetBaseUrl),
|
|
239
|
-
};
|
|
240
|
-
})
|
|
241
|
-
.filter(asset => asset && asset.browser_download_url),
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function selectReleaseAsset(release, patternText = '') {
|
|
246
|
-
const assets = Array.isArray(release && release.assets) ? release.assets : [];
|
|
247
|
-
if (patternText) {
|
|
248
|
-
const pattern = new RegExp(patternText);
|
|
249
|
-
const matched = assets.find(asset => pattern.test(String(asset.name || '')) && asset.browser_download_url);
|
|
250
|
-
if (matched) return matched;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return assets.find(asset => (
|
|
254
|
-
/^farming[-_].*\.tar\.gz$/i.test(String(asset.name || '')) &&
|
|
255
|
-
!/(sha256|checksum|checksums)/i.test(String(asset.name || '')) &&
|
|
256
|
-
asset.browser_download_url
|
|
257
|
-
)) || null;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function releaseAssetVersion(asset, fallbackVersion = '') {
|
|
261
|
-
return normalizeVersion(
|
|
262
|
-
asset && (asset.releaseVersion || asset.version || asset.name || asset.file || asset.browser_download_url) ||
|
|
263
|
-
fallbackVersion
|
|
264
|
-
);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function selectableReleaseAssets(release, patternText = '', runtime = null, preferredCompatibilityProfile = '') {
|
|
268
|
-
const assets = Array.isArray(release && release.assets) ? release.assets : [];
|
|
269
|
-
const pattern = patternText ? new RegExp(patternText) : null;
|
|
270
|
-
return assets
|
|
271
|
-
.filter(asset => (
|
|
272
|
-
/^farming[-_].*\.tar\.gz$/i.test(String(asset.name || '')) &&
|
|
273
|
-
!/(sha256|checksum|checksums)/i.test(String(asset.name || '')) &&
|
|
274
|
-
asset.browser_download_url &&
|
|
275
|
-
assetMatchesRuntime(asset, runtime) &&
|
|
276
|
-
(!preferredCompatibilityProfile || String(asset.compatibilityProfile || '').trim() === preferredCompatibilityProfile) &&
|
|
277
|
-
(!pattern || pattern.test(String(asset.name || '')))
|
|
278
|
-
))
|
|
279
|
-
.sort((left, right) => (
|
|
280
|
-
compareVersions(releaseAssetVersion(right), releaseAssetVersion(left)) ||
|
|
281
|
-
assetCompatibilityPreference(left, preferredCompatibilityProfile) - assetCompatibilityPreference(right, preferredCompatibilityProfile) ||
|
|
282
|
-
String(right.name || '').localeCompare(String(left.name || ''))
|
|
283
|
-
));
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
function selectReleaseAssetByName(release, assetName, patternText = '', runtime = null, preferredCompatibilityProfile = '') {
|
|
287
|
-
const wanted = String(assetName || '').trim();
|
|
288
|
-
if (!wanted) return selectableReleaseAssets(release, patternText, runtime, preferredCompatibilityProfile)[0] || null;
|
|
289
|
-
return selectableReleaseAssets(release, patternText, runtime, preferredCompatibilityProfile)
|
|
290
|
-
.find(asset => String(asset.name || '') === wanted) || null;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
function selectManifestAsset(release) {
|
|
294
|
-
const assets = Array.isArray(release && release.assets) ? release.assets : [];
|
|
295
|
-
return assets.find(asset => (
|
|
296
|
-
String(asset.name || '').toLowerCase() === 'manifest.json' &&
|
|
297
|
-
asset.browser_download_url
|
|
298
|
-
)) || null;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function manifestAssetSafety(asset, manifest, options = {}) {
|
|
302
|
-
const runtime = options.runtime || null;
|
|
303
|
-
if (!asset) {
|
|
304
|
-
return {
|
|
305
|
-
safe: false,
|
|
306
|
-
reason: 'Update manifest has no app bundle asset',
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
const assets = manifestAssets(manifest);
|
|
310
|
-
if (assets.length === 0 && !asset.checksum_url && !asset.checksumUrl && !asset.sha256) {
|
|
311
|
-
return {
|
|
312
|
-
safe: false,
|
|
313
|
-
reason: 'Update manifest is missing a SHA-256 checksum',
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
const assetName = String(asset.name || '');
|
|
318
|
-
const entry = assets.find(item => String(item.file || item.name || basenameFromUrl(item.url || item.tarUrl || item.downloadUrl) || '') === assetName) || (assets.length === 0 ? asset : null);
|
|
319
|
-
if (!entry) {
|
|
320
|
-
return {
|
|
321
|
-
safe: false,
|
|
322
|
-
reason: 'Update manifest does not describe the app bundle',
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
if (entry.type && entry.type !== 'app-bundle') {
|
|
326
|
-
return {
|
|
327
|
-
safe: false,
|
|
328
|
-
reason: 'Update manifest does not mark the app bundle correctly',
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
if (!assetMatchesRuntime(entry, runtime)) {
|
|
332
|
-
return {
|
|
333
|
-
safe: false,
|
|
334
|
-
reason: `Update app bundle is not compatible with ${runtime.platform}-${runtime.arch}`,
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
const checksumUrl = entry.checksum_url || entry.checksumUrl || asset.checksum_url || asset.checksumUrl || '';
|
|
338
|
-
if (!normalizeSha256(entry.sha256 || asset.sha256) && !checksumUrl) {
|
|
339
|
-
return {
|
|
340
|
-
safe: false,
|
|
341
|
-
reason: 'Update manifest app bundle is missing a SHA-256 checksum',
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
return { safe: true, reason: '' };
|
|
345
|
-
}
|
|
346
|
-
|
|
347
91
|
function requestWithRedirects(url, options = {}, redirectCount = 0, authOrigin = null) {
|
|
348
92
|
if (redirectCount > 5) {
|
|
349
93
|
return Promise.reject(new Error(`too many redirects for ${url}`));
|
|
@@ -415,274 +159,6 @@ async function requestJson(url, options = {}) {
|
|
|
415
159
|
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
416
160
|
}
|
|
417
161
|
|
|
418
|
-
async function requestText(url, options = {}) {
|
|
419
|
-
const response = await requestWithRedirects(url, options);
|
|
420
|
-
const chunks = [];
|
|
421
|
-
for await (const chunk of response) {
|
|
422
|
-
chunks.push(chunk);
|
|
423
|
-
}
|
|
424
|
-
return Buffer.concat(chunks).toString('utf8');
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
function decodeHtmlAttribute(value) {
|
|
428
|
-
return String(value || '')
|
|
429
|
-
.replace(/&/g, '&')
|
|
430
|
-
.replace(/"/g, '"')
|
|
431
|
-
.replace(/'/g, "'")
|
|
432
|
-
.replace(/</g, '<')
|
|
433
|
-
.replace(/>/g, '>');
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
function isDirectoryUpdateSource(value) {
|
|
437
|
-
const raw = String(value || '').trim();
|
|
438
|
-
if (!raw) return false;
|
|
439
|
-
try {
|
|
440
|
-
return new URL(raw).pathname.endsWith('/');
|
|
441
|
-
} catch {
|
|
442
|
-
return raw.endsWith('/');
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
function isGitHubReleasePage(value) {
|
|
447
|
-
try {
|
|
448
|
-
const url = new URL(value);
|
|
449
|
-
return url.hostname === 'github.com' && /\/releases\/(?:latest|tag\/[^/]+)\/?$/i.test(url.pathname);
|
|
450
|
-
} catch {
|
|
451
|
-
return false;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
function githubReleaseTagFromPage(body) {
|
|
456
|
-
for (const match of String(body || '').matchAll(/\/releases\/tag\/([^"'/?#<]+)/gi)) {
|
|
457
|
-
const tag = decodeHtmlAttribute(match[1]);
|
|
458
|
-
if (/^v\d/.test(tag)) return tag;
|
|
459
|
-
}
|
|
460
|
-
return '';
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
function githubExpandedAssetsUrl(pageUrl, tag) {
|
|
464
|
-
try {
|
|
465
|
-
const url = new URL(pageUrl);
|
|
466
|
-
const parts = url.pathname.split('/').filter(Boolean);
|
|
467
|
-
if (parts.length < 2 || !tag) return '';
|
|
468
|
-
return `${url.origin}/${parts[0]}/${parts[1]}/releases/expanded_assets/${encodeURIComponent(tag)}`;
|
|
469
|
-
} catch {
|
|
470
|
-
return '';
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
function releaseFromGitHubReleasePage(body, options = {}) {
|
|
475
|
-
const pageUrl = options.pageUrl || '';
|
|
476
|
-
const entries = [];
|
|
477
|
-
for (const match of String(body || '').matchAll(/\bhref\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi)) {
|
|
478
|
-
const value = decodeHtmlAttribute(match[1] || match[2] || match[3]).trim();
|
|
479
|
-
if (value) entries.push(value);
|
|
480
|
-
}
|
|
481
|
-
const checksumUrl = entries
|
|
482
|
-
.map(entry => ({ entry, file: basenameFromUrl(entry) }))
|
|
483
|
-
.find(({ file }) => /(?:checksums?|sha256)\.(?:txt|sha256)$/i.test(file))?.entry || '';
|
|
484
|
-
const assets = entries
|
|
485
|
-
.map(entry => {
|
|
486
|
-
const file = basenameFromUrl(entry);
|
|
487
|
-
const runtime = runtimeFromBundleName(file);
|
|
488
|
-
if (!runtime || !/^farming[-_].*\.tar\.gz$/i.test(file)) return null;
|
|
489
|
-
const browser_download_url = resolveUpdateUrl(entry, pageUrl);
|
|
490
|
-
if (!browser_download_url) return null;
|
|
491
|
-
return {
|
|
492
|
-
type: 'app-bundle',
|
|
493
|
-
name: file,
|
|
494
|
-
file,
|
|
495
|
-
releaseVersion: normalizeVersion(file),
|
|
496
|
-
browser_download_url,
|
|
497
|
-
checksum_url: resolveUpdateUrl(checksumUrl, pageUrl),
|
|
498
|
-
...runtime,
|
|
499
|
-
};
|
|
500
|
-
})
|
|
501
|
-
.filter(Boolean);
|
|
502
|
-
const latestVersion = assets[0]?.releaseVersion || '';
|
|
503
|
-
return {
|
|
504
|
-
__directAssets: true,
|
|
505
|
-
tag_name: latestVersion ? `v${latestVersion}` : '',
|
|
506
|
-
name: latestVersion ? `v${latestVersion}` : '',
|
|
507
|
-
published_at: '',
|
|
508
|
-
assets,
|
|
509
|
-
};
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
function releaseFromDirectoryListing(body, options = {}) {
|
|
513
|
-
const directoryUrl = options.directoryUrl || '';
|
|
514
|
-
const assetBaseUrl = options.assetBaseUrl || directoryUrl;
|
|
515
|
-
const entries = [];
|
|
516
|
-
const seen = new Set();
|
|
517
|
-
const addCandidate = (value) => {
|
|
518
|
-
const raw = decodeHtmlAttribute(value).trim();
|
|
519
|
-
if (!raw || seen.has(raw)) return;
|
|
520
|
-
seen.add(raw);
|
|
521
|
-
entries.push(raw);
|
|
522
|
-
};
|
|
523
|
-
|
|
524
|
-
for (const match of String(body || '').matchAll(/\bhref\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi)) {
|
|
525
|
-
addCandidate(match[1] || match[2] || match[3]);
|
|
526
|
-
}
|
|
527
|
-
for (const match of String(body || '').matchAll(/\bfarming[-_][^\s"'<>]+?\.tar\.gz(?![\w.])/gi)) {
|
|
528
|
-
addCandidate(match[0]);
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
const checksumUrls = new Map();
|
|
532
|
-
entries.forEach(entry => {
|
|
533
|
-
const file = basenameFromUrl(entry);
|
|
534
|
-
const match = /^(farming[-_].*\.tar\.gz)\.sha256$/i.exec(file);
|
|
535
|
-
if (!match) return;
|
|
536
|
-
const url = resolveUpdateUrl(entry, assetBaseUrl) || resolveUpdateUrl(file, assetBaseUrl);
|
|
537
|
-
if (url) checksumUrls.set(match[1], url);
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
const assets = entries
|
|
541
|
-
.map(entry => {
|
|
542
|
-
const file = basenameFromUrl(entry);
|
|
543
|
-
if (!/^farming[-_].*\.tar\.gz$/i.test(file)) return null;
|
|
544
|
-
if (/(sha256|checksum|checksums)/i.test(file)) return null;
|
|
545
|
-
const url = resolveUpdateUrl(entry, assetBaseUrl) || resolveUpdateUrl(file, assetBaseUrl);
|
|
546
|
-
if (!url) return null;
|
|
547
|
-
return {
|
|
548
|
-
type: 'app-bundle',
|
|
549
|
-
file,
|
|
550
|
-
name: file,
|
|
551
|
-
releaseVersion: normalizeVersion(file),
|
|
552
|
-
browser_download_url: url,
|
|
553
|
-
checksum_url: checksumUrls.get(file) || '',
|
|
554
|
-
size: 0,
|
|
555
|
-
};
|
|
556
|
-
})
|
|
557
|
-
.filter(Boolean)
|
|
558
|
-
.sort((left, right) => (
|
|
559
|
-
compareVersions(right.releaseVersion, left.releaseVersion) ||
|
|
560
|
-
String(right.name).localeCompare(String(left.name))
|
|
561
|
-
));
|
|
562
|
-
const latestVersion = assets[0] ? assets[0].releaseVersion : '';
|
|
563
|
-
return {
|
|
564
|
-
__manifest: {
|
|
565
|
-
releaseVersion: latestVersion,
|
|
566
|
-
assets: assets.map(asset => ({
|
|
567
|
-
type: asset.type,
|
|
568
|
-
file: asset.file,
|
|
569
|
-
url: asset.browser_download_url,
|
|
570
|
-
checksum_url: asset.checksum_url,
|
|
571
|
-
releaseVersion: asset.releaseVersion,
|
|
572
|
-
})),
|
|
573
|
-
},
|
|
574
|
-
tag_name: latestVersion ? `v${latestVersion}` : '',
|
|
575
|
-
name: latestVersion ? `v${latestVersion}` : '',
|
|
576
|
-
published_at: '',
|
|
577
|
-
assets,
|
|
578
|
-
};
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
async function downloadFile(url, outputPath, options = {}) {
|
|
582
|
-
const response = await requestWithRedirects(url, {
|
|
583
|
-
...options,
|
|
584
|
-
accept: 'application/octet-stream',
|
|
585
|
-
});
|
|
586
|
-
const responseLength = Number(response.headers['content-length']);
|
|
587
|
-
const expectedLength = Number(options.totalBytes);
|
|
588
|
-
const totalBytes = Number.isFinite(responseLength) && responseLength > 0
|
|
589
|
-
? responseLength
|
|
590
|
-
: (Number.isFinite(expectedLength) && expectedLength > 0 ? expectedLength : 0);
|
|
591
|
-
let receivedBytes = 0;
|
|
592
|
-
const trackProgress = new Transform({
|
|
593
|
-
transform(chunk, _encoding, callback) {
|
|
594
|
-
receivedBytes += chunk.length;
|
|
595
|
-
try {
|
|
596
|
-
if (typeof options.onProgress === 'function') {
|
|
597
|
-
options.onProgress({ receivedBytes, totalBytes });
|
|
598
|
-
}
|
|
599
|
-
callback(null, chunk);
|
|
600
|
-
} catch (error) {
|
|
601
|
-
callback(error);
|
|
602
|
-
}
|
|
603
|
-
},
|
|
604
|
-
});
|
|
605
|
-
await pipeline(response, trackProgress, fs.createWriteStream(outputPath));
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
function sha256File(filePath) {
|
|
609
|
-
const hash = crypto.createHash('sha256');
|
|
610
|
-
hash.update(fs.readFileSync(filePath));
|
|
611
|
-
return hash.digest('hex');
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
function sha256FromChecksumText(text, assetName) {
|
|
615
|
-
const escapedName = String(assetName || '').replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
616
|
-
const match = new RegExp(`^\\s*([a-f0-9]{64})\\s+(?:\\*?${escapedName})\\s*$`, 'im').exec(String(text || ''));
|
|
617
|
-
return normalizeSha256(match && match[1]);
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
function validateArchiveEntries(entries) {
|
|
621
|
-
const roots = new Set();
|
|
622
|
-
for (const rawEntry of Array.isArray(entries) ? entries : []) {
|
|
623
|
-
const entry = String(rawEntry || '').replace(/\\/g, '/');
|
|
624
|
-
if (!entry || entry.startsWith('/') || entry.includes('\0')) {
|
|
625
|
-
throw new Error(`downloaded release contains an unsafe archive path: ${rawEntry}`);
|
|
626
|
-
}
|
|
627
|
-
const parts = entry.split('/').filter(Boolean);
|
|
628
|
-
if (parts.some(part => part === '..')) {
|
|
629
|
-
throw new Error(`downloaded release contains path traversal: ${rawEntry}`);
|
|
630
|
-
}
|
|
631
|
-
if (parts[0]) roots.add(parts[0]);
|
|
632
|
-
}
|
|
633
|
-
if (roots.size !== 1) {
|
|
634
|
-
throw new Error(`downloaded release must contain exactly one top-level directory, found ${roots.size}`);
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
function listTarArchiveEntries(archivePath) {
|
|
639
|
-
return new Promise((resolve, reject) => {
|
|
640
|
-
childProcess.execFile('tar', ['-tzf', archivePath], { maxBuffer: 20 * 1024 * 1024 }, (error, stdout) => {
|
|
641
|
-
if (error) {
|
|
642
|
-
reject(new Error(`failed to inspect downloaded release archive: ${error.message || error}`));
|
|
643
|
-
return;
|
|
644
|
-
}
|
|
645
|
-
resolve(String(stdout || '').split(/\r?\n/).map(line => line.trim()).filter(Boolean));
|
|
646
|
-
});
|
|
647
|
-
});
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
function validateExtractedSymlinks(releaseDir) {
|
|
651
|
-
const releaseRoot = path.resolve(releaseDir);
|
|
652
|
-
const visit = directory => {
|
|
653
|
-
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
654
|
-
const entryPath = path.join(directory, entry.name);
|
|
655
|
-
if (entry.isSymbolicLink()) {
|
|
656
|
-
const target = fs.readlinkSync(entryPath);
|
|
657
|
-
const resolvedTarget = path.resolve(path.dirname(entryPath), target);
|
|
658
|
-
if (path.isAbsolute(target) || (resolvedTarget !== releaseRoot && !resolvedTarget.startsWith(`${releaseRoot}${path.sep}`))) {
|
|
659
|
-
throw new Error(`downloaded release contains a symlink outside the bundle: ${entryPath}`);
|
|
660
|
-
}
|
|
661
|
-
continue;
|
|
662
|
-
}
|
|
663
|
-
if (entry.isDirectory()) visit(entryPath);
|
|
664
|
-
}
|
|
665
|
-
};
|
|
666
|
-
visit(releaseRoot);
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
function findReleaseDirectory(rootDir) {
|
|
670
|
-
const entries = fs.readdirSync(rootDir, { withFileTypes: true });
|
|
671
|
-
const directory = entries.find(entry => entry.isDirectory());
|
|
672
|
-
if (!directory) {
|
|
673
|
-
throw new Error('downloaded release archive did not contain a directory');
|
|
674
|
-
}
|
|
675
|
-
return path.join(rootDir, directory.name);
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
function releaseInstallDir(rootDir, env = process.env) {
|
|
679
|
-
if (env.FARMING_INSTALL_DIR) return env.FARMING_INSTALL_DIR;
|
|
680
|
-
if (fs.existsSync(path.join(rootDir, '.farming.pid')) || fs.existsSync(path.join(rootDir, 'RELEASE.json'))) {
|
|
681
|
-
return rootDir;
|
|
682
|
-
}
|
|
683
|
-
return path.join(os.homedir(), 'farming');
|
|
684
|
-
}
|
|
685
|
-
|
|
686
162
|
function nodeScriptInvocation(nodePath, scriptPath, env = process.env) {
|
|
687
163
|
if (env.FARMING_NODE_LD && env.FARMING_NODE_LIBRARY_PATH) {
|
|
688
164
|
return {
|
|
@@ -762,11 +238,6 @@ function npmVersionsFromMetadata(metadata, currentVersion) {
|
|
|
762
238
|
class FarmingUpdateService {
|
|
763
239
|
constructor(options = {}) {
|
|
764
240
|
this.rootDir = options.rootDir || path.join(__dirname, '..');
|
|
765
|
-
this.getUpdateUrl = typeof options.getUpdateUrl === 'function' ? options.getUpdateUrl : null;
|
|
766
|
-
this.manifestUrl = options.updateUrl || options.manifestUrl || '';
|
|
767
|
-
this.assetBaseUrl = options.assetBaseUrl || '';
|
|
768
|
-
this.assetPattern = options.assetPattern || '';
|
|
769
|
-
this.authToken = options.authToken || '';
|
|
770
241
|
this.installMethod = options.installMethod || detectInstallMethod(this.rootDir, {
|
|
771
242
|
packagedRuntime: options.packagedRuntime === true,
|
|
772
243
|
});
|
|
@@ -783,23 +254,10 @@ class FarmingUpdateService {
|
|
|
783
254
|
this.configDir = options.configDir || path.join(os.homedir(), '.farming');
|
|
784
255
|
this.now = options.now || (() => Date.now());
|
|
785
256
|
this.fetchJson = options.fetchJson || requestJson;
|
|
786
|
-
this.fetchText = options.fetchText || requestText;
|
|
787
|
-
this.downloadFile = options.downloadFile || downloadFile;
|
|
788
|
-
this.listArchiveEntries = options.listArchiveEntries || listTarArchiveEntries;
|
|
789
257
|
this.execFile = options.execFile || childProcess.execFile;
|
|
790
258
|
this.getNpmGlobalRoot = options.getNpmGlobalRoot
|
|
791
259
|
|| ((npmCommand, npmPrefix) => readNpmGlobalRoot(npmCommand, npmPrefix, this.execFile));
|
|
792
260
|
this.spawn = options.spawn || childProcess.spawn;
|
|
793
|
-
this.prepareRuntimeDependencies = options.prepareRuntimeDependencies || (async (releaseDir) => {
|
|
794
|
-
if (process.env.NODE_ENV === 'test') return;
|
|
795
|
-
const managerPath = path.join(releaseDir, 'backend', 'runtime-dependency-manager.js');
|
|
796
|
-
const { prepareRuntimeDependencies } = require(managerPath);
|
|
797
|
-
await prepareRuntimeDependencies({
|
|
798
|
-
configDir: this.configDir,
|
|
799
|
-
env: { ...process.env, FARMING_CONFIG_DIR: this.configDir },
|
|
800
|
-
});
|
|
801
|
-
});
|
|
802
|
-
this.latestCache = null;
|
|
803
261
|
this.npmCache = null;
|
|
804
262
|
this.installState = { phase: 'idle' };
|
|
805
263
|
this.installStartPromise = null;
|
|
@@ -808,11 +266,6 @@ class FarmingUpdateService {
|
|
|
808
266
|
this.updateStagingDir = options.updateStagingDir || storageLayout.updateStagingDir(this.configDir);
|
|
809
267
|
}
|
|
810
268
|
|
|
811
|
-
updateUrl() {
|
|
812
|
-
const configured = this.getUpdateUrl ? this.getUpdateUrl() : this.manifestUrl;
|
|
813
|
-
return String(configured || '').trim();
|
|
814
|
-
}
|
|
815
|
-
|
|
816
269
|
currentVersion() {
|
|
817
270
|
const release = readJsonFile(path.join(this.rootDir, 'RELEASE.json')) || {};
|
|
818
271
|
const pkg = readJsonFile(path.join(this.rootDir, 'package.json')) || {};
|
|
@@ -825,7 +278,7 @@ class FarmingUpdateService {
|
|
|
825
278
|
compatibilityProfile: String(release.compatibilityProfile || ''),
|
|
826
279
|
bundledGlibcRuntime: release.bundledGlibcRuntime === true,
|
|
827
280
|
type: this.installMethod,
|
|
828
|
-
installDir: this.
|
|
281
|
+
installDir: this.rootDir,
|
|
829
282
|
};
|
|
830
283
|
}
|
|
831
284
|
|
|
@@ -972,157 +425,9 @@ class FarmingUpdateService {
|
|
|
972
425
|
};
|
|
973
426
|
}
|
|
974
427
|
|
|
975
|
-
async latestRelease(options = {}) {
|
|
976
|
-
const updateUrl = this.updateUrl();
|
|
977
|
-
if (!updateUrl) return null;
|
|
978
|
-
if (!options.force && this.latestCache && this.latestCache.source === updateUrl && this.now() - this.latestCache.checkedAt < CACHE_TTL_MS) {
|
|
979
|
-
return this.latestCache.release;
|
|
980
|
-
}
|
|
981
|
-
let release;
|
|
982
|
-
if (isGitHubReleasePage(updateUrl)) {
|
|
983
|
-
const pageBody = await this.fetchText(updateUrl, {
|
|
984
|
-
accept: 'text/html,application/xhtml+xml',
|
|
985
|
-
authToken: this.authToken,
|
|
986
|
-
});
|
|
987
|
-
const tag = githubReleaseTagFromPage(pageBody);
|
|
988
|
-
const expandedAssetsUrl = githubExpandedAssetsUrl(updateUrl, tag);
|
|
989
|
-
if (!expandedAssetsUrl) throw new Error('GitHub Release page did not expose a release tag');
|
|
990
|
-
release = releaseFromGitHubReleasePage(await this.fetchText(expandedAssetsUrl, {
|
|
991
|
-
accept: 'text/html,application/xhtml+xml',
|
|
992
|
-
authToken: this.authToken,
|
|
993
|
-
}), { pageUrl: expandedAssetsUrl });
|
|
994
|
-
} else if (isDirectoryUpdateSource(updateUrl)) {
|
|
995
|
-
release = releaseFromDirectoryListing(await this.fetchText(updateUrl, {
|
|
996
|
-
accept: 'text/html, text/plain',
|
|
997
|
-
authToken: this.authToken,
|
|
998
|
-
}), {
|
|
999
|
-
directoryUrl: updateUrl,
|
|
1000
|
-
assetBaseUrl: this.assetBaseUrl,
|
|
1001
|
-
});
|
|
1002
|
-
} else {
|
|
1003
|
-
const payload = await this.fetchJson(updateUrl, {
|
|
1004
|
-
accept: 'application/json',
|
|
1005
|
-
authToken: this.authToken,
|
|
1006
|
-
});
|
|
1007
|
-
release = Array.isArray(payload && payload.assets) && payload.assets.some(asset => asset && asset.browser_download_url)
|
|
1008
|
-
? releaseFromAssetListing(payload)
|
|
1009
|
-
: releaseFromManifest(payload, {
|
|
1010
|
-
manifestUrl: updateUrl,
|
|
1011
|
-
assetBaseUrl: this.assetBaseUrl,
|
|
1012
|
-
});
|
|
1013
|
-
}
|
|
1014
|
-
this.latestCache = {
|
|
1015
|
-
checkedAt: this.now(),
|
|
1016
|
-
source: updateUrl,
|
|
1017
|
-
release,
|
|
1018
|
-
};
|
|
1019
|
-
return release;
|
|
1020
|
-
}
|
|
1021
|
-
|
|
1022
|
-
async latestManifest(release) {
|
|
1023
|
-
if (release && release.__directAssets) return null;
|
|
1024
|
-
if (release && release.__manifest) return release.__manifest;
|
|
1025
|
-
const asset = selectManifestAsset(release);
|
|
1026
|
-
if (!asset) return null;
|
|
1027
|
-
try {
|
|
1028
|
-
return await this.fetchJson(asset.browser_download_url, {
|
|
1029
|
-
authToken: this.authToken,
|
|
1030
|
-
});
|
|
1031
|
-
} catch {
|
|
1032
|
-
return null;
|
|
1033
|
-
}
|
|
1034
|
-
}
|
|
1035
|
-
|
|
1036
|
-
versionOptionsFromRelease(release, manifest = null) {
|
|
1037
|
-
const current = this.currentVersion();
|
|
1038
|
-
const updateUrl = this.updateUrl();
|
|
1039
|
-
const comparableCurrentVersion = normalizeVersion(current.releaseVersion || current.packageVersion);
|
|
1040
|
-
const latestVersion = releaseVersionFromRelease(release);
|
|
1041
|
-
const configured = Boolean(updateUrl);
|
|
1042
|
-
return selectableReleaseAssets(release, this.assetPattern, this.runtime, current.compatibilityProfile).map(asset => {
|
|
1043
|
-
const version = releaseAssetVersion(asset, latestVersion);
|
|
1044
|
-
const safety = configured
|
|
1045
|
-
? manifestAssetSafety(asset, manifest, { runtime: this.runtime })
|
|
1046
|
-
: { safe: false, reason: UPDATE_SOURCE_UNCONFIGURED_REASON };
|
|
1047
|
-
const newer = Boolean(version && compareVersions(version, comparableCurrentVersion) > 0);
|
|
1048
|
-
return {
|
|
1049
|
-
version,
|
|
1050
|
-
assetName: asset.name || '',
|
|
1051
|
-
assetSize: asset.size || 0,
|
|
1052
|
-
blockedReason: safety.reason,
|
|
1053
|
-
installable: Boolean(safety.safe),
|
|
1054
|
-
available: Boolean(newer && safety.safe),
|
|
1055
|
-
};
|
|
1056
|
-
});
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
statusFromRelease(release, manifest = null, options = {}) {
|
|
1060
|
-
const current = this.currentVersion();
|
|
1061
|
-
const updateUrl = this.updateUrl();
|
|
1062
|
-
const latestVersion = releaseVersionFromRelease(release);
|
|
1063
|
-
const comparableCurrentVersion = normalizeVersion(current.releaseVersion || current.packageVersion);
|
|
1064
|
-
const latestAsset = selectableReleaseAssets(release, this.assetPattern, this.runtime, current.compatibilityProfile)[0] || null;
|
|
1065
|
-
const asset = selectReleaseAssetByName(release, options.assetName, this.assetPattern, this.runtime, current.compatibilityProfile);
|
|
1066
|
-
const configured = Boolean(updateUrl);
|
|
1067
|
-
const safety = configured
|
|
1068
|
-
? manifestAssetSafety(asset, manifest, { runtime: this.runtime })
|
|
1069
|
-
: { safe: false, reason: UPDATE_SOURCE_UNCONFIGURED_REASON };
|
|
1070
|
-
const selectedVersion = releaseAssetVersion(asset, latestVersion);
|
|
1071
|
-
const newer = Boolean(asset && selectedVersion && compareVersions(selectedVersion, comparableCurrentVersion) > 0);
|
|
1072
|
-
const available = Boolean(newer && safety.safe);
|
|
1073
|
-
const versions = this.versionOptionsFromRelease(release, manifest);
|
|
1074
|
-
const noCompatibleBundleReason = this.runtime
|
|
1075
|
-
? `Update source has no compatible app bundle for ${this.runtime.platform}-${this.runtime.arch}`
|
|
1076
|
-
: 'Update source has no app bundle asset';
|
|
1077
|
-
|
|
1078
|
-
return {
|
|
1079
|
-
method: this.installMethod,
|
|
1080
|
-
current,
|
|
1081
|
-
latest: {
|
|
1082
|
-
version: latestVersion,
|
|
1083
|
-
tag: release && release.tag_name ? release.tag_name : '',
|
|
1084
|
-
name: release && release.name ? release.name : '',
|
|
1085
|
-
publishedAt: release && release.published_at ? release.published_at : '',
|
|
1086
|
-
assetName: latestAsset ? latestAsset.name : '',
|
|
1087
|
-
assetSize: latestAsset ? latestAsset.size || 0 : 0,
|
|
1088
|
-
blockedReason: versions[0] ? versions[0].blockedReason : (this.runtime ? noCompatibleBundleReason : (safety.reason || noCompatibleBundleReason)),
|
|
1089
|
-
source: updateUrl || '',
|
|
1090
|
-
},
|
|
1091
|
-
selected: {
|
|
1092
|
-
version: selectedVersion,
|
|
1093
|
-
assetName: asset ? asset.name : '',
|
|
1094
|
-
assetSize: asset ? asset.size || 0 : 0,
|
|
1095
|
-
blockedReason: safety.reason,
|
|
1096
|
-
},
|
|
1097
|
-
versions,
|
|
1098
|
-
runtime: this.runtime,
|
|
1099
|
-
available,
|
|
1100
|
-
installable: Boolean(asset && safety.safe),
|
|
1101
|
-
checkedAt: new Date(this.now()).toISOString(),
|
|
1102
|
-
state: this.currentInstallState(),
|
|
1103
|
-
};
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
428
|
async check(options = {}) {
|
|
1107
429
|
if (this.installMethod === 'npm') return this.npmStatus(options);
|
|
1108
|
-
|
|
1109
|
-
if (!this.updateUrl()) return this.statusFromRelease(null, null, options);
|
|
1110
|
-
const release = await this.latestRelease(options);
|
|
1111
|
-
const manifest = await this.latestManifest(release);
|
|
1112
|
-
return this.statusFromRelease(release, manifest, options);
|
|
1113
|
-
}
|
|
1114
|
-
|
|
1115
|
-
installEnvironment() {
|
|
1116
|
-
const current = this.currentVersion();
|
|
1117
|
-
return {
|
|
1118
|
-
...process.env,
|
|
1119
|
-
FARMING_INSTALL_DIR: current.installDir,
|
|
1120
|
-
FARMING_PORT: process.env.FARMING_PORT || process.env.PORT || '6694',
|
|
1121
|
-
FARMING_BASE_PATH: process.env.FARMING_BASE_PATH || '/farming',
|
|
1122
|
-
FARMING_CONFIG_DIR: process.env.FARMING_CONFIG_DIR || this.configDir,
|
|
1123
|
-
...(process.env.FARMING_SERVER_HOME ? { FARMING_SERVER_HOME: process.env.FARMING_SERVER_HOME } : {}),
|
|
1124
|
-
...(process.env.FARMING_DISABLE_AUTH ? { FARMING_DISABLE_AUTH: process.env.FARMING_DISABLE_AUTH } : {}),
|
|
1125
|
-
};
|
|
430
|
+
return this.unsupportedStatus();
|
|
1126
431
|
}
|
|
1127
432
|
|
|
1128
433
|
async startInstall(options = {}) {
|
|
@@ -1144,52 +449,7 @@ class FarmingUpdateService {
|
|
|
1144
449
|
if (currentState.phase === 'ready-to-restart') return currentState;
|
|
1145
450
|
|
|
1146
451
|
if (this.installMethod === 'npm') return this.startNpmInstall(options);
|
|
1147
|
-
|
|
1148
|
-
throw new Error(installMethodBlockedReason(this.installMethod));
|
|
1149
|
-
}
|
|
1150
|
-
|
|
1151
|
-
const release = await this.latestRelease({ force: true });
|
|
1152
|
-
const manifest = await this.latestManifest(release);
|
|
1153
|
-
const status = this.statusFromRelease(release, manifest, options);
|
|
1154
|
-
if (!status.available) {
|
|
1155
|
-
const error = status.installable
|
|
1156
|
-
? 'Farming is already up to date'
|
|
1157
|
-
: (status.latest.blockedReason || 'Update source has no app bundle asset');
|
|
1158
|
-
this.installState = {
|
|
1159
|
-
phase: 'failed',
|
|
1160
|
-
error,
|
|
1161
|
-
checkedAt: status.checkedAt,
|
|
1162
|
-
};
|
|
1163
|
-
return this.installState;
|
|
1164
|
-
}
|
|
1165
|
-
|
|
1166
|
-
const asset = selectReleaseAssetByName(
|
|
1167
|
-
release,
|
|
1168
|
-
options.assetName,
|
|
1169
|
-
this.assetPattern,
|
|
1170
|
-
this.runtime,
|
|
1171
|
-
this.currentVersion().compatibilityProfile,
|
|
1172
|
-
);
|
|
1173
|
-
this.persistInstallState({
|
|
1174
|
-
phase: 'downloading',
|
|
1175
|
-
method: this.installMethod,
|
|
1176
|
-
version: status.selected.version,
|
|
1177
|
-
previousVersion: status.current.releaseVersion || status.current.packageVersion,
|
|
1178
|
-
assetName: status.selected.assetName,
|
|
1179
|
-
receivedBytes: 0,
|
|
1180
|
-
totalBytes: Number(status.selected.assetSize) || 0,
|
|
1181
|
-
startedAt: new Date(this.now()).toISOString(),
|
|
1182
|
-
logPath: path.join(this.configDir, 'farming-update.log'),
|
|
1183
|
-
});
|
|
1184
|
-
void this.runInstall(asset).catch(error => {
|
|
1185
|
-
this.persistInstallState({
|
|
1186
|
-
...this.installState,
|
|
1187
|
-
phase: 'failed',
|
|
1188
|
-
error: error.message || String(error),
|
|
1189
|
-
completedAt: new Date(this.now()).toISOString(),
|
|
1190
|
-
});
|
|
1191
|
-
});
|
|
1192
|
-
return this.installState;
|
|
452
|
+
throw new Error(installMethodBlockedReason(this.installMethod));
|
|
1193
453
|
}
|
|
1194
454
|
|
|
1195
455
|
async startNpmInstall(options = {}) {
|
|
@@ -1297,6 +557,9 @@ class FarmingUpdateService {
|
|
|
1297
557
|
}
|
|
1298
558
|
|
|
1299
559
|
async applyPreparedUpdateUnreserved() {
|
|
560
|
+
if (this.installMethod !== 'npm') {
|
|
561
|
+
throw new Error(installMethodBlockedReason(this.installMethod));
|
|
562
|
+
}
|
|
1300
563
|
const prepared = this.currentInstallState();
|
|
1301
564
|
if (prepared.phase !== 'ready-to-restart') {
|
|
1302
565
|
throw new Error('No prepared Farming update is ready to restart');
|
|
@@ -1314,94 +577,53 @@ class FarmingUpdateService {
|
|
|
1314
577
|
if (!serverProcessIdentity) {
|
|
1315
578
|
throw new Error('Running Farming server process identity could not be verified');
|
|
1316
579
|
}
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
}
|
|
1326
|
-
const stagingPrefix = normalizePathForCompare(prepared.stagingPrefix);
|
|
1327
|
-
if (!pathIsInside(normalizePathForCompare(this.updateStagingDir), stagingPrefix)) {
|
|
1328
|
-
throw new Error('Prepared npm update is outside the Farming staging directory');
|
|
1329
|
-
}
|
|
1330
|
-
if (path.resolve(prepared.stagingPackageRoot) !== path.resolve(npmPackageRoot(
|
|
1331
|
-
path.join(prepared.stagingPrefix, 'lib', 'node_modules'),
|
|
1332
|
-
this.npmPackageName,
|
|
1333
|
-
))) {
|
|
1334
|
-
throw new Error('Prepared npm update has an invalid package root');
|
|
1335
|
-
}
|
|
1336
|
-
const stagingPackageRoot = normalizePathForCompare(prepared.stagingPackageRoot);
|
|
1337
|
-
const stagedMetadata = readJsonFile(path.join(stagingPackageRoot, 'package.json')) || {};
|
|
1338
|
-
if (normalizeVersion(stagedMetadata.version) !== normalizeVersion(prepared.version)) {
|
|
1339
|
-
throw new Error('Prepared npm update is no longer available; prepare it again');
|
|
1340
|
-
}
|
|
1341
|
-
helperPath = path.join(__dirname, 'npm-update-helper.js');
|
|
1342
|
-
payload = {
|
|
1343
|
-
action: 'apply',
|
|
1344
|
-
packageName: this.npmPackageName,
|
|
1345
|
-
targetVersion: prepared.version,
|
|
1346
|
-
previousVersion: prepared.previousVersion,
|
|
1347
|
-
startedAt: prepared.startedAt,
|
|
1348
|
-
preparedAt: prepared.preparedAt,
|
|
1349
|
-
stateFile: this.updateStateFile,
|
|
1350
|
-
logPath: prepared.logPath || this.updateLogFile,
|
|
1351
|
-
cliPath: path.join(this.rootDir, 'bin', 'farming'),
|
|
1352
|
-
packageRoot: target.packageRoot,
|
|
1353
|
-
nodePath,
|
|
1354
|
-
npmCommand: process.env.FARMING_NPM_COMMAND || 'npm',
|
|
1355
|
-
npmPrefix: target.npmPrefix,
|
|
1356
|
-
npmFallbackRegistryUrl: this.npmRegistryUrl,
|
|
1357
|
-
stagingPrefix,
|
|
1358
|
-
stagingPackageRoot,
|
|
1359
|
-
serverPid: process.pid,
|
|
1360
|
-
serverProcessIdentity,
|
|
1361
|
-
configDir: this.configDir,
|
|
1362
|
-
port: process.env.FARMING_PORT || process.env.PORT || '6694',
|
|
1363
|
-
basePath: process.env.FARMING_BASE_PATH || '/farming',
|
|
1364
|
-
serverHome: process.env.FARMING_SERVER_HOME || '',
|
|
1365
|
-
disableAuth: /^(1|true|yes|on)$/i.test(String(process.env.FARMING_DISABLE_AUTH || '')),
|
|
1366
|
-
};
|
|
1367
|
-
env = { ...process.env, FARMING_NPM_UPDATE_PAYLOAD: JSON.stringify(payload) };
|
|
1368
|
-
} else {
|
|
1369
|
-
if (!installMethodAllowsBundleUpdate(this.installMethod)) {
|
|
1370
|
-
throw new Error(installMethodBlockedReason(this.installMethod));
|
|
1371
|
-
}
|
|
1372
|
-
if (!path.isAbsolute(String(prepared.releaseDir || '')) || !path.isAbsolute(String(prepared.installer || '')) || !fs.existsSync(prepared.installer)) {
|
|
1373
|
-
throw new Error('Prepared bundle update is no longer available; prepare it again');
|
|
1374
|
-
}
|
|
1375
|
-
const releaseDir = normalizePathForCompare(prepared.releaseDir);
|
|
1376
|
-
const installer = normalizePathForCompare(prepared.installer);
|
|
1377
|
-
if (!pathIsInside(normalizePathForCompare(os.tmpdir()), releaseDir)
|
|
1378
|
-
|| !path.basename(path.dirname(releaseDir)).startsWith('farming-update.')
|
|
1379
|
-
|| installer !== normalizePathForCompare(path.join(releaseDir, 'scripts', 'install-release.sh'))) {
|
|
1380
|
-
throw new Error('Prepared bundle update has an invalid staging identity');
|
|
1381
|
-
}
|
|
1382
|
-
const releaseMetadata = readJsonFile(path.join(releaseDir, 'RELEASE.json')) || {};
|
|
1383
|
-
const preparedVersion = normalizeVersion(releaseMetadata.releaseVersion || releaseMetadata.packageVersion);
|
|
1384
|
-
if (preparedVersion && preparedVersion !== normalizeVersion(prepared.version)) {
|
|
1385
|
-
throw new Error('Prepared bundle version does not match the selected update');
|
|
1386
|
-
}
|
|
1387
|
-
if (!assetMatchesRuntime(releaseMetadata, this.runtime)) {
|
|
1388
|
-
throw new Error(`Prepared bundle is not compatible with ${this.runtime.platform}-${this.runtime.arch}`);
|
|
1389
|
-
}
|
|
1390
|
-
helperPath = path.join(__dirname, 'bundle-update-helper.js');
|
|
1391
|
-
payload = {
|
|
1392
|
-
method: this.installMethod,
|
|
1393
|
-
targetMethod: prepared.targetMethod,
|
|
1394
|
-
version: prepared.version,
|
|
1395
|
-
previousVersion: prepared.previousVersion,
|
|
1396
|
-
startedAt: prepared.startedAt,
|
|
1397
|
-
preparedAt: prepared.preparedAt,
|
|
1398
|
-
stateFile: this.updateStateFile,
|
|
1399
|
-
logPath: prepared.logPath || this.updateLogFile,
|
|
1400
|
-
releaseDir,
|
|
1401
|
-
installer,
|
|
1402
|
-
};
|
|
1403
|
-
env = { ...this.installEnvironment(), FARMING_BUNDLE_UPDATE_PAYLOAD: JSON.stringify(payload) };
|
|
580
|
+
const target = await this.npmUpdateTarget();
|
|
581
|
+
if (!target.proven) throw new Error(target.error);
|
|
582
|
+
if (!path.isAbsolute(String(prepared.stagingPrefix || '')) || !path.isAbsolute(String(prepared.stagingPackageRoot || ''))) {
|
|
583
|
+
throw new Error('Prepared npm update is missing its staging identity');
|
|
584
|
+
}
|
|
585
|
+
const stagingPrefix = normalizePathForCompare(prepared.stagingPrefix);
|
|
586
|
+
if (!pathIsInside(normalizePathForCompare(this.updateStagingDir), stagingPrefix)) {
|
|
587
|
+
throw new Error('Prepared npm update is outside the Farming staging directory');
|
|
1404
588
|
}
|
|
589
|
+
if (path.resolve(prepared.stagingPackageRoot) !== path.resolve(npmPackageRoot(
|
|
590
|
+
path.join(prepared.stagingPrefix, 'lib', 'node_modules'),
|
|
591
|
+
this.npmPackageName,
|
|
592
|
+
))) {
|
|
593
|
+
throw new Error('Prepared npm update has an invalid package root');
|
|
594
|
+
}
|
|
595
|
+
const stagingPackageRoot = normalizePathForCompare(prepared.stagingPackageRoot);
|
|
596
|
+
const stagedMetadata = readJsonFile(path.join(stagingPackageRoot, 'package.json')) || {};
|
|
597
|
+
if (normalizeVersion(stagedMetadata.version) !== normalizeVersion(prepared.version)) {
|
|
598
|
+
throw new Error('Prepared npm update is no longer available; prepare it again');
|
|
599
|
+
}
|
|
600
|
+
const helperPath = path.join(__dirname, 'npm-update-helper.js');
|
|
601
|
+
const payload = {
|
|
602
|
+
action: 'apply',
|
|
603
|
+
packageName: this.npmPackageName,
|
|
604
|
+
targetVersion: prepared.version,
|
|
605
|
+
previousVersion: prepared.previousVersion,
|
|
606
|
+
startedAt: prepared.startedAt,
|
|
607
|
+
preparedAt: prepared.preparedAt,
|
|
608
|
+
stateFile: this.updateStateFile,
|
|
609
|
+
logPath: prepared.logPath || this.updateLogFile,
|
|
610
|
+
cliPath: path.join(this.rootDir, 'bin', 'farming'),
|
|
611
|
+
packageRoot: target.packageRoot,
|
|
612
|
+
nodePath,
|
|
613
|
+
npmCommand: process.env.FARMING_NPM_COMMAND || 'npm',
|
|
614
|
+
npmPrefix: target.npmPrefix,
|
|
615
|
+
npmFallbackRegistryUrl: this.npmRegistryUrl,
|
|
616
|
+
stagingPrefix,
|
|
617
|
+
stagingPackageRoot,
|
|
618
|
+
serverPid: process.pid,
|
|
619
|
+
serverProcessIdentity,
|
|
620
|
+
configDir: this.configDir,
|
|
621
|
+
port: process.env.FARMING_PORT || process.env.PORT || '6694',
|
|
622
|
+
basePath: process.env.FARMING_BASE_PATH || '/farming',
|
|
623
|
+
serverHome: process.env.FARMING_SERVER_HOME || '',
|
|
624
|
+
disableAuth: /^(1|true|yes|on)$/i.test(String(process.env.FARMING_DISABLE_AUTH || '')),
|
|
625
|
+
};
|
|
626
|
+
const env = { ...process.env, FARMING_NPM_UPDATE_PAYLOAD: JSON.stringify(payload) };
|
|
1405
627
|
|
|
1406
628
|
const state = this.persistInstallState({ ...prepared, phase: 'restarting', error: '' });
|
|
1407
629
|
const helperInvocation = nodeScriptInvocation(nodePath, helperPath);
|
|
@@ -1428,118 +650,16 @@ class FarmingUpdateService {
|
|
|
1428
650
|
return state;
|
|
1429
651
|
}
|
|
1430
652
|
|
|
1431
|
-
async runInstall(asset) {
|
|
1432
|
-
if (!asset || !asset.browser_download_url) {
|
|
1433
|
-
throw new Error('release asset is missing a download URL');
|
|
1434
|
-
}
|
|
1435
|
-
|
|
1436
|
-
fs.mkdirSync(this.configDir, { recursive: true });
|
|
1437
|
-
const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'farming-update.'));
|
|
1438
|
-
const archivePath = path.join(tempRoot, asset.name || 'farming-update.tar.gz');
|
|
1439
|
-
let lastProgressPercent = -1;
|
|
1440
|
-
let lastProgressBytes = 0;
|
|
1441
|
-
await this.downloadFile(asset.browser_download_url, archivePath, {
|
|
1442
|
-
authToken: this.authToken,
|
|
1443
|
-
totalBytes: Number(asset.size) || Number(this.installState.totalBytes) || 0,
|
|
1444
|
-
onProgress: ({ receivedBytes, totalBytes }) => {
|
|
1445
|
-
const safeReceivedBytes = Math.max(0, Number(receivedBytes) || 0);
|
|
1446
|
-
const safeTotalBytes = Math.max(0, Number(totalBytes) || 0);
|
|
1447
|
-
if (safeTotalBytes > 0) {
|
|
1448
|
-
const percent = Math.min(100, Math.floor((safeReceivedBytes / safeTotalBytes) * 100));
|
|
1449
|
-
if (percent === lastProgressPercent && safeReceivedBytes < safeTotalBytes) return;
|
|
1450
|
-
lastProgressPercent = percent;
|
|
1451
|
-
} else if (safeReceivedBytes - lastProgressBytes < 1024 * 1024) {
|
|
1452
|
-
return;
|
|
1453
|
-
}
|
|
1454
|
-
lastProgressBytes = safeReceivedBytes;
|
|
1455
|
-
this.persistInstallState({
|
|
1456
|
-
...this.installState,
|
|
1457
|
-
receivedBytes: safeReceivedBytes,
|
|
1458
|
-
totalBytes: safeTotalBytes,
|
|
1459
|
-
});
|
|
1460
|
-
},
|
|
1461
|
-
});
|
|
1462
|
-
let expectedSha256 = normalizeSha256(asset.sha256);
|
|
1463
|
-
const checksumUrl = asset.checksum_url || asset.checksumUrl || '';
|
|
1464
|
-
if (!expectedSha256 && checksumUrl) {
|
|
1465
|
-
const checksumText = await this.fetchText(checksumUrl, {
|
|
1466
|
-
accept: 'text/plain',
|
|
1467
|
-
authToken: this.authToken,
|
|
1468
|
-
});
|
|
1469
|
-
expectedSha256 = sha256FromChecksumText(checksumText, asset.name);
|
|
1470
|
-
}
|
|
1471
|
-
if (!expectedSha256) {
|
|
1472
|
-
throw new Error(`release asset is missing a valid SHA-256 checksum: ${asset.name || 'asset'}`);
|
|
1473
|
-
}
|
|
1474
|
-
const actualSha256 = sha256File(archivePath);
|
|
1475
|
-
if (actualSha256 !== expectedSha256) {
|
|
1476
|
-
throw new Error(`downloaded release checksum mismatch for ${asset.name || 'asset'}`);
|
|
1477
|
-
}
|
|
1478
|
-
|
|
1479
|
-
validateArchiveEntries(await this.listArchiveEntries(archivePath));
|
|
1480
|
-
|
|
1481
|
-
this.persistInstallState({ ...this.installState, phase: 'extracting' });
|
|
1482
|
-
await new Promise((resolve, reject) => {
|
|
1483
|
-
this.execFile('tar', ['-xzf', archivePath, '-C', tempRoot], (error) => {
|
|
1484
|
-
if (error) reject(error);
|
|
1485
|
-
else resolve();
|
|
1486
|
-
});
|
|
1487
|
-
});
|
|
1488
|
-
|
|
1489
|
-
const releaseDir = findReleaseDirectory(tempRoot);
|
|
1490
|
-
validateExtractedSymlinks(releaseDir);
|
|
1491
|
-
const releaseMetadata = readJsonFile(path.join(releaseDir, 'RELEASE.json')) || {};
|
|
1492
|
-
if (!assetMatchesRuntime(releaseMetadata, this.runtime)) {
|
|
1493
|
-
throw new Error(`downloaded release is not compatible with ${this.runtime.platform}-${this.runtime.arch}`);
|
|
1494
|
-
}
|
|
1495
|
-
const installer = path.join(releaseDir, 'scripts', 'install-release.sh');
|
|
1496
|
-
if (!fs.existsSync(installer)) {
|
|
1497
|
-
throw new Error('downloaded release is missing scripts/install-release.sh');
|
|
1498
|
-
}
|
|
1499
|
-
const logPath = this.installState.logPath || path.join(this.configDir, 'farming-update.log');
|
|
1500
|
-
const targetMethod = String(releaseMetadata.updateMethod || releaseMetadata.type || this.installMethod);
|
|
1501
|
-
this.persistInstallState({
|
|
1502
|
-
...this.installState,
|
|
1503
|
-
phase: 'preparing-runtimes',
|
|
1504
|
-
targetMethod,
|
|
1505
|
-
releaseDir,
|
|
1506
|
-
installer,
|
|
1507
|
-
logPath,
|
|
1508
|
-
});
|
|
1509
|
-
await this.prepareRuntimeDependencies(releaseDir);
|
|
1510
|
-
const runtimePreparedAt = new Date(this.now()).toISOString();
|
|
1511
|
-
this.persistInstallState({
|
|
1512
|
-
...this.installState,
|
|
1513
|
-
phase: 'ready-to-restart',
|
|
1514
|
-
targetMethod,
|
|
1515
|
-
releaseDir,
|
|
1516
|
-
installer,
|
|
1517
|
-
logPath,
|
|
1518
|
-
preparedAt: runtimePreparedAt,
|
|
1519
|
-
runtimePreparedAt,
|
|
1520
|
-
});
|
|
1521
|
-
}
|
|
1522
653
|
}
|
|
1523
654
|
|
|
1524
655
|
module.exports = {
|
|
1525
656
|
FarmingUpdateService,
|
|
1526
657
|
compareVersions,
|
|
1527
658
|
detectInstallMethod,
|
|
1528
|
-
installMethodAllowsBundleUpdate,
|
|
1529
659
|
normalizeVersion,
|
|
1530
660
|
npmPackageMetadataUrl,
|
|
1531
661
|
npmPrefixForPackageRoot,
|
|
1532
662
|
npmPackageRoot,
|
|
1533
663
|
npmVersionsFromMetadata,
|
|
1534
664
|
readNpmGlobalRoot,
|
|
1535
|
-
releaseInstallDir,
|
|
1536
|
-
manifestAssetSafety,
|
|
1537
|
-
normalizeSha256,
|
|
1538
|
-
validateArchiveEntries,
|
|
1539
|
-
releaseFromManifest,
|
|
1540
|
-
releaseFromDirectoryListing,
|
|
1541
|
-
releaseFromGitHubReleasePage,
|
|
1542
|
-
releaseVersionFromManifest,
|
|
1543
|
-
selectManifestAsset,
|
|
1544
|
-
selectReleaseAsset,
|
|
1545
665
|
};
|