@aptove/bridge 0.1.11 → 0.1.14

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.
Files changed (2) hide show
  1. package/package.json +6 -6
  2. package/postinstall.js +145 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aptove/bridge",
3
- "version": "0.1.11",
3
+ "version": "0.1.14",
4
4
  "description": "ACP bridge — connects ACP agents to mobile and desktop clients over WebSocket",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -33,10 +33,10 @@
33
33
  "node": ">=16"
34
34
  },
35
35
  "optionalDependencies": {
36
- "@aptove/bridge-darwin-arm64": "0.1.11",
37
- "@aptove/bridge-darwin-x64": "0.1.11",
38
- "@aptove/bridge-linux-arm64": "0.1.11",
39
- "@aptove/bridge-linux-x64": "0.1.11",
40
- "@aptove/bridge-win32-x64": "0.1.11"
36
+ "@aptove/bridge-darwin-arm64": "0.1.14",
37
+ "@aptove/bridge-darwin-x64": "0.1.14",
38
+ "@aptove/bridge-linux-arm64": "0.1.14",
39
+ "@aptove/bridge-linux-x64": "0.1.14",
40
+ "@aptove/bridge-win32-x64": "0.1.14"
41
41
  }
42
42
  }
package/postinstall.js CHANGED
@@ -1,11 +1,13 @@
1
1
  /**
2
2
  * bridge postinstall script
3
3
  *
4
- * Checks whether the platform-specific binary package was installed.
5
- * npm sometimes skips optional dependencies during global installs.
6
- * If so, prints the exact command to complete installation.
4
+ * Ensures the platform-specific binary package is installed at the correct version.
5
+ * npm sometimes skips optional dependencies during global installs, so we detect
6
+ * this and install the platform package explicitly if needed.
7
+ * Validates the final binary by running it with --version.
7
8
  */
8
9
 
10
+ const { execSync, spawnSync } = require('child_process');
9
11
  const path = require('path');
10
12
  const fs = require('fs');
11
13
 
@@ -17,31 +19,150 @@ const PLATFORM_PACKAGES = {
17
19
  'win32-x64': '@aptove/bridge-win32-x64',
18
20
  };
19
21
 
20
- const platformKey = `${process.platform}-${process.arch}`;
21
- const packageName = PLATFORM_PACKAGES[platformKey];
22
+ function isBinaryPresent(binaryPath) {
23
+ return fs.existsSync(binaryPath);
24
+ }
25
+
26
+ // Ensures the binary has executable permissions (no-op on Windows).
27
+ function ensureExecutable(binaryPath) {
28
+ if (process.platform === 'win32') return;
29
+ try {
30
+ fs.chmodSync(binaryPath, 0o755);
31
+ } catch (e) {
32
+ // best-effort
33
+ }
34
+ }
35
+
36
+ // Returns the version string from `bridge --version` (e.g. "0.1.12"), or null on failure.
37
+ function getBinaryVersion(binaryPath) {
38
+ const result = spawnSync(binaryPath, ['--version'], { stdio: 'pipe' });
39
+ if (result.error || result.status !== 0) return null;
40
+ const output = (result.stdout || '').toString().trim(); // e.g. "bridge 0.1.12"
41
+ const parts = output.split(' ');
42
+ return parts.length >= 2 ? parts[1] : output;
43
+ }
22
44
 
