Skip to content

Commit

Permalink
Move code from vscode-ws-jsonrpc into Theia
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Grant <[email protected]>
  • Loading branch information
Colin Grant committed Oct 14, 2021
1 parent 5d5613c commit db3b907
Show file tree
Hide file tree
Showing 32 changed files with 275 additions and 63 deletions.
1 change: 0 additions & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export class SomeClass {

### List of re-exported packages

- [`@codingame/monaco-jsonrpc@^0.3.1`](https://www.npmjs.com/package/@codingame/monaco-jsonrpc)
- [`@phosphor/algorithm@1`](https://www.npmjs.com/package/@phosphor/algorithm)
- [`@phosphor/commands@1`](https://www.npmjs.com/package/@phosphor/commands)
- [`@phosphor/coreutils@1`](https://www.npmjs.com/package/@phosphor/coreutils)
Expand Down
5 changes: 1 addition & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@
"uuid": "^8.3.2",
"vscode-languageserver-protocol": "^3.16.0",
"vscode-languageserver-types": "^3.16.0",
"vscode-jsonrpc": "^6.0.0",
"vscode-uri": "^2.1.1",
"@codingame/monaco-jsonrpc": "^0.3.1",
"ws": "^7.1.2",
"yargs": "^15.3.1"
},
Expand Down Expand Up @@ -102,8 +100,7 @@
"react-virtualized",
"vscode-languageserver-protocol",
"vscode-languageserver-types",
"vscode-uri",
"@codingame/monaco-jsonrpc"
"vscode-uri"
],
"export =": [
"dompurify as DOMPurify",
Expand Down
1 change: 0 additions & 1 deletion packages/core/shared/@codingame/monaco-jsonrpc.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/shared/@codingame/monaco-jsonrpc.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/src/browser/progress-status-bar-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { injectable, inject } from 'inversify';
import { CancellationToken } from '@codingame/monaco-jsonrpc';
import { CancellationToken } from 'vscode-languageserver-protocol';
import { ProgressClient } from '../common';
import { ProgressMessage, ProgressUpdate } from '../common';
import { StatusBar, StatusBarAlignment } from './status-bar';
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/common/logger-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/

import { injectable } from 'inversify';
import { Logger } from 'vscode-languageserver-protocol';
import { JsonRpcServer } from './messaging/proxy-factory';

export const ILoggerServer = Symbol('ILoggerServer');
Expand Down Expand Up @@ -86,6 +87,26 @@ export namespace LogLevel {
}
}

export class ConsoleLogger implements Logger {

error(message: string): void {
console.log(message);
}

warn(message: string): void {
console.log(message);
}

info(message: string): void {
console.log(message);
}

log(message: string): void {
console.log(message);
}

}

/* eslint-disable @typescript-eslint/no-explicit-any */
export namespace ConsoleLogger {
type Console = (message?: any, ...optionalParams: any[]) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
********************************************************************************/

import { injectable, interfaces } from 'inversify';
import { ConsoleLogger, createWebSocketConnection, Logger } from '@codingame/monaco-jsonrpc';
import { Logger } from 'vscode-languageserver-protocol';
import { Emitter, Event } from '../event';
import { ConsoleLogger } from '../logger-protocol';
import { createWebSocketConnection } from './connection';
import { ConnectionHandler } from './handler';
import { JsonRpcProxy, JsonRpcProxyFactory } from './proxy-factory';
import { WebSocketChannel } from './web-socket-channel';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Message } from '@codingame/monaco-jsonrpc';
import { Message } from 'vscode-languageserver-protocol';
import { ILogger } from '../../common';

export interface ResolvedConnectionErrorHandlerOptions {
Expand Down
59 changes: 59 additions & 0 deletions packages/core/src/common/messaging/connection.ts
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
};
}
2 changes: 1 addition & 1 deletion packages/core/src/common/messaging/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { MessageConnection } from '@codingame/monaco-jsonrpc';
import { MessageConnection } from 'vscode-languageserver-protocol';

export const ConnectionHandler = Symbol('ConnectionHandler');

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/messaging/proxy-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as chai from 'chai';
import { ConsoleLogger } from '../../node/messaging/logger';
import { JsonRpcProxyFactory, JsonRpcProxy } from './proxy-factory';
import { createMessageConnection, MessageReader, MessageWriter } from '@codingame/monaco-jsonrpc';
import { createMessageConnection, MessageReader, MessageWriter } from 'vscode-languageserver-protocol';
import * as stream from 'stream';

const expect = chai.expect;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/messaging/proxy-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/* eslint-disable @typescript-eslint/no-explicit-any */

import { MessageConnection, ResponseError } from '@codingame/monaco-jsonrpc';
import { MessageConnection, ResponseError } from 'vscode-languageserver-protocol';
import { ApplicationError } from '../application-error';
import { Event, Emitter } from '../event';
import { Disposable } from '../disposable';
Expand Down
121 changes: 121 additions & 0 deletions packages/core/src/common/messaging/socket-message-handlers.ts
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);
}
}

}
15 changes: 14 additions & 1 deletion packages/core/src/common/messaging/web-socket-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@

