@opensumi/ide-connection 3.0.2-next-1715826860.0 → 3.0.2-next-1715852509.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.
- package/lib/browser/ws-channel-handler.d.ts +0 -6
- package/lib/browser/ws-channel-handler.d.ts.map +1 -1
- package/lib/browser/ws-channel-handler.js +9 -68
- package/lib/browser/ws-channel-handler.js.map +1 -1
- package/lib/common/connection/drivers/reconnecting-websocket.d.ts +1 -1
- package/lib/common/connection/drivers/reconnecting-websocket.d.ts.map +1 -1
- package/lib/common/connection/drivers/reconnecting-websocket.js +1 -1
- package/lib/common/connection/drivers/reconnecting-websocket.js.map +1 -1
- package/lib/common/fury-extends/one-of.d.ts +1 -2
- package/lib/common/fury-extends/one-of.d.ts.map +1 -1
- package/lib/common/fury-extends/one-of.js +5 -12
- package/lib/common/fury-extends/one-of.js.map +1 -1
- package/lib/common/rpc/connection.d.ts.map +1 -1
- package/lib/common/rpc/connection.js +6 -3
- package/lib/common/rpc/connection.js.map +1 -1
- package/lib/common/server-handler.d.ts +4 -4
- package/lib/common/server-handler.d.ts.map +1 -1
- package/lib/common/server-handler.js +12 -25
- package/lib/common/server-handler.js.map +1 -1
- package/lib/common/ws-channel.d.ts +23 -77
- package/lib/common/ws-channel.d.ts.map +1 -1
- package/lib/common/ws-channel.js +4 -77
- package/lib/common/ws-channel.js.map +1 -1
- package/lib/node/ws.d.ts.map +1 -1
- package/lib/node/ws.js +0 -1
- package/lib/node/ws.js.map +1 -1
- package/package.json +6 -6
- package/src/browser/ws-channel-handler.ts +10 -74
- package/src/common/connection/drivers/reconnecting-websocket.ts +3 -5
- package/src/common/fury-extends/one-of.ts +3 -12
- package/src/common/rpc/connection.ts +149 -148
- package/src/common/server-handler.ts +18 -41
- package/src/common/ws-channel.ts +35 -143
- package/src/node/ws.ts +0 -1
package/src/common/ws-channel.ts
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
import { Type } from '@furyjs/fury';
|
|
2
2
|
|
|
3
3
|
import { EventEmitter } from '@opensumi/events';
|
|
4
|
-
import {
|
|
5
|
-
DisposableCollection,
|
|
6
|
-
DisposableStore,
|
|
7
|
-
EventQueue,
|
|
8
|
-
StateTracer,
|
|
9
|
-
randomString,
|
|
10
|
-
} from '@opensumi/ide-core-common';
|
|
4
|
+
import { DisposableCollection, DisposableStore, EventQueue } from '@opensumi/ide-core-common';
|
|
11
5
|
|
|
12
6
|
import { IConnectionShape } from './connection/types';
|
|
13
|
-
import {
|
|
7
|
+
import { oneOf7 } from './fury-extends/one-of';
|
|
14
8
|
import { ISumiConnectionOptions, SumiConnection } from './rpc/connection';
|
|
15
9
|
import { ILogger } from './types';
|
|
16
10
|
|
|
@@ -32,6 +26,27 @@ export interface PongMessage {
|
|
|
32
26
|
clientId: string;
|
|
33
27
|
}
|
|
34
28
|
|
|
29
|
+
/**
|
|
30
|
+
* `open` message is used to open a new channel.
|
|
31
|
+
* `path` is used to identify which handler should be used to handle the channel.
|
|
32
|
+
* `clientId` is used to identify the client.
|
|
33
|
+
*/
|
|
34
|
+
export interface OpenMessage {
|
|
35
|
+
kind: 'open';
|
|
36
|
+
id: string;
|
|
37
|
+
path: string;
|
|
38
|
+
clientId: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* when server receive a `open` message, it should reply a `server-ready` message.
|
|
43
|
+
* this is indicate that the channel is ready to use.
|
|
44
|
+
*/
|
|
45
|
+
export interface ServerReadyMessage {
|
|
46
|
+
kind: 'server-ready';
|
|
47
|
+
id: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
35
50
|
/**
|
|
36
51
|
* `data` message indicate that the channel has received some data.
|
|
37
52
|
* the `content` field is the data, it should be a string.
|
|
@@ -55,6 +70,15 @@ export interface CloseMessage {
|
|
|
55
70
|
reason: string;
|
|
56
71
|
}
|
|
57
72
|
|
|
73
|
+
export type ChannelMessage =
|
|
74
|
+
| PingMessage
|
|
75
|
+
| PongMessage
|
|
76
|
+
| OpenMessage
|
|
77
|
+
| ServerReadyMessage
|
|
78
|
+
| DataMessage
|
|
79
|
+
| BinaryMessage
|
|
80
|
+
| CloseMessage;
|
|
81
|
+
|
|
58
82
|
export interface IWSChannelCreateOptions {
|
|
59
83
|
/**
|
|
60
84
|
* every channel's unique id, it only used in client to server architecture.
|
|
@@ -148,14 +172,6 @@ export class WSChannel {
|
|
|
148
172
|
this._isServerReady = false;
|
|
149
173
|
}
|
|
150
174
|
|
|
151
|
-
onServerReady(cb: () => void) {
|
|
152
|
-
if (this._isServerReady) {
|
|
153
|
-
cb();
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
return this.emitter.on('open', cb);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
175
|
resume() {
|
|
160
176
|
this._isServerReady = true;
|
|
161
177
|
if (this.sendQueue) {
|
|
@@ -169,11 +185,7 @@ export class WSChannel {
|
|
|
169
185
|
dispatch(msg: ChannelMessage) {
|
|
170
186
|
switch (msg.kind) {
|
|
171
187
|
case 'server-ready':
|
|
172
|
-
this.stateTracer.fulfill(msg.token);
|
|
173
188
|
this.resume();
|
|
174
|
-
if (this.timer) {
|
|
175
|
-
clearTimeout(this.timer);
|
|
176
|
-
}
|
|
177
189
|
this.emitter.emit('open', msg.id);
|
|
178
190
|
break;
|
|
179
191
|
case 'data':
|
|
@@ -185,55 +197,16 @@ export class WSChannel {
|
|
|
185
197
|
}
|
|
186
198
|
}
|
|
187
199
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* @param connectionToken 一个 connection token 用于在全链路中追踪一个 channel 的生命周期,防止 channel 被重复打开
|
|
192
|
-
*/
|
|
193
|
-
open(path: string, clientId: string, connectionToken = randomString(16)) {
|
|
200
|
+
open(path: string, clientId: string) {
|
|
194
201
|
this.channelPath = path;
|
|
195
|
-
|
|
196
|
-
if (this.stateTracer.has(connectionToken)) {
|
|
197
|
-
this.logger.warn(
|
|
198
|
-
`channel already opened or in progress, path: ${path}, clientId: ${clientId}, connectionToken: ${connectionToken}`,
|
|
199
|
-
);
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
this.stateTracer.record(connectionToken);
|
|
204
|
-
|
|
205
202
|
this.connection.send(
|
|
206
203
|
stringify({
|
|
207
204
|
kind: 'open',
|
|
208
205
|
id: this.id,
|
|
209
206
|
path,
|
|
210
207
|
clientId,
|
|
211
|
-
connectionToken,
|
|
212
208
|
}),
|
|
213
209
|
);
|
|
214
|
-
|
|
215
|
-
if (this._ensureServerReady) {
|
|
216
|
-
this.ensureOpenSend(path, clientId, connectionToken);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return connectionToken;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
protected timer: NodeJS.Timeout;
|
|
223
|
-
/**
|
|
224
|
-
* 启动定时器,确保 server-ready 消息在一定时间内到达
|
|
225
|
-
*/
|
|
226
|
-
protected ensureOpenSend(path: string, clientId: string, connectionToken: string) {
|
|
227
|
-
if (this.timer) {
|
|
228
|
-
clearTimeout(this.timer);
|
|
229
|
-
}
|
|
230
|
-
this.timer = setTimeout(() => {
|
|
231
|
-
if (this._isServerReady) {
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
this.stateTracer.delete(connectionToken);
|
|
235
|
-
this.open(path, clientId, connectionToken);
|
|
236
|
-
}, 500);
|
|
237
210
|
}
|
|
238
211
|
|
|
239
212
|
send(content: string) {
|
|
@@ -287,61 +260,23 @@ export class WSChannel {
|
|
|
287
260
|
}
|
|
288
261
|
|
|
289
262
|
dispose() {
|
|
290
|
-
if (this.timer) {
|
|
291
|
-
clearTimeout(this.timer);
|
|
292
|
-
}
|
|
293
|
-
this.sendQueue = [];
|
|
294
263
|
this._disposables.dispose();
|
|
295
264
|
}
|
|
296
265
|
}
|
|
297
266
|
|
|
298
|
-
interface IWSServerChannelCreateOptions extends IWSChannelCreateOptions {
|
|
299
|
-
clientId: string;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
267
|
/**
|
|
303
268
|
* The server side channel, it will send a `server-ready` message after it receive a `open` message.
|
|
304
269
|
*/
|
|
305
270
|
export class WSServerChannel extends WSChannel {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
clientId: string;
|
|
309
|
-
constructor(public connection: IConnectionShape<Uint8Array>, options: IWSServerChannelCreateOptions) {
|
|
310
|
-
super(connection, options);
|
|
311
|
-
this.clientId = options.clientId;
|
|
312
|
-
}
|
|
313
|
-
serverReady(token: string) {
|
|
271
|
+
serverReady() {
|
|
314
272
|
this.connection.send(
|
|
315
273
|
stringify({
|
|
316
274
|
kind: 'server-ready',
|
|
317
275
|
id: this.id,
|
|
318
|
-
token,
|
|
319
276
|
}),
|
|
320
277
|
);
|
|
321
278
|
}
|
|
322
|
-
|
|
323
|
-
dispatch(msg: ChannelMessage) {
|
|
324
|
-
switch (msg.kind) {
|
|
325
|
-
case 'data':
|
|
326
|
-
this.emitter.emit('message', msg.content);
|
|
327
|
-
break;
|
|
328
|
-
case 'binary':
|
|
329
|
-
this.emitter.emit('binary', msg.binary);
|
|
330
|
-
break;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
279
|
}
|
|
334
|
-
|
|
335
|
-
export type ChannelMessage =
|
|
336
|
-
| PingMessage
|
|
337
|
-
| PongMessage
|
|
338
|
-
| OpenMessage
|
|
339
|
-
| ServerReadyMessage
|
|
340
|
-
| DataMessage
|
|
341
|
-
| BinaryMessage
|
|
342
|
-
| CloseMessage
|
|
343
|
-
| ErrorMessage;
|
|
344
|
-
|
|
345
280
|
export const PingProtocol = Type.object('ping', {
|
|
346
281
|
clientId: Type.string(),
|
|
347
282
|
id: Type.string(),
|
|
@@ -352,56 +287,14 @@ export const PongProtocol = Type.object('pong', {
|
|
|
352
287
|
id: Type.string(),
|
|
353
288
|
});
|
|
354
289
|
|
|
355
|
-
/**
|
|
356
|
-
* `open` message is used to open a new channel.
|
|
357
|
-
* `path` is used to identify which handler should be used to handle the channel.
|
|
358
|
-
* `clientId` is used to identify the client.
|
|
359
|
-
*/
|
|
360
|
-
export interface OpenMessage {
|
|
361
|
-
kind: 'open';
|
|
362
|
-
id: string;
|
|
363
|
-
path: string;
|
|
364
|
-
clientId: string;
|
|
365
|
-
connectionToken: string;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
290
|
export const OpenProtocol = Type.object('open', {
|
|
369
291
|
clientId: Type.string(),
|
|
370
292
|
id: Type.string(),
|
|
371
293
|
path: Type.string(),
|
|
372
|
-
connectionToken: Type.string(),
|
|
373
294
|
});
|
|
374
295
|
|
|
375
|
-
/**
|
|
376
|
-
* when server receive a `open` message, it should reply a `server-ready` message.
|
|
377
|
-
* this is indicate that the channel is ready to use.
|
|
378
|
-
*/
|
|
379
|
-
export interface ServerReadyMessage {
|
|
380
|
-
kind: 'server-ready';
|
|
381
|
-
id: string;
|
|
382
|
-
token: string;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
296
|
export const ServerReadyProtocol = Type.object('server-ready', {
|
|
386
297
|
id: Type.string(),
|
|
387
|
-
token: Type.string(),
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
export enum ErrorMessageCode {
|
|
391
|
-
ChannelNotFound = 1,
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
export interface ErrorMessage {
|
|
395
|
-
kind: 'error';
|
|
396
|
-
id: string;
|
|
397
|
-
code: ErrorMessageCode;
|
|
398
|
-
message: string;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
export const ErrorProtocol = Type.object('error', {
|
|
402
|
-
id: Type.string(),
|
|
403
|
-
code: Type.uint16(),
|
|
404
|
-
message: Type.string(),
|
|
405
298
|
});
|
|
406
299
|
|
|
407
300
|
export const DataProtocol = Type.object('data', {
|
|
@@ -420,7 +313,7 @@ export const CloseProtocol = Type.object('close', {
|
|
|
420
313
|
reason: Type.string(),
|
|
421
314
|
});
|
|
422
315
|
|
|
423
|
-
const serializer =
|
|
316
|
+
const serializer = oneOf7([
|
|
424
317
|
PingProtocol,
|
|
425
318
|
PongProtocol,
|
|
426
319
|
OpenProtocol,
|
|
@@ -428,7 +321,6 @@ const serializer = oneOf([
|
|
|
428
321
|
DataProtocol,
|
|
429
322
|
BinaryProtocol,
|
|
430
323
|
CloseProtocol,
|
|
431
|
-
ErrorProtocol,
|
|
432
324
|
]);
|
|
433
325
|
|
|
434
326
|
export function stringify(obj: ChannelMessage): Uint8Array {
|