@opentide/lsp-worker 0.0.0 → 1.0.0-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +6 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +146 -0
- package/package.json +36 -1
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) opentide contributors
|
|
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/NOTICE
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
@opentide/lsp-worker is MIT-licensed (LICENSE). It loads, and does not
|
|
2
|
+
contain, the opentide engine's WebAssembly module (`opentide_wasm.wasm` and
|
|
3
|
+
its wasm-bindgen glue), which is licensed under the EUPL-1.2. The module is
|
|
4
|
+
published with the language-server release (`opentide-wasm-web-<version>.zip`)
|
|
5
|
+
together with its LICENSE, NOTICE and THIRD-PARTY-LICENSES.html; whoever
|
|
6
|
+
serves the module to users ships those with it.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM worker host for `opentide-wasm`.
|
|
3
|
+
*
|
|
4
|
+
* Artifact: wasm32-unknown-unknown + wasm-bindgen.
|
|
5
|
+
* Do not mix WASI into this module. vscode.dev uses a separate wasm32-wasip1 binary.
|
|
6
|
+
*
|
|
7
|
+
* The module runs two ways:
|
|
8
|
+
*
|
|
9
|
+
* - `serveLanguageServer` runs the language server itself (`opentide-lsp`, the
|
|
10
|
+
* server `--stdio` runs) inside a Web Worker. Each message the worker
|
|
11
|
+
* receives is one JSON-RPC message from the client, as JSON text with no
|
|
12
|
+
* `Content-Length` header, and each message it posts is one from the server:
|
|
13
|
+
* what `@codemirror/lsp-client`'s `Transport` sends, and what
|
|
14
|
+
* `createOpentideClient({ transport: "worker", worker })` speaks.
|
|
15
|
+
* - `loadHighlightModule` loads `highlight()` and `legend()` to call in the
|
|
16
|
+
* same thread: `createOpentideClient({ transport: "worker", module })`.
|
|
17
|
+
*/
|
|
18
|
+
export interface WorkerHighlightModule {
|
|
19
|
+
highlight(language_id: string, bytes: Uint8Array): unknown;
|
|
20
|
+
legend(): string[];
|
|
21
|
+
}
|
|
22
|
+
/** What wasm-bindgen's glue for `opentide_wasm.wasm` exports. */
|
|
23
|
+
export interface GlueModule {
|
|
24
|
+
default?: unknown;
|
|
25
|
+
highlight?: unknown;
|
|
26
|
+
legend?: unknown;
|
|
27
|
+
version?: unknown;
|
|
28
|
+
LanguageServer?: unknown;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The glue: the URL of its JavaScript file, or the module itself when the
|
|
32
|
+
* host has imported it already (a bundler's `import * as glue from
|
|
33
|
+
* "./opentide_wasm.js"`, which puts the glue in the worker's own chunk).
|
|
34
|
+
*/
|
|
35
|
+
export type Glue = string | URL | GlueModule;
|
|
36
|
+
/** Where `--target web` glue reads the module from: a URL, a `Response`, bytes, or a compiled module. */
|
|
37
|
+
export type WasmSource = string | URL | Response | BufferSource | WebAssembly.Module;
|
|
38
|
+
/** Load the glue and return `highlight()` and `legend()`. */
|
|
39
|
+
export declare function loadHighlightModule(glueUrl: Glue, wasm?: WasmSource): Promise<WorkerHighlightModule>;
|
|
40
|
+
/** One session of the language server: its open documents and its workspace. */
|
|
41
|
+
export interface LanguageServerSession {
|
|
42
|
+
/** One JSON-RPC message in, as JSON text; the messages the server sends back, in order. */
|
|
43
|
+
handle(message: string): string[];
|
|
44
|
+
/** Whether the client has sent `exit`. */
|
|
45
|
+
readonly closed: boolean;
|
|
46
|
+
/** Release the session's memory in the module (wasm-bindgen classes have it). */
|
|
47
|
+
free?(): void;
|
|
48
|
+
}
|
|
49
|
+
/** The language server in a loaded module. */
|
|
50
|
+
export interface LanguageServerModule {
|
|
51
|
+
/** A new session, as a new `--stdio` process would be. */
|
|
52
|
+
createSession(): LanguageServerSession;
|
|
53
|
+
/** The engine's version. */
|
|
54
|
+
version(): string;
|
|
55
|
+
}
|
|
56
|
+
/** Load the glue and return the language server it carries (`opentide-wasm`'s `LanguageServer`). */
|
|
57
|
+
export declare function loadLanguageServer(glue: Glue, wasm?: WasmSource): Promise<LanguageServerModule>;
|
|
58
|
+
/**
|
|
59
|
+
* The end of a channel the server talks through: `self` inside a dedicated
|
|
60
|
+
* worker, or one port of a `MessageChannel`.
|
|
61
|
+
*/
|
|
62
|
+
export interface ServerPort {
|
|
63
|
+
postMessage(message: string): void;
|
|
64
|
+
addEventListener(type: "message", listener: (event: {
|
|
65
|
+
data: unknown;
|
|
66
|
+
}) => void): void;
|
|
67
|
+
start?(): void;
|
|
68
|
+
close?(): void;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Serve the language server on `port` until the client sends `exit`.
|
|
72
|
+
*
|
|
73
|
+
* Every message the port receives is one JSON-RPC message from the client and
|
|
74
|
+
* every message posted back is one from the server, each as JSON text with no
|
|
75
|
+
* `Content-Length` header (a message that arrives as an object is read as its
|
|
76
|
+
* JSON). Messages that arrive while `server` loads are handled in order once
|
|
77
|
+
* it has. If it fails to load, or the module stops, each request is answered
|
|
78
|
+
* with an error that says so, so the client's `initialize` fails at once
|
|
79
|
+
* instead of timing out; nothing is answered empty.
|
|
80
|
+
*
|
|
81
|
+
* After `exit` the port is closed: inside a dedicated worker, `self.close()`
|
|
82
|
+
* ends the worker. The promise resolves then.
|
|
83
|
+
*/
|
|
84
|
+
export declare function serveLanguageServer(port: ServerPort, server: LanguageServerModule | Promise<LanguageServerModule>): Promise<void>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM worker host for `opentide-wasm`.
|
|
3
|
+
*
|
|
4
|
+
* Artifact: wasm32-unknown-unknown + wasm-bindgen.
|
|
5
|
+
* Do not mix WASI into this module. vscode.dev uses a separate wasm32-wasip1 binary.
|
|
6
|
+
*
|
|
7
|
+
* The module runs two ways:
|
|
8
|
+
*
|
|
9
|
+
* - `serveLanguageServer` runs the language server itself (`opentide-lsp`, the
|
|
10
|
+
* server `--stdio` runs) inside a Web Worker. Each message the worker
|
|
11
|
+
* receives is one JSON-RPC message from the client, as JSON text with no
|
|
12
|
+
* `Content-Length` header, and each message it posts is one from the server:
|
|
13
|
+
* what `@codemirror/lsp-client`'s `Transport` sends, and what
|
|
14
|
+
* `createOpentideClient({ transport: "worker", worker })` speaks.
|
|
15
|
+
* - `loadHighlightModule` loads `highlight()` and `legend()` to call in the
|
|
16
|
+
* same thread: `createOpentideClient({ transport: "worker", module })`.
|
|
17
|
+
*/
|
|
18
|
+
function describe(glue) {
|
|
19
|
+
return typeof glue === "string" || glue instanceof URL ? String(glue) : "the glue module";
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Import the glue (unless it is a module already) and initialise it.
|
|
23
|
+
*
|
|
24
|
+
* For `wasm-bindgen --target web` glue, `wasm` is passed to its initialiser;
|
|
25
|
+
* when omitted, the glue fetches `opentide_wasm_bg.wasm` next to itself.
|
|
26
|
+
* `--target nodejs` glue loads its module on import and ignores `wasm`.
|
|
27
|
+
*/
|
|
28
|
+
async function initialise(glue, wasm) {
|
|
29
|
+
if (!glue) {
|
|
30
|
+
throw new Error("glueUrl is required");
|
|
31
|
+
}
|
|
32
|
+
// A URL known only at run time: a bundler is told not to resolve it.
|
|
33
|
+
const module = typeof glue === "string" || glue instanceof URL ? (await import(/* @vite-ignore */ String(glue))) : glue;
|
|
34
|
+
// `--target web` exports an async initialiser as the default export;
|
|
35
|
+
// `--target nodejs` is CommonJS, whose exports may sit on `default`.
|
|
36
|
+
if (typeof module.default === "function") {
|
|
37
|
+
await module.default(wasm === undefined ? undefined : { module_or_path: wasm });
|
|
38
|
+
}
|
|
39
|
+
return (typeof module.highlight === "function" ? module : module.default);
|
|
40
|
+
}
|
|
41
|
+
/** Load the glue and return `highlight()` and `legend()`. */
|
|
42
|
+
export async function loadHighlightModule(glueUrl, wasm) {
|
|
43
|
+
const exports = await initialise(glueUrl, wasm);
|
|
44
|
+
if (typeof exports?.highlight !== "function" || typeof exports.legend !== "function") {
|
|
45
|
+
throw new Error(`${describe(glueUrl)} is not opentide-wasm glue: it exports no highlight() and legend()`);
|
|
46
|
+
}
|
|
47
|
+
const { highlight, legend } = exports;
|
|
48
|
+
return {
|
|
49
|
+
highlight: (language_id, bytes) => highlight(language_id, bytes),
|
|
50
|
+
legend: () => [...legend()],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** Load the glue and return the language server it carries (`opentide-wasm`'s `LanguageServer`). */
|
|
54
|
+
export async function loadLanguageServer(glue, wasm) {
|
|
55
|
+
const exports = await initialise(glue, wasm);
|
|
56
|
+
const Server = exports?.LanguageServer;
|
|
57
|
+
const version = exports?.version;
|
|
58
|
+
if (typeof Server !== "function" || typeof version !== "function") {
|
|
59
|
+
throw new Error(`${describe(glue)} is not opentide-wasm glue with the language server: it exports no LanguageServer`);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
createSession: () => new Server(),
|
|
63
|
+
version: () => String(version()),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** The id of a JSON-RPC request (a message with a method and an id), if `text` is one. */
|
|
67
|
+
function requestId(text) {
|
|
68
|
+
try {
|
|
69
|
+
const message = JSON.parse(text);
|
|
70
|
+
const { id } = message;
|
|
71
|
+
return typeof message.method === "string" && (typeof id === "string" || typeof id === "number") ? id : undefined;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Serve the language server on `port` until the client sends `exit`.
|
|
79
|
+
*
|
|
80
|
+
* Every message the port receives is one JSON-RPC message from the client and
|
|
81
|
+
* every message posted back is one from the server, each as JSON text with no
|
|
82
|
+
* `Content-Length` header (a message that arrives as an object is read as its
|
|
83
|
+
* JSON). Messages that arrive while `server` loads are handled in order once
|
|
84
|
+
* it has. If it fails to load, or the module stops, each request is answered
|
|
85
|
+
* with an error that says so, so the client's `initialize` fails at once
|
|
86
|
+
* instead of timing out; nothing is answered empty.
|
|
87
|
+
*
|
|
88
|
+
* After `exit` the port is closed: inside a dedicated worker, `self.close()`
|
|
89
|
+
* ends the worker. The promise resolves then.
|
|
90
|
+
*/
|
|
91
|
+
export function serveLanguageServer(port, server) {
|
|
92
|
+
let session;
|
|
93
|
+
let failure;
|
|
94
|
+
let waiting = [];
|
|
95
|
+
let finished;
|
|
96
|
+
const done = new Promise((resolve) => (finished = resolve));
|
|
97
|
+
const refuse = (text) => {
|
|
98
|
+
const id = requestId(text);
|
|
99
|
+
if (id !== undefined) {
|
|
100
|
+
port.postMessage(JSON.stringify({ jsonrpc: "2.0", id, error: { code: -32603, message: failure } }));
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const handle = (text) => {
|
|
104
|
+
if (!session || failure !== undefined)
|
|
105
|
+
return refuse(text);
|
|
106
|
+
let replies;
|
|
107
|
+
try {
|
|
108
|
+
replies = session.handle(text);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
failure = `the opentide language server stopped: ${error instanceof Error ? error.message : String(error)}`;
|
|
112
|
+
return refuse(text);
|
|
113
|
+
}
|
|
114
|
+
for (const reply of replies)
|
|
115
|
+
port.postMessage(reply);
|
|
116
|
+
if (session.closed) {
|
|
117
|
+
session.free?.();
|
|
118
|
+
session = undefined;
|
|
119
|
+
failure = "the opentide language server has exited";
|
|
120
|
+
port.close?.();
|
|
121
|
+
finished();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
port.addEventListener("message", (event) => {
|
|
125
|
+
const text = typeof event.data === "string" ? event.data : JSON.stringify(event.data);
|
|
126
|
+
if (waiting)
|
|
127
|
+
waiting.push(text);
|
|
128
|
+
else
|
|
129
|
+
handle(text);
|
|
130
|
+
});
|
|
131
|
+
port.start?.();
|
|
132
|
+
Promise.resolve(server)
|
|
133
|
+
.then((module) => module.createSession())
|
|
134
|
+
.then((created) => {
|
|
135
|
+
session = created;
|
|
136
|
+
}, (error) => {
|
|
137
|
+
failure = `the opentide language server did not load: ${error instanceof Error ? error.message : String(error)}`;
|
|
138
|
+
})
|
|
139
|
+
.then(() => {
|
|
140
|
+
const queued = waiting ?? [];
|
|
141
|
+
waiting = undefined;
|
|
142
|
+
for (const text of queued)
|
|
143
|
+
handle(text);
|
|
144
|
+
});
|
|
145
|
+
return done;
|
|
146
|
+
}
|
package/package.json
CHANGED
|
@@ -1 +1,36 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@opentide/lsp-worker",
|
|
3
|
+
"version": "1.0.0-rc.4",
|
|
4
|
+
"description": "wasm32-unknown-unknown worker: the opentide language server and highlight(). Do not mix WASI.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"NOTICE"
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/opentidehq/opentide.git",
|
|
23
|
+
"directory": "packages/lsp-worker"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public",
|
|
27
|
+
"provenance": true
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc -p tsconfig.json",
|
|
31
|
+
"prepack": "npm run build"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"typescript": "5.9.3"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/README.md
DELETED