@opensumi/ide-connection 3.8.3-next-1741850419.0 → 3.8.3-next-1741917543.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/common/buffers/buffers.d.ts +0 -3
- package/lib/common/buffers/buffers.d.ts.map +1 -1
- package/lib/common/buffers/buffers.js +1 -32
- package/lib/common/buffers/buffers.js.map +1 -1
- package/lib/common/connection/drivers/frame-decoder.d.ts +14 -9
- package/lib/common/connection/drivers/frame-decoder.d.ts.map +1 -1
- package/lib/common/connection/drivers/frame-decoder.js +39 -95
- package/lib/common/connection/drivers/frame-decoder.js.map +1 -1
- package/lib/common/connection/drivers/reconnecting-websocket.d.ts +2 -8
- package/lib/common/connection/drivers/reconnecting-websocket.d.ts.map +1 -1
- package/lib/common/connection/drivers/reconnecting-websocket.js +26 -56
- package/lib/common/connection/drivers/reconnecting-websocket.js.map +1 -1
- package/lib/common/connection/drivers/stream.d.ts.map +1 -1
- package/lib/common/connection/drivers/stream.js +4 -11
- package/lib/common/connection/drivers/stream.js.map +1 -1
- package/lib/common/connection/drivers/ws-websocket.d.ts +1 -8
- package/lib/common/connection/drivers/ws-websocket.d.ts.map +1 -1
- package/lib/common/connection/drivers/ws-websocket.js +7 -77
- package/lib/common/connection/drivers/ws-websocket.js.map +1 -1
- package/lib/common/constants.d.ts +0 -4
- package/lib/common/constants.d.ts.map +1 -1
- package/lib/common/constants.js +1 -5
- package/lib/common/constants.js.map +1 -1
- package/lib/common/fury-extends/one-of.d.ts.map +1 -1
- package/lib/common/fury-extends/one-of.js +0 -3
- package/lib/common/fury-extends/one-of.js.map +1 -1
- package/lib/node/common-channel-handler.d.ts.map +1 -1
- package/lib/node/common-channel-handler.js +2 -1
- package/lib/node/common-channel-handler.js.map +1 -1
- package/package.json +6 -6
- package/src/common/buffers/buffers.ts +0 -40
- package/src/common/connection/drivers/frame-decoder.ts +43 -103
- package/src/common/connection/drivers/reconnecting-websocket.ts +26 -66
- package/src/common/connection/drivers/stream.ts +4 -11
- package/src/common/connection/drivers/ws-websocket.ts +8 -93
- package/src/common/constants.ts +0 -5
- package/src/common/fury-extends/one-of.ts +0 -3
- package/src/node/common-channel-handler.ts +2 -1
|
@@ -1,74 +1,20 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
1
|
import { IDisposable } from '@opensumi/ide-core-common';
|
|
3
2
|
import ReconnectingWebSocket, {
|
|
4
3
|
Options as ReconnectingWebSocketOptions,
|
|
5
4
|
UrlProvider,
|
|
6
5
|
} from '@opensumi/reconnecting-websocket';
|
|
7
6
|
|
|
8
|
-
import { chunkSize } from '../../constants';
|
|
9
|
-
|
|
10
7
|
import { BaseConnection } from './base';
|
|
11
|
-
import { LengthFieldBasedFrameDecoder } from './frame-decoder';
|
|
12
8
|
|
|
13
9
|
import type { ErrorEvent } from '@opensumi/reconnecting-websocket';
|
|
14
10
|
|
|
15
11
|
export class ReconnectingWebSocketConnection extends BaseConnection<Uint8Array> {
|
|
16
|
-
|
|
17
|
-
private sendQueue: Array<{ data: Uint8Array; resolve: () => void; reject: (error: Error) => void }> = [];
|
|
18
|
-
private sending = false;
|
|
19
|
-
|
|
20
|
-
protected constructor(private socket: ReconnectingWebSocket) {
|
|
12
|
+
constructor(private socket: ReconnectingWebSocket) {
|
|
21
13
|
super();
|
|
22
|
-
|
|
23
|
-
if (socket.binaryType === 'arraybuffer') {
|
|
24
|
-
this.socket.addEventListener('message', this.arrayBufferHandler);
|
|
25
|
-
} else if (socket.binaryType === 'blob') {
|
|
26
|
-
throw new Error('blob is not implemented');
|
|
27
|
-
}
|
|
28
14
|
}
|
|
29
15
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
this.sending = true;
|
|
35
|
-
|
|
36
|
-
while (this.sendQueue.length > 0) {
|
|
37
|
-
const { data, resolve, reject } = this.sendQueue[0];
|
|
38
|
-
let handle: { get: () => Uint8Array; dispose: () => void } | null = null;
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
handle = LengthFieldBasedFrameDecoder.construct(data).dumpAndOwn();
|
|
42
|
-
const packet = handle.get();
|
|
43
|
-
|
|
44
|
-
for (let i = 0; i < packet.byteLength; i += chunkSize) {
|
|
45
|
-
await new Promise<void>((resolve) => {
|
|
46
|
-
const chunk = packet.subarray(i, Math.min(i + chunkSize, packet.byteLength));
|
|
47
|
-
this.socket.send(chunk);
|
|
48
|
-
resolve();
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
resolve();
|
|
53
|
-
} catch (error) {
|
|
54
|
-
console.error('[ReconnectingWebSocket] Error sending data:', error);
|
|
55
|
-
reject(error);
|
|
56
|
-
} finally {
|
|
57
|
-
if (handle) {
|
|
58
|
-
handle.dispose();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
this.sendQueue.shift();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
this.sending = false;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
send(data: Uint8Array): Promise<void> {
|
|
68
|
-
return new Promise((resolve, reject) => {
|
|
69
|
-
this.sendQueue.push({ data, resolve, reject });
|
|
70
|
-
this.processSendQueue();
|
|
71
|
-
});
|
|
16
|
+
send(data: Uint8Array): void {
|
|
17
|
+
this.socket.send(data);
|
|
72
18
|
}
|
|
73
19
|
|
|
74
20
|
isOpen(): boolean {
|
|
@@ -83,8 +29,29 @@ export class ReconnectingWebSocketConnection extends BaseConnection<Uint8Array>
|
|
|
83
29
|
},
|
|
84
30
|
};
|
|
85
31
|
}
|
|
32
|
+
|
|
86
33
|
onMessage(cb: (data: Uint8Array) => void): IDisposable {
|
|
87
|
-
|
|
34
|
+
const handler = (e: MessageEvent) => {
|
|
35
|
+
let buffer: Promise<ArrayBuffer>;
|
|
36
|
+
if (e.data instanceof Blob) {
|
|
37
|
+
buffer = e.data.arrayBuffer();
|
|
38
|
+
} else if (e.data instanceof ArrayBuffer) {
|
|
39
|
+
buffer = Promise.resolve(e.data);
|
|
40
|
+
} else if (e.data?.constructor?.name === 'Buffer') {
|
|
41
|
+
// Compatibility with nodejs Buffer in test environment
|
|
42
|
+
buffer = Promise.resolve(e.data);
|
|
43
|
+
} else {
|
|
44
|
+
throw new Error('unknown message type, expect Blob or ArrayBuffer, received: ' + typeof e.data);
|
|
45
|
+
}
|
|
46
|
+
buffer.then((v) => cb(new Uint8Array(v, 0, v.byteLength)));
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
this.socket.addEventListener('message', handler);
|
|
50
|
+
return {
|
|
51
|
+
dispose: () => {
|
|
52
|
+
this.socket.removeEventListener('message', handler);
|
|
53
|
+
},
|
|
54
|
+
};
|
|
88
55
|
}
|
|
89
56
|
onceClose(cb: (code?: number, reason?: string) => void): IDisposable {
|
|
90
57
|
const disposable = this.onClose(wrapper);
|
|
@@ -124,15 +91,8 @@ export class ReconnectingWebSocketConnection extends BaseConnection<Uint8Array>
|
|
|
124
91
|
};
|
|
125
92
|
}
|
|
126
93
|
|
|
127
|
-
private arrayBufferHandler = (e: MessageEvent<ArrayBuffer>) => {
|
|
128
|
-
const buffer: ArrayBuffer = e.data;
|
|
129
|
-
this.decoder.push(new Uint8Array(buffer, 0, buffer.byteLength));
|
|
130
|
-
};
|
|
131
|
-
|
|
132
94
|
dispose(): void {
|
|
133
|
-
|
|
134
|
-
this.sendQueue = [];
|
|
135
|
-
this.sending = false;
|
|
95
|
+
// do nothing
|
|
136
96
|
}
|
|
137
97
|
|
|
138
98
|
static forURL(url: UrlProvider, protocols?: string | string[], options?: ReconnectingWebSocketOptions) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
1
|
import { IDisposable } from '@opensumi/ide-core-common';
|
|
3
2
|
|
|
4
3
|
import { BaseConnection } from './base';
|
|
@@ -22,16 +21,10 @@ export class StreamConnection extends BaseConnection<Uint8Array> {
|
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
send(data: Uint8Array): void {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
console.error('Failed to write data:', error);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
} finally {
|
|
33
|
-
handle.dispose();
|
|
34
|
-
}
|
|
24
|
+
const result = LengthFieldBasedFrameDecoder.construct(data);
|
|
25
|
+
this.writable.write(result, () => {
|
|
26
|
+
// TODO: logger error
|
|
27
|
+
});
|
|
35
28
|
}
|
|
36
29
|
|
|
37
30
|
onMessage(cb: (data: Uint8Array) => void): IDisposable {
|
|
@@ -1,102 +1,24 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
1
|
import { IDisposable } from '@opensumi/ide-core-common';
|
|
3
2
|
|
|
4
|
-
import { chunkSize } from '../../constants';
|
|
5
|
-
|
|
6
3
|
import { BaseConnection } from './base';
|
|
7
|
-
import { LengthFieldBasedFrameDecoder } from './frame-decoder';
|
|
8
4
|
|
|
9
5
|
import type WebSocket from 'ws';
|
|
10
6
|
|
|
11
|
-
interface SendQueueItem {
|
|
12
|
-
data: Uint8Array;
|
|
13
|
-
resolve: () => void;
|
|
14
|
-
reject: (error: Error) => void;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
7
|
export class WSWebSocketConnection extends BaseConnection<Uint8Array> {
|
|
18
|
-
protected decoder = new LengthFieldBasedFrameDecoder();
|
|
19
|
-
private static readonly MAX_QUEUE_SIZE = 1000; // 限制队列长度
|
|
20
|
-
|
|
21
|
-
private sendQueue: SendQueueItem[] = [];
|
|
22
|
-
private pendingSize = 0;
|
|
23
|
-
private sending = false;
|
|
24
|
-
|
|
25
8
|
constructor(public socket: WebSocket) {
|
|
26
9
|
super();
|
|
27
|
-
this.socket.on('message', (data: Buffer) => {
|
|
28
|
-
this.decoder.push(data);
|
|
29
|
-
});
|
|
30
10
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (this.sending) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
this.sending = true;
|
|
37
|
-
|
|
38
|
-
while (this.sendQueue.length > 0) {
|
|
39
|
-
const { data, resolve, reject } = this.sendQueue[0];
|
|
40
|
-
let handle: { get: () => Uint8Array; dispose: () => void } | null = null;
|
|
41
|
-
|
|
42
|
-
try {
|
|
43
|
-
handle = LengthFieldBasedFrameDecoder.construct(data).dumpAndOwn();
|
|
44
|
-
const packet = handle.get();
|
|
45
|
-
|
|
46
|
-
for (let i = 0; i < packet.byteLength; i += chunkSize) {
|
|
47
|
-
if (!this.isOpen()) {
|
|
48
|
-
throw new Error('Connection closed while sending');
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
await new Promise<void>((resolve, reject) => {
|
|
52
|
-
const chunk = packet.subarray(i, Math.min(i + chunkSize, packet.byteLength));
|
|
53
|
-
this.socket.send(chunk, { binary: true }, (error?: Error) => {
|
|
54
|
-
if (error) {
|
|
55
|
-
reject(error);
|
|
56
|
-
} else {
|
|
57
|
-
resolve();
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
resolve();
|
|
64
|
-
} catch (error) {
|
|
65
|
-
reject(error instanceof Error ? error : new Error(String(error)));
|
|
66
|
-
} finally {
|
|
67
|
-
if (handle) {
|
|
68
|
-
try {
|
|
69
|
-
handle.dispose();
|
|
70
|
-
} catch (error) {
|
|
71
|
-
console.warn('[WSWebSocket] Error disposing handle:', error);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
this.pendingSize -= this.sendQueue[0].data.byteLength;
|
|
75
|
-
this.sendQueue.shift();
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
this.sending = false;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
send(data: Uint8Array): Promise<void> {
|
|
83
|
-
return new Promise<void>((resolve, reject) => {
|
|
84
|
-
// 检查队列大小限制
|
|
85
|
-
if (this.sendQueue.length >= WSWebSocketConnection.MAX_QUEUE_SIZE) {
|
|
86
|
-
reject(new Error('Send queue full'));
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
this.pendingSize += data.byteLength;
|
|
91
|
-
this.sendQueue.push({ data, resolve, reject });
|
|
92
|
-
this.processSendQueue().catch((error) => {
|
|
93
|
-
console.error('[WSWebSocket] Error processing queue:', error);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
11
|
+
send(data: Uint8Array): void {
|
|
12
|
+
this.socket.send(data);
|
|
96
13
|
}
|
|
97
14
|
|
|
98
15
|
onMessage(cb: (data: Uint8Array) => void): IDisposable {
|
|
99
|
-
|
|
16
|
+
this.socket.on('message', cb);
|
|
17
|
+
return {
|
|
18
|
+
dispose: () => {
|
|
19
|
+
this.socket.off('message', cb);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
100
22
|
}
|
|
101
23
|
onceClose(cb: () => void): IDisposable {
|
|
102
24
|
this.socket.once('close', cb);
|
|
@@ -113,12 +35,5 @@ export class WSWebSocketConnection extends BaseConnection<Uint8Array> {
|
|
|
113
35
|
|
|
114
36
|
dispose(): void {
|
|
115
37
|
this.socket.removeAllListeners();
|
|
116
|
-
// 拒绝所有待发送的消息
|
|
117
|
-
while (this.sendQueue.length > 0) {
|
|
118
|
-
const { reject } = this.sendQueue.shift()!;
|
|
119
|
-
reject(new Error('Connection disposed'));
|
|
120
|
-
}
|
|
121
|
-
this.pendingSize = 0;
|
|
122
|
-
this.sending = false;
|
|
123
38
|
}
|
|
124
39
|
}
|
package/src/common/constants.ts
CHANGED
|
@@ -42,7 +42,8 @@ export class CommonChannelHandler extends BaseCommonChannelHandler implements We
|
|
|
42
42
|
...this.options.wsServerOptions,
|
|
43
43
|
});
|
|
44
44
|
this.wsServer.on('connection', (connection: WebSocket) => {
|
|
45
|
-
|
|
45
|
+
const wsConnection = new WSWebSocketConnection(connection);
|
|
46
|
+
this.receiveConnection(wsConnection);
|
|
46
47
|
});
|
|
47
48
|
}
|
|
48
49
|
|