@lsproxy/proxy 0.2.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/CHANGELOG.md +23 -0
- package/LICENSE +21 -0
- package/README.md +74 -0
- package/dist/backend-pool.d.ts +20 -0
- package/dist/backend-pool.d.ts.map +1 -0
- package/dist/backend-pool.js +123 -0
- package/dist/backend-pool.js.map +1 -0
- package/dist/client-session.d.ts +32 -0
- package/dist/client-session.d.ts.map +1 -0
- package/dist/client-session.js +150 -0
- package/dist/client-session.js.map +1 -0
- package/dist/document-state.d.ts +17 -0
- package/dist/document-state.d.ts.map +1 -0
- package/dist/document-state.js +69 -0
- package/dist/document-state.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +33 -0
- package/dist/main.js.map +1 -0
- package/dist/proxy-server.d.ts +28 -0
- package/dist/proxy-server.d.ts.map +1 -0
- package/dist/proxy-server.js +146 -0
- package/dist/proxy-server.js.map +1 -0
- package/dist/socket-path.d.ts +3 -0
- package/dist/socket-path.d.ts.map +1 -0
- package/dist/socket-path.js +13 -0
- package/dist/socket-path.js.map +1 -0
- package/package.json +32 -0
- package/src/backend-pool.test.ts +101 -0
- package/src/backend-pool.ts +151 -0
- package/src/client-session.ts +199 -0
- package/src/document-state.test.ts +64 -0
- package/src/document-state.ts +92 -0
- package/src/index.ts +1 -0
- package/src/main.ts +35 -0
- package/src/proxy-server.ts +158 -0
- package/src/socket-path.test.ts +34 -0
- package/src/socket-path.ts +15 -0
- package/tsconfig.json +12 -0
- package/tsconfig.tsbuildinfo +1 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @lsproxy/proxy
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 03fd44c: feat(cli): language-agnostic and capability-agnostic CLI
|
|
8
|
+
|
|
9
|
+
- `lsp.json` discovery: walks `<root>/lsp.json`, `<root>/.claude/lsp.json`, `<root>/.github/lsp.json`, `~/.claude/lsp.json`; `--server` overrides
|
|
10
|
+
- Runtime Zod → Commander translation: builds namespace/subcommand tree from `LSPSchemas × ServerCapabilities` so only what the connected server advertises is exposed
|
|
11
|
+
- Two-pass parse: `util.parseArgs` extracts globals, then Commander dispatches after capability detection
|
|
12
|
+
- Generic `lspeasy call <method> --params <json>` fallback always available
|
|
13
|
+
- Write operations (rename, formatting) now apply `WorkspaceEdit` results to disk; `--dry-run` shows planned changes without writing
|
|
14
|
+
- `workspace/symbol` gets a `<query>` positional via new `'query'` arg pattern
|
|
15
|
+
|
|
16
|
+
`@lspeasy/core`: expanded `LSPSchemas` to cover all standard text-document and workspace capabilities
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- Updated dependencies [0eb1694]
|
|
21
|
+
- Updated dependencies [03fd44c]
|
|
22
|
+
- @lspeasy/core@2.3.0
|
|
23
|
+
- @lspeasy/client@3.1.1
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Pradeep Mouli
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# @lsproxy/proxy
|
|
2
|
+
|
|
3
|
+
Per-root Unix domain socket daemon that holds warm LSP server connections for
|
|
4
|
+
`@lsproxy/cli`. Each project root gets its own daemon process; the CLI connects
|
|
5
|
+
to it instead of spawning a fresh language server on every invocation.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
An LSP server's `initialize` handshake and project indexing typically take 1–15
|
|
10
|
+
seconds. Paying that cost on every `lspeasy` call makes scripting impractical.
|
|
11
|
+
`@lsproxy/proxy` keeps the server alive between calls so subsequent invocations
|
|
12
|
+
attach in under 100ms.
|
|
13
|
+
|
|
14
|
+
## How it works
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
lspeasy textDocument hover src/foo.ts 12:7
|
|
18
|
+
│
|
|
19
|
+
├─ checks ~/.lsproxy/<hash(root)>.sock
|
|
20
|
+
│
|
|
21
|
+
├─ (first call) spawns lsproxy daemon, awaits socket
|
|
22
|
+
│
|
|
23
|
+
└─ connects via SocketTransport ──► ClientSession
|
|
24
|
+
│
|
|
25
|
+
BackendPool ◄┤ (one LSPClient per languageId)
|
|
26
|
+
│
|
|
27
|
+
LSP server ◄─┘ (warm, already initialized)
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
**`BackendPool`** — one `LSPClient` per `languageId`, lazy-initialised on first
|
|
31
|
+
use, with a configurable idle timeout (default 10 min) after which the server
|
|
32
|
+
process is killed and restarted on the next request.
|
|
33
|
+
|
|
34
|
+
**`DocumentStateManager`** — deduplicates `textDocument/didOpen` across
|
|
35
|
+
concurrent CLI sessions pointing at the same file, and fires `textDocument/didClose`
|
|
36
|
+
lazily (default 5 min after the last session referencing a URI disconnects).
|
|
37
|
+
|
|
38
|
+
**`ClientSession`** — intercepts `initialize` (responds with the server's
|
|
39
|
+
capabilities from the pool), `shutdown`/`exit` (no-op so the daemon stays up),
|
|
40
|
+
and routes `textDocument/*` to the correct backend by URI extension.
|
|
41
|
+
|
|
42
|
+
## Daemon lifecycle
|
|
43
|
+
|
|
44
|
+
| Event | Action |
|
|
45
|
+
|---|---|
|
|
46
|
+
| First CLI connection for a root | Daemon spawned automatically by the CLI |
|
|
47
|
+
| All CLI sessions disconnected | Idle timer starts (default 30 min) |
|
|
48
|
+
| Idle timeout fires | Daemon exits cleanly |
|
|
49
|
+
| Language server idle (no requests) | Backend killed after `--backend-idle-timeout` (default 10 min) |
|
|
50
|
+
|
|
51
|
+
## Socket paths
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
~/.lsproxy/<sha1(absoluteRoot).slice(0,12)>.sock # daemon socket
|
|
55
|
+
~/.lsproxy/<sha1(absoluteRoot).slice(0,12)>.pid # PID file
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Running directly
|
|
59
|
+
|
|
60
|
+
The daemon is normally spawned by the CLI. To run it manually:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
node dist/main.js --root /path/to/project
|
|
64
|
+
|
|
65
|
+
# Optional flags:
|
|
66
|
+
# --socket <path> override socket path
|
|
67
|
+
# --idle-timeout <ms> daemon idle shutdown (default 1800000 = 30min)
|
|
68
|
+
# --backend-idle-timeout <ms> per-server idle kill (default 600000 = 10min)
|
|
69
|
+
# --lazy-close-delay <ms> didClose debounce (default 300000 = 5min)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { LSPClient } from '@lspeasy/client';
|
|
2
|
+
export interface BackendPoolOptions {
|
|
3
|
+
backendIdleMs?: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class BackendPool {
|
|
6
|
+
private readonly root;
|
|
7
|
+
private readonly backends;
|
|
8
|
+
private readonly starting;
|
|
9
|
+
private readonly extensionMap;
|
|
10
|
+
private readonly backendIdleMs;
|
|
11
|
+
constructor(root: string, opts?: BackendPoolOptions);
|
|
12
|
+
getLanguageIdForExtension(ext: string): string | undefined;
|
|
13
|
+
getBackend(languageId: string): LSPClient | undefined;
|
|
14
|
+
ensureBackend(languageId: string): Promise<LSPClient>;
|
|
15
|
+
private startBackend;
|
|
16
|
+
stopBackend(languageId: string): Promise<void>;
|
|
17
|
+
stopAll(): Promise<void>;
|
|
18
|
+
private resetIdleTimer;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=backend-pool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backend-pool.d.ts","sourceRoot":"","sources":["../src/backend-pool.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAkC5C,MAAM,WAAW,kBAAkB;IACjC,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAQD,qBAAa,WAAW;IAOpB,OAAO,CAAC,QAAQ,CAAC,IAAI;IANvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmC;IAC5D,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAyC;IAClE,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAyB;IACtD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IAEvC,YACmB,IAAI,EAAE,MAAM,EAC7B,IAAI,GAAE,kBAAuB,EAI9B;IAED,yBAAyB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEzD;IAED,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,CAEpD;IAEK,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CAkB1D;YAEa,YAAY;IAiCpB,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAcnD;IAEK,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAE7B;IAED,OAAO,CAAC,cAAc;CAMvB"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// apps/proxy/src/backend-pool.ts
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { resolve, basename } from 'node:path';
|
|
4
|
+
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import { LSPClient } from '@lspeasy/client';
|
|
6
|
+
import { tokenizeCommand, discoverServerByLanguageId, discoverExtensionMap } from '@lspeasy/core';
|
|
7
|
+
import { StdioTransport } from '@lspeasy/core/node';
|
|
8
|
+
// Advertise full capabilities so backends expose their complete ServerCapabilities.
|
|
9
|
+
const CLIENT_CAPABILITIES = {
|
|
10
|
+
textDocument: {
|
|
11
|
+
synchronization: { dynamicRegistration: false },
|
|
12
|
+
definition: { dynamicRegistration: false },
|
|
13
|
+
declaration: { dynamicRegistration: false },
|
|
14
|
+
typeDefinition: { dynamicRegistration: false },
|
|
15
|
+
implementation: { dynamicRegistration: false },
|
|
16
|
+
references: { dynamicRegistration: false },
|
|
17
|
+
documentSymbol: { dynamicRegistration: false },
|
|
18
|
+
hover: { dynamicRegistration: false },
|
|
19
|
+
completion: { dynamicRegistration: false },
|
|
20
|
+
signatureHelp: { dynamicRegistration: false },
|
|
21
|
+
rename: { dynamicRegistration: false },
|
|
22
|
+
codeAction: { dynamicRegistration: false },
|
|
23
|
+
formatting: { dynamicRegistration: false },
|
|
24
|
+
rangeFormatting: { dynamicRegistration: false }
|
|
25
|
+
},
|
|
26
|
+
workspace: {
|
|
27
|
+
applyEdit: true,
|
|
28
|
+
executeCommand: { dynamicRegistration: false },
|
|
29
|
+
symbol: { dynamicRegistration: false }
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export class BackendPool {
|
|
33
|
+
root;
|
|
34
|
+
backends = new Map();
|
|
35
|
+
starting = new Map();
|
|
36
|
+
extensionMap;
|
|
37
|
+
backendIdleMs;
|
|
38
|
+
constructor(root, opts = {}) {
|
|
39
|
+
this.root = root;
|
|
40
|
+
this.backendIdleMs = opts.backendIdleMs ?? 600_000;
|
|
41
|
+
this.extensionMap = discoverExtensionMap(root);
|
|
42
|
+
}
|
|
43
|
+
getLanguageIdForExtension(ext) {
|
|
44
|
+
return this.extensionMap[ext];
|
|
45
|
+
}
|
|
46
|
+
getBackend(languageId) {
|
|
47
|
+
return this.backends.get(languageId)?.client;
|
|
48
|
+
}
|
|
49
|
+
async ensureBackend(languageId) {
|
|
50
|
+
const existing = this.backends.get(languageId);
|
|
51
|
+
if (existing) {
|
|
52
|
+
this.resetIdleTimer(languageId, existing);
|
|
53
|
+
return existing.client;
|
|
54
|
+
}
|
|
55
|
+
// Deduplicate concurrent startup calls for the same languageId
|
|
56
|
+
const inflight = this.starting.get(languageId);
|
|
57
|
+
if (inflight)
|
|
58
|
+
return inflight;
|
|
59
|
+
const promise = this.startBackend(languageId);
|
|
60
|
+
this.starting.set(languageId, promise);
|
|
61
|
+
try {
|
|
62
|
+
return await promise;
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
this.starting.delete(languageId);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async startBackend(languageId) {
|
|
69
|
+
const discovered = discoverServerByLanguageId(this.root, languageId);
|
|
70
|
+
if (!discovered) {
|
|
71
|
+
throw new Error(`No LSP server configured for languageId "${languageId}"`);
|
|
72
|
+
}
|
|
73
|
+
const [cmd, ...args] = tokenizeCommand(discovered.serverCommand);
|
|
74
|
+
if (!cmd)
|
|
75
|
+
throw new Error('Empty server command');
|
|
76
|
+
const proc = spawn(cmd, args, { cwd: this.root, stdio: 'pipe' });
|
|
77
|
+
const transport = new StdioTransport({
|
|
78
|
+
input: proc.stdout,
|
|
79
|
+
output: proc.stdin
|
|
80
|
+
});
|
|
81
|
+
const rootDir = resolve(this.root);
|
|
82
|
+
const rootUri = pathToFileURL(rootDir).href;
|
|
83
|
+
const client = new LSPClient({
|
|
84
|
+
name: 'lsproxy',
|
|
85
|
+
version: '0.1.0',
|
|
86
|
+
capabilities: CLIENT_CAPABILITIES,
|
|
87
|
+
rootUri,
|
|
88
|
+
workspaceFolders: [{ uri: rootUri, name: basename(rootDir) }]
|
|
89
|
+
});
|
|
90
|
+
await client.connect(transport);
|
|
91
|
+
const entry = { client, proc };
|
|
92
|
+
this.backends.set(languageId, entry);
|
|
93
|
+
this.resetIdleTimer(languageId, entry);
|
|
94
|
+
return client;
|
|
95
|
+
}
|
|
96
|
+
async stopBackend(languageId) {
|
|
97
|
+
const entry = this.backends.get(languageId);
|
|
98
|
+
if (!entry)
|
|
99
|
+
return;
|
|
100
|
+
if (entry.idleTimer) {
|
|
101
|
+
clearTimeout(entry.idleTimer);
|
|
102
|
+
delete entry.idleTimer;
|
|
103
|
+
}
|
|
104
|
+
this.backends.delete(languageId);
|
|
105
|
+
try {
|
|
106
|
+
await entry.client.disconnect();
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
// ignore
|
|
110
|
+
}
|
|
111
|
+
entry.proc.kill();
|
|
112
|
+
}
|
|
113
|
+
async stopAll() {
|
|
114
|
+
await Promise.all([...this.backends.keys()].map((id) => this.stopBackend(id)));
|
|
115
|
+
}
|
|
116
|
+
resetIdleTimer(languageId, entry) {
|
|
117
|
+
if (entry.idleTimer) {
|
|
118
|
+
clearTimeout(entry.idleTimer);
|
|
119
|
+
}
|
|
120
|
+
entry.idleTimer = setTimeout(() => this.stopBackend(languageId), this.backendIdleMs);
|
|
121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;YACpB,YAAY,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAChC,CAAC;QACD,KAAK,CAAC,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;IACvF,CAAC;CACF"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Transport } from '@lspeasy/core';
|
|
2
|
+
import type { BackendPool } from './backend-pool.js';
|
|
3
|
+
import type { DocumentStateManager } from './document-state.js';
|
|
4
|
+
export interface ClientSessionOptions {
|
|
5
|
+
sessionId: string;
|
|
6
|
+
transport: Transport;
|
|
7
|
+
pool: BackendPool;
|
|
8
|
+
docState: DocumentStateManager;
|
|
9
|
+
root: string;
|
|
10
|
+
onEnd: (sessionId: string) => void;
|
|
11
|
+
}
|
|
12
|
+
export declare class ClientSession {
|
|
13
|
+
private readonly id;
|
|
14
|
+
private readonly transport;
|
|
15
|
+
private readonly pool;
|
|
16
|
+
private readonly docState;
|
|
17
|
+
private readonly onEnd;
|
|
18
|
+
private languageId;
|
|
19
|
+
private requestIdCounter;
|
|
20
|
+
private readonly pendingClientRequests;
|
|
21
|
+
private applyEditDisposable;
|
|
22
|
+
constructor(opts: ClientSessionOptions);
|
|
23
|
+
private handleMessage;
|
|
24
|
+
private forwardToClient;
|
|
25
|
+
private handleRequest;
|
|
26
|
+
private handleInitialize;
|
|
27
|
+
private handleNotification;
|
|
28
|
+
private backendForMsg;
|
|
29
|
+
private languageIdForUri;
|
|
30
|
+
private handleClose;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=client-session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-session.d.ts","sourceRoot":"","sources":["../src/client-session.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAEhE,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,SAAS,CAAC;IACrB,IAAI,EAAE,WAAW,CAAC;IAClB,QAAQ,EAAE,oBAAoB,CAAC;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC;CACpC;AAWD,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAS;IAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;IACtC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAc;IACnC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAuB;IAChD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA8B;IACpD,OAAO,CAAC,UAAU,CAAe;IACjC,OAAO,CAAC,gBAAgB,CAAK;IAC7B,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAyD;IAC/F,OAAO,CAAC,mBAAmB,CAAkC;IAE7D,YAAY,IAAI,EAAE,oBAAoB,EAUrC;YAEa,aAAa;YAqCb,eAAe;YAQf,aAAa;YAeb,gBAAgB;YAgBhB,kBAAkB;IAgDhC,OAAO,CAAC,aAAa;IAWrB,OAAO,CAAC,gBAAgB;IASxB,OAAO,CAAC,WAAW;CAMpB"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// apps/proxy/src/client-session.ts
|
|
2
|
+
import { extname } from 'node:path';
|
|
3
|
+
export class ClientSession {
|
|
4
|
+
id;
|
|
5
|
+
transport;
|
|
6
|
+
pool;
|
|
7
|
+
docState;
|
|
8
|
+
onEnd;
|
|
9
|
+
languageId = 'plaintext';
|
|
10
|
+
requestIdCounter = 0;
|
|
11
|
+
pendingClientRequests = new Map();
|
|
12
|
+
applyEditDisposable;
|
|
13
|
+
constructor(opts) {
|
|
14
|
+
this.id = opts.sessionId;
|
|
15
|
+
this.transport = opts.transport;
|
|
16
|
+
this.pool = opts.pool;
|
|
17
|
+
this.docState = opts.docState;
|
|
18
|
+
this.onEnd = opts.onEnd;
|
|
19
|
+
this.transport.onMessage((msg) => this.handleMessage(msg));
|
|
20
|
+
this.transport.onClose(() => this.handleClose());
|
|
21
|
+
this.transport.onError((e) => process.stderr.write(`[session:${this.id}] ${e.message}\n`));
|
|
22
|
+
}
|
|
23
|
+
async handleMessage(msg) {
|
|
24
|
+
const isRequest = msg.id !== undefined && msg.method !== undefined;
|
|
25
|
+
const isNotification = msg.id === undefined && msg.method !== undefined;
|
|
26
|
+
// Response to a request we sent (e.g. workspace/applyEdit forwarded to CLI)
|
|
27
|
+
const isResponse = msg.id !== undefined && msg.method === undefined && msg.result !== undefined;
|
|
28
|
+
if (isResponse) {
|
|
29
|
+
const resolve = this.pendingClientRequests.get(msg.id);
|
|
30
|
+
if (resolve) {
|
|
31
|
+
this.pendingClientRequests.delete(msg.id);
|
|
32
|
+
resolve(msg.result);
|
|
33
|
+
}
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
if (isRequest) {
|
|
38
|
+
const result = await this.handleRequest(msg);
|
|
39
|
+
await this.transport.send({
|
|
40
|
+
jsonrpc: '2.0',
|
|
41
|
+
id: msg.id,
|
|
42
|
+
result
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else if (isNotification) {
|
|
46
|
+
await this.handleNotification(msg);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
if (isRequest) {
|
|
51
|
+
await this.transport.send({
|
|
52
|
+
jsonrpc: '2.0',
|
|
53
|
+
id: msg.id,
|
|
54
|
+
error: { code: -32603, message: String(err) }
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async forwardToClient(method, params) {
|
|
60
|
+
const id = ++this.requestIdCounter;
|
|
61
|
+
return new Promise((resolve) => {
|
|
62
|
+
this.pendingClientRequests.set(id, resolve);
|
|
63
|
+
void this.transport.send({ jsonrpc: '2.0', id, method, params });
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async handleRequest(msg) {
|
|
67
|
+
if (msg.method === 'initialize') {
|
|
68
|
+
return this.handleInitialize(msg.params);
|
|
69
|
+
}
|
|
70
|
+
if (msg.method === 'shutdown') {
|
|
71
|
+
// Ack without tearing down the shared backend
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const backend = this.backendForMsg(msg);
|
|
75
|
+
return backend.sendRequest(msg.method, msg.params);
|
|
76
|
+
}
|
|
77
|
+
async handleInitialize(params) {
|
|
78
|
+
const initOpts = params['initializationOptions'];
|
|
79
|
+
this.languageId = initOpts?.['languageId'] ?? 'plaintext';
|
|
80
|
+
const backend = await this.pool.ensureBackend(this.languageId);
|
|
81
|
+
// Forward workspace/applyEdit from backend to this CLI session.
|
|
82
|
+
// The registration overwrites any prior session's handler — acceptable because
|
|
83
|
+
// applyEdit only fires during executeCommand, which is driven by an active session.
|
|
84
|
+
this.applyEditDisposable?.dispose();
|
|
85
|
+
this.applyEditDisposable = backend.onRequest('workspace/applyEdit', (p) => this.forwardToClient('workspace/applyEdit', p));
|
|
86
|
+
return { capabilities: backend.getServerCapabilities() ?? {} };
|
|
87
|
+
}
|
|
88
|
+
async handleNotification(msg) {
|
|
89
|
+
// Session lifecycle — not forwarded to the shared backend
|
|
90
|
+
if (msg.method === 'initialized' || msg.method === 'exit')
|
|
91
|
+
return;
|
|
92
|
+
if (msg.method === 'textDocument/didOpen') {
|
|
93
|
+
const p = msg.params;
|
|
94
|
+
const td = p['textDocument'];
|
|
95
|
+
const uri = td['uri'];
|
|
96
|
+
const content = td['text'];
|
|
97
|
+
const langId = td['languageId'];
|
|
98
|
+
const action = this.docState.onDidOpen(this.id, uri, content, langId);
|
|
99
|
+
const backend = await this.pool.ensureBackend(langId || this.languageIdForUri(uri));
|
|
100
|
+
if (action === 'open') {
|
|
101
|
+
await backend.sendNotification('textDocument/didOpen', p);
|
|
102
|
+
}
|
|
103
|
+
else if (action === 'change') {
|
|
104
|
+
await backend.sendNotification('textDocument/didChange', {
|
|
105
|
+
textDocument: { uri, version: td['version'] + 1 },
|
|
106
|
+
contentChanges: [{ text: content }]
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
// 'skip' → no-op
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (msg.method === 'textDocument/didClose') {
|
|
113
|
+
const p = msg.params;
|
|
114
|
+
const td = p['textDocument'];
|
|
115
|
+
const uri = td['uri'];
|
|
116
|
+
this.docState.onDidClose(this.id, uri);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
// Forward all other notifications (didChange, willSave, etc.)
|
|
120
|
+
const backend = this.backendForMsg(msg);
|
|
121
|
+
await backend.sendNotification(msg.method, msg.params);
|
|
122
|
+
}
|
|
123
|
+
backendForMsg(msg) {
|
|
124
|
+
// Prefer routing by URI extension; fall back to session's primary languageId
|
|
125
|
+
const params = msg.params;
|
|
126
|
+
const td = params?.['textDocument'];
|
|
127
|
+
const uri = td?.['uri'];
|
|
128
|
+
const langId = uri ? (this.languageIdForUri(uri) ?? this.languageId) : this.languageId;
|
|
129
|
+
const backend = this.pool.getBackend(langId) ?? this.pool.getBackend(this.languageId);
|
|
130
|
+
if (!backend)
|
|
131
|
+
throw new Error(`No backend available for languageId "${langId}"`);
|
|
132
|
+
return backend;
|
|
133
|
+
}
|
|
134
|
+
languageIdForUri(uri) {
|
|
135
|
+
try {
|
|
136
|
+
const ext = extname(new URL(uri).pathname);
|
|
137
|
+
return this.pool.getLanguageIdForExtension(ext) ?? this.languageId;
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
return this.languageId;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
handleClose() {
|
|
144
|
+
this.applyEditDisposable?.dispose();
|
|
145
|
+
this.applyEditDisposable = undefined;
|
|
146
|
+
this.docState.onSessionEnd(this.id);
|
|
147
|
+
this.onEnd(this.id);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=client-session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-session.js","sourceRoot":"","sources":["../src/client-session.ts"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAwBpC,MAAM,OAAO,aAAa;IACP,EAAE,CAAS;IACX,SAAS,CAAY;IACrB,IAAI,CAAc;IAClB,QAAQ,CAAuB;IAC/B,KAAK,CAA8B;IAC5C,UAAU,GAAG,WAAW,CAAC;IACzB,gBAAgB,GAAG,CAAC,CAAC;IACZ,qBAAqB,GAAG,IAAI,GAAG,EAA8C,CAAC;IACvF,mBAAmB,CAAkC;IAE7D,YAAY,IAA0B;QACpC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC9B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QAExB,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,GAAwB,CAAC,CAAC,CAAC;QAChF,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;QACjD,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC,OAAO,IAAI,CAAC,CA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C,CAAC,IAAI,CAAC,UAAU,CAAC;QACvF,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACtF,IAAI,CAAC,OAAO;YAAE,MAAM,IAAI,KAAK,CAAC,wCAAwC,MAAM,GAAG,CAAC,CAAC;QACjF,OAAO,OAAO,CAAC;IACjB,CAAC;IAEO,gBAAgB,CAAC,GAAW;QAClC,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;YAC3C,OAAO,IAAI,CAAC,IAAI,CAAC,yBAAyB,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC;QACrE,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,CAAC,UAAU,CAAC;QACzB,CAAC;IACH,CAAC;IAEO,WAAW;QACjB,IAAI,CAAC,mBAAmB,EAAE,OAAO,EAAE,CAAC;QACpC,IAAI,CAAC,mBAAmB,GAAG,SAAS,CAAC;QACrC,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACpC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACtB,CAAC;CACF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type DidOpenAction = 'open' | 'change' | 'skip';
|
|
2
|
+
export interface DocumentStateOptions {
|
|
3
|
+
lazyCloseMs?: number;
|
|
4
|
+
onClose?: (uri: string) => void;
|
|
5
|
+
}
|
|
6
|
+
export declare class DocumentStateManager {
|
|
7
|
+
private readonly docs;
|
|
8
|
+
private readonly lazyCloseMs;
|
|
9
|
+
private readonly onClose;
|
|
10
|
+
constructor(opts?: DocumentStateOptions);
|
|
11
|
+
onDidOpen(sessionId: string, uri: string, content: string, languageId: string): DidOpenAction;
|
|
12
|
+
onDidClose(sessionId: string, uri: string): void;
|
|
13
|
+
onSessionEnd(sessionId: string): string[];
|
|
14
|
+
getContent(uri: string): string | undefined;
|
|
15
|
+
private scheduleLazyClose;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=document-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-state.d.ts","sourceRoot":"","sources":["../src/document-state.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,QAAQ,GAAG,MAAM,CAAC;AAEvD,MAAM,WAAW,oBAAoB;IACnC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CACjC;AAUD,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA+B;IACpD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsC;IAE9D,YAAY,IAAI,GAAE,oBAAyB,EAG1C;IAED,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,aAAa,CA0B5F;IAED,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAO/C;IAKD,YAAY,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAWxC;IAED,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAE1C;IAED,OAAO,CAAC,iBAAiB;CAM1B"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// apps/proxy/src/document-state.ts
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
export class DocumentStateManager {
|
|
4
|
+
docs = new Map();
|
|
5
|
+
lazyCloseMs;
|
|
6
|
+
onClose;
|
|
7
|
+
constructor(opts = {}) {
|
|
8
|
+
this.lazyCloseMs = opts.lazyCloseMs ?? 300_000;
|
|
9
|
+
this.onClose = opts.onClose;
|
|
10
|
+
}
|
|
11
|
+
onDidOpen(sessionId, uri, content, languageId) {
|
|
12
|
+
const hash = createHash('sha256').update(content).digest('hex');
|
|
13
|
+
const entry = this.docs.get(uri);
|
|
14
|
+
if (!entry) {
|
|
15
|
+
this.docs.set(uri, {
|
|
16
|
+
languageId,
|
|
17
|
+
content,
|
|
18
|
+
contentHash: hash,
|
|
19
|
+
openSessions: new Set([sessionId])
|
|
20
|
+
});
|
|
21
|
+
return 'open';
|
|
22
|
+
}
|
|
23
|
+
if (entry.closeTimer !== undefined) {
|
|
24
|
+
clearTimeout(entry.closeTimer);
|
|
25
|
+
delete entry.closeTimer;
|
|
26
|
+
}
|
|
27
|
+
entry.openSessions.add(sessionId);
|
|
28
|
+
if (entry.contentHash === hash)
|
|
29
|
+
return 'skip';
|
|
30
|
+
entry.content = content;
|
|
31
|
+
entry.contentHash = hash;
|
|
32
|
+
return 'change';
|
|
33
|
+
}
|
|
34
|
+
onDidClose(sessionId, uri) {
|
|
35
|
+
const entry = this.docs.get(uri);
|
|
36
|
+
if (!entry)
|
|
37
|
+
return;
|
|
38
|
+
entry.openSessions.delete(sessionId);
|
|
39
|
+
if (entry.openSessions.size === 0) {
|
|
40
|
+
this.scheduleLazyClose(uri, entry);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Returns URIs where this session was the last opener (informational).
|
|
44
|
+
// The actual backend didClose fires via onClose after lazyCloseMs — do NOT
|
|
45
|
+
// forward didClose immediately based on this return value.
|
|
46
|
+
onSessionEnd(sessionId) {
|
|
47
|
+
const toClose = [];
|
|
48
|
+
for (const [uri, entry] of this.docs) {
|
|
49
|
+
if (!entry.openSessions.has(sessionId))
|
|
50
|
+
continue;
|
|
51
|
+
entry.openSessions.delete(sessionId);
|
|
52
|
+
if (entry.openSessions.size === 0) {
|
|
53
|
+
toClose.push(uri);
|
|
54
|
+
this.scheduleLazyClose(uri, entry);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return toClose;
|
|
58
|
+
}
|
|
59
|
+
getContent(uri) {
|
|
60
|
+
return this.docs.get(uri)?.content;
|
|
61
|
+
}
|
|
62
|
+
scheduleLazyClose(uri, entry) {
|
|
63
|
+
entry.closeTimer = setTimeout(() => {
|
|
64
|
+
this.docs.delete(uri);
|
|
65
|
+
this.onClose?.(uri);
|
|
66
|
+
}, this.lazyCloseMs);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=document-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"document-state.js","sourceRoot":"","sources":["../src/document-state.ts"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAiBzC,MAAM,OAAO,oBAAoB;IACd,IAAI,GAAG,IAAI,GAAG,EAAoB,CAAC;IACnC,WAAW,CAAS;IACpB,OAAO,CAAsC;IAE9D,YAAY,IAAI,GAAyB,EAAE;QACzC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,IAAI,OAAO,CAAC;QAC/C,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC;IAC9B,CAAC;IAED,SAAS,CAAC,SAAiB,EAAE,GAAW,EAAE,OAAe,EAAE,UAAkB;QAC3E,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAChE,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEjC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE;gBACjB,UAAU;gBACV,OAAO;gBACP,WAAW,EAAE,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CAAC,YAAY,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;gBAClC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBAClB,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACrC,CAAC;QACH,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,UAAU,CAAC,GAAW;QACpB,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IACrC,CAAC;IAEO,iBAAiB,CAAC,GAAW,EAAE,KAAe;QACpD,KAAK,CAAC,UAAU,GAAG,UAAU,CAAC,GAAG,EAAE;YACjC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACtB,IAAI,CAAC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC;QACtB,CAAC,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IACvB,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC"}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":""}
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// apps/proxy/src/main.ts
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { resolve } from 'node:path';
|
|
5
|
+
import { ProxyServer } from './proxy-server.js';
|
|
6
|
+
const { values } = parseArgs({
|
|
7
|
+
options: {
|
|
8
|
+
root: { type: 'string' },
|
|
9
|
+
socket: { type: 'string' },
|
|
10
|
+
'idle-timeout': { type: 'string', default: '1800000' },
|
|
11
|
+
'backend-idle-timeout': { type: 'string', default: '600000' },
|
|
12
|
+
'lazy-close-delay': { type: 'string', default: '300000' }
|
|
13
|
+
},
|
|
14
|
+
allowPositionals: false,
|
|
15
|
+
strict: true
|
|
16
|
+
});
|
|
17
|
+
if (!values['root']) {
|
|
18
|
+
process.stderr.write('[lsproxy] fatal: --root is required\n');
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
const root = resolve(values['root']);
|
|
22
|
+
const server = new ProxyServer({
|
|
23
|
+
root,
|
|
24
|
+
...(values['socket'] !== undefined && { socketOverride: values['socket'] }),
|
|
25
|
+
idleTimeoutMs: Number(values['idle-timeout']),
|
|
26
|
+
backendIdleMs: Number(values['backend-idle-timeout']),
|
|
27
|
+
lazyCloseMs: Number(values['lazy-close-delay'])
|
|
28
|
+
});
|
|
29
|
+
server.start().catch((err) => {
|
|
30
|
+
process.stderr.write(`[lsproxy] fatal: ${err.message}\n`);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
});
|
|
33
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":";AACA,yBAAyB;AACzB,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;IAC3B,OAAO,EAAE;QACP,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE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