const { existsSync, readFileSync } = require('fs')
const { join } = require('path')
const { platform, arch } = process
let nativeBinding = null
let localFileExisted = false
let loadError = null
function isMusl() {
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim()
return readFileSync(lddPath, 'utf8').includes('musl')
} catch (e) {
return true
}
} else {
const { glibcVersionRuntime } = process.report.getReport().header
return !glibcVersionRuntime
}
}
switch (platform) {
case 'android':
switch (arch) {
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'self-encryption.android-arm64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.android-arm64.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-android-arm64')
}
} catch (e) {
loadError = e
}
break
case 'arm':
localFileExisted = existsSync(join(__dirname, 'self-encryption.android-arm-eabi.node'))
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.android-arm-eabi.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-android-arm-eabi')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Android ${arch}`)
}
break
case 'win32':
switch (arch) {
case 'x64':
localFileExisted = existsSync(
join(__dirname, 'self-encryption.win32-x64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.win32-x64-msvc.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-win32-x64-msvc')
}
} catch (e) {
loadError = e
}
break
case 'ia32':
localFileExisted = existsSync(
join(__dirname, 'self-encryption.win32-ia32-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.win32-ia32-msvc.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-win32-ia32-msvc')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'self-encryption.win32-arm64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.win32-arm64-msvc.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-win32-arm64-msvc')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Windows: ${arch}`)
}
break
case 'darwin':
localFileExisted = existsSync(join(__dirname, 'self-encryption.darwin-universal.node'))
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.darwin-universal.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-darwin-universal')
}
break
} catch {}
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'self-encryption.darwin-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.darwin-x64.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-darwin-x64')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'self-encryption.darwin-arm64.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.darwin-arm64.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-darwin-arm64')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on macOS: ${arch}`)
}
break
case 'freebsd':
if (arch !== 'x64') {
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
}
localFileExisted = existsSync(join(__dirname, 'self-encryption.freebsd-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.freebsd-x64.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-freebsd-x64')
}
} catch (e) {
loadError = e
}
break
case 'linux':
switch (arch) {
case 'x64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'self-encryption.linux-x64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.linux-x64-musl.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-linux-x64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'self-encryption.linux-x64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.linux-x64-gnu.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-linux-x64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'self-encryption.linux-arm64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.linux-arm64-musl.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-linux-arm64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'self-encryption.linux-arm64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.linux-arm64-gnu.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-linux-arm64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'self-encryption.linux-arm-musleabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.linux-arm-musleabihf.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-linux-arm-musleabihf')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'self-encryption.linux-arm-gnueabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
}
}
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'self-encryption.linux-riscv64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.linux-riscv64-musl.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'self-encryption.linux-riscv64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.linux-riscv64-gnu.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 's390x':
localFileExisted = existsSync(
join(__dirname, 'self-encryption.linux-s390x-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./self-encryption.linux-s390x-gnu.node')
} else {
nativeBinding = require('@withautonomi/self-encryption-linux-s390x-gnu')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${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`)
}
const { verifyChunk, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE, COMPRESSION_QUALITY, MIN_ENCRYPTABLE_BYTES, XorName, ChunkInfo, DataMap, EncryptedChunk, encrypt, decrypt, decryptFromStorage, streamingDecryptFromStorage, streamingEncryptFromFile, encryptFromFile, EncryptFromFileResult, EncryptResult } = nativeBinding
module.exports.verifyChunk = verifyChunk
module.exports.MIN_CHUNK_SIZE = MIN_CHUNK_SIZE
module.exports.MAX_CHUNK_SIZE = MAX_CHUNK_SIZE
module.exports.COMPRESSION_QUALITY = COMPRESSION_QUALITY
module.exports.MIN_ENCRYPTABLE_BYTES = MIN_ENCRYPTABLE_BYTES
module.exports.XorName = XorName
module.exports.ChunkInfo = ChunkInfo
module.exports.DataMap = DataMap
module.exports.EncryptedChunk = EncryptedChunk
module.exports.encrypt = encrypt
module.exports.decrypt = decrypt
module.exports.decryptFromStorage = decryptFromStorage
module.exports.streamingDecryptFromStorage = streamingDecryptFromStorage
module.exports.streamingEncryptFromFile = streamingEncryptFromFile
module.exports.encryptFromFile = encryptFromFile
module.exports.EncryptFromFileResult = EncryptFromFileResult
module.exports.EncryptResult = EncryptResult