@paimaexample/npm-midnight-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 ADDED
@@ -0,0 +1,56 @@
1
+ const os = require("os");
2
+ const fs = require("fs");
3
+ const axios = require("axios");
4
+ const extract = require("extract-zip");
5
+ const path = require("path");
6
+
7
+ function getPlatform() {
8
+ let platform = os.platform();
9
+ const arch = os.arch();
10
+ if (platform === "darwin") platform = "macos";
11
+ if (arch === "x64") arch = "amd64";
12
+ return `${platform}-${arch}`;
13
+ }
14
+
15
+ function getBinaryUrl() {
16
+ const platform = getPlatform();
17
+ const supportedPlatforms = require("./package.json").supportedPlatforms;
18
+ if (!supportedPlatforms.includes(platform)) {
19
+ throw new Error(`Unsupported platform: ${platform}`);
20
+ }
21
+ return `https://paima-midnight.nyc3.cdn.digitaloceanspaces.com/binaries/midnight-node-${platform}.zip`;
22
+ }
23
+
24
+ async function downloadAndSaveBinary() {
25
+ const url = getBinaryUrl();
26
+ try {
27
+ const response = await axios.get(url, { responseType: "stream" });
28
+ const writer = fs.createWriteStream(path.join(__dirname, "midnight-node.zip"));
29
+
30
+ response.data.pipe(writer);
31
+
32
+ return new Promise((resolve, reject) => {
33
+ writer.on('finish', resolve);
34
+ writer.on('error', reject);
35
+ });
36
+ } catch (error) {
37
+ console.error("Error downloading binary:", error);
38
+ throw error;
39
+ }
40
+ }
41
+
42
+ async function unzipBinary() {
43
+ await extract(path.join(__dirname, "midnight-node.zip"), {
44
+ dir: path.join(__dirname, "midnight-node"),
45
+ });
46
+ fs.unlinkSync(path.join(__dirname, "midnight-node.zip"));
47
+ }
48
+
49
+ async function binary() {
50
+ await downloadAndSaveBinary();
51
+ await unzipBinary();
52
+ }
53
+
54
+ module.exports = {
55
+ binary,
56
+ };
package/index.js ADDED
@@ -0,0 +1,18 @@
1
+ const { binary } = require("./binary");
2
+ const { runMidnightNode } = require("./run_midnight_node");
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+
6
+ function checkIfBinaryExists() {
7
+ return fs.existsSync(path.join(__dirname, "midnight-node", "midnight-node"));
8
+ }
9
+
10
+
11
+ async function main(args) {
12
+ if (!checkIfBinaryExists()) {
13
+ await binary();
14
+ }
15
+ runMidnightNode(process.env, args);
16
+ }
17
+
18
+ main(process.argv.slice(2));
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@paimaexample/npm-midnight-node",
3
+ "version": "0.3.15",
4
+ "description": "Downloads and starts the binary for Midnight Node",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "bin": {
10
+ "npm-midnight-node": "index.js"
11
+ },
12
+ "author": "Paima Studios",
13
+ "license": "ISC",
14
+ "supportedPlatforms": [
15
+ "linux-arm64",
16
+ "linux-amd64",
17
+ "macos-arm64"
18
+ ],
19
+ "dependencies": {
20
+ "axios": "^1.10.0",
21
+ "axios-proxy-builder": "^0.1.2",
22
+ "extract-zip": "^2.0.1"
23
+ }
24
+ }
@@ -0,0 +1,40 @@
1
+ const { spawn } = require('child_process');
2
+ const path = require('path');
3
+
4
+ /**
5
+ * Executes the midnight-node binary as a child process
6
+ * @param {Object} env - Environment variables to pass to the child process
7
+ * @param {Array} args - Optional arguments to pass to the binary
8
+ * @returns {ChildProcess} The spawned child process
9
+ */
10
+ function runMidnightNode(env = process.env, args = []) {
11
+ const binaryPath = path.join(__dirname, 'midnight-node', 'midnight-node');
12
+
13
+ console.log(`Starting midnight-node binary at: ${binaryPath}`);
14
+
15
+ const childProcess = spawn(binaryPath, args, {
16
+ env: env,
17
+ stdio: 'inherit', // Inherit stdin, stdout, stderr from parent process
18
+ cwd: path.join(__dirname, 'midnight-node') // Run from inside the midnight-node directory
19
+ });
20
+
21
+ childProcess.on('spawn', () => {
22
+ console.log(`midnight-node process spawned with PID: ${childProcess.pid}`);
23
+ });
24
+
25
+ childProcess.on('error', (error) => {
26
+ console.error('Failed to start midnight-node:', error);
27
+ });
28
+
29
+ childProcess.on('exit', (code, signal) => {
30
+ if (code !== null) {
31
+ console.log(`midnight-node process exited with code: ${code}`);
32
+ } else {
33
+ console.log(`midnight-node process terminated by signal: ${signal}`);
34
+ }
35
+ });
36
+
37
+ return childProcess;
38
+ }
39
+
40
+ module.exports = { runMidnightNode };