-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathload-addon.ts
61 lines (54 loc) · 1.46 KB
/
load-addon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import path from "path"
import fs from "fs"
function errStr(error: unknown) {
return error instanceof Error
? `${error.name}: ${error.message}\n${error.stack}`
: String(error)
}
function devWarn(message: string) {
if (process.env.NODE_ENV !== "production") {
console.warn(message)
}
}
function findAddon(): any | undefined {
let addon: undefined | any = undefined
try {
const addonParentDir = path.resolve(
path.join(
__dirname,
"..",
"build",
process.platform,
process.arch,
"node",
),
)
const addOnAbiDirs = fs.readdirSync(addonParentDir).sort((a, b) => {
return Number.parseInt(b, 10) - Number.parseInt(a, 10)
})
// try each available addon ABI
for (const addOnAbiDir of addOnAbiDirs) {
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
try {
addon = require(addonPath)
break
} catch (err) {
if (fs.existsSync(addonPath)) {
devWarn(
`Failed to load addon at ${addonPath}: ${errStr(err)}\nTrying others...`,
)
} else {
devWarn(`No addon.node found in ${addonPath}\nTrying others...`)
}
}
}
} catch (err) {
throw new Error(`Failed to load zeromq.js addon.node: ${errStr(err)}`)
}
if (addon === undefined) {
throw new Error("No compatible zeromq.js addon found")
}
return addon
}
const addon = findAddon()
export default addon