diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1ea7a02c..6b5093e4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6782,7 +6782,7 @@ snapshots: '@develar/schema-utils': 2.6.5 '@electron/asar': 3.4.1 '@electron/fuses': 1.8.0 - '@electron/get': 3.0.0 + '@electron/get': 3.1.0 '@electron/notarize': 2.5.0 '@electron/osx-sign': 1.3.3 '@electron/rebuild': 4.0.3 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 89a58379..c3ae1dfa 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -15,6 +15,7 @@ allowBuilds: esbuild: true overrides: + "@electron/get": "3.1.0" dompurify: ">=3.4.5" lodash: ">=4.17.23" "@isaacs/brace-expansion": ">=5.0.1" diff --git a/scripts/patch-electron-builder-fs.cjs b/scripts/patch-electron-builder-fs.cjs index 230fd0fe..ddd0ae48 100644 --- a/scripts/patch-electron-builder-fs.cjs +++ b/scripts/patch-electron-builder-fs.cjs @@ -24,16 +24,48 @@ const preamble = `const __ebFsConstants = (() => { `; +const cacheModeFallback = { + ReadWrite: 0, + ReadOnly: 1, + WriteOnly: 2, + Bypass: 3, +}; + function patchBinDownload(source) { - if (source.includes("__ebFsConstants")) { - return source; + let next = source; + let changed = false; + + if (!next.includes("__ebFsConstants")) { + next = preamble + next; + next = next.replace(/fs_1\.constants\.R_OK/g, "__ebFsConstants.R_OK"); + next = next.replace(/fs_1\.constants\.W_OK/g, "__ebFsConstants.W_OK"); + next = next.replace(/fs\.constants\.R_OK/g, "__ebFsConstants.R_OK"); + next = next.replace(/fs\.constants\.W_OK/g, "__ebFsConstants.W_OK"); + changed = true; } - let next = preamble + source; - next = next.replace(/fs_1\.constants\.R_OK/g, "__ebFsConstants.R_OK"); - next = next.replace(/fs_1\.constants\.W_OK/g, "__ebFsConstants.W_OK"); - next = next.replace(/fs\.constants\.R_OK/g, "__ebFsConstants.R_OK"); - next = next.replace(/fs\.constants\.W_OK/g, "__ebFsConstants.W_OK"); - return next; + + if (!next.includes("__ebElectronDownloadCacheMode")) { + const injectAfterGetRequire = + /(const get_1 = require\("@electron\/get"\);)/; + if (injectAfterGetRequire.test(next)) { + next = next.replace( + injectAfterGetRequire, + `$1 +const __ebElectronDownloadCacheMode = get_1.ElectronDownloadCacheMode ?? ${JSON.stringify(cacheModeFallback)};`, + ); + next = next.replace( + /get_1\.ElectronDownloadCacheMode\.ReadWrite/g, + "__ebElectronDownloadCacheMode.ReadWrite", + ); + next = next.replace( + /in get_1\.ElectronDownloadCacheMode/g, + "in __ebElectronDownloadCacheMode", + ); + changed = true; + } + } + + return changed ? next : source; } for (const target of targets) {