@hydranium/client-theia 1.0.0-next.51 → 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.
Files changed (41) hide show
  1. package/lib/browser/connection-diagnostics-contribution.d.ts +69 -0
  2. package/lib/browser/connection-diagnostics-contribution.d.ts.map +1 -0
  3. package/lib/browser/connection-diagnostics-contribution.js +153 -0
  4. package/lib/browser/connection-diagnostics-contribution.js.map +1 -0
  5. package/lib/browser/index.d.ts +2 -0
  6. package/lib/browser/index.d.ts.map +1 -1
  7. package/lib/browser/index.js +2 -0
  8. package/lib/browser/index.js.map +1 -1
  9. package/lib/browser/session-aware-connection-source.d.ts +94 -0
  10. package/lib/browser/session-aware-connection-source.d.ts.map +1 -0
  11. package/lib/browser/session-aware-connection-source.js +163 -0
  12. package/lib/browser/session-aware-connection-source.js.map +1 -0
  13. package/lib/common/connection-resilience-options.d.ts +24 -0
  14. package/lib/common/connection-resilience-options.d.ts.map +1 -0
  15. package/lib/common/connection-resilience-options.js +11 -0
  16. package/lib/common/connection-resilience-options.js.map +1 -0
  17. package/lib/common/framed-socket-write-buffer.d.ts +121 -0
  18. package/lib/common/framed-socket-write-buffer.d.ts.map +1 -0
  19. package/lib/common/framed-socket-write-buffer.js +176 -0
  20. package/lib/common/framed-socket-write-buffer.js.map +1 -0
  21. package/lib/common/index.d.ts +11 -0
  22. package/lib/common/index.d.ts.map +1 -0
  23. package/lib/common/index.js +30 -0
  24. package/lib/common/index.js.map +1 -0
  25. package/lib/node/index.d.ts +1 -0
  26. package/lib/node/index.d.ts.map +1 -1
  27. package/lib/node/index.js +1 -0
  28. package/lib/node/index.js.map +1 -1
  29. package/lib/node/session-bound-frontend-connection-service.d.ts +73 -0
  30. package/lib/node/session-bound-frontend-connection-service.d.ts.map +1 -0
  31. package/lib/node/session-bound-frontend-connection-service.js +124 -0
  32. package/lib/node/session-bound-frontend-connection-service.js.map +1 -0
  33. package/package.json +12 -3
  34. package/src/browser/connection-diagnostics-contribution.ts +142 -0
  35. package/src/browser/index.ts +2 -0
  36. package/src/browser/session-aware-connection-source.ts +175 -0
  37. package/src/common/connection-resilience-options.ts +24 -0
  38. package/src/common/framed-socket-write-buffer.ts +212 -0
  39. package/src/common/index.ts +14 -0
  40. package/src/node/index.ts +1 -0
  41. package/src/node/session-bound-frontend-connection-service.ts +151 -0
