@opensumi/ide-connection 2.27.3-next-1706686746.0 → 2.27.3-rc-1706687185.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 (91) hide show
  1. package/lib/browser/ws-channel-handler.d.ts.map +1 -1
  2. package/lib/browser/ws-channel-handler.js +4 -0
  3. package/lib/browser/ws-channel-handler.js.map +1 -1
  4. package/lib/common/connection/drivers/base.d.ts +1 -0
  5. package/lib/common/connection/drivers/base.d.ts.map +1 -1
  6. package/lib/common/connection/drivers/base.js +4 -0
  7. package/lib/common/connection/drivers/base.js.map +1 -1
  8. package/lib/common/ext-rpc-protocol.d.ts.map +1 -1
  9. package/lib/common/ext-rpc-protocol.js +2 -3
  10. package/lib/common/ext-rpc-protocol.js.map +1 -1
  11. package/lib/common/proxy/base.d.ts +8 -16
  12. package/lib/common/proxy/base.d.ts.map +1 -1
  13. package/lib/common/proxy/base.js +21 -62
  14. package/lib/common/proxy/base.js.map +1 -1
  15. package/lib/common/proxy/legacy.d.ts +7 -3
  16. package/lib/common/proxy/legacy.d.ts.map +1 -1
  17. package/lib/common/proxy/legacy.js +135 -88
  18. package/lib/common/proxy/legacy.js.map +1 -1
  19. package/lib/common/rpc-service/center.d.ts +1 -5
  20. package/lib/common/rpc-service/center.d.ts.map +1 -1
  21. package/lib/common/rpc-service/center.js +16 -36
  22. package/lib/common/rpc-service/center.js.map +1 -1
  23. package/lib/common/rpc-service/stub.d.ts.map +1 -1
  24. package/lib/common/rpc-service/stub.js +5 -1
  25. package/lib/common/rpc-service/stub.js.map +1 -1
  26. package/lib/common/types.d.ts +1 -1
  27. package/lib/common/types.d.ts.map +1 -1
  28. package/lib/common/ws-channel.d.ts +18 -86
  29. package/lib/common/ws-channel.d.ts.map +1 -1
  30. package/lib/common/ws-channel.js +33 -80
  31. package/lib/common/ws-channel.js.map +1 -1
  32. package/package.json +4 -4
  33. package/src/browser/ws-channel-handler.ts +4 -0
  34. package/src/common/connection/drivers/base.ts +6 -0
  35. package/src/common/ext-rpc-protocol.ts +2 -4
  36. package/src/common/proxy/base.ts +28 -80
  37. package/src/common/proxy/legacy.ts +147 -97
  38. package/src/common/rpc-service/center.ts +16 -46
  39. package/src/common/rpc-service/stub.ts +5 -1
  40. package/src/common/types.ts +1 -1
  41. package/src/common/ws-channel.ts +35 -103
  42. package/lib/common/fury-extends/one-of.d.ts +0 -14
  43. package/lib/common/fury-extends/one-of.d.ts.map +0 -1
  44. package/lib/common/fury-extends/one-of.js +0 -53
  45. package/lib/common/fury-extends/one-of.js.map +0 -1
  46. package/lib/common/protocols/common-server.d.ts +0 -3
  47. package/lib/common/protocols/common-server.d.ts.map +0 -1
  48. package/lib/common/protocols/common-server.js +0 -18
  49. package/lib/common/protocols/common-server.js.map +0 -1
  50. package/lib/common/protocols/common.d.ts +0 -24
  51. package/lib/common/protocols/common.d.ts.map +0 -1
  52. package/lib/common/protocols/common.js +0 -12
  53. package/lib/common/protocols/common.js.map +0 -1
  54. package/lib/common/proxy/runner.d.ts +0 -20
  55. package/lib/common/proxy/runner.d.ts.map +0 -1
  56. package/lib/common/proxy/runner.js +0 -46
  57. package/lib/common/proxy/runner.js.map +0 -1
  58. package/lib/common/proxy/sumi.d.ts +0 -9
  59. package/lib/common/proxy/sumi.d.ts.map +0 -1
  60. package/lib/common/proxy/sumi.js +0 -86
  61. package/lib/common/proxy/sumi.js.map +0 -1
  62. package/lib/common/rpc/connection.d.ts +0 -29
  63. package/lib/common/rpc/connection.d.ts.map +0 -1
  64. package/lib/common/rpc/connection.js +0 -195
  65. package/lib/common/rpc/connection.js.map +0 -1
  66. package/lib/common/rpc/index.d.ts +0 -2
  67. package/lib/common/rpc/index.d.ts.map +0 -1
  68. package/lib/common/rpc/index.js +0 -5
  69. package/lib/common/rpc/index.js.map +0 -1
  70. package/lib/common/rpc/packet.d.ts +0 -49
  71. package/lib/common/rpc/packet.d.ts.map +0 -1
  72. package/lib/common/rpc/packet.js +0 -69
  73. package/lib/common/rpc/packet.js.map +0 -1
  74. package/lib/common/rpc/protocol-repository.d.ts +0 -32
  75. package/lib/common/rpc/protocol-repository.d.ts.map +0 -1
  76. package/lib/common/rpc/protocol-repository.js +0 -94
  77. package/lib/common/rpc/protocol-repository.js.map +0 -1
  78. package/lib/common/rpc/types.d.ts +0 -13
  79. package/lib/common/rpc/types.d.ts.map +0 -1
  80. package/lib/common/rpc/types.js +0 -3
  81. package/lib/common/rpc/types.js.map +0 -1
  82. package/src/common/fury-extends/one-of.ts +0 -63
  83. package/src/common/protocols/common-server.ts +0 -18
  84. package/src/common/protocols/common.ts +0 -9
  85. package/src/common/proxy/runner.ts +0 -58
  86. package/src/common/proxy/sumi.ts +0 -81
  87. package/src/common/rpc/connection.ts +0 -247
  88. package/src/common/rpc/index.ts +0 -1
  89. package/src/common/rpc/packet.ts +0 -85
  90. package/src/common/rpc/protocol-repository.ts +0 -148
  91. package/src/common/rpc/types.ts +0 -23
