@hydranium/client-theia 1.0.0-next.13 → 1.0.0-next.132

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 (81) hide show
  1. package/README.md +35 -8
  2. package/lib/browser/channel-logger.d.ts +4 -3
  3. package/lib/browser/channel-logger.d.ts.map +1 -1
  4. package/lib/browser/channel-logger.js +9 -4
  5. package/lib/browser/channel-logger.js.map +1 -1
  6. package/lib/browser/connection-diagnostics-contribution.d.ts +69 -0
  7. package/lib/browser/connection-diagnostics-contribution.d.ts.map +1 -0
  8. package/lib/browser/connection-diagnostics-contribution.js +153 -0
  9. package/lib/browser/connection-diagnostics-contribution.js.map +1 -0
  10. package/lib/browser/editor-disk-sync.d.ts +133 -0
  11. package/lib/browser/editor-disk-sync.d.ts.map +1 -0
  12. package/lib/browser/editor-disk-sync.js +232 -0
  13. package/lib/browser/editor-disk-sync.js.map +1 -0
  14. package/lib/browser/hydranium-file-service.d.ts +44 -0
  15. package/lib/browser/hydranium-file-service.d.ts.map +1 -0
  16. package/lib/browser/hydranium-file-service.js +61 -0
  17. package/lib/browser/hydranium-file-service.js.map +1 -0
  18. package/lib/browser/index.d.ts +4 -0
  19. package/lib/browser/index.d.ts.map +1 -1
  20. package/lib/browser/index.js +4 -0
  21. package/lib/browser/index.js.map +1 -1
  22. package/lib/browser/log-level-preference.d.ts.map +1 -1
  23. package/lib/browser/log-level-preference.js +11 -0
  24. package/lib/browser/log-level-preference.js.map +1 -1
  25. package/lib/browser/memory-diagnostics-contribution.d.ts +40 -4
  26. package/lib/browser/memory-diagnostics-contribution.d.ts.map +1 -1
  27. package/lib/browser/memory-diagnostics-contribution.js +161 -31
  28. package/lib/browser/memory-diagnostics-contribution.js.map +1 -1
  29. package/lib/browser/session-aware-connection-source.d.ts +98 -0
  30. package/lib/browser/session-aware-connection-source.d.ts.map +1 -0
  31. package/lib/browser/session-aware-connection-source.js +167 -0
  32. package/lib/browser/session-aware-connection-source.js.map +1 -0
  33. package/lib/common/clock.d.ts +19 -0
  34. package/lib/common/clock.d.ts.map +1 -0
  35. package/lib/common/clock.js +13 -0
  36. package/lib/common/clock.js.map +1 -0
  37. package/lib/common/connection-resilience-options.d.ts +24 -0
  38. package/lib/common/connection-resilience-options.d.ts.map +1 -0
  39. package/lib/common/connection-resilience-options.js +11 -0
  40. package/lib/common/connection-resilience-options.js.map +1 -0
  41. package/lib/common/framed-socket-write-buffer.d.ts +121 -0
  42. package/lib/common/framed-socket-write-buffer.d.ts.map +1 -0
  43. package/lib/common/framed-socket-write-buffer.js +190 -0
  44. package/lib/common/framed-socket-write-buffer.js.map +1 -0
  45. package/lib/common/index.d.ts +12 -0
  46. package/lib/common/index.d.ts.map +1 -0
  47. package/lib/common/index.js +31 -0
  48. package/lib/common/index.js.map +1 -0
  49. package/lib/node/abstract-socket-forwarding-connection-handler.d.ts +8 -7
  50. package/lib/node/abstract-socket-forwarding-connection-handler.d.ts.map +1 -1
  51. package/lib/node/abstract-socket-forwarding-connection-handler.js +28 -7
  52. package/lib/node/abstract-socket-forwarding-connection-handler.js.map +1 -1
  53. package/lib/node/index.d.ts +2 -0
  54. package/lib/node/index.d.ts.map +1 -1
  55. package/lib/node/index.js +2 -0
  56. package/lib/node/index.js.map +1 -1
  57. package/lib/node/session-bound-frontend-connection-service.d.ts +77 -0
  58. package/lib/node/session-bound-frontend-connection-service.d.ts.map +1 -0
  59. package/lib/node/session-bound-frontend-connection-service.js +128 -0
  60. package/lib/node/session-bound-frontend-connection-service.js.map +1 -0
  61. package/lib/node/socket-channel-forwarder.d.ts +54 -0
  62. package/lib/node/socket-channel-forwarder.d.ts.map +1 -0
  63. package/lib/node/socket-channel-forwarder.js +93 -0
  64. package/lib/node/socket-channel-forwarder.js.map +1 -0
  65. package/package.json +24 -9
  66. package/src/browser/channel-logger.ts +9 -4
  67. package/src/browser/connection-diagnostics-contribution.ts +142 -0
  68. package/src/browser/editor-disk-sync.ts +236 -0
  69. package/src/browser/hydranium-file-service.ts +56 -0
  70. package/src/browser/index.ts +4 -0
  71. package/src/browser/log-level-preference.ts +11 -0
  72. package/src/browser/memory-diagnostics-contribution.ts +268 -45
  73. package/src/browser/session-aware-connection-source.ts +179 -0
  74. package/src/common/clock.ts +20 -0
  75. package/src/common/connection-resilience-options.ts +24 -0
  76. package/src/common/framed-socket-write-buffer.ts +227 -0
  77. package/src/common/index.ts +15 -0
  78. package/src/node/abstract-socket-forwarding-connection-handler.ts +19 -9
  79. package/src/node/index.ts +2 -0
  80. package/src/node/session-bound-frontend-connection-service.ts +155 -0
  81. package/src/node/socket-channel-forwarder.ts +99 -0
