@hmharness/lsp 0.23.31 → 0.23.33
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/package.json +23 -23
- package/src/__tests__/deveco.test.ts +60 -60
- package/src/__tests__/fixtures/fake-server.cjs +89 -89
- package/src/__tests__/lsp.test.ts +131 -131
- package/src/__tests__/trust.test.ts +68 -68
- package/src/client.ts +137 -137
- package/src/index.ts +15 -15
- package/src/process-manager.ts +118 -118
- package/src/protocol.ts +113 -113
- package/src/registry.ts +130 -130
- package/src/tools.ts +229 -229
- package/src/trust.ts +108 -108
- package/tsconfig.build.json +18 -18
- package/verify-clangd.mts +40 -40
- package/verify-live.mts +27 -27
package/src/process-manager.ts
CHANGED
|
@@ -1,118 +1,118 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @hmharness/lsp - process manager (master prompt security rules)
|
|
3
|
-
*
|
|
4
|
-
* Owns language-server child processes with the hard walls from the LSP
|
|
5
|
-
* 专项方案 (03):
|
|
6
|
-
* - cwd is BOUND to the workspace root (a server cannot wander)
|
|
7
|
-
* - environment is an ALLOWLIST (secret env vars never inherit)
|
|
8
|
-
* - crash restart has a cap + exponential backoff (a flapping server
|
|
9
|
-
* must not become a fork bomb)
|
|
10
|
-
* - every request has a timeout and can be cancelled
|
|
11
|
-
* - the server's identity (command/version) is recorded for audit
|
|
12
|
-
*/
|
|
13
|
-
import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
|
|
14
|
-
|
|
15
|
-
export interface ServerSpec {
|
|
16
|
-
/** server id, e.g. 'typescript' | 'pyright' | 'rust-analyzer' */
|
|
17
|
-
id: string;
|
|
18
|
-
/** executable + args exactly as discovered (no auto-download here) */
|
|
19
|
-
command: string;
|
|
20
|
-
args: string[];
|
|
21
|
-
/** provenance recorded for audit: where this command came from */
|
|
22
|
-
source: 'PATH' | 'explicit';
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface ManagedServerOptions {
|
|
26
|
-
/** secret-free env allowlist passed to the server (default: PATH/SYSTEMROOT/LANG only) */
|
|
27
|
-
envAllowlist?: string[];
|
|
28
|
-
maxRestarts?: number;
|
|
29
|
-
requestTimeoutMs?: number;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Environment scrub: language servers do NOT need API keys to index code.
|
|
33
|
-
* Default allowlist keeps PATH working on Windows without inheriting
|
|
34
|
-
* provider tokens or user secrets. */
|
|
35
|
-
export function scrubEnv(extraAllow: string[] = []): NodeJS.ProcessEnv {
|
|
36
|
-
const allow = new Set(['PATH', 'Path', 'SYSTEMROOT', 'SystemRoot', 'TEMP', 'TMP', 'LANG', 'HOME', 'USERPROFILE', 'APPDATA', 'LOCALAPPDATA', 'PROGRAMFILES', ...extraAllow]);
|
|
37
|
-
const out: NodeJS.ProcessEnv = {};
|
|
38
|
-
for (const [k, v] of Object.entries(process.env)) {
|
|
39
|
-
if (v !== undefined && allow.has(k.toUpperCase()) === false && !allow.has(k)) continue;
|
|
40
|
-
if (v !== undefined) out[k] = v;
|
|
41
|
-
}
|
|
42
|
-
return out;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export class ProcessManager {
|
|
46
|
-
private proc: ChildProcessWithoutNullStreams | null = null;
|
|
47
|
-
/** total spawns; spawns beyond the first are restarts (exit clears this.proc,
|
|
48
|
-
* so counting there would miss exactly the flapping case) */
|
|
49
|
-
private spawns = 0;
|
|
50
|
-
readonly startedAt: string | null = null;
|
|
51
|
-
lastExit: { code: number | null; signal: NodeJS.Signals | null; at: string } | null = null;
|
|
52
|
-
|
|
53
|
-
constructor(
|
|
54
|
-
readonly spec: ServerSpec,
|
|
55
|
-
private cwd: string,
|
|
56
|
-
private opts: ManagedServerOptions = {},
|
|
57
|
-
) {}
|
|
58
|
-
|
|
59
|
-
get restartCount(): number {
|
|
60
|
-
return Math.max(0, this.spawns - 1);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
get running(): boolean {
|
|
64
|
-
return this.proc !== null && this.proc.exitCode === null && !this.proc.killed;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** Spawn (or respawn after crash, up to the cap). Resolves with the
|
|
68
|
-
* child's stdio streams; rejects when the restart budget is spent. */
|
|
69
|
-
start(): ChildProcessWithoutNullStreams {
|
|
70
|
-
if (this.running && this.proc) return this.proc;
|
|
71
|
-
const max = this.opts.maxRestarts ?? 3;
|
|
72
|
-
// allowed total spawns = the initial one + maxRestarts
|
|
73
|
-
if (this.spawns >= 1 + max) {
|
|
74
|
-
throw new Error(`server ${this.spec.id} exceeded restart budget (${this.restartCount} restarts >= ${max}) — refusing to flap`);
|
|
75
|
-
}
|
|
76
|
-
this.spawns += 1;
|
|
77
|
-
const proc = spawn(this.spec.command, this.spec.args, {
|
|
78
|
-
cwd: this.cwd, // bound to the workspace (03 方案: workspace 外访问)
|
|
79
|
-
env: scrubEnv(),
|
|
80
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
81
|
-
windowsHide: true,
|
|
82
|
-
}) as ChildProcessWithoutNullStreams;
|
|
83
|
-
proc.on('exit', (code, signal) => {
|
|
84
|
-
this.lastExit = { code, signal, at: new Date().toISOString() };
|
|
85
|
-
if (this.proc === proc) this.proc = null;
|
|
86
|
-
});
|
|
87
|
-
this.proc = proc;
|
|
88
|
-
return proc;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Graceful stop, then hard kill. Always resolves. */
|
|
92
|
-
async stop(): Promise<void> {
|
|
93
|
-
const proc = this.proc;
|
|
94
|
-
if (!proc) return;
|
|
95
|
-
this.proc = null;
|
|
96
|
-
await new Promise<void>((resolve) => {
|
|
97
|
-
const killTimer = setTimeout(() => { try { proc.kill('SIGKILL'); } catch { /* gone */ } }, 3_000);
|
|
98
|
-
proc.once('exit', () => { clearTimeout(killTimer); resolve(); });
|
|
99
|
-
try { proc.kill(); } catch { clearTimeout(killTimer); resolve(); }
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** Synchronous last-resort kill (process 'exit' hook: async cleanup will
|
|
104
|
-
* not run there). Never spawns; kills the live child if any. */
|
|
105
|
-
killSync(): void {
|
|
106
|
-
const proc = this.proc;
|
|
107
|
-
this.proc = null;
|
|
108
|
-
if (proc) { try { proc.kill('SIGKILL'); } catch { /* gone */ } }
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/** Discover a server binary from PATH (explicit, never auto-downloaded —
|
|
113
|
-
* 03 方案: 自动下载供应链风险,来源白名单+hash 是后续 Capability OS 的事). */
|
|
114
|
-
export function fromPath(id: string, command: string, args: string[]): ServerSpec | null {
|
|
115
|
-
// PATH lookup without spawning: `where` on win, `which` elsewhere — cheap
|
|
116
|
-
// probe done by the caller; here we just mark provenance
|
|
117
|
-
return { id, command, args, source: 'PATH' };
|
|
118
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/lsp - process manager (master prompt security rules)
|
|
3
|
+
*
|
|
4
|
+
* Owns language-server child processes with the hard walls from the LSP
|
|
5
|
+
* 专项方案 (03):
|
|
6
|
+
* - cwd is BOUND to the workspace root (a server cannot wander)
|
|
7
|
+
* - environment is an ALLOWLIST (secret env vars never inherit)
|
|
8
|
+
* - crash restart has a cap + exponential backoff (a flapping server
|
|
9
|
+
* must not become a fork bomb)
|
|
10
|
+
* - every request has a timeout and can be cancelled
|
|
11
|
+
* - the server's identity (command/version) is recorded for audit
|
|
12
|
+
*/
|
|
13
|
+
import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
|
|
14
|
+
|
|
15
|
+
export interface ServerSpec {
|
|
16
|
+
/** server id, e.g. 'typescript' | 'pyright' | 'rust-analyzer' */
|
|
17
|
+
id: string;
|
|
18
|
+
/** executable + args exactly as discovered (no auto-download here) */
|
|
19
|
+
command: string;
|
|
20
|
+
args: string[];
|
|
21
|
+
/** provenance recorded for audit: where this command came from */
|
|
22
|
+
source: 'PATH' | 'explicit';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ManagedServerOptions {
|
|
26
|
+
/** secret-free env allowlist passed to the server (default: PATH/SYSTEMROOT/LANG only) */
|
|
27
|
+
envAllowlist?: string[];
|
|
28
|
+
maxRestarts?: number;
|
|
29
|
+
requestTimeoutMs?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Environment scrub: language servers do NOT need API keys to index code.
|
|
33
|
+
* Default allowlist keeps PATH working on Windows without inheriting
|
|
34
|
+
* provider tokens or user secrets. */
|
|
35
|
+
export function scrubEnv(extraAllow: string[] = []): NodeJS.ProcessEnv {
|
|
36
|
+
const allow = new Set(['PATH', 'Path', 'SYSTEMROOT', 'SystemRoot', 'TEMP', 'TMP', 'LANG', 'HOME', 'USERPROFILE', 'APPDATA', 'LOCALAPPDATA', 'PROGRAMFILES', ...extraAllow]);
|
|
37
|
+
const out: NodeJS.ProcessEnv = {};
|
|
38
|
+
for (const [k, v] of Object.entries(process.env)) {
|
|
39
|
+
if (v !== undefined && allow.has(k.toUpperCase()) === false && !allow.has(k)) continue;
|
|
40
|
+
if (v !== undefined) out[k] = v;
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class ProcessManager {
|
|
46
|
+
private proc: ChildProcessWithoutNullStreams | null = null;
|
|
47
|
+
/** total spawns; spawns beyond the first are restarts (exit clears this.proc,
|
|
48
|
+
* so counting there would miss exactly the flapping case) */
|
|
49
|
+
private spawns = 0;
|
|
50
|
+
readonly startedAt: string | null = null;
|
|
51
|
+
lastExit: { code: number | null; signal: NodeJS.Signals | null; at: string } | null = null;
|
|
52
|
+
|
|
53
|
+
constructor(
|
|
54
|
+
readonly spec: ServerSpec,
|
|
55
|
+
private cwd: string,
|
|
56
|
+
private opts: ManagedServerOptions = {},
|
|
57
|
+
) {}
|
|
58
|
+
|
|
59
|
+
get restartCount(): number {
|
|
60
|
+
return Math.max(0, this.spawns - 1);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get running(): boolean {
|
|
64
|
+
return this.proc !== null && this.proc.exitCode === null && !this.proc.killed;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Spawn (or respawn after crash, up to the cap). Resolves with the
|
|
68
|
+
* child's stdio streams; rejects when the restart budget is spent. */
|
|
69
|
+
start(): ChildProcessWithoutNullStreams {
|
|
70
|
+
if (this.running && this.proc) return this.proc;
|
|
71
|
+
const max = this.opts.maxRestarts ?? 3;
|
|
72
|
+
// allowed total spawns = the initial one + maxRestarts
|
|
73
|
+
if (this.spawns >= 1 + max) {
|
|
74
|
+
throw new Error(`server ${this.spec.id} exceeded restart budget (${this.restartCount} restarts >= ${max}) — refusing to flap`);
|
|
75
|
+
}
|
|
76
|
+
this.spawns += 1;
|
|
77
|
+
const proc = spawn(this.spec.command, this.spec.args, {
|
|
78
|
+
cwd: this.cwd, // bound to the workspace (03 方案: workspace 外访问)
|
|
79
|
+
env: scrubEnv(),
|
|
80
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
81
|
+
windowsHide: true,
|
|
82
|
+
}) as ChildProcessWithoutNullStreams;
|
|
83
|
+
proc.on('exit', (code, signal) => {
|
|
84
|
+
this.lastExit = { code, signal, at: new Date().toISOString() };
|
|
85
|
+
if (this.proc === proc) this.proc = null;
|
|
86
|
+
});
|
|
87
|
+
this.proc = proc;
|
|
88
|
+
return proc;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Graceful stop, then hard kill. Always resolves. */
|
|
92
|
+
async stop(): Promise<void> {
|
|
93
|
+
const proc = this.proc;
|
|
94
|
+
if (!proc) return;
|
|
95
|
+
this.proc = null;
|
|
96
|
+
await new Promise<void>((resolve) => {
|
|
97
|
+
const killTimer = setTimeout(() => { try { proc.kill('SIGKILL'); } catch { /* gone */ } }, 3_000);
|
|
98
|
+
proc.once('exit', () => { clearTimeout(killTimer); resolve(); });
|
|
99
|
+
try { proc.kill(); } catch { clearTimeout(killTimer); resolve(); }
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Synchronous last-resort kill (process 'exit' hook: async cleanup will
|
|
104
|
+
* not run there). Never spawns; kills the live child if any. */
|
|
105
|
+
killSync(): void {
|
|
106
|
+
const proc = this.proc;
|
|
107
|
+
this.proc = null;
|
|
108
|
+
if (proc) { try { proc.kill('SIGKILL'); } catch { /* gone */ } }
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Discover a server binary from PATH (explicit, never auto-downloaded —
|
|
113
|
+
* 03 方案: 自动下载供应链风险,来源白名单+hash 是后续 Capability OS 的事). */
|
|
114
|
+
export function fromPath(id: string, command: string, args: string[]): ServerSpec | null {
|
|
115
|
+
// PATH lookup without spawning: `where` on win, `which` elsewhere — cheap
|
|
116
|
+
// probe done by the caller; here we just mark provenance
|
|
117
|
+
return { id, command, args, source: 'PATH' };
|
|
118
|
+
}
|
package/src/protocol.ts
CHANGED
|
@@ -1,113 +1,113 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @hmharness/lsp - protocol (LSP 3.18 subset + JSON-RPC framing)
|
|
3
|
-
*
|
|
4
|
-
* Client-side types ONLY (master prompt: we implement the Client/Manager,
|
|
5
|
-
* never a language server). The subset below covers the Tier-0/Tier-1
|
|
6
|
-
* agent surface: lifecycle, diagnostics, hover, definition, references,
|
|
7
|
-
* document symbols, plus the framing primitives every message rides on.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/* ---------------- JSON-RPC / framing ---------------- */
|
|
11
|
-
|
|
12
|
-
export interface JsonRpcRequest {
|
|
13
|
-
jsonrpc: '2.0';
|
|
14
|
-
id: number;
|
|
15
|
-
method: string;
|
|
16
|
-
params?: unknown;
|
|
17
|
-
}
|
|
18
|
-
export interface JsonRpcNotification {
|
|
19
|
-
jsonrpc: '2.0';
|
|
20
|
-
method: string;
|
|
21
|
-
params?: unknown;
|
|
22
|
-
}
|
|
23
|
-
export interface JsonRpcResponse {
|
|
24
|
-
jsonrpc: '2.0';
|
|
25
|
-
id: number;
|
|
26
|
-
result?: unknown;
|
|
27
|
-
error?: { code: number; message: string; data?: unknown };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** `Content-Length: N\r\n\r\n<payload>` framing (LSP base protocol). */
|
|
31
|
-
export function encodeMessage(obj: unknown): Buffer {
|
|
32
|
-
const payload = Buffer.from(JSON.stringify(obj), 'utf8');
|
|
33
|
-
return Buffer.concat([Buffer.from(`Content-Length: ${payload.length}\r\n\r\n`, 'ascii'), payload]);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** Incremental frame decoder: feed stdout chunks, get parsed messages. */
|
|
37
|
-
export class MessageDecoder {
|
|
38
|
-
private buf = Buffer.alloc(0);
|
|
39
|
-
push(chunk: Buffer): unknown[] {
|
|
40
|
-
this.buf = Buffer.concat([this.buf, chunk]);
|
|
41
|
-
const out: unknown[] = [];
|
|
42
|
-
for (;;) {
|
|
43
|
-
const headerEnd = this.buf.indexOf('\r\n\r\n');
|
|
44
|
-
if (headerEnd < 0) break;
|
|
45
|
-
const header = this.buf.subarray(0, headerEnd).toString('ascii');
|
|
46
|
-
const m = /Content-Length:\s*(\d+)/i.exec(header);
|
|
47
|
-
if (!m) { this.buf = this.buf.subarray(headerEnd + 4); continue; }
|
|
48
|
-
const len = Number(m[1]);
|
|
49
|
-
const bodyStart = headerEnd + 4;
|
|
50
|
-
if (this.buf.length < bodyStart + len) break; // partial body
|
|
51
|
-
const body = this.buf.subarray(bodyStart, bodyStart + len).toString('utf8');
|
|
52
|
-
this.buf = this.buf.subarray(bodyStart + len);
|
|
53
|
-
try { out.push(JSON.parse(body)); } catch { /* skip malformed frame */ }
|
|
54
|
-
}
|
|
55
|
-
return out;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/* ---------------- lifecycle ---------------- */
|
|
60
|
-
|
|
61
|
-
export interface InitializeParams {
|
|
62
|
-
processId: number | null;
|
|
63
|
-
rootUri: string | null;
|
|
64
|
-
workspaceFolders: Array<{ name: string; uri: string }> | null;
|
|
65
|
-
capabilities: Record<string, unknown>;
|
|
66
|
-
}
|
|
67
|
-
export interface InitializeResult {
|
|
68
|
-
capabilities: Record<string, unknown>;
|
|
69
|
-
serverInfo?: { name?: string; version?: string };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/* ---------------- positions / documents ---------------- */
|
|
73
|
-
|
|
74
|
-
export interface Position { line: number; character: number }
|
|
75
|
-
export interface Range { start: Position; end: Position }
|
|
76
|
-
export interface Location { uri: string; range: Range }
|
|
77
|
-
export interface TextDocumentIdentifier { uri: string }
|
|
78
|
-
export interface VersionedTextDocumentIdentifier { uri: string; version: number }
|
|
79
|
-
|
|
80
|
-
export interface DidOpenParams {
|
|
81
|
-
textDocument: { uri: string; languageId: string; version: number; text: string };
|
|
82
|
-
}
|
|
83
|
-
export interface DidChangeParams {
|
|
84
|
-
textDocument: VersionedTextDocumentIdentifier;
|
|
85
|
-
contentChanges: Array<{ range?: Range; text: string }>;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/* ---------------- Tier-0 results ---------------- */
|
|
89
|
-
|
|
90
|
-
export interface Diagnostic {
|
|
91
|
-
range: Range;
|
|
92
|
-
severity?: 1 | 2 | 3 | 4; // Error | Warning | Information | Hint
|
|
93
|
-
code?: number | string;
|
|
94
|
-
source?: string;
|
|
95
|
-
message: string;
|
|
96
|
-
}
|
|
97
|
-
export interface PublishDiagnosticsParams { uri: string; diagnostics: Diagnostic[] }
|
|
98
|
-
|
|
99
|
-
export interface Hover { contents: unknown; range?: Range }
|
|
100
|
-
export interface DocumentSymbol {
|
|
101
|
-
name: string;
|
|
102
|
-
kind: number;
|
|
103
|
-
range: Range;
|
|
104
|
-
selectionRange?: Range;
|
|
105
|
-
children?: DocumentSymbol[];
|
|
106
|
-
}
|
|
107
|
-
export interface ReferenceParams {
|
|
108
|
-
textDocument: TextDocumentIdentifier;
|
|
109
|
-
position: Position;
|
|
110
|
-
context: { includeDeclaration: boolean };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export const LSP_TIMEOUT_DEFAULT_MS = 20_000;
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/lsp - protocol (LSP 3.18 subset + JSON-RPC framing)
|
|
3
|
+
*
|
|
4
|
+
* Client-side types ONLY (master prompt: we implement the Client/Manager,
|
|
5
|
+
* never a language server). The subset below covers the Tier-0/Tier-1
|
|
6
|
+
* agent surface: lifecycle, diagnostics, hover, definition, references,
|
|
7
|
+
* document symbols, plus the framing primitives every message rides on.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/* ---------------- JSON-RPC / framing ---------------- */
|
|
11
|
+
|
|
12
|
+
export interface JsonRpcRequest {
|
|
13
|
+
jsonrpc: '2.0';
|
|
14
|
+
id: number;
|
|
15
|
+
method: string;
|
|
16
|
+
params?: unknown;
|
|
17
|
+
}
|
|
18
|
+
export interface JsonRpcNotification {
|
|
19
|
+
jsonrpc: '2.0';
|
|
20
|
+
method: string;
|
|
21
|
+
params?: unknown;
|
|
22
|
+
}
|
|
23
|
+
export interface JsonRpcResponse {
|
|
24
|
+
jsonrpc: '2.0';
|
|
25
|
+
id: number;
|
|
26
|
+
result?: unknown;
|
|
27
|
+
error?: { code: number; message: string; data?: unknown };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** `Content-Length: N\r\n\r\n<payload>` framing (LSP base protocol). */
|
|
31
|
+
export function encodeMessage(obj: unknown): Buffer {
|
|
32
|
+
const payload = Buffer.from(JSON.stringify(obj), 'utf8');
|
|
33
|
+
return Buffer.concat([Buffer.from(`Content-Length: ${payload.length}\r\n\r\n`, 'ascii'), payload]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Incremental frame decoder: feed stdout chunks, get parsed messages. */
|
|
37
|
+
export class MessageDecoder {
|
|
38
|
+
private buf = Buffer.alloc(0);
|
|
39
|
+
push(chunk: Buffer): unknown[] {
|
|
40
|
+
this.buf = Buffer.concat([this.buf, chunk]);
|
|
41
|
+
const out: unknown[] = [];
|
|
42
|
+
for (;;) {
|
|
43
|
+
const headerEnd = this.buf.indexOf('\r\n\r\n');
|
|
44
|
+
if (headerEnd < 0) break;
|
|
45
|
+
const header = this.buf.subarray(0, headerEnd).toString('ascii');
|
|
46
|
+
const m = /Content-Length:\s*(\d+)/i.exec(header);
|
|
47
|
+
if (!m) { this.buf = this.buf.subarray(headerEnd + 4); continue; }
|
|
48
|
+
const len = Number(m[1]);
|
|
49
|
+
const bodyStart = headerEnd + 4;
|
|
50
|
+
if (this.buf.length < bodyStart + len) break; // partial body
|
|
51
|
+
const body = this.buf.subarray(bodyStart, bodyStart + len).toString('utf8');
|
|
52
|
+
this.buf = this.buf.subarray(bodyStart + len);
|
|
53
|
+
try { out.push(JSON.parse(body)); } catch { /* skip malformed frame */ }
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* ---------------- lifecycle ---------------- */
|
|
60
|
+
|
|
61
|
+
export interface InitializeParams {
|
|
62
|
+
processId: number | null;
|
|
63
|
+
rootUri: string | null;
|
|
64
|
+
workspaceFolders: Array<{ name: string; uri: string }> | null;
|
|
65
|
+
capabilities: Record<string, unknown>;
|
|
66
|
+
}
|
|
67
|
+
export interface InitializeResult {
|
|
68
|
+
capabilities: Record<string, unknown>;
|
|
69
|
+
serverInfo?: { name?: string; version?: string };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/* ---------------- positions / documents ---------------- */
|
|
73
|
+
|
|
74
|
+
export interface Position { line: number; character: number }
|
|
75
|
+
export interface Range { start: Position; end: Position }
|
|
76
|
+
export interface Location { uri: string; range: Range }
|
|
77
|
+
export interface TextDocumentIdentifier { uri: string }
|
|
78
|
+
export interface VersionedTextDocumentIdentifier { uri: string; version: number }
|
|
79
|
+
|
|
80
|
+
export interface DidOpenParams {
|
|
81
|
+
textDocument: { uri: string; languageId: string; version: number; text: string };
|
|
82
|
+
}
|
|
83
|
+
export interface DidChangeParams {
|
|
84
|
+
textDocument: VersionedTextDocumentIdentifier;
|
|
85
|
+
contentChanges: Array<{ range?: Range; text: string }>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/* ---------------- Tier-0 results ---------------- */
|
|
89
|
+
|
|
90
|
+
export interface Diagnostic {
|
|
91
|
+
range: Range;
|
|
92
|
+
severity?: 1 | 2 | 3 | 4; // Error | Warning | Information | Hint
|
|
93
|
+
code?: number | string;
|
|
94
|
+
source?: string;
|
|
95
|
+
message: string;
|
|
96
|
+
}
|
|
97
|
+
export interface PublishDiagnosticsParams { uri: string; diagnostics: Diagnostic[] }
|
|
98
|
+
|
|
99
|
+
export interface Hover { contents: unknown; range?: Range }
|
|
100
|
+
export interface DocumentSymbol {
|
|
101
|
+
name: string;
|
|
102
|
+
kind: number;
|
|
103
|
+
range: Range;
|
|
104
|
+
selectionRange?: Range;
|
|
105
|
+
children?: DocumentSymbol[];
|
|
106
|
+
}
|
|
107
|
+
export interface ReferenceParams {
|
|
108
|
+
textDocument: TextDocumentIdentifier;
|
|
109
|
+
position: Position;
|
|
110
|
+
context: { includeDeclaration: boolean };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export const LSP_TIMEOUT_DEFAULT_MS = 20_000;
|