Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: replace an older injected get-uri FTP patch when the patch changes (#9462)

Adam Eivy826dc210d8bd
diff --git a/scripts/lib/getUriFtpPatch.js b/scripts/lib/getUriFtpPatch.js
index fc7f860c86..66edf01c1e 100644
--- a/scripts/lib/getUriFtpPatch.js
+++ b/scripts/lib/getUriFtpPatch.js
@@ -30,7 +30,8 @@ import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
export const GET_URI_PATCH_TARGET_VERSION = '6.0.5';
-export const GET_URI_PATCH_MARKER = '/* portos-patch #9462: unix LIST modified date */';
+const MARKER_BEGIN = '/* portos-patch #9462 begin';
+const MARKER_END = '/* portos-patch #9462 end */\n';
const ANCHOR = 'lastModified = entry.modifiedAt;';
const HELPER_ANCHOR = 'const ftp = ';
@@ -88,25 +89,39 @@ export function parseUnixListDate(raw, now = Date.now()) {
throw fail();
}
+/** Changes whenever the injected code or its target does — see trusted-rebuild-stamp.js. */
+export const patchFingerprint = () => createHash('sha256')
+ .update([GET_URI_PATCH_TARGET_VERSION, ANCHOR, PATCHED, parseUnixListDate.toString()].join('\0'))
+ .digest('hex')
+ .slice(0, 16);
+
+// Reverts a previously injected patch so a changed patch replaces it instead of
+// stacking on (or being skipped behind) the old one.
+const stripPatch = (source) => {
+ const start = source.indexOf(MARKER_BEGIN);
+ const end = source.indexOf(MARKER_END);
+ const base = start !== -1 && end > start
+ ? source.slice(0, start) + source.slice(end + MARKER_END.length)
+ : source;
+ return base.replace(/lastModified = entry\.modifiedAt \?\? parseUnixListDate\([^;]*\);/, ANCHOR);
+};
+
/**
- * Patch `<nodeModulesDir>/get-uri/dist/ftp.js` in place. Idempotent. Returns
- * `absent` (get-uri not installed here), `patched`, `already-patched`, or
+ * Patch `<nodeModulesDir>/get-uri/dist/ftp.js` in place. Idempotent, and a
+ * changed patch replaces the one an earlier install injected. Returns `absent`
+ * (get-uri not installed here), `patched`, `already-patched`, or
* `unrecognized` (a get-uri this patch was not written for — never edited blind).
*/
export function applyGetUriFtpPatch(nodeModulesDir) {
const file = join(nodeModulesDir, 'get-uri', 'dist', 'ftp.js');
if (!existsSync(file)) return 'absent';
- const source = readFileSync(file, 'utf8');
- if (source.includes(GET_URI_PATCH_MARKER)) return 'already-patched';
+ const installed = readFileSync(file, 'utf8');
+ const fingerprint = patchFingerprint();
+ if (installed.includes(`${MARKER_BEGIN} ${fingerprint} */`)) return 'already-patched';
+ const source = stripPatch(installed);
if (!source.includes(ANCHOR) || !source.includes(HELPER_ANCHOR)) return 'unrecognized';
- const helper = `${GET_URI_PATCH_MARKER}\n${parseUnixListDate.toString()}\n`;
+ const helper = `${MARKER_BEGIN} ${fingerprint} */\n${parseUnixListDate.toString()}\n${MARKER_END}`;
const patched = source.replace(ANCHOR, PATCHED).replace(HELPER_ANCHOR, `${helper}${HELPER_ANCHOR}`);
writeFileSync(file, patched);
return 'patched';
}
-
-/** Changes whenever the injected code or its target does — see trusted-rebuild-stamp.js. */
-export const patchFingerprint = () => createHash('sha256')
- .update([GET_URI_PATCH_TARGET_VERSION, ANCHOR, PATCHED, parseUnixListDate.toString()].join('\0'))
- .digest('hex')
- .slice(0, 16);
diff --git a/scripts/lib/getUriFtpPatch.test.js b/scripts/lib/getUriFtpPatch.test.js
index c47f8f2193..f8a1453808 100644
--- a/scripts/lib/getUriFtpPatch.test.js
+++ b/scripts/lib/getUriFtpPatch.test.js
@@ -5,7 +5,6 @@ import { tmpdir } from 'os';
import { join } from 'path';
import {
- GET_URI_PATCH_MARKER,
applyGetUriFtpPatch,
parseUnixListDate,
patchFingerprint
@@ -92,7 +91,20 @@ describe('applyGetUriFtpPatch', () => {
const once = readFileSync(join(nodeModules, 'get-uri', 'dist', 'ftp.js'), 'utf8');
expect(applyGetUriFtpPatch(nodeModules)).toBe('already-patched');
expect(readFileSync(join(nodeModules, 'get-uri', 'dist', 'ftp.js'), 'utf8')).toBe(once);
- expect(once.split(GET_URI_PATCH_MARKER)).toHaveLength(2);
+ expect(once.match(/portos-patch #9462 begin/g)).toHaveLength(1);
+ });
+
+ it('replaces an older injected patch instead of keeping or stacking it', () => {
+ const nodeModules = install();
+ applyGetUriFtpPatch(nodeModules);
+ const file = join(nodeModules, 'get-uri', 'dist', 'ftp.js');
+ const current = readFileSync(file, 'utf8');
+ // Simulate a tree patched by an earlier release of this patch.
+ writeFileSync(file, current
+ .replace(/begin [0-9a-f]{16}/, 'begin 0000000000000000')
+ .replace(/function parseUnixListDate[\s\S]*?\n}\n/, 'function parseUnixListDate() { return new Date(0); }\n'));
+ expect(applyGetUriFtpPatch(nodeModules)).toBe('patched');
+ expect(readFileSync(file, 'utf8')).toBe(current);
});
it('reports a missing install and never edits a get-uri it does not recognize', () => {