Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (69 loc) · 2.81 KB
/
Copy pathindex.js
File metadata and controls
79 lines (69 loc) · 2.81 KB
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
/* eslint-disable */
// Loads the native addon. Maintained by hand, not generated by napi: the
// generated loader requires unscoped `minigraf-<triple>` packages, while the
// release publishes the binaries as `@minigraf/<triple>` (#7). Build with
// `napi build --platform --no-js` so this file is not overwritten.
const { existsSync } = require('node:fs')
const { join } = require('node:path')
const { version } = require('./package.json')
function isMusl() {
if (process.report && typeof process.report.getReport === 'function') {
process.report.excludeNetwork = true
return !process.report.getReport().header.glibcVersionRuntime
}
return false
}
// The platform triple of the binary for this process, or null if none is published.
function platformTriple() {
const { platform, arch } = process
if (platform === 'darwin' && (arch === 'x64' || arch === 'arm64')) return 'darwin-universal'
if (platform === 'win32' && arch === 'x64') return 'win32-x64-msvc'
if (platform === 'linux' && (arch === 'x64' || arch === 'arm64') && !isMusl()) {
return `linux-${arch}-gnu`
}
return null
}
function loadBinding() {
if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
return require(process.env.NAPI_RS_NATIVE_LIBRARY_PATH)
}
const triple = platformTriple()
if (triple === null) {
throw new Error(
`minigraf has no prebuilt binary for ${process.platform}-${process.arch}` +
`${process.platform === 'linux' && isMusl() ? ' (musl)' : ''}. ` +
'Build from source: https://github.com/project-minigraf/minigraf-node#building-from-source',
)
}
// A local build (`napi build --platform`) puts the binary next to this file.
// On macOS it builds for the host arch only, not the universal binary.
const localNames = [`minigraf.${triple}.node`]
if (process.platform === 'darwin') localNames.push(`minigraf.darwin-${process.arch}.node`)
for (const name of localNames) {
const local = join(__dirname, name)
if (existsSync(local)) return require(local)
}
const pkg = `@minigraf/${triple}`
let pkgVersion
try {
pkgVersion = require(`${pkg}/package.json`).version
} catch (e) {
throw new Error(
`Cannot find ${pkg}, the minigraf binary for this platform. It is an optional ` +
'dependency of minigraf; reinstall without --no-optional or --omit=optional.',
{ cause: e },
)
}
// The binary embeds the minigraf core, so another version would run a
// different storage engine than this package's version says.
if (pkgVersion !== version) {
throw new Error(
`${pkg} is version ${pkgVersion}, but minigraf is version ${version}. ` +
'Reinstall dependencies so both versions match.',
)
}
return require(pkg)
}
const nativeBinding = loadBinding()
module.exports = nativeBinding
module.exports.MiniGrafDb = nativeBinding.MiniGrafDb