Skip to content

Commit a3330bc

Browse files
committed
fix: sort the addon paths via a map
1 parent eef34b5 commit a3330bc

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

lib/load-addon.js

+14-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/load-addon.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/load-addon.ts

+22-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "fs"
33

44
function errStr(error: unknown) {
55
return error instanceof Error
6-
? `${error.name}: ${error.message}\n${error.stack}`
6+
? `${error.name}: ${error.stack}`
77
: String(error)
88
}
99

@@ -23,36 +23,43 @@ function findAddon(): any | undefined {
2323
) as Record<string, string>
2424

2525
// compatible addons (abi -> addon path)
26-
const compatibleAddons: Record<string, string> = {}
26+
const compatibleAddons: Map<BuildConfiguration, string> = new Map()
27+
28+
const libc = detectLibc()
2729

2830
const configs = Object.keys(manifest)
2931
for (const configStr of configs) {
3032
const config = JSON.parse(configStr) as BuildConfiguration
3133

3234
// check if the config is compatible with the current runtime
33-
if (config.os !== process.platform || config.arch !== process.arch) {
34-
continue
35-
}
36-
const libc = detectLibc()
37-
if (config.libc !== libc) {
35+
if (
36+
config.os !== process.platform ||
37+
config.arch !== process.arch ||
38+
config.libc !== libc
39+
) {
3840
continue
3941
}
4042

4143
const addonRelativePath = manifest[configStr]
42-
compatibleAddons[config.abi ?? 0] = path.resolve(
43-
buildDir,
44-
addonRelativePath,
44+
compatibleAddons.set(config, path.resolve(buildDir, addonRelativePath))
45+
}
46+
if (compatibleAddons.size === 0) {
47+
throw new Error(
48+
`No compatible zeromq.js addon found for ${process.platform} ${process.arch} ${libc}. The candidates were:\n${configs.join(
49+
"\n",
50+
)}`,
4551
)
4652
}
4753

4854
// sort the compatible abis in descending order
49-
const compatibleAbis = Object.keys(compatibleAddons).sort((a, b) => {
50-
return Number.parseInt(b, 10) - Number.parseInt(a, 10)
51-
})
55+
const compatibleAddonsSorted = [...compatibleAddons.entries()].sort(
56+
([c1, _p1], [c2, _p2]) => {
57+
return (c2.abi ?? 0) - (c1.abi ?? 0)
58+
},
59+
)
5260

5361
// try each available addon ABI
54-
for (const abi of compatibleAbis) {
55-
const addonPath = compatibleAddons[abi]
62+
for (const [_config, addonPath] of compatibleAddonsSorted) {
5663
try {
5764
addon = require(addonPath)
5865
break

0 commit comments

Comments
 (0)