@hydranium/client-theia 1.0.0-next.39 → 1.0.0-next.390

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 (95) hide show
  1. package/README.md +53 -81
  2. package/lib/browser/browser-capture.js.map +1 -1
  3. package/lib/browser/channel-logger.d.ts +4 -3
  4. package/lib/browser/channel-logger.d.ts.map +1 -1
  5. package/lib/browser/channel-logger.js +11 -19
  6. package/lib/browser/channel-logger.js.map +1 -1
  7. package/lib/browser/connection-diagnostics-contribution.d.ts +73 -0
  8. package/lib/browser/connection-diagnostics-contribution.d.ts.map +1 -0
  9. package/lib/browser/connection-diagnostics-contribution.js +162 -0
  10. package/lib/browser/connection-diagnostics-contribution.js.map +1 -0
  11. package/lib/browser/connection-reporter.d.ts +57 -0
  12. package/lib/browser/connection-reporter.d.ts.map +1 -0
  13. package/lib/browser/connection-reporter.js +104 -0
  14. package/lib/browser/connection-reporter.js.map +1 -0
  15. package/lib/browser/editor-disk-sync.d.ts +142 -0
  16. package/lib/browser/editor-disk-sync.d.ts.map +1 -0
  17. package/lib/browser/editor-disk-sync.js +250 -0
  18. package/lib/browser/editor-disk-sync.js.map +1 -0
  19. package/lib/browser/hydranium-file-service.d.ts +44 -0
  20. package/lib/browser/hydranium-file-service.d.ts.map +1 -0
  21. package/lib/browser/hydranium-file-service.js +61 -0
  22. package/lib/browser/hydranium-file-service.js.map +1 -0
  23. package/lib/browser/index.d.ts +5 -0
  24. package/lib/browser/index.d.ts.map +1 -1
  25. package/lib/browser/index.js +5 -0
  26. package/lib/browser/index.js.map +1 -1
  27. package/lib/browser/log-level-preference.d.ts.map +1 -1
  28. package/lib/browser/log-level-preference.js +11 -0
  29. package/lib/browser/log-level-preference.js.map +1 -1
  30. package/lib/browser/memory-diagnostics-contribution.d.ts +1 -1
  31. package/lib/browser/memory-diagnostics-contribution.d.ts.map +1 -1
  32. package/lib/browser/memory-diagnostics-contribution.js +1 -1
  33. package/lib/browser/memory-diagnostics-contribution.js.map +1 -1
  34. package/lib/browser/session-aware-connection-source.d.ts +104 -0
  35. package/lib/browser/session-aware-connection-source.d.ts.map +1 -0
  36. package/lib/browser/session-aware-connection-source.js +193 -0
  37. package/lib/browser/session-aware-connection-source.js.map +1 -0
  38. package/lib/common/clock.d.ts +19 -0
  39. package/lib/common/clock.d.ts.map +1 -0
  40. package/lib/common/clock.js +13 -0
  41. package/lib/common/clock.js.map +1 -0
  42. package/lib/common/connection-resilience-options.d.ts +30 -0
  43. package/lib/common/connection-resilience-options.d.ts.map +1 -0
  44. package/lib/common/connection-resilience-options.js +11 -0
  45. package/lib/common/connection-resilience-options.js.map +1 -0
  46. package/lib/common/framed-socket-write-buffer.d.ts +173 -0
  47. package/lib/common/framed-socket-write-buffer.d.ts.map +1 -0
  48. package/lib/common/framed-socket-write-buffer.js +293 -0
  49. package/lib/common/framed-socket-write-buffer.js.map +1 -0
  50. package/lib/common/inbound-message-sequence.d.ts +56 -0
  51. package/lib/common/inbound-message-sequence.d.ts.map +1 -0
  52. package/lib/common/inbound-message-sequence.js +98 -0
  53. package/lib/common/inbound-message-sequence.js.map +1 -0
  54. package/lib/common/index.d.ts +13 -0
  55. package/lib/common/index.d.ts.map +1 -0
  56. package/lib/common/index.js +32 -0
  57. package/lib/common/index.js.map +1 -0
  58. package/lib/node/abstract-socket-forwarding-connection-handler.d.ts +16 -12
  59. package/lib/node/abstract-socket-forwarding-connection-handler.d.ts.map +1 -1
  60. package/lib/node/abstract-socket-forwarding-connection-handler.js +94 -31
  61. package/lib/node/abstract-socket-forwarding-connection-handler.js.map +1 -1
  62. package/lib/node/index.d.ts +2 -0
  63. package/lib/node/index.d.ts.map +1 -1
  64. package/lib/node/index.js +2 -0
  65. package/lib/node/index.js.map +1 -1
  66. package/lib/node/session-bound-frontend-connection-service.d.ts +122 -0
  67. package/lib/node/session-bound-frontend-connection-service.d.ts.map +1 -0
  68. package/lib/node/session-bound-frontend-connection-service.js +236 -0
  69. package/lib/node/session-bound-frontend-connection-service.js.map +1 -0
  70. package/lib/node/socket-channel-forwarder.d.ts +54 -0
  71. package/lib/node/socket-channel-forwarder.d.ts.map +1 -0
  72. package/lib/node/socket-channel-forwarder.js +95 -0
  73. package/lib/node/socket-channel-forwarder.js.map +1 -0
  74. package/lib/testing/stub-inversify-context.js.map +1 -1
  75. package/lib/testing/stub-output-channel.js.map +1 -1
  76. package/package.json +33 -34
  77. package/src/browser/browser-capture.ts +1 -1
  78. package/src/browser/channel-logger.ts +11 -19
  79. package/src/browser/connection-diagnostics-contribution.ts +146 -0
  80. package/src/browser/connection-reporter.ts +121 -0
  81. package/src/browser/editor-disk-sync.ts +253 -0
  82. package/src/browser/hydranium-file-service.ts +56 -0
  83. package/src/browser/index.ts +5 -0
  84. package/src/browser/log-level-preference.ts +11 -0
  85. package/src/browser/memory-diagnostics-contribution.ts +4 -3
  86. package/src/browser/session-aware-connection-source.ts +205 -0
  87. package/src/common/clock.ts +20 -0
  88. package/src/common/connection-resilience-options.ts +30 -0
  89. package/src/common/framed-socket-write-buffer.ts +345 -0
  90. package/src/common/inbound-message-sequence.ts +102 -0
  91. package/src/common/index.ts +16 -0
  92. package/src/node/abstract-socket-forwarding-connection-handler.ts +85 -36
  93. package/src/node/index.ts +2 -0
  94. package/src/node/session-bound-frontend-connection-service.ts +264 -0
  95. package/src/node/socket-channel-forwarder.ts +101 -0
