@spuentesp/lain-mcp 0.5.0 → 0.7.4
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/README.md +16 -13
- package/bin/lain.js +14 -24
- package/bin/lain.test.js +7 -142
- package/package.json +14 -4
- package/scripts/install.js +9 -259
- package/scripts/install.test.js +81 -260
- package/scripts/runtime.js +143 -0
package/README.md
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
|
-
# lain-mcp
|
|
1
|
+
# @spuentesp/lain-mcp
|
|
2
2
|
|
|
3
3
|
> Architectural code intelligence for AI coding agents.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install -g lain-mcp
|
|
8
|
+
npm install -g @spuentesp/lain-mcp
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
Or with npx (downloads binary on first run):
|
|
12
12
|
```bash
|
|
13
|
-
npx lain-mcp
|
|
13
|
+
npx @spuentesp/lain-mcp mcp
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
## What this installs
|
|
17
17
|
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
- **Models**: `~/.lain/models/` — empty dir for ONNX embedding models (optional)
|
|
18
|
+
- A native binary downloaded from the matching GitHub release.
|
|
19
|
+
- A cache entry separated by LAIN version and platform target.
|
|
20
|
+
- SHA-256 verification against the release's `SHA256SUMS` file.
|
|
22
21
|
|
|
23
22
|
## Configuration
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
Set `LAIN_VERSION` to run a different published version. Set
|
|
25
|
+
`LAIN_CACHE_DIR` to override the operating system's user cache directory.
|
|
26
|
+
An already verified cache entry works offline.
|
|
26
27
|
|
|
27
28
|
## Usage
|
|
28
29
|
|
|
@@ -33,7 +34,7 @@ After installation, add Lain to your MCP configuration:
|
|
|
33
34
|
"mcpServers": {
|
|
34
35
|
"lain": {
|
|
35
36
|
"command": "lain",
|
|
36
|
-
"args": ["
|
|
37
|
+
"args": ["mcp"]
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
}
|
|
@@ -42,13 +43,15 @@ After installation, add Lain to your MCP configuration:
|
|
|
42
43
|
Or run directly:
|
|
43
44
|
|
|
44
45
|
```bash
|
|
45
|
-
|
|
46
|
+
npx @spuentesp/lain-mcp mcp
|
|
46
47
|
```
|
|
47
48
|
|
|
48
49
|
## Uninstall
|
|
49
50
|
|
|
50
51
|
```bash
|
|
51
|
-
npm uninstall -g lain-mcp
|
|
52
|
-
# Binary and config remain in ~/.lain/ — delete manually to fully remove
|
|
53
|
-
rm -rf ~/.lain
|
|
52
|
+
npm uninstall -g @spuentesp/lain-mcp
|
|
54
53
|
```
|
|
54
|
+
|
|
55
|
+
The verified native binary remains in your user cache so a later install can
|
|
56
|
+
work offline. Remove the `lain` cache directory using your operating system's
|
|
57
|
+
cache-management tools if you also want to discard downloaded versions.
|
package/bin/lain.js
CHANGED
|
@@ -1,29 +1,19 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// Launcher stub — included in npm package.
|
|
3
|
-
// Real binary is downloaded by postinstall to ~/.lain/bin/
|
|
4
2
|
|
|
5
|
-
const path = require('path');
|
|
6
3
|
const { spawn } = require('child_process');
|
|
7
|
-
const
|
|
4
|
+
const { ensureBinary } = require('../scripts/runtime');
|
|
8
5
|
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
cwd: process.cwd(),
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
child.on('exit', (code) => process.exit(code ?? 0));
|
|
26
|
-
child.on('error', (err) => {
|
|
27
|
-
console.error('Failed to start lain:', err.message);
|
|
28
|
-
process.exit(1);
|
|
6
|
+
ensureBinary().then((binary) => {
|
|
7
|
+
const child = spawn(binary, process.argv.slice(2), { cwd: process.cwd(), stdio: 'inherit' });
|
|
8
|
+
child.on('error', (error) => {
|
|
9
|
+
process.stderr.write(`Failed to start Lain: ${error.message}\n`);
|
|
10
|
+
process.exitCode = 1;
|
|
11
|
+
});
|
|
12
|
+
child.on('exit', (code, signal) => {
|
|
13
|
+
if (signal) process.kill(process.pid, signal);
|
|
14
|
+
else process.exitCode = code == null ? 1 : code;
|
|
15
|
+
});
|
|
16
|
+
}).catch((error) => {
|
|
17
|
+
process.stderr.write(`Failed to prepare Lain: ${error.message}\n`);
|
|
18
|
+
process.exitCode = 1;
|
|
29
19
|
});
|
package/bin/lain.test.js
CHANGED
|
@@ -1,147 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// Tests for npm-shim bin/lain.js shim logic
|
|
3
2
|
|
|
4
3
|
const assert = require('assert');
|
|
4
|
+
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
|
-
const os = require('os');
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const isWin = process.platform === 'win32';
|
|
15
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
16
|
-
return path.join(LAIN_DIR, 'bin', launcherName);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function getLauncherContent(binaryPath, isWin = process.platform === 'win32') {
|
|
20
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
21
|
-
return isWin
|
|
22
|
-
? `@echo off\n"${binaryPath}" %*`
|
|
23
|
-
: `#!/bin/sh\nexec "${binaryPath}" "$@"\n`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// ─────────────────────────────────────────────
|
|
27
|
-
// Tests
|
|
28
|
-
// ─────────────────────────────────────────────
|
|
29
|
-
|
|
30
|
-
let passed = 0;
|
|
31
|
-
let failed = 0;
|
|
32
|
-
|
|
33
|
-
function test(name, fn) {
|
|
34
|
-
try {
|
|
35
|
-
fn();
|
|
36
|
-
console.log(` ✓ ${name}`);
|
|
37
|
-
passed++;
|
|
38
|
-
} catch (e) {
|
|
39
|
-
console.log(` ✗ ${name}`);
|
|
40
|
-
console.log(` ${e.message}`);
|
|
41
|
-
failed++;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
console.log('\n[ getLauncherPath ]');
|
|
46
|
-
|
|
47
|
-
test('launcher path for unix', () => {
|
|
48
|
-
const isWin = false;
|
|
49
|
-
const LAIN_DIR = path.join(os.homedir(), '.lain');
|
|
50
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
51
|
-
const launcherPath = path.join(LAIN_DIR, 'bin', launcherName);
|
|
52
|
-
assert.strictEqual(launcherPath, path.join(os.homedir(), '.lain', 'bin', 'lain-launcher'));
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test('launcher path for windows', () => {
|
|
56
|
-
const isWin = true;
|
|
57
|
-
const LAIN_DIR = path.join(os.homedir(), '.lain');
|
|
58
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
59
|
-
const launcherPath = path.join(LAIN_DIR, 'bin', launcherName);
|
|
60
|
-
assert.strictEqual(launcherPath, path.join(os.homedir(), '.lain', 'bin', 'lain-launcher.cmd'));
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('getLauncherPath uses os.homedir()', () => {
|
|
64
|
-
const result = getLauncherPath();
|
|
65
|
-
assert.ok(result.endsWith('.lain/bin/lain-launcher'));
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
console.log('\n[ launcher content ]');
|
|
69
|
-
|
|
70
|
-
test('unix launcher is valid sh script with shebang', () => {
|
|
71
|
-
const content = getLauncherContent('/home/user/.lain/bin/lain', false);
|
|
72
|
-
assert.ok(content.startsWith('#!/bin/sh'));
|
|
73
|
-
assert.ok(content.includes('exec "/home/user/.lain/bin/lain" "$@"'));
|
|
74
|
-
assert.ok(content.endsWith('\n'));
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test('windows launcher uses @echo off', () => {
|
|
78
|
-
const content = getLauncherContent('C:\\Users\\user\\.lain\\bin\\lain.exe', true);
|
|
79
|
-
assert.ok(content.startsWith('@echo off'));
|
|
80
|
-
assert.ok(content.includes('"C:\\Users\\user\\.lain\\bin\\lain.exe"'));
|
|
81
|
-
assert.ok(content.includes('%*'));
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
console.log('\n[ shim path chain ]');
|
|
85
|
-
|
|
86
|
-
test('bin/lain.js spawns the launcher, not the binary directly', () => {
|
|
87
|
-
// Verify the launcher path exists as a distinct concept from the binary path
|
|
88
|
-
const isWin = false;
|
|
89
|
-
const LAIN_DIR = path.join(os.homedir(), '.lain');
|
|
90
|
-
const binaryName = isWin ? 'lain.exe' : 'lain';
|
|
91
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
92
|
-
|
|
93
|
-
const binaryPath = path.join(LAIN_DIR, 'bin', binaryName); // ~/.lain/bin/lain
|
|
94
|
-
const launcherPath = path.join(LAIN_DIR, 'bin', launcherName); // ~/.lain/bin/lain-launcher
|
|
95
|
-
|
|
96
|
-
assert.notStrictEqual(binaryPath, launcherPath);
|
|
97
|
-
assert.ok(launcherPath.endsWith('lain-launcher'));
|
|
98
|
-
assert.ok(binaryPath.endsWith('lain'));
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
console.log('\n[ error message correctness ]');
|
|
102
|
-
|
|
103
|
-
test('error message references launcher path, not binary', () => {
|
|
104
|
-
const isWin = false;
|
|
105
|
-
const LAIN_DIR = path.join(os.homedir(), '.lain');
|
|
106
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
107
|
-
const launcherPath = path.join(LAIN_DIR, 'bin', launcherName);
|
|
108
|
-
|
|
109
|
-
const msg = `Lain binary not found at ${launcherPath}`;
|
|
110
|
-
// The message should mention the launcher, not the bare binary
|
|
111
|
-
assert.ok(msg.includes('lain-launcher'), 'error message should reference launcher');
|
|
112
|
-
assert.ok(!msg.includes('/lain"') || msg.includes('lain-launcher'),
|
|
113
|
-
'should not show bare binary path in error');
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
console.log('\n[ spawn argument forwarding ]');
|
|
117
|
-
|
|
118
|
-
test('process.argv.slice(2) strips node and script name', () => {
|
|
119
|
-
// Simulate: node /path/to/lain.js --workspace /foo bar
|
|
120
|
-
const argv = ['node', '/path/to/lain.js', '--workspace', '/foo', 'bar'];
|
|
121
|
-
const forwarded = argv.slice(2);
|
|
122
|
-
assert.deepStrictEqual(forwarded, ['--workspace', '/foo', 'bar']);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test('process.argv.slice(2) preserves all arguments', () => {
|
|
126
|
-
const argv = ['node', '/path/to/lain.js', '--transport', 'stdio', '--workspace', '/proj'];
|
|
127
|
-
const forwarded = argv.slice(2);
|
|
128
|
-
assert.deepStrictEqual(forwarded, ['--transport', 'stdio', '--workspace', '/proj']);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
console.log('\n[ exit code handling ]');
|
|
132
|
-
|
|
133
|
-
test('child exit code is passed through', () => {
|
|
134
|
-
// Simulate: code ?? 0
|
|
135
|
-
const cases = [[0, 0], [1, 1], [null, 0], [undefined, 0], [42, 42]];
|
|
136
|
-
for (const [code, expected] of cases) {
|
|
137
|
-
const result = code ?? 0;
|
|
138
|
-
assert.strictEqual(result, expected, `code=${code} should yield ${expected}`);
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
// ── Summary ────────────────────────────────
|
|
143
|
-
|
|
144
|
-
console.log(`\n${'─'.repeat(40)}`);
|
|
145
|
-
console.log(` Results: ${passed} passed, ${failed} failed`);
|
|
146
|
-
if (failed > 0) process.exit(1);
|
|
147
|
-
console.log(' All tests passed.\n');
|
|
7
|
+
const source = fs.readFileSync(path.join(__dirname, 'lain.js'), 'utf8');
|
|
8
|
+
assert(source.includes("require('../scripts/runtime')"));
|
|
9
|
+
assert(source.includes('process.argv.slice(2)'));
|
|
10
|
+
assert(source.includes("stdio: 'inherit'"));
|
|
11
|
+
assert(!source.includes("'.lain'"), 'launcher must not hard-code the legacy shared cache');
|
|
12
|
+
process.stdout.write('launcher delegates cache selection and forwards arguments\n');
|
package/package.json
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spuentesp/lain-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.4",
|
|
4
4
|
"mcpName": "io.github.spuentesp/lain",
|
|
5
|
-
"description": "
|
|
6
|
-
"keywords": [
|
|
5
|
+
"description": "High-performance knowledge graph MCP server for AI coding agents with semantic search.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"mcp",
|
|
8
|
+
"model-context-protocol",
|
|
9
|
+
"ai",
|
|
10
|
+
"coding",
|
|
11
|
+
"knowledge-graph",
|
|
12
|
+
"rust",
|
|
13
|
+
"semantic-search",
|
|
14
|
+
"code-analysis"
|
|
15
|
+
],
|
|
7
16
|
"license": "MIT",
|
|
8
17
|
"homepage": "https://github.com/spuentesp/lain",
|
|
9
18
|
"repository": {
|
|
@@ -27,7 +36,8 @@
|
|
|
27
36
|
"arm64"
|
|
28
37
|
],
|
|
29
38
|
"scripts": {
|
|
30
|
-
"postinstall": "node scripts/install.js"
|
|
39
|
+
"postinstall": "node scripts/install.js",
|
|
40
|
+
"test": "node scripts/install.test.js && node bin/lain.test.js"
|
|
31
41
|
},
|
|
32
42
|
"engines": {
|
|
33
43
|
"node": ">=18.0.0"
|
package/scripts/install.js
CHANGED
|
@@ -1,264 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// postinstall script — downloads the Lain binary and creates ~/.lain/ structure
|
|
3
2
|
|
|
4
|
-
const {
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const path = require('path');
|
|
3
|
+
const { ensureBinary } = require('./runtime');
|
|
7
4
|
|
|
8
|
-
const LAIN_DIR = path.join(process.env.HOME, '.lain');
|
|
9
|
-
const BIN_DIR = path.join(LAIN_DIR, 'bin');
|
|
10
|
-
const MODELS_DIR = path.join(LAIN_DIR, 'models');
|
|
11
|
-
const TOOLCHAINS_DIR = path.join(LAIN_DIR, 'toolchains');
|
|
12
|
-
const TUNING_FILE = path.join(LAIN_DIR, 'tuning.toml');
|
|
13
|
-
|
|
14
|
-
// Skip postinstall in CI unless explicitly requested
|
|
15
5
|
if (process.env.CI && !process.env.LAIN_FORCE_INSTALL) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// Detect platform
|
|
27
|
-
function getPlatform() {
|
|
28
|
-
const platform = process.platform;
|
|
29
|
-
const arch = process.arch;
|
|
30
|
-
if (platform === 'darwin' && arch === 'arm64') return 'aarch64-apple-darwin';
|
|
31
|
-
if (platform === 'darwin' && arch === 'x64') return 'x86_64-apple-darwin';
|
|
32
|
-
if (platform === 'linux' && arch === 'x64') return 'x86_64-unknown-linux-gnu';
|
|
33
|
-
if (platform === 'win32' && arch === 'x64') return 'x86_64-pc-windows-msvc.exe';
|
|
34
|
-
throw new Error(`Unsupported platform: ${platform}-${arch}`);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function getAssetName(platform, version) {
|
|
38
|
-
const versionSlug = version.replace(/^v/, '');
|
|
39
|
-
const map = {
|
|
40
|
-
'aarch64-apple-darwin': `lain-${versionSlug}-aarch64-apple-darwin`,
|
|
41
|
-
'x86_64-apple-darwin': `lain-${versionSlug}-x86_64-apple-darwin`,
|
|
42
|
-
'x86_64-unknown-linux-gnu': `lain-${versionSlug}-x86_64-unknown-linux-gnu`,
|
|
43
|
-
'x86_64-pc-windows-msvc.exe': `lain-${versionSlug}-x86_64-pc-windows-msvc.exe`,
|
|
44
|
-
};
|
|
45
|
-
const asset = map[platform];
|
|
46
|
-
if (!asset) throw new Error(`Unsupported platform for binary download: ${platform}`);
|
|
47
|
-
return asset;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Ensure directory exists
|
|
51
|
-
function ensureDir(dir) {
|
|
52
|
-
if (!fs.existsSync(dir)) {
|
|
53
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Download file with curl, fallback to PowerShell on Windows
|
|
58
|
-
function download(url, dest) {
|
|
59
|
-
console.log(` Downloading ${path.basename(dest)}...`);
|
|
60
|
-
try {
|
|
61
|
-
execSync(`curl -L "${url}" -o "${dest}" --progress-bar`, { stdio: 'inherit' });
|
|
62
|
-
} catch (e) {
|
|
63
|
-
if (process.platform === 'win32') {
|
|
64
|
-
console.log(' curl not found, using PowerShell...');
|
|
65
|
-
execSync(
|
|
66
|
-
`powershell -Command "Invoke-WebRequest -Uri \\"${url}\\" -OutFile \\"${dest}\\""`,
|
|
67
|
-
{ stdio: 'inherit' }
|
|
68
|
-
);
|
|
69
|
-
} else {
|
|
70
|
-
throw new Error(`curl not found and no fallback available for ${process.platform}`);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Create default tuning.toml
|
|
76
|
-
function createTuningConfig() {
|
|
77
|
-
const content = `# Lain tuning configuration — loaded from ~/.lain/tuning.toml
|
|
78
|
-
# All values are optional; omit a field to use the built-in default.
|
|
79
|
-
|
|
80
|
-
[semantic]
|
|
81
|
-
threshold = 0.1 # Semantic search cosine similarity floor
|
|
82
|
-
anchor_weight = 0.3 # Weight for anchor_score in hybrid ranking
|
|
83
|
-
|
|
84
|
-
[ingestion]
|
|
85
|
-
max_pattern_edges = 200
|
|
86
|
-
lsp_pool_size = 4
|
|
87
|
-
files_per_batch = 50
|
|
88
|
-
max_files_per_scan = 5000
|
|
89
|
-
cochange_commit_window = 100
|
|
90
|
-
cochange_min_pair_count = 2
|
|
91
|
-
cochange_max_commit_files = 100
|
|
92
|
-
|
|
93
|
-
[execution]
|
|
94
|
-
default_command_timeout_secs = 60
|
|
95
|
-
default_test_timeout_secs = 300
|
|
96
|
-
lsp_symbol_poll_timeout_secs = 2
|
|
97
|
-
lsp_symbol_poll_interval_ms = 50
|
|
98
|
-
`;
|
|
99
|
-
fs.writeFileSync(TUNING_FILE, content);
|
|
100
|
-
console.log(` Created ${TUNING_FILE}`);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Create toolchains/ README
|
|
104
|
-
function createToolchainsDir() {
|
|
105
|
-
ensureDir(TOOLCHAINS_DIR);
|
|
106
|
-
const readme = `# User toolchain overrides
|
|
107
|
-
# Drop .toml files here to override built-in toolchain profiles.
|
|
108
|
-
# See the built-in profiles in the Lain source at toolchains/
|
|
109
|
-
`;
|
|
110
|
-
fs.writeFileSync(path.join(TOOLCHAINS_DIR, 'README.md'), readme);
|
|
111
|
-
console.log(` Created ${TOOLCHAINS_DIR}/`);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Create models/ README
|
|
115
|
-
function createModelsDir() {
|
|
116
|
-
ensureDir(MODELS_DIR);
|
|
117
|
-
const readme = `# ONNX embedding models
|
|
118
|
-
# Place .onnx model files here for semantic search.
|
|
119
|
-
# Required files:
|
|
120
|
-
# model.onnx — the ONNX model
|
|
121
|
-
# tokenizer.json — the tokenizer
|
|
122
|
-
#
|
|
123
|
-
# Download the default model:
|
|
124
|
-
# ./scripts/install.sh --fast # if using Lain's install script
|
|
125
|
-
# or from https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2
|
|
126
|
-
`;
|
|
127
|
-
fs.writeFileSync(path.join(MODELS_DIR, 'README.md'), readme);
|
|
128
|
-
console.log(` Created ${MODELS_DIR}/`);
|
|
6
|
+
process.stderr.write('Skipping Lain download in CI; set LAIN_FORCE_INSTALL=1 to test installation.\n');
|
|
7
|
+
} else if (process.env.npm_config_ignore_scripts === 'true') {
|
|
8
|
+
process.stderr.write('Skipping Lain download because npm scripts are disabled.\n');
|
|
9
|
+
} else {
|
|
10
|
+
ensureBinary().catch((error) => {
|
|
11
|
+
process.stderr.write(`Lain installation failed: ${error.message}\n`);
|
|
12
|
+
process.exitCode = 1;
|
|
13
|
+
});
|
|
129
14
|
}
|
|
130
|
-
|
|
131
|
-
// Create the launcher script (shell for unix, batch for windows)
|
|
132
|
-
function createLauncher(binaryPath) {
|
|
133
|
-
const isWin = process.platform === 'win32';
|
|
134
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
135
|
-
const launcherPath = path.join(BIN_DIR, launcherName);
|
|
136
|
-
const content = isWin
|
|
137
|
-
? `@echo off\n"${binaryPath}" %*`
|
|
138
|
-
: `#!/bin/sh\nexec "${binaryPath}" "$@"\n`;
|
|
139
|
-
fs.writeFileSync(launcherPath, content);
|
|
140
|
-
if (!isWin) fs.chmodSync(launcherPath, 0o755);
|
|
141
|
-
console.log(` Created ${launcherPath}`);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Create symlink in node_modules/.bin for npm-installed binaries
|
|
145
|
-
function createSymlink() {
|
|
146
|
-
const isWin = process.platform === 'win32';
|
|
147
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
148
|
-
// node_modules/.bin is inside the package, not a sibling of node_modules
|
|
149
|
-
const nodeBin = path.join(__dirname, '..', 'node_modules', '.bin', launcherName);
|
|
150
|
-
try {
|
|
151
|
-
const binDir = path.dirname(nodeBin);
|
|
152
|
-
if (!fs.existsSync(binDir)) {
|
|
153
|
-
fs.mkdirSync(binDir, { recursive: true });
|
|
154
|
-
}
|
|
155
|
-
if (isWin) {
|
|
156
|
-
// nodeBin already ends in .cmd via launcherName, don't append again
|
|
157
|
-
fs.writeFileSync(nodeBin, `@echo off\n"${path.join(BIN_DIR, launcherName)}" %*`);
|
|
158
|
-
} else {
|
|
159
|
-
if (fs.existsSync(nodeBin)) fs.unlinkSync(nodeBin);
|
|
160
|
-
fs.symlinkSync(path.join(BIN_DIR, launcherName), nodeBin);
|
|
161
|
-
}
|
|
162
|
-
console.log(` Linked to node_modules/.bin/${launcherName}`);
|
|
163
|
-
} catch (e) {
|
|
164
|
-
if (e.code !== 'ENOENT' && e.code !== 'EEXIST') {
|
|
165
|
-
console.warn(` Warning: could not create .bin symlink: ${e.message}`);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Main install
|
|
171
|
-
async function install() {
|
|
172
|
-
console.log('\n=== Lain installer ===\n');
|
|
173
|
-
|
|
174
|
-
// Ensure directories
|
|
175
|
-
ensureDir(BIN_DIR);
|
|
176
|
-
ensureDir(LAIN_DIR);
|
|
177
|
-
|
|
178
|
-
const platform = getPlatform();
|
|
179
|
-
const version = `v${require('../package.json').version}`;
|
|
180
|
-
const assetName = getAssetName(platform, version);
|
|
181
|
-
// On unix the asset IS the binary; on windows it's .exe
|
|
182
|
-
const binaryName = process.platform === 'win32' ? 'lain.exe' : 'lain';
|
|
183
|
-
// Download to a staging name first, then rename to avoid overwriting launcher
|
|
184
|
-
const stagingPath = path.join(BIN_DIR, binaryName + '.new');
|
|
185
|
-
const binaryPath = path.join(BIN_DIR, binaryName);
|
|
186
|
-
const githubRepo = 'spuentesp/lain';
|
|
187
|
-
const url = `https://github.com/${githubRepo}/releases/download/${version}/${assetName}`;
|
|
188
|
-
|
|
189
|
-
console.log(` Platform: ${platform}`);
|
|
190
|
-
console.log(` Binary: ${binaryPath}`);
|
|
191
|
-
|
|
192
|
-
// Download binary
|
|
193
|
-
if (fs.existsSync(binaryPath)) {
|
|
194
|
-
console.log(' Binary already exists — skipping download');
|
|
195
|
-
} else {
|
|
196
|
-
console.log(` Downloading from GitHub releases...`);
|
|
197
|
-
download(url, stagingPath);
|
|
198
|
-
fs.renameSync(stagingPath, binaryPath);
|
|
199
|
-
if (!process.platform.startsWith('win')) {
|
|
200
|
-
fs.chmodSync(binaryPath, 0o755);
|
|
201
|
-
}
|
|
202
|
-
console.log(` Installed ${binaryPath}`);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// Create config directories
|
|
206
|
-
if (!fs.existsSync(TUNING_FILE)) {
|
|
207
|
-
createTuningConfig();
|
|
208
|
-
} else {
|
|
209
|
-
console.log(` tuning.toml already exists — skipping`);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
if (!fs.existsSync(TOOLCHAINS_DIR)) {
|
|
213
|
-
createToolchainsDir();
|
|
214
|
-
} else {
|
|
215
|
-
console.log(` toolchains/ already exists — skipping`);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (!fs.existsSync(MODELS_DIR)) {
|
|
219
|
-
createModelsDir();
|
|
220
|
-
} else {
|
|
221
|
-
console.log(` models/ already exists — skipping`);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// Create launcher
|
|
225
|
-
createLauncher(binaryPath);
|
|
226
|
-
|
|
227
|
-
// Symlink for npm
|
|
228
|
-
createSymlink();
|
|
229
|
-
|
|
230
|
-
// Post-install: if a known agent config dir is present, run
|
|
231
|
-
// `lain init --agent auto -y` so the user lands on a working MCP
|
|
232
|
-
// configuration. Skip with LAIN_SKIP_INIT=1 for CI/scripted installs.
|
|
233
|
-
const agentDirs = [
|
|
234
|
-
path.join(process.env.HOME, '.claude'),
|
|
235
|
-
path.join(process.env.HOME, '.gemini'),
|
|
236
|
-
path.join(process.env.HOME, '.cursor'),
|
|
237
|
-
path.join(process.env.HOME, '.codeium', 'windsurf'),
|
|
238
|
-
path.join(process.env.HOME, '.cline'),
|
|
239
|
-
path.join(process.env.HOME, '.kimi-code'),
|
|
240
|
-
];
|
|
241
|
-
const hasAgentConfig = agentDirs.some(d => fs.existsSync(d));
|
|
242
|
-
|
|
243
|
-
if (hasAgentConfig && !process.env.LAIN_SKIP_INIT) {
|
|
244
|
-
console.log('\nDetected an agent config directory. Running `lain init --agent auto -y`...');
|
|
245
|
-
const result = spawnSync(launcherPath, ['init', '--agent', 'auto', '-y'], { stdio: 'inherit' });
|
|
246
|
-
if (result.status !== 0) {
|
|
247
|
-
console.warn(' init failed; run manually: lain init --agent <name>');
|
|
248
|
-
}
|
|
249
|
-
} else {
|
|
250
|
-
console.log('\nNo agent config detected. To configure lain for your agent, run:');
|
|
251
|
-
console.log(` ${launcherPath} init --agent <claude|gemini|cursor|windsurf|cline|kimi>`);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
console.log('\n=== Installation complete ===\n');
|
|
255
|
-
console.log(' To use Lain:');
|
|
256
|
-
console.log(` ${binaryPath} --workspace /path/to/project`);
|
|
257
|
-
console.log('\n Or add to your PATH:');
|
|
258
|
-
console.log(` export PATH="$HOME/.lain/bin:$PATH"\n`);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
install().catch((err) => {
|
|
262
|
-
console.error('Installation failed:', err.message);
|
|
263
|
-
process.exit(1);
|
|
264
|
-
});
|
package/scripts/install.test.js
CHANGED
|
@@ -1,280 +1,101 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// Tests for npm-shim install.js logic
|
|
3
2
|
|
|
4
3
|
const assert = require('assert');
|
|
5
|
-
const
|
|
6
|
-
const os = require('os');
|
|
4
|
+
const crypto = require('crypto');
|
|
7
5
|
const fs = require('fs');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const { execFileSync } = require('child_process');
|
|
9
|
+
const runtime = require('./runtime');
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// so tests run against the same code paths.
|
|
12
|
-
// ─────────────────────────────────────────────
|
|
13
|
-
|
|
14
|
-
function getPlatform(platform = process.platform, arch = process.arch) {
|
|
15
|
-
if (platform === 'darwin' && arch === 'arm64') return 'aarch64-apple-darwin';
|
|
16
|
-
if (platform === 'darwin' && arch === 'x64') return 'x86_64-apple-darwin';
|
|
17
|
-
if (platform === 'linux' && arch === 'x64') return 'x86_64-unknown-linux-gnu';
|
|
18
|
-
if (platform === 'win32' && arch === 'x64') return 'x86_64-pc-windows-msvc.exe';
|
|
19
|
-
throw new Error(`Unsupported platform: ${platform}-${arch}`);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function getAssetName(platform, version) {
|
|
23
|
-
const versionSlug = version.replace(/^v/, '');
|
|
24
|
-
const map = {
|
|
25
|
-
'aarch64-apple-darwin': `lain-${versionSlug}-aarch64-apple-darwin`,
|
|
26
|
-
'x86_64-apple-darwin': `lain-${versionSlug}-x86_64-apple-darwin`,
|
|
27
|
-
'x86_64-unknown-linux-gnu': `lain-${versionSlug}-x86_64-unknown-linux-gnu`,
|
|
28
|
-
'x86_64-pc-windows-msvc.exe': `lain-${versionSlug}-x86_64-pc-windows-msvc.exe`,
|
|
29
|
-
};
|
|
30
|
-
const asset = map[platform];
|
|
31
|
-
if (!asset) throw new Error(`Unsupported platform for binary download: ${platform}`);
|
|
32
|
-
return asset;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function getBinaryName(isWin) {
|
|
36
|
-
return isWin ? 'lain.exe' : 'lain';
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function buildUrl(platform, version = 'v0.1.0', repo = 'spuentesp/lain') {
|
|
40
|
-
const assetName = getAssetName(platform, version);
|
|
41
|
-
return `https://github.com/${repo}/releases/download/${version}/${assetName}`;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// ─────────────────────────────────────────────
|
|
45
|
-
// Tests
|
|
46
|
-
// ─────────────────────────────────────────────
|
|
47
|
-
|
|
48
|
-
let passed = 0;
|
|
49
|
-
let failed = 0;
|
|
50
|
-
|
|
51
|
-
function test(name, fn) {
|
|
52
|
-
try {
|
|
53
|
-
fn();
|
|
54
|
-
console.log(` ✓ ${name}`);
|
|
55
|
-
passed++;
|
|
56
|
-
} catch (e) {
|
|
57
|
-
console.log(` ✗ ${name}`);
|
|
58
|
-
console.log(` ${e.message}`);
|
|
59
|
-
failed++;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// ── Platform detection ─────────────────────
|
|
64
|
-
|
|
65
|
-
console.log('\n[ getPlatform ]');
|
|
66
|
-
|
|
67
|
-
test('darwin arm64 → aarch64-apple-darwin', () => {
|
|
68
|
-
assert.strictEqual(getPlatform('darwin', 'arm64'), 'aarch64-apple-darwin');
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test('darwin x64 → x86_64-apple-darwin', () => {
|
|
72
|
-
assert.strictEqual(getPlatform('darwin', 'x64'), 'x86_64-apple-darwin');
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('linux x64 → x86_64-unknown-linux-gnu', () => {
|
|
76
|
-
assert.strictEqual(getPlatform('linux', 'x64'), 'x86_64-unknown-linux-gnu');
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test('win32 x64 → x86_64-pc-windows-msvc.exe', () => {
|
|
80
|
-
assert.strictEqual(getPlatform('win32', 'x64'), 'x86_64-pc-windows-msvc.exe');
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test('unknown platform throws', () => {
|
|
84
|
-
let threw = false;
|
|
85
|
-
try { getPlatform('freebsd', 'x64'); } catch (e) { threw = true; }
|
|
86
|
-
assert(threw, 'should throw on freebsd');
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// ── Asset name construction ─────────────────
|
|
90
|
-
|
|
91
|
-
console.log('\n[ getAssetName ]');
|
|
92
|
-
|
|
93
|
-
test('v0.1.0 → "0.1.0" slug (no leading v in asset name)', () => {
|
|
94
|
-
const name = getAssetName('x86_64-unknown-linux-gnu', 'v0.1.0');
|
|
95
|
-
assert.strictEqual(name, 'lain-0.1.0-x86_64-unknown-linux-gnu');
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
test('0.1.0 (no v) also works', () => {
|
|
99
|
-
const name = getAssetName('x86_64-apple-darwin', '0.1.0');
|
|
100
|
-
assert.strictEqual(name, 'lain-0.1.0-x86_64-apple-darwin');
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test('macOS ARM asset name is correct', () => {
|
|
104
|
-
const name = getAssetName('aarch64-apple-darwin', 'v0.1.0');
|
|
105
|
-
assert.strictEqual(name, 'lain-0.1.0-aarch64-apple-darwin');
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
test('Windows asset name ends in .exe', () => {
|
|
109
|
-
const name = getAssetName('x86_64-pc-windows-msvc.exe', 'v0.1.0');
|
|
110
|
-
assert.strictEqual(name, 'lain-0.1.0-x86_64-pc-windows-msvc.exe');
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
test('unknown platform in getAssetName throws', () => {
|
|
114
|
-
let threw = false;
|
|
115
|
-
try { getAssetName('freebsd-x64', 'v0.1.0'); } catch (e) { threw = true; }
|
|
116
|
-
assert(threw, 'should throw on unknown platform');
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
// ── URL construction ────────────────────────
|
|
120
|
-
|
|
121
|
-
console.log('\n[ buildUrl ]');
|
|
122
|
-
|
|
123
|
-
test('GitHub release URL is correctly formed', () => {
|
|
124
|
-
const url = buildUrl('x86_64-unknown-linux-gnu', 'v0.1.0');
|
|
125
|
-
assert.strictEqual(
|
|
126
|
-
url,
|
|
127
|
-
'https://github.com/spuentesp/lain/releases/download/v0.1.0/lain-0.1.0-x86_64-unknown-linux-gnu'
|
|
128
|
-
);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
test('custom repo works', () => {
|
|
132
|
-
const url = buildUrl('x86_64-apple-darwin', 'v0.1.0', 'myorg/mylain');
|
|
133
|
-
assert.strictEqual(
|
|
134
|
-
url,
|
|
135
|
-
'https://github.com/myorg/mylain/releases/download/v0.1.0/lain-0.1.0-x86_64-apple-darwin'
|
|
136
|
-
);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
test('different version in URL', () => {
|
|
140
|
-
const url = buildUrl('x86_64-apple-darwin', 'v1.2.3');
|
|
141
|
-
assert.ok(url.includes('/v1.2.3/'));
|
|
142
|
-
assert.ok(url.includes('lain-1.2.3-x86_64-apple-darwin'));
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
// ── Binary name ─────────────────────────────
|
|
146
|
-
|
|
147
|
-
console.log('\n[ getBinaryName ]');
|
|
148
|
-
|
|
149
|
-
test('win32 → lain.exe', () => {
|
|
150
|
-
assert.strictEqual(getBinaryName(true), 'lain.exe');
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
test('unix → lain (no extension)', () => {
|
|
154
|
-
assert.strictEqual(getBinaryName(false), 'lain');
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
// ── Symlink path construction ──────────────
|
|
158
|
-
|
|
159
|
-
console.log('\n[ createSymlink path ]');
|
|
160
|
-
|
|
161
|
-
test('node_modules/.bin path on unix', () => {
|
|
162
|
-
// Simulate: __dirname = /pkg/scripts → package root = /pkg
|
|
163
|
-
const __dirname = '/pkg/scripts';
|
|
164
|
-
const launcherName = 'lain-launcher';
|
|
165
|
-
const nodeBin = path.join(__dirname, '..', 'node_modules', '.bin', launcherName);
|
|
166
|
-
assert.strictEqual(nodeBin, '/pkg/node_modules/.bin/lain-launcher');
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
test('node_modules/.bin path on windows', () => {
|
|
170
|
-
// path.join normalizes separators for the current platform
|
|
171
|
-
// on darwin it uses /, on win32 it uses \
|
|
172
|
-
const nodeBin = path.join('C:\\pkg', 'scripts', '..', 'node_modules', '.bin', 'lain-launcher.cmd');
|
|
173
|
-
const expected = path.join('C:\\pkg', 'node_modules', '.bin', 'lain-launcher.cmd');
|
|
174
|
-
assert.strictEqual(nodeBin, expected);
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
// ── LAIN_DIR path ───────────────────────────
|
|
178
|
-
|
|
179
|
-
console.log('\n[ LAIN_DIR paths ]');
|
|
180
|
-
|
|
181
|
-
test('LAIN_DIR is ~/.lain', () => {
|
|
182
|
-
const lainDir = path.join(process.env.HOME, '.lain');
|
|
183
|
-
assert.strictEqual(lainDir, path.join(os.homedir(), '.lain'));
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
test('BIN_DIR is ~/.lain/bin', () => {
|
|
187
|
-
const lainDir = path.join(process.env.HOME, '.lain');
|
|
188
|
-
const binDir = path.join(lainDir, 'bin');
|
|
189
|
-
assert.strictEqual(binDir, path.join(os.homedir(), '.lain', 'bin'));
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
// ── CI guard logic ──────────────────────────
|
|
193
|
-
|
|
194
|
-
console.log('\n[ CI guard ]');
|
|
11
|
+
const tests = [];
|
|
12
|
+
function test(name, run) { tests.push({ name, run }); }
|
|
195
13
|
|
|
196
|
-
test('
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
assert.strictEqual(
|
|
14
|
+
test('maps only published targets', () => {
|
|
15
|
+
assert.strictEqual(runtime.targetFor('linux', 'x64'), 'x86_64-unknown-linux-gnu');
|
|
16
|
+
assert.strictEqual(runtime.targetFor('darwin', 'arm64'), 'aarch64-apple-darwin');
|
|
17
|
+
assert.strictEqual(runtime.targetFor('win32', 'x64'), 'x86_64-pc-windows-msvc');
|
|
18
|
+
assert.throws(() => runtime.targetFor('linux', 'arm64'), /Unsupported/);
|
|
19
|
+
assert.throws(() => runtime.targetFor('darwin', 'x64'), /Unsupported/);
|
|
200
20
|
});
|
|
201
21
|
|
|
202
|
-
test('
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
assert.strictEqual(shouldSkip, false);
|
|
22
|
+
test('LAIN_VERSION overrides package metadata and is validated', () => {
|
|
23
|
+
assert.strictEqual(runtime.selectedVersion({ LAIN_VERSION: 'v1.2.3-rc1' }), '1.2.3-rc1');
|
|
24
|
+
assert.throws(() => runtime.selectedVersion({ LAIN_VERSION: '../latest' }), /Invalid/);
|
|
206
25
|
});
|
|
207
26
|
|
|
208
|
-
test('
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
assert.strictEqual(shouldSkip, false);
|
|
27
|
+
test('cache paths include version and target', () => {
|
|
28
|
+
const value = runtime.binaryPath('1.2.3', 'x86_64-unknown-linux-gnu', { LAIN_CACHE_DIR: '/cache' }, 'linux');
|
|
29
|
+
assert.strictEqual(value, path.join('/cache', '1.2.3', 'x86_64-unknown-linux-gnu', 'lain'));
|
|
212
30
|
});
|
|
213
31
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
test('npm_config_ignore_scripts=true skips install', () => {
|
|
219
|
-
const env = { npm_config_ignore_scripts: 'true' };
|
|
220
|
-
const shouldSkip = env.npm_config_ignore_scripts === 'true';
|
|
221
|
-
assert.strictEqual(shouldSkip, true);
|
|
32
|
+
test('release asset contract omits the tag prefix', () => {
|
|
33
|
+
const name = runtime.assetName('v1.2.3', 'aarch64-apple-darwin');
|
|
34
|
+
assert.strictEqual(name, 'lain-1.2.3-aarch64-apple-darwin.tar.gz');
|
|
35
|
+
assert(runtime.releaseUrl('1.2.3', name).endsWith(`/v1.2.3/${name}`));
|
|
222
36
|
});
|
|
223
37
|
|
|
224
|
-
test('
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
assert.
|
|
38
|
+
test('checksum parser requires an exact asset match', () => {
|
|
39
|
+
const hash = 'a'.repeat(64);
|
|
40
|
+
assert.strictEqual(runtime.parseChecksum(`${hash} lain-1-linux.tar.gz\n`, 'lain-1-linux.tar.gz'), hash);
|
|
41
|
+
assert.throws(() => runtime.parseChecksum(`${hash} other.tar.gz\n`, 'lain-1-linux.tar.gz'), /no checksum/);
|
|
228
42
|
});
|
|
229
43
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
44
|
+
test('checksum mismatch fails closed', () => {
|
|
45
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lain-checksum-'));
|
|
46
|
+
try {
|
|
47
|
+
const file = path.join(directory, 'archive');
|
|
48
|
+
fs.writeFileSync(file, 'actual bytes');
|
|
49
|
+
assert.throws(() => runtime.verifyArchive(file, '0'.repeat(64)), /Checksum mismatch/);
|
|
50
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (process.platform !== 'win32') {
|
|
54
|
+
test('verified install is atomic and a second invocation is offline', async () => {
|
|
55
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lain-install-'));
|
|
56
|
+
const source = path.join(directory, 'source');
|
|
57
|
+
const cache = path.join(directory, 'cache');
|
|
58
|
+
fs.mkdirSync(source);
|
|
59
|
+
const executable = path.join(source, 'lain');
|
|
60
|
+
fs.writeFileSync(executable, '#!/bin/sh\nprintf "lain 1.2.3\\n"\n');
|
|
61
|
+
fs.chmodSync(executable, 0o755);
|
|
62
|
+
const asset = runtime.assetName('1.2.3', 'x86_64-unknown-linux-gnu');
|
|
63
|
+
const archive = path.join(source, asset);
|
|
64
|
+
execFileSync('tar', ['czf', archive, '-C', source, 'lain']);
|
|
65
|
+
const digest = crypto.createHash('sha256').update(fs.readFileSync(archive)).digest('hex');
|
|
66
|
+
fs.writeFileSync(path.join(source, 'SHA256SUMS'), `${digest} ${asset}\n`);
|
|
67
|
+
let downloads = 0;
|
|
68
|
+
const download = async (url, destination) => {
|
|
69
|
+
downloads += 1;
|
|
70
|
+
fs.copyFileSync(path.join(source, path.basename(url)), destination);
|
|
71
|
+
};
|
|
72
|
+
const options = { version: '1.2.3', platform: 'linux', arch: 'x64', env: { LAIN_CACHE_DIR: cache }, download };
|
|
73
|
+
try {
|
|
74
|
+
const installed = await runtime.ensureBinary(options);
|
|
75
|
+
assert.strictEqual(downloads, 2);
|
|
76
|
+
assert.strictEqual(fs.statSync(installed).mode & 0o777, 0o755);
|
|
77
|
+
assert(!fs.readdirSync(path.dirname(installed)).some((name) => name.startsWith('.install-')));
|
|
78
|
+
await runtime.ensureBinary({ ...options, download: async () => { throw new Error('network used'); } });
|
|
79
|
+
assert.strictEqual(downloads, 2);
|
|
80
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
|
|
81
|
+
});
|
|
262
82
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
assert.ok(url.startsWith('https://github.com/spuentesp/lain/releases/download/v0.1.0/lain-'));
|
|
83
|
+
test('downloaded binary version must match the selected release', () => {
|
|
84
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lain-version-'));
|
|
85
|
+
try {
|
|
86
|
+
const executable = path.join(directory, 'lain');
|
|
87
|
+
fs.writeFileSync(executable, '#!/bin/sh\nprintf "lain 9.9.9\\n"\n');
|
|
88
|
+
fs.chmodSync(executable, 0o755);
|
|
89
|
+
assert.throws(() => runtime.verifyBinary(executable, '1.2.3'), /expected/);
|
|
90
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
|
|
272
91
|
});
|
|
273
92
|
}
|
|
274
93
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
94
|
+
(async () => {
|
|
95
|
+
let failed = 0;
|
|
96
|
+
for (const { name, run } of tests) {
|
|
97
|
+
try { await run(); process.stdout.write(`ok - ${name}\n`); }
|
|
98
|
+
catch (error) { failed += 1; process.stderr.write(`not ok - ${name}: ${error.stack}\n`); }
|
|
99
|
+
}
|
|
100
|
+
if (failed) process.exitCode = 1;
|
|
101
|
+
})();
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const https = require('https');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const { execFileSync, spawnSync } = require('child_process');
|
|
9
|
+
|
|
10
|
+
const REPOSITORY = 'spuentesp/lain';
|
|
11
|
+
|
|
12
|
+
function targetFor(platform = process.platform, arch = process.arch) {
|
|
13
|
+
const targets = { 'darwin-arm64': 'aarch64-apple-darwin', 'linux-x64': 'x86_64-unknown-linux-gnu', 'win32-x64': 'x86_64-pc-windows-msvc' };
|
|
14
|
+
const target = targets[`${platform}-${arch}`];
|
|
15
|
+
if (!target) throw new Error(`Unsupported platform: ${platform}-${arch}`);
|
|
16
|
+
return target;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function normalizeVersion(value) {
|
|
20
|
+
const version = String(value || '').replace(/^v/, '');
|
|
21
|
+
if (!/^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$/.test(version)) throw new Error(`Invalid LAIN_VERSION: ${value}`);
|
|
22
|
+
return version;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function selectedVersion(env = process.env) {
|
|
26
|
+
return normalizeVersion(env.LAIN_VERSION || require('../package.json').version);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function cacheRoot(env = process.env, platform = process.platform) {
|
|
30
|
+
if (env.LAIN_CACHE_DIR) return path.resolve(env.LAIN_CACHE_DIR);
|
|
31
|
+
if (platform === 'win32') return path.join(env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local'), 'lain', 'Cache');
|
|
32
|
+
if (platform === 'darwin') return path.join(os.homedir(), 'Library', 'Caches', 'lain');
|
|
33
|
+
return path.join(env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache'), 'lain');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function assetName(version, target) { return `lain-${normalizeVersion(version)}-${target}.tar.gz`; }
|
|
37
|
+
|
|
38
|
+
function binaryPath(version, target, env = process.env, platform = process.platform) {
|
|
39
|
+
return path.join(cacheRoot(env, platform), normalizeVersion(version), target, platform === 'win32' ? 'lain.exe' : 'lain');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function releaseUrl(version, name) { return `https://github.com/${REPOSITORY}/releases/download/v${normalizeVersion(version)}/${name}`; }
|
|
43
|
+
|
|
44
|
+
function parseChecksum(text, expectedAsset) {
|
|
45
|
+
for (const line of text.split(/\r?\n/)) {
|
|
46
|
+
const match = line.trim().match(/^([a-fA-F0-9]{64})\s+\*?(.+)$/);
|
|
47
|
+
if (match && match[2] === expectedAsset) return match[1].toLowerCase();
|
|
48
|
+
}
|
|
49
|
+
throw new Error(`SHA256SUMS has no checksum for ${expectedAsset}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sha256(file) {
|
|
53
|
+
const hash = crypto.createHash('sha256');
|
|
54
|
+
hash.update(fs.readFileSync(file));
|
|
55
|
+
return hash.digest('hex');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function verifyArchive(file, expected) {
|
|
59
|
+
const actual = sha256(file);
|
|
60
|
+
if (actual !== expected.toLowerCase()) throw new Error(`Checksum mismatch for ${path.basename(file)}: expected ${expected}, got ${actual}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function verifyBinary(file, version) {
|
|
64
|
+
const result = spawnSync(file, ['--version'], { encoding: 'utf8', timeout: 10_000 });
|
|
65
|
+
if (result.error) throw new Error(`Cannot run downloaded binary: ${result.error.message}`);
|
|
66
|
+
if (result.status !== 0) throw new Error(`Downloaded binary exited ${result.status} during version check`);
|
|
67
|
+
const reported = String(result.stdout).trim();
|
|
68
|
+
if (reported !== `lain ${normalizeVersion(version)}`) throw new Error(`Downloaded binary reports ${JSON.stringify(reported)}, expected "lain ${normalizeVersion(version)}"`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function download(url, destination, redirects = 5) {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
const request = https.get(url, { headers: { 'User-Agent': '@spuentesp/lain-mcp' } }, (response) => {
|
|
74
|
+
if ([301, 302, 303, 307, 308].includes(response.statusCode) && response.headers.location) {
|
|
75
|
+
response.resume();
|
|
76
|
+
if (redirects === 0) return reject(new Error(`Too many redirects downloading ${url}`));
|
|
77
|
+
return download(response.headers.location, destination, redirects - 1).then(resolve, reject);
|
|
78
|
+
}
|
|
79
|
+
if (response.statusCode !== 200) {
|
|
80
|
+
response.resume();
|
|
81
|
+
return reject(new Error(`HTTP ${response.statusCode} downloading ${url}`));
|
|
82
|
+
}
|
|
83
|
+
const output = fs.createWriteStream(destination, { flags: 'wx' });
|
|
84
|
+
let received = 0;
|
|
85
|
+
const total = Number(response.headers['content-length']) || 0;
|
|
86
|
+
response.on('data', (chunk) => {
|
|
87
|
+
received += chunk.length;
|
|
88
|
+
if (process.stderr.isTTY && total) process.stderr.write(`\rDownloading ${path.basename(destination)} ${Math.floor(received * 100 / total)}%`);
|
|
89
|
+
});
|
|
90
|
+
response.pipe(output);
|
|
91
|
+
output.on('finish', () => output.close(() => {
|
|
92
|
+
if (process.stderr.isTTY && total) process.stderr.write('\n');
|
|
93
|
+
resolve();
|
|
94
|
+
}));
|
|
95
|
+
output.on('error', reject);
|
|
96
|
+
response.on('error', reject);
|
|
97
|
+
});
|
|
98
|
+
request.setTimeout(30_000, () => request.destroy(new Error(`Download timed out: ${url}`)));
|
|
99
|
+
request.on('error', reject);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function ensureBinary(options = {}) {
|
|
104
|
+
const env = options.env || process.env;
|
|
105
|
+
const platform = options.platform || process.platform;
|
|
106
|
+
const arch = options.arch || process.arch;
|
|
107
|
+
const version = normalizeVersion(options.version || selectedVersion(env));
|
|
108
|
+
const target = targetFor(platform, arch);
|
|
109
|
+
const finalBinary = binaryPath(version, target, env, platform);
|
|
110
|
+
if (fs.existsSync(finalBinary)) {
|
|
111
|
+
verifyBinary(finalBinary, version);
|
|
112
|
+
return finalBinary;
|
|
113
|
+
}
|
|
114
|
+
const directory = path.dirname(finalBinary);
|
|
115
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
116
|
+
const temporary = path.join(directory, `.install-${process.pid}-${crypto.randomBytes(6).toString('hex')}`);
|
|
117
|
+
fs.mkdirSync(temporary);
|
|
118
|
+
const asset = assetName(version, target);
|
|
119
|
+
const archive = path.join(temporary, asset);
|
|
120
|
+
const sums = path.join(temporary, 'SHA256SUMS');
|
|
121
|
+
try {
|
|
122
|
+
const fetch = options.download || download;
|
|
123
|
+
await fetch(releaseUrl(version, 'SHA256SUMS'), sums);
|
|
124
|
+
await fetch(releaseUrl(version, asset), archive);
|
|
125
|
+
verifyArchive(archive, parseChecksum(fs.readFileSync(sums, 'utf8'), asset));
|
|
126
|
+
execFileSync('tar', ['xzf', archive, '-C', temporary], { stdio: ['ignore', 'ignore', 'inherit'] });
|
|
127
|
+
const extracted = path.join(temporary, platform === 'win32' ? 'lain.exe' : 'lain');
|
|
128
|
+
if (!fs.existsSync(extracted)) throw new Error(`${asset} does not contain the Lain executable at its root`);
|
|
129
|
+
if (platform !== 'win32') fs.chmodSync(extracted, 0o755);
|
|
130
|
+
verifyBinary(extracted, version);
|
|
131
|
+
try {
|
|
132
|
+
fs.renameSync(extracted, finalBinary);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (!fs.existsSync(finalBinary)) throw error;
|
|
135
|
+
verifyBinary(finalBinary, version);
|
|
136
|
+
}
|
|
137
|
+
return finalBinary;
|
|
138
|
+
} finally {
|
|
139
|
+
fs.rmSync(temporary, { recursive: true, force: true });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
module.exports = { assetName, binaryPath, cacheRoot, ensureBinary, normalizeVersion, parseChecksum, releaseUrl, selectedVersion, sha256, targetFor, verifyArchive, verifyBinary };
|