vscode-ws-jsonrpc
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -5,2 +5,6 @@ # Changelog | ||
## [2.0.2] - 2022-01-24 | ||
- Gracefully handle JSON.parse errors #[455](https://github.com/TypeFox/monaco-languageclient/pull/455) | ||
## [2.0.1] - 2022-12-01 | ||
@@ -7,0 +11,0 @@ |
@@ -57,4 +57,13 @@ /* -------------------------------------------------------------------------------------------- | ||
else if (this.state === 'listening') { | ||
const data = JSON.parse(message); | ||
this.callback(data); | ||
try { | ||
const data = JSON.parse(message); | ||
this.callback(data); | ||
} | ||
catch (err) { | ||
const error = { | ||
name: '' + 400, | ||
message: `Error during message parsing, reason = ${typeof err === 'object' ? err.message : 'unknown'}` | ||
}; | ||
this.fireError(error); | ||
} | ||
} | ||
@@ -61,0 +70,0 @@ } |
{ | ||
"name": "vscode-ws-jsonrpc", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "VSCode JSON RPC over WebSocket", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -5,11 +5,17 @@ # VSCode WebSocket JSON RPC | ||
See the following example code how to use this library or take a look of the `monaco-languageclient` and `vscode-ws-jsonrpc` examples here: | ||
- [client](https://github.com/TypeFox/monaco-languageclient/blob/main/packages/examples/client/) | ||
- [server](https://github.com/TypeFox/monaco-languageclient/blob/main/packages/examples/server/) | ||
## Client side connection handling | ||
```ts | ||
import * as rpc from 'vscode-ws-jsonrpc'; | ||
import { MessageConnection, NotificationType } from 'vscode-jsonrpc'; | ||
import { listen } from 'vscode-ws-jsonrpc'; | ||
const webSocket = new WebSocket('ws://www.example.com/socketserver'); | ||
rpc.listen({ | ||
listen({ | ||
webSocket, | ||
onConnection: (connection: rpc.MessageConnection) => { | ||
onConnection: (connection: MessageConnection) => { | ||
const notification = new rpc.NotificationType<string, void>('testNotification'); | ||
@@ -25,12 +31,11 @@ connection.listen(); | ||
```ts | ||
import * as rpc from 'vscode-ws-jsonrpc'; | ||
import { createWebSocketConnection, ConsoleLogger, IWebSocket } from 'vscode-ws-jsonrpc'; | ||
import { NotificationType } from 'vscode-languageserver'; | ||
const socket: rpc.IWebSocket; // open the web socket | ||
const reader = new rpc.WebSocketMessageReader(socket); | ||
const writer = new rpc.WebSocketMessageWriter(socket); | ||
const logger = new rpc.ConsoleLogger(); | ||
const connection = rpc.createMessageConnection(reader, writer, logger); | ||
const notification = new rpc.NotificationType<string, void>('testNotification'); | ||
const socket: IWebSocket; // open the web socket | ||
const logger = new ConsoleLogger(); | ||
const connection = createWebSocketConnection(socket, logger); | ||
const notification = new NotificationType<string, void>('testNotification'); | ||
connection.onNotification(notification, (param: string) => { | ||
console.log(param); // This prints Hello World | ||
console.log(param); // This prints Hello World | ||
}); | ||
@@ -44,12 +49,13 @@ | ||
```ts | ||
import * as rpc from 'vscode-ws-jsonrpc'; | ||
import * as server from 'vscode-ws-jsonrpc/server'; | ||
import { IWebSocket, WebSocketMessageReader, WebSocketMessageWriter } from 'vscode-ws-jsonrpc'; | ||
import { createConnection, createServerProcess, forward } from 'vscode-ws-jsonrpc/server'; | ||
import { Message } from 'vscode-languageserver'; | ||
const socket: rpc.IWebSocket; // open the web socket | ||
const reader = new rpc.WebSocketMessageReader(socket); | ||
const writer = new rpc.WebSocketMessageWriter(socket); | ||
const socketConnection = server.createConnection(reader, writer, () => socket.dispose()) | ||
const serverConnection = server.createServerProcess('Example', 'node', ['example.js']); | ||
server.forward(socketConnection, serverConnection, message => { | ||
if (rpc.isNotificationMessage(message)) { | ||
const socket: IWebSocket; // open the web socket | ||
const reader = new WebSocketMessageReader(socket); | ||
const writer = new WebSocketMessageWriter(socket); | ||
const socketConnection = createConnection(reader, writer, () => socket.dispose()) | ||
const serverConnection = createServerProcess('Example', 'node', ['example.js']); | ||
forward(socketConnection, serverConnection, message => { | ||
if (Message.isNotification(message)) { | ||
if (message.method === 'testNotification') { | ||
@@ -65,2 +71,2 @@ // handle the test notification | ||
[MIT](https://github.com/TypeFox/vscode-ws-jsonrpc/blob/main/packages/vscode-ws-jsonrpc/License.txt) | ||
[MIT](https://github.com/TypeFox/monaco-languageclient/blob/main/packages/vscode-ws-jsonrpc/License.txt) |
@@ -63,4 +63,12 @@ /* -------------------------------------------------------------------------------------------- | ||
} else if (this.state === 'listening') { | ||
const data = JSON.parse(message); | ||
this.callback!(data); | ||
try { | ||
const data = JSON.parse(message); | ||
this.callback!(data); | ||
} catch (err) { | ||
const error: Error = { | ||
name: '' + 400, | ||
message: `Error during message parsing, reason = ${typeof err === 'object' ? (err as any).message : 'unknown'}` | ||
}; | ||
this.fireError(error); | ||
} | ||
} | ||
@@ -67,0 +75,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57929
745
69