@spuentesp/lain-mcp 0.6.1 → 0.8.0
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 +31 -13
- package/bin/lain.js +29 -24
- package/bin/lain.test.js +9 -142
- package/package.json +14 -4
- package/scripts/install.js +9 -247
- package/scripts/install.test.js +159 -264
- package/scripts/runtime.js +197 -0
package/README.md
CHANGED
|
@@ -1,28 +1,44 @@
|
|
|
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
|
-
-
|
|
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.
|
|
21
|
+
- A `lain --version` check that must match the npm package version.
|
|
22
|
+
|
|
23
|
+
The launcher downloads into a temporary directory and only moves verified
|
|
24
|
+
files into the versioned cache. It checks cached binaries again before running
|
|
25
|
+
them. A verified cache entry works offline.
|
|
26
|
+
|
|
27
|
+
## Published targets
|
|
28
|
+
|
|
29
|
+
| Host | Target | Current status |
|
|
30
|
+
|---|---|---|
|
|
31
|
+
| x86-64 Linux | `x86_64-unknown-linux-gnu` | Supported |
|
|
32
|
+
| Apple silicon | `aarch64-apple-darwin` | Supported |
|
|
33
|
+
| x86-64 Windows | `x86_64-pc-windows-msvc` | The repository contains the `DirectML.dll` packaging repair; `v0.7.4` predates it |
|
|
34
|
+
|
|
35
|
+
The release workflow doesn't currently publish x86-64 macOS or ARM64 Linux
|
|
36
|
+
archives. On those hosts, build Lain from source.
|
|
22
37
|
|
|
23
38
|
## Configuration
|
|
24
39
|
|
|
25
|
-
|
|
40
|
+
Set `LAIN_VERSION` to run a different published version. Set
|
|
41
|
+
`LAIN_CACHE_DIR` to override the operating system's user cache directory.
|
|
26
42
|
|
|
27
43
|
## Usage
|
|
28
44
|
|
|
@@ -33,7 +49,7 @@ After installation, add Lain to your MCP configuration:
|
|
|
33
49
|
"mcpServers": {
|
|
34
50
|
"lain": {
|
|
35
51
|
"command": "lain",
|
|
36
|
-
"args": ["
|
|
52
|
+
"args": ["mcp"]
|
|
37
53
|
}
|
|
38
54
|
}
|
|
39
55
|
}
|
|
@@ -42,13 +58,15 @@ After installation, add Lain to your MCP configuration:
|
|
|
42
58
|
Or run directly:
|
|
43
59
|
|
|
44
60
|
```bash
|
|
45
|
-
|
|
61
|
+
npx @spuentesp/lain-mcp mcp
|
|
46
62
|
```
|
|
47
63
|
|
|
48
64
|
## Uninstall
|
|
49
65
|
|
|
50
66
|
```bash
|
|
51
|
-
npm uninstall -g lain-mcp
|
|
52
|
-
# Binary and config remain in ~/.lain/ — delete manually to fully remove
|
|
53
|
-
rm -rf ~/.lain
|
|
67
|
+
npm uninstall -g @spuentesp/lain-mcp
|
|
54
68
|
```
|
|
69
|
+
|
|
70
|
+
The verified native binary remains in your user cache so a later install can
|
|
71
|
+
work offline. Remove the `lain` cache directory using your operating system's
|
|
72
|
+
cache-management tools if you also want to discard downloaded versions.
|
package/bin/lain.js
CHANGED
|
@@ -1,29 +1,34 @@
|
|
|
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
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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) {
|
|
14
|
+
process.kill(process.pid, signal);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const exitCode = code == null ? 1 : code;
|
|
18
|
+
if (exitCode < 0 || exitCode > 255) {
|
|
19
|
+
// Windows NTSTATUS values arrive as large integers (a missing
|
|
20
|
+
// runtime DLL gives 0xC0000135). Assigning one to
|
|
21
|
+
// process.exitCode throws a RangeError during Node's exit path,
|
|
22
|
+
// which kills the process silently with code 1 — report the real
|
|
23
|
+
// status and clamp instead.
|
|
24
|
+
const status = (exitCode >>> 0).toString(16);
|
|
25
|
+
process.stderr.write(`Lain exited with Windows status 0x${status}; 0xc0000135 means a runtime DLL (DirectML.dll) is missing next to lain.exe\n`);
|
|
26
|
+
process.exitCode = 1;
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
process.exitCode = exitCode;
|
|
30
|
+
});
|
|
31
|
+
}).catch((error) => {
|
|
32
|
+
process.stderr.write(`Failed to prepare Lain: ${error.message}\n`);
|
|
33
|
+
process.exitCode = 1;
|
|
29
34
|
});
|
package/bin/lain.test.js
CHANGED
|
@@ -1,147 +1,14 @@
|
|
|
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
|
-
|
|
15
|
-
|
|
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
|
+
assert(source.includes('Windows status'), 'launcher must surface Windows NTSTATUS exits instead of dying silently');
|
|
13
|
+
assert(source.includes('exitCode > 255') || source.includes('exitCode < 0 || exitCode > 255'), 'launcher must clamp out-of-range exit codes');
|
|
14
|
+
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.8.0",
|
|
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,252 +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 === 'linux' && arch === 'x64') return 'x86_64-unknown-linux-gnu';
|
|
32
|
-
if (platform === 'linux' && arch === 'arm64') return 'aarch64-unknown-linux-gnu';
|
|
33
|
-
if (platform === 'win32' && arch === 'x64') return 'x86_64-pc-windows-msvc';
|
|
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.tar.gz`,
|
|
41
|
-
'x86_64-unknown-linux-gnu': `lain-${versionSlug}-x86_64-unknown-linux-gnu.tar.gz`,
|
|
42
|
-
'aarch64-unknown-linux-gnu': `lain-${versionSlug}-aarch64-unknown-linux-gnu.tar.gz`,
|
|
43
|
-
'x86_64-pc-windows-msvc': `lain-${versionSlug}-x86_64-pc-windows-msvc.tar.gz`,
|
|
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 (-f: fail on HTTP errors instead of saving
|
|
58
|
-
// the 404 page as the binary), fallback to PowerShell on Windows
|
|
59
|
-
function download(url, dest) {
|
|
60
|
-
console.log(` Downloading ${path.basename(dest)}...`);
|
|
61
|
-
try {
|
|
62
|
-
execSync(`curl -fsSL "${url}" -o "${dest}" --progress-bar`, { stdio: 'inherit' });
|
|
63
|
-
} catch (e) {
|
|
64
|
-
if (process.platform === 'win32') {
|
|
65
|
-
console.log(' curl not found, using PowerShell...');
|
|
66
|
-
execSync(
|
|
67
|
-
`powershell -Command "Invoke-WebRequest -Uri \\"${url}\\" -OutFile \\"${dest}\\""`,
|
|
68
|
-
{ stdio: 'inherit' }
|
|
69
|
-
);
|
|
70
|
-
} else {
|
|
71
|
-
throw new Error(`curl not found and no fallback available for ${process.platform}`);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Create default tuning.toml
|
|
77
|
-
function createTuningConfig() {
|
|
78
|
-
const content = `# Lain tuning configuration — loaded from ~/.lain/tuning.toml
|
|
79
|
-
# All values are optional; omit a field to use the built-in default.
|
|
80
|
-
|
|
81
|
-
[semantic]
|
|
82
|
-
threshold = 0.1 # Semantic search cosine similarity floor
|
|
83
|
-
anchor_weight = 0.3 # Weight for anchor_score in hybrid ranking
|
|
84
|
-
|
|
85
|
-
[ingestion]
|
|
86
|
-
max_pattern_edges = 200
|
|
87
|
-
lsp_pool_size = 4
|
|
88
|
-
files_per_batch = 50
|
|
89
|
-
max_files_per_scan = 5000
|
|
90
|
-
cochange_commit_window = 100
|
|
91
|
-
cochange_min_pair_count = 2
|
|
92
|
-
cochange_max_commit_files = 100
|
|
93
|
-
|
|
94
|
-
[execution]
|
|
95
|
-
default_command_timeout_secs = 60
|
|
96
|
-
default_test_timeout_secs = 300
|
|
97
|
-
lsp_symbol_poll_timeout_secs = 2
|
|
98
|
-
lsp_symbol_poll_interval_ms = 50
|
|
99
|
-
`;
|
|
100
|
-
fs.writeFileSync(TUNING_FILE, content);
|
|
101
|
-
console.log(` Created ${TUNING_FILE}`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Create toolchains/ README
|
|
105
|
-
function createToolchainsDir() {
|
|
106
|
-
ensureDir(TOOLCHAINS_DIR);
|
|
107
|
-
const readme = `# User toolchain overrides
|
|
108
|
-
# Drop .toml files here to override built-in toolchain profiles.
|
|
109
|
-
# See the built-in profiles in the Lain source at toolchains/
|
|
110
|
-
`;
|
|
111
|
-
fs.writeFileSync(path.join(TOOLCHAINS_DIR, 'README.md'), readme);
|
|
112
|
-
console.log(` Created ${TOOLCHAINS_DIR}/`);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Create models/ README
|
|
116
|
-
function createModelsDir() {
|
|
117
|
-
ensureDir(MODELS_DIR);
|
|
118
|
-
const readme = `# ONNX embedding models
|
|
119
|
-
# Place .onnx model files here for semantic search.
|
|
120
|
-
# Required files:
|
|
121
|
-
# model.onnx — the ONNX model
|
|
122
|
-
# tokenizer.json — the tokenizer
|
|
123
|
-
#
|
|
124
|
-
# Download the default model:
|
|
125
|
-
# ./scripts/install.sh --fast # if using Lain's install script
|
|
126
|
-
# or from https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2
|
|
127
|
-
`;
|
|
128
|
-
fs.writeFileSync(path.join(MODELS_DIR, 'README.md'), readme);
|
|
129
|
-
console.log(` Created ${MODELS_DIR}/`);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Create the launcher script (shell for unix, batch for windows)
|
|
133
|
-
function createLauncher(binaryPath) {
|
|
134
|
-
const isWin = process.platform === 'win32';
|
|
135
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
136
|
-
const launcherPath = path.join(BIN_DIR, launcherName);
|
|
137
|
-
const content = isWin
|
|
138
|
-
? `@echo off\n"${binaryPath}" %*`
|
|
139
|
-
: `#!/bin/sh\nexec "${binaryPath}" "$@"\n`;
|
|
140
|
-
fs.writeFileSync(launcherPath, content);
|
|
141
|
-
if (!isWin) fs.chmodSync(launcherPath, 0o755);
|
|
142
|
-
console.log(` Created ${launcherPath}`);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// Create symlink in node_modules/.bin for npm-installed binaries
|
|
146
|
-
function createSymlink() {
|
|
147
|
-
const isWin = process.platform === 'win32';
|
|
148
|
-
const launcherName = isWin ? 'lain-launcher.cmd' : 'lain-launcher';
|
|
149
|
-
// node_modules/.bin is inside the package, not a sibling of node_modules
|
|
150
|
-
const nodeBin = path.join(__dirname, '..', 'node_modules', '.bin', launcherName);
|
|
151
|
-
try {
|
|
152
|
-
const binDir = path.dirname(nodeBin);
|
|
153
|
-
if (!fs.existsSync(binDir)) {
|
|
154
|
-
fs.mkdirSync(binDir, { recursive: true });
|
|
155
|
-
}
|
|
156
|
-
if (isWin) {
|
|
157
|
-
// nodeBin already ends in .cmd via launcherName, don't append again
|
|
158
|
-
fs.writeFileSync(nodeBin, `@echo off\n"${path.join(BIN_DIR, launcherName)}" %*`);
|
|
159
|
-
} else {
|
|
160
|
-
if (fs.existsSync(nodeBin)) fs.unlinkSync(nodeBin);
|
|
161
|
-
fs.symlinkSync(path.join(BIN_DIR, launcherName), nodeBin);
|
|
162
|
-
}
|
|
163
|
-
console.log(` Linked to node_modules/.bin/${launcherName}`);
|
|
164
|
-
} catch (e) {
|
|
165
|
-
if (e.code !== 'ENOENT' && e.code !== 'EEXIST') {
|
|
166
|
-
console.warn(` Warning: could not create .bin symlink: ${e.message}`);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
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
|
+
});
|
|
169
14
|
}
|
|
170
|
-
|
|
171
|
-
// Main install
|
|
172
|
-
async function install() {
|
|
173
|
-
console.log('\n=== Lain installer ===\n');
|
|
174
|
-
|
|
175
|
-
// Ensure directories
|
|
176
|
-
ensureDir(BIN_DIR);
|
|
177
|
-
ensureDir(LAIN_DIR);
|
|
178
|
-
|
|
179
|
-
const platform = getPlatform();
|
|
180
|
-
const version = `v${require('../package.json').version}`;
|
|
181
|
-
const assetName = getAssetName(platform, version);
|
|
182
|
-
// Assets are tarballs containing the binary (`lain` / `lain.exe`).
|
|
183
|
-
const binaryName = process.platform === 'win32' ? 'lain.exe' : 'lain';
|
|
184
|
-
const binaryPath = path.join(BIN_DIR, binaryName);
|
|
185
|
-
const archivePath = path.join(BIN_DIR, assetName);
|
|
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 + extract 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, archivePath);
|
|
198
|
-
execSync(`tar xzf "${archivePath}" -C "${BIN_DIR}"`, { stdio: 'inherit' });
|
|
199
|
-
fs.unlinkSync(archivePath);
|
|
200
|
-
if (!fs.existsSync(binaryPath)) {
|
|
201
|
-
throw new Error(`Archive ${assetName} did not contain ${binaryName}`);
|
|
202
|
-
}
|
|
203
|
-
if (!process.platform.startsWith('win')) {
|
|
204
|
-
fs.chmodSync(binaryPath, 0o755);
|
|
205
|
-
}
|
|
206
|
-
console.log(` Installed ${binaryPath}`);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// Create config directories
|
|
210
|
-
if (!fs.existsSync(TUNING_FILE)) {
|
|
211
|
-
createTuningConfig();
|
|
212
|
-
} else {
|
|
213
|
-
console.log(` tuning.toml already exists — skipping`);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
if (!fs.existsSync(TOOLCHAINS_DIR)) {
|
|
217
|
-
createToolchainsDir();
|
|
218
|
-
} else {
|
|
219
|
-
console.log(` toolchains/ already exists — skipping`);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (!fs.existsSync(MODELS_DIR)) {
|
|
223
|
-
createModelsDir();
|
|
224
|
-
} else {
|
|
225
|
-
console.log(` models/ already exists — skipping`);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Create launcher
|
|
229
|
-
createLauncher(binaryPath);
|
|
230
|
-
|
|
231
|
-
// Symlink for npm
|
|
232
|
-
createSymlink();
|
|
233
|
-
|
|
234
|
-
// Post-install: nothing more to do. The user configures their
|
|
235
|
-
// agent's MCP wiring themselves, pointing it at the zero-config
|
|
236
|
-
// `lain mcp` entry (walks up for .git, no repos.yaml needed).
|
|
237
|
-
|
|
238
|
-
console.log('\n=== Installation complete ===\n');
|
|
239
|
-
console.log(' The zero-config MCP entry point (run from any git repo):');
|
|
240
|
-
console.log(` ${binaryPath} mcp`);
|
|
241
|
-
console.log('\n Add it to your agent\'s MCP config as:');
|
|
242
|
-
console.log(' { "mcpServers": { "lain": { "command": "lain", "args": ["mcp"] } } }');
|
|
243
|
-
console.log('\n See `lain --help` for the full subcommand surface');
|
|
244
|
-
console.log(' (server, mcp, workspaces, repos, query, hooks, doctor).');
|
|
245
|
-
console.log('\n Or add lain to your PATH:');
|
|
246
|
-
console.log(` export PATH="$HOME/.lain/bin:$PATH"\n`);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
install().catch((err) => {
|
|
250
|
-
console.error('Installation failed:', err.message);
|
|
251
|
-
process.exit(1);
|
|
252
|
-
});
|
package/scripts/install.test.js
CHANGED
|
@@ -1,286 +1,181 @@
|
|
|
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 === 'linux' && arch === 'x64') return 'x86_64-unknown-linux-gnu';
|
|
17
|
-
if (platform === 'linux' && arch === 'arm64') return 'aarch64-unknown-linux-gnu';
|
|
18
|
-
if (platform === 'win32' && arch === 'x64') return 'x86_64-pc-windows-msvc';
|
|
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.tar.gz`,
|
|
26
|
-
'x86_64-unknown-linux-gnu': `lain-${versionSlug}-x86_64-unknown-linux-gnu.tar.gz`,
|
|
27
|
-
'aarch64-unknown-linux-gnu': `lain-${versionSlug}-aarch64-unknown-linux-gnu.tar.gz`,
|
|
28
|
-
'x86_64-pc-windows-msvc': `lain-${versionSlug}-x86_64-pc-windows-msvc.tar.gz`,
|
|
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 throws (no published asset)', () => {
|
|
72
|
-
let threw = false;
|
|
73
|
-
try { getPlatform('darwin', 'x64'); } catch (e) { threw = true; }
|
|
74
|
-
assert(threw, 'should throw on darwin x64');
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test('linux x64 → x86_64-unknown-linux-gnu', () => {
|
|
78
|
-
assert.strictEqual(getPlatform('linux', 'x64'), 'x86_64-unknown-linux-gnu');
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('linux arm64 → aarch64-unknown-linux-gnu', () => {
|
|
82
|
-
assert.strictEqual(getPlatform('linux', 'arm64'), 'aarch64-unknown-linux-gnu');
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
test('win32 x64 → x86_64-pc-windows-msvc', () => {
|
|
86
|
-
assert.strictEqual(getPlatform('win32', 'x64'), 'x86_64-pc-windows-msvc');
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
test('unknown platform throws', () => {
|
|
90
|
-
let threw = false;
|
|
91
|
-
try { getPlatform('freebsd', 'x64'); } catch (e) { threw = true; }
|
|
92
|
-
assert(threw, 'should throw on freebsd');
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// ── Asset name construction ─────────────────
|
|
96
|
-
|
|
97
|
-
console.log('\n[ getAssetName ]');
|
|
98
|
-
|
|
99
|
-
test('v0.1.0 → "0.1.0" slug (no leading v in asset name)', () => {
|
|
100
|
-
const name = getAssetName('x86_64-unknown-linux-gnu', 'v0.1.0');
|
|
101
|
-
assert.strictEqual(name, 'lain-0.1.0-x86_64-unknown-linux-gnu.tar.gz');
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
test('0.1.0 (no v) also works', () => {
|
|
105
|
-
const name = getAssetName('aarch64-unknown-linux-gnu', '0.1.0');
|
|
106
|
-
assert.strictEqual(name, 'lain-0.1.0-aarch64-unknown-linux-gnu.tar.gz');
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
test('macOS ARM asset name is correct', () => {
|
|
110
|
-
const name = getAssetName('aarch64-apple-darwin', 'v0.1.0');
|
|
111
|
-
assert.strictEqual(name, 'lain-0.1.0-aarch64-apple-darwin.tar.gz');
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test('Windows asset name ends in .tar.gz', () => {
|
|
115
|
-
const name = getAssetName('x86_64-pc-windows-msvc', 'v0.1.0');
|
|
116
|
-
assert.strictEqual(name, 'lain-0.1.0-x86_64-pc-windows-msvc.tar.gz');
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('unknown platform in getAssetName throws', () => {
|
|
120
|
-
let threw = false;
|
|
121
|
-
try { getAssetName('freebsd-x64', 'v0.1.0'); } catch (e) { threw = true; }
|
|
122
|
-
assert(threw, 'should throw on unknown platform');
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
// ── URL construction ────────────────────────
|
|
126
|
-
|
|
127
|
-
console.log('\n[ buildUrl ]');
|
|
128
|
-
|
|
129
|
-
test('GitHub release URL is correctly formed', () => {
|
|
130
|
-
const url = buildUrl('x86_64-unknown-linux-gnu', 'v0.1.0');
|
|
131
|
-
assert.strictEqual(
|
|
132
|
-
url,
|
|
133
|
-
'https://github.com/spuentesp/lain/releases/download/v0.1.0/lain-0.1.0-x86_64-unknown-linux-gnu.tar.gz'
|
|
134
|
-
);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test('custom repo works', () => {
|
|
138
|
-
const url = buildUrl('aarch64-apple-darwin', 'v0.1.0', 'myorg/mylain');
|
|
139
|
-
assert.strictEqual(
|
|
140
|
-
url,
|
|
141
|
-
'https://github.com/myorg/mylain/releases/download/v0.1.0/lain-0.1.0-aarch64-apple-darwin.tar.gz'
|
|
142
|
-
);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
test('different version in URL', () => {
|
|
146
|
-
const url = buildUrl('aarch64-apple-darwin', 'v1.2.3');
|
|
147
|
-
assert.ok(url.includes('/v1.2.3/'));
|
|
148
|
-
assert.ok(url.includes('lain-1.2.3-aarch64-apple-darwin'));
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
// ── Binary name ─────────────────────────────
|
|
152
|
-
|
|
153
|
-
console.log('\n[ getBinaryName ]');
|
|
154
|
-
|
|
155
|
-
test('win32 → lain.exe', () => {
|
|
156
|
-
assert.strictEqual(getBinaryName(true), 'lain.exe');
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
test('unix → lain (no extension)', () => {
|
|
160
|
-
assert.strictEqual(getBinaryName(false), 'lain');
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
// ── Symlink path construction ──────────────
|
|
164
|
-
|
|
165
|
-
console.log('\n[ createSymlink path ]');
|
|
166
|
-
|
|
167
|
-
test('node_modules/.bin path on unix', () => {
|
|
168
|
-
// Simulate: __dirname = /pkg/scripts → package root = /pkg
|
|
169
|
-
const __dirname = '/pkg/scripts';
|
|
170
|
-
const launcherName = 'lain-launcher';
|
|
171
|
-
const nodeBin = path.join(__dirname, '..', 'node_modules', '.bin', launcherName);
|
|
172
|
-
assert.strictEqual(nodeBin, '/pkg/node_modules/.bin/lain-launcher');
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test('node_modules/.bin path on windows', () => {
|
|
176
|
-
// path.join normalizes separators for the current platform
|
|
177
|
-
// on darwin it uses /, on win32 it uses \
|
|
178
|
-
const nodeBin = path.join('C:\\pkg', 'scripts', '..', 'node_modules', '.bin', 'lain-launcher.cmd');
|
|
179
|
-
const expected = path.join('C:\\pkg', 'node_modules', '.bin', 'lain-launcher.cmd');
|
|
180
|
-
assert.strictEqual(nodeBin, expected);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
// ── LAIN_DIR path ───────────────────────────
|
|
184
|
-
|
|
185
|
-
console.log('\n[ LAIN_DIR paths ]');
|
|
186
|
-
|
|
187
|
-
test('LAIN_DIR is ~/.lain', () => {
|
|
188
|
-
const lainDir = path.join(process.env.HOME, '.lain');
|
|
189
|
-
assert.strictEqual(lainDir, path.join(os.homedir(), '.lain'));
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
test('BIN_DIR is ~/.lain/bin', () => {
|
|
193
|
-
const lainDir = path.join(process.env.HOME, '.lain');
|
|
194
|
-
const binDir = path.join(lainDir, 'bin');
|
|
195
|
-
assert.strictEqual(binDir, path.join(os.homedir(), '.lain', 'bin'));
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// ── CI guard logic ──────────────────────────
|
|
199
|
-
|
|
200
|
-
console.log('\n[ CI guard ]');
|
|
201
|
-
|
|
202
|
-
test('CI=true skips install', () => {
|
|
203
|
-
const env = { CI: 'true' };
|
|
204
|
-
const shouldSkip = env.CI && !env.LAIN_FORCE_INSTALL;
|
|
205
|
-
assert.strictEqual(shouldSkip, true);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
test('CI=true + LAIN_FORCE_INSTALL=1 does NOT skip', () => {
|
|
209
|
-
const env = { CI: 'true', LAIN_FORCE_INSTALL: '1' };
|
|
210
|
-
const shouldSkip = env.CI && !env.LAIN_FORCE_INSTALL;
|
|
211
|
-
assert.strictEqual(shouldSkip, false);
|
|
212
|
-
});
|
|
11
|
+
const tests = [];
|
|
12
|
+
function test(name, run) { tests.push({ name, run }); }
|
|
213
13
|
|
|
214
|
-
test('
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
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/);
|
|
218
20
|
});
|
|
219
21
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
test('npm_config_ignore_scripts=true skips install', () => {
|
|
225
|
-
const env = { npm_config_ignore_scripts: 'true' };
|
|
226
|
-
const shouldSkip = env.npm_config_ignore_scripts === 'true';
|
|
227
|
-
assert.strictEqual(shouldSkip, true);
|
|
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/);
|
|
228
25
|
});
|
|
229
26
|
|
|
230
|
-
test('
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
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'));
|
|
234
30
|
});
|
|
235
31
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
test('Unix launcher uses exec with quoted binary path', () => {
|
|
241
|
-
const isWin = false;
|
|
242
|
-
const binaryPath = '/Users/user/.lain/bin/lain';
|
|
243
|
-
const content = isWin
|
|
244
|
-
? `@echo off\n"${binaryPath}" %*`
|
|
245
|
-
: `#!/bin/sh\nexec "${binaryPath}" "$@"\n`;
|
|
246
|
-
assert.strictEqual(content, '#!/bin/sh\nexec "/Users/user/.lain/bin/lain" "$@"\n');
|
|
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}`));
|
|
247
36
|
});
|
|
248
37
|
|
|
249
|
-
test('
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
? `@echo off\n"${binaryPath}" %*`
|
|
254
|
-
: `#!/bin/sh\nexec "${binaryPath}" "$@"\n`;
|
|
255
|
-
assert.strictEqual(content, '@echo off\n"C:\\Users\\user\\.lain\\bin\\lain.exe" %*');
|
|
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/);
|
|
256
42
|
});
|
|
257
43
|
|
|
258
|
-
|
|
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
|
+
test('Windows runtime DLLs are installed beside the executable', () => {
|
|
54
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lain-windows-dlls-'));
|
|
55
|
+
const source = path.join(directory, 'source');
|
|
56
|
+
const destination = path.join(directory, 'cache');
|
|
57
|
+
fs.mkdirSync(source);
|
|
58
|
+
fs.mkdirSync(destination);
|
|
59
|
+
fs.writeFileSync(path.join(source, 'DirectML.dll'), 'authenticated archive bytes');
|
|
60
|
+
fs.writeFileSync(path.join(source, 'not-a-runtime.txt'), 'leave me behind');
|
|
61
|
+
try {
|
|
62
|
+
const installed = runtime.installWindowsRuntimeDlls(source, destination, 'win32');
|
|
63
|
+
assert.deepStrictEqual(installed, ['DirectML.dll']);
|
|
64
|
+
assert(fs.existsSync(path.join(destination, 'DirectML.dll')));
|
|
65
|
+
assert(fs.existsSync(path.join(source, 'not-a-runtime.txt')));
|
|
66
|
+
runtime.verifyWindowsRuntimeDlls(destination, 'win32');
|
|
67
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('Windows install fails clearly when DirectML.dll is absent', () => {
|
|
71
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lain-windows-dlls-missing-'));
|
|
72
|
+
const source = path.join(directory, 'source');
|
|
73
|
+
const destination = path.join(directory, 'cache');
|
|
74
|
+
fs.mkdirSync(source);
|
|
75
|
+
fs.mkdirSync(destination);
|
|
76
|
+
try {
|
|
77
|
+
assert.throws(
|
|
78
|
+
() => runtime.installWindowsRuntimeDlls(source, destination, 'win32'),
|
|
79
|
+
/missing DirectML\.dll/
|
|
80
|
+
);
|
|
81
|
+
assert.throws(
|
|
82
|
+
() => runtime.verifyWindowsRuntimeDlls(destination, 'win32'),
|
|
83
|
+
/missing DirectML\.dll/
|
|
84
|
+
);
|
|
85
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
if (process.platform !== 'win32') {
|
|
89
|
+
test('verified install is atomic and a second invocation is offline', async () => {
|
|
90
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lain-install-'));
|
|
91
|
+
const source = path.join(directory, 'source');
|
|
92
|
+
const cache = path.join(directory, 'cache');
|
|
93
|
+
fs.mkdirSync(source);
|
|
94
|
+
const executable = path.join(source, 'lain');
|
|
95
|
+
fs.writeFileSync(executable, '#!/bin/sh\nprintf "lain 1.2.3\\n"\n');
|
|
96
|
+
fs.chmodSync(executable, 0o755);
|
|
97
|
+
const asset = runtime.assetName('1.2.3', 'x86_64-unknown-linux-gnu');
|
|
98
|
+
const archive = path.join(source, asset);
|
|
99
|
+
execFileSync('tar', ['czf', archive, '-C', source, 'lain']);
|
|
100
|
+
const digest = crypto.createHash('sha256').update(fs.readFileSync(archive)).digest('hex');
|
|
101
|
+
fs.writeFileSync(path.join(source, 'SHA256SUMS'), `${digest} ${asset}\n`);
|
|
102
|
+
let downloads = 0;
|
|
103
|
+
const download = async (url, destination) => {
|
|
104
|
+
downloads += 1;
|
|
105
|
+
fs.copyFileSync(path.join(source, path.basename(url)), destination);
|
|
106
|
+
};
|
|
107
|
+
const options = { version: '1.2.3', platform: 'linux', arch: 'x64', env: { LAIN_CACHE_DIR: cache }, download };
|
|
108
|
+
try {
|
|
109
|
+
const installed = await runtime.ensureBinary(options);
|
|
110
|
+
assert.strictEqual(downloads, 2);
|
|
111
|
+
assert.strictEqual(fs.statSync(installed).mode & 0o777, 0o755);
|
|
112
|
+
assert(!fs.readdirSync(path.dirname(installed)).some((name) => name.startsWith('.install-')));
|
|
113
|
+
await runtime.ensureBinary({ ...options, download: async () => { throw new Error('network used'); } });
|
|
114
|
+
assert.strictEqual(downloads, 2);
|
|
115
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
|
|
116
|
+
});
|
|
259
117
|
|
|
260
|
-
|
|
118
|
+
test('downloaded binary version must match the selected release', () => {
|
|
119
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lain-version-'));
|
|
120
|
+
try {
|
|
121
|
+
const executable = path.join(directory, 'lain');
|
|
122
|
+
fs.writeFileSync(executable, '#!/bin/sh\nprintf "lain 9.9.9\\n"\n');
|
|
123
|
+
fs.chmodSync(executable, 0o755);
|
|
124
|
+
assert.throws(() => runtime.verifyBinary(executable, '1.2.3'), /expected/);
|
|
125
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
|
|
126
|
+
});
|
|
261
127
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
128
|
+
test('verified install unpacks sidecar binary if present', async () => {
|
|
129
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lain-install-sidecar-'));
|
|
130
|
+
const source = path.join(directory, 'source');
|
|
131
|
+
const cache = path.join(directory, 'cache');
|
|
132
|
+
fs.mkdirSync(source);
|
|
133
|
+
const executable = path.join(source, 'lain');
|
|
134
|
+
fs.writeFileSync(executable, '#!/bin/sh\nprintf "lain 1.2.3\\n"\n');
|
|
135
|
+
fs.chmodSync(executable, 0o755);
|
|
136
|
+
const sidecar = path.join(source, 'lain-git-sidecar');
|
|
137
|
+
fs.writeFileSync(sidecar, '#!/bin/sh\nprintf "lain-git-sidecar 1.2.3\\n"\n');
|
|
138
|
+
fs.chmodSync(sidecar, 0o755);
|
|
139
|
+
const asset = runtime.assetName('1.2.3', 'x86_64-unknown-linux-gnu');
|
|
140
|
+
const archive = path.join(source, asset);
|
|
141
|
+
execFileSync('tar', ['czf', archive, '-C', source, 'lain', 'lain-git-sidecar']);
|
|
142
|
+
const digest = crypto.createHash('sha256').update(fs.readFileSync(archive)).digest('hex');
|
|
143
|
+
fs.writeFileSync(path.join(source, 'SHA256SUMS'), `${digest} ${asset}\n`);
|
|
144
|
+
let downloads = 0;
|
|
145
|
+
const download = async (url, destination) => {
|
|
146
|
+
downloads += 1;
|
|
147
|
+
fs.copyFileSync(path.join(source, path.basename(url)), destination);
|
|
148
|
+
};
|
|
149
|
+
const options = { version: '1.2.3', platform: 'linux', arch: 'x64', env: { LAIN_CACHE_DIR: cache }, download };
|
|
150
|
+
try {
|
|
151
|
+
const installed = await runtime.ensureBinary(options);
|
|
152
|
+
assert.strictEqual(downloads, 2);
|
|
153
|
+
assert.strictEqual(fs.statSync(installed).mode & 0o777, 0o755);
|
|
154
|
+
const installedSidecar = path.join(path.dirname(installed), 'lain-git-sidecar');
|
|
155
|
+
assert(fs.existsSync(installedSidecar), 'sidecar binary should exist alongside main binary');
|
|
156
|
+
assert.strictEqual(fs.statSync(installedSidecar).mode & 0o777, 0o755);
|
|
157
|
+
assert(!fs.readdirSync(path.dirname(installed)).some((name) => name.startsWith('.install-')));
|
|
158
|
+
await runtime.ensureBinary({ ...options, download: async () => { throw new Error('network used'); } });
|
|
159
|
+
assert.strictEqual(downloads, 2);
|
|
160
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
|
|
161
|
+
});
|
|
268
162
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
assert.ok(url.startsWith('https://github.com/spuentesp/lain/releases/download/v0.1.0/lain-'));
|
|
163
|
+
test('downloaded sidecar binary version must match the selected release', () => {
|
|
164
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'lain-sidecar-version-'));
|
|
165
|
+
try {
|
|
166
|
+
const executable = path.join(directory, 'lain-git-sidecar');
|
|
167
|
+
fs.writeFileSync(executable, '#!/bin/sh\nprintf "lain-git-sidecar 9.9.9\\n"\n');
|
|
168
|
+
fs.chmodSync(executable, 0o755);
|
|
169
|
+
assert.throws(() => runtime.verifySidecarBinary(executable, '1.2.3'), /expected/);
|
|
170
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
|
|
278
171
|
});
|
|
279
172
|
}
|
|
280
173
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
174
|
+
(async () => {
|
|
175
|
+
let failed = 0;
|
|
176
|
+
for (const { name, run } of tests) {
|
|
177
|
+
try { await run(); process.stdout.write(`ok - ${name}\n`); }
|
|
178
|
+
catch (error) { failed += 1; process.stderr.write(`not ok - ${name}: ${error.stack}\n`); }
|
|
179
|
+
}
|
|
180
|
+
if (failed) process.exitCode = 1;
|
|
181
|
+
})();
|
|
@@ -0,0 +1,197 @@
|
|
|
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 verifySidecarBinary(file, version) {
|
|
72
|
+
const result = spawnSync(file, ['--version'], { encoding: 'utf8', timeout: 10_000 });
|
|
73
|
+
if (result.error) throw new Error(`Cannot run downloaded sidecar: ${result.error.message}`);
|
|
74
|
+
if (result.status !== 0) throw new Error(`Downloaded sidecar exited ${result.status} during version check`);
|
|
75
|
+
const reported = String(result.stdout).trim();
|
|
76
|
+
if (reported !== `lain-git-sidecar ${normalizeVersion(version)}`) throw new Error(`Downloaded sidecar reports ${JSON.stringify(reported)}, expected "lain-git-sidecar ${normalizeVersion(version)}"`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function installWindowsRuntimeDlls(source, destination, platform = process.platform) {
|
|
80
|
+
if (platform !== 'win32') return [];
|
|
81
|
+
const dlls = fs.readdirSync(source).filter((name) => name.toLowerCase().endsWith('.dll'));
|
|
82
|
+
if (!dlls.some((name) => name.toLowerCase() === 'directml.dll')) {
|
|
83
|
+
throw new Error('Windows release archive is missing DirectML.dll required by the Lain executable');
|
|
84
|
+
}
|
|
85
|
+
for (const name of dlls) {
|
|
86
|
+
const from = path.join(source, name);
|
|
87
|
+
const to = path.join(destination, name);
|
|
88
|
+
try {
|
|
89
|
+
fs.renameSync(from, to);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
// Another concurrent npx invocation may have installed the same
|
|
92
|
+
// version first. The aggregate archive checksum authenticates the
|
|
93
|
+
// DLL bytes, and versioned cache directories keep releases apart.
|
|
94
|
+
if (!fs.existsSync(to)) throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return dlls;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function verifyWindowsRuntimeDlls(directory, platform = process.platform) {
|
|
101
|
+
if (platform !== 'win32') return;
|
|
102
|
+
if (!fs.existsSync(path.join(directory, 'DirectML.dll'))) {
|
|
103
|
+
throw new Error('Cached Windows installation is missing DirectML.dll; remove this version from LAIN_CACHE_DIR and retry');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function download(url, destination, redirects = 5) {
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
const request = https.get(url, { headers: { 'User-Agent': '@spuentesp/lain-mcp' } }, (response) => {
|
|
110
|
+
if ([301, 302, 303, 307, 308].includes(response.statusCode) && response.headers.location) {
|
|
111
|
+
response.resume();
|
|
112
|
+
if (redirects === 0) return reject(new Error(`Too many redirects downloading ${url}`));
|
|
113
|
+
return download(response.headers.location, destination, redirects - 1).then(resolve, reject);
|
|
114
|
+
}
|
|
115
|
+
if (response.statusCode !== 200) {
|
|
116
|
+
response.resume();
|
|
117
|
+
return reject(new Error(`HTTP ${response.statusCode} downloading ${url}`));
|
|
118
|
+
}
|
|
119
|
+
const output = fs.createWriteStream(destination, { flags: 'wx' });
|
|
120
|
+
let received = 0;
|
|
121
|
+
const total = Number(response.headers['content-length']) || 0;
|
|
122
|
+
response.on('data', (chunk) => {
|
|
123
|
+
received += chunk.length;
|
|
124
|
+
if (process.stderr.isTTY && total) process.stderr.write(`\rDownloading ${path.basename(destination)} ${Math.floor(received * 100 / total)}%`);
|
|
125
|
+
});
|
|
126
|
+
response.pipe(output);
|
|
127
|
+
output.on('finish', () => output.close(() => {
|
|
128
|
+
if (process.stderr.isTTY && total) process.stderr.write('\n');
|
|
129
|
+
resolve();
|
|
130
|
+
}));
|
|
131
|
+
output.on('error', reject);
|
|
132
|
+
response.on('error', reject);
|
|
133
|
+
});
|
|
134
|
+
request.setTimeout(30_000, () => request.destroy(new Error(`Download timed out: ${url}`)));
|
|
135
|
+
request.on('error', reject);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function ensureBinary(options = {}) {
|
|
140
|
+
const env = options.env || process.env;
|
|
141
|
+
const platform = options.platform || process.platform;
|
|
142
|
+
const arch = options.arch || process.arch;
|
|
143
|
+
const version = normalizeVersion(options.version || selectedVersion(env));
|
|
144
|
+
const target = targetFor(platform, arch);
|
|
145
|
+
const finalBinary = binaryPath(version, target, env, platform);
|
|
146
|
+
const directory = path.dirname(finalBinary);
|
|
147
|
+
const sidecarName = platform === 'win32' ? 'lain-git-sidecar.exe' : 'lain-git-sidecar';
|
|
148
|
+
const finalSidecar = path.join(directory, sidecarName);
|
|
149
|
+
if (fs.existsSync(finalBinary)) {
|
|
150
|
+
verifyWindowsRuntimeDlls(directory, platform);
|
|
151
|
+
verifyBinary(finalBinary, version);
|
|
152
|
+
if (fs.existsSync(finalSidecar)) {
|
|
153
|
+
verifySidecarBinary(finalSidecar, version);
|
|
154
|
+
}
|
|
155
|
+
return finalBinary;
|
|
156
|
+
}
|
|
157
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
158
|
+
const temporary = path.join(directory, `.install-${process.pid}-${crypto.randomBytes(6).toString('hex')}`);
|
|
159
|
+
fs.mkdirSync(temporary);
|
|
160
|
+
const asset = assetName(version, target);
|
|
161
|
+
const archive = path.join(temporary, asset);
|
|
162
|
+
const sums = path.join(temporary, 'SHA256SUMS');
|
|
163
|
+
try {
|
|
164
|
+
const fetch = options.download || download;
|
|
165
|
+
await fetch(releaseUrl(version, 'SHA256SUMS'), sums);
|
|
166
|
+
await fetch(releaseUrl(version, asset), archive);
|
|
167
|
+
verifyArchive(archive, parseChecksum(fs.readFileSync(sums, 'utf8'), asset));
|
|
168
|
+
execFileSync('tar', ['xzf', archive, '-C', temporary], { stdio: ['ignore', 'ignore', 'inherit'] });
|
|
169
|
+
const extracted = path.join(temporary, platform === 'win32' ? 'lain.exe' : 'lain');
|
|
170
|
+
if (!fs.existsSync(extracted)) throw new Error(`${asset} does not contain the Lain executable at its root`);
|
|
171
|
+
if (platform !== 'win32') fs.chmodSync(extracted, 0o755);
|
|
172
|
+
verifyBinary(extracted, version);
|
|
173
|
+
const extractedSidecar = path.join(temporary, sidecarName);
|
|
174
|
+
if (fs.existsSync(extractedSidecar)) {
|
|
175
|
+
if (platform !== 'win32') fs.chmodSync(extractedSidecar, 0o755);
|
|
176
|
+
verifySidecarBinary(extractedSidecar, version);
|
|
177
|
+
try {
|
|
178
|
+
fs.renameSync(extractedSidecar, finalSidecar);
|
|
179
|
+
} catch (error) {
|
|
180
|
+
if (!fs.existsSync(finalSidecar)) throw error;
|
|
181
|
+
verifySidecarBinary(finalSidecar, version);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
installWindowsRuntimeDlls(temporary, directory, platform);
|
|
185
|
+
try {
|
|
186
|
+
fs.renameSync(extracted, finalBinary);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (!fs.existsSync(finalBinary)) throw error;
|
|
189
|
+
verifyBinary(finalBinary, version);
|
|
190
|
+
}
|
|
191
|
+
return finalBinary;
|
|
192
|
+
} finally {
|
|
193
|
+
fs.rmSync(temporary, { recursive: true, force: true });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
module.exports = { assetName, binaryPath, cacheRoot, ensureBinary, installWindowsRuntimeDlls, normalizeVersion, parseChecksum, releaseUrl, selectedVersion, sha256, targetFor, verifyArchive, verifyBinary, verifySidecarBinary, verifyWindowsRuntimeDlls };
|