MCPcopy Create free account
hub / github.com/WiseLibs/better-sqlite3 / getBinding

Function getBinding

lib/binding.js:9–39  ·  view source on GitHub ↗
(nativeBinding)

Source from the content-addressed store, hash-verified

7let DEFAULT_ADDON;
8
9function getBinding(nativeBinding) {
10 // If a path was provided, load the binding from the filesystem.
11 if (typeof nativeBinding === 'string') {
12 // See <https://webpack.js.org/api/module-variables/#__non_webpack_require__-webpack-specific>
13 const requireFunc = typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require;
14 return requireFunc(path.resolve(nativeBinding).replace(/(\.node)?$/, '.node'));
15 }
16
17 // If an object was provided, use it as the binding directly.
18 if (typeof nativeBinding === 'object' && nativeBinding !== null) {
19 return nativeBinding;
20 }
21
22 // If we're using the default binding and it already exists, just return it.
23 if (DEFAULT_ADDON) {
24 return DEFAULT_ADDON;
25 }
26
27 // Otherwise, try to find the binding as a prebuilt binary.
28 let filename = getPrebuildPath();
29 if (filename) {
30 return DEFAULT_ADDON = require(filename);
31 }
32
33 // If no prebuilt binary was found, try the default node-gyp locations.
34 filename = path.join(__dirname, '..', 'build', 'Debug', 'better_sqlite3.node');
35 if (!fs.existsSync(filename)) {
36 filename = path.join(__dirname, '..', 'build', 'Release', 'better_sqlite3.node');
37 }
38 return DEFAULT_ADDON = require(filename);
39}
40
41function getPrebuildPath() {
42 if (PREBUILD_PLATFORMS.includes(process.platform) && PREBUILD_ARCHS.includes(process.arch)) {

Callers

nothing calls this directly

Calls 1

getPrebuildPathFunction · 0.85

Tested by

no test coverage detected