@@ -0,0 +1,205 @@
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 { environment } from '@theia/core/shared/@theia/application-package/lib/environment';
17
+ import { injectable, type interfaces } from '@theia/core/shared/inversify';
18
+ import { SocketWriteBuffer } from '@theia/core/lib/common/messaging/socket-write-buffer';
19
+ import { type ConnectionResilienceOptions } from '../common/connection-resilience-options';
20
+ import {
21
+ type ConnectionBufferOverflow,
22
+ createFramedSocketWriteBuffer,
23
+ FramedSocketWriteBuffer,
24
+ supportsConnectionResilience,
25
+ warnConnectionResilienceUnavailable
26
+ } from '../common/framed-socket-write-buffer';
27
+ import { ACKNOWLEDGEMENT_EVENT, InboundMessageSequence } from '../common/inbound-message-sequence';
28
+
29
+ /**
30
+ * Sends a message only once the server has confirmed the session, and only
31
+ * behind anything already queued.
32
+ *
33
+ * Theia sends straight away when `socket.connected` is true and buffers
34
+ * otherwise. The gap is that reconnecting sets that flag immediately, while the
35
+ * session is only confirmed a round trip later. A message produced in that
36
+ * window either jumps ahead of older messages still waiting in the buffer, or,
37
+ * if nothing was waiting, goes out on a socket whose channel the server has not
38
+ * attached yet and is discarded by a peer with no listener for it.
39
+ *
40
+ * Losing one or reordering them is equally unrecoverable for anything that
41
+ * applies messages by position. Theia's plugin host keeps a line-indexed copy of
42
+ * every open document, so a single late or missing edit leaves that copy wrong
43
+ * for good, and the next edit past its end fails.
44
+ *
45
+ * The server side needs no equivalent: it adopts its socket and flushes in one
46
+ * synchronous step, so it has no window of this kind.
47
+ */
48
+ @injectable()
49
+ export class SessionAwareConnectionSource extends WebSocketConnectionSource {
50
+ /**
51
+ * Whether the server has confirmed, on the socket in use right now, that it
52
+ * still holds this frontend's session. A connected socket is not enough:
53
+ * until the handshake is answered the server has not attached its channel to
54
+ * this socket, and a message sent meanwhile reaches a peer with no listener
55
+ * for it and is discarded without a trace.
56
+ */
57
+ protected sessionResumed = false;
58
+ protected sessionListenersAttached = false;
59
+
60
+ /**
61
+ * Every connect starts a socket the server has not confirmed yet, including
62
+ * the reconnects socket.io performs on its own. Resetting here rather than on
63
+ * disconnect also covers a connect that follows no clean disconnect event.
64
+ */
65
+ protected override handleSocketConnected(): void {
66
+ this.trackSessionState();
67
+ this.sessionResumed = false;
68
+ super.handleSocketConnected();
69
+ }
70
+
71
+ /**
72
+ * Watches the connection handshake for its outcome.
73
+ *
74
+ * Attached once, to the socket socket.io reuses across reconnects, and before
75
+ * the base class adds its own per-negotiation listeners — so this has already
76
+ * recorded the outcome by the time the base class reacts to it by flushing.
77
+ */
78
+ protected trackSessionState(): void {
79
+ if (this.sessionListenersAttached) {
80
+ return;
81
+ }
82
+ this.sessionListenersAttached = true;
83
+ this.socket.on(ConnectionManagementMessages.INITIAL_CONNECT, () => {
84
+ this.sessionResumed = true;
85
+ });
86
+ this.socket.on(ConnectionManagementMessages.RECONNECT, (hasConnection: boolean) => {
87
+ this.sessionResumed = hasConnection;
88
+ });
89
+ this.socket.on(ACKNOWLEDGEMENT_EVENT, (sequence: unknown) => {
90
+ if (typeof sequence === 'number') {
91
+ this.framedBuffer.acknowledge(sequence);
92
+ }
93
+ });
94
+ }
95
+
96
+ /** Whether a message may go out now, as opposed to waiting in the buffer. */
97
+ protected get canSend(): boolean {
98
+ return this.socket.connected && this.sessionResumed;
99
+ }
100
+
101
+ /**
102
+ * Copied from Theia apart from the duplicate check on receipt and the `onCommit` body, because
103
+ * the base builds both callbacks inline and exposes no narrower seam. Re-diff when the supported
104
+ * range moves.
105
+ *
106
+ * Called once per session, so the receive-side state starts afresh with the server's numbering.
107
+ */
108
+ protected override createChannel(): AbstractChannel {
109
+ const toDispose = new DisposableCollection();
110
+ const inbound = new InboundMessageSequence(sequence => this.socket.emit(ACKNOWLEDGEMENT_EVENT, sequence));
111
+ toDispose.push(Disposable.create(() => inbound.dispose()));
112
+ const messageHandler = (data: ArrayBuffer | Uint8Array, sequence?: unknown): void => {
113
+ this.onIncomingMessageActivityEmitter.fire();
114
+ if (typeof sequence === 'number') {
115
+ this.framedBuffer.markPeerDeduplicates();
116
+ }
117
+ if (!inbound.accept(sequence, data.byteLength)) {
118
+ return;
119
+ }
120
+ if (this.currentChannel) {
121
+ // socket.io hands binary over as ArrayBuffer in the browser.
122
+ const buffer = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
123
+ this.currentChannel.onMessageEmitter.fire(() => new Uint8ArrayReadBuffer(buffer));
124
+ }
125
+ };
126
+ this.socket.on('message', messageHandler);
127
+ toDispose.push(Disposable.create(() => this.socket.off('message', messageHandler)));
128
+
129
+ return new ForwardingChannel(
130
+ 'any',
131
+ () => toDispose.dispose(),
132
+ () => {
133
+ const result = new Uint8ArrayWriteBuffer();
134
+ // Says only whether the session can carry a message; the buffer decides whether it may
135
+ // go ahead of anything already waiting.
136
+ result.onCommit(buffer => this.framedBuffer.sendOrQueue(this.canSend ? this.socket : undefined, buffer));
137
+ return result;
138
+ }
139
+ );
140
+ }
141
+
142
+ /** Fires when the buffer runs out of room, so an adopter can say so rather than just stopping. */
143
+ get onBufferOverflow(): Event<ConnectionBufferOverflow> {
144
+ return this.framedBuffer.onOverflow;
145
+ }
146
+
147
+ /**
148
+ * The write buffer Theia injected into the base class.
149
+ *
150
+ * Reached through a structural view rather than as `this.writeBuffer`,
151
+ * because that member is `private` in Theia 1.70 and only became `protected`
152
+ * in 1.71 — and this package compiles against the whole range. The view
153
+ * asserts nothing the versions disagree about: the field is there in both,
154
+ * holding whatever `SocketWriteBuffer` is bound, and the check below is what
155
+ * establishes it is ours.
156
+ */
157
+ protected get framedBuffer(): FramedSocketWriteBuffer {
158
+ const buffer = (this as unknown as { readonly writeBuffer: SocketWriteBuffer }).writeBuffer;
159
+ if (!(buffer instanceof FramedSocketWriteBuffer)) {
160
+ throw new Error('SessionAwareConnectionSource requires FramedSocketWriteBuffer to be bound');
161
+ }
162
+ return buffer;
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Replaces the frontend pieces that decide how outgoing messages survive a
168
+ * reconnect. Every one of them rebinds something Theia's
169
+ * `messagingFrontendModule` already bound, so this belongs in a module loaded
170
+ * into the same container.
171
+ *
172
+ * It must be a `frontendPreload` module rather than a normal frontend module:
173
+ * Theia's preloader resolves `WebSocketConnectionSource` (and with it the write
174
+ * buffer) while loading i18n and OS settings, which happens before any frontend
175
+ * module is loaded. A rebind there would come too late — the instances would
176
+ * already exist. All `frontendPreload` modules, by contrast, are loaded before
177
+ * the preloader constructs anything.
178
+ *
179
+ * Does nothing but warn on a Theia too old to expose the buffer binding, so the
180
+ * package keeps one supported range rather than splitting its floor for this
181
+ * feature. Does nothing in Electron, which binds its own connection source;
182
+ * there only the backend half runs, and it resends nothing to a frontend
183
+ * without the hardening.
184
+ * Returns whether the hardening was installed, for an adopter that wants to
185
+ * branch on it.
186
+ */
187
+ export function bindConnectionResilience(
188
+ isBound: interfaces.IsBound,
189
+ rebind: interfaces.Rebind,
190
+ options: ConnectionResilienceOptions = {}
191
+ ): boolean {
192
+ // Electron binds `WebSocketConnectionSource` to its own source, so a rebind
193
+ // here adds a second socket under the same frontend id.
194
+ if (environment.electron.is()) {
195
+ return false;
196
+ }
197
+ if (!supportsConnectionResilience(isBound)) {
198
+ warnConnectionResilienceUnavailable('frontend');
199
+ return false;
200
+ }
201
+ // Scopes are kept as Theia declares them: one buffer per connection source, one shared socket owner.
202
+ rebind(SocketWriteBuffer).toDynamicValue(() => createFramedSocketWriteBuffer(options));
203
+ rebind(WebSocketConnectionSource).to(SessionAwareConnectionSource).inSingletonScope();
204
+ return true;
205
+ }
@@ -0,0 +1,20 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2026 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 type { Clock as ProtocolClock } from '@hydranium/protocol';
11
+
12
+ /**
13
+ * The protocol's `Clock`, as a Theia container binds it: type and injection
14
+ * token under one identifier, as Theia declares its own. The framework's
15
+ * frontend and backend classes that time or bound something inject it as
16
+ * optional, and use a `SystemClock` when the container binds none, so a
17
+ * container can supply another clock.
18
+ */
19
+ export type Clock = ProtocolClock;
20
+ export const Clock = Symbol('Clock');
@@ -0,0 +1,30 @@
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
+ /**
25
+ * How much each side keeps of what it sent and the peer has not acknowledged
26
+ * yet, in bytes. Past it the copies are discarded and a dropped connection
27
+ * loses them, so it bounds what a peer that never acknowledges can cost.
28
+ */
29
+ readonly unacknowledgedBytes?: number;
30
+ }
@@ -0,0 +1,345 @@
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
+ import { type ConnectionResilienceOptions } from './connection-resilience-options';
13
+
14
+ /**
15
+ * Prefix on every connection log line, deliberately shared by the browser and the
16
+ * server so one pattern greps a console and a pod log at once.
17
+ */
18
+ export const CONNECTION_LOG_PREFIX = '[connection]';
19
+
20
+ /**
21
+ * The part of a socket this buffer uses. Narrower than the socket type Theia
22
+ * declares, which keeps the dependency honest and lets the class be tested
23
+ * without a connection.
24
+ */
25
+ export interface MessageSink {
26
+ /** `sequence` travels as a second socket.io argument, which a reader that does not expect it ignores. */
27
+ send(data: Uint8Array, sequence: number): void;
28
+ }
29
+
30
+ /**
31
+ * Reported when the buffer runs out of room. The message that did not fit is
32
+ * rejected, and so is every further one for as long as the buffer stays full.
33
+ * Reconnecting drains it and sending resumes, but whatever was rejected
34
+ * meanwhile is gone, so the two sides no longer agree about the session.
35
+ */
36
+ export interface ConnectionBufferOverflow {
37
+ /** Messages held when the limit was reached. */
38
+ readonly messages: number;
39
+ /** Bytes held when the limit was reached. */
40
+ readonly bytes: number;
41
+ /** The limit in force, so a reader can tell what to raise it to. */
42
+ readonly maxBytes: number;
43
+ }
44
+
45
+ /**
46
+ * Holds messages while the peer is away and re-sends them when it returns.
47
+ *
48
+ * Theia copies them into one growing byte array and flushes that with a single
49
+ * `send`. A websocket delivers whole messages rather than a byte stream, so
50
+ * Theia's encoding adds no length prefix and every reader assumes one delivery
51
+ * is one message. The concatenated flush breaks that assumption: the reader
52
+ * decodes the first message and throws away the bytes behind it.
53
+ *
54
+ * Changed here: one entry per message, sent one at a time, so each arrives as
55
+ * its own delivery. The buffer also decides whether a message may go out
56
+ * directly — see {@link sendOrQueue} — because a caller that decides for itself
57
+ * is how messages end up overtaking a backlog.
58
+ *
59
+ * Unchanged from Theia: exceeding the limit rejects the message rather than
60
+ * dropping something silently. The limit is settable, since the right value
61
+ * depends on how long an outage has to survive.
62
+ *
63
+ * Added here: every message sent is numbered and kept until the peer
64
+ * acknowledges it, and {@link flush} resends what is still unacknowledged ahead
65
+ * of the backlog. A socket whose far end died unnoticed still accepts writes, so
66
+ * a message is not delivered just because it was sent.
67
+ *
68
+ * Logging goes to `console` rather than through a `Logger`. On the browser side
69
+ * Theia's preloader builds this before any Output channel exists, so there is
70
+ * nothing else to write to; the {@link CONNECTION_LOG_PREFIX} keeps the lines
71
+ * findable on both sides.
72
+ */
73
+ export class FramedSocketWriteBuffer extends SocketWriteBuffer {
74
+ /** Theia's fixed limit, kept as the default. */
75
+ static readonly DEFAULT_MAX_BYTES = 100 * 1024;
76
+
77
+ /**
78
+ * Settable so a deployment can trade memory for longer outages.
79
+ *
80
+ * Consulted directly rather than through the base class's `maxBufferSize`,
81
+ * which does not exist before Theia 1.71 — an `override` of it would not
82
+ * compile against the oldest release this package supports. Nothing is lost:
83
+ * `buffer`, `flush` and `drain` are all replaced here, so the base's own
84
+ * limit is never reached.
85
+ */
86
+ maxBytes = FramedSocketWriteBuffer.DEFAULT_MAX_BYTES;
87
+
88
+ /** Kept copies past this are discarded, so a peer that never acknowledges cannot grow them without bound. */
89
+ static readonly DEFAULT_MAX_UNACKNOWLEDGED_BYTES = 1024 * 1024;
90
+
91
+ /**
92
+ * Separate from {@link maxBytes}, which counts only what is produced while
93
+ * the peer is away: copies kept on a healthy connection must not use up the
94
+ * room an outage needs.
95
+ */
96
+ maxUnacknowledgedBytes = FramedSocketWriteBuffer.DEFAULT_MAX_UNACKNOWLEDGED_BYTES;
97
+
98
+ protected pending: Uint8Array[] = [];
99
+ protected pendingBytes = 0;
100
+ protected overflowReported = false;
101
+ protected readonly onOverflowEmitter = new Emitter<ConnectionBufferOverflow>();
102
+
103
+ protected nextSequence = 1;
104
+ /** Oldest first: {@link acknowledge} stops at the first entry the peer has not accepted. */
105
+ protected unacknowledged: { readonly sequence: number; readonly data: Uint8Array }[] = [];
106
+ protected unacknowledgedBytes = 0;
107
+ protected unacknowledgedOverflowReported = false;
108
+ /**
109
+ * Whether the peer drops a message it already has. One without the hardening
110
+ * applies a resent message a second time, so nothing is resent until the
111
+ * peer has shown otherwise.
112
+ */
113
+ protected peerDeduplicates = false;
114
+
115
+ /** Fires once per outage when the limit is reached. */
116
+ get onOverflow(): Event<ConnectionBufferOverflow> {
117
+ return this.onOverflowEmitter.event;
118
+ }
119
+
120
+ /**
121
+ * Sends `data` now only if nothing is waiting, and queues it otherwise.
122
+ *
123
+ * Callers say whether the peer is ready for a message; the buffer decides
124
+ * whether it may go ahead of one already waiting. Splitting it this way is
125
+ * the point: a caller that answers both questions from one flag is how a
126
+ * fresh message ends up overtaking a backlog.
127
+ */
128
+ sendOrQueue(socket: MessageSink | undefined, data: Uint8Array): void {
129
+ if (socket && !this.hasBacklog) {
130
+ this.send(socket, data);
131
+ } else {
132
+ this.buffer(data);
133
+ }
134
+ }
135
+
136
+ override buffer(data: Uint8Array): void {
137
+ if (this.pendingBytes + data.byteLength > this.maxBytes) {
138
+ this.reportOverflow();
139
+ throw new Error(`Max disconnected buffer size exceeded by adding ${data.byteLength} bytes`);
140
+ }
141
+ // Copied for the same reason the base class copies: the caller owns `data`.
142
+ this.pending.push(data.slice());
143
+ this.pendingBytes += data.byteLength;
144
+ if (this.pending.length === 1) {
145
+ console.info(`${CONNECTION_LOG_PREFIX} buffering messages, the peer is disconnected`);
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Kept only once `send` returns: a message whose send threw stays in the
151
+ * backlog, and keeping it as well would send it again under a second number,
152
+ * which the receiver cannot recognise as a duplicate.
153
+ */
154
+ protected send(socket: MessageSink, data: Uint8Array): void {
155
+ const sequence = this.nextSequence++;
156
+ socket.send(data, sequence);
157
+ this.keep(sequence, data);
158
+ }
159
+
160
+ protected keep(sequence: number, data: Uint8Array): void {
161
+ if (this.unacknowledgedBytes + data.byteLength > this.maxUnacknowledgedBytes) {
162
+ // Said only to a peer known to acknowledge: one without the hardening never does, and
163
+ // nothing would be resent to it anyway.
164
+ if (this.peerDeduplicates && !this.unacknowledgedOverflowReported) {
165
+ this.unacknowledgedOverflowReported = true;
166
+ console.warn(
167
+ `${CONNECTION_LOG_PREFIX} the peer has not acknowledged ${this.unacknowledged.length} message(s), ` +
168
+ `${this.unacknowledgedBytes} bytes; discarding their copies, so a connection dropped now loses them`
169
+ );
170
+ }
171
+ this.unacknowledged = [];
172
+ this.unacknowledgedBytes = 0;
173
+ return;
174
+ }
175
+ // Not copied: a committed write buffer hands over a fresh array and is disposed.
176
+ this.unacknowledged.push({ sequence, data });
177
+ this.unacknowledgedBytes += data.byteLength;
178
+ }
179
+
180
+ /**
181
+ * Records that the peer numbers its messages, which only a peer that also
182
+ * drops duplicates does, so resending to it is safe.
183
+ */
184
+ markPeerDeduplicates(): void {
185
+ this.peerDeduplicates = true;
186
+ }
187
+
188
+ /** Releases every kept message numbered up to `sequence`, which the peer has accepted. */
189
+ acknowledge(sequence: number): void {
190
+ this.peerDeduplicates = true;
191
+ let index = 0;
192
+ while (index < this.unacknowledged.length && this.unacknowledged[index].sequence <= sequence) {
193
+ this.unacknowledgedBytes -= this.unacknowledged[index].data.byteLength;
194
+ index++;
195
+ }
196
+ if (index > 0) {
197
+ this.unacknowledged = this.unacknowledged.slice(index);
198
+ }
199
+ }
200
+
201
+ /**
202
+ * Resends what the peer has not acknowledged, under the same numbers, then
203
+ * sends the backlog. Some of the resent messages may have arrived already;
204
+ * the receiver recognises them by number and drops them.
205
+ *
206
+ * Resends only to a peer known to drop duplicates, and otherwise discards the
207
+ * copies, leaving any message lost with the old socket lost.
208
+ */
209
+ override flush(socket: MessageSink): void {
210
+ if (this.peerDeduplicates) {
211
+ for (const { sequence, data } of this.unacknowledged) {
212
+ socket.send(data, sequence);
213
+ }
214
+ if (this.unacknowledged.length > 0) {
215
+ console.info(
216
+ `${CONNECTION_LOG_PREFIX} resent ${this.unacknowledged.length} unacknowledged message(s), ${this.unacknowledgedBytes} bytes`
217
+ );
218
+ }
219
+ } else {
220
+ this.unacknowledged = [];
221
+ this.unacknowledgedBytes = 0;
222
+ }
223
+ if (this.pending.length === 0) {
224
+ return;
225
+ }
226
+ const count = this.pending.length;
227
+ const bytes = this.pendingBytes;
228
+ // Walked with an index and dropped in one slice, not shifted per message: a shift re-indexes
229
+ // the whole array, so the drain would cost the square of a backlog `bufferBytes` invites an
230
+ // adopter to enlarge. Leaving the queue in place is what keeps `hasBacklog` true until the
231
+ // last message is out, and re-reading the length is what drains one appended by a nested send.
232
+ let index = 0;
233
+ try {
234
+ while (index < this.pending.length) {
235
+ const message = this.pending[index];
236
+ this.send(socket, message);
237
+ index++;
238
+ this.pendingBytes -= message.byteLength;
239
+ }
240
+ } catch (error: unknown) {
241
+ // Said here because nothing retries: Theia flushes once, from a reconnect handler that has
242
+ // deregistered by now, so the stall would otherwise surface much later as an overflow —
243
+ // which names the wrong cause, the socket being up rather than absent.
244
+ console.error(
245
+ `${CONNECTION_LOG_PREFIX} send failed ${index} message(s) into a ${count}-message backlog; ` +
246
+ `${this.pending.length - index} still queued and nothing will retry them`,
247
+ error
248
+ );
249
+ throw error;
250
+ } finally {
251
+ this.pending = this.pending.slice(index);
252
+ }
253
+ this.overflowReported = false;
254
+ console.info(`${CONNECTION_LOG_PREFIX} sent ${count} buffered message(s), ${bytes} bytes`);
255
+ }
256
+
257
+ override drain(): void {
258
+ if (this.pending.length > 0 || this.unacknowledged.length > 0) {
259
+ console.warn(
260
+ `${CONNECTION_LOG_PREFIX} discarded ${this.pending.length} buffered message(s), ${this.pendingBytes} bytes, ` +
261
+ `and ${this.unacknowledged.length} sent but unacknowledged, ${this.unacknowledgedBytes} bytes`
262
+ );
263
+ }
264
+ this.reset();
265
+ }
266
+
267
+ /** Whether messages are still waiting to go out. */
268
+ get hasBacklog(): boolean {
269
+ return this.pending.length > 0;
270
+ }
271
+
272
+ protected reportOverflow(): void {
273
+ if (this.overflowReported) {
274
+ return;
275
+ }
276
+ this.overflowReported = true;
277
+ const overflow: ConnectionBufferOverflow = { messages: this.pending.length, bytes: this.pendingBytes, maxBytes: this.maxBytes };
278
+ console.error(
279
+ `${CONNECTION_LOG_PREFIX} buffer full at ${overflow.maxBytes} bytes after ${overflow.messages} message(s); ` +
280
+ 'this message is rejected, and so is every further one until the peer returns'
281
+ );
282
+ this.onOverflowEmitter.fire(overflow);
283
+ }
284
+
285
+ /** Also discards the kept copies: a drain ends the session, and resending them would replay it into the next one. */
286
+ protected reset(): void {
287
+ this.pending = [];
288
+ this.pendingBytes = 0;
289
+ this.overflowReported = false;
290
+ this.nextSequence = 1;
291
+ this.unacknowledged = [];
292
+ this.unacknowledgedBytes = 0;
293
+ this.unacknowledgedOverflowReported = false;
294
+ this.peerDeduplicates = false;
295
+ }
296
+ }
297
+
298
+ /** Named in the warning a skipped install emits, so the reader is told what to upgrade to. */
299
+ export const REQUIRED_THEIA_HINT = '@theia/core 1.71 or newer';
300
+
301
+ /**
302
+ * Whether this Theia exposes the seam the reconnect hardening replaces.
303
+ *
304
+ * Theia only began binding {@link SocketWriteBuffer} in 1.71; before that each
305
+ * connection built one privately, so there is no binding to rebind and no way
306
+ * in. Every other API involved has been there throughout, which makes this one
307
+ * probe the whole compatibility question — and it is a binding check rather
308
+ * than a version parse, so it answers about the container actually in front of
309
+ * us instead of a number that may have been overridden or vendored.
310
+ *
311
+ * Callers that get `false` must leave Theia's own wiring alone: rebinding half
312
+ * of it would be worse than not rebinding at all.
313
+ */
314
+ export function supportsConnectionResilience(isBound: (identifier: typeof SocketWriteBuffer) => boolean): boolean {
315
+ return isBound(SocketWriteBuffer);
316
+ }
317
+
318
+ /**
319
+ * The warning a skipped install emits. Says what is lost rather than only what
320
+ * is missing: the failures this guards against are silent, so an adopter who
321
+ * never sees this line has no other way to learn the guard is absent.
322
+ */
323
+ export function warnConnectionResilienceUnavailable(tier: 'frontend' | 'backend'): void {
324
+ console.warn(
325
+ `${CONNECTION_LOG_PREFIX} ${tier} reconnect hardening NOT installed: this Theia does not expose an injectable ` +
326
+ `socket write buffer (needs ${REQUIRED_THEIA_HINT}). Theia's own behaviour is left in place, which can lose or ` +
327
+ 'reorder messages when a socket drops and reconnects.'
328
+ );
329
+ }
330
+
331
+ /** Builds a buffer with the configured limits, or the defaults for any not configured. */
332
+ export function createFramedSocketWriteBuffer(options: ConnectionResilienceOptions = {}): FramedSocketWriteBuffer {
333
+ const buffer = new FramedSocketWriteBuffer();
334
+ if (isPositive(options.bufferBytes)) {
335
+ buffer.maxBytes = options.bufferBytes;
336
+ }
337
+ if (isPositive(options.unacknowledgedBytes)) {
338
+ buffer.maxUnacknowledgedBytes = options.unacknowledgedBytes;
339
+ }
340
+ return buffer;
341
+ }
342
+
343
+ function isPositive(value: number | undefined): value is number {
344
+ return typeof value === 'number' && value > 0;
345
+ }