fix(target): preserve app bundle filesystem failure reasons
This commit is contained in:
@@ -36,6 +36,15 @@ export interface ResolvedAppBundle {
|
||||
readonly infoPlist?: string;
|
||||
}
|
||||
|
||||
/** Filesystem operations used by the resolver, injectable for boundary tests. */
|
||||
export interface AppBundleFileSystem {
|
||||
readonly readFile: (path: string) => Promise<Buffer>;
|
||||
readonly realpath: (path: string) => Promise<string>;
|
||||
readonly decodeBinaryPlist?: (path: string) => Promise<string>;
|
||||
}
|
||||
|
||||
const defaultFileSystem: AppBundleFileSystem = { readFile, realpath };
|
||||
|
||||
/**
|
||||
* Resolve an app bundle directory to its declared program file. macOS and
|
||||
* flat iOS-style layouts are read directly. An iOS app installed on a Mac is
|
||||
@@ -44,21 +53,34 @@ export interface ResolvedAppBundle {
|
||||
*/
|
||||
export const resolveAppBundleExecutable = async (
|
||||
path: string,
|
||||
fileSystem: AppBundleFileSystem = defaultFileSystem,
|
||||
): Promise<Result<ResolvedAppBundle, BinaryTargetError>> => {
|
||||
const layout = await presentLayout(path);
|
||||
if (layout !== undefined) return resolveLayoutExecutable(path, layout);
|
||||
const wrapped = await wrappedBundle(path);
|
||||
if (wrapped !== undefined) {
|
||||
const wrappedLayout = await presentLayout(wrapped);
|
||||
if (wrappedLayout !== undefined)
|
||||
return resolveLayoutExecutable(wrapped, wrappedLayout);
|
||||
try {
|
||||
const layout = await presentLayout(path);
|
||||
if (layout !== undefined)
|
||||
return resolveLayoutExecutable(path, layout, fileSystem);
|
||||
const wrapped = await wrappedBundle(path);
|
||||
if (wrapped !== undefined) {
|
||||
const wrappedLayout = await presentLayout(wrapped);
|
||||
if (wrappedLayout !== undefined)
|
||||
return resolveLayoutExecutable(wrapped, wrappedLayout, fileSystem);
|
||||
}
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof BinaryTargetError) return err(cause);
|
||||
return err(
|
||||
new BinaryTargetError(path, errorReason(cause, path), { cause }),
|
||||
);
|
||||
}
|
||||
return err(
|
||||
new BinaryTargetError(path, "app has no readable CFBundleExecutable", {
|
||||
cause: new Error(
|
||||
"No Contents/Info.plist, root Info.plist, or single Wrapper/*.app bundle was found",
|
||||
),
|
||||
}),
|
||||
new BinaryTargetError(
|
||||
path,
|
||||
`app Info.plist is missing or no supported bundle layout was found: ${path}`,
|
||||
{
|
||||
cause: new Error(
|
||||
"No Contents/Info.plist, root Info.plist, or single Wrapper/*.app bundle was found",
|
||||
),
|
||||
},
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
@@ -80,16 +102,50 @@ const layoutPlistPresent = async (path: string): Promise<boolean> => {
|
||||
return (await stat(path)).isFile();
|
||||
} catch (cause: unknown) {
|
||||
if (isAbsence(cause)) return false;
|
||||
if (isPermissionDenied(cause))
|
||||
throw new BinaryTargetError(
|
||||
path,
|
||||
`permission denied inspecting app Info.plist ${path}`,
|
||||
{ cause },
|
||||
);
|
||||
throw cause;
|
||||
}
|
||||
};
|
||||
|
||||
const isAbsence = (cause: unknown): boolean => {
|
||||
const code: unknown =
|
||||
cause instanceof Error ? Reflect.get(cause, "code") : undefined;
|
||||
const code = errorCode(cause);
|
||||
return code === "ENOENT" || code === "ENOTDIR";
|
||||
};
|
||||
|
||||
const errorCode = (cause: unknown): unknown =>
|
||||
cause instanceof Error ? Reflect.get(cause, "code") : undefined;
|
||||
|
||||
const codeDescription = (cause: unknown): string => {
|
||||
const code = errorCode(cause);
|
||||
return typeof code === "string" || typeof code === "number"
|
||||
? ` (${String(code)})`
|
||||
: "";
|
||||
};
|
||||
|
||||
const isSystemErrorCode = (cause: unknown): boolean => {
|
||||
const code = errorCode(cause);
|
||||
return (
|
||||
typeof code === "string" &&
|
||||
/^[A-Z][A-Z0-9_]+$/u.test(code) &&
|
||||
!code.startsWith("ERR_")
|
||||
);
|
||||
};
|
||||
|
||||
const isPermissionDenied = (cause: unknown): boolean =>
|
||||
errorCode(cause) === "EACCES" || errorCode(cause) === "EPERM";
|
||||
|
||||
const errorReason = (cause: unknown, path: string): string =>
|
||||
isPermissionDenied(cause)
|
||||
? `permission denied accessing app bundle path ${path}`
|
||||
: isAbsence(cause)
|
||||
? `app bundle path is missing: ${path}`
|
||||
: `could not inspect app bundle path ${path}`;
|
||||
|
||||
/** The single real `.app` directory inside an iOS-on-Mac `Wrapper`. */
|
||||
const wrappedBundle = async (bundle: string): Promise<string | undefined> => {
|
||||
const wrapper = join(bundle, "Wrapper");
|
||||
@@ -111,20 +167,45 @@ const wrappedBundle = async (bundle: string): Promise<string | undefined> => {
|
||||
const resolveLayoutExecutable = async (
|
||||
bundle: string,
|
||||
layout: BundleLayout,
|
||||
fileSystem: AppBundleFileSystem,
|
||||
): Promise<Result<ResolvedAppBundle, BinaryTargetError>> => {
|
||||
const plistPath = join(bundle, ...layout.plist);
|
||||
let plist: Buffer;
|
||||
try {
|
||||
plist = await fileSystem.readFile(plistPath);
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new BinaryTargetError(
|
||||
plistPath,
|
||||
isPermissionDenied(cause)
|
||||
? `permission denied reading app Info.plist ${plistPath}`
|
||||
: isAbsence(cause)
|
||||
? `app Info.plist is missing: ${plistPath}`
|
||||
: `could not read app Info.plist ${plistPath}${codeDescription(cause)}`,
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let name: string;
|
||||
try {
|
||||
const plist = await readFile(plistPath);
|
||||
name =
|
||||
plist.subarray(0, 6).toString("ascii") === "bplist"
|
||||
? await readBinaryPlistExecutable(plistPath)
|
||||
? await (fileSystem.decodeBinaryPlist ?? readBinaryPlistExecutable)(
|
||||
plistPath,
|
||||
)
|
||||
: parseXmlPlistExecutable(plist.toString("utf8"));
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new BinaryTargetError(bundle, "app has no readable CFBundleExecutable", {
|
||||
cause,
|
||||
}),
|
||||
new BinaryTargetError(
|
||||
plistPath,
|
||||
isPermissionDenied(cause)
|
||||
? `permission denied decoding app Info.plist ${plistPath}${codeDescription(cause)}`
|
||||
: isSystemErrorCode(cause)
|
||||
? `could not decode app Info.plist ${plistPath}${codeDescription(cause)}`
|
||||
: `app Info.plist is malformed or lacks CFBundleExecutable: ${plistPath}`,
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
if (!isSafeExecutableName(name))
|
||||
@@ -135,8 +216,8 @@ const resolveLayoutExecutable = async (
|
||||
const executable = join(programs, name);
|
||||
try {
|
||||
const [canonicalPrograms, canonicalExecutable] = await Promise.all([
|
||||
realpath(programs),
|
||||
realpath(executable),
|
||||
fileSystem.realpath(programs),
|
||||
fileSystem.realpath(executable),
|
||||
]);
|
||||
if (!isPathWithinRoot(canonicalPrograms, canonicalExecutable))
|
||||
return err(
|
||||
@@ -148,7 +229,15 @@ const resolveLayoutExecutable = async (
|
||||
return ok({ executable: canonicalExecutable, infoPlist: plistPath });
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new BinaryTargetError(bundle, "app program file is missing", { cause }),
|
||||
new BinaryTargetError(
|
||||
executable,
|
||||
isPermissionDenied(cause)
|
||||
? `permission denied resolving app program file ${executable}`
|
||||
: isAbsence(cause)
|
||||
? `app program file is missing: ${executable}`
|
||||
: `could not resolve app program file ${executable}`,
|
||||
{ cause },
|
||||
),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user