@effectstream/npm-midnight-node 0.3.99 → 0.100.8

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 CHANGED
@@ -4,7 +4,8 @@ const axios = require("axios");
4
4
  const extract = require("extract-zip");
5
5
  const path = require("path");
6
6
 
7
- const CURRENT_BINARY_VERSION = "0.12.0";
7
+ const CURRENT_BINARY_VERSION = "0.22.2";
8
+ const FINAL_BINARY_NAME = "midnight-node";
8
9
 
9
10
  /*
10
11
  @returns {string} The platform and architecture of the current machine.
@@ -29,7 +30,8 @@ function getBinaryUrl() {
29
30
  if (!supportedPlatforms.includes(platform)) {
30
31
  throw new Error(`Unsupported platform: ${platform}`);
31
32
  }
32
- return `https://paima-midnight.nyc3.cdn.digitaloceanspaces.com/binaries/${FILE_NAME}`;
33
+
34
+ return `https://github.com/effectstream/binaries/releases/download/0.3.120/${FILE_NAME}`;
33
35
  }
34
36
 
35
37
  /*
@@ -64,24 +66,41 @@ async function downloadAndSaveBinary() {
64
66
  @returns {Promise<void>} Unzips the binary for the current platform.
65
67
  */
66
68
  async function unzipBinary() {
67
- await extract(path.join(__dirname, FILE_NAME), {
68
- dir: path.join(__dirname, "midnight-node"),
69
- });
69
+ const binaryDir = path.join(__dirname, "midnight-node");
70
+ await extract(path.join(__dirname, FILE_NAME), { dir: binaryDir });
70
71
  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
- ),
72
+ const extractedBinaryPath = path.join(
73
+ binaryDir,
74
+ `midnight-node-${platform}-${CURRENT_BINARY_VERSION}`,
84
75
  );
76
+ const finalBinaryPath = path.join(binaryDir, FINAL_BINARY_NAME);
77
+
78
+ // Rename the extracted file to midnight-node
79
+ if (fs.existsSync(extractedBinaryPath)) {
80
+ if (fs.existsSync(finalBinaryPath)) {
81
+ fs.unlinkSync(finalBinaryPath);
82
+ }
83
+ fs.renameSync(extractedBinaryPath, finalBinaryPath);
84
+ } else {
85
+ throw new Error(`Extracted binary not found at: ${extractedBinaryPath}`);
86
+ }
87
+
88
+ // Clean up any other extracted files (e.g., readme.md)
89
+ const files = fs.readdirSync(binaryDir);
90
+ for (const file of files) {
91
+ const filePath = path.join(binaryDir, file);
92
+ if (filePath !== finalBinaryPath && fs.statSync(filePath).isFile()) {
93
+ fs.unlinkSync(filePath);
94
+ }
95
+ }
96
+
97
+ if (!fs.existsSync(finalBinaryPath)) {
98
+ throw new Error(`Expected binary not found: ${finalBinaryPath}`);
99
+ }
100
+
101
+ fs.chmodSync(finalBinaryPath, 0o755);
102
+
103
+ fs.unlinkSync(path.join(__dirname, FILE_NAME));
85
104
  }
86
105
 
87
106
  async function binary() {
@@ -89,7 +108,35 @@ async function binary() {
89
108
  await unzipBinary();
90
109
  }
91
110
 
111
+ async function cleanBinaries() {
112
+ const binaryDir = path.join(__dirname, "midnight-node");
113
+ const zipPath = path.join(__dirname, FILE_NAME);
114
+
115
+ let deletedFiles = [];
116
+
117
+ if (fs.existsSync(binaryDir)) {
118
+ try {
119
+ fs.rmSync(binaryDir, { recursive: true, force: true });
120
+ deletedFiles.push(binaryDir);
121
+ } catch (error) {
122
+ console.error(`Error removing directory ${binaryDir}:`, error.message);
123
+ }
124
+ }
125
+
126
+ if (fs.existsSync(zipPath)) {
127
+ try {
128
+ fs.unlinkSync(zipPath);
129
+ deletedFiles.push(zipPath);
130
+ } catch (error) {
131
+ console.error(`Error removing file ${zipPath}:`, error.message);
132
+ }
133
+ }
134
+
135
+ return deletedFiles;
136
+ }
137
+
92
138
  module.exports = {
93
139
  binary,
94
140
  getPlatform,
141
+ cleanBinaries,
95
142
  };
package/index.js CHANGED
@@ -1,21 +1,81 @@
1
- const { binary } = require("./binary");
1
+ const { binary, cleanBinaries } = require("./binary");
2
2
  const { runMidnightNode } = require("./run_midnight_node");
3
- const { getPlatform } = require("./binary");
4
3
  const fs = require("fs");
5
4
  const path = require("path");
6
5
 
6
+ const FINAL_BINARY_NAME = "midnight-node";
7
+
7
8
  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));