@@ -1,58 +0,0 @@
1
- import { EventEmitter } from '@opensumi/events';
2
-
3
- import { IRPCServiceMap, RPCServiceMethod } from '../types';
4
- import { getServiceMethods } from '../utils';
5
-
6
- /**
7
- * Store all executable services
8
- */
9
- export class ServiceRunner {
10
- protected emitter = new EventEmitter<{
11
- servicesUpdate: [services: string[]];
12
- }>();
13
-
14
- private serviceMethodMap = {} as unknown as IRPCServiceMap;
15
-
16
- register(name: string, methodFn: RPCServiceMethod) {
17
- this.serviceMethodMap[name] = methodFn;
18
- this.emitter.emit('servicesUpdate', [name]);
19
- }
20
-
21
- registerService(
22
- service: any,
23
- options?: {
24
- nameConverter?: (str: string) => string;
25
- },
26
- ) {
27
- const serviceNames = [] as string[];
28
- const { nameConverter } = options || {};
29
- const methods = getServiceMethods(service);
30
- for (const method of methods) {
31
- let methodName = method;
32
- if (nameConverter) {
33
- methodName = nameConverter(method);
34
- }
35
-
36
- this.serviceMethodMap[methodName] = service[method].bind(service);
37
- serviceNames.push(methodName);
38
- }
39
-
40
- this.emitter.emit('servicesUpdate', serviceNames);
41
- }
42
-
43
- has(name: PropertyKey) {
44
- return !!this.serviceMethodMap[name];
45
- }
46
-
47
- run(name: PropertyKey, ...args: any[]): any {
48
- return this.serviceMethodMap[name](...args);
49
- }
50
-
51
- getServices() {
52
- return Object.keys(this.serviceMethodMap);
53
- }
54
-
55
- onServicesUpdate(cb: (services: string[]) => void) {
56
- this.emitter.on('servicesUpdate', cb);
57
- }
58
- }
@@ -1,81 +0,0 @@
1
- import { METHOD_NOT_REGISTERED } from '../constants';
2
- import { Connection } from '../rpc/connection';
3
-
4
- import { ProxyBase } from './base';
5
-
6
- export class ProxySumi extends ProxyBase<Connection> {
7
- protected engine = 'sumi' as const;
8
-
9
- protected bindMethods(methods: string[]): void {
10
- for (const method of methods) {
11
- if (method.startsWith('on')) {
12
- this.connection.onNotification(method, async (...args: any[]) => {
13
- this.captureOnNotification(method, args);
14
- try {
15
- await this.runner.run(method, ...args);
16
- } catch (e) {
17
- this.logger.warn(`notification exec ${method} error`, e);
18
- }
19
- });
20
- } else {
21
- this.connection.onRequest(method, async (...args: any[]) => {
22
- const requestId = this.nextRequestId();
23
- this.captureOnRequest(requestId, method, args);
24
-
25
- try {
26
- const result = await this.runner.run(method, ...args);
27
- this.captureOnRequestResult(requestId, method, result);
28
- return result;
29
- } catch (e) {
30
- this.captureOnRequestFail(requestId, method, e);
31
- throw e;
32
- }
33
- });
34
- }
35
- }
36
- }
37
-
38
- public getInvokeProxy<T = any>(): T {
39
- return new Proxy(Object.create(null), {
40
- get: (target: any, p: PropertyKey) => {
41
- const prop = p.toString();
42
- if (!target[prop]) {
43
- target[prop] = async (...args: any[]) => {
44
- await this.connectionPromise.promise;
45
-
46
- // 调用方法为 on 开头时,作为单项通知
47
- if (prop.startsWith('on')) {
48
- this.captureSendNotification(prop, args);
49
- this.connection.sendNotification(prop, ...args);
50
- } else {
51
- // generate a unique requestId to associate request and requestResult
52
- const requestId = this.nextRequestId();
53
- this.captureSendRequest(requestId, prop, args);
54
- try {
55
- const result = await this.connection.sendRequest(prop, ...args);
56
- this.captureSendRequestResult(requestId, prop, result);
57
- return result;
58
- } catch (error) {
59
- this.captureSendRequestFail(requestId, prop, error);
60
- throw error;
61
- }
62
- }
63
- };
64
- }
65
- return target[prop];
66
- },
67
- });
68
- }
69
-
70
- listen(connection: Connection): void {
71
- super.listen(connection);
72
- connection.onRequestNotFound((method) => {
73
- if (!this.runner.has(method)) {
74
- const requestId = this.nextRequestId();
75
- this.captureOnRequest(requestId, method, []);
76
- this.captureOnRequestFail(requestId, method, METHOD_NOT_REGISTERED);
77
- throw METHOD_NOT_REGISTERED;
78
- }
79
- });
80
- }
81
- }
@@ -1,247 +0,0 @@
1
- import { EventEmitter } from '@opensumi/events';
2
- import { DisposableCollection, IDisposable, Disposable, parseError } from '@opensumi/ide-utils';
3
-
4
- import { BaseConnection } from '../connection';
5
- import { METHOD_NOT_REGISTERED } from '../constants';
6
-
7
- import {
8
- CODEC,
9
- ERROR_STATUS,
10
- RPC_TYPE,
11
- createErrorResponsePacket,
12
- createRequestPacket,
13
- createResponsePacket,
14
- reader,
15
- } from './packet';
16
- import { ProtocolRepository } from './protocol-repository';
17
- import {
18
- TGenericNotificationHandler,
19
- TGenericRequestHandler,
20
- TOnNotificationNotFoundHandler,
21
- TOnRequestNotFoundHandler,
22
- TRequestCallback,
23
- } from './types';
24
-
25
- function isPromise<T = any>(obj: any): obj is Promise<T> {
26
- return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
27
- }
28
-
29
- const assert = (condition: any, message: string) => {
30
- if (!condition) {
31
- throw new Error(message);
32
- }
33
- };
34
-
35
- const innerEvents = {
36
- onRequestNotFound: '##onRequestNotFound',
37
- onNotificationNotFound: '##onNotificationNotFound',
38
- } as const;
39
-
40
- export class Connection implements IDisposable {
41
- protected disposable = new DisposableCollection();
42
-
43
- private _binaryEmitter = new EventEmitter<{
44
- [key: string]: [requestId: number, headers: Record<string, any>, args: any[]];
45
- }>();
46
- private _notificationEmitter = new EventEmitter<{
47
- [key: string]: [headers: Record<string, any>, args: any[]];
48
- }>();
49
-
50
- private _innerEventEmitter = new EventEmitter<{
51
- [innerEvents.onNotificationNotFound]: [method: string, headers: Record<string, any>, args: any[]];
52
- [innerEvents.onRequestNotFound]: [requestId: number, method: string, headers: Record<string, any>, args: any[]];
53
- }>();
54
-
55
- private _requestId = 0;
56
- private _callbacks = new Map<number, TRequestCallback>();
57
-
58
- private protocolRepository: ProtocolRepository;
59
-
60
- constructor(private socket: BaseConnection<Uint8Array>) {}
61
-
62
- sendNotification(method: string, ...args: any[]) {
63
- if (!this.protocolRepository.has(method)) {
64
- throw new MethodProtocolNotFoundError(method);
65
- }
66
-
67
- const payload = this.protocolRepository.serializeRequest(method, args);
68
- this.socket.send(createRequestPacket(this._requestId++, RPC_TYPE.Notification, method, {}, payload));
69
- }
70
-
71
- sendRequest(method: string, ...args: any[]) {
72
- return new Promise<any>((resolve, reject) => {
73
- this._callbacks.set(this._requestId, (headers, error, buffer) => {
74
- if (error) {
75
- if (error === METHOD_NOT_REGISTERED) {
76
- resolve(error);
77
- return;
78
- }
79
-
80
- reject(error);
81
- return;
82
- }
83
-
84
- const result = this.protocolRepository.deserializeResult(method, buffer);
85
- resolve(result);
86
- });
87
-
88
- if (!this.protocolRepository.has(method)) {
89
- throw new MethodProtocolNotFoundError(method);
90
- }
91
- const payload = this.protocolRepository.serializeRequest(method, args);
92
- this.socket.send(createRequestPacket(this._requestId++, RPC_TYPE.Request, method, {}, payload));
93
- });
94
- }
95
-
96
- onNotification(method: string, handler: TGenericNotificationHandler): IDisposable {
97
- const handlerWrapper = (headers: Record<string, any>, args: any[]) => {
98
- handler(...args);
99
- };
100
- return Disposable.create(this._notificationEmitter.on(method, handlerWrapper));
101
- }
102
-
103
- onNotificationNotFound(handler: TOnNotificationNotFoundHandler): IDisposable {
104
- const handlerWrapper = (method: string, headers: Record<string, any>, args: any[]) => {
105
- handler(method, args);
106
- };
107
- return Disposable.create(this._innerEventEmitter.on(innerEvents.onNotificationNotFound, handlerWrapper));
108
- }
109
-
110
- private runRequestHandler<T extends (...args: any[]) => any>(
111
- method: string,
112
- handler: T,
113
- requestId: number,
114
- headers: Record<string, any>,
115
- args: any[],
116
- ) {
117
- let result: any;
118
- let error: Error | undefined;
119
-
120
- try {
121
- result = handler(...args);
122
- } catch (err) {
123
- error = err;
124
- }
125
-
126
- if (error) {
127
- this.socket.send(createErrorResponsePacket(requestId, ERROR_STATUS.EXEC_ERROR, {}, error));
128
- } else if (isPromise(result)) {
129
- result
130
- .then((result) => {
131
- const payload = this.protocolRepository.serializeResult(method, result);
132
- this.socket.send(createResponsePacket(requestId, {}, payload));
133
- })
134
- .catch((err) => {
135
- this.socket.send(createErrorResponsePacket(requestId, ERROR_STATUS.EXEC_ERROR, {}, err));
136
- });
137
- } else {
138
- const payload = this.protocolRepository.serializeResult(method, result);
139
- this.socket.send(createResponsePacket(requestId, {}, payload));
140
- }
141
- }
142
-
143
- onRequest<T = any>(method: string, handler: TGenericRequestHandler<T>): IDisposable {
144
- const handlerWrapper = (requestId: number, headers: Record<string, any>, args: any[]) => {
145
- this.runRequestHandler(method, handler, requestId, headers, args);
146
- };
147
- return Disposable.create(this._binaryEmitter.on(method, handlerWrapper));
148
- }
149
-
150
- onRequestNotFound(handler: TOnRequestNotFoundHandler): IDisposable {
151
- const handlerWrapper = (requestId: number, method: string, headers: Record<string, any>, args: any[]) => {
152
- this.runRequestHandler(method, handler, requestId, headers, args);
153
- };
154
-
155
- return Disposable.create(this._innerEventEmitter.on(innerEvents.onRequestNotFound, handlerWrapper));
156
- }
157
-
158
- setProtocolRepository(protocolRepository: ProtocolRepository) {
159
- this.protocolRepository = protocolRepository;
160
- }
161
-
162
- listen() {
163
- const toDispose = this.socket.onMessage((data) => {
164
- reader.reset(data);
165
- reader.skip(1);
166
-
167
- const rpcType = reader.uint8();
168
- const requestId = reader.uint32();
169
- const codec = reader.uint8();
170
-
171
- if (rpcType === RPC_TYPE.Response) {
172
- const callback = this._callbacks.get(requestId);
173
- if (!callback) {
174
- throw new Error(`No callback for request id: ${requestId}`);
175
- }
176
-
177
- const status = reader.uint16();
178
- const headers = readHeaders();
179
-
180
- // if error code is not 0, it's an error
181
- if (status !== ERROR_STATUS.OK) {
182
- // 错误信息用 JSON 格式,方便且兼容性好,可恢复成 Error
183
- assert(codec === CODEC.JSON, 'Error response should be JSON encoded');
184
- const content = reader.stringOfVarUInt32();
185
- const error = parseError(content);
186
- callback(headers, error);
187
- return;
188
- }
189
-
190
- if (codec === CODEC.Binary) {
191
- const contentLen = reader.varUInt32();
192
- const buffer = reader.buffer(contentLen);
193
- callback(headers, undefined, buffer);
194
- return;
195
- }
196
-
197
- const content = reader.stringOfVarUInt32();
198
- if (codec === CODEC.JSON) {
199
- callback(headers, undefined, JSON.parse(content));
200
- } else {
201
- callback(headers, undefined, content);
202
- }
203
- } else if (rpcType === RPC_TYPE.Notification || rpcType === RPC_TYPE.Request) {
204
- const method = reader.stringOfVarUInt32();
205
- const headers = readHeaders();
206
-
207
- const contentLen = reader.varUInt32();
208
- const content = reader.buffer(contentLen);
209
- const argsArray = this.protocolRepository.deserializeRequest(method, content);
210
-
211
- if (rpcType === RPC_TYPE.Request) {
212
- if (this._binaryEmitter.hasListener(method)) {
213
- this._binaryEmitter.emit(method, requestId, headers, argsArray);
214
- } else {
215
- this._innerEventEmitter.emit(innerEvents.onRequestNotFound, requestId, method, headers, argsArray);
216
- }
217
- } else {
218
- if (this._notificationEmitter.hasListener(method)) {
219
- this._notificationEmitter.emit(method, headers, argsArray);
220
- } else {
221
- this._innerEventEmitter.emit(innerEvents.onNotificationNotFound, method, headers, argsArray);
222
- }
223
- }
224
- } else {
225
- throw new Error(`Unknown rpc type: ${rpcType}`);
226
- }
227
-
228
- function readHeaders() {
229
- const header = reader.stringOfVarUInt32();
230
- return JSON.parse(header);
231
- }
232
- });
233
- if (toDispose) {
234
- this.disposable.push(toDispose);
235
- }
236
- }
237
-
238
- dispose(): void {
239
- this.disposable.dispose();
240
- }
241
- }
242
-
243
- export class MethodProtocolNotFoundError extends Error {
244
- constructor(method: string) {
245
- super(`method ${method} not found`);
246
- }
247
- }
@@ -1 +0,0 @@
1
- export * from './types';
@@ -1,85 +0,0 @@
1
- import { BinaryReader } from '@furyjs/fury/dist/lib/reader';
2
- import { BinaryWriter } from '@furyjs/fury/dist/lib/writer';
3
-
4
- import { stringifyError } from '@opensumi/ide-core-common/lib/utils';
5
-
6
- const PROTO_VERSION = 1;
7
-
8
- export enum RPC_TYPE {
9
- Handshake,
10
- Request,
11
- Notification,
12
- Response,
13
- Heartbeat,
14
- }
15
-
16
- export enum CODEC {
17
- Text,
18
- Binary,
19
- JSON,
20
- }
21
-
22
- export enum ERROR_STATUS {
23
- OK,
24
- EXEC_ERROR,
25
- PARSE_ERROR,
26
- SERVER_ERROR,
27
- }
28
-
29
- export const reader = BinaryReader({});
30
- const writer = BinaryWriter({});
31
-
32
- export const createRequestPacket = (
33
- requestId: number,
34
- rpcType: number,
35
- method: string,
36
- headers: Record<string, any>,
37
- payload: Uint8Array,
38
- ) => {
39
- writer.reset();
40
-
41
- writer.uint8(PROTO_VERSION);
42
- writer.uint8(rpcType);
43
- writer.uint32(requestId);
44
- writer.uint8(CODEC.Binary);
45
- writer.stringOfVarUInt32(method);
46
- writer.stringOfVarUInt32(JSON.stringify(headers));
47
- writer.varUInt32(payload.length);
48
- writer.buffer(payload);
49
-
50
- return writer.dump();
51
- };
52
-
53
- export const createResponsePacket = (requestId: number, headers: Record<string, any>, payload: Uint8Array) => {
54
- writer.reset();
55
-
56
- writer.uint8(PROTO_VERSION);
57
- writer.uint8(RPC_TYPE.Response);
58
- writer.uint32(requestId);
59
- writer.uint8(CODEC.Binary);
60
- writer.uint16(ERROR_STATUS.OK);
61
- writer.stringOfVarUInt32(JSON.stringify(headers));
62
- writer.varUInt32(payload.length);
63
- writer.buffer(payload);
64
-
65
- return writer.dump();
66
- };
67
-
68
- export const createErrorResponsePacket = (
69
- requestId: number,
70
- status: number,
71
- headers: Record<string, any>,
72
- error: any,
73
- ) => {
74
- writer.reset();
75
-
76
- writer.uint8(PROTO_VERSION);
77
- writer.uint8(RPC_TYPE.Response);
78
- writer.uint32(requestId);
79
- writer.uint8(CODEC.JSON);
80
- writer.uint16(status);
81
- writer.stringOfVarUInt32(JSON.stringify(headers));
82
- writer.stringOfVarUInt32(stringifyError(error));
83
-
84
- return writer.dump();
85
- };
@@ -1,148 +0,0 @@
1
- import Fury, { Type, TypeDescription } from '@furyjs/fury';
2
-
3
- import { TSumiProtocol, TSumiProtocolMethod } from './types';
4
-
5
- export interface ISerializableRequest {
6
- /**
7
- * Arguments
8
- */
9
- a: any[];
10
- }
11
-
12
- export interface ISerializableResult {
13
- /**
14
- * function execute result
15
- */
16
- r: any;
17
-
18
- /**
19
- * Function Return void or other type that protocol not defined
20
- */
21
- $?: string;
22
- }
23
-
24
- const createResultProto = (name: string, type?: TypeDescription) => {
25
- const props = {
26
- $: Type.string(),
27
- } as Record<string, TypeDescription>;
28
- if (type) {
29
- props.r = type;
30
- }
31
- return Type.object(name, props);
32
- };
33
-
34
- export class ProtocolRepository {
35
- fury = new Fury();
36
-
37
- private serializerMap = {} as Record<
38
- string,
39
- {
40
- request: ReturnType<Fury['registerSerializer']>;
41
- result: ReturnType<Fury['registerSerializer']>;
42
- }
43
- >;
44
-
45
- has(name: string) {
46
- return !!this.serializerMap[name];
47
- }
48
-
49
- loadProtocol(
50
- protocol: TSumiProtocol,
51
- options?: {
52
- nameConverter?: (str: string) => string;
53
- },
54
- ) {
55
- const { methods } = protocol;
56
-
57
- for (const proto of methods) {
58
- let method = proto.method;
59
- if (options?.nameConverter) {
60
- method = options.nameConverter(method);
61
- }
62
-
63
- const copy = {
64
- ...proto,
65
- method,
66
- };
67
-
68
- this.loadProtocolMethod(copy);
69
- }
70
- }
71
-
72
- loadProtocolMethod(methodProtocol: TSumiProtocolMethod) {
73
- const methodName = methodProtocol.method;
74
- const argsTuple = [] as TypeDescription[];
75
-
76
- for (const element of methodProtocol.request) {
77
- argsTuple.push(element.type);
78
- }
79
-
80
- const requestProto = Type.object(methodName + '^', {
81
- a: Type.tuple(argsTuple),
82
- });
83
-
84
- const resultProto = createResultProto(methodName + 'v', methodProtocol.response.type);
85
-
86
- this.serializerMap[methodName] = {
87
- request: this.fury.registerSerializer(requestProto),
88
- result: this.fury.registerSerializer(resultProto),
89
- };
90
- }
91
-
92
- serializeRequest(name: string, args: any[]): Uint8Array {
93
- const newArray = new Array(args.length);
94
- for (let i = 0; i < args.length; i++) {
95
- newArray[i] = args[i];
96
- }
97
-
98
- const payload: ISerializableRequest = {
99
- a: newArray,
100
- };
101
-
102
- return this.serializerMap[name].request.serialize(payload);
103
- }
104
-
105
- deserializeRequest(name: string, buffer: Uint8Array): any[] {
106
- const { a: argsArray } = this.serializerMap[name].request.deserialize(buffer) as ISerializableRequest;
107
- return argsArray;
108
- }
109
-
110
- serializeResult<T = any>(name: string, result: T): Uint8Array {
111
- const payload = {
112
- r: result,
113
- } as ISerializableResult;
114
-
115
- payload.$ = ReturnValueTransfer.replacer(result);
116
-
117
- return this.serializerMap[name].result.serialize(payload);
118
- }
119
-
120
- deserializeResult<T = any>(name: string, buffer: Uint8Array): T {
121
- const payload = this.serializerMap[name].result.deserialize(buffer) as ISerializableResult;
122
-
123
- if (payload.$) {
124
- return ReturnValueTransfer.reviver(payload.$);
125
- }
126
-
127
- return payload.r;
128
- }
129
- }
130
-
131
- class ReturnValueTransfer {
132
- static TypeUndefined = '$undefined';
133
-
134
- static replacer(value: any) {
135
- if (typeof value === 'undefined') {
136
- return ReturnValueTransfer.TypeUndefined;
137
- }
138
- return undefined;
139
- }
140
-
141
- static reviver(value: any): any {
142
- if (value === ReturnValueTransfer.TypeUndefined) {
143
- return undefined;
144
- }
145
-
146
- throw new Error('Not implemented');
147
- }
148
- }
@@ -1,23 +0,0 @@
1
- import { TypeDescription } from '@furyjs/fury';
2
-
3
- import {
4
- RPCProtocol as _RPCProtocol,
5
- RPCProtocolMethod as _RPCProtocolMethod,
6
- Request as _Request,
7
- Response as _Response,
8
- } from '@opensumi/ide-core-common/lib/types/rpc';
9
-
10
- export type TSumiProtocol = _RPCProtocol<TypeDescription>;
11
- export type TSumiProtocolMethod = _RPCProtocolMethod<TypeDescription>;
12
- export type TSumiRequest = _Request<TypeDescription>;
13
- export type TSumiResponse = _Response<TypeDescription>;
14
-
15
- export type TRequestCallback = (headers: Record<string, any>, error?: any, payload?: Uint8Array | string | any) => void;
16
-
17
- export type THandlerResult<R> = R | Promise<R>;
18
-
19
- export type TGenericNotificationHandler = (...args: any[]) => void | Promise<void>;
20
- export type TGenericRequestHandler<R> = (...args: any[]) => THandlerResult<R>;
21
-
22
- export type TOnRequestNotFoundHandler = (method: string, args: any[]) => THandlerResult<any>;
23
- export type TOnNotificationNotFoundHandler = (method: string, args: any[]) => THandlerResult<void>;