Skip to content

Commit 11e41f7

Browse files
sunnylqmclaude
andcommitted
Make native module loading robust and switch to prebuilds-only install
- Loader prefers a local build/Release build (dev), then static per-platform prebuild paths incl. darwin-x64/win32-x64, and falls back to node-gyp-build with a clear error on unsupported platforms; a missing or ABI-incompatible prebuild no longer crashes require() - Drop the install script: the published package has no sources, so node-gyp fallback could only ever fail; move node-addon-api to devDependencies and declare engines.node >=14.17 - Remove redundant .npmignore (files field wins); anchor the version tag regex in prepublish.ts Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 2b982ef commit 11e41f7

5 files changed

Lines changed: 34 additions & 19 deletions

File tree

.npmignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

bun.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
function loadNative() {
2-
const platform = process.platform;
3-
const arch = process.arch;
1+
const fs = require('fs');
2+
const path = require('path');
43

5-
if (platform === 'darwin' && arch === 'arm64') {
6-
return require('./prebuilds/darwin-arm64/node-hdiffpatch.node');
7-
}
8-
if (platform === 'linux' && arch === 'x64') {
9-
return require('./prebuilds/linux-x64/node-hdiffpatch.node');
10-
}
11-
if (platform === 'linux' && arch === 'arm64') {
12-
return require('./prebuilds/linux-arm64/node-hdiffpatch.node');
4+
function loadNative() {
5+
// 开发环境:本地编译产物优先于随包分发的 prebuild(与 node-gyp-build 的顺序一致)
6+
const localBuild = path.join(__dirname, 'build/Release/hdiffpatch.node');
7+
if (fs.existsSync(localBuild)) {
8+
return require(localBuild);
139
}
1410

11+
// 静态 require 路径便于打包工具分析;失败(缺文件、musl 等 ABI 不符)时回退 node-gyp-build
12+
try {
13+
switch (`${process.platform}-${process.arch}`) {
14+
case 'darwin-arm64':
15+
return require('./prebuilds/darwin-arm64/node-hdiffpatch.node');
16+
case 'darwin-x64':
17+
return require('./prebuilds/darwin-x64/node-hdiffpatch.node');
18+
case 'linux-x64':
19+
return require('./prebuilds/linux-x64/node-hdiffpatch.node');
20+
case 'linux-arm64':
21+
return require('./prebuilds/linux-arm64/node-hdiffpatch.node');
22+
case 'win32-x64':
23+
return require('./prebuilds/win32-x64/node-hdiffpatch.node');
24+
}
25+
} catch (err) {}
26+
1527
return require('node-gyp-build')(__dirname);
1628
}
1729

@@ -20,6 +32,8 @@ const native = loadNative();
2032
exports.native = native;
2133

2234
exports.diff = native.diff;
35+
exports.diffWithCovers = native.diffWithCovers;
2336
exports.patch = native.patch;
2437
exports.diffStream = native.diffStream;
2538
exports.patchStream = native.patchStream;
39+
exports.patchSingleStream = native.patchSingleStream;

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@
1515
],
1616
"scripts": {
1717
"test": "node test/test.js",
18+
"test:bun": "bun ./test/test.js",
1819
"prepublishOnly": "bun scripts/prepublish.ts",
1920
"benchmark": "node --expose-gc test/benchmark.js",
20-
"prebuild": "prebuildify --napi --strip",
21-
"install": "node-gyp-build"
21+
"prebuild": "prebuildify --napi --strip"
2222
},
2323
"gypfile": true,
2424
"author": "housisong, sunnylqm",
2525
"license": "MIT",
26+
"engines": {
27+
"node": ">=14.17.0"
28+
},
2629
"dependencies": {
27-
"node-addon-api": "^8.5.0",
2830
"node-gyp-build": "^4.8.1"
2931
},
3032
"devDependencies": {
3133
"@types/bun": "^1.3.8",
34+
"node-addon-api": "^8.5.0",
3235
"node-gyp": "^12.2.0",
3336
"prebuildify": "^6.0.0"
3437
},

scripts/prepublish.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ async function modifyPackageJson({
3636

3737
async function main(): Promise<void> {
3838
const version = (await $`git describe --tags --always`.text())
39-
.replace('v', '')
40-
.trim();
39+
.trim()
40+
.replace(/^v/, '');
4141
try {
4242
await modifyPackageJson({ version });
4343
console.log('✅ Prepublish script completed successfully');

0 commit comments

Comments
 (0)