Skip to content

Commit

Permalink
refactor smartUnpack
Browse files Browse the repository at this point in the history
  • Loading branch information
beyondkmp committed Oct 30, 2024
1 parent 3d31094 commit a4118e9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 65 deletions.
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/asar/asarUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class AsarPackager {

for await (const fileSet of fileSets) {
if (this.config.options.smartUnpack !== false) {
detectUnpackedDirs(fileSet, unpackedPaths, this.config.defaultDestination)
detectUnpackedDirs(fileSet, unpackedPaths)
}
for (let i = 0; i < fileSet.files.length; i++) {
const file = fileSet.files[i]
Expand Down
71 changes: 10 additions & 61 deletions packages/app-builder-lib/src/asar/unpackDetector.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,32 @@
import { log } from "builder-util"
import { log, FilterStats } from "builder-util"
import { isBinaryFileSync } from "isbinaryfile"
import * as path from "path"
import { NODE_MODULES_PATTERN } from "../fileTransformer"
import { getDestinationPath, ResolvedFileSet } from "../util/appFileCopier"

function addValue(map: Map<string, Array<string>>, key: string, value: string) {
let list = map.get(key)
if (list == null) {
list = [value]
map.set(key, list)
} else {
list.push(value)
}
}
import { ResolvedFileSet } from "../util/appFileCopier"

export function isLibOrExe(file: string): boolean {
// https://github.com/electron-userland/electron-builder/issues/3038
return file.endsWith(".dll") || file.endsWith(".exe") || file.endsWith(".dylib") || file.endsWith(".so") || file.endsWith(".node")
}

/** @internal */
export function detectUnpackedDirs(fileSet: ResolvedFileSet, autoUnpackDirs: Set<string>, defaultDestination: string) {
const dirToCreate = new Map<string, Array<string>>()
export function detectUnpackedDirs(fileSet: ResolvedFileSet, autoUnpackDirs: Set<string>) {
const metadata = fileSet.metadata

function addParents(child: string, root: string) {
child = path.dirname(child)
if (autoUnpackDirs.has(child)) {
return
}

do {
autoUnpackDirs.add(child)
const p = path.dirname(child)
// create parent dir to be able to copy file later without directory existence check
addValue(dirToCreate, p, path.basename(child))

if (child === root || p === root || autoUnpackDirs.has(p)) {
break
}
child = p
} while (true)

autoUnpackDirs.add(root)
}

for (let i = 0, n = fileSet.files.length; i < n; i++) {
const file = fileSet.files[i]
const index = file.lastIndexOf(NODE_MODULES_PATTERN)
if (index < 0) {
const stat: FilterStats = metadata.get(file)!
if (stat.destNodeModulesPath) {
continue
}

let nextSlashIndex = file.indexOf(path.sep, index + NODE_MODULES_PATTERN.length + 1)
if (nextSlashIndex < 0) {
continue
}

if (file[index + NODE_MODULES_PATTERN.length] === "@") {
nextSlashIndex = file.indexOf(path.sep, nextSlashIndex + 1)
}

if (!metadata.get(file)!.isFile()) {
continue
}

const packageDir = file.substring(0, nextSlashIndex)
const packageDirPathInArchive = path.relative(defaultDestination, getDestinationPath(packageDir, fileSet))
const pathInArchive = path.relative(defaultDestination, getDestinationPath(file, fileSet))
if (autoUnpackDirs.has(packageDirPathInArchive)) {
// if package dir is unpacked, any file also unpacked
addParents(pathInArchive, packageDirPathInArchive)
if (!stat.isFile()) {
continue
}

// https://github.com/electron-userland/electron-builder/issues/2679
let shouldUnpack = false
// ffprobe-static and ffmpeg-static are known packages to always unpack
const moduleName = path.basename(packageDir)
const moduleName = stat.moduleName
const fileBaseName = path.basename(file)
const hasExtension = path.extname(fileBaseName)
if (moduleName === "ffprobe-static" || moduleName === "ffmpeg-static" || isLibOrExe(file)) {
Expand All @@ -91,9 +40,9 @@ export function detectUnpackedDirs(fileSet: ResolvedFileSet, autoUnpackDirs: Set
}

if (log.isDebugEnabled) {
log.debug({ file: pathInArchive, reason: "contains executable code" }, "not packed into asar archive")
log.debug({ file: stat.destNodeModulesPath, reason: "contains executable code" }, "not packed into asar archive")
}

addParents(pathInArchive, packageDirPathInArchive)
autoUnpackDirs.add(fileSet.destination)
break
}
}
3 changes: 2 additions & 1 deletion packages/app-builder-lib/src/util/NodeModuleCopyHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export class NodeModuleCopyHelper extends FileCopyHelper {
}

return lstat(filePath).then((stat: FilterStats) => {
stat.relativeNodeModulesPath = path.join("node_modules", moduleName, path.relative(depPath, filePath))
stat.destNodeModulesPath = path.join("node_modules", moduleName, path.relative(depPath, filePath))
stat.moduleName = moduleName
if (filter != null && !filter(filePath, stat)) {
return null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/util/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function ensureEndSlash(s: string) {
}

function getRelativePath(file: string, srcWithEndSlash: string, stat: FilterStats) {
let relative = stat.relativeNodeModulesPath || file.substring(srcWithEndSlash.length)
let relative = stat.destNodeModulesPath || file.substring(srcWithEndSlash.length)
if (path.sep === "\\") {
if (relative.startsWith("\\")) {
// windows problem: double backslash, the above substring call removes root path with a single slash, so here can me some leftovers
Expand Down
3 changes: 2 additions & 1 deletion packages/builder-util/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export type FileTransformer = (file: string) => Promise<null | string | Buffer |
export interface FilterStats extends Stats {
// relative path of the dependency(node_modules + moduleName + file)
// Mainly used for filter, such as files filtering and asarUnpack filtering
relativeNodeModulesPath?: string
destNodeModulesPath?: string
moduleName?: string
// deal with asar unpack sysmlink
relativeLink?: string
linkRelativeToFile?: string
Expand Down

0 comments on commit a4118e9

Please sign in to comment.