@paimaexample/npm-midnight-node 0.3.121 → 0.3.123

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.12.1";
8
+ const FINAL_BINARY_NAME = "midnight-node";
8
9
 
9
10
  /*
10
11
  @returns {string} The platform and architecture of the current machine.
@@ -29,10 +30,8 @@ function getBinaryUrl() {
29
30
  if (!supportedPlatforms.includes(platform)) {
30
31
  throw new Error(`Unsupported platform: ${platform}`);
31
32
  }
32
- if (platform === "macos-arm64" && CURRENT_BINARY_VERSION === "0.12.0") {
33
- return `https://github.com/effectstream/binaries/releases/download/0.3.120/${FILE_NAME}`;
34
- }
35
- 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}`;
36
35
  }
37
36
 
38
37
  /*
@@ -66,25 +65,66 @@ async function downloadAndSaveBinary() {
66
65
  /*
67
66
  @returns {Promise<void>} Unzips the binary for the current platform.
68
67
  */
68
+ function findExtractedBinary(binaryDir, platform) {
69
+ const candidateNames = new Set([
70
+ FINAL_BINARY_NAME,
71
+ `midnight-node-${platform}`,
72
+ `midnight-node-${platform}-${CURRENT_BINARY_VERSION}`,
73
+ ]);
74
+
75
+ const explore = (dir) => {
76
+ for (const entry of fs.readdirSync(dir)) {
77
+ const entryPath = path.join(dir, entry);
78
+ const entryStats = fs.statSync(entryPath);
79
+
80
+ if (entryStats.isFile() && candidateNames.has(entry)) {
81
+ return entryPath;
82
+ }
83
+
84
+ if (entryStats.isDirectory()) {
85
+ for (const candidate of candidateNames) {
86
+ const nestedPath = path.join(entryPath, candidate);
87
+ if (fs.existsSync(nestedPath) && fs.statSync(nestedPath).isFile()) {
88
+ return nestedPath;
89
+ }
90
+ }
91
+ const nestedResult = explore(entryPath);
92
+ if (nestedResult) return nestedResult;
93
+ }
94
+ }
95
+ return null;
96
+ };
97
+
98
+ return explore(binaryDir);
99
+ }
100
+
69
101
  async function unzipBinary() {
70
- await extract(path.join(__dirname, FILE_NAME), {
71
- dir: path.join(__dirname, "midnight-node"),
72
- });
102
+ const binaryDir = path.join(__dirname, "midnight-node");
103
+ await extract(path.join(__dirname, FILE_NAME), { dir: binaryDir });
73
104
  const platform = getPlatform();
74
- const parts = platform.split("-");
75
- const binaryName = `midnight-node-${platform}`;
76
- if (parts[0] === "linux") {
77
- fs.chmodSync(
78
- path.join(__dirname, "midnight-node", binaryName),
79
- 0o755,
105
+ const finalBinaryPath = path.join(binaryDir, FINAL_BINARY_NAME);
106
+ const extractedBinaryPath = findExtractedBinary(binaryDir, platform);
107
+
108
+ if (!extractedBinaryPath) {
109
+ throw new Error(
110
+ `Expected binary not found after extraction in ${binaryDir}`,
80
111
  );
81
112
  }
82
- fs.unlinkSync(
83
- path.join(
84
- __dirname,
85
- FILE_NAME,
86
- ),
87
- );
113
+
114
+ if (extractedBinaryPath !== finalBinaryPath) {
115
+ if (fs.existsSync(finalBinaryPath)) {
116
+ fs.unlinkSync(finalBinaryPath);
117
+ }
118
+ fs.renameSync(extractedBinaryPath, finalBinaryPath);
119
+ }
120
+
121
+ if (!fs.existsSync(finalBinaryPath)) {
122
+ throw new Error(`Expected binary not found: ${finalBinaryPath}`);
123
+ }
124
+
125
+ fs.chmodSync(finalBinaryPath, 0o755);
126
+
127
+ fs.unlinkSync(path.join(__dirname, FILE_NAME));
88
128
  }
89
129
 
90
130
  async function binary() {
package/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  const { binary } = 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
12
  }
13
13
 
14
14
  async function main(args) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paimaexample/npm-midnight-node",
3
- "version": "0.3.121",
3
+ "version": "0.3.123",
4
4
  "description": "Downloads and starts the binary for Midnight Node",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,6 +1,7 @@
1
1
  const { spawn } = require("child_process");
2
2
  const path = require("path");
3
- const { getPlatform } = require("./binary");
3
+
4
+ const BINARY_NAME = "midnight-node";
4
5
  /**
5
6
  * Executes the midnight-node binary as a child process
6
7
  * @param {Object} env - Environment variables to pass to the child process
@@ -8,10 +9,7 @@ const { getPlatform } = require("./binary");
8
9
  * @returns {ChildProcess} The spawned child process
9
10
  */
10
11
  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);
12
+ const binaryPath = path.join(__dirname, "midnight-node", BINARY_NAME);
15
13
 
16
14
  console.log(
17
15
  `Starting midnight-node binary at: ${binaryPath} ${args.join(" ")}`,