swls-wasm 0.3.2-alpha.1 → 0.3.2-alpha.3

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 CHANGED
@@ -1,86 +1,67 @@
1
- # SWLS-Web
1
+ # swls-wasm
2
2
 
3
- WASM bindings for the semantic web language server.
3
+ The [Semantic Web Language Server](https://github.com/SemanticWebLanguageServer/swls)
4
+ (Turtle, TriG, SPARQL, JSON-LD) compiled to WebAssembly, plus thin helpers for
5
+ running it as a worker. Built with `wasm-pack --target web`, so it loads in
6
+ browsers, bundlers, Web Workers and Node without any bundler plugins.
4
7
 
5
- As SWLS is not yet published, this package expects SWLS to be present in the parent directory.
8
+ Both directions exchange whole JSON-RPC messages over `postMessage` with **no
9
+ `Content-Length` framing**.
6
10
 
7
- ## 🚴 Usage
11
+ ## Browser / bundler — `createSwlsWorker()`
8
12
 
9
- ### 🛠️ Build with `wasm-pack build`
13
+ The easiest path: get a ready-to-use Web Worker. Works with Vite, webpack, etc.
14
+ (the bundler picks up the worker and its `.wasm` automatically).
10
15
 
11
- ```
12
- wasm-pack build
13
- ```
14
-
15
- ### 🔬 Test in Headless Browsers with `wasm-pack test`
16
+ ```js
17
+ import { createSwlsWorker } from "swls-wasm/client";
16
18
 
19
+ const worker = createSwlsWorker();
20
+ // hand `worker` to your editor's LSP client (e.g. swls-codemirror, monaco)
17
21
  ```
18
- wasm-pack test --headless --firefox
19
- ```
20
-
21
- ### 🎁 Publish to NPM with `wasm-pack publish`
22
-
23
- ```
24
- wasm-pack publish
25
- ```
26
-
27
-
28
- ## Custom LSP Requests
29
-
30
- The WASM LSP server sends custom requests **from server to client** that the host must handle. All requests use `params: { url: string }`.
31
22
 
32
- If a handler is not implemented, the server falls back to a sensible default.
23
+ The worker emits `{ jsonrpc: "2.0", method: "swls/ready" }` once the wasm has
24
+ finished initializing.
33
25
 
34
- ### `custom/readFile`
26
+ ## From a CDN — no build step
35
27
 
36
- Reads the contents of a file.
28
+ `worker.js` and its sibling `swls_wasm_bg.wasm` are both published, so a worker
29
+ can be loaded straight from a CDN:
37
30
 
38
- ```ts
39
- params: { url: string }
40
- result: { content: string } | { error: string }
31
+ ```js
32
+ const worker = new Worker(
33
+ "https://cdn.jsdelivr.net/npm/swls-wasm@0.3/worker.js",
34
+ { type: "module" },
35
+ );
41
36
  ```
42
37
 
43
- ### `custom/readDir`
38
+ The worker resolves `swls_wasm_bg.wasm` relative to its own URL, so it is fetched
39
+ from the same CDN path. Cross-origin module workers need permissive CORS (jsDelivr
40
+ and unpkg send it); Safari is stricter about cross-origin worker scripts, so if you
41
+ need to support it, load a small same-origin bootstrap that `import()`s the CDN URL.
44
42
 
45
- Lists the entries of a directory.
43
+ ## Custom host `createSwlsServer()`
46
44
 
47
- ```ts
48
- params: { url: string }
49
- result: Array<{ name: string; path: string; is_dir: boolean }>
50
- // fallback: returns null/error → treated as empty
51
- ```
52
-
53
- ### `custom/isFile`
54
-
55
- Returns whether the given URL refers to a file.
56
-
57
- ```ts
58
- params: { url: string }
59
- result: boolean
60
- // fallback: true when URL does not end with '/'
61
- ```
45
+ For hosts that aren't a DOM `Worker` (a Node `worker_threads` worker, the VS Code
46
+ web-extension host, …): wire the server to your own transport. `post` receives
47
+ each outgoing message (already parsed); feed incoming messages (object or string)
48
+ into the returned function.
62
49
 
63
- ### `custom/isDir`
50
+ ```js
51
+ import { createSwlsServer } from "swls-wasm";
64
52
 
65
- Returns whether the given URL refers to a directory.
53
+ const feed = await createSwlsServer((message) => sendToClient(message), {
54
+ debug: (line) => console.log(line),
55
+ // Optional: override how the wasm is loaded. Defaults to fetching the sibling
56
+ // `swls_wasm_bg.wasm`; pass bytes/URL when the host can't fetch that itself
57
+ // (e.g. Node, which can't `fetch` a `file://` URL).
58
+ wasm: undefined,
59
+ });
66
60
 
