From 6d5323918b85cb8a351f75b0ff3700e91aff4a4d Mon Sep 17 00:00:00 2001 From: Andy Bunce Date: Thu, 20 Nov 2025 22:25:52 +0000 Subject: [PATCH] [add] base64 handling --- README.md | 2 +- webapp/lsp/jsonrpc.xqm | 15 --------------- webapp/lsp/lsp-ws.xqm | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index cdde9cf..b5787ec 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ It written for BaseX 12.0 and uses the [BaseX websocket](https://docs.basex.org/ RFC 6455](https://www.rfc-editor.org/rfc/rfc6455.html) # Demo -The demos expect `Docker` or `Podman` on the local machine. This is only a requirement for the demos. +The demo makes use of `Docker` or `Podman` for ease of setup. This is not a requirement to use the software. 1 Clone this repo 2. Run `docker compose up -d` The LSP server shall now running on port 3000 (edit `compose.yaml` to change port) diff --git a/webapp/lsp/jsonrpc.xqm b/webapp/lsp/jsonrpc.xqm index f673186..2258682 100644 --- a/webapp/lsp/jsonrpc.xqm +++ b/webapp/lsp/jsonrpc.xqm @@ -33,21 +33,6 @@ declare variable $rpc:Methods:=map:merge(( $lsp-text:methods )); -(:~ return map if $msg is jsonrpc else empty :) -declare -function rpc:parse($msg as xs:string) -as map(*)? -{ - try { - let $json:=parse-json($msg) - return if($json?jsonrpc eq "2.0" and exists($json?method)) - then $json - else () - } catch *{ - () - } - -}; (:~ send reply to $json message get functions for methods diff --git a/webapp/lsp/lsp-ws.xqm b/webapp/lsp/lsp-ws.xqm index 86f31c4..3de7de6 100644 --- a/webapp/lsp/lsp-ws.xqm +++ b/webapp/lsp/lsp-ws.xqm @@ -39,14 +39,15 @@ declare function lsp-ws:message( $message as xs:string ) as empty-sequence() { - - let $json := rpc:parse($message) + + let $json := lsp-ws:parse($message) return if(exists($json)) then rpc:reply($json) else message($message,"bad RPC: ") }; + (:~ : Closes a WebSocket connection. :) @@ -55,3 +56,31 @@ declare function lsp-ws:close() as empty-sequence() { rpc:admin-log("CLOSE","🥀") }; + +(:~ return map if $msg is jsonrpc else empty :) +declare +function lsp-ws:parse($msg as xs:string) +as map(*)? +{ + try { + let $jtext:=if(starts-with($msg,"{")) + then $msg + else lsp-ws:decode($msg) + + let $json:=parse-json($jtext) + return if($json?jsonrpc eq "2.0" and exists($json?method)) + then $json + else () + } catch *{ + () + } + +}; + +(:~ decode +"Q29udGVudC1MZW5ndGg6IDU4NDUNCg0K" -> "Content-Length: 5845" +:) +declare function lsp-ws:decode($basex64 as xs:string) +as xs:string{ + xs:base64Binary($basex64) => bin:to-octets() => codepoints-to-string() +};