@hydranium/client-theia 1.0.0-next.50 → 1.0.0-next.52
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/connection-diagnostics-contribution.d.ts +69 -0
- package/lib/browser/connection-diagnostics-contribution.d.ts.map +1 -0
- package/lib/browser/connection-diagnostics-contribution.js +153 -0
- package/lib/browser/connection-diagnostics-contribution.js.map +1 -0
- package/lib/browser/index.d.ts +2 -0
- package/lib/browser/index.d.ts.map +1 -1
- package/lib/browser/index.js +2 -0
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/session-aware-connection-source.d.ts +94 -0
- package/lib/browser/session-aware-connection-source.d.ts.map +1 -0
- package/lib/browser/session-aware-connection-source.js +163 -0
- package/lib/browser/session-aware-connection-source.js.map +1 -0
- package/lib/common/connection-resilience-options.d.ts +24 -0
- package/lib/common/connection-resilience-options.d.ts.map +1 -0
- package/lib/common/connection-resilience-options.js +11 -0
- package/lib/common/connection-resilience-options.js.map +1 -0
- package/lib/common/framed-socket-write-buffer.d.ts +121 -0
- package/lib/common/framed-socket-write-buffer.d.ts.map +1 -0
- package/lib/common/framed-socket-write-buffer.js +176 -0
- package/lib/common/framed-socket-write-buffer.js.map +1 -0
- package/lib/common/index.d.ts +11 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +30 -0
- package/lib/common/index.js.map +1 -0
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +1 -0
- package/lib/node/index.js.map +1 -1
- package/lib/node/session-bound-frontend-connection-service.d.ts +73 -0
- package/lib/node/session-bound-frontend-connection-service.d.ts.map +1 -0
- package/lib/node/session-bound-frontend-connection-service.js +124 -0
- package/lib/node/session-bound-frontend-connection-service.js.map +1 -0
- package/package.json +12 -3
- package/src/browser/connection-diagnostics-contribution.ts +142 -0
- package/src/browser/index.ts +2 -0
- package/src/browser/session-aware-connection-source.ts +175 -0
- package/src/common/connection-resilience-options.ts +24 -0
- package/src/common/framed-socket-write-buffer.ts +212 -0
- package/src/common/index.ts +14 -0
- package/src/node/index.ts +1 -0
- package/src/node/session-bound-frontend-connection-service.ts +151 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hydranium/client-theia",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.52",
|
|
4
4
|
"description": "Cross-head Theia client primitives shared by the hydranium protocol heads. Provides the socket-forwarding connection-handler base that the data-server and GLSP Theia integrations subclass, plus the preference-driven Output-channel logger; carries no head-specific dependency.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hydranium",
|
|
@@ -36,6 +36,14 @@
|
|
|
36
36
|
"types": "./lib/browser/index.d.ts",
|
|
37
37
|
"default": "./lib/browser/index.js"
|
|
38
38
|
},
|
|
39
|
+
"./common": {
|
|
40
|
+
"types": "./lib/common/index.d.ts",
|
|
41
|
+
"default": "./lib/common/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./lib/common": {
|
|
44
|
+
"types": "./lib/common/index.d.ts",
|
|
45
|
+
"default": "./lib/common/index.js"
|
|
46
|
+
},
|
|
39
47
|
"./node": {
|
|
40
48
|
"types": "./lib/node/index.d.ts",
|
|
41
49
|
"default": "./lib/node/index.js"
|
|
@@ -70,7 +78,7 @@
|
|
|
70
78
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
71
79
|
},
|
|
72
80
|
"devDependencies": {
|
|
73
|
-
"@hydranium/protocol": "1.0.0-next.
|
|
81
|
+
"@hydranium/protocol": "1.0.0-next.52",
|
|
74
82
|
"@theia/core": "^1.70.0",
|
|
75
83
|
"@theia/output": "^1.70.0",
|
|
76
84
|
"@types/node": "^22.0.0",
|
|
@@ -80,7 +88,7 @@
|
|
|
80
88
|
"typescript": "^5.8.0"
|
|
81
89
|
},
|
|
82
90
|
"peerDependencies": {
|
|
83
|
-
"@hydranium/protocol": "1.0.0-next.
|
|
91
|
+
"@hydranium/protocol": "1.0.0-next.52",
|
|
84
92
|
"@theia/core": "^1.70.0",
|
|
85
93
|
"@theia/output": "^1.70.0",
|
|
86
94
|
"inversify": "^6.0.0"
|
|
@@ -91,5 +99,6 @@
|
|
|
91
99
|
"publishConfig": {
|
|
92
100
|
"access": "public"
|
|
93
101
|
},
|
|
102
|
+
"//peerDependencies": "@theia/core stays at 1.70 here, matching the sibling Theia packages, even though the connection-resilience bindings need 1.71: Theia only began binding `SocketWriteBuffer` then, and before that each connection built one privately, so there is no binding to rebind. Rather than split the package's floor for one feature, `bindConnectionResilience` probes for that binding and warns instead of installing when it is absent — a capability check rather than a version parse, so it answers about the container in front of it. Everything else the feature touches exists at 1.70, and the emitted declarations deliberately name no type that 1.70 lacks, so the package still compiles there. Verified by unpacking 1.70, 1.71 and 1.72, not inferred from a changelog.",
|
|
94
103
|
"//prepack": "The publish guard, and it deliberately is NOT a `prepare`: npm runs a workspace `prepare` BEFORE the root `postinstall` that applies patches/vscode-jsonrpc+9.0.1.patch, so building there fails on a cold clone and npm rolls the entire install back. `prepack` runs only when a tarball is made (`npm pack`, `npm publish`) and never on install, so it cannot break the install it has no business touching. It FAILS rather than rebuilds, because the rebuild is exactly the part that ordering defeats. What it defends against: `files` lists `lib`, `lib` is gitignored, and a `files` entry matching nothing is skipped SILENTLY — so `npm publish` from an unbuilt tree emits a tarball of `src` and nothing else, with no error."
|
|
95
104
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
import { MessageService, nls } from '@theia/core';
|
|
11
|
+
import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application-contribution';
|
|
12
|
+
import { ConnectionStatus, ConnectionStatusService } from '@theia/core/lib/browser/connection-status-service';
|
|
13
|
+
import { WebSocketConnectionSource } from '@theia/core/lib/browser/messaging/ws-connection-source';
|
|
14
|
+
import { inject, injectable, type interfaces } from '@theia/core/shared/inversify';
|
|
15
|
+
import { type ConnectionBufferOverflow } from '../common/framed-socket-write-buffer';
|
|
16
|
+
import { ChannelLogger } from './channel-logger';
|
|
17
|
+
import { SessionAwareConnectionSource } from './session-aware-connection-source';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Records the browser side of the websocket lifecycle into the adopter's Output
|
|
21
|
+
* channel, and tells the user the one thing they cannot recover from on their
|
|
22
|
+
* own.
|
|
23
|
+
*
|
|
24
|
+
* Theia reports an outage only as an "Offline" status-bar item, which cannot
|
|
25
|
+
* distinguish a plain network drop from the server having stopped answering on a
|
|
26
|
+
* socket that is still up. The latter is what a connection defect looks like
|
|
27
|
+
* from here, so the two are logged differently — that distinction is what says
|
|
28
|
+
* whether reopening an editor helps or only a reload does.
|
|
29
|
+
*
|
|
30
|
+
* Purely observational apart from the overflow message: nothing here changes how
|
|
31
|
+
* the connection behaves.
|
|
32
|
+
*/
|
|
33
|
+
@injectable()
|
|
34
|
+
export class ConnectionDiagnosticsContribution implements FrontendApplicationContribution {
|
|
35
|
+
@inject(WebSocketConnectionSource) protected readonly connectionSource: WebSocketConnectionSource;
|
|
36
|
+
@inject(ConnectionStatusService) protected readonly connectionStatus: ConnectionStatusService;
|
|
37
|
+
@inject(ChannelLogger) protected readonly logger: ChannelLogger;
|
|
38
|
+
@inject(MessageService) protected readonly messageService: MessageService;
|
|
39
|
+
|
|
40
|
+
/** Main channels created so far; more than one means the server did not recognise this frontend. */
|
|
41
|
+
protected channelCount = 0;
|
|
42
|
+
protected socketClosedAt?: number;
|
|
43
|
+
protected offlineAt?: number;
|
|
44
|
+
|
|
45
|
+
initialize(): void {
|
|
46
|
+
this.connectionSource.onSocketDidOpen(() => this.handleSocketOpen());
|
|
47
|
+
this.connectionSource.onSocketDidClose(() => this.handleSocketClose());
|
|
48
|
+
this.connectionSource.onConnectionDidOpen(() => this.handleChannelOpen());
|
|
49
|
+
this.connectionStatus.onStatusChange(status => this.handleStatusChange(status));
|
|
50
|
+
if (this.connectionSource instanceof SessionAwareConnectionSource) {
|
|
51
|
+
this.connectionSource.onBufferOverflow(overflow => this.handleBufferOverflow(overflow));
|
|
52
|
+
}
|
|
53
|
+
// The socket is opened by the preloader, before this contribution exists, so the first
|
|
54
|
+
// `onSocketDidOpen` has already fired; state the current situation instead.
|
|
55
|
+
this.logger.info(`diagnostics active; socket ${this.socketId()}, backend ${this.statusName(this.connectionStatus.currentStatus)}`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
protected handleSocketOpen(): void {
|
|
59
|
+
const downFor = this.socketClosedAt === undefined ? '' : ` after ${Date.now() - this.socketClosedAt} ms down`;
|
|
60
|
+
this.socketClosedAt = undefined;
|
|
61
|
+
this.logger.info(`websocket connected: socket ${this.socketId()}${downFor}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
protected handleSocketClose(): void {
|
|
65
|
+
this.socketClosedAt = Date.now();
|
|
66
|
+
this.logger.info(`websocket disconnected: socket ${this.socketId()}; outgoing messages are buffered until it returns`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* A second channel means the reconnect was refused and the session discarded.
|
|
71
|
+
* Only reachable with `reloadOnReconnect` off — with it on, Theia reloads the
|
|
72
|
+
* page instead of opening another channel.
|
|
73
|
+
*/
|
|
74
|
+
protected handleChannelOpen(): void {
|
|
75
|
+
this.channelCount++;
|
|
76
|
+
if (this.channelCount === 1) {
|
|
77
|
+
this.logger.info(`initial channel opened on socket ${this.socketId()}`);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.logger.warn(
|
|
81
|
+
`new channel #${this.channelCount} on socket ${this.socketId()}: the backend did not recognise this frontend, ` +
|
|
82
|
+
'so its session was discarded and every pending request rejected'
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected handleStatusChange(status: ConnectionStatus): void {
|
|
87
|
+
if (status === ConnectionStatus.ONLINE) {
|
|
88
|
+
const offlineFor = this.offlineAt === undefined ? '' : ` after ${Date.now() - this.offlineAt} ms`;
|
|
89
|
+
this.offlineAt = undefined;
|
|
90
|
+
this.logger.info(`backend reachable again${offlineFor}`);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.offlineAt = Date.now();
|
|
94
|
+
const socket = this.connectionSource.socket;
|
|
95
|
+
if (socket?.connected) {
|
|
96
|
+
this.logger.warn(
|
|
97
|
+
`backend stopped answering while socket ${socket.id} is still connected: nothing this session sends will ` +
|
|
98
|
+
'arrive, and socket.io will not reconnect on its own. Reload the page to recover.'
|
|
99
|
+
);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.logger.info(`backend unreachable; websocket is disconnected (socket ${this.socketId()})`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The outage outlasted what the buffer can hold, so changes made from here on
|
|
107
|
+
* are being thrown away rather than queued. Reconnecting resumes sending but
|
|
108
|
+
* cannot bring those back, which leaves the editor disagreeing with the
|
|
109
|
+
* server — and without a message the user would see nothing but an editor
|
|
110
|
+
* that has quietly stopped saving, so say it plainly.
|
|
111
|
+
*/
|
|
112
|
+
protected handleBufferOverflow(overflow: ConnectionBufferOverflow): void {
|
|
113
|
+
this.logger.error(`buffer full at ${overflow.maxBytes} bytes holding ${overflow.messages} message(s)`);
|
|
114
|
+
this.messageService.error(
|
|
115
|
+
nls.localize(
|
|
116
|
+
'hydranium/connection/offlineLimitReached',
|
|
117
|
+
'Disconnected for too long: recent changes were not sent. Reload the page before continuing.'
|
|
118
|
+
)
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
protected socketId(): string {
|
|
123
|
+
return this.connectionSource.socket?.id ?? WebSocketConnectionSource.NO_CONNECTION;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
protected statusName(status: ConnectionStatus): string {
|
|
127
|
+
return status === ConnectionStatus.ONLINE ? 'online' : 'offline';
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Bind {@link ConnectionDiagnosticsContribution} as a `FrontendApplicationContribution`.
|
|
133
|
+
*
|
|
134
|
+
* Needs a {@link ChannelLogger} binding, so call it from a module that has also
|
|
135
|
+
* called `bindChannelLogger`. Unlike `bindConnectionResilience` this belongs in a
|
|
136
|
+
* normal frontend module: it only observes, so it has no reason to run during
|
|
137
|
+
* preload, and `MessageService` is not available that early.
|
|
138
|
+
*/
|
|
139
|
+
export function bindConnectionDiagnostics(bind: interfaces.Bind): void {
|
|
140
|
+
bind(ConnectionDiagnosticsContribution).toSelf().inSingletonScope();
|
|
141
|
+
bind(FrontendApplicationContribution).toService(ConnectionDiagnosticsContribution);
|
|
142
|
+
}
|
package/src/browser/index.ts
CHANGED
|
@@ -12,3 +12,5 @@ export * from './channel-logger';
|
|
|
12
12
|
export * from './log-level-preference';
|
|
13
13
|
export * from './browser-capture';
|
|
14
14
|
export * from './memory-diagnostics-contribution';
|
|
15
|
+
export * from './session-aware-connection-source';
|
|
16
|
+
export * from './connection-diagnostics-contribution';
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
import { WebSocketConnectionSource } from '@theia/core/lib/browser/messaging/ws-connection-source';
|
|
11
|
+
import { Disposable, DisposableCollection } from '@theia/core/lib/common/disposable';
|
|
12
|
+
import { type Event } from '@theia/core/lib/common/event';
|
|
13
|
+
import { type AbstractChannel, ForwardingChannel } from '@theia/core/lib/common/message-rpc/channel';
|
|
14
|
+
import { Uint8ArrayReadBuffer, Uint8ArrayWriteBuffer } from '@theia/core/lib/common/message-rpc/uint8-array-message-buffer';
|
|
15
|
+
import { ConnectionManagementMessages } from '@theia/core/lib/common/messaging/connection-management';
|
|
16
|
+
import { injectable, type interfaces } from '@theia/core/shared/inversify';
|
|
17
|
+
import { SocketWriteBuffer } from '@theia/core/lib/common/messaging/socket-write-buffer';
|
|
18
|
+
import { type ConnectionResilienceOptions } from '../common/connection-resilience-options';
|
|
19
|
+
import {
|
|
20
|
+
type ConnectionBufferOverflow,
|
|
21
|
+
createFramedSocketWriteBuffer,
|
|
22
|
+
FramedSocketWriteBuffer,
|
|
23
|
+
supportsConnectionResilience,
|
|
24
|
+
warnConnectionResilienceUnavailable
|
|
25
|
+
} from '../common/framed-socket-write-buffer';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Sends a message only once the server has confirmed the session, and only
|
|
29
|
+
* behind anything already queued.
|
|
30
|
+
*
|
|
31
|
+
* Theia sends straight away when `socket.connected` is true and buffers
|
|
32
|
+
* otherwise. The gap is that reconnecting sets that flag immediately, while the
|
|
33
|
+
* session is only confirmed a round trip later. A message produced in that
|
|
34
|
+
* window either jumps ahead of older messages still waiting in the buffer, or,
|
|
35
|
+
* if nothing was waiting, goes out on a socket whose channel the server has not
|
|
36
|
+
* attached yet and is discarded by a peer with no listener for it.
|
|
37
|
+
*
|
|
38
|
+
* Losing one or reordering them is equally unrecoverable for anything that
|
|
39
|
+
* applies messages by position. Theia's plugin host keeps a line-indexed copy of
|
|
40
|
+
* every open document, so a single late or missing edit leaves that copy wrong
|
|
41
|
+
* for good, and the next edit past its end fails.
|
|
42
|
+
*
|
|
43
|
+
* The server side needs no equivalent: it adopts its socket and flushes in one
|
|
44
|
+
* synchronous step, so it has no window of this kind.
|
|
45
|
+
*/
|
|
46
|
+
@injectable()
|
|
47
|
+
export class SessionAwareConnectionSource extends WebSocketConnectionSource {
|
|
48
|
+
/**
|
|
49
|
+
* Whether the server has confirmed, on the socket in use right now, that it
|
|
50
|
+
* still holds this frontend's session. A connected socket is not enough:
|
|
51
|
+
* until the handshake is answered the server has not attached its channel to
|
|
52
|
+
* this socket, and a message sent meanwhile reaches a peer with no listener
|
|
53
|
+
* for it and is discarded without a trace.
|
|
54
|
+
*/
|
|
55
|
+
protected sessionResumed = false;
|
|
56
|
+
protected sessionListenersAttached = false;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Every connect starts a socket the server has not confirmed yet, including
|
|
60
|
+
* the reconnects socket.io performs on its own. Resetting here rather than on
|
|
61
|
+
* disconnect also covers a connect that follows no clean disconnect event.
|
|
62
|
+
*/
|
|
63
|
+
protected override handleSocketConnected(): void {
|
|
64
|
+
this.trackSessionState();
|
|
65
|
+
this.sessionResumed = false;
|
|
66
|
+
super.handleSocketConnected();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Watches the connection handshake for its outcome.
|
|
71
|
+
*
|
|
72
|
+
* Attached once, to the socket socket.io reuses across reconnects, and before
|
|
73
|
+
* the base class adds its own per-negotiation listeners — so this has already
|
|
74
|
+
* recorded the outcome by the time the base class reacts to it by flushing.
|
|
75
|
+
*/
|
|
76
|
+
protected trackSessionState(): void {
|
|
77
|
+
if (this.sessionListenersAttached) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.sessionListenersAttached = true;
|
|
81
|
+
this.socket.on(ConnectionManagementMessages.INITIAL_CONNECT, () => {
|
|
82
|
+
this.sessionResumed = true;
|
|
83
|
+
});
|
|
84
|
+
this.socket.on(ConnectionManagementMessages.RECONNECT, (hasConnection: boolean) => {
|
|
85
|
+
this.sessionResumed = hasConnection;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Whether a message may go out now, as opposed to waiting in the buffer. */
|
|
90
|
+
protected get canSend(): boolean {
|
|
91
|
+
return this.socket.connected && this.sessionResumed;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
protected override createChannel(): AbstractChannel {
|
|
95
|
+
const toDispose = new DisposableCollection();
|
|
96
|
+
const messageHandler = (data: ArrayBuffer | Uint8Array): void => {
|
|
97
|
+
this.onIncomingMessageActivityEmitter.fire();
|
|
98
|
+
if (this.currentChannel) {
|
|
99
|
+
// socket.io hands binary over as ArrayBuffer in the browser.
|
|
100
|
+
const buffer = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
|
|
101
|
+
this.currentChannel.onMessageEmitter.fire(() => new Uint8ArrayReadBuffer(buffer));
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
this.socket.on('message', messageHandler);
|
|
105
|
+
toDispose.push(Disposable.create(() => this.socket.off('message', messageHandler)));
|
|
106
|
+
|
|
107
|
+
return new ForwardingChannel(
|
|
108
|
+
'any',
|
|
109
|
+
() => toDispose.dispose(),
|
|
110
|
+
() => {
|
|
111
|
+
const result = new Uint8ArrayWriteBuffer();
|
|
112
|
+
// Says only whether the session can carry a message; the buffer decides whether it may
|
|
113
|
+
// go ahead of anything already waiting.
|
|
114
|
+
result.onCommit(buffer => this.framedBuffer.sendOrQueue(this.canSend ? this.socket : undefined, buffer));
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Fires when the buffer runs out of room, so an adopter can say so rather than just stopping. */
|
|
121
|
+
get onBufferOverflow(): Event<ConnectionBufferOverflow> {
|
|
122
|
+
return this.framedBuffer.onOverflow;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The write buffer Theia injected into the base class.
|
|
127
|
+
*
|
|
128
|
+
* Reached through a structural view rather than as `this.writeBuffer`,
|
|
129
|
+
* because that member is `private` in Theia 1.70 and only became `protected`
|
|
130
|
+
* in 1.71 — and this package compiles against the whole range. The view
|
|
131
|
+
* asserts nothing the versions disagree about: the field is there in both,
|
|
132
|
+
* holding whatever `SocketWriteBuffer` is bound, and the check below is what
|
|
133
|
+
* establishes it is ours.
|
|
134
|
+
*/
|
|
135
|
+
protected get framedBuffer(): FramedSocketWriteBuffer {
|
|
136
|
+
const buffer = (this as unknown as { readonly writeBuffer: SocketWriteBuffer }).writeBuffer;
|
|
137
|
+
if (!(buffer instanceof FramedSocketWriteBuffer)) {
|
|
138
|
+
throw new Error('SessionAwareConnectionSource requires FramedSocketWriteBuffer to be bound');
|
|
139
|
+
}
|
|
140
|
+
return buffer;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Replaces the frontend pieces that decide how outgoing messages survive a
|
|
146
|
+
* reconnect. Every one of them rebinds something Theia's
|
|
147
|
+
* `messagingFrontendModule` already bound, so this belongs in a module loaded
|
|
148
|
+
* into the same container.
|
|
149
|
+
*
|
|
150
|
+
* It must be a `frontendPreload` module rather than a normal frontend module:
|
|
151
|
+
* Theia's preloader resolves `WebSocketConnectionSource` (and with it the write
|
|
152
|
+
* buffer) while loading i18n and OS settings, which happens before any frontend
|
|
153
|
+
* module is loaded. A rebind there would come too late — the instances would
|
|
154
|
+
* already exist. All `frontendPreload` modules, by contrast, are loaded before
|
|
155
|
+
* the preloader constructs anything.
|
|
156
|
+
*
|
|
157
|
+
* Does nothing but warn on a Theia too old to expose the buffer binding, so the
|
|
158
|
+
* package keeps one supported range rather than splitting its floor for this
|
|
159
|
+
* feature. Returns whether the hardening was installed, for an adopter that
|
|
160
|
+
* wants to branch on it.
|
|
161
|
+
*/
|
|
162
|
+
export function bindConnectionResilience(
|
|
163
|
+
isBound: interfaces.IsBound,
|
|
164
|
+
rebind: interfaces.Rebind,
|
|
165
|
+
options: ConnectionResilienceOptions = {}
|
|
166
|
+
): boolean {
|
|
167
|
+
if (!supportsConnectionResilience(isBound)) {
|
|
168
|
+
warnConnectionResilienceUnavailable('frontend');
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
// Scopes are kept as Theia declares them: one buffer per connection source, one shared socket owner.
|
|
172
|
+
rebind(SocketWriteBuffer).toDynamicValue(() => createFramedSocketWriteBuffer(options.bufferBytes));
|
|
173
|
+
rebind(WebSocketConnectionSource).to(SessionAwareConnectionSource).inSingletonScope();
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Options for the `bindConnectionResilience` helper each tier exports.
|
|
12
|
+
*
|
|
13
|
+
* Lives in `common` rather than beside either helper so the server entry can
|
|
14
|
+
* name the type without importing the browser module, and the other way round.
|
|
15
|
+
*/
|
|
16
|
+
export interface ConnectionResilienceOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Size of the buffer holding messages while the socket is down, in bytes.
|
|
19
|
+
* Defaults to Theia's own limit. It decides how long an outage can last
|
|
20
|
+
* before changes start being rejected, so a deployment on a poor network
|
|
21
|
+
* trades memory for tolerance here.
|
|
22
|
+
*/
|
|
23
|
+
readonly bufferBytes?: number;
|
|
24
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
import { Emitter, type Event } from '@theia/core/lib/common/event';
|
|
11
|
+
import { SocketWriteBuffer } from '@theia/core/lib/common/messaging/socket-write-buffer';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Prefix on every connection log line, deliberately shared by the browser and the
|
|
15
|
+
* server so one pattern greps a console and a pod log at once.
|
|
16
|
+
*/
|
|
17
|
+
export const CONNECTION_LOG_PREFIX = '[connection]';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The part of a socket this buffer uses. Narrower than the socket type Theia
|
|
21
|
+
* declares, which keeps the dependency honest and lets the class be tested
|
|
22
|
+
* without a connection.
|
|
23
|
+
*/
|
|
24
|
+
export interface MessageSink {
|
|
25
|
+
send(data: Uint8Array): void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Reported when the buffer runs out of room. The message that did not fit is
|
|
30
|
+
* rejected, and so is every further one for as long as the buffer stays full.
|
|
31
|
+
* Reconnecting drains it and sending resumes, but whatever was rejected
|
|
32
|
+
* meanwhile is gone, so the two sides no longer agree about the session.
|
|
33
|
+
*/
|
|
34
|
+
export interface ConnectionBufferOverflow {
|
|
35
|
+
/** Messages held when the limit was reached. */
|
|
36
|
+
readonly messages: number;
|
|
37
|
+
/** Bytes held when the limit was reached. */
|
|
38
|
+
readonly bytes: number;
|
|
39
|
+
/** The limit in force, so a reader can tell what to raise it to. */
|
|
40
|
+
readonly maxBytes: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Holds messages while the peer is away and re-sends them when it returns.
|
|
45
|
+
*
|
|
46
|
+
* Theia copies them into one growing byte array and flushes that with a single
|
|
47
|
+
* `send`. A websocket delivers whole messages rather than a byte stream, so
|
|
48
|
+
* Theia's encoding adds no length prefix and every reader assumes one delivery
|
|
49
|
+
* is one message. The concatenated flush breaks that assumption: the reader
|
|
50
|
+
* decodes the first message and throws away the bytes behind it.
|
|
51
|
+
*
|
|
52
|
+
* Changed here: one entry per message, sent one at a time, so each arrives as
|
|
53
|
+
* its own delivery. The buffer also decides whether a message may go out
|
|
54
|
+
* directly — see {@link sendOrQueue} — because a caller that decides for itself
|
|
55
|
+
* is how messages end up overtaking a backlog.
|
|
56
|
+
*
|
|
57
|
+
* Unchanged from Theia: exceeding the limit rejects the message rather than
|
|
58
|
+
* dropping something silently. The limit is settable, since the right value
|
|
59
|
+
* depends on how long an outage has to survive.
|
|
60
|
+
*
|
|
61
|
+
* Logging goes to `console` rather than through a `Logger`. On the browser side
|
|
62
|
+
* Theia's preloader builds this before any Output channel exists, so there is
|
|
63
|
+
* nothing else to write to; the {@link CONNECTION_LOG_PREFIX} keeps the lines
|
|
64
|
+
* findable on both sides.
|
|
65
|
+
*/
|
|
66
|
+
export class FramedSocketWriteBuffer extends SocketWriteBuffer {
|
|
67
|
+
/** Theia's fixed limit, kept as the default. */
|
|
68
|
+
static readonly DEFAULT_MAX_BYTES = 100 * 1024;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Settable so a deployment can trade memory for longer outages.
|
|
72
|
+
*
|
|
73
|
+
* Consulted directly rather than through the base class's `maxBufferSize`,
|
|
74
|
+
* which does not exist before Theia 1.71 — an `override` of it would not
|
|
75
|
+
* compile against the oldest release this package supports. Nothing is lost:
|
|
76
|
+
* `buffer`, `flush` and `drain` are all replaced here, so the base's own
|
|
77
|
+
* limit is never reached.
|
|
78
|
+
*/
|
|
79
|
+
maxBytes = FramedSocketWriteBuffer.DEFAULT_MAX_BYTES;
|
|
80
|
+
|
|
81
|
+
protected pending: Uint8Array[] = [];
|
|
82
|
+
protected pendingBytes = 0;
|
|
83
|
+
protected overflowReported = false;
|
|
84
|
+
protected readonly onOverflowEmitter = new Emitter<ConnectionBufferOverflow>();
|
|
85
|
+
|
|
86
|
+
/** Fires once per outage when the limit is reached. */
|
|
87
|
+
get onOverflow(): Event<ConnectionBufferOverflow> {
|
|
88
|
+
return this.onOverflowEmitter.event;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Sends `data` now only if nothing is waiting, and queues it otherwise.
|
|
93
|
+
*
|
|
94
|
+
* Callers say whether the peer is ready for a message; the buffer decides
|
|
95
|
+
* whether it may go ahead of one already waiting. Splitting it this way is
|
|
96
|
+
* the point: a caller that answers both questions from one flag is how a
|
|
97
|
+
* fresh message ends up overtaking a backlog.
|
|
98
|
+
*/
|
|
99
|
+
sendOrQueue(socket: MessageSink | undefined, data: Uint8Array): void {
|
|
100
|
+
if (socket && !this.hasBacklog) {
|
|
101
|
+
socket.send(data);
|
|
102
|
+
} else {
|
|
103
|
+
this.buffer(data);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
override buffer(data: Uint8Array): void {
|
|
108
|
+
if (this.pendingBytes + data.byteLength > this.maxBytes) {
|
|
109
|
+
this.reportOverflow();
|
|
110
|
+
throw new Error(`Max disconnected buffer size exceeded by adding ${data.byteLength} bytes`);
|
|
111
|
+
}
|
|
112
|
+
// Copied for the same reason the base class copies: the caller owns `data`.
|
|
113
|
+
this.pending.push(data.slice());
|
|
114
|
+
this.pendingBytes += data.byteLength;
|
|
115
|
+
if (this.pending.length === 1) {
|
|
116
|
+
console.info(`${CONNECTION_LOG_PREFIX} buffering messages, the peer is disconnected`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
override flush(socket: MessageSink): void {
|
|
121
|
+
if (this.pending.length === 0) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const count = this.pending.length;
|
|
125
|
+
const bytes = this.pendingBytes;
|
|
126
|
+
// Drained one by one rather than snapshot-then-send, so `hasBacklog` stays true until the last
|
|
127
|
+
// message is out and anything produced meanwhile still queues behind it. Each message is
|
|
128
|
+
// removed only once it has been sent, so a throwing send leaves the backlog intact and in
|
|
129
|
+
// order rather than swallowing the message it failed on.
|
|
130
|
+
while (this.pending.length > 0) {
|
|
131
|
+
const message = this.pending[0];
|
|
132
|
+
socket.send(message);
|
|
133
|
+
this.pending.shift();
|
|
134
|
+
this.pendingBytes -= message.byteLength;
|
|
135
|
+
}
|
|
136
|
+
this.overflowReported = false;
|
|
137
|
+
console.info(`${CONNECTION_LOG_PREFIX} sent ${count} buffered message(s), ${bytes} bytes`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
override drain(): void {
|
|
141
|
+
if (this.pending.length > 0) {
|
|
142
|
+
console.warn(`${CONNECTION_LOG_PREFIX} discarded ${this.pending.length} buffered message(s), ${this.pendingBytes} bytes`);
|
|
143
|
+
}
|
|
144
|
+
this.reset();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Whether messages are still waiting to go out. */
|
|
148
|
+
get hasBacklog(): boolean {
|
|
149
|
+
return this.pending.length > 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
protected reportOverflow(): void {
|
|
153
|
+
if (this.overflowReported) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
this.overflowReported = true;
|
|
157
|
+
const overflow: ConnectionBufferOverflow = { messages: this.pending.length, bytes: this.pendingBytes, maxBytes: this.maxBytes };
|
|
158
|
+
console.error(
|
|
159
|
+
`${CONNECTION_LOG_PREFIX} buffer full at ${overflow.maxBytes} bytes after ${overflow.messages} message(s); ` +
|
|
160
|
+
'this message is rejected, and so is every further one until the peer returns'
|
|
161
|
+
);
|
|
162
|
+
this.onOverflowEmitter.fire(overflow);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
protected reset(): void {
|
|
166
|
+
this.pending = [];
|
|
167
|
+
this.pendingBytes = 0;
|
|
168
|
+
this.overflowReported = false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Named in the warning a skipped install emits, so the reader is told what to upgrade to. */
|
|
173
|
+
export const REQUIRED_THEIA_HINT = '@theia/core 1.71 or newer';
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Whether this Theia exposes the seam the reconnect hardening replaces.
|
|
177
|
+
*
|
|
178
|
+
* Theia only began binding {@link SocketWriteBuffer} in 1.71; before that each
|
|
179
|
+
* connection built one privately, so there is no binding to rebind and no way
|
|
180
|
+
* in. Every other API involved has been there throughout, which makes this one
|
|
181
|
+
* probe the whole compatibility question — and it is a binding check rather
|
|
182
|
+
* than a version parse, so it answers about the container actually in front of
|
|
183
|
+
* us instead of a number that may have been overridden or vendored.
|
|
184
|
+
*
|
|
185
|
+
* Callers that get `false` must leave Theia's own wiring alone: rebinding half
|
|
186
|
+
* of it would be worse than not rebinding at all.
|
|
187
|
+
*/
|
|
188
|
+
export function supportsConnectionResilience(isBound: (identifier: typeof SocketWriteBuffer) => boolean): boolean {
|
|
189
|
+
return isBound(SocketWriteBuffer);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The warning a skipped install emits. Says what is lost rather than only what
|
|
194
|
+
* is missing: the failures this guards against are silent, so an adopter who
|
|
195
|
+
* never sees this line has no other way to learn the guard is absent.
|
|
196
|
+
*/
|
|
197
|
+
export function warnConnectionResilienceUnavailable(tier: 'frontend' | 'backend'): void {
|
|
198
|
+
console.warn(
|
|
199
|
+
`${CONNECTION_LOG_PREFIX} ${tier} reconnect hardening NOT installed: this Theia does not expose an injectable ` +
|
|
200
|
+
`socket write buffer (needs ${REQUIRED_THEIA_HINT}). Theia's own behaviour is left in place, which can lose or ` +
|
|
201
|
+
'reorder messages when a socket drops and reconnects.'
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Builds a buffer with the given limit, or Theia's default when none is configured. */
|
|
206
|
+
export function createFramedSocketWriteBuffer(maxBytes?: number): FramedSocketWriteBuffer {
|
|
207
|
+
const buffer = new FramedSocketWriteBuffer();
|
|
208
|
+
if (typeof maxBytes === 'number' && maxBytes > 0) {
|
|
209
|
+
buffer.maxBytes = maxBytes;
|
|
210
|
+
}
|
|
211
|
+
return buffer;
|
|
212
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
// Environment-neutral connection primitives, shared by the browser connection
|
|
11
|
+
// source and the server connection service. Kept out of both tier barrels
|
|
12
|
+
// because each side binds the same buffer for its own half of the socket.
|
|
13
|
+
export * from './framed-socket-write-buffer';
|
|
14
|
+
export * from './connection-resilience-options';
|
package/src/node/index.ts
CHANGED