Skip to content

Commit 5ab8810

Browse files
committed
chore: replace node pre gyp
Signed-off-by: Timo Glastra <timo@animo.id>
1 parent d66d81b commit 5ab8810

File tree

8 files changed

+172
-193
lines changed

8 files changed

+172
-193
lines changed
-9.08 MB
Binary file not shown.

packages/anoncreds-nodejs/package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"@2060.io/ffi-napi": "^4.0.9",
2828
"@2060.io/ref-napi": "^3.0.6",
2929
"@hyperledger/anoncreds-shared": "workspace:*",
30-
"@mapbox/node-pre-gyp": "^1.0.11",
3130
"ref-array-di": "1.2.2",
3231
"ref-struct-di": "1.1.1"
3332
},
@@ -38,11 +37,9 @@
3837
"typescript": "catalog:"
3938
},
4039
"binary": {
41-
"module_name": "anoncreds",
42-
"module_path": "native",
43-
"remote_path": "v0.2.0",
44-
"host": "https://github.com/hyperledger/anoncreds-rs/releases/download/",
45-
"package_name": "library-{platform}-{arch}.tar.gz"
40+
"version": "v0.2.0",
41+
"host": "https://github.com/hyperledger/anoncreds-rs/releases/download",
42+
"packageName": "library-{platform}-{arch}.tar.gz"
4643
},
4744
"engines": {
4845
"node": ">= 18"
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
const { execSync } = require('node:child_process')
1+
const { downloadBinaryIfNeeded } = require('@hyperledger/anoncreds-shared/installBinary')
22
const { arch, platform: osPlatform } = require('os')
3+
const path = require('path')
4+
5+
const { binary } = require('../package.json')
36

47
const archTable = {
58
x64: 'x86_64',
@@ -8,8 +11,12 @@ const archTable = {
811

912
const platform = osPlatform()
1013
const targetPlatform = platform === 'win32' ? 'windows' : platform
11-
const targetArchitecture = platform === 'darwin' ? 'universal' : archTable[arch]
12-
13-
const command = `node-pre-gyp install --target_arch=${targetArchitecture} --target_platform=${targetPlatform}`
14+
const targetArchitecture = platform === 'darwin' ? 'universal' : archTable[arch()]
15+
const packageName = binary.packageName.replace('{platform}', targetPlatform).replace('{arch}', targetArchitecture)
1416

15-
execSync(command)
17+
downloadBinaryIfNeeded({
18+
packageName,
19+
host: binary.host,
20+
version: binary.version,
21+
targetDirectory: path.join(__dirname, '../native'),
22+
})

packages/anoncreds-react-native/package.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@
2626
"ios/Anoncreds.xcodeproj/project.pbxproj",
2727
"cpp/**/*.cpp",
2828
"cpp/**/*.h",
29-
"anoncreds.podspec"
29+
"anoncreds.podspec",
30+
"scripts"
3031
],
3132
"scripts": {
3233
"check-types": "pnpm compile --noEmit",
3334
"build": "pnpm clean && pnpm compile",
3435
"clean": "rimraf -rf ./build",
3536
"compile": "tsc -p ./tsconfig.build.json",
36-
"install": "node-pre-gyp install"
37+
"install": "node scripts/install.js"
3738
},
3839
"dependencies": {
39-
"@hyperledger/anoncreds-shared": "workspace:*",
40-
"@mapbox/node-pre-gyp": "^1.0.11"
40+
"@hyperledger/anoncreds-shared": "workspace:*"
4141
},
4242
"devDependencies": {
4343
"react": "18.2.0",
@@ -49,10 +49,8 @@
4949
"react-native": ">= 0.71"
5050
},
5151
"binary": {
52-
"module_name": "anoncreds",
53-
"module_path": "native",
54-
"remote_path": "v0.2.0",
55-
"host": "https://github.com/hyperledger/anoncreds-rs/releases/download/",
56-
"package_name": "library-ios-android.tar.gz"
52+
"version": "v0.2.0",
53+
"host": "https://github.com/hyperledger/anoncreds-rs/releases/download",
54+
"packageName": "library-ios-android.tar.gz"
5755
}
5856
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { downloadBinaryIfNeeded } = require('@hyperledger/anoncreds-shared/installBinary')
2+
const { binary } = require('../package.json')
3+
const path = require('path')
4+
5+
downloadBinaryIfNeeded({
6+
packageName: binary.packageName,
7+
host: binary.host,
8+
version: binary.version,
9+
targetDirectory: path.join(__dirname, '../native'),
10+
})
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const path = require('path')
2+
const tar = require('tar')
3+
const fs = require('fs')
4+
5+
async function downloadBinaryIfNeeded({ packageName, version, host, targetDirectory }) {
6+
if (!needsDownload({ version, targetDirectory })) return
7+
8+
await downloadBinary({ packageName, version, host, targetDirectory })
9+
}
10+
11+
function needsDownload({ version, targetDirectory }) {
12+
const metadataPath = path.join(targetDirectory, 'version.json')
13+
14+
if (!fs.existsSync(metadataPath)) return true
15+
16+
// Check if metadata exists and matches current version
17+
try {
18+
const metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf8'))
19+
if (metadata.version === version) {
20+
console.log('Binary is up to date, skipping download')
21+
return false
22+
}
23+
} catch (error) {
24+
// If metadata is corrupted, download anyway
25+
console.log('Metadata file is invalid, will download binary')
26+
}
27+
28+
return true
29+
}
30+
31+
async function downloadBinary({ packageName, version, host, targetDirectory }) {
32+
const url = `${host}/${version}/${packageName}`
33+
const metadataPath = path.join(targetDirectory, 'version.json')
34+
35+
console.log(`Downloading binary from ${url} to \n ${targetDirectory}...`)
36+
37+
try {
38+
// Ensure the directory exists
39+
if (!fs.existsSync(targetDirectory)) {
40+
fs.mkdirSync(targetDirectory, { recursive: true })
41+
}
42+
43+
const response = await fetch(url)
44+
45+
if (!response.ok) {
46+
throw new Error(`Failed to download: ${response.status} ${response.statusText}`)
47+
}
48+
const data = await response.arrayBuffer()
49+
50+
// Write the downloaded file first
51+
const tempFile = path.join(targetDirectory, 'anoncreds.tar.gz')
52+
fs.writeFileSync(tempFile, Buffer.from(data))
53+
54+
// Then extract it
55+
return tar
56+
.extract({
57+
file: tempFile,
58+
cwd: targetDirectory,
59+
strip: 1,
60+
})
61+
.then(() => {
62+
// Remove the tarball after extraction
63+
fs.unlinkSync(tempFile)
64+
65+
// Save version metadata
66+
fs.writeFileSync(
67+
metadataPath,
68+
JSON.stringify({
69+
version,
70+
})
71+
)
72+
73+
console.log('Binary downloaded and extracted successfully')
74+
})
75+
} catch (error) {
76+
console.error('Error downloading binary:', error)
77+
process.exit(1)
78+
}
79+
}
80+
81+
module.exports = {
82+
downloadBinaryIfNeeded,
83+
}

packages/anoncreds-shared/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"publishConfig": {
1515
"access": "public"
1616
},
17-
"files": ["build"],
17+
"files": ["build", "installBinary.js"],
1818
"scripts": {
1919
"check-types": "pnpm compile --noEmit",
2020
"build": "pnpm clean && pnpm compile",
@@ -23,5 +23,8 @@
2323
},
2424
"devDependencies": {
2525
"typescript": "catalog:"
26+
},
27+
"dependencies": {
28+
"tar": "^7.4.3"
2629
}
2730
}

0 commit comments

Comments
 (0)