@paimaexample/npm-avail-node 0.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/binary.js +116 -0
- package/docker.js +85 -0
- package/index.js +119 -0
- package/package.json +19 -0
package/binary.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const os = require('os')
|
|
2
|
+
const distro = require('linus')
|
|
3
|
+
const tar = require('tar')
|
|
4
|
+
const fs = require('fs')
|
|
5
|
+
const path = require('path')
|
|
6
|
+
const { spawn } = require('child_process')
|
|
7
|
+
|
|
8
|
+
const downloadLinks = {
|
|
9
|
+
'x86_64-ubuntu-2404': 'https://github.com/availproject/avail/releases/download/v2.3.0.1-rc1/x86_64-ubuntu-2404-avail-node.tar.gz',
|
|
10
|
+
'arm64-ubuntu-2204': 'https://github.com/availproject/avail/releases/download/v2.3.0.1-rc1/arm64-ubuntu-2204-avail-node.tar.gz',
|
|
11
|
+
'x86_64-ubuntu-2204': 'https://github.com/availproject/avail/releases/download/v2.3.0.1-rc1/x86_64-ubuntu-2204-avail-node.tar.gz',
|
|
12
|
+
'x86_64-ubuntu-2004': 'https://github.com/availproject/avail/releases/download/v2.3.0.1-rc1/x86_64-ubuntu-2004-avail-node.tar.gz',
|
|
13
|
+
'x86_64-fedora-41': 'https://github.com/availproject/avail/releases/download/v2.3.0.1-rc1/x86_64-fedora-41-avail-node.tar.gz',
|
|
14
|
+
'x86_64-arch': 'https://github.com/availproject/avail/releases/download/v2.3.0.1-rc1/x86_64-arch-avail-node.tar.gz',
|
|
15
|
+
'x86_64-debian-12': 'https://github.com/availproject/avail/releases/download/v2.3.0.1-rc1/x86_64-debian-12-avail-node.tar.gz',
|
|
16
|
+
'arm64-macos': 'https://paima-midnight.nyc3.cdn.digitaloceanspaces.com/binaries/arm64-macos-avail-node.tar.gz',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const checkIfSupported = () => {
|
|
20
|
+
const platform = os.platform()
|
|
21
|
+
const isSupported = platform === 'linux' || platform === 'darwin'
|
|
22
|
+
if (!isSupported) {
|
|
23
|
+
console.error('This script is only supported on Linux and macOS.')
|
|
24
|
+
process.exit(1)
|
|
25
|
+
}
|
|
26
|
+
return true
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const getBinaryKey = () => {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const arch = os.arch() === 'arm64' ? 'arm64' : 'x86_64'
|
|
32
|
+
if (os.platform() === 'darwin') {
|
|
33
|
+
if (arch === 'arm64') {
|
|
34
|
+
return resolve('arm64-macos')
|
|
35
|
+
}
|
|
36
|
+
return reject(new Error('Unsupported macos architecture'))
|
|
37
|
+
}
|
|
38
|
+
distro.name((err, name) => {
|
|
39
|
+
if (err) {
|
|
40
|
+
reject(new Error(`Failed to get distro name: ${err.message}`))
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
distro.version((err, version) => {
|
|
45
|
+
if (err) {
|
|
46
|
+
if (name.toLowerCase() === 'arch') {
|
|
47
|
+
return resolve(`${arch}-arch`)
|
|
48
|
+
}
|
|
49
|
+
reject(new Error(`Failed to get distro version: ${err.message}`))
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const distroKey = `${arch}-${name.toLowerCase()}-${version.replace('.', '')}`
|
|
54
|
+
resolve(distroKey)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const downloadBinary = async (distroKey) => {
|
|
61
|
+
const link = downloadLinks[distroKey]
|
|
62
|
+
if (!link) {
|
|
63
|
+
throw new Error(`Unsupported distro: ${distroKey}`)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const binDir = path.join(__dirname, 'bin');
|
|
67
|
+
if (!fs.existsSync(binDir)) {
|
|
68
|
+
fs.mkdirSync(binDir, { recursive: true });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
console.log(`Downloading from ${link}...`);
|
|
72
|
+
const response = await fetch(link);
|
|
73
|
+
if (!response.ok) {
|
|
74
|
+
throw new Error(`Failed to download binary: ${response.statusText}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const tarPath = path.join(binDir, 'avail-node.tar.gz');
|
|
78
|
+
const dest = fs.createWriteStream(tarPath);
|
|
79
|
+
for await (const chunk of response.body) {
|
|
80
|
+
dest.write(chunk);
|
|
81
|
+
}
|
|
82
|
+
dest.end();
|
|
83
|
+
|
|
84
|
+
return new Promise((resolve, reject) => {
|
|
85
|
+
dest.on('finish', () => {
|
|
86
|
+
console.log('Extracting binary...');
|
|
87
|
+
tar.x({
|
|
88
|
+
file: tarPath,
|
|
89
|
+
cwd: binDir,
|
|
90
|
+
strip: 1,
|
|
91
|
+
}).then(() => {
|
|
92
|
+
console.log('Extraction complete.');
|
|
93
|
+
const binaryPath = path.join(binDir, 'avail-node')
|
|
94
|
+
fs.chmodSync(binaryPath, '755')
|
|
95
|
+
fs.unlinkSync(tarPath);
|
|
96
|
+
resolve(binaryPath);
|
|
97
|
+
}).catch(reject);
|
|
98
|
+
});
|
|
99
|
+
dest.on('error', reject);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const runBinary = (binaryPath, args = []) => {
|
|
104
|
+
const child = spawn(binaryPath, args, { stdio: 'inherit' });
|
|
105
|
+
child.on('close', (code) => {
|
|
106
|
+
console.log(`Binary process exited with code ${code}`);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
module.exports = {
|
|
111
|
+
checkIfSupported,
|
|
112
|
+
getBinaryKey,
|
|
113
|
+
downloadBinary,
|
|
114
|
+
runBinary
|
|
115
|
+
}
|
|
116
|
+
|
package/docker.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const { exec } = require('child_process')
|
|
2
|
+
const util = require('util')
|
|
3
|
+
const execAsync = util.promisify(exec)
|
|
4
|
+
const path = require('path')
|
|
5
|
+
const fs = require('fs')
|
|
6
|
+
|
|
7
|
+
const DOCKER_IMAGE = 'availj/avail'
|
|
8
|
+
|
|
9
|
+
const dockerPullCommand = (tag='latest') => `docker pull ${DOCKER_IMAGE}:${tag}`
|
|
10
|
+
|
|
11
|
+
async function checkIfDockerExists() {
|
|
12
|
+
try {
|
|
13
|
+
await execAsync('docker --version');
|
|
14
|
+
return true;
|
|
15
|
+
} catch (error) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const runDockerContainer = (options) => {
|
|
21
|
+
if (!checkIfDockerExists()) {
|
|
22
|
+
console.error('Docker is not installed. Please install Docker and try again.')
|
|
23
|
+
process.exit(1)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const {
|
|
27
|
+
tag = 'latest',
|
|
28
|
+
p2pPort = 30333,
|
|
29
|
+
rpcPort = 9944,
|
|
30
|
+
args = []
|
|
31
|
+
} = options
|
|
32
|
+
|
|
33
|
+
let hostOutputDir = path.join(process.cwd(), 'output')
|
|
34
|
+
const containerOutputDir = '/output'
|
|
35
|
+
const finalArgs = []
|
|
36
|
+
|
|
37
|
+
for (let i = 0; i < args.length; i++) {
|
|
38
|
+
const arg = args[i]
|
|
39
|
+
if (arg === '-d' || arg === '--base-path') {
|
|
40
|
+
if (i + 1 < args.length) {
|
|
41
|
+
hostOutputDir = path.resolve(args[i + 1])
|
|
42
|
+
i++ // skip next arg, it's the path
|
|
43
|
+
}
|
|
44
|
+
continue // Don't add the original -d or --base-path to finalArgs
|
|
45
|
+
}
|
|
46
|
+
if (arg.startsWith('--base-path=')) {
|
|
47
|
+
hostOutputDir = path.resolve(arg.substring('--base-path='.length))
|
|
48
|
+
continue // Don't add the original --base-path to finalArgs
|
|
49
|
+
}
|
|
50
|
+
finalArgs.push(arg)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!fs.existsSync(hostOutputDir)) {
|
|
54
|
+
fs.mkdirSync(hostOutputDir, { recursive: true })
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Always add the base path for inside the container
|
|
58
|
+
finalArgs.push('-d', containerOutputDir)
|
|
59
|
+
|
|
60
|
+
const command = [
|
|
61
|
+
'docker run --rm',
|
|
62
|
+
`-p ${p2pPort}:30333`,
|
|
63
|
+
`-p ${rpcPort}:9944`,
|
|
64
|
+
`-v ${hostOutputDir}:${containerOutputDir}`,
|
|
65
|
+
`${DOCKER_IMAGE}:${tag}`,
|
|
66
|
+
...finalArgs
|
|
67
|
+
].join(' ')
|
|
68
|
+
|
|
69
|
+
console.log(`Running command: ${command}`)
|
|
70
|
+
const child = exec(command)
|
|
71
|
+
|
|
72
|
+
child.stdout.pipe(process.stdout)
|
|
73
|
+
child.stderr.pipe(process.stderr)
|
|
74
|
+
|
|
75
|
+
child.on('close', (code) => {
|
|
76
|
+
console.log(`Docker container exited with code ${code}`)
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = {
|
|
81
|
+
dockerPullCommand,
|
|
82
|
+
runDockerContainer,
|
|
83
|
+
checkIfDockerExists
|
|
84
|
+
}
|
|
85
|
+
|
package/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const os = require('os')
|
|
3
|
+
const fs = require('fs')
|
|
4
|
+
const path = require('path')
|
|
5
|
+
const { getBinaryKey, downloadBinary, runBinary, checkIfSupported } = require('./binary')
|
|
6
|
+
const { runDockerContainer, checkIfDockerExists } = require('./docker')
|
|
7
|
+
|
|
8
|
+
const main = async () => {
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
const rawArgs = process.argv.slice(2)
|
|
12
|
+
|
|
13
|
+
const useDockerFlag = rawArgs.includes('--docker')
|
|
14
|
+
if (!useDockerFlag && !checkIfSupported()) {
|
|
15
|
+
console.error('This script is only supported on Linux and macOS.')
|
|
16
|
+
process.exit(1)
|
|
17
|
+
}
|
|
18
|
+
let dockerTag = 'latest'
|
|
19
|
+
let dockerPort = 30333
|
|
20
|
+
let dockerPortRpc = 9944
|
|
21
|
+
const passThroughArgs = []
|
|
22
|
+
|
|
23
|
+
for (let i = 0; i < rawArgs.length; i++) {
|
|
24
|
+
const arg = rawArgs[i]
|
|
25
|
+
if (arg === '--docker') {
|
|
26
|
+
continue
|
|
27
|
+
}
|
|
28
|
+
if (arg === '--docker-tag') {
|
|
29
|
+
if (i + 1 < rawArgs.length) {
|
|
30
|
+
dockerTag = rawArgs[i + 1]
|
|
31
|
+
i++
|
|
32
|
+
}
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
if (arg.startsWith('--docker-tag=')) {
|
|
36
|
+
dockerTag = arg.substring('--docker-tag='.length)
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
if (arg === '--docker-port') {
|
|
40
|
+
if (i + 1 < rawArgs.length) {
|
|
41
|
+
dockerPort = rawArgs[i + 1]
|
|
42
|
+
i++
|
|
43
|
+
}
|
|
44
|
+
continue
|
|
45
|
+
}
|
|
46
|
+
if (arg.startsWith('--docker-port=')) {
|
|
47
|
+
dockerPort = arg.substring('--docker-port='.length)
|
|
48
|
+
continue
|
|
49
|
+
}
|
|
50
|
+
if (arg === '--docker-port-rpc') {
|
|
51
|
+
if (i + 1 < rawArgs.length) {
|
|
52
|
+
dockerPortRpc = rawArgs[i + 1]
|
|
53
|
+
i++
|
|
54
|
+
}
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
if (arg.startsWith('--docker-port-rpc=')) {
|
|
58
|
+
dockerPortRpc = arg.substring('--docker-port-rpc='.length)
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
passThroughArgs.push(arg)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (useDockerFlag) {
|
|
65
|
+
const dockerExists = await checkIfDockerExists()
|
|
66
|
+
if (!dockerExists) {
|
|
67
|
+
console.error('Error: Docker is required but not found.')
|
|
68
|
+
console.error('You specified the --docker flag, so Docker is required.')
|
|
69
|
+
console.error('Please install Docker and ensure the Docker daemon is running.')
|
|
70
|
+
process.exit(1)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const hasChain = passThroughArgs.some(arg => arg === '--chain' || arg.startsWith('--chain='))
|
|
75
|
+
const hasDev = passThroughArgs.includes('--dev')
|
|
76
|
+
|
|
77
|
+
if (!hasChain && !hasDev) {
|
|
78
|
+
console.error('Error: The --chain argument is mandatory.')
|
|
79
|
+
console.error('Please specify a chain (e.g., --chain mainnet) or use the --dev flag for a development environment.')
|
|
80
|
+
process.exit(1)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (useDockerFlag) {
|
|
84
|
+
console.log(`Running with Docker (tag: ${dockerTag})...`)
|
|
85
|
+
runDockerContainer({
|
|
86
|
+
tag: dockerTag,
|
|
87
|
+
p2pPort: dockerPort,
|
|
88
|
+
rpcPort: dockerPortRpc,
|
|
89
|
+
args: passThroughArgs
|
|
90
|
+
})
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
console.log('Attempting to use binary...')
|
|
96
|
+
const binaryKey = await getBinaryKey()
|
|
97
|
+
if (!binaryKey) {
|
|
98
|
+
throw new Error('Could not determine binary key for this distribution. Please run with --docker flag.')
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const binaryDir = path.join(__dirname, 'bin')
|
|
102
|
+
const binaryPath = path.join(binaryDir, 'avail-node')
|
|
103
|
+
|
|
104
|
+
if (!fs.existsSync(binaryPath)) {
|
|
105
|
+
console.log(`Binary not found for ${binaryKey}, downloading...`)
|
|
106
|
+
await downloadBinary(binaryKey)
|
|
107
|
+
} else {
|
|
108
|
+
console.log('Binary found, skipping download.')
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
runBinary(binaryPath, passThroughArgs)
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.error(`Error: ${error.message}`)
|
|
114
|
+
console.error('Failed to run binary. Please try running with the --docker flag.')
|
|
115
|
+
process.exit(1)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
main()
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@paimaexample/npm-avail-node",
|
|
3
|
+
"version": "0.3.15",
|
|
4
|
+
"description": "A wrapper for the Avail node binary",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"npm-avail-node": "index.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"start": "node index.js"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"linus": "^0.0.6",
|
|
14
|
+
"tar": "^6.2.0"
|
|
15
|
+
},
|
|
16
|
+
"type": "commonjs",
|
|
17
|
+
"author": "Paima Studios",
|
|
18
|
+
"license": "ISC"
|
|
19
|
+
}
|