vouchington-tooling 0.4.0 → 0.4.1
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.
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export type NativeFamily = 'elf' | 'macho' | 'pe';
|
|
2
2
|
export declare function nativeFamilyFromMagic(buffer: Buffer): NativeFamily | undefined;
|
|
3
3
|
export declare function expectedNativeFamily(platform?: NodeJS.Platform): NativeFamily | undefined;
|
|
4
|
-
|
|
4
|
+
declare function packageExcludesPlatform(owner: string, platform: string): Promise<boolean>;
|
|
5
|
+
export declare function mismatchedNativeBinaries(root?: string, platform?: NodeJS.Platform, readPackageExclusion?: typeof packageExcludesPlatform): Promise<string[]>;
|
|
5
6
|
export declare function nativeBinariesMatchRuntime(root?: string, platform?: NodeJS.Platform): Promise<boolean>;
|
|
6
7
|
export declare function repairedNativeBinariesMatchRuntime(paths: string[], platform?: NodeJS.Platform): Promise<boolean>;
|
|
8
|
+
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { glob, open, stat } from 'node:fs/promises';
|
|
1
|
+
import { glob, open, readFile, stat } from 'node:fs/promises';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
const ELF = Buffer.from([0x7f, 0x45, 0x4c, 0x46]);
|
|
4
4
|
const PE = Buffer.from([0x4d, 0x5a]);
|
|
@@ -58,7 +58,38 @@ async function searchRoot(nodeModules) {
|
|
|
58
58
|
return nodeModules;
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
-
|
|
61
|
+
function excludesPlatform(os, platform) {
|
|
62
|
+
const values = typeof os === 'string' ? [os] : Array.isArray(os) ? os : undefined;
|
|
63
|
+
if (values === undefined || !values.every((value) => typeof value === 'string'))
|
|
64
|
+
return false;
|
|
65
|
+
if (values.length === 1 && values[0] === 'any')
|
|
66
|
+
return false;
|
|
67
|
+
if (values.includes(`!${platform}`))
|
|
68
|
+
return true;
|
|
69
|
+
return values.some((value) => !value.startsWith('!')) && !values.includes(platform);
|
|
70
|
+
}
|
|
71
|
+
function packageRoot(pathname, searchRoot) {
|
|
72
|
+
const segments = path.relative(searchRoot, pathname).split(path.sep);
|
|
73
|
+
const nodeModules = segments.lastIndexOf('node_modules');
|
|
74
|
+
const packageStart = nodeModules === -1 ? 0 : nodeModules + 1;
|
|
75
|
+
const name = segments[packageStart];
|
|
76
|
+
if (name === undefined || name === '.bin')
|
|
77
|
+
return undefined;
|
|
78
|
+
const packageLength = name.startsWith('@') ? 2 : 1;
|
|
79
|
+
if (segments.length <= packageStart + packageLength)
|
|
80
|
+
return undefined;
|
|
81
|
+
return path.join(searchRoot, ...segments.slice(0, packageStart + packageLength));
|
|
82
|
+
}
|
|
83
|
+
async function packageExcludesPlatform(owner, platform) {
|
|
84
|
+
try {
|
|
85
|
+
const manifest = JSON.parse(await readFile(path.join(owner, 'package.json'), 'utf8'));
|
|
86
|
+
return excludesPlatform(manifest.os, platform);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export async function mismatchedNativeBinaries(root = process.cwd(), platform = process.platform, readPackageExclusion = packageExcludesPlatform) {
|
|
62
93
|
const expected = expectedNativeFamily(platform);
|
|
63
94
|
if (expected === undefined)
|
|
64
95
|
return [];
|
|
@@ -72,17 +103,28 @@ export async function mismatchedNativeBinaries(root = process.cwd(), platform =
|
|
|
72
103
|
return [];
|
|
73
104
|
}
|
|
74
105
|
const cwd = await searchRoot(nodeModules);
|
|
106
|
+
const pattern = cwd === nodeModules ? ['**/*.{node,bin}', '.bin/*.{node,bin}'] : '**/*.{node,bin}';
|
|
75
107
|
const mismatches = [];
|
|
76
|
-
for await (const relative of glob(
|
|
108
|
+
for await (const relative of glob(pattern, { cwd })) {
|
|
77
109
|
const pathname = path.join(cwd, relative);
|
|
78
110
|
const magic = await readMagic(pathname);
|
|
79
111
|
if (magic === undefined)
|
|
80
112
|
continue;
|
|
81
113
|
const family = nativeFamilyFromMagic(magic);
|
|
82
|
-
if (family !== undefined && family !== expected)
|
|
83
|
-
mismatches.push(pathname);
|
|
114
|
+
if (family !== undefined && family !== expected) {
|
|
115
|
+
mismatches.push({ owner: packageRoot(pathname, cwd), pathname });
|
|
116
|
+
}
|
|
84
117
|
}
|
|
85
|
-
|
|
118
|
+
const owners = [
|
|
119
|
+
...new Set(mismatches.flatMap(({ owner }) => (owner === undefined ? [] : [owner]))),
|
|
120
|
+
];
|
|
121
|
+
const exclusions = new Map(await Promise.all(owners.map(async (owner) => [
|
|
122
|
+
owner,
|
|
123
|
+
await readPackageExclusion(owner, platform),
|
|
124
|
+
])));
|
|
125
|
+
return mismatches
|
|
126
|
+
.filter(({ owner }) => owner === undefined || !exclusions.get(owner))
|
|
127
|
+
.map(({ pathname }) => pathname);
|
|
86
128
|
}
|
|
87
129
|
export async function nativeBinariesMatchRuntime(root = process.cwd(), platform = process.platform) {
|
|
88
130
|
return (await mismatchedNativeBinaries(root, platform)).length === 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vouchington-tooling",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Vouchington CLI and extractable tooling libraries.",
|
|
5
5
|
"homepage": "https://github.com/vouchington/vouchington-tooling/tree/main/packages/vouchington-tooling#readme",
|
|
6
6
|
"bugs": {
|