@@ -0,0 +1,236 @@
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 { SystemClock, TIMED_OUT } from '@hydranium/protocol';
11
+ import type { FrontendApplicationContribution } from '@theia/core/lib/browser';
12
+ import { ILogger } from '@theia/core/lib/common/logger';
13
+ import URI from '@theia/core/lib/common/uri';
14
+ import { inject, injectable, optional } from '@theia/core/shared/inversify';
15
+ import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
16
+ import type { EditorWidget } from '@theia/editor/lib/browser/editor-widget';
17
+ import type { FileResourceVersion } from '@theia/filesystem/lib/browser/file-resource';
18
+ import { FileService, type TextFileContent } from '@theia/filesystem/lib/browser/file-service';
19
+ import { Clock } from '../common/clock';
20
+
21
+ /**
22
+ * The members of Theia's Monaco editor model {@link EditorDiskSync} reads and
23
+ * resets. `contentChanges`, `resourceVersion` and `run` are `protected` there,
24
+ * so {@link isResyncableEditorDocument} checks for them at runtime: a Theia
25
+ * that renames them yields a document it refuses, and the sync then leaves it
26
+ * alone.
27
+ */
28
+ export interface ResyncableEditorDocument {
29
+ readonly uri: string;
30
+ readonly dirty: boolean;
31
+ getText(): string;
32
+ revert(options: { soft: boolean }): Promise<void>;
33
+ /** The edits the next save applies to the file, in place of the whole text. */
34
+ readonly contentChanges: unknown[];
35
+ /** The file version the next save expects to find on disk. */
36
+ resourceVersion: FileResourceVersion | undefined;
37
+ /** Queue `operation` behind the model's saves and syncs in progress. */
38
+ run(operation: () => Promise<void>): Promise<void>;
39
+ /** Awaited by each save before it takes the pending edits. */
40
+ onModelWillSaveModel(listener: () => Promise<void>): { dispose(): void };
41
+ }
42
+
43
+ /** Whether `document` carries the members of {@link ResyncableEditorDocument}. */
44
+ export function isResyncableEditorDocument(document: object): document is ResyncableEditorDocument {
45
+ return (
46
+ 'uri' in document &&
47
+ typeof document.uri === 'string' &&
48
+ 'dirty' in document &&
49
+ 'getText' in document &&
50
+ typeof document.getText === 'function' &&
51
+ 'revert' in document &&
52
+ typeof document.revert === 'function' &&
53
+ 'contentChanges' in document &&
54
+ Array.isArray(document.contentChanges) &&
55
+ 'resourceVersion' in document &&
56
+ 'run' in document &&
57
+ typeof document.run === 'function' &&
58
+ 'onModelWillSaveModel' in document &&
59
+ typeof document.onModelWillSaveModel === 'function'
60
+ );
61
+ }
62
+
63
+ /**
64
+ * Keeps a dirty editor's save from applying edits its file already holds, as
65
+ * the file does once the server saves a document the editor shows unsaved.
66
+ *
67
+ * Theia keeps such an editor dirty, and its next save corrupts the file: the
68
+ * save applies the editor's pending edits to the file rather than writing the
69
+ * whole text, and the file already holds them, so each is applied twice.
70
+ * Theia's check that the file has not changed since the editor read it passes
71
+ * whenever the new file has the old one's size, so no conflict is reported. A
72
+ * revert alone keeps the pending edits.
73
+ *
74
+ * Before each save of the editor it reads the file. When the file holds the
75
+ * text the buffer held as the save began, it drops the edits pending then and
76
+ * has the save expect the file's version, so the save writes only the edits
77
+ * made after that point, a save participant's included, onto the file as it
78
+ * is. The save is not cancelled: it completes and is reported as saved. Save
79
+ * All relies on this check, since it saves a diagram and then an editor on the
80
+ * same file at once, before the file watcher has reported the diagram's write.
81
+ * The buffer is taken when the save calls its will-save listeners. Theia runs
82
+ * its save participants one after another, each behind an await, and its
83
+ * first one awaits before it edits, so none has changed the buffer by then. A
84
+ * participant ordered ahead of it that edits before its first await makes the
85
+ * file and the taken text differ, and the save applies every edit again.
86
+ *
87
+ * On each change the file watcher reports, it marks the editor clean when the
88
+ * file holds exactly its buffer. The file is compared with the buffer after
89
+ * it has been read, and the editor is marked in the same step, so a keystroke
90
+ * made while the file was read leaves the editor dirty. This sync waits behind
91
+ * a save in flight: that save drops the edits it sent from the pending ones
92
+ * when it finishes, and a sync before then would have it drop the edits typed
93
+ * meanwhile.
94
+ *
95
+ * An editor whose file holds neither text is left to Theia's own save,
96
+ * same-size gap included, and so is one whose file cannot be read within
97
+ * {@link readTimeoutMs}, or whose check throws.
98
+ *
99
+ * The adopter binds it as a `FrontendApplicationContribution`.
100
+ */
101
+ @injectable()
102
+ export class EditorDiskSync implements FrontendApplicationContribution {
103
+ @inject(FileService) protected readonly fileService!: FileService;
104
+ @inject(EditorManager) protected readonly editorManager!: EditorManager;
105
+ @inject(ILogger) protected readonly logger!: ILogger;
106
+ /** Times {@link readTimeoutMs}: the container's {@link Clock}, or a `SystemClock`. */
107
+ @inject(Clock) @optional() protected readonly clock: Clock = new SystemClock();
108
+ /** Whether an editor's document has failed {@link isResyncableEditorDocument}; warned of once. */
109
+ protected warnedUnsyncable = false;
110
+ /**
111
+ * How long a check waits for the file. The editor's save, and every later
112
+ * save and sync of that editor, waits for the check.
113
+ */
114
+ protected readTimeoutMs = 1_000;
115
+
116
+ onStart(): void {
117
+ this.fileService.onDidFilesChange(event => {
118
+ for (const change of event.changes) {
119
+ this.checkFile(change.resource);
120
+ }
121
+ });
122
+ this.editorManager.onCreated(widget => this.guardSaves(widget));
123
+ for (const widget of this.editorManager.all) {
124
+ this.guardSaves(widget);
125
+ }
126
+ }
127
+
128
+ /** Check `widget`'s document before each of its saves, for as long as the widget lives. */
129
+ protected guardSaves(widget: EditorWidget): void {
130
+ const document = this.resyncable(widget.editor.document);
131
+ if (document) {
132
+ const registration = document.onModelWillSaveModel(() => this.isolate(() => this.dropChangesOnDisk(document)));
133
+ widget.onDidDispose(() => registration.dispose());
134
+ }
135
+ }
136
+
137
+ /** Check each dirty document an editor shows on `uri`, behind that document's save in flight. */
138
+ protected checkFile(uri: URI): void {
139
+ for (const document of this.dirtyDocuments(uri)) {
140
+ void document.run(() => this.isolate(() => this.syncIfOnDisk(document)));
141
+ }
142
+ }
143
+
144
+ /** Read the file of `document` if it is dirty, and sync it if the file holds its buffer. */
145
+ protected async syncIfOnDisk(document: ResyncableEditorDocument): Promise<void> {
146
+ if (!document.dirty) {
147
+ return;
148
+ }
149
+ const content = await this.readFile(document);
150
+ if (content && document.getText() === content.value) {
151
+ this.syncFromDisk(document, { etag: content.etag, mtime: content.mtime, encoding: content.encoding });
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Before a save of `document`, drop the edits pending now if its file holds
157
+ * the buffer as it is now, and take the file's version. The buffer and the
158
+ * count are taken before the first await, while the save is still calling
159
+ * its will-save listeners; an edit made after that stays pending, and the
160
+ * save writes it onto the file.
161
+ */
162
+ protected async dropChangesOnDisk(document: ResyncableEditorDocument): Promise<void> {
163
+ if (!document.dirty) {
164
+ return;
165
+ }
166
+ const text = document.getText();
167
+ const pending = document.contentChanges.length;
168
+ const content = await this.readFile(document);
169
+ if (content?.value === text) {
170
+ document.contentChanges.splice(0, pending);
171
+ document.resourceVersion = { etag: content.etag, mtime: content.mtime, encoding: content.encoding };
172
+ }
173
+ }
174
+
175
+ /**
176
+ * The file of `document`, or `undefined` when it is gone, cannot be read, or
177
+ * is not read within {@link readTimeoutMs}.
178
+ */
179
+ protected async readFile(document: ResyncableEditorDocument): Promise<TextFileContent | undefined> {
180
+ const read = this.fileService.read(new URI(document.uri)).catch(() => undefined);
181
+ const content = await this.clock.raceTimer(read, this.readTimeoutMs);
182
+ return content === TIMED_OUT ? undefined : content;
183
+ }
184
+
185
+ /**
186
+ * Run `check`, and log its failure rather than pass it on. A will-save
187
+ * listener that rejects makes Theia drop the save without writing, and a
188
+ * sync that rejects goes unhandled.
189
+ */
190
+ protected async isolate(check: () => Promise<void>): Promise<void> {
191
+ try {
192
+ await check();
193
+ } catch (err: unknown) {
194
+ void this.logger.error('EditorDiskSync: checking an editor against its file failed; the editor is left as it is.', err);
195
+ }
196
+ }
197
+
198
+ /** The dirty documents open in an editor on `uri`, each once however many editors show it. */
199
+ protected dirtyDocuments(uri: URI): ResyncableEditorDocument[] {
200
+ const target = uri.toString();
201
+ const documents = new Set<ResyncableEditorDocument>();
202
+ for (const widget of this.editorManager.all) {
203
+ const document = this.resyncable(widget.editor.document);
204
+ if (document && document.uri === target && document.dirty) {
205
+ documents.add(document);
206
+ }
207
+ }
208
+ return [...documents];
209
+ }
210
+
211
+ /** `document` if it carries the members the sync resets; otherwise `undefined`, warned of once. */
212
+ protected resyncable(document: object): ResyncableEditorDocument | undefined {
213
+ if (isResyncableEditorDocument(document)) {
214
+ return document;
215
+ }
216
+ if (!this.warnedUnsyncable) {
217
+ this.warnedUnsyncable = true;
218
+ void this.logger.warn(
219
+ 'EditorDiskSync: an editor document lacks the Monaco model members the sync resets, so such editors are not synced with their files.'
220
+ );
221
+ }
222
+ return undefined;
223
+ }
224
+
225
+ /**
226
+ * Take `version` as the file `document` was last saved to, with nothing
227
+ * pending, and mark it clean. Synchronous throughout: a soft revert only
228
+ * marks the document clean, and reads nothing. The revert also cancels a
229
+ * save queued behind the sync, which would have written the same text.
230
+ */
231
+ protected syncFromDisk(document: ResyncableEditorDocument, version: FileResourceVersion): void {
232
+ document.contentChanges.splice(0, document.contentChanges.length);
233
+ document.resourceVersion = version;
234
+ void document.revert({ soft: true });
235
+ }
236
+ }
@@ -0,0 +1,56 @@
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 URI from '@theia/core/lib/common/uri';
11
+ import { injectable } from '@theia/core/shared/inversify';
12
+ import { FileService, type UpdateTextFileOptions } from '@theia/filesystem/lib/browser/file-service';
13
+ import { ETAG_DISABLED } from '@theia/filesystem/lib/common/files';
14
+
15
+ /**
16
+ * Theia's `FileService`, refusing an incremental save once the file has
17
+ * changed since the editor read it.
18
+ *
19
+ * Theia's own check, which `write` and `update` share, passes a file whose
20
+ * mtime moved on when its size still matches the etag the editor read. That
21
+ * is safe for `write`, which replaces the file with the whole text, but
22
+ * `update` applies the editor's pending edits to the file as it is, so after a
23
+ * same-size change, the server saving those very edits included, it applies
24
+ * them a second time. Here `update` hands that check the etag with a `!`
25
+ * appended, which no file's etag can match: Theia writes an etag as the mtime
26
+ * in base 29 followed by the size in base 31, digits and lower-case letters
27
+ * only. An advanced mtime is then enough for Theia to refuse with its
28
+ * `FILE_MODIFIED_SINCE`. The editor's save then falls back to `write` with the
29
+ * whole text, which Theia checks as before: it writes when the size is
30
+ * unchanged, and asks the user otherwise. A same-size change by another
31
+ * process is therefore overwritten by that write rather than merged with the
32
+ * editor's edits. `write` is left as it is.
33
+ *
34
+ * It is the safety net behind `EditorDiskSync`, which acts only when the
35
+ * file holds the editor's buffer: a file holding some of the edits, as when a
36
+ * save participant ordered ahead of Theia's first one edits before its first
37
+ * await, is left to this check.
38
+ *
39
+ * A write within the file system's mtime resolution of the read goes unseen,
40
+ * as it does for Theia's own check. A file whose mtime moves while its text
41
+ * stays the same costs a whole-text write rather than the edits.
42
+ *
43
+ * The adopter binds it with `rebind(FileService).to(HydraniumFileService).inSingletonScope()`.
44
+ */
45
+ @injectable()
46
+ export class HydraniumFileService extends FileService {
47
+ override update(
48
+ resource: URI,
49
+ changes: Parameters<FileService['update']>[1],
50
+ options: UpdateTextFileOptions
51
+ ): ReturnType<FileService['update']> {
52
+ const strict =
53
+ typeof options.etag === 'string' && options.etag !== ETAG_DISABLED ? { ...options, etag: `${options.etag}!` } : options;
54
+ return super.update(resource, changes, strict);
55
+ }
56
+ }
@@ -12,3 +12,7 @@ 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';
17
+ export * from './editor-disk-sync';
18
+ export * from './hydranium-file-service';
@@ -108,7 +108,18 @@ export class LogLevelPreferenceContribution implements FrontendApplicationContri
108
108
  protected applyLevel(): void {
109
109
  const level = parseLogLevel(this.preferences.get<string>(this.preferenceName));
110
110
  if (level) {
111
+ const previous = Logger.getLevel();
111
112
  Logger.setLevel(level);
113
+ if (previous !== level) {
114
+ // Through Theia's `ILogger`, which has a threshold of its own and so
115
+ // is not silenced by the value just applied — the framework logger
116
+ // this preference governs would drop the line on a switch to `error`.
117
+ //
118
+ // Frontend and server announce separately: this preference reaches
119
+ // the server only if a client delivers it as configuration, so one
120
+ // line without the other says it did not.
121
+ this.logger.info(`Log level ${previous} → ${level} (preference '${this.preferenceName}')`);
122
+ }
112
123
  }
113
124
  }
114
125
  }