1 | const { existsSync, readFileSync } = require('fs')
|
2 | const { join } = require('path')
|
3 |
|
4 | const { platform, arch } = process
|
5 |
|
6 | let nativeBinding = null
|
7 | let localFileExisted = false
|
8 | let loadError = null
|
9 |
|
10 | function isMusl() {
|
11 |
|
12 | if (!process.report || typeof process.report.getReport !== 'function') {
|
13 | try {
|
14 | return readFileSync('/usr/bin/ldd', 'utf8').includes('musl')
|
15 | } catch (e) {
|
16 | return false
|
17 | }
|
18 | } else {
|
19 | const { glibcVersionRuntime } = process.report.getReport().header
|
20 | return !glibcVersionRuntime
|
21 | }
|
22 | }
|
23 |
|
24 | switch (platform) {
|
25 | case 'android':
|
26 | if (arch !== 'arm64') {
|
27 | throw new Error(`Unsupported architecture on Android ${arch}`)
|
28 | }
|
29 | localFileExisted = existsSync(join(__dirname, 'xxhash.android-arm64.node'))
|
30 | try {
|
31 | if (localFileExisted) {
|
32 | nativeBinding = require('./xxhash.android-arm64.node')
|
33 | } else {
|
34 | nativeBinding = require('@node-rs/xxhash-android-arm64')
|
35 | }
|
36 | } catch (e) {
|
37 | loadError = e
|
38 | }
|
39 | break
|
40 | case 'win32':
|
41 | switch (arch) {
|
42 | case 'x64':
|
43 | localFileExisted = existsSync(join(__dirname, 'xxhash.win32-x64-msvc.node'))
|
44 | try {
|
45 | if (localFileExisted) {
|
46 | nativeBinding = require('./xxhash.win32-x64-msvc.node')
|
47 | } else {
|
48 | nativeBinding = require('@node-rs/xxhash-win32-x64-msvc')
|
49 | }
|
50 | } catch (e) {
|
51 | loadError = e
|
52 | }
|
53 | break
|
54 | case 'ia32':
|
55 | localFileExisted = existsSync(join(__dirname, 'xxhash.win32-ia32-msvc.node'))
|
56 | try {
|
57 | if (localFileExisted) {
|
58 | nativeBinding = require('./xxhash.win32-ia32-msvc.node')
|
59 | } else {
|
60 | nativeBinding = require('@node-rs/xxhash-win32-ia32-msvc')
|
61 | }
|
62 | } catch (e) {
|
63 | loadError = e
|
64 | }
|
65 | break
|
66 | case 'arm64':
|
67 | localFileExisted = existsSync(join(__dirname, 'xxhash.win32-arm64-msvc.node'))
|
68 | try {
|
69 | if (localFileExisted) {
|
70 | nativeBinding = require('./xxhash.win32-arm64-msvc.node')
|
71 | } else {
|
72 | nativeBinding = require('@node-rs/xxhash-win32-arm64-msvc')
|
73 | }
|
74 | } catch (e) {
|
75 | loadError = e
|
76 | }
|
77 | break
|
78 | default:
|
79 | throw new Error(`Unsupported architecture on Windows: ${arch}`)
|
80 | }
|
81 | break
|
82 | case 'darwin':
|
83 | switch (arch) {
|
84 | case 'x64':
|
85 | localFileExisted = existsSync(join(__dirname, 'xxhash.darwin-x64.node'))
|
86 | try {
|
87 | if (localFileExisted) {
|
88 | nativeBinding = require('./xxhash.darwin-x64.node')
|
89 | } else {
|
90 | nativeBinding = require('@node-rs/xxhash-darwin-x64')
|
91 | }
|
92 | } catch (e) {
|
93 | loadError = e
|
94 | }
|
95 | break
|
96 | case 'arm64':
|
97 | localFileExisted = existsSync(join(__dirname, 'xxhash.darwin-arm64.node'))
|
98 | try {
|
99 | if (localFileExisted) {
|
100 | nativeBinding = require('./xxhash.darwin-arm64.node')
|
101 | } else {
|
102 | nativeBinding = require('@node-rs/xxhash-darwin-arm64')
|
103 | }
|
104 | } catch (e) {
|
105 | loadError = e
|
106 | }
|
107 | break
|
108 | default:
|
109 | throw new Error(`Unsupported architecture on macOS: ${arch}`)
|
110 | }
|
111 | break
|
112 | case 'freebsd':
|
113 | if (arch !== 'x64') {
|
114 | throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
|
115 | }
|
116 | localFileExisted = existsSync(join(__dirname, 'xxhash.freebsd-x64.node'))
|
117 | try {
|
118 | if (localFileExisted) {
|
119 | nativeBinding = require('./xxhash.freebsd-x64.node')
|
120 | } else {
|
121 | nativeBinding = require('@node-rs/xxhash-freebsd-x64')
|
122 | }
|
123 | } catch (e) {
|
124 | loadError = e
|
125 | }
|
126 | break
|
127 | case 'linux':
|
128 | switch (arch) {
|
129 | case 'x64':
|
130 | if (isMusl()) {
|
131 | localFileExisted = existsSync(join(__dirname, 'xxhash.linux-x64-musl.node'))
|
132 | try {
|
133 | if (localFileExisted) {
|
134 | nativeBinding = require('./xxhash.linux-x64-musl.node')
|
135 | } else {
|
136 | nativeBinding = require('@node-rs/xxhash-linux-x64-musl')
|
137 | }
|
138 | } catch (e) {
|
139 | loadError = e
|
140 | }
|
141 | } else {
|
142 | localFileExisted = existsSync(join(__dirname, 'xxhash.linux-x64-gnu.node'))
|
143 | try {
|
144 | if (localFileExisted) {
|
145 | nativeBinding = require('./xxhash.linux-x64-gnu.node')
|
146 | } else {
|
147 | nativeBinding = require('@node-rs/xxhash-linux-x64-gnu')
|
148 | }
|
149 | } catch (e) {
|
150 | loadError = e
|
151 | }
|
152 | }
|
153 | break
|
154 | case 'arm64':
|
155 | if (isMusl()) {
|
156 | localFileExisted = existsSync(join(__dirname, 'xxhash.linux-arm64-musl.node'))
|
157 | try {
|
158 | if (localFileExisted) {
|
159 | nativeBinding = require('./xxhash.linux-arm64-musl.node')
|
160 | } else {
|
161 | nativeBinding = require('@node-rs/xxhash-linux-arm64-musl')
|
162 | }
|
163 | } catch (e) {
|
164 | loadError = e
|
165 | }
|
166 | } else {
|
167 | localFileExisted = existsSync(join(__dirname, 'xxhash.linux-arm64-gnu.node'))
|
168 | try {
|
169 | if (localFileExisted) {
|
170 | nativeBinding = require('./xxhash.linux-arm64-gnu.node')
|
171 | } else {
|
172 | nativeBinding = require('@node-rs/xxhash-linux-arm64-gnu')
|
173 | }
|
174 | } catch (e) {
|
175 | loadError = e
|
176 | }
|
177 | }
|
178 | break
|
179 | case 'arm':
|
180 | localFileExisted = existsSync(join(__dirname, 'xxhash.linux-arm-gnueabihf.node'))
|
181 | try {
|
182 | if (localFileExisted) {
|
183 | nativeBinding = require('./xxhash.linux-arm-gnueabihf.node')
|
184 | } else {
|
185 | nativeBinding = require('@node-rs/xxhash-linux-arm-gnueabihf')
|
186 | }
|
187 | } catch (e) {
|
188 | loadError = e
|
189 | }
|
190 | break
|
191 | default:
|
192 | throw new Error(`Unsupported architecture on Linux: ${arch}`)
|
193 | }
|
194 | break
|
195 | default:
|
196 | throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
|
197 | }
|
198 |
|
199 | if (!nativeBinding) {
|
200 | if (loadError) {
|
201 | throw loadError
|
202 | }
|
203 | throw new Error(`Failed to load native binding`)
|
204 | }
|
205 |
|
206 | const { xxh32, Xxh32, xxh64, Xxh64, xxh3 } = nativeBinding
|
207 |
|
208 | module.exports.xxh32 = xxh32
|
209 | module.exports.Xxh32 = Xxh32
|
210 | module.exports.xxh64 = xxh64
|
211 | module.exports.Xxh64 = Xxh64
|
212 | module.exports.xxh3 = xxh3
|