67
- ```ts
68
- params: { url: string }
69
- result: boolean
70
- // fallback: true when URL ends with '/'
61
+ feed({ jsonrpc: "2.0", id: 1, method: "initialize", params: { /* … */ } });
71
62
  ```
72
63
 
73
- ### `custom/canonicalize`
74
-
75
- Resolves a URL to its canonical form.
76
-
77
- ```ts
78
- params: { url: string }
79
- result: { url: string }
80
- // fallback: returns the input URL unchanged
81
- ```
82
-
83
- ## License
84
-
85
- * MIT license ([LICENSE](LICENSE) or http://opensource.org/licenses/MIT)
64
+ ## Low-level — `WasmLsp`
86
65
 
66
+ `WasmLsp` (and the wasm `init` default export) are also re-exported for full
67
+ control. `createSwlsServer` is just a thin wrapper around them.
package/client.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Creates a Web Worker running the swls language server (browser/web hosts).
3
+ *
4
+ * The returned worker speaks whole JSON-RPC messages over `postMessage` (no
5
+ * `Content-Length` framing) and emits a `{ method: "swls/ready" }` notification
6
+ * once the wasm has finished initializing.
7
+ *
8
+ * @param options Extra worker options; `type` is always `"module"`.
9
+ */
10
+ export function createSwlsWorker(options?: Omit<WorkerOptions, "type">): Worker;
package/client.js ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Creates a Web Worker running the swls language server (browser/web hosts).
3
+ *
4
+ * The returned worker speaks whole JSON-RPC messages over `postMessage` (no
5
+ * `Content-Length` framing) and emits a `{ method: "swls/ready" }` notification
6
+ * once the wasm has finished initializing.
7
+ *
8
+ * Kept in its own entry (`swls-wasm/client`) rather than the package root so
9
+ * that Node/bundler consumers importing `swls-wasm` never pull in a nested
10
+ * `Worker`/wasm chunk they don't use.
11
+ *
12
+ * @param {WorkerOptions} [options] Extra worker options; `type` is always
13
+ * `"module"`. (Kept after the spread so bundlers can statically read `type`.)
14
+ * @returns {Worker}
15
+ */
16
+ export function createSwlsWorker(options) {
17
+ return new Worker(new URL("./worker.js", import.meta.url), {
18
+ ...options,
19
+ type: "module",
20
+ });
21
+ }
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default, initSync, WasmLsp, start_wasm_lsp } from "./swls_wasm.js";
2
+ export { createSwlsServer } from "./pump.js";
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { default, initSync, WasmLsp, start_wasm_lsp } from "./swls_wasm.js";
2
+ export { createSwlsServer } from "./pump.js";
package/package.json CHANGED
@@ -4,17 +4,40 @@
4
4
  "collaborators": [
5
5
  "ajuvercr <arthur.vercruysse@outlook.com>"
6
6
  ],
7
- "version": "0.3.2-alpha.1",
7
+ "version": "0.3.2-alpha.3",
8
8
  "files": [
9
9
  "swls_wasm_bg.wasm",
10
10
  "swls_wasm.js",
11
- "swls_wasm_bg.js",
12
- "swls_wasm.d.ts"
11
+ "swls_wasm.d.ts",
12
+ "index.js",
13
+ "index.d.ts",
14
+ "pump.js",
15
+ "pump.d.ts",
16
+ "worker.js",
17
+ "client.js",
18
+ "client.d.ts"
13
19
  ],
14
- "main": "swls_wasm.js",
15
- "types": "swls_wasm.d.ts",
20
+ "main": "index.js",
21
+ "types": "index.d.ts",
16
22
  "sideEffects": [
17
- "./swls_wasm.js",
18
- "./snippets/*"
19
- ]
23
+ "./worker.js"
24
+ ],
25
+ "module": "index.js",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./index.d.ts",
29
+ "default": "./index.js"
30
+ },
31
+ "./client": {
32
+ "types": "./client.d.ts",
33
+ "default": "./client.js"
34
+ },
35
+ "./worker": "./worker.js",
36
+ "./swls_wasm.js": {
37
+ "types": "./swls_wasm.d.ts",
38
+ "default": "./swls_wasm.js"
39
+ },
40
+ "./swls_wasm_bg.wasm": "./swls_wasm_bg.wasm",
41
+ "./package.json": "./package.json"
42
+ }
20
43
  }
