This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo 指纹
一个用于从 React Native 项目生成指纹的库。
@expo/fingerprint 提供了一个 API,用于生成项目的指纹(hash),以便判断应用原生层与 JavaScript 层之间的兼容性。哈希计算是可配置的,但默认情况下会基于应用依赖项、自定义原生代码、原生项目文件和配置进行哈希计算。
安装
默认情况下,@expo/fingerprint 已包含在 expo 和 expo-updates 中。
如果你希望将 @expo/fingerprint 作为独立包使用,可以运行以下命令进行安装:
CLI 用法
配置
@expo/fingerprint 提供了适用于大多数项目的默认设置,但也提供了几种方式来配置指纹生成过程,以更好地适配你的应用结构和工作流。
.fingerprintignore
放置在项目根目录中的 .fingerprintignore 是一种类似 .gitignore 的忽略机制,用于将文件排除在哈希计算之外。所有模式路径都相对于项目根目录。它的行为类似,但使用 minimatch 进行模式匹配,因此有一些 限制(参见 Options 下 ignorePaths 的文档)。
下面是一个 .fingerprintignore 配置示例:
fingerprint.config.js
放置在项目根目录中的 fingerprint.config.js 允许你指定超出 .fingerprintignore 可配置范围的自定义哈希计算配置。支持的配置项请参见 Config 和 SourceSkips。
下面是一个 fingerprint.config.js 配置示例,假设你已将 @expo/fingerprint 作为直接依赖安装:
如果你是通过 expo 使用 @expo/fingerprint(其中 @expo/fingerprint 是作为传递依赖安装的),你可以从 expo/fingerprint 导入 fingerprint:
/** @type {import('expo/fingerprint').Config} */
高级:在指纹哈希之前自定义来源
在某些情况下,你可能希望在指纹生成之前自定义数据来源。例如:
- 你想从应用配置中移除敏感数据。
- 你想稳定应用配置中的动态值。
- 你想将文件哈希转换为稳定值。
为此,你可以在 fingerprint.config.js 文件中使用 fileHookTransform 选项,在哈希计算前转换来源内容。了解更多关于 fileHookTransform 选项。
限制
对 @expo/config-plugins 原始函数的支持有限
在使用带有原始函数的 config plugins 时,需要注意某些限制,尤其是在指纹生成的上下文中。该库会尽最大努力为通过 config plugins 所做的更改生成指纹;然而,原始函数会带来特定挑战。原始函数无法序列化为指纹,这意味着它们不能直接用于生成唯一的哈希。
为了解决这一限制,该库采用以下策略之一为原始函数创建可序列化的指纹:
-
使用
Function.name:如果具名原始函数可用,该库会使用Function.name属性。该属性提供了函数的可识别名称,可用作指纹属性。 -
使用
withAnonymous:对于没有Function.name的匿名原始函数,该库会使用withAnonymous作为指纹属性。这是匿名函数的通用标识符。
下面是一个示例,用于说明库在指纹哈希中会将 [withMyPlugin, withAnonymous] 作为插件属性的情况:
需要注意的是,由于这种设计,如果你修改原始 config plugins 函数的实现,例如更改 withMyPlugin 中的 Info.plist 值,指纹仍然会生成相同的哈希值。为了在修改 config plugins 实现时确保指纹唯一,请考虑以下选项:
-
避免匿名函数:避免使用匿名的原始 config plugins 函数。相反,尽可能使用具名函数,并确保只要实现发生变化,它们的名称保持一致。
-
使用本地 config plugins:或者,你可以将本地 config plugins 创建为独立模块,每个模块都有自己的导出。这样在修改 config plugins 实现时,你可以指定不同的函数名。
下面是一个使用本地 config plugin 的示例:
遵循这些指南,你可以有效管理对 config plugins 的更改,并确保指纹生成保持一致且可靠。
API
import * as Fingerprint from '@expo/fingerprint';
Constants
Methods
Create a fingerprint for a project.
Promise<Fingerprint>Example
const fingerprint = await createFingerprintAsync('/app'); console.log(fingerprint);
Create a native hash value for a project.
Promise<string>Example
const hash = await createProjectHashAsync('/app'); console.log(hash);
Diff the fingerprint with the fingerprint of the provided project.
Promise<FingerprintDiffItem[]>Example
// Create a fingerprint for the project const fingerprint = await createFingerprintAsync('/app'); // Make some changes to the project // Calculate the diff const diff = await diffFingerprintChangesAsync(fingerprint, '/app'); console.log(diff);
Diff two fingerprints. The implementation assumes that the sources are sorted.
FingerprintDiffItem[]Example
// Create a fingerprint for the project const fingerprint = await createFingerprintAsync('/app'); // Make some changes to the project // Create a fingerprint again const fingerprint2 = await createFingerprintAsync('/app'); const diff = await diffFingerprints(fingerprint, fingerprint2); console.log(diff);
Interfaces
Types
Supported options for use in fingerprint.config.js
Type: Pick<Options, 'concurrentIoLimit' | 'hashAlgorithm' | 'ignorePaths' | 'extraSources' | 'enableReactImportsPatcher' | 'useRNCoreAutolinkingFromExpo' | 'debug' | 'fileHookTransform'> extended by:
Literal type: union
Acceptable values are: DebugInfoFile | DebugInfoDir | DebugInfoContents
Hook function to transform file content sources before hashing.
Buffer | string | null
The source parameter for FileHookTransformFunction.
Type: object shaped as below:
Or object shaped as below:
Type: object shaped as below:
Or object shaped as below:
Or object shaped as below:
Literal type: union
Acceptable values are: HashResultFile | HashResultDir | HashResultContents
Literal type: union
Acceptable values are: HashSourceFile | HashSourceDir | HashSourceContents
Enums
Bitmask of values that can be used to skip certain parts of the sourcers when generating a fingerprint.
SourceSkips.ExpoConfigVersions = 1Versions in app.json, including Android versionCode and iOS buildNumber
SourceSkips.ExpoConfigRuntimeVersionIfString = 2runtimeVersion in app.json if it is a string
SourceSkips.ExpoConfigNames = 4App names in app.json, including shortName and description
SourceSkips.ExpoConfigIosBundleIdentifier = 16iOS bundle identifier in app.json
SourceSkips.ExpoConfigAssets = 128Assets in app.json, including icons and splash assets
SourceSkips.ExpoConfigAll = 256Skip the whole ExpoConfig. Prefer the other ExpoConfig source skips when possible and use this flag with caution. This will potentially ignore some native changes that should be part of most fingerprints. E.g., adding a new config plugin, changing the app icon, or changing the app name.
SourceSkips.PackageJsonAndroidAndIosScriptsIfNotContainRun = 512package.json scripts if android and ios items do not contain "run". Because prebuild will change the scripts in package.json, this is useful to generate a consistent fingerprint before and after prebuild.
SourceSkips.PackageJsonScriptsAll = 1024Skip the whole scripts section in the project's package.json.