9
+ return fs.existsSync(
10
+ path.join(__dirname, "midnight-node", FINAL_BINARY_NAME),
11
+ );
12
+ }
13
+
14
+ function showUsage() {
15
+ console.log(`
16
+ Usage: node index.js [options] [args...]
17
+
18
+ Options:
19
+ --clean-binaries Delete downloaded binaries and download them again
20
+ --only-clean Only delete downloaded binaries without downloading them again
21
+ --help, -h Show this help message
22
+ `);
23
+ }
24
+
25
+ function parseFlags(args) {
26
+ const flags = {
27
+ cleanBinaries: false,
28
+ onlyClean: false,
29
+ showHelp: false,
30
+ remainingArgs: [],
31
+ };
32
+
33
+ for (let i = 0; i < args.length; i++) {
34
+ if (args[i] === "--clean-binaries") {
35
+ flags.cleanBinaries = true;
36
+ } else if (args[i] === "--only-clean") {
37
+ flags.onlyClean = true;
38
+ } else if (args[i] === "--help" || args[i] === "-h") {
39
+ flags.showHelp = true;
40
+ } else {
41
+ flags.remainingArgs.push(args[i]);
42
+ }
43
+ }
44
+
45
+ return flags;
12
46
  }
13
47
 
14
48
  async function main(args) {
15
- if (!checkIfBinaryExists()) {
49
+ const flags = parseFlags(args);
50
+
51
+ if (flags.showHelp) {
52
+ showUsage();
53
+ process.exit(0);
54
+ }
55
+
56
+ if (flags.onlyClean) {
57
+ console.log("Cleaning downloaded binaries...");
58
+ const deletedFiles = await cleanBinaries();
59
+ if (deletedFiles.length > 0) {
60
+ console.log("Deleted:", deletedFiles.join(", "));
61
+ } else {
62
+ console.log("No downloaded binaries found to delete.");
63
+ }
64
+ process.exit(0);
65
+ }
66
+
67
+ if (flags.cleanBinaries || !checkIfBinaryExists()) {
68
+ if (flags.cleanBinaries) {
69
+ console.log("Cleaning downloaded binaries...");
70
+ await cleanBinaries();
71
+ }
16
72
  await binary();
17
73
  }
18
- runMidnightNode(process.env, args);
74
+ runMidnightNode(process.env, flags.remainingArgs);
19
75
  }
20
76
 
77
+ module.exports = {
78
+ cleanBinaries,
79
+ };
80
+
21
81
  main(process.argv.slice(2));
@@ -0,0 +1,36 @@
1
+ [
2
+ {
3
+ "registrations": [
4
+ {
5
+ "name": "LocalValidator1",
6
+ "aura_pub_key": "0xb606fc73f57f03cdb4c932d475ab426043e429cecc2ffff0d2672b0df8398c48",
7
+ "grandpa_pub_key": "0xbf3a763d817cee09bf785b9cc6118f58dab5c03f3ace6d524899bcb28ac74f27",
8
+ "sidechain_pub_key": "0x0333022898140662dfea847e3cbfe5e989845ac6766e83472f8b0c650d85e77bae",
9
+ "mainchain_pub_key": "0xd5f64925e8722583ab9f8bb633a6938780873cf59504b3d12527719d3310b0ff",
10
+ "mainchain_signature": "0x9e417ede1ab0d710ce3d5854627baef9b32d4dd69ec75f20d1f28234f167b3f679dadafce995fc63a05eefefa322ebe84b5ffccf526481710d086a16aef61506",
11
+ "sidechain_signature": "0x568e305af7f6d51668a76f3aaa72aa3f0f3554be20f150dcabb759f8bff66b484bf66cf248916a40bda649b3ce302ecaf689b9c7b4abd66b44b2aeb039ff85a5",
12
+ "registration_utxo": "ff428f8f916a832d146e58f3656c17c769a6bbc44bba1693fe2a4f9c605b8f16#0",
13
+ "status": "Active"
14
+ }
15
+ ],
16
+ "permissioned": [
17
+ {
18
+ "name": "LocalPermissioned1",
19
+ "aura_pub_key": "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d",
20
+ "grandpa_pub_key": "0x88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee",
21
+ "sidechain_pub_key": "0x020a1091341fe5664bfa1782d5e04779689068c916b04cb365ec3153755684d9a1",
22
+ "mainchain_pub_key": "0xd5f64925e8722583ab9f8bb633a6938780873cf59504b3d12527719d3310b0ff",
23
+ "mainchain_signature": "0x9e417ede1ab0d710ce3d5854627baef9b32d4dd69ec75f20d1f28234f167b3f679dadafce995fc63a05eefefa322ebe84b5ffccf526481710d086a16aef61506",
24
+ "sidechain_signature": "0x568e305af7f6d51668a76f3aaa72aa3f0f3554be20f150dcabb759f8bff66b484bf66cf248916a40bda649b3ce302ecaf689b9c7b4abd66b44b2aeb039ff85a5",
25
+ "registration_utxo": "ff428f8f916a832d146e58f3656c17c769a6bbc44bba1693fe2a4f9c605b8f16#1",
26
+ "status": "Active"
27
+ }
28
+ ],
29
+ "nonce": "0x1234",
30
+ "d_parameter": {
31
+ "permissioned": 6,
32
+ "registered": 0
33
+ }
34
+ }
35
+ ]
36
+
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@effectstream/npm-midnight-node",
3
- "version": "0.3.99",
3
+ "version": "0.100.8",
4
4
  "description": "Downloads and starts the binary for Midnight Node",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "clean": "node index.js --clean-binaries",
9
+ "clean-only": "node index.js --only-clean"
8
10
  },