package/pump.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Boots the swls wasm LSP and wires it to a host transport.
3
+ *
4
+ * Host-agnostic: the caller decides how outgoing messages are delivered
5
+ * (`post`) and feeds incoming messages into the returned function. Both
6
+ * directions exchange whole JSON-RPC messages with no `Content-Length`
7
+ * framing — `post` receives already-parsed objects, and the returned feed
8
+ * accepts either a parsed object or a raw JSON string.
9
+ *
10
+ * @param post Called with each outgoing JSON-RPC message (already parsed).
11
+ * @param options.debug The server's debug-log sink.
12
+ * @param options.wasm Overrides the module/URL forwarded to the wasm `init()`
13
+ * (defaults to the sibling `swls_wasm_bg.wasm`); pass it when the host cannot
14
+ * fetch that sibling itself (e.g. Node, which can't `fetch` a `file://` URL).
15
+ * @returns Resolves once wasm is initialized to a function that feeds one
16
+ * incoming message (object or string) to the server.
17
+ */
18
+ export function createSwlsServer(
19
+ post: (message: unknown) => void,
20
+ options?: {
21
+ debug?: (message: string) => void;
22
+ wasm?: string | URL | Request | BufferSource | WebAssembly.Module;
23
+ },
24
+ ): Promise<(data: unknown) => void>;
package/pump.js ADDED
@@ -0,0 +1,36 @@
1
+ import init, { WasmLsp } from "./swls_wasm.js";
2
+
3
+ /**
4
+ * Boots the swls wasm LSP and wires it to a host transport.
5
+ *
6
+ * Host-agnostic: the caller decides how outgoing messages are delivered
7
+ * (`post`) and feeds incoming messages into the returned function. Both
8
+ * directions exchange whole JSON-RPC messages with no `Content-Length`
9
+ * framing — `post` receives already-parsed objects, and the returned feed
10
+ * accepts either a parsed object or a raw JSON string.
11
+ *
12
+ * Used directly by non-DOM hosts (e.g. a VS Code `worker_threads` server or
13
+ * the web-extension host); browser consumers get this wrapped in a Worker via
14
+ * {@link createSwlsWorker} (see `swls-wasm/client`).
15
+ *
16
+ * @param {(message: unknown) => void} post
17
+ * Called with each outgoing JSON-RPC message (already parsed).
18
+ * @param {{
19
+ * debug?: (message: string) => void,
20
+ * wasm?: string | URL | Request | BufferSource | WebAssembly.Module,
21
+ * }} [options]
22
+ * `debug` is the server's debug-log sink. `wasm` overrides the module/URL
23
+ * forwarded to the wasm `init()` (defaults to the sibling
24
+ * `swls_wasm_bg.wasm`); pass it when the host cannot fetch that sibling
25
+ * itself (e.g. Node, which can't `fetch` a `file://` URL).
26
+ * @returns {Promise<(data: unknown) => void>}
27
+ * Resolves once wasm is initialized to a function that feeds one incoming
28
+ * message (object or string) to the server.
29
+ */
30
+ export async function createSwlsServer(post, options = {}) {
31
+ const { debug, wasm } = options;
32
+ await init(wasm === undefined ? undefined : { module_or_path: wasm });
33
+ const lsp = new WasmLsp((message) => post(JSON.parse(message)), debug);
34
+ return (data) =>
35
+ lsp.send(typeof data === "string" ? data : JSON.stringify(data));
36
+ }
package/swls_wasm.d.ts CHANGED
@@ -24,3 +24,46 @@ export class WasmLsp {
24
24
  }
25
25
 
26
26
  export function start_wasm_lsp(): Promise<void>;
27
+
28
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
29
+
30
+ export interface InitOutput {
31
+ readonly memory: WebAssembly.Memory;
32
+ readonly __wbg_wasmlsp_free: (a: number, b: number) => void;
33
+ readonly start_wasm_lsp: () => void;
34
+ readonly wasmlsp_new: (a: any, b: number) => number;
35
+ readonly wasmlsp_send: (a: number, b: number, c: number) => void;
36
+ readonly wasm_bindgen__convert__closures_____invoke__h5d8169a9c6a0716a: (a: number, b: number, c: any) => [number, number];
37
+ readonly wasm_bindgen__convert__closures_____invoke__h0a08a9b6706aa208: (a: number, b: number, c: any, d: any) => void;
38
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
39
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
40
+ readonly __wbindgen_exn_store: (a: number) => void;
41
+ readonly __externref_table_alloc: () => number;
42
+ readonly __wbindgen_externrefs: WebAssembly.Table;
43
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
44
+ readonly __wbindgen_destroy_closure: (a: number, b: number) => void;
45
+ readonly __externref_table_dealloc: (a: number) => void;
46
+ readonly __wbindgen_start: () => void;
47
+ }
48
+
49
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
50
+
51
+ /**
52
+ * Instantiates the given `module`, which can either be bytes or
53
+ * a precompiled `WebAssembly.Module`.
54
+ *
55
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
56
+ *
57
+ * @returns {InitOutput}
58
+ */
59
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
60
+
61
+ /**
62
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
63
+ * for everything else, calls `WebAssembly.instantiate` directly.
64
+ *
65
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
66
+ *
67
+ * @returns {Promise<InitOutput>}
68
+ */
69
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;