-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move code from vscode-ws-jsonrpc into Theia
Signed-off-by: Colin Grant <[email protected]>
- Loading branch information
Colin Grant
committed
Oct 14, 2021
1 parent
5d5613c
commit 058d32c
Showing
30 changed files
with
275 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 Ericsson and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { MessageReader, MessageWriter, Message, createMessageConnection, Logger, MessageConnection } from 'vscode-languageserver-protocol'; | ||
import { Disposable, DisposableCollection } from '../../common'; | ||
import { WebSocketMessageReader, WebSocketMessageWriter } from './socket-message-handlers'; | ||
import { IWebSocket } from './web-socket-channel'; | ||
|
||
// Copied from https://github.com/CodinGame/monaco-jsonrpc/blob/e3eea9123da2cc11845c409bcfae8e44b7d3a0e6/src/server/connection.ts | ||
export interface IConnection extends Disposable { | ||
readonly reader: MessageReader; | ||
readonly writer: MessageWriter; | ||
forward(to: IConnection, map?: (message: Message) => Message): void; | ||
onClose(callback: () => void): Disposable; | ||
} | ||
|
||
// Copied from https://github.com/CodinGame/monaco-jsonrpc/blob/e3eea9123da2cc11845c409bcfae8e44b7d3a0e6/src/socket/connection.ts | ||
export function createWebSocketConnection(socket: IWebSocket, logger: Logger): MessageConnection { | ||
const messageReader = new WebSocketMessageReader(socket); | ||
const messageWriter = new WebSocketMessageWriter(socket); | ||
const connection = createMessageConnection(messageReader, messageWriter, logger); | ||
connection.onClose(() => connection.dispose()); | ||
return connection; | ||
} | ||
|
||
// Copied from https://github.com/CodinGame/monaco-jsonrpc/blob/e3eea9123da2cc11845c409bcfae8e44b7d3a0e6/src/server/connection.ts | ||
export function createConnection<T extends {}>(reader: MessageReader, writer: MessageWriter, onDispose: () => void, | ||
extensions: T = {} as T): IConnection & T { | ||
const disposeOnClose = new DisposableCollection(); | ||
reader.onClose(() => disposeOnClose.dispose()); | ||
writer.onClose(() => disposeOnClose.dispose()); | ||
return { | ||
reader, writer, | ||
forward(to: IConnection, map: (message: Message) => Message = message => message): void { | ||
reader.listen(input => { | ||
const output = map(input); | ||
to.writer.write(output); | ||
}); | ||
}, | ||
onClose(callback: () => void): Disposable { | ||
return disposeOnClose.push(Disposable.create(callback)); | ||
}, | ||
dispose: () => onDispose(), | ||
...extensions | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/core/src/common/messaging/socket-message-handlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 Ericsson and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { AbstractMessageReader, MessageReader, DataCallback, AbstractMessageWriter, MessageWriter, Message, Disposable } from 'vscode-languageserver-protocol'; | ||
import { IWebSocket } from './web-socket-channel'; | ||
|
||
// Copied from https://github.com/CodinGame/monaco-jsonrpc/blob/e3eea9123da2cc11845c409bcfae8e44b7d3a0e6/src/socket/reader.ts | ||
export class WebSocketMessageReader extends AbstractMessageReader implements MessageReader { | ||
|
||
protected state: 'initial' | 'listening' | 'closed' = 'initial'; | ||
protected callback: DataCallback | undefined; | ||
protected readonly events: { message?: string, error?: unknown }[] = []; | ||
|
||
constructor(protected readonly socket: IWebSocket) { | ||
super(); | ||
this.socket.onMessage(message => | ||
this.readMessage(message) | ||
); | ||
this.socket.onError(error => | ||
this.fireError(error) | ||
); | ||
this.socket.onClose((code, reason) => { | ||
if (code !== 1000) { | ||
const error: Error = { | ||
name: '' + code, | ||
message: `Error during socket reconnect: code = ${code}, reason = ${reason}` | ||
}; | ||
this.fireError(error); | ||
} | ||
this.fireClose(); | ||
}); | ||
} | ||
|
||
listen(callback: DataCallback): Disposable { | ||
if (this.state === 'initial') { | ||
this.state = 'listening'; | ||
this.callback = callback; | ||
while (this.events.length !== 0) { | ||
const event = this.events.pop()!; | ||
if (event.message) { | ||
this.readMessage(event.message); | ||
} else if (event.error) { | ||
this.fireError(event.error); | ||
} else { | ||
this.fireClose(); | ||
} | ||
} | ||
} | ||
return { | ||
dispose: () => { | ||
if (this.callback === callback) { | ||
this.callback = undefined; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
protected readMessage(message: string): void { | ||
if (this.state === 'initial') { | ||
this.events.splice(0, 0, { message }); | ||
} else if (this.state === 'listening') { | ||
const data = JSON.parse(message); | ||
this.callback!(data); | ||
} | ||
} | ||
|
||
protected fireError(error: unknown): void { | ||
if (this.state === 'initial') { | ||
this.events.splice(0, 0, { error }); | ||
} else if (this.state === 'listening') { | ||
super.fireError(error); | ||
} | ||
} | ||
|
||
protected fireClose(): void { | ||
if (this.state === 'initial') { | ||
this.events.splice(0, 0, {}); | ||
} else if (this.state === 'listening') { | ||
super.fireClose(); | ||
} | ||
this.state = 'closed'; | ||
} | ||
|
||
} | ||
|
||
// Copied from https://github.com/CodinGame/monaco-jsonrpc/blob/e3eea9123da2cc11845c409bcfae8e44b7d3a0e6/src/socket/writer.ts | ||
export class WebSocketMessageWriter extends AbstractMessageWriter implements MessageWriter { | ||
|
||
protected errorCount = 0; | ||
|
||
constructor(protected readonly socket: IWebSocket) { | ||
super(); | ||
} | ||
|
||
end(): void { | ||
} | ||
|
||
async write(msg: Message): Promise<void> { | ||
try { | ||
const content = JSON.stringify(msg); | ||
this.socket.send(content); | ||
} catch (e) { | ||
this.errorCount++; | ||
this.fireError(e, msg, this.errorCount); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.