@opensumi/ide-connection 2.27.3-rc-1708399099.0 → 2.27.3-rc-1710386185.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/README.md +1 -1
  2. package/lib/browser/ws-channel-handler.d.ts +1 -1
  3. package/lib/browser/ws-channel-handler.d.ts.map +1 -1
  4. package/lib/browser/ws-channel-handler.js +23 -33
  5. package/lib/browser/ws-channel-handler.js.map +1 -1
  6. package/lib/common/buffers/buffers.d.ts +1 -0
  7. package/lib/common/buffers/buffers.d.ts.map +1 -1
  8. package/lib/common/buffers/buffers.js +3 -3
  9. package/lib/common/buffers/buffers.js.map +1 -1
  10. package/lib/common/{utils.d.ts → capturer.d.ts} +1 -19
  11. package/lib/common/capturer.d.ts.map +1 -0
  12. package/lib/common/capturer.js +26 -0
  13. package/lib/common/capturer.js.map +1 -0
  14. package/lib/common/connection/drivers/frame-decoder.d.ts +40 -1
  15. package/lib/common/connection/drivers/frame-decoder.d.ts.map +1 -1
  16. package/lib/common/connection/drivers/frame-decoder.js +9 -10
  17. package/lib/common/connection/drivers/frame-decoder.js.map +1 -1
  18. package/lib/common/connection/drivers/index.d.ts +0 -1
  19. package/lib/common/connection/drivers/index.d.ts.map +1 -1
  20. package/lib/common/connection/drivers/index.js +0 -1
  21. package/lib/common/connection/drivers/index.js.map +1 -1
  22. package/lib/common/connection/drivers/message-port.d.ts.map +1 -1
  23. package/lib/common/connection/drivers/message-port.js +1 -4
  24. package/lib/common/connection/drivers/message-port.js.map +1 -1
  25. package/lib/common/connection/drivers/node-message-port.d.ts +1 -1
  26. package/lib/common/connection/drivers/node-message-port.d.ts.map +1 -1
  27. package/lib/common/connection/drivers/node-message-port.js.map +1 -1
  28. package/lib/common/connection/drivers/reconnecting-websocket.d.ts.map +1 -1
  29. package/lib/common/connection/drivers/reconnecting-websocket.js +1 -1
  30. package/lib/common/connection/drivers/reconnecting-websocket.js.map +1 -1
  31. package/lib/common/connection/drivers/socket.d.ts +3 -10
  32. package/lib/common/connection/drivers/socket.d.ts.map +1 -1
  33. package/lib/common/connection/drivers/socket.js +3 -36
  34. package/lib/common/connection/drivers/socket.js.map +1 -1
  35. package/lib/common/connection/drivers/stream.d.ts +17 -0
  36. package/lib/common/connection/drivers/stream.d.ts.map +1 -0
  37. package/lib/common/connection/drivers/stream.js +70 -0
  38. package/lib/common/connection/drivers/stream.js.map +1 -0
  39. package/lib/common/connection/drivers/ws-websocket.d.ts +1 -1
  40. package/lib/common/connection/drivers/ws-websocket.d.ts.map +1 -1
  41. package/lib/common/connection/drivers/ws-websocket.js.map +1 -1
  42. package/lib/common/fury-extends/any.d.ts +10 -7
  43. package/lib/common/fury-extends/any.d.ts.map +1 -1
  44. package/lib/common/fury-extends/any.js +126 -105
  45. package/lib/common/fury-extends/any.js.map +1 -1
  46. package/lib/common/fury-extends/one-of.d.ts +10 -5
  47. package/lib/common/fury-extends/one-of.d.ts.map +1 -1
  48. package/lib/common/fury-extends/one-of.js +65 -30
  49. package/lib/common/fury-extends/one-of.js.map +1 -1
  50. package/lib/common/fury-extends/shared.d.ts +2 -0
  51. package/lib/common/fury-extends/shared.d.ts.map +1 -1
  52. package/lib/common/fury-extends/shared.js.map +1 -1
  53. package/lib/common/index.d.ts +2 -2
  54. package/lib/common/index.d.ts.map +1 -1
  55. package/lib/common/index.js +2 -2
  56. package/lib/common/index.js.map +1 -1
  57. package/lib/common/message.d.ts +1 -1
  58. package/lib/common/message.d.ts.map +1 -1
  59. package/lib/common/rpc/connection.d.ts +31 -15
  60. package/lib/common/rpc/connection.d.ts.map +1 -1
  61. package/lib/common/rpc/connection.js +206 -138
  62. package/lib/common/rpc/connection.js.map +1 -1
  63. package/lib/common/rpc/index.d.ts +1 -1
  64. package/lib/common/rpc/index.d.ts.map +1 -1
  65. package/lib/common/rpc/index.js +1 -1
  66. package/lib/common/rpc/index.js.map +1 -1
  67. package/lib/common/rpc/message-io.d.ts +61 -0
  68. package/lib/common/rpc/message-io.d.ts.map +1 -0
  69. package/lib/common/rpc/message-io.js +159 -0
  70. package/lib/common/rpc/message-io.js.map +1 -0
  71. package/lib/common/rpc/multiplexer.d.ts +3 -0
  72. package/lib/common/rpc/multiplexer.d.ts.map +1 -1
  73. package/lib/common/rpc/multiplexer.js +3 -0
  74. package/lib/common/rpc/multiplexer.js.map +1 -1
  75. package/lib/common/rpc/types.d.ts +26 -5
  76. package/lib/common/rpc/types.d.ts.map +1 -1
  77. package/lib/common/rpc-service/center.d.ts +6 -3
  78. package/lib/common/rpc-service/center.d.ts.map +1 -1
  79. package/lib/common/rpc-service/center.js +39 -31
  80. package/lib/common/rpc-service/center.js.map +1 -1
  81. package/lib/common/rpc-service/proxy/base.d.ts +5 -3
  82. package/lib/common/rpc-service/proxy/base.d.ts.map +1 -1
  83. package/lib/common/rpc-service/proxy/base.js +56 -17
  84. package/lib/common/rpc-service/proxy/base.js.map +1 -1
  85. package/lib/common/rpc-service/proxy/index.d.ts +1 -3
  86. package/lib/common/rpc-service/proxy/index.d.ts.map +1 -1
  87. package/lib/common/rpc-service/proxy/index.js +1 -3
  88. package/lib/common/rpc-service/proxy/index.js.map +1 -1
  89. package/lib/common/rpc-service/proxy/{legacy.d.ts → json.d.ts} +5 -5
  90. package/lib/common/rpc-service/proxy/json.d.ts.map +1 -0
  91. package/lib/common/rpc-service/proxy/{legacy.js → json.js} +46 -56
  92. package/lib/common/rpc-service/proxy/json.js.map +1 -0
  93. package/lib/common/rpc-service/proxy/sumi.d.ts +1 -1
  94. package/lib/common/rpc-service/proxy/sumi.d.ts.map +1 -1
  95. package/lib/common/rpc-service/proxy/sumi.js +25 -37
  96. package/lib/common/rpc-service/proxy/sumi.js.map +1 -1
  97. package/lib/common/rpc-service/{proxy/registry.d.ts → registry.d.ts} +12 -1
  98. package/lib/common/rpc-service/registry.d.ts.map +1 -0
  99. package/lib/common/rpc-service/registry.js +99 -0
  100. package/lib/common/rpc-service/registry.js.map +1 -0
  101. package/lib/common/rpc-service/stub.js +4 -6
  102. package/lib/common/rpc-service/stub.js.map +1 -1
  103. package/lib/common/{ext-rpc-protocol.d.ts → rpcProtocol.d.ts} +2 -2
  104. package/lib/common/rpcProtocol.d.ts.map +1 -0
  105. package/lib/common/{ext-rpc-protocol.js → rpcProtocol.js} +5 -6
  106. package/lib/common/rpcProtocol.js.map +1 -0
  107. package/lib/common/server-handler.d.ts +35 -0
  108. package/lib/common/server-handler.d.ts.map +1 -0
  109. package/lib/common/server-handler.js +165 -0
  110. package/lib/common/server-handler.js.map +1 -0
  111. package/lib/common/types.d.ts +14 -0
  112. package/lib/common/types.d.ts.map +1 -1
  113. package/lib/common/ws-channel.d.ts +6 -10
  114. package/lib/common/ws-channel.d.ts.map +1 -1
  115. package/lib/common/ws-channel.js +16 -15
  116. package/lib/common/ws-channel.js.map +1 -1
  117. package/lib/electron/channel-handler.d.ts +14 -0
  118. package/lib/electron/channel-handler.d.ts.map +1 -0
  119. package/lib/electron/channel-handler.js +26 -0
  120. package/lib/electron/channel-handler.js.map +1 -0
  121. package/lib/electron/index.d.ts +2 -0
  122. package/lib/electron/index.d.ts.map +1 -0
  123. package/lib/electron/index.js +5 -0
  124. package/lib/electron/index.js.map +1 -0
  125. package/lib/node/common-channel-handler.d.ts +6 -25
  126. package/lib/node/common-channel-handler.d.ts.map +1 -1
  127. package/lib/node/common-channel-handler.js +9 -151
  128. package/lib/node/common-channel-handler.js.map +1 -1
  129. package/package.json +5 -5
  130. package/src/browser/ws-channel-handler.ts +26 -40
  131. package/src/common/buffers/buffers.ts +1 -1
  132. package/src/common/capturer.ts +36 -0
  133. package/src/common/connection/drivers/frame-decoder.ts +11 -11
  134. package/src/common/connection/drivers/index.ts +0 -1
  135. package/src/common/connection/drivers/message-port.ts +1 -4
  136. package/src/common/connection/drivers/node-message-port.ts +2 -2
  137. package/src/common/connection/drivers/reconnecting-websocket.ts +3 -2
  138. package/src/common/connection/drivers/socket.ts +4 -42
  139. package/src/common/connection/drivers/stream.ts +76 -0
  140. package/src/common/connection/drivers/ws-websocket.ts +2 -2
  141. package/src/common/fury-extends/any.ts +122 -102
  142. package/src/common/fury-extends/one-of.ts +79 -31
  143. package/src/common/fury-extends/shared.ts +3 -0
  144. package/src/common/index.ts +2 -2
  145. package/src/common/message.ts +1 -1
  146. package/src/common/rpc/connection.ts +228 -166
  147. package/src/common/rpc/index.ts +1 -1
  148. package/src/common/rpc/message-io.ts +223 -0
  149. package/src/common/rpc/multiplexer.ts +3 -0
  150. package/src/common/rpc/types.ts +30 -5
  151. package/src/common/rpc-service/center.ts +37 -31
  152. package/src/common/rpc-service/proxy/base.ts +59 -12
  153. package/src/common/rpc-service/proxy/index.ts +1 -3
  154. package/src/common/rpc-service/proxy/{legacy.ts → json.ts} +47 -57
  155. package/src/common/rpc-service/proxy/sumi.ts +23 -35
  156. package/src/common/rpc-service/registry.ts +125 -0
  157. package/src/common/rpc-service/stub.ts +6 -6
  158. package/src/common/{ext-rpc-protocol.ts → rpcProtocol.ts} +2 -2
  159. package/src/common/server-handler.ts +183 -0
  160. package/src/common/types.ts +13 -0
  161. package/src/common/ws-channel.ts +21 -23
  162. package/src/electron/channel-handler.ts +26 -0
  163. package/src/electron/index.ts +1 -0
  164. package/src/node/common-channel-handler.ts +10 -165
  165. package/lib/common/connection/drivers/utils.d.ts +0 -12
  166. package/lib/common/connection/drivers/utils.d.ts.map +0 -1
  167. package/lib/common/connection/drivers/utils.js +0 -49
  168. package/lib/common/connection/drivers/utils.js.map +0 -1
  169. package/lib/common/ext-rpc-protocol.d.ts.map +0 -1
  170. package/lib/common/ext-rpc-protocol.js.map +0 -1
  171. package/lib/common/rpc/packet.d.ts +0 -65
  172. package/lib/common/rpc/packet.d.ts.map +0 -1
  173. package/lib/common/rpc/packet.js +0 -82
  174. package/lib/common/rpc/packet.js.map +0 -1
  175. package/lib/common/rpc/protocol-repository.d.ts +0 -32
  176. package/lib/common/rpc/protocol-repository.d.ts.map +0 -1
  177. package/lib/common/rpc/protocol-repository.js +0 -118
  178. package/lib/common/rpc/protocol-repository.js.map +0 -1
  179. package/lib/common/rpc/utils.d.ts +0 -2
  180. package/lib/common/rpc/utils.d.ts.map +0 -1
  181. package/lib/common/rpc/utils.js +0 -10
  182. package/lib/common/rpc/utils.js.map +0 -1
  183. package/lib/common/rpc-service/proxy/invoker.d.ts +0 -14
  184. package/lib/common/rpc-service/proxy/invoker.d.ts.map +0 -1
  185. package/lib/common/rpc-service/proxy/invoker.js +0 -34
  186. package/lib/common/rpc-service/proxy/invoker.js.map +0 -1
  187. package/lib/common/rpc-service/proxy/legacy.d.ts.map +0 -1
  188. package/lib/common/rpc-service/proxy/legacy.js.map +0 -1
  189. package/lib/common/rpc-service/proxy/registry.d.ts.map +0 -1
  190. package/lib/common/rpc-service/proxy/registry.js +0 -46
  191. package/lib/common/rpc-service/proxy/registry.js.map +0 -1
  192. package/lib/common/utils.d.ts.map +0 -1
  193. package/lib/common/utils.js +0 -57
  194. package/lib/common/utils.js.map +0 -1
  195. package/src/common/connection/drivers/utils.ts +0 -52
  196. package/src/common/rpc/packet.ts +0 -104
  197. package/src/common/rpc/protocol-repository.ts +0 -180
  198. package/src/common/rpc/utils.ts +0 -5
  199. package/src/common/rpc-service/proxy/invoker.ts +0 -45
  200. package/src/common/rpc-service/proxy/registry.ts +0 -56
  201. package/src/common/utils.ts +0 -80
