@octane-xplat/fingerprint 0.0.0-stage → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +47 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +538 -0
- package/dist/native-config.d.ts +4 -0
- package/dist/native-config.js +85 -0
- package/dist/native-resolutions.d.ts +2 -0
- package/dist/native-resolutions.js +70 -0
- package/dist/toolchain.d.ts +3 -0
- package/dist/toolchain.js +94 -0
- package/dist/types.d.ts +50 -0
- package/dist/types.js +1 -0
- package/package.json +46 -5
package/README.md
CHANGED
|
@@ -1,3 +1,118 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @octane-xplat/fingerprint
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Compare the native build inputs of two Octane Xplat apps before sending a
|
|
4
|
+
JavaScript update. A fingerprint is a SHA-256 identity plus a list of hashed
|
|
5
|
+
inputs. The API follows [Expo Fingerprint](https://docs.expo.dev/versions/latest/sdk/fingerprint/)
|
|
6
|
+
names while collecting NativeScript inputs instead of Expo or React Native inputs.
|
|
7
|
+
|
|
8
|
+
This is a **Node build tool**, not an app runtime module. Install it in your
|
|
9
|
+
app's development dependencies with Node 22.22.2 or newer and Bun:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
bun add -D @octane-xplat/fingerprint
|
|
13
|
+
bun run xplat-fingerprint --dir . --platform ios > native-inputs.json
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The command prints JSON containing `hash` and `sources`. Compare it after a
|
|
17
|
+
change; each difference names an added, removed or changed input. Ordinary
|
|
18
|
+
app JavaScript changes do not change the native identity.
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
bun run xplat-fingerprint --dir . --platform ios --diff native-inputs.json
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Use in a build script
|
|
25
|
+
|
|
26
|
+
Create `scripts/native-inputs.mjs` in the app. The same inputs in different
|
|
27
|
+
checkout directories produce the same hash. Source records contain paths,
|
|
28
|
+
reasons and hashes; they omit the configuration values and file contents.
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import {
|
|
32
|
+
createFingerprintAsync,
|
|
33
|
+
createProjectHashAsync,
|
|
34
|
+
diffFingerprintChangesAsync,
|
|
35
|
+
} from '@octane-xplat/fingerprint'
|
|
36
|
+
|
|
37
|
+
const before = await createFingerprintAsync('.', { platforms: ['ios'] })
|
|
38
|
+
console.log(before.hash, before.sources.length)
|
|
39
|
+
console.log(await createProjectHashAsync('.', { platforms: ['ios'] }))
|
|
40
|
+
console.log(await diffFingerprintChangesAsync(before, '.', { platforms: ['ios'] }))
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The defaults collect iOS and Android inputs together. Select a single platform
|
|
44
|
+
when comparing native binaries. Installed dependencies and a resolved Bun or pnpm
|
|
45
|
+
lockfile are required. When both lockfiles are present, set `packageManager`
|
|
46
|
+
in `package.json` to select one. Missing or ambiguous native dependency metadata fails
|
|
47
|
+
the operation rather than producing a compatibility claim.
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
import { createFingerprintAsync, diffFingerprints } from '@octane-xplat/fingerprint'
|
|
51
|
+
|
|
52
|
+
const before = await createFingerprintAsync('.', { platforms: ['android'] })
|
|
53
|
+
const after = await createFingerprintAsync('.', { platforms: ['android'] })
|
|
54
|
+
console.log(diffFingerprints(before, after))
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Compare with an OTA binary
|
|
58
|
+
|
|
59
|
+
Follow [phone update setup](https://octane-xplat.goddardai.org/updates) to build
|
|
60
|
+
an OTA-enabled release. Its prepare hook captures compiler and SDK identity,
|
|
61
|
+
build mode, declared compiler overrides, native sources and resolved native
|
|
62
|
+
dependencies. The hook bakes the hash into native startup and saves a private
|
|
63
|
+
build record at `.xplat/fingerprints/ios.json` or `android.json`. Keep `.xplat/`
|
|
64
|
+
out of Git. Use that record to reproduce the build's compiler context on a
|
|
65
|
+
publishing machine; a source-only invocation has a different context.
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
bun run xplat-fingerprint --dir . \
|
|
69
|
+
--build-record .xplat/fingerprints/ios.json \
|
|
70
|
+
--diff .xplat/fingerprints/ios.json
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
An empty diff means the collected inputs still match. It does not prove that
|
|
74
|
+
arbitrary custom build steps are deterministic or that a payload is safe.
|
|
75
|
+
The OTA publisher checks the record automatically; native startup separately
|
|
76
|
+
rejects an incompatible staged payload before its JavaScript runs.
|
|
77
|
+
|
|
78
|
+
## Include a custom native input
|
|
79
|
+
|
|
80
|
+
Add `fingerprint.config.json` at the app root when a custom build step reads
|
|
81
|
+
an additional file. `extraSources` augments automatic collection; executable
|
|
82
|
+
fingerprint config files are unsupported. `ignorePaths` and `.fingerprintignore`
|
|
83
|
+
can exclude extra inputs, but excluding an automatic native input is an error.
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
{
|
|
87
|
+
"extraSources": [
|
|
88
|
+
{
|
|
89
|
+
"type": "file",
|
|
90
|
+
"filePath": "native-options.json",
|
|
91
|
+
"reasons": ["custom native generator input"]
|
|
92
|
+
}
|
|
93
|
+
],
|
|
94
|
+
"toolchain": { "customGeneratorVersion": "1" }
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The collector includes evaluated NativeScript configuration, custom resources,
|
|
99
|
+
declared `NativeSource` files, native plugin files and versions, relevant Bun or pnpm
|
|
100
|
+
resolutions and patches, hooks, native declarations and available Pod/SwiftPM/
|
|
101
|
+
Gradle lock records. It executes the app's trusted NativeScript configuration
|
|
102
|
+
as build code, including relative configuration helpers. Declare any extra
|
|
103
|
+
inputs those helpers consume. Symbolic links must stay within their declared
|
|
104
|
+
input root. Inspect the collected records when qualifying a custom build:
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
import { createFingerprintAsync } from '@octane-xplat/fingerprint'
|
|
108
|
+
|
|
109
|
+
const fingerprint = await createFingerprintAsync('.', { platforms: ['ios'] })
|
|
110
|
+
for (const source of fingerprint.sources) console.log(source)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Source collection also accepts `macos`, `linux` and `windows` for build-tool
|
|
114
|
+
diagnostics. OTA activation remains iOS/Android only; there is no browser
|
|
115
|
+
runtime API. Apple compiler capture belongs on the Apple build host, and
|
|
116
|
+
Android compiler capture belongs on its configured SDK host. Android runtime
|
|
117
|
+
qualification is currently unverified. See the [implementation evidence](../../.agents/docs/notes/fingerprint.md)
|
|
118
|
+
for collector limits and platform verification.
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { readFile } from 'node:fs/promises';
|
|
5
|
+
import { resolve } from 'node:path';
|
|
6
|
+
import { createFingerprintAsync, diffFingerprintChangesAsync, NATIVE_FINGERPRINT_FORMAT, } from "./index.js";
|
|
7
|
+
const { values } = parseArgs({
|
|
8
|
+
options: {
|
|
9
|
+
dir: { type: 'string' },
|
|
10
|
+
platform: { type: 'string' },
|
|
11
|
+
diff: { type: 'string' },
|
|
12
|
+
'build-record': { type: 'string' },
|
|
13
|
+
help: { type: 'boolean' },
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
if (values.help) {
|
|
17
|
+
console.log('xplat-fingerprint [--dir APP_ROOT] [--platform ios|android|macos|linux|windows] [--build-record FILE] [--diff FILE]\nPrint JSON only; a build record supplies the platform and recorded compiler context. Diff compares a saved fingerprint with the project.');
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
try {
|
|
21
|
+
const root = resolve(values.dir ?? '.');
|
|
22
|
+
const options = values.platform ? { platforms: [values.platform] } : {};
|
|
23
|
+
if (values['build-record']) {
|
|
24
|
+
const record = JSON.parse(await readFile(resolve(values['build-record']), 'utf8'));
|
|
25
|
+
if (!record ||
|
|
26
|
+
record.format !== NATIVE_FINGERPRINT_FORMAT ||
|
|
27
|
+
!['ios', 'android'].includes(record.platform) ||
|
|
28
|
+
!record.toolchain ||
|
|
29
|
+
typeof record.toolchain !== 'object' ||
|
|
30
|
+
Array.isArray(record.toolchain) ||
|
|
31
|
+
Object.values(record.toolchain).some((value) => typeof value !== 'string') ||
|
|
32
|
+
(values.platform && values.platform !== record.platform)) {
|
|
33
|
+
throw new Error('Invalid or mismatched native build record');
|
|
34
|
+
}
|
|
35
|
+
options.platforms = [record.platform];
|
|
36
|
+
options.toolchain = record.toolchain;
|
|
37
|
+
}
|
|
38
|
+
const result = values.diff
|
|
39
|
+
? await diffFingerprintChangesAsync(JSON.parse(await readFile(resolve(values.diff), 'utf8')), root, options)
|
|
40
|
+
: await createFingerprintAsync(root, options);
|
|
41
|
+
console.log(JSON.stringify(result, null, 2));
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error(error instanceof Error ? error.message : 'Fingerprint failed');
|
|
45
|
+
process.exitCode = 1;
|
|
46
|
+
}
|
|
47
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { captureNativeToolchainAsync } from './toolchain.ts';
|
|
2
|
+
export { readNativeConfig as resolveNativeConfigAsync } from './native-config.ts';
|
|
3
|
+
export declare const NATIVE_FINGERPRINT_FORMAT = "octane-xplat.native-fingerprint.v1";
|
|
4
|
+
import type { Fingerprint, FingerprintDiffItem, Options } from './types.ts';
|
|
5
|
+
export type * from './types.ts';
|
|
6
|
+
/** Collect installed native inputs and the app's resolved NativeScript configuration. */
|
|
7
|
+
export declare function createFingerprintAsync(projectRoot: string, supplied?: Options): Promise<Fingerprint>;
|
|
8
|
+
export declare function createProjectHashAsync(root: string, options?: Options): Promise<string>;
|
|
9
|
+
export declare function diffFingerprints(before: Fingerprint, after: Fingerprint): FingerprintDiffItem[];
|
|
10
|
+
export declare function diffFingerprintChangesAsync(fingerprint: Fingerprint, root: string, options?: Options): Promise<FingerprintDiffItem[]>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { existsSync, realpathSync } from 'node:fs';
|
|
3
|
+
import { glob, lstat, readFile, readdir, realpath } from 'node:fs/promises';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
6
|
+
import { minimatch } from 'minimatch';
|
|
7
|
+
import { parse } from 'yaml';
|
|
8
|
+
import { parse as parseJsonc } from 'jsonc-parser';
|
|
9
|
+
import { nativeResolutions } from "./native-resolutions.js";
|
|
10
|
+
import { configValue, readNativeConfig } from "./native-config.js";
|
|
11
|
+
export { captureNativeToolchainAsync } from "./toolchain.js";
|
|
12
|
+
export { readNativeConfig as resolveNativeConfigAsync } from "./native-config.js";
|
|
13
|
+
export const NATIVE_FINGERPRINT_FORMAT = 'octane-xplat.native-fingerprint.v1';
|
|
14
|
+
const hash = (bytes) => createHash('sha256').update(bytes).digest('hex');
|
|
15
|
+
const slash = (path) => path.split(sep).join('/');
|
|
16
|
+
const id = (value) => value.type === 'file' ? `file:${value.filePath}` : `contents:${value.id}`;
|
|
17
|
+
function stable(value) {
|
|
18
|
+
if (value === null || typeof value !== 'object') {
|
|
19
|
+
return JSON.stringify(value);
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
return '[' + value.map(stable).join(',') + ']';
|
|
23
|
+
}
|
|
24
|
+
return ('{' +
|
|
25
|
+
Object.entries(value)
|
|
26
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
27
|
+
.map(([key, value]) => JSON.stringify(key) + ':' + stable(value))
|
|
28
|
+
.join(',') +
|
|
29
|
+
'}');
|
|
30
|
+
}
|
|
31
|
+
async function json(file) {
|
|
32
|
+
return JSON.parse(await readFile(file, 'utf8'));
|
|
33
|
+
}
|
|
34
|
+
const within = (root, file) => {
|
|
35
|
+
const path = relative(root, file);
|
|
36
|
+
return path === '' || (!path.startsWith('..' + sep) && path !== '..' && !isAbsolute(path));
|
|
37
|
+
};
|
|
38
|
+
const ignoredDirectories = new Set([
|
|
39
|
+
'node_modules',
|
|
40
|
+
'.git',
|
|
41
|
+
'.cache',
|
|
42
|
+
'.turbo',
|
|
43
|
+
'build',
|
|
44
|
+
'dist',
|
|
45
|
+
'Pods',
|
|
46
|
+
'.gradle',
|
|
47
|
+
'DerivedData',
|
|
48
|
+
]);
|
|
49
|
+
async function walk(root, path, action, ancestors = new Set()) {
|
|
50
|
+
const actual = await realpath(path);
|
|
51
|
+
if (!within(root, actual)) {
|
|
52
|
+
throw new Error('Fingerprint source symlink escapes its declared root');
|
|
53
|
+
}
|
|
54
|
+
if (ancestors.has(actual)) {
|
|
55
|
+
throw new Error('Fingerprint source has a symlink cycle');
|
|
56
|
+
}
|
|
57
|
+
const stat = await lstat(actual);
|
|
58
|
+
if (stat.isFile()) {
|
|
59
|
+
await action(path);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (!stat.isDirectory()) {
|
|
63
|
+
throw new Error('Fingerprint requires ordinary files/directories');
|
|
64
|
+
}
|
|
65
|
+
const next = new Set(ancestors).add(actual);
|
|
66
|
+
for (const entry of (await readdir(path)).sort()) {
|
|
67
|
+
if (!ignoredDirectories.has(entry)) {
|
|
68
|
+
await walk(root, join(path, entry), action, next);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function options(value) {
|
|
73
|
+
if (!value ||
|
|
74
|
+
typeof value !== 'object' ||
|
|
75
|
+
Array.isArray(value) ||
|
|
76
|
+
Object.keys(value).some((key) => ![
|
|
77
|
+
'platforms',
|
|
78
|
+
'hashAlgorithm',
|
|
79
|
+
'ignorePaths',
|
|
80
|
+
'extraSources',
|
|
81
|
+
'toolchain',
|
|
82
|
+
'nativeConfig',
|
|
83
|
+
].includes(key))) {
|
|
84
|
+
throw new Error('Unsupported fingerprint option');
|
|
85
|
+
}
|
|
86
|
+
if (value.hashAlgorithm !== undefined && value.hashAlgorithm !== 'sha256') {
|
|
87
|
+
throw new Error('Native fingerprints require SHA-256');
|
|
88
|
+
}
|
|
89
|
+
if (value.platforms !== undefined &&
|
|
90
|
+
(!Array.isArray(value.platforms) ||
|
|
91
|
+
!value.platforms.length ||
|
|
92
|
+
value.platforms.some((p) => !['ios', 'android', 'macos', 'linux', 'windows'].includes(p)))) {
|
|
93
|
+
throw new Error('Invalid fingerprint platforms');
|
|
94
|
+
}
|
|
95
|
+
if (value.ignorePaths !== undefined &&
|
|
96
|
+
(!Array.isArray(value.ignorePaths) ||
|
|
97
|
+
value.ignorePaths.some((p) => typeof p !== 'string' || !p || isAbsolute(p)))) {
|
|
98
|
+
throw new Error('Fingerprint ignores must be relative glob patterns');
|
|
99
|
+
}
|
|
100
|
+
if (value.toolchain !== undefined &&
|
|
101
|
+
(!value.toolchain ||
|
|
102
|
+
typeof value.toolchain !== 'object' ||
|
|
103
|
+
Array.isArray(value.toolchain) ||
|
|
104
|
+
Object.values(value.toolchain).some((v) => typeof v !== 'string'))) {
|
|
105
|
+
throw new Error('Fingerprint toolchain values must be strings');
|
|
106
|
+
}
|
|
107
|
+
if (value.extraSources !== undefined && !Array.isArray(value.extraSources)) {
|
|
108
|
+
throw new Error('Fingerprint extraSources must be an array');
|
|
109
|
+
}
|
|
110
|
+
if (value.nativeConfig !== undefined &&
|
|
111
|
+
(!value.nativeConfig ||
|
|
112
|
+
typeof value.nativeConfig !== 'object' ||
|
|
113
|
+
Array.isArray(value.nativeConfig))) {
|
|
114
|
+
throw new Error('Resolved native configuration must be an object');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function resolvePackage(name, root, optional = false) {
|
|
118
|
+
const require = createRequire(join(root, 'package.json'));
|
|
119
|
+
for (const folder of require.resolve.paths(name) ?? []) {
|
|
120
|
+
const manifest = join(folder, name, 'package.json');
|
|
121
|
+
if (existsSync(manifest)) {
|
|
122
|
+
return realpathSync(join(folder, name));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (optional) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
throw new Error(`Fingerprint dependency ${name} is not installed`);
|
|
129
|
+
}
|
|
130
|
+
/** Collect installed native inputs and the app's resolved NativeScript configuration. */
|
|
131
|
+
export async function createFingerprintAsync(projectRoot, supplied = {}) {
|
|
132
|
+
options(supplied);
|
|
133
|
+
const root = await realpath(projectRoot);
|
|
134
|
+
for (const file of [
|
|
135
|
+
'fingerprint.config.js',
|
|
136
|
+
'fingerprint.config.cjs',
|
|
137
|
+
'fingerprint.config.mjs',
|
|
138
|
+
'fingerprint.config.ts',
|
|
139
|
+
]) {
|
|
140
|
+
if (existsSync(join(root, file))) {
|
|
141
|
+
throw new Error('Use data-only fingerprint.config.json; executable fingerprint config is unsupported');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const config = existsSync(join(root, 'fingerprint.config.json'))
|
|
145
|
+
? await json(join(root, 'fingerprint.config.json'))
|
|
146
|
+
: {};
|
|
147
|
+
options(config);
|
|
148
|
+
if (config.nativeConfig !== undefined) {
|
|
149
|
+
throw new Error('Resolved nativeConfig must come from the build hook, not fingerprint.config.json');
|
|
150
|
+
}
|
|
151
|
+
const value = {
|
|
152
|
+
...config,
|
|
153
|
+
...supplied,
|
|
154
|
+
toolchain: { ...config.toolchain, ...supplied.toolchain },
|
|
155
|
+
};
|
|
156
|
+
options(value);
|
|
157
|
+
const platforms = [...new Set(value.platforms ?? ['ios', 'android'])].sort();
|
|
158
|
+
const nativeConfig = value.nativeConfig ?? (await readNativeConfig(root));
|
|
159
|
+
const resourcePath = resolve(root, typeof nativeConfig.appResourcesPath === 'string'
|
|
160
|
+
? nativeConfig.appResourcesPath
|
|
161
|
+
: 'App_Resources');
|
|
162
|
+
const resources = existsSync(resourcePath) ? await realpath(resourcePath) : resourcePath;
|
|
163
|
+
const patterns = [...(config.ignorePaths ?? []), ...(supplied.ignorePaths ?? [])];
|
|
164
|
+
if (existsSync(join(root, '.fingerprintignore'))) {
|
|
165
|
+
patterns.push(...(await readFile(join(root, '.fingerprintignore'), 'utf8'))
|
|
166
|
+
.split(/\r?\n/)
|
|
167
|
+
.map((v) => v.trim())
|
|
168
|
+
.filter((v) => v && !v.startsWith('#')));
|
|
169
|
+
}
|
|
170
|
+
const excluded = (path) => {
|
|
171
|
+
let result = false;
|
|
172
|
+
for (const pattern of patterns) {
|
|
173
|
+
if (minimatch(path, pattern.startsWith('!') ? pattern.slice(1) : pattern, { dot: true })) {
|
|
174
|
+
result = !pattern.startsWith('!');
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
};
|
|
179
|
+
const records = new Map();
|
|
180
|
+
const add = (source) => {
|
|
181
|
+
const key = id(source), previous = records.get(key);
|
|
182
|
+
if (previous && previous.hash !== source.hash) {
|
|
183
|
+
throw new Error(`Ambiguous native source ${key}`);
|
|
184
|
+
}
|
|
185
|
+
records.set(key, previous
|
|
186
|
+
? { ...source, reasons: [...new Set([...previous.reasons, ...source.reasons])].sort() }
|
|
187
|
+
: source);
|
|
188
|
+
};
|
|
189
|
+
const contents = (name, bytes, reason) => add({ type: 'contents', id: name, reasons: [reason], hash: hash(stable(bytes)) });
|
|
190
|
+
const file = async (path, name, reason, critical = true, normalizeConfig = false) => {
|
|
191
|
+
const omit = excluded(name);
|
|
192
|
+
if (omit && critical) {
|
|
193
|
+
throw new Error(`Cannot ignore automatic native input ${name}`);
|
|
194
|
+
}
|
|
195
|
+
add({
|
|
196
|
+
type: 'file',
|
|
197
|
+
filePath: name,
|
|
198
|
+
reasons: [reason],
|
|
199
|
+
hash: omit
|
|
200
|
+
? null
|
|
201
|
+
: hash(normalizeConfig
|
|
202
|
+
? (await readFile(path, 'utf8'))
|
|
203
|
+
.replaceAll(resources, '<App_Resources>')
|
|
204
|
+
.replaceAll(root, '<project>')
|
|
205
|
+
: await readFile(path)),
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
const tree = async (base, path, prefix, reason, critical = true, declarationsOnly = false) => {
|
|
209
|
+
if (existsSync(path)) {
|
|
210
|
+
await walk(base, path, async (target) => {
|
|
211
|
+
if (reason === 'local native Pod sources' &&
|
|
212
|
+
/\.(?:[cm]?js|jsx|ts|tsx|tsrx|md|mdx|map)$/.test(target)) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (declarationsOnly && slash(relative(path, target)).startsWith('types/generated/')) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (!declarationsOnly || /\.d\.(?:ts|mts|cts)$/.test(target)) {
|
|
219
|
+
await file(target, prefix + slash(relative(path, target)), reason, critical);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
const app = await json(join(root, 'package.json'));
|
|
225
|
+
contents('collector', { format: NATIVE_FINGERPRINT_FORMAT, platforms }, 'collector contract');
|
|
226
|
+
contents('app-native-config', {
|
|
227
|
+
nativescript: app.nativescript ?? null,
|
|
228
|
+
xplat: app.xplat ?? null,
|
|
229
|
+
packageManager: app.packageManager ?? null,
|
|
230
|
+
toolchain: value.toolchain ?? {},
|
|
231
|
+
}, 'declared native build settings');
|
|
232
|
+
contents('resolved-native-config', configValue(nativeConfig, root, resources), 'evaluated native build configuration');
|
|
233
|
+
for (const name of [
|
|
234
|
+
'nativescript.config.ts',
|
|
235
|
+
'nativescript.config.js',
|
|
236
|
+
'nativescript.config.cjs',
|
|
237
|
+
'native-toolchain.json',
|
|
238
|
+
]) {
|
|
239
|
+
if (existsSync(join(root, name))) {
|
|
240
|
+
await file(join(root, name), name, 'native build configuration', true, true);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const names = {
|
|
244
|
+
ios: 'iOS',
|
|
245
|
+
android: 'Android',
|
|
246
|
+
macos: 'macos',
|
|
247
|
+
linux: 'linux',
|
|
248
|
+
windows: 'Windows',
|
|
249
|
+
};
|
|
250
|
+
for (const platform of platforms) {
|
|
251
|
+
await tree(resources, join(resources, names[platform]), `App_Resources/${names[platform]}/`, 'application native resources');
|
|
252
|
+
if (!['ios', 'android'].includes(platform)) {
|
|
253
|
+
await tree(root, join(root, 'platforms', platform), `platforms/${platform}/`, 'application desktop native sources');
|
|
254
|
+
}
|
|
255
|
+
const nativeSources = nativeConfig[platform]?.NativeSource;
|
|
256
|
+
if (nativeSources !== undefined) {
|
|
257
|
+
if (!Array.isArray(nativeSources)) {
|
|
258
|
+
throw new Error('NativeSource must be a named source array');
|
|
259
|
+
}
|
|
260
|
+
for (const source of nativeSources) {
|
|
261
|
+
if (!source ||
|
|
262
|
+
typeof source.name !== 'string' ||
|
|
263
|
+
!source.name ||
|
|
264
|
+
typeof source.path !== 'string' ||
|
|
265
|
+
!source.path) {
|
|
266
|
+
throw new Error('Invalid NativeSource declaration');
|
|
267
|
+
}
|
|
268
|
+
const pattern = resolve(root, source.path);
|
|
269
|
+
const prefix = pattern.split(/[*?[\]{}]/, 1)[0];
|
|
270
|
+
const base = await realpath(prefix.endsWith(sep) ? prefix : dirname(prefix));
|
|
271
|
+
let count = 0;
|
|
272
|
+
for await (const match of glob(slash(pattern))) {
|
|
273
|
+
await walk(base, match, async (input) => {
|
|
274
|
+
await file(input, `native-source/${platform}/${source.name}/${slash(relative(base, input))}`, 'resolved NativeSource declaration');
|
|
275
|
+
count++;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
if (!count) {
|
|
279
|
+
throw new Error(`NativeSource ${source.name} matches no files`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
await tree(root, join(root, 'native', platform), `native/${platform}/`, 'application native sources');
|
|
284
|
+
}
|
|
285
|
+
await tree(root, join(root, 'hooks'), 'hooks/', 'application native build hooks');
|
|
286
|
+
let lockRoot = root;
|
|
287
|
+
while (!existsSync(join(lockRoot, 'bun.lock')) &&
|
|
288
|
+
!existsSync(join(lockRoot, 'pnpm-lock.yaml')) &&
|
|
289
|
+
dirname(lockRoot) !== lockRoot) {
|
|
290
|
+
lockRoot = dirname(lockRoot);
|
|
291
|
+
}
|
|
292
|
+
const owner = existsSync(join(lockRoot, 'package.json'))
|
|
293
|
+
? await json(join(lockRoot, 'package.json'))
|
|
294
|
+
: {};
|
|
295
|
+
const manager = owner.packageManager?.split('@')[0];
|
|
296
|
+
if (manager !== 'bun' &&
|
|
297
|
+
manager !== 'pnpm' &&
|
|
298
|
+
existsSync(join(lockRoot, 'bun.lock')) &&
|
|
299
|
+
existsSync(join(lockRoot, 'pnpm-lock.yaml'))) {
|
|
300
|
+
throw new Error('Both Bun and pnpm lockfiles exist. Set packageManager in package.json.');
|
|
301
|
+
}
|
|
302
|
+
const bun = manager === 'bun' || (manager !== 'pnpm' && existsSync(join(lockRoot, 'bun.lock')));
|
|
303
|
+
const lockFile = join(lockRoot, bun ? 'bun.lock' : 'pnpm-lock.yaml');
|
|
304
|
+
let lock = null;
|
|
305
|
+
if (existsSync(lockFile)) {
|
|
306
|
+
const text = await readFile(lockFile, 'utf8');
|
|
307
|
+
if (bun) {
|
|
308
|
+
const errors = [];
|
|
309
|
+
lock = parseJsonc(text, errors, { allowTrailingComma: true });
|
|
310
|
+
if (errors.length) {
|
|
311
|
+
throw new Error(`Invalid Bun lockfile: ${lockFile}`);
|
|
312
|
+
}
|
|
313
|
+
lock.importers = lock.workspaces;
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
lock = parse(text);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (!lock?.packages || typeof lock.packages !== 'object') {
|
|
320
|
+
throw new Error('A resolved Bun or pnpm lockfile is required for native fingerprints');
|
|
321
|
+
}
|
|
322
|
+
const workspace = !bun && existsSync(join(lockRoot, 'pnpm-workspace.yaml'))
|
|
323
|
+
? parse(await readFile(join(lockRoot, 'pnpm-workspace.yaml'), 'utf8'))
|
|
324
|
+
: {};
|
|
325
|
+
const patchMap = bun
|
|
326
|
+
? (owner.patchedDependencies ?? {})
|
|
327
|
+
: {
|
|
328
|
+
...workspace?.patchedDependencies,
|
|
329
|
+
...app.pnpm?.patchedDependencies,
|
|
330
|
+
};
|
|
331
|
+
const visited = new Set();
|
|
332
|
+
const nativeIdentities = new Map();
|
|
333
|
+
async function visit(packageRoot, requiredName, parent = app.name ?? '') {
|
|
334
|
+
let bunKey = requiredName;
|
|
335
|
+
let bunEntry;
|
|
336
|
+
if (bun) {
|
|
337
|
+
for (let prefix = parent;; prefix = prefix.includes('/') ? prefix.slice(0, prefix.lastIndexOf('/')) : '') {
|
|
338
|
+
const key = prefix ? `${prefix}/${requiredName}` : requiredName;
|
|
339
|
+
if (lock.packages[key]) {
|
|
340
|
+
bunKey = key;
|
|
341
|
+
bunEntry = lock.packages[key];
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
if (!prefix) {
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (visited.has(packageRoot)) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
visited.add(packageRoot);
|
|
353
|
+
const pkg = await json(join(packageRoot, 'package.json'));
|
|
354
|
+
if (typeof pkg.name !== 'string' || typeof pkg.version !== 'string') {
|
|
355
|
+
throw new Error(`Incomplete dependency metadata for ${requiredName}`);
|
|
356
|
+
}
|
|
357
|
+
const runtimePlatform = /^@nativescript\/(?:types-)?(ios|android)$/.exec(pkg.name)?.[1];
|
|
358
|
+
const native = (!runtimePlatform || platforms.includes(runtimePlatform)) &&
|
|
359
|
+
((platforms.includes('macos') && pkg.xplat?.macos !== undefined) ||
|
|
360
|
+
platforms.some((platform) => existsSync(join(packageRoot, 'platforms', platform))) ||
|
|
361
|
+
existsSync(join(packageRoot, 'native')) ||
|
|
362
|
+
['include.gradle', 'Podfile', 'Package.swift'].some((name) => existsSync(join(packageRoot, name))) ||
|
|
363
|
+
/^@nativescript\/(?:core|ios|android|types(?:-.+)?)$/.test(pkg.name));
|
|
364
|
+
if (native) {
|
|
365
|
+
const identity = `${pkg.name}@${pkg.version}`, prefix = `node_modules/${pkg.name}/`;
|
|
366
|
+
const lockEntries = bun
|
|
367
|
+
? []
|
|
368
|
+
: Object.entries(lock.packages)
|
|
369
|
+
.filter(([key, raw]) => {
|
|
370
|
+
if (key === identity || key.startsWith(identity + '(')) {
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
const entry = raw;
|
|
374
|
+
return (key.startsWith(pkg.name + '@file:') &&
|
|
375
|
+
entry.resolution?.type === 'directory' &&
|
|
376
|
+
typeof entry.resolution.directory === 'string' &&
|
|
377
|
+
existsSync(resolve(lockRoot, entry.resolution.directory)) &&
|
|
378
|
+
realpathSync(resolve(lockRoot, entry.resolution.directory)) === packageRoot);
|
|
379
|
+
})
|
|
380
|
+
.map(([key, raw]) => {
|
|
381
|
+
const entry = raw;
|
|
382
|
+
return entry.resolution?.type === 'directory'
|
|
383
|
+
? [identity + ':directory', { ...entry, resolution: { type: 'directory' } }]
|
|
384
|
+
: [key, entry];
|
|
385
|
+
});
|
|
386
|
+
if (bunEntry) {
|
|
387
|
+
const source = String(bunEntry[0]);
|
|
388
|
+
const at = source.indexOf('@', 1);
|
|
389
|
+
const spec = source.slice(at + 1);
|
|
390
|
+
if (source.slice(0, at) !== pkg.name ||
|
|
391
|
+
(/^\d+\.\d+\.\d+/.test(spec) && spec !== pkg.version)) {
|
|
392
|
+
throw new Error(`Native dependency ${identity} disagrees with Bun lock metadata ${source}`);
|
|
393
|
+
}
|
|
394
|
+
// Integrity identifies local archives without hashing checkout-specific paths.
|
|
395
|
+
const local = /^(?:file:|link:|workspace:|\/|[A-Za-z]:)/.test(spec) ||
|
|
396
|
+
(spec.endsWith('.tgz') && !/^https?:/.test(spec));
|
|
397
|
+
lockEntries.push([
|
|
398
|
+
identity,
|
|
399
|
+
{
|
|
400
|
+
source: local ? '<local>' : source,
|
|
401
|
+
integrity: bunEntry.find((value) => typeof value === 'string' && /^sha(?:256|512)-/.test(value)) ?? null,
|
|
402
|
+
},
|
|
403
|
+
]);
|
|
404
|
+
}
|
|
405
|
+
if (!lockEntries.length &&
|
|
406
|
+
!Object.prototype.hasOwnProperty.call(lock.importers ?? {}, slash(relative(lockRoot, packageRoot)))) {
|
|
407
|
+
throw new Error(`Native dependency ${identity} is missing lock metadata`);
|
|
408
|
+
}
|
|
409
|
+
const nativeDescriptor = stable({
|
|
410
|
+
name: pkg.name,
|
|
411
|
+
version: pkg.version,
|
|
412
|
+
nativescript: pkg.nativescript ?? null,
|
|
413
|
+
xplat: pkg.xplat ?? null,
|
|
414
|
+
lock: lockEntries.map(([key, entry]) => [key, entry]),
|
|
415
|
+
patch: lock.patchedDependencies?.[identity] ?? null,
|
|
416
|
+
});
|
|
417
|
+
const old = nativeIdentities.get(identity);
|
|
418
|
+
if (old && old !== nativeDescriptor) {
|
|
419
|
+
throw new Error(`Ambiguous installed native dependency ${identity}`);
|
|
420
|
+
}
|
|
421
|
+
nativeIdentities.set(identity, nativeDescriptor);
|
|
422
|
+
contents(`dependency:${identity}`, JSON.parse(nativeDescriptor), 'installed native dependency and lock resolution');
|
|
423
|
+
for (const platform of platforms) {
|
|
424
|
+
await tree(packageRoot, join(packageRoot, 'platforms', platform), prefix + `platforms/${platform}/`, 'native dependency sources');
|
|
425
|
+
}
|
|
426
|
+
if (/^@nativescript\/(?:ios|android)$/.test(pkg.name)) {
|
|
427
|
+
await tree(packageRoot, packageRoot, prefix, 'native runtime engine files');
|
|
428
|
+
}
|
|
429
|
+
for (const directory of ['libs', 'frameworks', 'jniLibs']) {
|
|
430
|
+
await tree(packageRoot, join(packageRoot, directory), prefix + directory + '/', 'native dependency libraries');
|
|
431
|
+
}
|
|
432
|
+
await tree(packageRoot, join(packageRoot, 'native'), prefix + 'native/', 'native bootstrap sources');
|
|
433
|
+
await tree(packageRoot, join(packageRoot, 'hooks'), prefix + 'hooks/', 'native build hooks');
|
|
434
|
+
for (const filename of [
|
|
435
|
+
'nativescript.config.js',
|
|
436
|
+
'nativescript.config.ts',
|
|
437
|
+
'include.gradle',
|
|
438
|
+
'Podfile',
|
|
439
|
+
'Package.swift',
|
|
440
|
+
'prepare.cjs',
|
|
441
|
+
]) {
|
|
442
|
+
if (existsSync(join(packageRoot, filename))) {
|
|
443
|
+
await file(join(packageRoot, filename), prefix + filename, 'native dependency build settings');
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
await tree(packageRoot, packageRoot, prefix, 'native metadata declarations', true, true);
|
|
447
|
+
const patch = patchMap[identity];
|
|
448
|
+
if (patch) {
|
|
449
|
+
const path = typeof patch === 'string' ? patch : patch.path;
|
|
450
|
+
if (typeof path !== 'string' || !within(lockRoot, resolve(lockRoot, path))) {
|
|
451
|
+
throw new Error('Invalid native dependency patch path');
|
|
452
|
+
}
|
|
453
|
+
await file(resolve(lockRoot, path), `patches/${identity}`, 'native dependency patch');
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
for (const name of Object.keys({ ...pkg.dependencies, ...pkg.optionalDependencies }).sort()) {
|
|
457
|
+
const dependency = resolvePackage(name, packageRoot, Boolean(pkg.optionalDependencies?.[name] && !pkg.dependencies?.[name]));
|
|
458
|
+
if (dependency) {
|
|
459
|
+
await visit(dependency, name, bunKey);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const dependencies = {
|
|
464
|
+
...app.dependencies,
|
|
465
|
+
...app.optionalDependencies,
|
|
466
|
+
...Object.fromEntries(Object.entries(app.devDependencies ?? {}).filter(([name]) => name.startsWith('@nativescript/'))),
|
|
467
|
+
};
|
|
468
|
+
for (const name of Object.keys(dependencies).sort()) {
|
|
469
|
+
const dependency = resolvePackage(name, root, Boolean(app.optionalDependencies?.[name] && !app.dependencies?.[name]));
|
|
470
|
+
if (dependency) {
|
|
471
|
+
await visit(dependency, name);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
await nativeResolutions(root, platforms, nativeConfig, (name, value) => contents(name, value, 'resolved native dependency lock'), tree);
|
|
475
|
+
for (const source of [
|
|
476
|
+
...(config.extraSources ?? []),
|
|
477
|
+
...(supplied.extraSources ?? []),
|
|
478
|
+
]) {
|
|
479
|
+
if (!source ||
|
|
480
|
+
!Array.isArray(source.reasons) ||
|
|
481
|
+
source.reasons.some((reason) => typeof reason !== 'string')) {
|
|
482
|
+
throw new Error('Invalid extra fingerprint source');
|
|
483
|
+
}
|
|
484
|
+
if (source.type === 'contents') {
|
|
485
|
+
if (typeof source.id !== 'string' || !source.id || typeof source.contents !== 'string') {
|
|
486
|
+
throw new Error('Invalid contents source');
|
|
487
|
+
}
|
|
488
|
+
add({
|
|
489
|
+
type: 'contents',
|
|
490
|
+
id: source.id,
|
|
491
|
+
reasons: source.reasons,
|
|
492
|
+
hash: excluded(source.id) ? null : hash(source.contents),
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
else if (source.type === 'file' || source.type === 'dir') {
|
|
496
|
+
if (typeof source.filePath !== 'string' || isAbsolute(source.filePath)) {
|
|
497
|
+
throw new Error('Extra sources must be relative paths');
|
|
498
|
+
}
|
|
499
|
+
const path = resolve(root, source.filePath), actual = await realpath(path);
|
|
500
|
+
if (!within(root, actual)) {
|
|
501
|
+
throw new Error('Extra source escapes the project root');
|
|
502
|
+
}
|
|
503
|
+
if (source.type === 'file') {
|
|
504
|
+
await file(path, slash(relative(root, path)), source.reasons.join('; '), false);
|
|
505
|
+
}
|
|
506
|
+
else {
|
|
507
|
+
await tree(root, path, slash(relative(root, path)) + '/', source.reasons.join('; '), false);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
throw new Error('Unknown fingerprint source type');
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
const sources = [...records.values()].sort((a, b) => (id(a) < id(b) ? -1 : id(a) > id(b) ? 1 : 0));
|
|
515
|
+
return {
|
|
516
|
+
sources,
|
|
517
|
+
hash: hash(stable(sources.map((source) => ({ id: id(source), hash: source.hash })))),
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
export async function createProjectHashAsync(root, options) {
|
|
521
|
+
return (await createFingerprintAsync(root, options)).hash;
|
|
522
|
+
}
|
|
523
|
+
export function diffFingerprints(before, after) {
|
|
524
|
+
const a = new Map(before.sources.map((source) => [id(source), source])), b = new Map(after.sources.map((source) => [id(source), source]));
|
|
525
|
+
return [...new Set([...a.keys(), ...b.keys()])].sort().flatMap((key) => {
|
|
526
|
+
const old = a.get(key), next = b.get(key);
|
|
527
|
+
return !old
|
|
528
|
+
? [{ op: 'added', addedSource: next }]
|
|
529
|
+
: !next
|
|
530
|
+
? [{ op: 'removed', removedSource: old }]
|
|
531
|
+
: old.hash !== next.hash
|
|
532
|
+
? [{ op: 'changed', beforeSource: old, afterSource: next }]
|
|
533
|
+
: [];
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
export async function diffFingerprintChangesAsync(fingerprint, root, options) {
|
|
537
|
+
return diffFingerprints(fingerprint, await createFingerprintAsync(root, options));
|
|
538
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Evaluate the trusted app config as NativeScript does, with a fresh module per call. */
|
|
2
|
+
export declare function readNativeConfig(root: string): Promise<Record<string, any>>;
|
|
3
|
+
/** Stable, value-only config. Unserializable settings fail instead of silently disappearing. */
|
|
4
|
+
export declare function configValue(value: unknown, root: string, resources: string): unknown;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { dirname, resolve } from 'node:path';
|
|
5
|
+
import { compileFunction } from 'node:vm';
|
|
6
|
+
import { ModuleKind, transpileModule } from 'typescript';
|
|
7
|
+
/** Evaluate the trusted app config as NativeScript does, with a fresh module per call. */
|
|
8
|
+
export async function readNativeConfig(root) {
|
|
9
|
+
const override = process.env.NATIVESCRIPT_CONFIG_NAME;
|
|
10
|
+
const defaults = ['nativescript.config.ts', 'nativescript.config.js', 'nsconfig.json'];
|
|
11
|
+
const candidates = override === 'false'
|
|
12
|
+
? ['nsconfig.json']
|
|
13
|
+
: override
|
|
14
|
+
? [
|
|
15
|
+
override.endsWith('.ts') || override.endsWith('.js') || override.endsWith('.json')
|
|
16
|
+
? override
|
|
17
|
+
: override + '.ts',
|
|
18
|
+
override + '.js',
|
|
19
|
+
override + '.json',
|
|
20
|
+
...defaults,
|
|
21
|
+
]
|
|
22
|
+
: defaults;
|
|
23
|
+
const file = candidates.map((name) => resolve(root, name)).find(existsSync);
|
|
24
|
+
if (!file) {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const modules = new Map();
|
|
29
|
+
const quiet = { log() { }, info() { }, warn() { }, error() { }, debug() { } };
|
|
30
|
+
function load(path) {
|
|
31
|
+
const previous = modules.get(path);
|
|
32
|
+
if (previous) {
|
|
33
|
+
return previous.exports;
|
|
34
|
+
}
|
|
35
|
+
const module = { exports: {} };
|
|
36
|
+
modules.set(path, module);
|
|
37
|
+
const source = readFileSync(path, 'utf8');
|
|
38
|
+
if (path.endsWith('.json')) {
|
|
39
|
+
module.exports = JSON.parse(source);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
const code = path.endsWith('.ts')
|
|
43
|
+
? transpileModule(source, { compilerOptions: { module: ModuleKind.CommonJS } }).outputText
|
|
44
|
+
: source;
|
|
45
|
+
const require = createRequire(path);
|
|
46
|
+
const localRequire = Object.assign((name) => name.startsWith('.') || name.startsWith('/')
|
|
47
|
+
? load(require.resolve(name))
|
|
48
|
+
: require(name), { resolve: require.resolve });
|
|
49
|
+
compileFunction(code, ['exports', 'require', 'module', '__filename', '__dirname', 'console'], { filename: path })(module.exports, localRequire, module, path, dirname(path), quiet);
|
|
50
|
+
}
|
|
51
|
+
return module.exports;
|
|
52
|
+
}
|
|
53
|
+
const exported = load(file);
|
|
54
|
+
const value = exported.default ?? exported;
|
|
55
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
56
|
+
throw new Error();
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
throw new Error('Cannot evaluate NativeScript configuration; supply a valid exported configuration object');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Stable, value-only config. Unserializable settings fail instead of silently disappearing. */
|
|
65
|
+
export function configValue(value, root, resources) {
|
|
66
|
+
if (value === undefined) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
if (value === null || typeof value === 'boolean' || typeof value === 'number') {
|
|
70
|
+
if (typeof value === 'number' && !Number.isFinite(value)) {
|
|
71
|
+
throw new Error('Native build settings must be finite JSON values');
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
if (typeof value === 'string') {
|
|
76
|
+
return value.replaceAll(resources, '<App_Resources>').replaceAll(root, '<project>');
|
|
77
|
+
}
|
|
78
|
+
if (Array.isArray(value)) {
|
|
79
|
+
return value.map((item) => configValue(item, root, resources));
|
|
80
|
+
}
|
|
81
|
+
if (typeof value !== 'object' || Object.getPrototypeOf(value) !== Object.prototype) {
|
|
82
|
+
throw new Error('Native build settings must contain serializable values');
|
|
83
|
+
}
|
|
84
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, configValue(item, root, resources)]));
|
|
85
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/** Native lock records omit cache paths and derived origin hashes, retaining resolved identities. */
|
|
2
|
+
export declare function nativeResolutions(root: string, platforms: readonly string[], nativeConfig: Record<string, any>, add: (id: string, value: unknown) => void, tree: (base: string, path: string, prefix: string, reason: string, critical?: boolean, declarationsOnly?: boolean) => Promise<void>): Promise<void>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { glob, readFile, realpath } from 'node:fs/promises';
|
|
3
|
+
import { join, resolve, sep } from 'node:path';
|
|
4
|
+
import { parse } from 'yaml';
|
|
5
|
+
import { configValue } from "./native-config.js";
|
|
6
|
+
/** Native lock records omit cache paths and derived origin hashes, retaining resolved identities. */
|
|
7
|
+
export async function nativeResolutions(root, platforms, nativeConfig, add, tree) {
|
|
8
|
+
const nativeRoot = resolve(root, typeof nativeConfig.buildPath === 'string' ? nativeConfig.buildPath : 'platforms');
|
|
9
|
+
if (platforms.includes('ios')) {
|
|
10
|
+
const ios = join(nativeRoot, 'ios'), podfile = join(ios, 'Podfile.lock');
|
|
11
|
+
if (existsSync(podfile)) {
|
|
12
|
+
const value = parse(await readFile(podfile, 'utf8'));
|
|
13
|
+
if (!value || typeof value !== 'object' || !Array.isArray(value.PODS)) {
|
|
14
|
+
throw new Error('Invalid native Podfile.lock');
|
|
15
|
+
}
|
|
16
|
+
delete value['PODFILE CHECKSUM'];
|
|
17
|
+
for (const [name, source] of Object.entries(value['EXTERNAL SOURCES'] ?? {})) {
|
|
18
|
+
const path = source[':path'];
|
|
19
|
+
if (typeof path === 'string') {
|
|
20
|
+
const directory = await realpath(resolve(ios, path));
|
|
21
|
+
await tree(directory, directory, `native-pod/${name}/`, 'local native Pod sources');
|
|
22
|
+
source[':path'] = '<local-native-source>';
|
|
23
|
+
}
|
|
24
|
+
const spec = source[':podspec'];
|
|
25
|
+
if (typeof spec === 'string' && !/^https?:/.test(spec)) {
|
|
26
|
+
source[':podspec'] = spec.replaceAll(root, '<project>');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
add('native-resolution:ios/Podfile.lock', configValue(value, root, root));
|
|
30
|
+
}
|
|
31
|
+
if (existsSync(ios)) {
|
|
32
|
+
for await (const file of glob('**/Package.resolved', {
|
|
33
|
+
cwd: ios,
|
|
34
|
+
exclude: ['**/Pods/**', '**/build/**', '**/DerivedData/**'],
|
|
35
|
+
})) {
|
|
36
|
+
const value = JSON.parse(await readFile(join(ios, file), 'utf8'));
|
|
37
|
+
const pins = value.pins ?? value.object?.pins;
|
|
38
|
+
if (![1, 2, 3].includes(value.version) ||
|
|
39
|
+
!Array.isArray(pins) ||
|
|
40
|
+
pins.some((pin) => !pin ||
|
|
41
|
+
typeof pin !== 'object' ||
|
|
42
|
+
!pin.state ||
|
|
43
|
+
(typeof pin.state.revision !== 'string' && typeof pin.state.version !== 'string'))) {
|
|
44
|
+
throw new Error('Unsupported native Swift package resolution metadata');
|
|
45
|
+
}
|
|
46
|
+
const logical = file
|
|
47
|
+
.split(sep)
|
|
48
|
+
.join('/')
|
|
49
|
+
.replace(/[^/]+\.(xcworkspace|xcodeproj)(?=\/)/g, 'project.$1');
|
|
50
|
+
const identity = (pin) => pin.identity ?? pin.package ?? pin.repositoryURL ?? pin.location ?? '';
|
|
51
|
+
if (pins.some((pin) => typeof identity(pin) !== 'string' || !identity(pin)) ||
|
|
52
|
+
new Set(pins.map(identity)).size !== pins.length) {
|
|
53
|
+
throw new Error('Ambiguous native Swift package identities');
|
|
54
|
+
}
|
|
55
|
+
add('native-resolution:ios/' + logical, configValue({
|
|
56
|
+
version: value.version,
|
|
57
|
+
pins: [...pins].sort((a, b) => identity(a) < identity(b) ? -1 : identity(a) > identity(b) ? 1 : 0),
|
|
58
|
+
}, root, resolve(root, nativeConfig.appResourcesPath ?? 'App_Resources')));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (platforms.includes('android')) {
|
|
63
|
+
const android = join(nativeRoot, 'android');
|
|
64
|
+
if (existsSync(android)) {
|
|
65
|
+
for await (const file of glob(['**/gradle.lockfile', '**/dependency-locks/*.lockfile', '**/libs.versions.toml'], { cwd: android, exclude: ['**/build/**', '**/.gradle/**'] })) {
|
|
66
|
+
add('native-resolution:android/' + file.split(sep).join('/'), (await readFile(join(android, file), 'utf8')).replaceAll(root, '<project>'));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Platform } from './types.ts';
|
|
2
|
+
/** Capture compiler/SDK identity at native prepare; publishing reuses these recorded public versions. */
|
|
3
|
+
export declare function captureNativeToolchainAsync(projectRoot: string, platform: Platform): Promise<Record<string, string>>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import { readFile, readdir } from 'node:fs/promises';
|
|
6
|
+
import { join, resolve } from 'node:path';
|
|
7
|
+
/** Capture compiler/SDK identity at native prepare; publishing reuses these recorded public versions. */
|
|
8
|
+
export async function captureNativeToolchainAsync(projectRoot, platform) {
|
|
9
|
+
const overrides = {};
|
|
10
|
+
for (const key of [
|
|
11
|
+
'CFLAGS',
|
|
12
|
+
'CPPFLAGS',
|
|
13
|
+
'CXXFLAGS',
|
|
14
|
+
'OBJCFLAGS',
|
|
15
|
+
'OTHER_CFLAGS',
|
|
16
|
+
'OTHER_SWIFT_FLAGS',
|
|
17
|
+
'LDFLAGS',
|
|
18
|
+
'CC',
|
|
19
|
+
'CXX',
|
|
20
|
+
'SWIFT_VERSION',
|
|
21
|
+
'ARCHS',
|
|
22
|
+
'VALID_ARCHS',
|
|
23
|
+
'SDKROOT',
|
|
24
|
+
'IPHONEOS_DEPLOYMENT_TARGET',
|
|
25
|
+
'MACOSX_DEPLOYMENT_TARGET',
|
|
26
|
+
'JAVA_TOOL_OPTIONS',
|
|
27
|
+
'GRADLE_OPTS',
|
|
28
|
+
'ANDROID_NDK_HOME',
|
|
29
|
+
'ANDROID_NDK_ROOT',
|
|
30
|
+
]) {
|
|
31
|
+
if (process.env[key] !== undefined) {
|
|
32
|
+
overrides[key] = process.env[key].replaceAll(resolve(projectRoot), '<project>');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const environment = createHash('sha256').update(JSON.stringify(overrides)).digest('hex');
|
|
36
|
+
if (platform === 'ios' || platform === 'macos') {
|
|
37
|
+
try {
|
|
38
|
+
const xcode = execFileSync('xcodebuild', ['-version'], {
|
|
39
|
+
encoding: 'utf8',
|
|
40
|
+
timeout: 15000,
|
|
41
|
+
}).match(/^Xcode [0-9.]+\r?\nBuild version [A-Za-z0-9]+/m)?.[0];
|
|
42
|
+
const sdk = execFileSync('xcrun', ['--sdk', platform === 'ios' ? 'iphoneos' : 'macosx', '--show-sdk-version'], { encoding: 'utf8', timeout: 15000 }).trim();
|
|
43
|
+
if (!xcode || !/^[0-9.]+$/.test(sdk)) {
|
|
44
|
+
throw new Error();
|
|
45
|
+
}
|
|
46
|
+
return { xcode, sdk, environment };
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
throw new Error('Cannot identify the Apple compiler/SDK for this native build');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (platform === 'android') {
|
|
53
|
+
let sdk = process.env.ANDROID_HOME ?? process.env.ANDROID_SDK_ROOT;
|
|
54
|
+
const properties = join(resolve(projectRoot), 'platforms/android/local.properties');
|
|
55
|
+
if (!sdk && existsSync(properties)) {
|
|
56
|
+
sdk = (await readFile(properties, 'utf8'))
|
|
57
|
+
.split(/\r?\n/)
|
|
58
|
+
.find((line) => line.startsWith('sdk.dir='))
|
|
59
|
+
?.slice(8)
|
|
60
|
+
.replaceAll('\\:', ':')
|
|
61
|
+
.replaceAll('\\\\', '\\');
|
|
62
|
+
}
|
|
63
|
+
if (!sdk || !existsSync(sdk)) {
|
|
64
|
+
throw new Error('Cannot identify the Android SDK for this native build');
|
|
65
|
+
}
|
|
66
|
+
const versions = {};
|
|
67
|
+
for (const group of ['platforms', 'build-tools', 'ndk', 'cmdline-tools']) {
|
|
68
|
+
const path = join(sdk, group);
|
|
69
|
+
if (!existsSync(path)) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
for (const name of (await readdir(path)).sort()) {
|
|
73
|
+
const properties = join(path, name, 'source.properties');
|
|
74
|
+
if (existsSync(properties)) {
|
|
75
|
+
versions[`${group}/${name}`] = (await readFile(properties, 'utf8')).trim();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (!Object.keys(versions).some((name) => name.startsWith('platforms/'))) {
|
|
80
|
+
throw new Error('No installed Android platform SDK metadata');
|
|
81
|
+
}
|
|
82
|
+
const java = spawnSync(process.env.JAVA_HOME ? join(process.env.JAVA_HOME, 'bin/java') : 'java', ['-version'], { encoding: 'utf8', timeout: 15000 });
|
|
83
|
+
if (java.status !== 0 || !(java.stdout + java.stderr).trim()) {
|
|
84
|
+
throw new Error('Cannot identify the Java toolchain for this native build');
|
|
85
|
+
}
|
|
86
|
+
const version = (java.stdout + java.stderr).match(/^(?:openjdk|java)(?: version)? "?([0-9][A-Za-z0-9._+-]*)/m)?.[1];
|
|
87
|
+
if (!version) {
|
|
88
|
+
throw new Error('Cannot parse the Java toolchain version');
|
|
89
|
+
}
|
|
90
|
+
versions.java = version;
|
|
91
|
+
return { ...versions, environment };
|
|
92
|
+
}
|
|
93
|
+
throw new Error('Automatic native toolchain capture is implemented for Apple and Android builds');
|
|
94
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type Platform = 'ios' | 'android' | 'macos' | 'linux' | 'windows';
|
|
2
|
+
export type HashSource = {
|
|
3
|
+
type: 'file' | 'dir';
|
|
4
|
+
filePath: string;
|
|
5
|
+
reasons: string[];
|
|
6
|
+
} | {
|
|
7
|
+
type: 'contents';
|
|
8
|
+
id: string;
|
|
9
|
+
contents: string;
|
|
10
|
+
reasons: string[];
|
|
11
|
+
};
|
|
12
|
+
/** Input contents are deliberately omitted; output contains paths/IDs and hashes only. */
|
|
13
|
+
export type FingerprintSource = ({
|
|
14
|
+
type: 'file';
|
|
15
|
+
filePath: string;
|
|
16
|
+
} | {
|
|
17
|
+
type: 'contents';
|
|
18
|
+
id: string;
|
|
19
|
+
}) & {
|
|
20
|
+
reasons: string[];
|
|
21
|
+
hash: string | null;
|
|
22
|
+
};
|
|
23
|
+
export interface Fingerprint {
|
|
24
|
+
sources: FingerprintSource[];
|
|
25
|
+
hash: string;
|
|
26
|
+
}
|
|
27
|
+
export type FingerprintDiffItem = {
|
|
28
|
+
op: 'added';
|
|
29
|
+
addedSource: FingerprintSource;
|
|
30
|
+
} | {
|
|
31
|
+
op: 'removed';
|
|
32
|
+
removedSource: FingerprintSource;
|
|
33
|
+
} | {
|
|
34
|
+
op: 'changed';
|
|
35
|
+
beforeSource: FingerprintSource;
|
|
36
|
+
afterSource: FingerprintSource;
|
|
37
|
+
};
|
|
38
|
+
export interface Options {
|
|
39
|
+
platforms?: Platform[];
|
|
40
|
+
/** SHA-256 is fixed for the versioned native compatibility contract. */
|
|
41
|
+
hashAlgorithm?: 'sha256';
|
|
42
|
+
/** Applied to extraSources. Excluding an automatic native input fails explicitly. */
|
|
43
|
+
ignorePaths?: string[];
|
|
44
|
+
extraSources?: HashSource[];
|
|
45
|
+
/** Declared SDK/compiler/build variables that affect native output. No host paths or secrets. */
|
|
46
|
+
toolchain?: Record<string, string>;
|
|
47
|
+
/** Resolved NativeScript configuration from its prepare hook; evaluated from disk otherwise. */
|
|
48
|
+
nativeConfig?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
export type Config = Options;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@octane-xplat/fingerprint",
|
|
3
|
+
"version": "0.19.0",
|
|
4
|
+
"description": "Expo-shaped native compatibility fingerprints for Octane Xplat projects",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
9
|
+
"directory": "packages/fingerprint"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"xplat-fingerprint": "./dist/cli.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsc -p tsconfig.build.json",
|
|
30
|
+
"typecheck": "tsc -p tsconfig.build.json --noEmit",
|
|
31
|
+
"test": "node --test tests/*.test.mjs",
|
|
32
|
+
"prepack": "bun run build",
|
|
33
|
+
"test:packed": "node tests/packed-consumer.mjs"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"jsonc-parser": "3.3.1",
|
|
37
|
+
"minimatch": "10.2.6",
|
|
38
|
+
"typescript": "5.9.3",
|
|
39
|
+
"yaml": "2.9.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "24.10.1"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=22.22.2"
|
|
46
|
+
}
|
|
47
|
+
}
|