23
- if (!packageName) {
24
- console.warn(`⚠️ bridge: unsupported platform ${platformKey}`);
25
- process.exit(0);
45
+ // Reads the expected platform package version from the main package's optionalDependencies.
46
+ function getExpectedVersion(packageJsonPath) {
47
+ try {
48
+ delete require.cache[require.resolve(packageJsonPath)];
49
+ const pkg = require(packageJsonPath);
50
+ // optionalDependencies values are updated by the release workflow to match the published version
51
+ const deps = pkg.optionalDependencies || {};
52
+ const versions = Object.values(deps).filter(v => v !== '*');
53
+ return versions[0] || pkg.version;
54
+ } catch (e) {
55
+ return null;
56
+ }
26
57
  }
27
58
 
28
- const binaryName = process.platform === 'win32' ? 'bridge.exe' : 'bridge';
29
- let installed = false;
59
+ /**
60
+ * Main postinstall logic.
61
+ *
62
+ * All I/O is injectable for testing:
63
+ * baseDir — replaces __dirname for computing the sibling binary path
64
+ * packageJsonPath— path to the main package's package.json
65
+ * platformKey — override platform detection (default: process.platform-process.arch)
66
+ * npmPrefix — override npm_config_prefix
67
+ * installFn — (packageName, version) => void; throws on failure
68
+ * defaults to running `npm install` via execSync
69
+ * log / warn — console.log / console.warn replacements
70
+ * exitFn — process.exit replacement
71
+ */
72
+ function main({
73
+ baseDir = __dirname,
74
+ packageJsonPath = path.join(__dirname, 'package.json'),
75
+ platformKey = `${process.platform}-${process.arch}`,
76
+ npmPrefix = process.env.npm_config_prefix,
77
+ installFn = null,
78
+ log = (...a) => console.log(...a),
79
+ warn = (...a) => console.warn(...a),
80
+ exitFn = process.exit,
81
+ } = {}) {
82
+ const packageName = PLATFORM_PACKAGES[platformKey];
83
+
84
+ if (!packageName) {
85
+ warn(`⚠️ bridge: unsupported platform ${platformKey}`);
86
+ exitFn(0); return;
87
+ }
88
+
89
+ const binaryName = process.platform === 'win32' ? 'bridge.exe' : 'bridge';
90
+ const shortName = packageName.split('/')[1]; // e.g. "bridge-darwin-arm64"
91
+
92
+ // postinstall.js lives at @aptove/bridge/postinstall.js
93
+ // platform binary lives at @aptove/bridge-darwin-arm64/bin/bridge (sibling package)
94
+ const siblingBinaryPath = path.join(baseDir, '..', shortName, 'bin', binaryName);
95
+
96
+ function tryInstall(version) {
97
+ const fn = installFn || ((pkg, ver) => {
98
+ const prefixFlag = npmPrefix ? `--prefix "${npmPrefix}"` : '';
99
+ execSync(
100
+ `npm install ${prefixFlag} --no-save --no-audit --no-fund "${pkg}@${ver}"`,
101
+ { stdio: 'inherit' }
102
+ );
103
+ });
104
+ try {
105
+ log(` Installing ${packageName}@${version}...`);
106
+ fn(packageName, version);
107
+ // GitHub artifact uploads strip execute permissions; restore them.
108
+ if (isBinaryPresent(siblingBinaryPath)) ensureExecutable(siblingBinaryPath);
109
+ return true;
110
+ } catch (e) {
111
+ return false;
112
+ }
113
+ }
30
114
 
31
- try {
32
- const packageJsonPath = require.resolve(`${packageName}/package.json`);
33
- const binaryPath = path.join(path.dirname(packageJsonPath), 'bin', binaryName);
34
- installed = fs.existsSync(binaryPath);
35
- } catch (e) {
36
- // package not installed
115
+ const expectedVersion = getExpectedVersion(packageJsonPath);
116
+
117
+ if (isBinaryPresent(siblingBinaryPath)) {
118
+ // Fix permissions in case the binary was published without the execute bit
119
+ // (GitHub Actions artifacts do not preserve file permissions).
120
+ ensureExecutable(siblingBinaryPath);
121
+ const installedVersion = getBinaryVersion(siblingBinaryPath);
122
+ if (installedVersion && expectedVersion && installedVersion !== expectedVersion) {
123
+ log(`⬆ bridge: updating platform binary ${installedVersion} → ${expectedVersion}...`);
124
+ if (!tryInstall(expectedVersion)) {
125
+ warn(`⚠️ bridge: update failed — run: npm install -g ${packageName}@${expectedVersion}`);
126
+ exitFn(0); return;
127
+ }
128
+ } else {
129
+ log(`✓ bridge ${installedVersion || '(unknown)'} installed successfully for ${platformKey}`);
130
+ exitFn(0); return;
131
+ }
132
+ } else {
133
+ log(`\n⬇ bridge: platform binary not found, installing ${packageName}...`);
134
+ if (!tryInstall(expectedVersion || 'latest')) {
135
+ warn(`\n⚠️ bridge: failed to install ${packageName}`);
136
+ warn(` Run manually: npm install -g ${packageName}${expectedVersion ? '@' + expectedVersion : ''}`);
137
+ exitFn(0); return;
138
+ }
139
+ }
140
+
141
+ // Validate after install using sibling path directly.
142
+ // (require.resolve cannot be used here — Node.js caches negative module
143
+ // resolution results within the same process.)
144
+ if (isBinaryPresent(siblingBinaryPath)) {
145
+ ensureExecutable(siblingBinaryPath);
146
+ const installedVersion = getBinaryVersion(siblingBinaryPath);
147
+ if (installedVersion) {
148
+ if (expectedVersion && installedVersion !== expectedVersion) {
149
+ warn(`⚠️ bridge: installed ${installedVersion} but expected ${expectedVersion} (may not be published yet)`);
150
+ } else {
151
+ log(`✓ bridge ${installedVersion} installed successfully for ${platformKey}`);
152
+ }
153
+ } else {
154
+ warn(`⚠️ bridge: binary present but failed to run — try: ${siblingBinaryPath} --version`);
155
+ }
156
+ } else {
157
+ warn(`\n⚠️ bridge: binary not found after installation`);
158
+ warn(` Run manually: npm install -g ${packageName}${expectedVersion ? '@' + expectedVersion : ''}`);
159
+ }
160
+
161
+ exitFn(0);
37
162
  }
38
163
 
39
- if (installed) {
40
- console.log(`✓ bridge installed successfully for ${platformKey}`);
41
- } else {
42
- console.log('');
43
- console.log(`⚠️ bridge: platform binary not found (npm skipped optional dependency)`);
44
- console.log(` To complete installation, run:`);
45
- console.log(` npm install -g ${packageName}`);
46
- console.log('');
164
+ module.exports = { main, PLATFORM_PACKAGES, isBinaryPresent, ensureExecutable, getBinaryVersion, getExpectedVersion };
165
+
166
+ if (require.main === module) {
167
+ main();
47
168
  }