@@ -0,0 +1,223 @@
1
+ import Fury, { Serializer, Type, TypeDescription } from '@furyjs/fury';
2
+ import { generateSerializer } from '@furyjs/fury/dist/lib/gen';
3
+ import { BinaryReader, BinaryWriter } from '@furyjs/fury/dist/lib/type';
4
+
5
+ import { stringifyError } from '@opensumi/ide-core-common/lib/utils';
6
+
7
+ import { AnySerializer } from '../fury-extends/any';
8
+ import { furyFactory } from '../fury-extends/shared';
9
+
10
+ import {
11
+ IProtocolSerializer,
12
+ IRequestHeaders,
13
+ IResponseHeaders,
14
+ ITransferable,
15
+ TSerializer,
16
+ TSumiProtocol,
17
+ TSumiProtocolMethod,
18
+ } from './types';
19
+
20
+ const ProtoVersionV1 = 1;
21
+
22
+ export enum OperationType {
23
+ Request,
24
+ Notification,
25
+ Response,
26
+ Cancel,
27
+ }
28
+
29
+ export enum Status {
30
+ OK,
31
+ Err,
32
+ }
33
+
34
+ export const HeadersProto = {
35
+ Request: Type.object('req-headers', {
36
+ cancelable: Type.bool(),
37
+ }),
38
+ Response: Type.object('resp-headers', {
39
+ chunked: Type.bool(),
40
+ }),
41
+ } as const;
42
+
43
+ class SumiProtocolSerializer implements IProtocolSerializer {
44
+ request: TSerializer;
45
+ result: TSerializer;
46
+
47
+ constructor(methodProtocol: TSumiProtocolMethod, public fury: Fury) {
48
+ const argsTuple = [] as TypeDescription[];
49
+
50
+ for (const element of methodProtocol.request) {
51
+ argsTuple.push(element.type);
52
+ }
53
+
54
+ const requestProto = Type.tuple(argsTuple);
55
+ const resultProto = methodProtocol.response.type || Type.any();
56
+
57
+ this.request = this.fury.registerSerializer(requestProto);
58
+ this.result = this.fury.registerSerializer(resultProto);
59
+ }
60
+
61
+ writeRequest(args: any[]): void {
62
+ return this.request.serializer.write(args);
63
+ }
64
+ readRequest(): any[] {
65
+ return this.request.serializer.read();
66
+ }
67
+ writeResponse<T = ITransferable>(result: T): void {
68
+ return this.result.serializer.write(result);
69
+ }
70
+ readResponse<T = ITransferable>(): T {
71
+ return this.result.serializer.read();
72
+ }
73
+ }
74
+
75
+ class AnyProtocolSerializer implements IProtocolSerializer {
76
+ anySerializer: AnySerializer;
77
+
78
+ constructor(public writer: BinaryWriter, public reader: BinaryReader) {
79
+ this.anySerializer = new AnySerializer(this.writer, this.reader);
80
+ }
81
+
82
+ writeRequest(args: any[]): void {
83
+ this.anySerializer.write(args);
84
+ }
85
+ readRequest(): any[] {
86
+ return this.anySerializer.read();
87
+ }
88
+ writeResponse<T = any>(result: T): void {
89
+ this.anySerializer.write(result);
90
+ }
91
+ readResponse<T = any>(): T {
92
+ return this.anySerializer.read();
93
+ }
94
+ }
95
+
96
+ export class MessageIO {
97
+ fury: Fury;
98
+ reader: BinaryReader;
99
+ writer: BinaryWriter;
100
+
101
+ private serializerMap = new Map<string, SumiProtocolSerializer>();
102
+
103
+ private anySerializer: AnyProtocolSerializer;
104
+
105
+ requestHeadersSerializer: Serializer<IRequestHeaders, IRequestHeaders>;
106
+ responseHeadersSerializer: Serializer<IResponseHeaders, IRequestHeaders>;
107
+
108
+ constructor() {
109
+ const fury = furyFactory();
110
+ this.fury = fury.fury;
111
+ this.reader = fury.reader;
112
+ this.writer = fury.writer;
113
+
114
+ this.requestHeadersSerializer = generateSerializer(this.fury, HeadersProto.Request);
115
+ this.responseHeadersSerializer = generateSerializer(this.fury, HeadersProto.Response);
116
+
117
+ this.anySerializer = new AnyProtocolSerializer(this.writer, this.reader);
118
+ }
119
+
120
+ has(name: string) {
121
+ return this.serializerMap.has(name);
122
+ }
123
+
124
+ loadProtocol(
125
+ protocol: TSumiProtocol,
126
+ options?: {
127
+ nameConverter?: (str: string) => string;
128
+ },
129
+ ) {
130
+ const { methods } = protocol;
131
+
132
+ for (const proto of methods) {
133
+ this.loadProtocolMethod(proto, options);
134
+ }
135
+ }
136
+
137
+ loadProtocolMethod(
138
+ methodProtocol: TSumiProtocolMethod,
139
+ options?: {
140
+ nameConverter?: (str: string) => string;
141
+ },
142
+ ) {
143
+ let method = methodProtocol.method;
144
+ if (options?.nameConverter) {
145
+ method = options.nameConverter(method);
146
+ }
147
+ this.serializerMap.set(method, new SumiProtocolSerializer(methodProtocol, this.fury));
148
+ }
149
+
150
+ getProcessor(method: string): IProtocolSerializer {
151
+ const processor = this.serializerMap.get(method);
152
+ if (processor) {
153
+ return processor;
154
+ }
155
+ return this.anySerializer;
156
+ }
157
+
158
+ Notification(requestId: number, method: string, headers: IRequestHeaders, args: any[]) {
159
+ const { writer } = this;
160
+ writer.reset();
161
+
162
+ writer.uint16((OperationType.Notification << 8) | ProtoVersionV1);
163
+ writer.uint32(requestId);
164
+
165
+ writer.stringOfVarUInt32(method);
166
+ this.requestHeadersSerializer.write(headers);
167
+ this.getProcessor(method).writeRequest(args);
168
+
169
+ return writer.dump();
170
+ }
171
+
172
+ Request(requestId: number, method: string, headers: IRequestHeaders, args: any[]) {
173
+ const { writer } = this;
174
+ writer.reset();
175
+
176
+ writer.uint16((OperationType.Request << 8) | ProtoVersionV1);
177
+ writer.uint32(requestId);
178
+
179
+ writer.stringOfVarUInt32(method);
180
+ this.requestHeadersSerializer.write(headers);
181
+ this.getProcessor(method).writeRequest(args);
182
+
183
+ return writer.dump();
184
+ }
185
+
186
+ Cancel(requestId: number) {
187
+ const { writer } = this;
188
+ writer.reset();
189
+
190
+ writer.uint16((OperationType.Cancel << 8) | ProtoVersionV1);
191
+ writer.uint32(requestId);
192
+
193
+ return writer.dump();
194
+ }
195
+
196
+ Response(requestId: number, method: string, headers: Record<string, any>, result: any) {
197
+ const { writer } = this;
198
+ writer.reset();
199
+
200
+ writer.uint16((OperationType.Response << 8) | ProtoVersionV1);
201
+ writer.uint32(requestId);
202
+ writer.stringOfVarUInt32(method);
203
+ writer.uint16(Status.OK);
204
+ this.responseHeadersSerializer.write(headers);
205
+ this.getProcessor(method).writeResponse(result);
206
+
207
+ return writer.dump();
208
+ }
209
+
210
+ Error(requestId: number, method: string, headers: Record<string, any>, error: any) {
211
+ const { writer } = this;
212
+ writer.reset();
213
+
214
+ writer.uint16((OperationType.Response << 8) | ProtoVersionV1);
215
+ writer.uint32(requestId);
216
+ writer.stringOfVarUInt32(method);
217
+ writer.uint16(Status.Err);
218
+ this.responseHeadersSerializer.write(headers);
219
+ writer.stringOfVarUInt32(stringifyError(error));
220
+
221
+ return writer.dump();
222
+ }
223
+ }
@@ -35,6 +35,9 @@ function extractServiceAndMethod(rpcId: string): [string, string] {
35
35
  return [rpcId.substring(0, idx), rpcId.substring(idx + 1)];
36
36
  }
