mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 07:20:22 +08:00
fix: 发布 1.1.22,官方桌面 app.asar 还在时应用不再 ENOENT
归档还在时改写真实 resources/app;没有归档时仍用 node:fs。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4538da436d
commit
58c950ae65
10 files changed
+153
-50
No files matched your search
+5
-3
@@ -1,5 +1,3 @@
|
||||
import { promises as fsp } from "node:fs";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
@@ -13,10 +11,14 @@ import {
|
||||
revertHideConsoleFromBin,
|
||||
} from "./hide-console.js";
|
||||
import * as desktop from "./desktop.js";
|
||||
import { asarArchiveIsFile, extractAsar, overlayAsarUnpacked, renameAsarAside } from "./extract-asar.js";
|
||||
import { asarArchiveIsFile, extractAsar, hostFs, overlayAsarUnpacked, renameAsarAside } from "./extract-asar.js";
|
||||
import { adapterFor, detectSurface } from "./surface.js";
|
||||
import { openSlot } from "./table-read.js";
|
||||
|
||||
// 无归档时用 node:fs;app.asar 还在时改写真实的 resources/app,避免新建文件 ENOENT。
|
||||
const fs = hostFs;
|
||||
const fsp = hostFs.promises;
|
||||
|
||||
const isWindows = process.platform === "win32";
|
||||
const SHIM_NAMES = ["dsh.cmd", "dsh.ps1", "dsh"];
|
||||
const HIDDEN_EXEC = {
|
||||
|
||||
+4
-1
@@ -1,9 +1,12 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { execFileSync, spawn } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { launchOfficialDesktopUpdate, pendingOfficialUpdate } from "./official-update.js";
|
||||
import { hostFs } from "./extract-asar.js";
|
||||
|
||||
// 无归档时用 node:fs。app.asar 还在时不能把映射出来的 resources/app 当成已经解开。
|
||||
const fs = hostFs;
|
||||
|
||||
/** 路径落在 `app.asar/...` 里面。`app.asar` 文件本身和 `app.asar.unpacked` 不算。 */
|
||||
export function isAsarSealedPath(p) {
|
||||
|
||||
+101
-18
@@ -1,15 +1,95 @@
|
||||
import fs from "node:fs";
|
||||
import nodeFs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
/** Electron 会把 app.asar 伪装成目录。读归档必须走 original-fs,否则 isFile 为假、解不开。 */
|
||||
function archiveFs() {
|
||||
let cachedDiskFs;
|
||||
|
||||
/**
|
||||
* 真实磁盘上的 fs。
|
||||
* Electron 自带的 fs 会把 `resources/app` 映射进仍在的 `app.asar`,
|
||||
* 往里面新建文件会 ENOENT。解开目录,以及归档还在时打补丁,走这里。
|
||||
* 非 Electron 环境没有 original-fs,退回 node:fs。
|
||||
*/
|
||||
export function diskFs() {
|
||||
if (cachedDiskFs) return cachedDiskFs;
|
||||
process.noDeprecation = true;
|
||||
try {
|
||||
return createRequire(import.meta.url)("original-fs");
|
||||
cachedDiskFs = createRequire(import.meta.url)("original-fs");
|
||||
} catch {
|
||||
return fs;
|
||||
cachedDiskFs = nodeFs;
|
||||
}
|
||||
return cachedDiskFs;
|
||||
}
|
||||
|
||||
function asarFileIn(dir) {
|
||||
if (!dir) return false;
|
||||
try {
|
||||
const asar = path.join(dir, "app.asar");
|
||||
return diskFs().existsSync(asar) && diskFs().statSync(asar).isFile();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 这个路径会被 Electron 映射进旁边的 app.asar。已解开且归档已挪走时为假。 */
|
||||
export function asarShadowsPath(targetPath) {
|
||||
const raw = String(targetPath || "").trim();
|
||||
if (!raw) return false;
|
||||
let dir = path.normalize(raw);
|
||||
for (let i = 0; i < 12; i += 1) {
|
||||
const base = path.basename(dir).toLowerCase();
|
||||
if ((base === "app" || base.endsWith(".asar")) && asarFileIn(path.dirname(dir))) return true;
|
||||
if (asarFileIn(dir)) return true;
|
||||
const parent = path.dirname(dir);
|
||||
if (!parent || parent === dir) break;
|
||||
dir = parent;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 宿主文件用哪套 fs。
|
||||
* 没有 app.asar(Web、第三方桌面、官方包已经解开并挪走归档)时继续用 node:fs。
|
||||
* 归档还在、Electron 会把 resources/app 映进去时,改用真实磁盘。
|
||||
*/
|
||||
export function fsFor(targetPath) {
|
||||
return asarShadowsPath(targetPath) ? diskFs() : nodeFs;
|
||||
}
|
||||
|
||||
function fsForPair(left, right) {
|
||||
return asarShadowsPath(left) || asarShadowsPath(right) ? diskFs() : nodeFs;
|
||||
}
|
||||
|
||||
function route(method, pair) {
|
||||
return (first, ...rest) => {
|
||||
const f = pair ? fsForPair(first, rest[0]) : fsFor(first);
|
||||
return f[method](first, ...rest);
|
||||
};
|
||||
}
|
||||
|
||||
const HOST_SYNC = [
|
||||
"existsSync", "statSync", "lstatSync", "readFileSync", "writeFileSync",
|
||||
"readdirSync", "mkdirSync", "realpathSync", "chmodSync", "unlinkSync",
|
||||
"rmSync", "renameSync", "appendFileSync", "openSync", "symlinkSync",
|
||||
];
|
||||
|
||||
/** 按路径自动选择 node:fs 或真实磁盘。补丁、探测、状态都走这一个。 */
|
||||
export const hostFs = {
|
||||
copyFileSync: route("copyFileSync", true),
|
||||
promises: {
|
||||
readFile: (p, ...args) => fsFor(p).promises.readFile(p, ...args),
|
||||
writeFile: (p, ...args) => fsFor(p).promises.writeFile(p, ...args),
|
||||
mkdir: (p, ...args) => fsFor(p).promises.mkdir(p, ...args),
|
||||
unlink: (p, ...args) => fsFor(p).promises.unlink(p, ...args),
|
||||
rm: (p, ...args) => fsFor(p).promises.rm(p, ...args),
|
||||
copyFile: (src, dest, ...args) => fsForPair(src, dest).promises.copyFile(src, dest, ...args),
|
||||
},
|
||||
};
|
||||
for (const method of HOST_SYNC) hostFs[method] = route(method, false);
|
||||
|
||||
/** Electron 会把 app.asar 伪装成目录。读归档必须走 original-fs,否则 isFile 为假、解不开。 */
|
||||
function archiveFs() {
|
||||
return diskFs();
|
||||
}
|
||||
|
||||
function renameBusy(error) {
|
||||
@@ -101,7 +181,8 @@ export function listAsarEntries(asarPath) {
|
||||
}
|
||||
|
||||
function copyRange(afs, fd, offset, size, outPath) {
|
||||
const out = fs.openSync(outPath, "w");
|
||||
const outFs = diskFs();
|
||||
const out = outFs.openSync(outPath, "w");
|
||||
try {
|
||||
const buf = Buffer.alloc(Math.min(1024 * 1024, Math.max(size, 1)));
|
||||
let left = size;
|
||||
@@ -109,31 +190,32 @@ function copyRange(afs, fd, offset, size, outPath) {
|
||||
while (left > 0) {
|
||||
const n = afs.readSync(fd, buf, 0, Math.min(buf.length, left), pos);
|
||||
if (n <= 0) throw new Error(`asar short read at ${outPath}`);
|
||||
fs.writeSync(out, buf, 0, n);
|
||||
outFs.writeSync(out, buf, 0, n);
|
||||
pos += n;
|
||||
left -= n;
|
||||
}
|
||||
} finally {
|
||||
fs.closeSync(out);
|
||||
outFs.closeSync(out);
|
||||
}
|
||||
}
|
||||
|
||||
/** asar 里标记 unpacked 的原生文件不在归档正文。解开目录启动前必须补到 resources/app。 */
|
||||
export function overlayAsarUnpacked(asarPath, destDir) {
|
||||
const outFs = diskFs();
|
||||
const unpacked = `${asarPath}.unpacked`;
|
||||
if (!destDir || !fs.existsSync(unpacked)) return { copied: 0, missing: true };
|
||||
if (!destDir || !outFs.existsSync(unpacked)) return { copied: 0, missing: true };
|
||||
let copied = 0;
|
||||
const walk = (dir) => {
|
||||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||
for (const ent of outFs.readdirSync(dir, { withFileTypes: true })) {
|
||||
const from = path.join(dir, ent.name);
|
||||
const to = path.join(destDir, path.relative(unpacked, from));
|
||||
if (ent.isDirectory()) {
|
||||
fs.mkdirSync(to, { recursive: true });
|
||||
outFs.mkdirSync(to, { recursive: true });
|
||||
walk(from);
|
||||
} else if (ent.isFile()) {
|
||||
if (fs.existsSync(to)) continue;
|
||||
fs.mkdirSync(path.dirname(to), { recursive: true });
|
||||
fs.copyFileSync(from, to);
|
||||
if (outFs.existsSync(to)) continue;
|
||||
outFs.mkdirSync(path.dirname(to), { recursive: true });
|
||||
outFs.copyFileSync(from, to);
|
||||
copied += 1;
|
||||
}
|
||||
}
|
||||
@@ -150,15 +232,16 @@ export function extractAsar(asarPath, destDir) {
|
||||
const { header, dataOffset } = readHeader(fd, afs);
|
||||
const entries = [];
|
||||
walk(header, "", entries);
|
||||
fs.mkdirSync(destDir, { recursive: true });
|
||||
const outFs = diskFs();
|
||||
outFs.mkdirSync(destDir, { recursive: true });
|
||||
for (const entry of entries) {
|
||||
const info = entry.info;
|
||||
const outPath = path.join(destDir, ...entry.rel.split("/"));
|
||||
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
||||
outFs.mkdirSync(path.dirname(outPath), { recursive: true });
|
||||
if (info.unpacked) continue;
|
||||
if (typeof info.link === "string") {
|
||||
try {
|
||||
fs.symlinkSync(info.link, outPath);
|
||||
outFs.symlinkSync(info.link, outPath);
|
||||
} catch {
|
||||
// 不能建链接就跳过,主程序文件仍在归档里
|
||||
}
|
||||
@@ -167,7 +250,7 @@ export function extractAsar(asarPath, destDir) {
|
||||
const size = Number(info.size || 0);
|
||||
const offset = dataOffset + Number(info.offset || 0);
|
||||
if (size === 0) {
|
||||
fs.writeFileSync(outPath, Buffer.alloc(0));
|
||||
outFs.writeFileSync(outPath, Buffer.alloc(0));
|
||||
} else {
|
||||
copyRange(afs, fd, offset, size, outPath);
|
||||
}
|
||||
|
||||
+11
-10
@@ -3,6 +3,7 @@ import { pathToFileURL } from "node:url";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import path from "node:path";
|
||||
import { isAsarSealedPath } from "./desktop.js";
|
||||
import { diskFs, fsFor } from "./extract-asar.js";
|
||||
|
||||
const MARK = "__dshPurgeHideConsole";
|
||||
const IMPORT_MARK = "dsh-purge-hide-console";
|
||||
@@ -167,15 +168,15 @@ if (process.platform === "win32" && cp && !cp[MARK]) {
|
||||
`;
|
||||
}
|
||||
|
||||
function requireFs() {
|
||||
return createRequire(import.meta.url)("node:fs");
|
||||
}
|
||||
function requirePath() {
|
||||
return createRequire(import.meta.url)("node:path");
|
||||
}
|
||||
|
||||
function ioFor(explicit, targetPath) {
|
||||
return explicit ?? fsFor(targetPath);
|
||||
}
|
||||
|
||||
export function resolveDshBinRoot(hint, { fs, path: nodePath } = {}) {
|
||||
const nodeFs = fs ?? requireFs();
|
||||
const p = nodePath ?? requirePath();
|
||||
const tries = [];
|
||||
const push = (x) => {
|
||||
@@ -199,7 +200,7 @@ export function resolveDshBinRoot(hint, { fs, path: nodePath } = {}) {
|
||||
try {
|
||||
const binJs = p.join(root, "lib", "bin.js");
|
||||
if (isAsarSealedPath(root) || isAsarSealedPath(binJs)) continue;
|
||||
if (nodeFs.existsSync(binJs)) return p.normalize(root);
|
||||
if (ioFor(fs, binJs).existsSync(binJs)) return p.normalize(root);
|
||||
} catch {}
|
||||
}
|
||||
return null;
|
||||
@@ -209,16 +210,16 @@ const IMPORT_LINE = `import "./${IMPORT_MARK}.js"; // [dsh-purge] hide-console`;
|
||||
|
||||
export function installHideConsoleIntoBin(aiBase, { fs, path: nodePath } = {}) {
|
||||
if (process.platform !== "win32") return "skipped_not_win32";
|
||||
const nodeFs = fs ?? requireFs();
|
||||
const p = nodePath ?? requirePath();
|
||||
const root = resolveDshBinRoot(aiBase, { fs: nodeFs, path: p });
|
||||
const root = resolveDshBinRoot(aiBase, { fs, path: p });
|
||||
if (!root) return "missing_bin";
|
||||
const nodeFs = ioFor(fs, root);
|
||||
const binJs = p.join(root, "lib", "bin.js");
|
||||
const side = p.join(root, "lib", `${IMPORT_MARK}.js`);
|
||||
const facadeSide = p.join(root, "lib", "dsh-purge-child-process-hide.mjs");
|
||||
if (isAsarSealedPath(root) || isAsarSealedPath(binJs)) return "sealed_asar";
|
||||
|
||||
const facadeSrc = nodeFs.readFileSync(
|
||||
const facadeSrc = diskFs().readFileSync(
|
||||
fileURLToPath(new URL("./child-process-hide.mjs", import.meta.url)),
|
||||
"utf8",
|
||||
);
|
||||
@@ -244,10 +245,10 @@ export function installHideConsoleIntoBin(aiBase, { fs, path: nodePath } = {}) {
|
||||
}
|
||||
|
||||
export function revertHideConsoleFromBin(aiBase, { fs, path: nodePath } = {}) {
|
||||
const nodeFs = fs ?? requireFs();
|
||||
const p = nodePath ?? requirePath();
|
||||
const root = resolveDshBinRoot(aiBase, { fs: nodeFs, path: p });
|
||||
const root = resolveDshBinRoot(aiBase, { fs, path: p });
|
||||
if (!root) return "missing_bin";
|
||||
const nodeFs = ioFor(fs, root);
|
||||
const binJs = p.join(root, "lib", "bin.js");
|
||||
const side = p.join(root, "lib", `${IMPORT_MARK}.js`);
|
||||
const facadeSide = p.join(root, "lib", "dsh-purge-child-process-hide.mjs");
|
||||
|
||||
+2
-3
@@ -1,5 +1,4 @@
|
||||
import fs from "node:fs";
|
||||
import { promises as fsp } from "node:fs";
|
||||
import { fsFor } from "./extract-asar.js";
|
||||
import * as core from "./core.js";
|
||||
import * as hostctl from "./host.js";
|
||||
import * as rules from "./rules.js";
|
||||
@@ -99,7 +98,7 @@ function watchWebTreeReady(ctx) {
|
||||
}
|
||||
|
||||
function fsExists(fp) {
|
||||
return fs.existsSync(fp);
|
||||
return fsFor(fp).existsSync(fp);
|
||||
}
|
||||
|
||||
function line(title, value, ok = null) {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
// 官方桌面已下载新版本时,重启改去跑安装包,装完再解开并重新打补丁。
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { spawn } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { readAsarFile } from "./extract-asar.js";
|
||||
import { hostFs as fs, readAsarFile } from "./extract-asar.js";
|
||||
|
||||
const CACHE_DIR_NAME = "@deepseek-aidsh-desktop-updater";
|
||||
const MARKER_NAME = "dsh-purge-reapply-asar";
|
||||
|
||||
Reference in new issue
Block a user