@effectstream/npm-midnight-node 0.3.99
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 +95 -0
- package/index.js +21 -0
- package/package.json +24 -0
- package/run_midnight_node.js +45 -0
package/binary.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
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
|
+
const CURRENT_BINARY_VERSION = "0.12.0";
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
@returns {string} The platform and architecture of the current machine.
|
|
11
|
+
*/
|
|
12
|
+
function getPlatform() {
|
|
13
|
+
let platform = os.platform();
|
|
14
|
+
let arch = os.arch();
|
|
15
|
+
if (platform === "darwin") platform = "macos";
|
|
16
|
+
if (arch === "x64") arch = "amd64";
|
|
17
|
+
return `${platform}-${arch}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const FILE_NAME =
|
|
21
|
+
`midnight-node-${getPlatform()}-${CURRENT_BINARY_VERSION}.zip`;
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
@returns {string} The URL to download the binary for the current platform.
|
|
25
|
+
*/
|
|
26
|
+
function getBinaryUrl() {
|
|
27
|
+
const platform = getPlatform();
|
|
28
|
+
const supportedPlatforms = require("./package.json").supportedPlatforms;
|
|
29
|
+
if (!supportedPlatforms.includes(platform)) {
|
|
30
|
+
throw new Error(`Unsupported platform: ${platform}`);
|
|
31
|
+
}
|
|
32
|
+
return `https://paima-midnight.nyc3.cdn.digitaloceanspaces.com/binaries/${FILE_NAME}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
@returns {Promise<void>} Downloads and saves the binary for the current platform.
|
|
37
|
+
*/
|
|
38
|
+
async function downloadAndSaveBinary() {
|
|
39
|
+
const url = getBinaryUrl();
|
|
40
|
+
try {
|
|
41
|
+
console.error(`Downloading... ${url}`);
|
|
42
|
+
|
|
43
|
+
const response = await axios.get(url, { responseType: "stream" });
|
|
44
|
+
const writer = fs.createWriteStream(
|
|
45
|
+
path.join(
|
|
46
|
+
__dirname,
|
|
47
|
+
FILE_NAME,
|
|
48
|
+
),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
response.data.pipe(writer);
|
|
52
|
+
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
writer.on("finish", resolve);
|
|
55
|
+
writer.on("error", reject);
|
|
56
|
+
});
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error("Error downloading binary:", error);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/*
|
|
64
|
+
@returns {Promise<void>} Unzips the binary for the current platform.
|
|
65
|
+
*/
|
|
66
|
+
async function unzipBinary() {
|
|
67
|
+
await extract(path.join(__dirname, FILE_NAME), {
|
|
68
|
+
dir: path.join(__dirname, "midnight-node"),
|
|
69
|
+
});
|
|
70
|
+
const platform = getPlatform();
|
|
71
|
+
const parts = platform.split("-");
|
|
72
|
+
const binaryName = `midnight-node-${platform}`;
|
|
73
|
+
if (parts[0] === "linux") {
|
|
74
|
+
fs.chmodSync(
|
|
75
|
+
path.join(__dirname, "midnight-node", binaryName),
|
|
76
|
+
0o755,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
fs.unlinkSync(
|
|
80
|
+
path.join(
|
|
81
|
+
__dirname,
|
|
82
|
+
FILE_NAME,
|
|
83
|
+
),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function binary() {
|
|
88
|
+
await downloadAndSaveBinary();
|
|
89
|
+
await unzipBinary();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
module.exports = {
|
|
93
|
+
binary,
|
|
94
|
+
getPlatform,
|
|
95
|
+
};
|
package/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const { binary } = require("./binary");
|
|
2
|
+
const { runMidnightNode } = require("./run_midnight_node");
|
|
3
|
+
const { getPlatform } = require("./binary");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
|
|
7
|
+
function checkIfBinaryExists() {
|
|
8
|
+
const platform = getPlatform();
|
|
9
|
+
const parts = platform.split("-");
|
|
10
|
+
const binaryName = `midnight-node-${platform}`;
|
|
11
|
+
return fs.existsSync(path.join(__dirname, "midnight-node", binaryName));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function main(args) {
|
|
15
|
+
if (!checkIfBinaryExists()) {
|
|
16
|
+
await binary();
|
|
17
|
+
}
|
|
18
|
+
runMidnightNode(process.env, args);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
main(process.argv.slice(2));
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effectstream/npm-midnight-node",
|
|
3
|
+
"version": "0.3.99",
|
|
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,45 @@
|
|
|
1
|
+
const { spawn } = require("child_process");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const { getPlatform } = require("./binary");
|
|
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 platform = getPlatform();
|
|
12
|
+
const parts = platform.split("-");
|
|
13
|
+
const binaryName = `midnight-node-${platform}`;
|
|
14
|
+
const binaryPath = path.join(__dirname, "midnight-node", binaryName);
|
|
15
|
+
|
|
16
|
+
console.log(
|
|
17
|
+
`Starting midnight-node binary at: ${binaryPath} ${args.join(" ")}`,
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const childProcess = spawn(binaryPath, args, {
|
|
21
|
+
env: env,
|
|
22
|
+
stdio: "inherit", // Inherit stdin, stdout, stderr from parent process
|
|
23
|
+
cwd: path.join(__dirname, "midnight-node"), // Run from inside the midnight-node directory
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
childProcess.on("spawn", () => {
|
|
27
|
+
console.log(`midnight-node process spawned with PID: ${childProcess.pid}`);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
childProcess.on("error", (error) => {
|
|
31
|
+
console.error("Failed to start midnight-node:", error);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
childProcess.on("exit", (code, signal) => {
|
|
35
|
+
if (code !== null) {
|
|
36
|
+
console.log(`midnight-node process exited with code: ${code}`);
|
|
37
|
+
} else {
|
|
38
|
+
console.log(`midnight-node process terminated by signal: ${signal}`);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return childProcess;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = { runMidnightNode };
|