-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
97 lines (91 loc) · 2.75 KB
/
loader.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const { existsSync } = require('fs')
const { join } = require('path')
const { platform, arch } = process
const NAPI_NAME = 'rusb'
const NPM_PACKAGE_NAME = '@bubblex/rusb'
let nativeBinding = null
let localFileExisted = false
let loadError = null
switch (platform) {
case 'win32':
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, `${NAPI_NAME}.win32-x64-msvc.node`))
try {
if (localFileExisted) {
nativeBinding = require(`./${NAPI_NAME}.win32-x64-msvc.node`)
} else {
nativeBinding = require(`${NPM_PACKAGE_NAME}-win32-x64-msvc`)
}
} catch (e) {
loadError = e
}
break
case 'ia32':
localFileExisted = existsSync(join(__dirname, `${NAPI_NAME}.win32-ia32-msvc.node`))
try {
if (localFileExisted) {
nativeBinding = require(`./${NAPI_NAME}.win32-ia32-msvc.node`)
} else {
nativeBinding = require(`${NPM_PACKAGE_NAME}-win32-ia32-msvc`)
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(join(__dirname, `${NAPI_NAME}.win32-arm64-msvc.node`))
try {
if (localFileExisted) {
nativeBinding = require(`./${NAPI_NAME}.win32-arm64-msvc.node`)
} else {
nativeBinding = require(`${NPM_PACKAGE_NAME}-win32-arm64-msvc`)
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Windows: ${arch}`)
}
break
case 'darwin':
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, `${NAPI_NAME}.darwin-x64.node`))
try {
if (localFileExisted) {
nativeBinding = require(`./${NAPI_NAME}.darwin-x64.node`)
} else {
nativeBinding = require(`${NPM_PACKAGE_NAME}-darwin-x64`)
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(join(__dirname, `${NAPI_NAME}.darwin-arm64.node`))
try {
if (localFileExisted) {
nativeBinding = require(`./${NAPI_NAME}.darwin-arm64.node`)
} else {
nativeBinding = require(`${NPM_PACKAGE_NAME}-darwin-arm64`)
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on macOS: ${arch}`)
}
break
default:
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}
if (!nativeBinding) {
if (loadError) {
throw loadError
}
throw new Error(`Failed to load native binding`)
}
module.exports = nativeBinding