37
37
 
38
+ /**
39
+ * A connection multiplexer that allows to register multiple local RPC services and to create proxies for them.
40
+ */
38
41
  export class SumiConnectionMultiplexer extends SumiConnection implements IRPCProtocol {
39
42
  private readonly _locals: Map<string, any>;
40
43
  private readonly _proxies: Map<string, any>;
@@ -1,23 +1,48 @@
1
- import { TypeDescription } from '@furyjs/fury';
2
-
3
1
  import {
2
+ Headers,
4
3
  RPCProtocol as _RPCProtocol,
5
4
  RPCProtocolMethod as _RPCProtocolMethod,
6
5
  Request as _Request,
7
6
  Response as _Response,
8
7
  } from '@opensumi/ide-core-common/lib/types/rpc';
9
8
 
9
+ import type Fury from '@furyjs/fury';
10
+ import type { Serializer, TypeDescription } from '@furyjs/fury';
11
+
10
12
  export type TSumiProtocol = _RPCProtocol<TypeDescription>;
11
13
  export type TSumiProtocolMethod = _RPCProtocolMethod<TypeDescription>;
12
14
  export type TSumiRequest = _Request<TypeDescription>;
13
15
  export type TSumiResponse = _Response<TypeDescription>;
14
16
 
15
- export type TRequestCallback = (headers: Record<string, any>, error?: any, payload?: Uint8Array | string | any) => void;
17
+ export type TRequestCallback = (headers: Record<string, any>, error?: any, payload?: any) => void;
16
18
 
17
19
  export type THandlerResult<R> = R | Promise<R>;
18
20
 
19
21
  export type TGenericNotificationHandler = (...args: any[]) => void | Promise<void>;
20
22
  export type TGenericRequestHandler<R> = (...args: any[]) => THandlerResult<R>;
21
23
 
22
- export type TOnRequestNotFoundHandler = (method: string, args: any[]) => THandlerResult<any>;
23
- export type TOnNotificationNotFoundHandler = (method: string, args: any[]) => THandlerResult<void>;
24
+ export type TRequestNotFoundHandler = (method: string, args: any[]) => THandlerResult<any>;
25
+ export type TNotificationNotFoundHandler = (method: string, args: any[]) => THandlerResult<void>;
26
+
27
+ export type TSerializer = { serializer: Serializer } & Omit<ReturnType<Fury['registerSerializer']>, 'serializer'>;
28
+
29
+ export interface IRequestHeaders extends Headers {
30
+ /**
31
+ * If set to true, the request can be canceled.
32
+ *
33
+ * the server will push a `CancellationToken` to the request arguments.
34
+ */
35
+ cancelable?: boolean;
36
+ }
37
+
38
+ export type IResponseHeaders = Headers;
39
+
40
+ export type ITransferable = any;
41
+ export type TRequestTransferable = ITransferable[];
42
+
43
+ export interface IProtocolSerializer {
44
+ writeRequest(args: any[]): void;
45
+ readRequest(): any[];
46
+ writeResponse<T = any>(result: T): void;
47
+ readResponse<T = any>(): T;
48
+ }
@@ -2,22 +2,23 @@ import { Deferred } from '@opensumi/ide-core-common';
2
2
  import { MessageConnection } from '@opensumi/vscode-jsonrpc';
3
3
 
4
4
  import { METHOD_NOT_REGISTERED } from '../constants';
5
- import { TSumiProtocol, ProtocolRepository } from '../rpc';
5
+ import { TSumiProtocol } from '../rpc';
6
6
  import { SumiConnection } from '../rpc/connection';
7
7
  import { IBench, ILogger, RPCServiceMethod, ServiceType } from '../types';
8
- import { getMethodName } from '../utils';
9
8
 
10
- import { ServiceRegistry, Invoker, ProxyLegacy, ProxySumi } from './proxy';
9
+ import { ProxyJson, ProxySumi } from './proxy';
10
+ import { ProxyBase } from './proxy/base';
11
+ import { ProtocolRegistry, ServiceRegistry } from './registry';
11
12
 
12
13
  const safeProcess: { pid: string } = typeof process === 'undefined' ? { pid: 'unknown' } : (process as any);
13
14
 
14
15
  export class RPCServiceCenter {
15
16
  public uid: string;
16
17
 
17
- private invokers: Invoker[] = [];
18
+ private proxies: ProxyBase<any>[] = [];
18
19
 
19
- private protocolRepository = new ProtocolRepository();
20
- private registry = new ServiceRegistry();
20
+ private serviceRegistry = new ServiceRegistry();
21
+ private protocolRegistry = new ProtocolRegistry();
21
22
 
22
23
  private deferred = new Deferred<void>();
23
24
  private logger: ILogger;
@@ -40,64 +41,58 @@ export class RPCServiceCenter {
40
41
  }
41
42
 
42
43
  loadProtocol(protocol: TSumiProtocol) {
43
- this.protocolRepository.loadProtocol(protocol, {
44
+ this.protocolRegistry.addProtocol(protocol, {
44
45
  nameConverter: (name) => getMethodName(protocol.name, name),
45
46
  });
46
47
  }
47
48
 
48
49
  setSumiConnection(connection: SumiConnection) {
49
- if (this.invokers.length === 0) {
50
+ if (this.proxies.length === 0) {
50
51
  this.deferred.resolve();
51
52
  }
52
53
 
53
- const index = this.invokers.length - 1;
54
+ this.protocolRegistry.applyTo(connection.io);
54
55
 
55
- const invoker = new Invoker();
56
+ const index = this.proxies.length - 1;
57
+ const proxy = new ProxySumi(this.serviceRegistry, this.logger);
58
+ proxy.listen(connection);
56
59
 
57
- const sumiProxy = new ProxySumi(this.registry, this.logger);
58
- invoker.attachSumi(sumiProxy);
59
- sumiProxy.listen(connection);
60
-
61
- this.invokers.push(invoker);
60
+ this.proxies.push(proxy);
62
61
 
63
62
  return {
64
63
  dispose: () => {
65
- this.invokers.splice(index, 1);
66
- invoker.dispose();
64
+ this.proxies.splice(index, 1);
65
+ proxy.dispose();
67
66
  },
68
67
  };
69
68
  }
70
69
 
71
70
  setConnection(connection: MessageConnection) {
72
- if (this.invokers.length === 0) {
71
+ if (this.proxies.length === 0) {
73
72
  this.deferred.resolve();
74
73
  }
75
74
 
76
- const index = this.invokers.length - 1;
77
-
78
- const invoker = new Invoker();
75
+ const index = this.proxies.length - 1;
79
76
 
80
- const legacyProxy = new ProxyLegacy(this.registry, this.logger);
81
- legacyProxy.listen(connection);
77
+ const proxy = new ProxyJson(this.serviceRegistry, this.logger);
78
+ proxy.listen(connection);
82
79
 
83
- invoker.attachLegacy(legacyProxy);
84
-
85
- this.invokers.push(invoker);
80
+ this.proxies.push(proxy);
86
81
 
87
82
  return {
88
83
  dispose: () => {
89
- this.invokers.splice(index, 1);
90
- invoker.dispose();
84
+ this.proxies.splice(index, 1);
85
+ proxy.dispose();
91
86
  },
92
87
  };
93
88
  }
94
89
 
95
90
  onRequest(serviceName: string, _name: string, method: RPCServiceMethod) {
96
- this.registry.register(getMethodName(serviceName, _name), method);
91
+ this.serviceRegistry.register(getMethodName(serviceName, _name), method);
97
92
  }
98
93
 
99
94
  onRequestService(serviceName: string, service: any) {
100
- this.registry.registerService(service, {
95
+ this.serviceRegistry.registerService(service, {
101
96
  nameConverter: (name) => getMethodName(serviceName, name),
102
97
  });
103
98
  }
@@ -106,7 +101,7 @@ export class RPCServiceCenter {
106
101
  await this.ready();
107
102
 
108
103
  const name = getMethodName(serviceName, _name);
109
- const broadcastResult = await Promise.all(this.invokers.map((proxy) => proxy.invoke(name, ...args)));
104
+ const broadcastResult = await Promise.all(this.proxies.map((proxy) => proxy.invoke(name, ...args)));
110
105
 
111
106
  const doubtfulResult = [] as any[];
112
107
  const result = [] as any[];
@@ -132,3 +127,14 @@ export class RPCServiceCenter {
132
127
  return result.length === 1 ? result[0] : result;
133
128
  }
134
129
  }
130
+
131
+ export function getNotificationName(serviceName: string, name: string) {
132
+ return `on:${serviceName}:${name}`;
133
+ }
134
+ export function getRequestName(serviceName: string, name: string) {
135
+ return `${serviceName}:${name}`;
136
+ }
137
+
138
+ export function getMethodName(serviceName: string, name: string) {
139
+ return name.startsWith('on') ? getNotificationName(serviceName, name) : getRequestName(serviceName, name);
140
+ }
@@ -1,15 +1,17 @@
1
- import { Deferred, isDefined } from '@opensumi/ide-core-common';
1
+ import { Deferred } from '@opensumi/ide-core-common';
2
2
 
3
+ import { ICapturedMessage, MessageType, ResponseStatus, getCapturer } from '../../capturer';
3
4
  import { ILogger, IRPCServiceMap } from '../../types';
4
- import { ICapturedMessage, MessageType, ResponseStatus, getCapturer } from '../../utils';
5
5
 
6
- import type { ServiceRegistry } from './registry';
6
+ import type { ServiceRegistry } from '../registry';
7
7
 
8
8
  interface IBaseConnection {
9
9
  listen(): void;
10
10
  dispose(): void;
11
11
  }
12
12
 
13
+ const defaultReservedWordSet = new Set(['then', 'finally']);
14
+
13
15
  let requestId = 0;
14
16
 
15
17
  export abstract class ProxyBase<T extends IBaseConnection> {
@@ -18,11 +20,14 @@ export abstract class ProxyBase<T extends IBaseConnection> {
18
20
 
19
21
  protected connectionPromise: Deferred<void> = new Deferred<void>();
20
22
 
21
- protected abstract engine: 'legacy' | 'sumi';
23
+ protected abstract engine: 'json' | 'sumi';
24
+ capturer: (data: any) => void;
22
25
 
23
26
  constructor(public registry: ServiceRegistry, logger?: ILogger) {
24
27
  this.logger = logger || console;
25
28
 
29
+ this.capturer = getCapturer();
30
+
26
31
  this.registry.onServicesUpdate((services) => {
27
32
  if (this.connection) {
28
33
  this.bindMethods(services);
@@ -32,13 +37,10 @@ export abstract class ProxyBase<T extends IBaseConnection> {
32
37
 
33
38
  // capture messages for opensumi devtools
34
39
  private capture(message: ICapturedMessage): void {
35
- const capturer = getCapturer();
36
- if (isDefined(capturer)) {
37
- capturer({
38
- ...message,
39
- engine: this.engine,
40
- });
41
- }
40
+ this.capturer({
41
+ ...message,
42
+ engine: this.engine,
43
+ });
42
44
  }
43
45
 
44
46
  protected nextRequestId() {
@@ -46,10 +48,16 @@ export abstract class ProxyBase<T extends IBaseConnection> {
46
48
  }
47
49
 
48
50
  protected captureOnRequest(requestId: string, serviceMethod: string, args: any[]): void {
51
+ if (!this.capturer) {
52
+ return;
53
+ }
49
54
  this.capture({ type: MessageType.OnRequest, requestId, serviceMethod, arguments: args });
50
55
  }
51
56
 
52
57
  protected captureOnRequestResult(requestId: string, serviceMethod: string, data: any): void {
58
+ if (!this.capturer) {
59
+ return;
60
+ }
53
61
  this.capture({
54
62
  type: MessageType.OnRequestResult,
55
63
  status: ResponseStatus.Success,
@@ -60,6 +68,10 @@ export abstract class ProxyBase<T extends IBaseConnection> {
60
68
  }
61
69
 
62
70
  protected captureOnRequestFail(requestId: string, serviceMethod: string, error: any): void {
71
+ if (!this.capturer) {
72
+ return;
73
+ }
74
+
63
75
  this.logger.warn(`request exec ${serviceMethod} error`, error);
64
76
 
65
77
  this.capture({
@@ -72,14 +84,24 @@ export abstract class ProxyBase<T extends IBaseConnection> {
72
84
  }
73
85
 
74
86
  protected captureOnNotification(serviceMethod: string, args: any[]): void {
87
+ if (!this.capturer) {
88
+ return;
89
+ }
75
90
  this.capture({ type: MessageType.OnNotification, serviceMethod, arguments: args });
76
91
  }
77
92
 
78
93
  protected captureSendRequest(requestId: string, serviceMethod: string, args: any[]): void {
94
+ if (!this.capturer) {
95
+ return;
96
+ }
79
97
  this.capture({ type: MessageType.SendRequest, requestId, serviceMethod, arguments: args });
80
98
  }
81
99
 
82
100
  protected captureSendRequestResult(requestId: string, serviceMethod: string, data: any): void {
101
+ if (!this.capturer) {
102
+ return;
103
+ }
104
+
83
105
  this.capture({
84
106
  type: MessageType.RequestResult,
85
107
  status: ResponseStatus.Success,
@@ -90,6 +112,10 @@ export abstract class ProxyBase<T extends IBaseConnection> {
90
112
  }
91
113
 
92
114
  protected captureSendRequestFail(requestId: string, serviceMethod: string, error: any): void {
115
+ if (!this.capturer) {
116
+ return;
117
+ }
118
+
93
119
  this.capture({
94
120
  type: MessageType.RequestResult,
95
121
  status: ResponseStatus.Fail,
@@ -100,6 +126,9 @@ export abstract class ProxyBase<T extends IBaseConnection> {
100
126
  }
101
127
 
102
128
  protected captureSendNotification(serviceMethod: string, args: any[]): void {
129
+ if (!this.capturer) {
130
+ return;
131
+ }
103
132
  this.capture({ type: MessageType.SendNotification, serviceMethod, arguments: args });
104
133
  }
105
134
 
@@ -121,6 +150,24 @@ export abstract class ProxyBase<T extends IBaseConnection> {
121
150
  }
122
151
  }
123
152
 
124
- abstract getInvokeProxy<T = any>(): T;
153
+ public abstract invoke(prop: string, ...args: any[]): Promise<any>;
125
154
  protected abstract bindMethods(services: PropertyKey[]): void;
155
+
156
+ public getInvokeProxy<T = any>(): T {
157
+ return new Proxy(Object.create(null), {
158
+ get: (target: any, p: PropertyKey) => {
159
+ if (typeof p !== 'string') {
160
+ return null;
161
+ }
162
+ if (defaultReservedWordSet.has(p)) {
163
+ return Promise.resolve();
164
+ }
165
+
166
+ if (!target[p]) {
167
+ target[p] = (...args: any[]) => this.invoke(p, ...args);
168
+ }
169
+ return target[p];
170
+ },
171
+ });
172
+ }
126
173
  }
@@ -1,4 +1,2 @@
1
- export * from './legacy';
1
+ export * from './json';
2
2
  export * from './sumi';
3
- export * from './registry';
4
- export * from './invoker';