@@ -0,0 +1,151 @@
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 { BackendApplicationConfigProvider } from '@theia/core/lib/node/backend-application-config-provider';
11
+ import { SocketWriteBuffer } from '@theia/core/lib/common/messaging/socket-write-buffer';
12
+ import { WebsocketFrontendConnectionService } from '@theia/core/lib/node/messaging/websocket-frontend-connection-service';
13
+ import { injectable, type interfaces } from '@theia/core/shared/inversify';
14
+ import { type ConnectionResilienceOptions } from '../common/connection-resilience-options';
15
+ import {
16
+ CONNECTION_LOG_PREFIX as PREFIX,
17
+ createFramedSocketWriteBuffer,
18
+ supportsConnectionResilience,
19
+ warnConnectionResilienceUnavailable
20
+ } from '../common/framed-socket-write-buffer';
21
+
22
+ /**
23
+ * Socket Theia hands the disconnect handler, read off the base signature rather
24
+ * than imported, which spares a direct `socket.io` dependency carried purely
25
+ * for a parameter type.
26
+ */
27
+ export type FrontendSocket = Parameters<WebsocketFrontendConnectionService['handleSocketDisconnect']>[0];
28
+
29
+ /**
30
+ * Channel Theia hands the disconnect handler, read off the base signature so
31
+ * this package's emitted declarations name only
32
+ * `WebsocketFrontendConnectionService`.
33
+ *
34
+ * `ReconnectableSocketChannel` is not exported by every Theia release in the
35
+ * supported range, so importing it would make those declarations unresolvable
36
+ * for an adopter on one of them — a compile error for a feature they may not
37
+ * even be using.
38
+ */
39
+ export type FrontendChannel = Parameters<WebsocketFrontendConnectionService['handleSocketDisconnect']>[1];
40
+
41
+ /**
42
+ * Ignores a disconnect from a socket that no longer belongs to the frontend.
43
+ *
44
+ * A frontend that loses its network reconnects on a new socket within seconds;
45
+ * the server only notices the old one is gone when its next heartbeat write
46
+ * fails, which is a whole ping interval later. For that stretch both sockets are
47
+ * registered against the same session, and Theia's disconnect handler tears the
48
+ * session off its socket without checking which one just died. The late
49
+ * notification from the abandoned socket therefore cuts the healthy one loose.
50
+ * The frontend sees a connected socket and never retries, so the session is dead
51
+ * until the page is reloaded.
52
+ *
53
+ * Changed here: only the socket the session is currently using may act on a
54
+ * disconnect. The handler is rewritten rather than wrapped because the problem
55
+ * sits inside the listener Theia installs; the rest of the body is Theia's.
56
+ */
57
+ @injectable()
58
+ export class SessionBoundFrontendConnectionService extends WebsocketFrontendConnectionService {
59
+ /** Socket each frontend's channel is currently bound to, with the time it was bound. */
60
+ protected readonly bindings = new Map<string, { socketId: string; boundAt: number }>();
61
+ /** When a frontend's live socket went away, so the reconnect gap can be reported. */
62
+ protected readonly offlineSince = new Map<string, number>();
63
+
64
+ override handleSocketDisconnect(socket: FrontendSocket, channel: FrontendChannel, frontEndId: string): void {
65
+ const previous = this.bindings.get(frontEndId);
66
+ this.bindings.set(frontEndId, { socketId: socket.id, boundAt: Date.now() });
67
+
68
+ const wentOfflineAt = this.offlineSince.get(frontEndId);
69
+ this.offlineSince.delete(frontEndId);
70
+ const replacing = previous ? `, replacing socket ${previous.socketId}` : '';
71
+ const gap = wentOfflineAt === undefined ? '' : `, after ${Date.now() - wentOfflineAt} ms offline`;
72
+ console.info(`${PREFIX} frontend ${frontEndId} channel bound to socket ${socket.id}${replacing}${gap}`);
73
+
74
+ socket.on('disconnect', reason => {
75
+ const bound = this.bindings.get(frontEndId);
76
+ if (bound?.socketId !== socket.id) {
77
+ // Either a superseded socket being reaped late, or one outliving `closeConnection`, which
78
+ // deletes the binding. Neither owns the channel, so neither may disconnect it, arm a close
79
+ // timeout, or close a connection that is no longer in `connectionsByFrontend`.
80
+ const owner = bound
81
+ ? `socket ${bound.socketId} owns it (bound ${Date.now() - bound.boundAt} ms ago)`
82
+ : 'the connection is already closed';
83
+ console.info(`${PREFIX} ignoring disconnect of stale socket ${socket.id} (frontend ${frontEndId}, ${reason}); ${owner}`);
84
+ return;
85
+ }
86
+
87
+ // From here on: Theia's own handler body, unchanged apart from naming the socket.
88
+ this.offlineSince.set(frontEndId, Date.now());
89
+ console.info(`${PREFIX} socket ${socket.id} (frontend ${frontEndId}) disconnected: ${reason}`);
90
+ channel.disconnect();
91
+ const timeout = this.connectionTimeout();
92
+ const isMarkedForClose = this.channelsMarkedForClose.delete(frontEndId);
93
+ if (timeout === 0 || isMarkedForClose) {
94
+ this.closeConnection(frontEndId, reason);
95
+ } else if (timeout > 0) {
96
+ console.info(`${PREFIX} close timeout for frontend ${frontEndId} set to ${timeout} ms`);
97
+ this.closeTimeouts.set(
98
+ frontEndId,
99
+ setTimeout(() => this.closeConnection(frontEndId, reason), timeout)
100
+ );
101
+ }
102
+ // timeout < 0: never close the back end.
103
+ });
104
+ }
105
+
106
+ protected override closeConnection(frontEndId: string, reason: string): void {
107
+ console.info(`${PREFIX} closing frontend ${frontEndId}: ${reason}`);
108
+ this.bindings.delete(frontEndId);
109
+ this.offlineSince.delete(frontEndId);
110
+ super.closeConnection(frontEndId, reason);
111
+ }
112
+
113
+ /** Mirrors Theia's private `frontendConnectionTimeout`, including `Number('')` resolving to 0. */
114
+ protected connectionTimeout(): number {
115
+ const envValue = Number(process.env['FRONTEND_CONNECTION_TIMEOUT']);
116
+ if (!isNaN(envValue)) {
117
+ return envValue;
118
+ }
119
+ return BackendApplicationConfigProvider.get().frontendConnectionTimeout;
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Replaces the server pieces that decide how a session survives a reconnect.
125
+ *
126
+ * `FrontendConnectionService` is bound `toService(WebsocketFrontendConnectionService)`,
127
+ * so rebinding the concrete class is enough. The buffer stays transient (one per
128
+ * connection), matching Theia's `bind(SocketWriteBuffer).toSelf()`; the frontend
129
+ * counterpart is bound by `bindConnectionResilience` in a `frontendPreload`
130
+ * module, which is the only point early enough on that side.
131
+ *
132
+ * Does nothing but warn on a Theia too old to expose the buffer binding, so the
133
+ * package keeps one supported range rather than splitting its floor for this
134
+ * feature. Returns whether the hardening was installed, for an adopter that
135
+ * wants to branch on it.
136
+ */
137
+ export function bindConnectionResilience(
138
+ bind: interfaces.Bind,
139
+ isBound: interfaces.IsBound,
140
+ rebind: interfaces.Rebind,
141
+ options: ConnectionResilienceOptions = {}
142
+ ): boolean {
143
+ if (!supportsConnectionResilience(isBound)) {
144
+ warnConnectionResilienceUnavailable('backend');
145
+ return false;
146
+ }
147
+ bind(SessionBoundFrontendConnectionService).toSelf().inSingletonScope();
148
+ rebind(WebsocketFrontendConnectionService).toService(SessionBoundFrontendConnectionService);
149
+ rebind(SocketWriteBuffer).toDynamicValue(() => createFramedSocketWriteBuffer(options.bufferBytes));
150
+ return true;
151
+ }