json-rpc-interface
v1.0.1
Published
Create a JSON RPC server to communicate with other processes. Can be used to communicate with text editors using LSP.
Readme
JSON-RPC-Interface
A library for communicating with other processes as a JSON RPC server.
Can be used to communicate with text editors using LSP
How to use:
Simple to use interface, only 4 functions that you need to know:
onRequest
onRequest events allow you to send a return value back to the process.
jsonRpc.onRequest(name, handler)
name: string - the name of the request handler: function - The function will receieve a params object and can return a value back to the requester The handler function may be async or return a promise.
onNotification
Similar to onRequest, except notifier is not expecting a return value
jsonRpc.onNotification(name, handler)
sendRequest
An async function that sends a request to the client process and returns a promise that will resolve with the response from the client.
const response = await jsonRpc.sendRequest('request-name', {data: "request-params"})
sendNotification
Similar to sendRequest, however there is no response to wait for, so this function has no return value.
jsonRpc.sendNotification('notification-name', {data: "notification-params"})
Example:
Here is quick example using this library to create an LSP server for text editors.
// Create the server using stdin and stdout as the input and output streams
const jsonRpc = new JsonRpcInterface({ inputStream: process.stdin, outputStream: process.stdout })
jsonRpc.onRequest("initialize", (params) => {
return {
serverInfo: {
name: "My LSP Server",
version: "1.0.0",
},
capabilities: {
textDocumentSync: 1, // full text sync
},
}
})
// use this function to hook into initalized event
jsonRpc.onNotification("initialized", () => {
})
// handle shutdown and exit requests and notificaitons
jsonRpc.onRequest("shutdown", () => {
return null
})
jsonRpc.onNotification("exit", () => {
process.exit(0)
})
// handle LSP file open and change notifications
jsonRpc.onNotification("textDocument/didOpen", (params) => {
})
jsonRpc.onNotification("textDocument/didChange", (params) => {
})
}
100% Test coverage for all files
$ npm test
RUN v3.2.4 /Users/niels/dev/src/json-rpc-interface
Coverage enabled with v8
✓ src/LspWriter.test.js (9 tests) 3ms
✓ src/LspReader.test.js (33 tests) 5ms
✓ src/JsonRpcInterface.test.js (27 tests) 7ms
Test Files 3 passed (3)
Tests 69 passed (69)
Start at 13:35:13
Duration 262ms (transform 40ms, setup 0ms, collect 67ms, tests 15ms, environment 0ms, prepare 114ms)
% Coverage report from v8
------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
json-rpc-interface | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
json-rpc-interface/src | 100 | 100 | 100 | 100 |
JsonRpcInterface.js | 100 | 100 | 100 | 100 |
LspReader.js | 100 | 100 | 100 | 100 |
LspWriter.js | 100 | 100 | 100 | 100 |
constants.js | 100 | 100 | 100 | 100 |