/* eslint-disable @typescript-eslint/no-explicit-any */

import { IWebSocket } from '@codingame/monaco-jsonrpc/lib/socket/socket';
import { Disposable, DisposableCollection } from '../disposable';
import { Emitter } from '../event';
import { IConnection } from './connection';

// Copied from https://github.com/CodinGame/monaco-jsonrpc/blob/e3eea9123da2cc11845c409bcfae8e44b7d3a0e6/src/socket/socket.ts
export interface IWebSocket extends Disposable {
send(content: string): void;
onMessage(cb: (data: any) => void): void;
onError(cb: (reason: any) => void): void;
onClose(cb: (code: number, reason: string) => void): void;
}

// Copied from https://github.com/CodinGame/monaco-jsonrpc/blob/e3eea9123da2cc11845c409bcfae8e44b7d3a0e6/src/socket/socket.ts
export interface IWebSocketConnection extends IConnection {
readonly socket: IWebSocket;
}

export class WebSocketChannel implements IWebSocket {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { IpcMainEvent, ipcMain, WebContents } from '../../../shared/electron';
import { inject, injectable, named, postConstruct } from 'inversify';
import { createWebSocketConnection, MessageConnection } from '@codingame/monaco-jsonrpc';
import { MessageConnection } from 'vscode-languageserver-protocol';
import { ContributionProvider } from '../../common/contribution-provider';
import { WebSocketChannel } from '../../common/messaging/web-socket-channel';
import { MessagingContribution } from '../../node/messaging/messaging-contribution';
Expand All @@ -25,6 +25,7 @@ import { THEIA_ELECTRON_IPC_CHANNEL_NAME } from '../../electron-common/messaging
import { ElectronMainApplicationContribution } from '../electron-main-application';
import { ElectronMessagingService } from './electron-messaging-service';
import { ElectronConnectionHandler } from '../../electron-common/messaging/electron-connection-handler';
import { createWebSocketConnection } from '../../common/messaging/connection';

/**
* This component replicates the role filled by `MessagingContribution` but for Electron.
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/node/messaging/ipc-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

import 'reflect-metadata';
import { dynamicRequire } from '../dynamic-require';
import { ConsoleLogger, createMessageConnection, Trace } from '@codingame/monaco-jsonrpc';
import { IPCMessageReader, IPCMessageWriter } from 'vscode-jsonrpc/node';
import { createMessageConnection, Trace } from 'vscode-languageserver-protocol';
import { IPCMessageReader, IPCMessageWriter } from 'vscode-languageserver-protocol/node';
import { checkParentAlive, IPCEntryPoint } from './ipc-protocol';
import { ConsoleLogger } from './logger';

checkParentAlive();

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/node/messaging/ipc-connection-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import * as cp from 'child_process';
import * as path from 'path';
import { injectable, inject } from 'inversify';
import { IPCMessageReader, IPCMessageWriter } from 'vscode-jsonrpc/node';
import { Trace, createMessageConnection, MessageConnection, Message } from '@codingame/monaco-jsonrpc';
import { IPCMessageReader, IPCMessageWriter } from 'vscode-languageserver-protocol/node';
import { Trace, createMessageConnection, MessageConnection, Message } from 'vscode-languageserver-protocol';
import { ILogger, ConnectionErrorHandler, DisposableCollection, Disposable } from '../../common';
import { createIpcEnv } from './ipc-protocol';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/node/messaging/ipc-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { MessageConnection } from '@codingame/monaco-jsonrpc';
import { MessageConnection } from 'vscode-languageserver-protocol';

const THEIA_PARENT_PID = 'THEIA_PARENT_PID';
const THEIA_ENTRY_POINT = 'THEIA_ENTRY_POINT';
Expand Down
Loading

0 comments on commit db3b907

Please sign in to comment.