9
11
  "bin": {
10
12
  "npm-midnight-node": "index.js"
@@ -21,4 +23,4 @@
21
23
  "axios-proxy-builder": "^0.1.2",
22
24
  "extract-zip": "^2.0.1"
23
25
  }
24
- }
26
+ }
@@ -1,6 +1,22 @@
1
1
  const { spawn } = require("child_process");
2
2
  const path = require("path");
3
- const { getPlatform } = require("./binary");
3
+
4
+ /**
5
+ * Applies default environment variables for midnight-node if not already set
6
+ * @param {Object} env - Environment variables object
7
+ * @returns {Object} Environment variables with defaults applied
8
+ */
9
+ function applyDefaultEnv(env) {
10
+ const newEnv = { ...env };
11
+ if (!("MC__SLOT_DURATION_MILLIS" in newEnv)) {
12
+ newEnv.MC__SLOT_DURATION_MILLIS = "1000";
13
+ }
14
+ if (!("MOCK_REGISTRATIONS_FILE" in newEnv)) {
15
+ newEnv.MOCK_REGISTRATIONS_FILE = path.join(__dirname, "local-mock.json");
16
+ }
17
+ return newEnv;
18
+ }
19
+
4
20
  /**
5
21
  * Executes the midnight-node binary as a child process
6
22
  * @param {Object} env - Environment variables to pass to the child process
@@ -8,9 +24,9 @@ const { getPlatform } = require("./binary");
8
24
  * @returns {ChildProcess} The spawned child process
9
25
  */
10
26
  function runMidnightNode(env = process.env, args = []) {
11
- const platform = getPlatform();
12
- const parts = platform.split("-");
13
- const binaryName = `midnight-node-${platform}`;
27
+ const newEnv = applyDefaultEnv(env);
28
+
29
+ const binaryName = "midnight-node";
14
30
  const binaryPath = path.join(__dirname, "midnight-node", binaryName);
15
31
 
16
32
  console.log(
@@ -18,7 +34,7 @@ function runMidnightNode(env = process.env, args = []) {
18
34
  );
19
35
 
20
36
  const childProcess = spawn(binaryPath, args, {
21
- env: env,
37
+ env: newEnv,
22
38
  stdio: "inherit", // Inherit stdin, stdout, stderr from parent process
23
39
  cwd: path.join(__dirname, "midnight-node"), // Run from inside the midnight-node directory
24
40
  });