Skip to content

Commit

Permalink
update JSONRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Grant committed Oct 12, 2021
1 parent 3ccbfb0 commit 938a78d
Show file tree
Hide file tree
Showing 32 changed files with 99 additions and 96 deletions.
6 changes: 3 additions & 3 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ 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 All @@ -93,10 +94,9 @@ export class SomeClass {
- [`react@^16.8.0`](https://www.npmjs.com/package/react)
- [`react-dom@^16.8.0`](https://www.npmjs.com/package/react-dom)
- [`react-virtualized@^9.20.0`](https://www.npmjs.com/package/react-virtualized)
- [`vscode-languageserver-protocol@~3.15.3`](https://www.npmjs.com/package/vscode-languageserver-protocol)
- [`vscode-languageserver-types@^3.15.1`](https://www.npmjs.com/package/vscode-languageserver-types)
- [`vscode-languageserver-protocol@^3.16.0`](https://www.npmjs.com/package/vscode-languageserver-protocol)
- [`vscode-languageserver-types@^3.16.0`](https://www.npmjs.com/package/vscode-languageserver-types)
- [`vscode-uri@^2.1.1`](https://www.npmjs.com/package/vscode-uri)
- [`vscode-ws-jsonrpc@^0.2.0`](https://www.npmjs.com/package/vscode-ws-jsonrpc)
- [`ws@^7.1.2`](https://www.npmjs.com/package/ws)
- [`yargs@^15.3.1`](https://www.npmjs.com/package/yargs)

Expand Down
9 changes: 5 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@
"route-parser": "^0.0.5",
"safer-buffer": "^2.1.2",
"uuid": "^8.3.2",
"vscode-languageserver-protocol": "~3.15.3",
"vscode-languageserver-types": "^3.15.1",
"vscode-languageserver-protocol": "^3.16.0",
"vscode-languageserver-types": "^3.16.0",
"vscode-jsonrpc": "^6.0.0",
"vscode-uri": "^2.1.1",
"vscode-ws-jsonrpc": "^0.2.0",
"@codingame/monaco-jsonrpc": "^0.3.1",
"ws": "^7.1.2",
"yargs": "^15.3.1"
},
Expand Down Expand Up @@ -102,7 +103,7 @@
"vscode-languageserver-protocol",
"vscode-languageserver-types",
"vscode-uri",
"vscode-ws-jsonrpc"
"@codingame/monaco-jsonrpc"
],
"export =": [
"dompurify as DOMPurify",
Expand Down
1 change: 1 addition & 0 deletions packages/core/shared/@codingame/monaco-jsonrpc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@codingame/monaco-jsonrpc';
1 change: 1 addition & 0 deletions packages/core/shared/@codingame/monaco-jsonrpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@codingame/monaco-jsonrpc');
1 change: 0 additions & 1 deletion packages/core/shared/vscode-ws-jsonrpc.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/shared/vscode-ws-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 'vscode-ws-jsonrpc';
import { CancellationToken } from '@codingame/monaco-jsonrpc';
import { ProgressClient } from '../common';
import { ProgressMessage, ProgressUpdate } from '../common';
import { StatusBar, StatusBarAlignment } from './status-bar';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
********************************************************************************/

import { injectable, interfaces } from 'inversify';
import { ConsoleLogger, createWebSocketConnection, Logger } from 'vscode-ws-jsonrpc';
import { ConsoleLogger, createWebSocketConnection, Logger } from '@codingame/monaco-jsonrpc';
import { Emitter, Event } from '../event';
import { ConnectionHandler } from './handler';
import { JsonRpcProxy, JsonRpcProxyFactory } from './proxy-factory';
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 'vscode-ws-jsonrpc';
import { Message } from '@codingame/monaco-jsonrpc';
import { ILogger } from '../../common';

export interface ResolvedConnectionErrorHandlerOptions {
Expand Down
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 'vscode-ws-jsonrpc';
import { MessageConnection } from '@codingame/monaco-jsonrpc';

export const ConnectionHandler = Symbol('ConnectionHandler');

Expand Down
30 changes: 13 additions & 17 deletions 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 } from 'vscode-jsonrpc/lib/main';
import { createMessageConnection, MessageReader, MessageWriter } from '@codingame/monaco-jsonrpc';
import * as stream from 'stream';

const expect = chai.expect;
Expand Down Expand Up @@ -55,22 +55,16 @@ class TestClient {

describe('Proxy-Factory', () => {

it('Should correctly send notifications and requests.', done => {
it('Should correctly send notifications', async () => {
const it = getSetup();
it.clientProxy.notifyThat('hello');
function check(): void {
if (it.client.notifications.length === 0) {
console.log('waiting another 50 ms');
setTimeout(check, 50);
} else {
expect(it.client.notifications[0]).eq('hello');
it.serverProxy.doStuff('foo').then(result => {
expect(result).to.be.eq('done: foo');
done();
});
}
}
check();
await new Promise(resolve => setTimeout(resolve, 50));
expect(it.client.notifications[0]).eq('hello');
});
it('Should correctly handle requests', async () => {
const it = getSetup();
const result = await it.serverProxy.doStuff('foo');
expect(result).to.be.eq('done: foo');
});
it('Rejected Promise should result in rejected Promise.', done => {
const it = getSetup();
Expand Down Expand Up @@ -102,8 +96,10 @@ function getSetup(): {
const server = new TestServer();

const serverProxyFactory = new JsonRpcProxyFactory<TestServer>(client);
const client2server = new NoTransform();
const server2client = new NoTransform();
// @ts-ignore
const client2server: MessageReader & MessageWriter = new NoTransform();
// @ts-ignore
const server2client: MessageReader & MessageWriter = new NoTransform();
const serverConnection = createMessageConnection(server2client, client2server, new ConsoleLogger());
serverProxyFactory.listen(serverConnection);
const serverProxy = serverProxyFactory.createProxy();
Expand Down
14 changes: 10 additions & 4 deletions 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 'vscode-ws-jsonrpc';
import { MessageConnection, ResponseError } from '@codingame/monaco-jsonrpc';
import { ApplicationError } from '../application-error';
import { Event, Emitter } from '../event';
import { Disposable } from '../disposable';
Expand Down Expand Up @@ -132,8 +132,14 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
* response.
*/
listen(connection: MessageConnection): void {
connection.onRequest((prop, ...args) => this.onRequest(prop, ...args));
connection.onNotification((prop, ...args) => this.onNotification(prop, ...args));
connection.onRequest((prop, args, token) => {
const argsArray = Array.isArray(args) ? [...args, token] : [args, token];
return this.onRequest(prop, ...argsArray);
});
connection.onNotification((prop, args) => {
const argsArray = Array.isArray(args) ? args : [args];
return this.onNotification(prop, ...argsArray);
});
connection.onDispose(() => this.waitForConnection());
connection.listen();
this.connectionPromiseResolve(connection);
Expand Down Expand Up @@ -236,7 +242,7 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
const method = p.toString();
const capturedError = new Error(`Request '${method}' failed`);
return this.connectionPromise.then(connection =>
new Promise((resolve, reject) => {
new Promise<void>((resolve, reject) => {
try {
if (isNotify) {
connection.sendNotification(method, ...args);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/messaging/web-socket-channel.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 { IWebSocket } from 'vscode-ws-jsonrpc/lib/socket/socket';
import { IWebSocket } from '@codingame/monaco-jsonrpc/lib/socket/socket';
import { Disposable, DisposableCollection } from '../disposable';
import { Emitter } from '../event';

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

import { IpcMainEvent, ipcMain, WebContents } from '../../../shared/electron';
import { inject, injectable, named, postConstruct } from 'inversify';
import { MessageConnection } from 'vscode-ws-jsonrpc';
import { createWebSocketConnection } from 'vscode-ws-jsonrpc/lib/socket/connection';
import { createWebSocketConnection, MessageConnection } from '@codingame/monaco-jsonrpc';
import { ContributionProvider } from '../../common/contribution-provider';
import { WebSocketChannel } from '../../common/messaging/web-socket-channel';
import { MessagingContribution } from '../../node/messaging/messaging-contribution';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/node/messaging/ipc-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

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

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

Expand Down Expand Up @@ -83,10 +84,12 @@ export class IPCConnectionProvider {
info: (message: string) => this.logger.info(`[${options.serverName}: ${childProcess.pid}] ${message}`),
log: (message: string) => this.logger.info(`[${options.serverName}: ${childProcess.pid}] ${message}`)
});
const traceVerbosity = this.logger.isDebug() ? Trace.Verbose : Trace.Off;
connection.trace(traceVerbosity, {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
log: (message: any, data?: string) => this.logger.debug(`[${options.serverName}: ${childProcess.pid}] ${message}` + (typeof data === 'string' ? ' ' + data : ''))
this.logger.isDebug().then(isDebug => {
const traceVerbosity = isDebug ? Trace.Verbose : Trace.Off;
connection.trace(traceVerbosity, {
log: (message: unknown, data?: string) => this.logger
.debug(`[${options.serverName}: ${childProcess.pid}] ${message}` + (typeof data === 'string' ? ' ' + data : ''))
});
});
return connection;
}
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 'vscode-ws-jsonrpc';
import { MessageConnection } from '@codingame/monaco-jsonrpc';

const THEIA_PARENT_PID = 'THEIA_PARENT_PID';
const THEIA_ENTRY_POINT = 'THEIA_ENTRY_POINT';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/node/messaging/logger.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 { Logger } from 'vscode-ws-jsonrpc';
import { Logger } from '@codingame/monaco-jsonrpc';

export class ConsoleLogger implements Logger {

Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/node/messaging/messaging-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import * as net from 'net';
import * as http from 'http';
import * as https from 'https';
import { injectable, inject, named, postConstruct, interfaces, Container } from 'inversify';
import { MessageConnection } from 'vscode-ws-jsonrpc';
import { createWebSocketConnection } from 'vscode-ws-jsonrpc/lib/socket/connection';
import { IConnection } from 'vscode-ws-jsonrpc/lib/server/connection';
import * as launch from 'vscode-ws-jsonrpc/lib/server/launch';
import { createWebSocketConnection, MessageConnection } from '@codingame/monaco-jsonrpc';
import { IConnection } from '@codingame/monaco-jsonrpc/lib/server/connection';
import * as launch from '@codingame/monaco-jsonrpc/lib/server/launch';
import { ContributionProvider, ConnectionHandler, bindContributionProvider } from '../../common';
import { WebSocketChannel } from '../../common/messaging/web-socket-channel';
import { BackendApplicationContribution } from '../backend-application';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/node/messaging/messaging-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
********************************************************************************/

import * as ws from 'ws';
import { MessageConnection } from 'vscode-ws-jsonrpc';
import { IConnection } from 'vscode-ws-jsonrpc/lib/server/connection';
import { MessageConnection } from '@codingame/monaco-jsonrpc';
import { IConnection } from '@codingame/monaco-jsonrpc/lib/server/connection';
import { WebSocketChannel } from '../../common/messaging/web-socket-channel';

export interface MessagingService {
Expand Down
2 changes: 1 addition & 1 deletion packages/debug/src/browser/debug-session-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { DebugProtocol } from 'vscode-debugprotocol';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { Event, Emitter, DisposableCollection, Disposable, MaybePromise } from '@theia/core';
import { OutputChannel } from '@theia/output/lib/browser/output-channel';
import { IWebSocket } from '@theia/core/shared/vscode-ws-jsonrpc';
import { IWebSocket } from '@theia/core/shared/@codingame/monaco-jsonrpc';

export interface DebugExitEvent {
code?: number
Expand Down
2 changes: 1 addition & 1 deletion packages/debug/src/browser/debug-session-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { DebugSessionOptions } from './debug-session-options';
import { OutputChannelManager, OutputChannel } from '@theia/output/lib/browser/output-channel';
import { DebugPreferences } from './debug-preferences';
import { DebugSessionConnection } from './debug-session-connection';
import { IWebSocket } from '@theia/core/shared/vscode-ws-jsonrpc';
import { IWebSocket } from '@theia/core/shared/@codingame/monaco-jsonrpc';
import { DebugAdapterPath } from '../common/debug-service';
import { ContributionProvider } from '@theia/core/lib/common/contribution-provider';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
Expand Down
2 changes: 1 addition & 1 deletion packages/debug/src/node/debug-adapter-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
DebugAdapterSession
} from './debug-model';
import { DebugProtocol } from 'vscode-debugprotocol';
import { IWebSocket } from '@theia/core/shared/vscode-ws-jsonrpc';
import { IWebSocket } from '@theia/core/shared/@codingame/monaco-jsonrpc';
import { DisposableCollection, Disposable } from '@theia/core/lib/common/disposable';

/**
Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/browser/editor-widget-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class EditorWidgetFactory implements WidgetFactory {

protected async createEditor(uri: URI, options?: NavigatableWidgetOptions): Promise<EditorWidget> {
const newEditor = await this.constructEditor(uri);

this.setLabels(newEditor, uri);
const labelListener = this.labelProvider.onDidChange(event => {
if (event.affects(uri)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ export class RemoteFileSystemProvider implements Required<FileSystemProvider>, D

readFileStream(resource: URI, opts: FileReadStreamOptions, token: CancellationToken): ReadableStreamEvents<Uint8Array> {
const capturedError = new Error();
// eslint-disable-next-line @typescript-eslint/no-shadow
const stream = newWriteableStream<Uint8Array>(data => BinaryBuffer.concat(data.map(data => BinaryBuffer.wrap(data))).buffer);
const stream = newWriteableStream<Uint8Array>(data => BinaryBuffer.concat(data.map(datum => BinaryBuffer.wrap(datum))).buffer);
this.server.readFileStream(resource.toString(), opts, token).then(streamHandle => {
if (token.isCancellationRequested) {
stream.end(cancelled());
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-ext/src/common/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import { Disposable } from './disposable-util';
import { PluginMessageReader } from './plugin-message-reader';
import { PluginMessageWriter } from './plugin-message-writer';
import { MessageReader, MessageWriter, Message } from '@theia/core/shared/vscode-ws-jsonrpc';
import { IWebSocket } from '@theia/core/shared/vscode-ws-jsonrpc';
import { MessageReader, MessageWriter, Message, IWebSocket } from '@theia/core/shared/@codingame/monaco-jsonrpc';

/**
* The interface for describing the connection between plugins and main side.
Expand Down Expand Up @@ -59,7 +58,7 @@ export class PluginWebSocketChannel implements IWebSocket {
constructor(protected readonly connection: PluginConnection) { }

send(content: string): void {
this.connection.writer.write(content);
this.connection.writer.write({ jsonrpc: content });
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-ext/src/common/plugin-message-reader.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 { DataCallback, Emitter, Event, PartialMessageInfo } from '@theia/core/shared/vscode-ws-jsonrpc';
import { DataCallback, Disposable, Emitter, Event, PartialMessageInfo } from '@theia/core/shared/@codingame/monaco-jsonrpc';

export abstract class AbstractMessageReader {
protected errorEmitter = new Emitter<Error>();
Expand Down Expand Up @@ -65,7 +65,7 @@ export class PluginMessageReader extends AbstractMessageReader {
super();
}

listen(callback: DataCallback): void {
listen(callback: DataCallback): Disposable {
if (this.state === 'initial') {
this.state = 'listening';
this.callback = callback;
Expand All @@ -79,7 +79,9 @@ export class PluginMessageReader extends AbstractMessageReader {
this.fireClose();
}
}
return { dispose: () => this.callback = undefined };
}
return { dispose: () => { } };
}

readMessage(message: string): void {
Expand Down
17 changes: 11 additions & 6 deletions packages/plugin-ext/src/common/plugin-message-writer.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 { Message, MessageWriter, Emitter, Event } from '@theia/core/shared/vscode-ws-jsonrpc';
import { Message, MessageWriter, Emitter, Event } from '@theia/core/shared/@codingame/monaco-jsonrpc';
import { ConnectionMain, ConnectionExt } from './plugin-api-rpc';

export abstract class AbstractMessageWriter {
Expand Down Expand Up @@ -56,10 +56,15 @@ export class PluginMessageWriter extends AbstractMessageWriter implements Messag
super();
}

write(message: string): void;
write(message: Message): void;
write(arg: string | Message): void {
const content = JSON.stringify(arg);
this.proxy.$sendMessage(this.id, content);
write(message: Message): Promise<void> {
const content = JSON.stringify(message);
try {
this.proxy.$sendMessage(this.id, content);
return Promise.resolve();
} catch (e) {
return Promise.reject(e);
}
}

end(): void { }
}
Loading

0 comments on commit 938a78d

Please sign in to comment.