@hydranium/protocol 1.0.0-next.33 → 1.0.0-next.331

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 (285) hide show
  1. package/README.md +44 -83
  2. package/lib/abstract-logger.d.ts +5 -0
  3. package/lib/abstract-logger.d.ts.map +1 -1
  4. package/lib/abstract-logger.js +7 -0
  5. package/lib/abstract-logger.js.map +1 -1
  6. package/lib/client/data-connection.d.ts +302 -0
  7. package/lib/client/data-connection.d.ts.map +1 -0
  8. package/lib/client/data-connection.js +533 -0
  9. package/lib/client/data-connection.js.map +1 -0
  10. package/lib/client/data-events.d.ts +17 -1
  11. package/lib/client/data-events.d.ts.map +1 -1
  12. package/lib/client/data-events.js +25 -0
  13. package/lib/client/data-events.js.map +1 -1
  14. package/lib/client/data-port.d.ts +34 -24
  15. package/lib/client/data-port.d.ts.map +1 -1
  16. package/lib/client/data-session.d.ts +684 -89
  17. package/lib/client/data-session.d.ts.map +1 -1
  18. package/lib/client/data-session.js +1148 -115
  19. package/lib/client/data-session.js.map +1 -1
  20. package/lib/client/index.d.ts +15 -9
  21. package/lib/client/index.d.ts.map +1 -1
  22. package/lib/client/index.js +15 -9
  23. package/lib/client/index.js.map +1 -1
  24. package/lib/client/message-relay.d.ts +4 -4
  25. package/lib/client/message-relay.js +4 -4
  26. package/lib/client/message-relay.js.map +1 -1
  27. package/lib/client/post-message-transport.d.ts +64 -3
  28. package/lib/client/post-message-transport.d.ts.map +1 -1
  29. package/lib/client/post-message-transport.js +175 -1
  30. package/lib/client/post-message-transport.js.map +1 -1
  31. package/lib/client/rpc-connection.d.ts +157 -0
  32. package/lib/client/rpc-connection.d.ts.map +1 -0
  33. package/lib/client/rpc-connection.js +214 -0
  34. package/lib/client/rpc-connection.js.map +1 -0
  35. package/lib/client/worker-handshake.d.ts +86 -0
  36. package/lib/client/worker-handshake.d.ts.map +1 -0
  37. package/lib/client/worker-handshake.js +51 -0
  38. package/lib/client/worker-handshake.js.map +1 -0
  39. package/lib/client-ids.d.ts +45 -0
  40. package/lib/client-ids.d.ts.map +1 -0
  41. package/lib/client-ids.js +48 -0
  42. package/lib/client-ids.js.map +1 -0
  43. package/lib/clock.d.ts +50 -10
  44. package/lib/clock.d.ts.map +1 -1
  45. package/lib/clock.js +36 -1
  46. package/lib/clock.js.map +1 -1
  47. package/lib/console-logger.d.ts +23 -0
  48. package/lib/console-logger.d.ts.map +1 -0
  49. package/lib/console-logger.js +39 -0
  50. package/lib/console-logger.js.map +1 -0
  51. package/lib/data/data-protocol-methods.d.ts +4 -4
  52. package/lib/data/data-protocol-methods.d.ts.map +1 -1
  53. package/lib/data/data-protocol-methods.js +13 -1
  54. package/lib/data/data-protocol-methods.js.map +1 -1
  55. package/lib/data/data-server-protocol.d.ts +170 -48
  56. package/lib/data/data-server-protocol.d.ts.map +1 -1
  57. package/lib/data/events.d.ts +137 -21
  58. package/lib/data/events.d.ts.map +1 -1
  59. package/lib/data/requests.d.ts +71 -11
  60. package/lib/data/requests.d.ts.map +1 -1
  61. package/lib/debouncer.d.ts +3 -3
  62. package/lib/debouncer.d.ts.map +1 -1
  63. package/lib/debouncer.js +6 -9
  64. package/lib/debouncer.js.map +1 -1
  65. package/lib/errors.d.ts +183 -30
  66. package/lib/errors.d.ts.map +1 -1
  67. package/lib/errors.js +268 -32
  68. package/lib/errors.js.map +1 -1
  69. package/lib/glsp-edits-settled-actions.d.ts +52 -0
  70. package/lib/glsp-edits-settled-actions.d.ts.map +1 -0
  71. package/lib/glsp-edits-settled-actions.js +28 -0
  72. package/lib/glsp-edits-settled-actions.js.map +1 -0
  73. package/lib/glsp-request-model-args.d.ts +16 -0
  74. package/lib/glsp-request-model-args.d.ts.map +1 -0
  75. package/lib/glsp-request-model-args.js +19 -0
  76. package/lib/glsp-request-model-args.js.map +1 -0
  77. package/lib/glsp-save-model-actions.d.ts +50 -0
  78. package/lib/glsp-save-model-actions.d.ts.map +1 -0
  79. package/lib/glsp-save-model-actions.js +28 -0
  80. package/lib/glsp-save-model-actions.js.map +1 -0
  81. package/lib/index.d.ts +10 -0
  82. package/lib/index.d.ts.map +1 -1
  83. package/lib/index.js +10 -0
  84. package/lib/index.js.map +1 -1
  85. package/lib/latency-collector.d.ts +8 -4
  86. package/lib/latency-collector.d.ts.map +1 -1
  87. package/lib/latency-collector.js.map +1 -1
  88. package/lib/logger.d.ts +38 -7
  89. package/lib/logger.d.ts.map +1 -1
  90. package/lib/logger.js +42 -4
  91. package/lib/logger.js.map +1 -1
  92. package/lib/messages/index.d.ts +4 -2
  93. package/lib/messages/index.d.ts.map +1 -1
  94. package/lib/messages/index.js +15 -4
  95. package/lib/messages/index.js.map +1 -1
  96. package/lib/messages/primitives.d.ts +59 -4
  97. package/lib/messages/primitives.d.ts.map +1 -1
  98. package/lib/messages/primitives.js +97 -4
  99. package/lib/messages/primitives.js.map +1 -1
  100. package/lib/model-server.d.ts +60 -13
  101. package/lib/model-server.d.ts.map +1 -1
  102. package/lib/model-server.js +4 -2
  103. package/lib/model-server.js.map +1 -1
  104. package/lib/model-service/base-version.d.ts +67 -0
  105. package/lib/model-service/base-version.d.ts.map +1 -0
  106. package/lib/model-service/base-version.js +44 -0
  107. package/lib/model-service/base-version.js.map +1 -0
  108. package/lib/model-service/index.d.ts +1 -1
  109. package/lib/model-service/index.d.ts.map +1 -1
  110. package/lib/model-service/index.js +4 -5
  111. package/lib/model-service/index.js.map +1 -1
  112. package/lib/model-service/reference-candidate.d.ts +5 -3
  113. package/lib/model-service/reference-candidate.d.ts.map +1 -1
  114. package/lib/node/index.d.ts +10 -0
  115. package/lib/node/index.d.ts.map +1 -0
  116. package/lib/node/index.js +29 -0
  117. package/lib/node/index.js.map +1 -0
  118. package/lib/node/process-memory.d.ts +66 -0
  119. package/lib/node/process-memory.d.ts.map +1 -0
  120. package/lib/node/process-memory.js +291 -0
  121. package/lib/node/process-memory.js.map +1 -0
  122. package/lib/noop-logger.d.ts.map +1 -1
  123. package/lib/noop-logger.js.map +1 -1
  124. package/lib/observable-value.js.map +1 -1
  125. package/lib/patch-merge.d.ts +63 -35
  126. package/lib/patch-merge.d.ts.map +1 -1
  127. package/lib/patch-merge.js +171 -25
  128. package/lib/patch-merge.js.map +1 -1
  129. package/lib/pending-work-tracker.d.ts +29 -0
  130. package/lib/pending-work-tracker.d.ts.map +1 -0
  131. package/lib/pending-work-tracker.js +46 -0
  132. package/lib/pending-work-tracker.js.map +1 -0
  133. package/lib/profile-session.d.ts +8 -4
  134. package/lib/profile-session.d.ts.map +1 -1
  135. package/lib/profile-session.js.map +1 -1
  136. package/lib/project.d.ts +4 -1
  137. package/lib/project.d.ts.map +1 -1
  138. package/lib/project.js.map +1 -1
  139. package/lib/random-uuid.d.ts +14 -0
  140. package/lib/random-uuid.d.ts.map +1 -0
  141. package/lib/random-uuid.js +24 -0
  142. package/lib/random-uuid.js.map +1 -0
  143. package/lib/reconcile-write.d.ts +67 -0
  144. package/lib/reconcile-write.d.ts.map +1 -0
  145. package/lib/reconcile-write.js +69 -0
  146. package/lib/reconcile-write.js.map +1 -0
  147. package/lib/rpc/bind-rpc-methods.d.ts +6 -2
  148. package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
  149. package/lib/rpc/bind-rpc-methods.js +17 -7
  150. package/lib/rpc/bind-rpc-methods.js.map +1 -1
  151. package/lib/rpc/create-rpc-proxy.d.ts +3 -0
  152. package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
  153. package/lib/rpc/create-rpc-proxy.js +6 -1
  154. package/lib/rpc/create-rpc-proxy.js.map +1 -1
  155. package/lib/rpc/index.d.ts +1 -0
  156. package/lib/rpc/index.d.ts.map +1 -1
  157. package/lib/rpc/index.js +1 -0
  158. package/lib/rpc/index.js.map +1 -1
  159. package/lib/rpc/send-by-method-name.d.ts +76 -0
  160. package/lib/rpc/send-by-method-name.d.ts.map +1 -0
  161. package/lib/rpc/send-by-method-name.js +120 -0
  162. package/lib/rpc/send-by-method-name.js.map +1 -0
  163. package/lib/rpc/wire-prefix.js.map +1 -1
  164. package/lib/save-text-document.d.ts +40 -0
  165. package/lib/save-text-document.d.ts.map +1 -0
  166. package/lib/save-text-document.js +34 -0
  167. package/lib/save-text-document.js.map +1 -0
  168. package/lib/testing/catalogue-audit.d.ts +1 -1
  169. package/lib/testing/catalogue-audit.js +1 -1
  170. package/lib/testing/catalogue-audit.js.map +1 -1
  171. package/lib/testing/data-doubles.d.ts +41 -12
  172. package/lib/testing/data-doubles.d.ts.map +1 -1
  173. package/lib/testing/data-doubles.js +56 -8
  174. package/lib/testing/data-doubles.js.map +1 -1
  175. package/lib/testing/fake-clock.d.ts +17 -2
  176. package/lib/testing/fake-clock.d.ts.map +1 -1
  177. package/lib/testing/fake-clock.js +61 -46
  178. package/lib/testing/fake-clock.js.map +1 -1
  179. package/lib/testing/index.d.ts +1 -0
  180. package/lib/testing/index.d.ts.map +1 -1
  181. package/lib/testing/index.js +4 -3
  182. package/lib/testing/index.js.map +1 -1
  183. package/lib/testing/node/duplex-connection.d.ts.map +1 -1
  184. package/lib/testing/node/duplex-connection.js +3 -2
  185. package/lib/testing/node/duplex-connection.js.map +1 -1
  186. package/lib/testing/node/duplex-stream.js.map +1 -1
  187. package/lib/testing/node/index.d.ts +1 -0
  188. package/lib/testing/node/index.d.ts.map +1 -1
  189. package/lib/testing/node/index.js +2 -2
  190. package/lib/testing/node/index.js.map +1 -1
  191. package/lib/testing/node/message-port-pair.d.ts +25 -0
  192. package/lib/testing/node/message-port-pair.d.ts.map +1 -0
  193. package/lib/testing/node/message-port-pair.js +26 -0
  194. package/lib/testing/node/message-port-pair.js.map +1 -0
  195. package/lib/testing/transfer-node-builder.d.ts +81 -0
  196. package/lib/testing/transfer-node-builder.d.ts.map +1 -0
  197. package/lib/testing/transfer-node-builder.js +47 -0
  198. package/lib/testing/transfer-node-builder.js.map +1 -0
  199. package/lib/testing/wait-for.d.ts +9 -3
  200. package/lib/testing/wait-for.d.ts.map +1 -1
  201. package/lib/testing/wait-for.js +57 -11
  202. package/lib/testing/wait-for.js.map +1 -1
  203. package/lib/tracer.d.ts.map +1 -1
  204. package/lib/tracer.js +5 -9
  205. package/lib/tracer.js.map +1 -1
  206. package/lib/transfer-diagnostic.js.map +1 -1
  207. package/lib/transfer-document.d.ts +95 -32
  208. package/lib/transfer-document.d.ts.map +1 -1
  209. package/lib/transfer-document.js +17 -9
  210. package/lib/transfer-document.js.map +1 -1
  211. package/lib/transfer-element.d.ts +4 -4
  212. package/lib/types.d.ts +32 -0
  213. package/lib/types.d.ts.map +1 -0
  214. package/lib/{model-service/args.js → types.js} +2 -2
  215. package/lib/types.js.map +1 -0
  216. package/lib/uri.d.ts.map +1 -1
  217. package/lib/uri.js.map +1 -1
  218. package/lib/util.d.ts +8 -2
  219. package/lib/util.d.ts.map +1 -1
  220. package/lib/util.js +32 -4
  221. package/lib/util.js.map +1 -1
  222. package/package.json +24 -40
  223. package/src/abstract-logger.ts +8 -0
  224. package/src/client/data-connection.ts +653 -0
  225. package/src/client/data-events.ts +37 -1
  226. package/src/client/data-port.ts +36 -25
  227. package/src/client/data-session.ts +1469 -137
  228. package/src/client/index.ts +15 -9
  229. package/src/client/message-relay.ts +4 -4
  230. package/src/client/post-message-transport.ts +219 -4
  231. package/src/client/rpc-connection.ts +281 -0
  232. package/src/client/worker-handshake.ts +113 -0
  233. package/src/client-ids.ts +49 -0
  234. package/src/clock.ts +68 -10
  235. package/src/console-logger.ts +39 -0
  236. package/src/data/data-protocol-methods.ts +14 -4
  237. package/src/data/data-server-protocol.ts +197 -48
  238. package/src/data/events.ts +143 -21
  239. package/src/data/requests.ts +77 -11
  240. package/src/debouncer.ts +7 -10
  241. package/src/errors.ts +326 -40
  242. package/src/glsp-edits-settled-actions.ts +60 -0
  243. package/src/glsp-request-model-args.ts +16 -0
  244. package/src/glsp-save-model-actions.ts +59 -0
  245. package/src/index.ts +10 -0
  246. package/src/latency-collector.ts +8 -3
  247. package/src/logger.ts +44 -8
  248. package/src/messages/index.ts +4 -2
  249. package/src/messages/primitives.ts +129 -5
  250. package/src/model-server.ts +64 -19
  251. package/src/model-service/base-version.ts +75 -0
  252. package/src/model-service/index.ts +4 -5
  253. package/src/model-service/reference-candidate.ts +5 -3
  254. package/src/node/index.ts +14 -0
  255. package/src/node/process-memory.ts +299 -0
  256. package/src/patch-merge.ts +230 -45
  257. package/src/pending-work-tracker.ts +46 -0
  258. package/src/profile-session.ts +9 -4
  259. package/src/project.ts +4 -1
  260. package/src/random-uuid.ts +21 -0
  261. package/src/reconcile-write.ts +128 -0
  262. package/src/rpc/README.md +6 -6
  263. package/src/rpc/bind-rpc-methods.ts +18 -8
  264. package/src/rpc/create-rpc-proxy.ts +6 -1
  265. package/src/rpc/index.ts +1 -0
  266. package/src/rpc/send-by-method-name.ts +140 -0
  267. package/src/save-text-document.ts +39 -0
  268. package/src/testing/catalogue-audit.ts +1 -1
  269. package/src/testing/data-doubles.ts +141 -19
  270. package/src/testing/fake-clock.ts +76 -48
  271. package/src/testing/index.ts +4 -3
  272. package/src/testing/node/duplex-connection.ts +3 -2
  273. package/src/testing/node/index.ts +2 -2
  274. package/src/testing/node/message-port-pair.ts +40 -0
  275. package/src/testing/transfer-node-builder.ts +107 -0
  276. package/src/testing/wait-for.ts +61 -13
  277. package/src/tracer.ts +5 -9
  278. package/src/transfer-document.ts +115 -34
  279. package/src/transfer-element.ts +4 -4
  280. package/src/types.ts +30 -0
  281. package/src/util.ts +33 -6
  282. package/lib/model-service/args.d.ts +0 -64
  283. package/lib/model-service/args.d.ts.map +0 -1
  284. package/lib/model-service/args.js.map +0 -1
  285. package/src/model-service/args.ts +0 -67
@@ -33,13 +33,19 @@ export interface WaitForOptions {
33
33
  readonly timeoutMs?: number;
34
34
  /** Poll the predicate every this-many milliseconds. Default 5. */
35
35
  readonly intervalMs?: number;
36
- /** Error message on timeout. Default a generic phrasing. */
37
- readonly message?: string;
36
+ /**
37
+ * Error message on timeout, and when the predicate throws. A function is
38
+ * called only then, so it can report the state the wait ended on; when it
39
+ * throws, the default text is used, with its throw as the error's `cause`
40
+ * unless the predicate threw. Default a generic phrasing.
41
+ */
42
+ readonly message?: string | (() => string);
38
43
  }
39
44
 
40
45
  /**
41
46
  * Resolve once `predicate()` returns true, polling at `intervalMs`; reject with
42
- * `message` once `timeoutMs` elapses. The **non-racy** alternative to a fixed
47
+ * `message` once `timeoutMs` elapses, or at once when the predicate throws, with
48
+ * the throw as the error's `cause`. The **non-racy** alternative to a fixed
43
49
  * sleep for awaiting an asynchronous side effect that lands on an observable
44
50
  * value.
45
51
  *
@@ -56,20 +62,62 @@ export interface WaitForOptions {
56
62
  export function waitFor(predicate: () => boolean, options: WaitForOptions = {}): Promise<void> {
57
63
  const timeoutMs = options.timeoutMs ?? 2000;
58
64
  const intervalMs = options.intervalMs ?? 5;
59
- const message = options.message ?? `Timed out after ${timeoutMs}ms waiting for a condition`;
60
- if (predicate()) {
61
- return Promise.resolve();
62
- }
65
+ const defaultMessage = `Timed out after ${timeoutMs}ms waiting for a condition`;
63
66
  return new Promise<void>((resolve, reject) => {
64
- const deadline = setTimeout(() => {
67
+ // A throw from the predicate or the message function would leave the
68
+ // promise unsettled from the poll's or the deadline's timer, or escape
69
+ // `waitFor` itself on the first check; it rejects instead, with the
70
+ // predicate's throw, else the message function's, as cause. True once
71
+ // the promise is settled.
72
+ const settle = (timedOut: boolean): boolean => {
73
+ let met = false;
74
+ let threw = false;
75
+ let cause: unknown;
76
+ try {
77
+ met = predicate();
78
+ } catch (error: unknown) {
79
+ threw = true;
80
+ cause = error;
81
+ }
82
+ if (met) {
83
+ resolve();
84
+ return true;
85
+ }
86
+ if (!timedOut && !threw) {
87
+ return false;
88
+ }
89
+ let message = defaultMessage;
90
+ try {
91
+ if (typeof options.message === 'function') {
92
+ message = options.message();
93
+ } else if (options.message !== undefined) {
94
+ message = options.message;
95
+ }
96
+ } catch (error: unknown) {
97
+ if (!threw) {
98
+ threw = true;
99
+ cause = error;
100
+ }
101
+ }
102
+ reject(new Error(message, threw ? { cause } : undefined));
103
+ return true;
104
+ };
105
+ if (settle(false)) {
106
+ return;
107
+ }
108
+ const stop = (): void => {
65
109
  clearInterval(poll);
66
- reject(new Error(message));
110
+ clearTimeout(deadline);
111
+ };
112
+ // A stalled loop can run the deadline after the condition came true but
113
+ // before the next poll saw it, so the deadline checks once more.
114
+ const deadline = setTimeout(() => {
115
+ settle(true);
116
+ stop();
67
117
  }, timeoutMs);
68
118
  const poll = setInterval(() => {
69
- if (predicate()) {
70
- clearInterval(poll);
71
- clearTimeout(deadline);
72
- resolve();
119
+ if (settle(false)) {
120
+ stop();
73
121
  }
74
122
  }, intervalMs);
75
123
  });
package/src/tracer.ts CHANGED
@@ -12,6 +12,7 @@ import { Format, Logger, type LogThreshold } from './logger';
12
12
  import { NoopLogger } from './noop-logger';
13
13
  import { DefaultProfileSession, type ProfileSession } from './profile-session';
14
14
  import { Disposable } from './util';
15
+ import { describeErrorWithStack } from './messages/primitives';
15
16
 
16
17
  /** Module-global so nested timers don't reuse ids. */
17
18
  let nextTimerId = 0;
@@ -181,15 +182,10 @@ export class DefaultTracer implements Tracer {
181
182
  const suffix = this.timingSuffix(status, elapsed, options);
182
183
  this.logger[logLevel](`${label} [#${id} ${status}, ${Format.elapsed(elapsed)}${currentExtras()}${suffix}]`);
183
184
  if (status === 'failed') {
184
- // The timing line above sits at `logLevel`, which may be below the
185
- // active threshold — and on its own it never carries *why* the op
186
- // failed. Surface the error at warn so the failure is never silent,
187
- // with the stack at debug. Cancellations are normal control flow.
188
- const detail = error instanceof Error ? error.message : String(error);
189
- this.logger.warn(`${label} [#${id}] failed: ${detail}`);
190
- if (error instanceof Error && error.stack) {
191
- this.logger.debug(error.stack);
192
- }
185
+ // Reporting the failure is the caller's, who receives it: a line at
186
+ // warn here would exist only while `logLevel` is enabled.
187
+ const detail = describeErrorWithStack(error);
188
+ this.logger.debug(`${label} [#${id}] failed: ${detail}`);
193
189
  }
194
190
  };
195
191
  let result: T;
@@ -9,53 +9,123 @@
9
9
 
10
10
  import type { TransferDiagnostic } from './transfer-diagnostic';
11
11
  import type { TransferElement } from './transfer-element';
12
+ import { asModelVersion, type ModelVersion, type TextVersion } from './model-service/base-version';
13
+
14
+ /** The model a data server built for a document, encoded for the wire. */
15
+ export interface TransferModelSnapshot<TRoot extends TransferElement, TDiagnostic = TransferDiagnostic> {
16
+ root: TRoot;
17
+ /** Absent until the document is validated; `[]` once it is and nothing was found. */
18
+ diagnostics?: TDiagnostic[];
19
+ /**
20
+ * The version of the text `root` was parsed from. A caller that mutates the
21
+ * model sends this back as the `baseVersion` of its write, and the server
22
+ * rejects the write with `ConflictError` once its text has moved past it.
23
+ */
24
+ version: ModelVersion;
25
+ /**
26
+ * The data server's fingerprint of `root` and `diagnostics`, or under its
27
+ * `'text-diagnostics'` strategy of the text `root` was parsed from and
28
+ * `diagnostics`.
29
+ * Independent of `version`; an equal hash also means equally validated.
30
+ */
31
+ hash: string;
32
+ }
33
+
34
+ /** The text a data server held for a document when it sent it. */
35
+ export interface TextState {
36
+ /**
37
+ * The server's version of that text. A `TransferModelSnapshot.version` below it
38
+ * means the model was parsed from older text, and an update at this version
39
+ * or a later one follows.
40
+ */
41
+ version: TextVersion;
42
+ /**
43
+ * What `textHash` makes of the text alone: equal texts hash equal whatever
44
+ * their version or dirty state, across a revert or a server restart that
45
+ * numbers versions afresh.
46
+ */
47
+ hash: string;
48
+ /**
49
+ * Whether the text differs from the file, as the server last knew the file.
50
+ * A later change arrives as `onDocumentDirtyChanged` to a client watching
51
+ * the document.
52
+ */
53
+ dirty: boolean;
54
+ }
12
55
 
13
56
  /**
14
57
  * Wire envelope exchanged between the model-server's data-server head and
15
- * its clients. Carries a transfer-shaped root — never an AST root — plus
16
- * the document's diagnostics in the adopter's wire-diagnostic shape.
58
+ * its clients: the server's model of a document and the text it holds for it,
59
+ * each absent where the server has none.
17
60
  *
18
61
  * The constraint `TTransfer extends TransferElement` pins the type system
19
62
  * to the wire data shape (no `$container` cycles, references as strings;
20
- * see {@link TransferElement}). The paired server-internal envelope is
21
- * `AstDocument<TAst extends AstNode, TDiagnostic>` in
22
- * `@hydranium/core` — same structural shape, different generic
23
- * constraint, used at the AST layer before the `TransferEncoder`
24
- * translation produces a {@link TransferDocument}.
63
+ * see {@link TransferElement}).
25
64
  */
26
65
  export interface TransferDocument<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic> {
27
66
  uri: string;
28
67
  /**
29
- * The document version this snapshot was taken at — sourced from the
30
- * server's text-document version counter. Callers that mutate the
31
- * document subsequently pass this value back as `TransferUpdateArgs.baseVersion`
32
- * (or `TransferSaveArgs.baseVersion`) so the server can detect stale-based
33
- * updates and reject them with `ConflictError`.
34
- *
35
- * See `@hydranium/protocol#errors` for the conflict-detection contract.
68
+ * Absent when the document does not exist: the server answers an unknown
69
+ * URI with a shaped envelope rather than an error, so absence is an
70
+ * ordinary branch and not a catch.
36
71
  */
37
- version: number;
38
- /**
39
- * Absent when the document does not exist — the server answers an unknown URI
40
- * with a shaped envelope rather than an error, so absence is an ordinary
41
- * branch and not a catch. Declared required it reads as a guarantee, and
42
- * every `root.x` compiles into a throw.
43
- */
44
- root?: TTransfer;
45
- diagnostics: TDiagnostic[];
72
+ model?: TransferModelSnapshot<TTransfer, TDiagnostic>;
73
+ /** Absent when the server holds no text for the document. */
74
+ text?: TextState;
46
75
  }
47
76
 
48
77
  /**
49
- * A {@link TransferDocument} the server actually had, so `root` is present.
78
+ * A {@link TransferDocument} the server actually had, so `model` is present.
50
79
  * Narrowed to by {@link TransferDocument.isLoaded} / {@link TransferDocument.assertLoaded}.
51
80
  */
52
81
  export type LoadedTransferDocument<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic> = TransferDocument<
53
82
  TTransfer,
54
83
  TDiagnostic
55
84
  > & {
56
- root: TTransfer;
85
+ model: TransferModelSnapshot<TTransfer, TDiagnostic>;
57
86
  };
58
87
 
88
+ /**
89
+ * What a save or a persist answers: the document, and in `persisted` the
90
+ * version of the text it wrote to the file. That can differ from
91
+ * `model.version` either way, since writes land between the build and the
92
+ * take, so a client marking a version saved takes it from here.
93
+ */
94
+ export type TransferSavedDocument<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic> = TransferDocument<
95
+ TTransfer,
96
+ TDiagnostic
97
+ > & {
98
+ persisted: { version: TextVersion };
99
+ };
100
+
101
+ /**
102
+ * What a create answers: the document as a read after the create finds it,
103
+ * and in `created` the version the create's text took. Another client's write
104
+ * or an integrity repair can land between the create and that read, so
105
+ * `model.version` can be past it, and a write based on it would carry that
106
+ * text as the creator's; a write based on `created.version` conflicts instead.
107
+ * A model version, not a text version, because the creator holds the text at
108
+ * that version: it sent it.
109
+ */
110
+ export type TransferCreatedDocument<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic> = TransferDocument<
111
+ TTransfer,
112
+ TDiagnostic
113
+ > & {
114
+ created: { version: ModelVersion };
115
+ };
116
+
117
+ /** What a watch answers, once it is in place. */
118
+ export interface TransferWatchedDocument {
119
+ /** The key the watch's events name the document by, which a symlinked URI does not spell. */
120
+ readonly uri: string;
121
+ /** Whether a document exists there; one the server fails to load counts, only a missing one does not. */
122
+ readonly exists: boolean;
123
+ /** The position of the last update or deletion event sent before `exists` was taken; an event past it was sent after. */
124
+ readonly sequence: number;
125
+ /** The text the server held when `exists` was taken, as a read answers it; absent when it held none. */
126
+ readonly text?: TextState;
127
+ }
128
+
59
129
  export namespace TransferDocument {
60
130
  /**
61
131
  * Whether the server had this document. Branch on it where absence is an
@@ -64,13 +134,13 @@ export namespace TransferDocument {
64
134
  export function isLoaded<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic>(
65
135
  document: TransferDocument<TTransfer, TDiagnostic>
66
136
  ): document is LoadedTransferDocument<TTransfer, TDiagnostic> {
67
- return document.root !== undefined;
137
+ return document.model !== undefined;
68
138
  }
69
139
 
70
140
  /**
71
- * The same document with `root` no longer optional, or a throw. For call
141
+ * The same document with `model` no longer optional, or a throw. For call
72
142
  * sites where absence is a bug rather than a branch; the whole envelope is
73
- * returned because a caller needing `root` usually needs `uri` or `version`
143
+ * returned because a caller needing `model` usually needs `uri` or `text`
74
144
  * with it.
75
145
  */
76
146
  export function assertLoaded<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic>(
@@ -82,18 +152,29 @@ export namespace TransferDocument {
82
152
  return document;
83
153
  }
84
154
 
155
+ /** The envelope for a document the server does not have. */
156
+ export function absent<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic>(
157
+ uri: string
158
+ ): TransferDocument<TTransfer, TDiagnostic> {
159
+ return { uri };
160
+ }
161
+
85
162
  /**
86
- * Construct a {@link TransferDocument} envelope. `diagnostics` defaults
87
- * to `[]` so test fixtures, fake protocol implementations, and the
88
- * "no diagnostics yet" code paths don't need to repeat the empty array
89
- * at every call site.
163
+ * Construct a {@link TransferDocument} envelope with a model, its version
164
+ * marked as coming from a read. `diagnostics` absent means not validated.
90
165
  */
91
166
  export function create<TTransfer extends TransferElement, TDiagnostic = TransferDiagnostic>(
92
167
  uri: string,
93
168
  version: number,
94
169
  root: TTransfer,
95
- diagnostics: TDiagnostic[] = []
170
+ hash: string,
171
+ diagnostics?: TDiagnostic[],
172
+ text?: TextState
96
173
  ): TransferDocument<TTransfer, TDiagnostic> {
97
- return { uri, version, root, diagnostics };
174
+ return {
175
+ uri,
176
+ model: { root, version: asModelVersion(version), hash, ...(diagnostics === undefined ? {} : { diagnostics }) },
177
+ ...(text === undefined ? {} : { text })
178
+ };
98
179
  }
99
180
  }
@@ -29,10 +29,10 @@ export interface TransferElement {
29
29
  * declared wire shape — or fall back to the structural base
30
30
  * {@link TransferElement} when no entry is declared.
31
31
  *
32
- * Adopters declare the overlay by interface merging or by generating an
33
- * interface keyed on those `$type` strings. Types not in the overlay resolve to
34
- * `TransferElement` — the framework default behaves correctly without an
35
- * overlay (no autocomplete, but no compile error either).
32
+ * The transfer-model generator emits the overlay as `<LanguageId>TransferTypes`;
33
+ * an encoder that relabels a type overrides that entry. Types not in the
34
+ * overlay resolve to `TransferElement` — the framework default behaves
35
+ * correctly without an overlay (no autocomplete, but no compile error either).
36
36
  *
37
37
  * The lookup is compile-time only; no runtime cost.
38
38
  */
package/src/types.ts ADDED
@@ -0,0 +1,30 @@
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
+ /**
11
+ * The keys of a type map, Langium's `<LanguageName>AstType` or the generated
12
+ * `<LanguageId>TransferTypes`, that a node can carry as its `$type`: those
13
+ * listed in their own type's `$type`. A union, or an abstract type whose
14
+ * `$type` names only its subtypes, has a constant too, but building one would
15
+ * yield a node that no guard accepts. A concrete type with subtypes lists its
16
+ * own name beside theirs, as Langium writes it, and stays buildable.
17
+ */
18
+ export type ConcreteTypeName<TMap> = {
19
+ [K in keyof TMap]: K extends (TMap[K] extends { readonly $type: infer TType } ? TType : never) ? K : never;
20
+ }[keyof TMap] &
21
+ string;
22
+
23
+ type IsUnion<T, TAll = T> = T extends unknown ? ([TAll] extends [T] ? false : true) : never;
24
+
25
+ /**
26
+ * Intersected onto a builder's type-constant parameter, refuses a constant
27
+ * typed by a union of type names: the init would hold only the members' common
28
+ * fields, so either type could be built incomplete.
29
+ */
30
+ export type SingleTypeName<TType> = true extends IsUnion<TType> ? { readonly $type: never } : unknown;
package/src/util.ts CHANGED
@@ -146,12 +146,6 @@ export function asMutable<T>(item: T): Mutable<T> {
146
146
  return item;
147
147
  }
148
148
 
149
- /** Type-guard variant: narrows `item` to `Mutable<T> | undefined` if `guard` accepts it. */
150
- export function toMutable<T>(item: unknown, guard: TypeGuard<T>): item is Mutable<T> | undefined;
151
- export function toMutable<T>(item: unknown, guard?: TypeGuard<T>): item is Mutable<T> | undefined {
152
- return guard ? guard(item) : true;
153
- }
154
-
155
149
  /**
156
150
  * Wrap `text` in `quoteChar`, escaping interior occurrences with `replaceChar`.
157
151
  *
@@ -248,3 +242,36 @@ export function getAt<T = unknown>(obj: unknown, path: string[]): T | undefined
248
242
  }
249
243
  return cur as T;
250
244
  }
245
+
246
+ /**
247
+ * A stable, non-cryptographic digest of `text`, equal on every host for equal
248
+ * text: the `TextState.hash` a data server stamps, so a client can
249
+ * hash text of its own to compare. An array is read as its elements joined, so
250
+ * a large text is hashed in parts without concatenating it. It tells whether a
251
+ * text changed, not who could forge one.
252
+ */
253
+ export function textHash(text: string | readonly string[]): string {
254
+ const { high, low } = cyrb53(typeof text === 'string' ? [text] : text);
255
+ return high.toString(16).padStart(8, '0') + low.toString(16).padStart(8, '0');
256
+ }
257
+
258
+ /**
259
+ * The two 32-bit words cyrb53 ends on, each unsigned, for `parts` read as one
260
+ * string. It needs no `node:*` module, so every host runs it.
261
+ */
262
+ function cyrb53(parts: readonly string[]): { readonly high: number; readonly low: number } {
263
+ let h1 = 0xdeadbeef;
264
+ let h2 = 0x41c6ce57;
265
+ for (const part of parts) {
266
+ for (let i = 0; i < part.length; i++) {
267
+ const code = part.charCodeAt(i);
268
+ h1 = Math.imul(h1 ^ code, 2654435761);
269
+ h2 = Math.imul(h2 ^ code, 1597334677);
270
+ }
271
+ }
272
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
273
+ h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
274
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
275
+ h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
276
+ return { high: h2 >>> 0, low: h1 >>> 0 };
277
+ }
@@ -1,64 +0,0 @@
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
- * Argument shapes for the in-process `ModelService` facade — the
11
- * canonical home for the lifecycle operation signatures. Implementation
12
- * lives in `@hydranium/core/langium/model-service`; this protocol
13
- * package owns the types so wire-side projections (the data-server
14
- * protocol in `./data`) can structurally extend them rather than
15
- * mirroring them by hand.
16
- *
17
- * `./model-server.ts` re-exports them, so an import from either path
18
- * resolves.
19
- */
20
- /** Identifies a client-document binding. Every facade operation carries these fields. */
21
- export interface TransferClientArgs {
22
- /** Document URI. */
23
- uri: string;
24
- /** Stable identifier for the client invoking the operation. */
25
- clientId: string;
26
- }
27
- /**
28
- * Update a document's content via the in-process facade. `model` may be
29
- * the structured (transfer-model or AST) root or its serialised textual
30
- * form — implementations decide which is faster on their transport.
31
- * Generic over `T` so adopters parameterise the structured shape against
32
- * their grammar's transfer-model overlay; passing a string is always
33
- * allowed.
34
- */
35
- export interface TransferUpdateArgs<T> extends TransferClientArgs {
36
- /** Structured model root or its serialised textual form. */
37
- model: T | string;
38
- /**
39
- * Optional based-on version: the text-document version this update
40
- * was authored against. When set, the server compares against its
41
- * current text-document version for `uri` and throws
42
- * `ConflictError` on mismatch. Omit to opt out of the gate — mirrors
43
- * LSP's `OptionalVersionedTextDocumentIdentifier` posture, intended
44
- * for headless / CLI / batch tooling without a meaningful based-on
45
- * version.
46
- */
47
- baseVersion?: number;
48
- }
49
- /**
50
- * Persist a document to disk via the in-process facade. Same `model`
51
- * shape as {@link TransferUpdateArgs}.
52
- */
53
- export interface TransferSaveArgs<T> extends TransferClientArgs {
54
- /** Structured model root or its serialised textual form. */
55
- model: T | string;
56
- /**
57
- * Optional based-on version — same semantics as
58
- * {@link TransferUpdateArgs.baseVersion}. `save` delegates the gating
59
- * check to its inner `update`, so this field threads through to the
60
- * same `ConflictError` site.
61
- */
62
- baseVersion?: number;
63
- }
64
- //# sourceMappingURL=args.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"args.d.ts","sourceRoot":"","sources":["../../src/model-service/args.ts"],"names":[],"mappings":"AAAA;;;;;;;kFAOkF;AAElF;;;;;;;;;;GAUG;AAEH,yFAAyF;AACzF,MAAM,WAAW,kBAAkB;IAChC,oBAAoB;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,+DAA+D;IAC/D,QAAQ,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,kBAAkB,CAAC,CAAC,CAAE,SAAQ,kBAAkB;IAC9D,4DAA4D;IAC5D,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC;IAClB;;;;;;;;OAQG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,CAAE,SAAQ,kBAAkB;IAC5D,4DAA4D;IAC5D,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC;IAClB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACvB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"args.js","sourceRoot":"","sources":["../../src/model-service/args.ts"],"names":[],"mappings":";AAAA;;;;;;;kFAOkF"}
@@ -1,67 +0,0 @@
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
- * Argument shapes for the in-process `ModelService` facade — the
12
- * canonical home for the lifecycle operation signatures. Implementation
13
- * lives in `@hydranium/core/langium/model-service`; this protocol
14
- * package owns the types so wire-side projections (the data-server
15
- * protocol in `./data`) can structurally extend them rather than
16
- * mirroring them by hand.
17
- *
18
- * `./model-server.ts` re-exports them, so an import from either path
19
- * resolves.
20
- */
21
-
22
- /** Identifies a client-document binding. Every facade operation carries these fields. */
23
- export interface TransferClientArgs {
24
- /** Document URI. */
25
- uri: string;
26
- /** Stable identifier for the client invoking the operation. */
27
- clientId: string;
28
- }
29
-
30
- /**
31
- * Update a document's content via the in-process facade. `model` may be
32
- * the structured (transfer-model or AST) root or its serialised textual
33
- * form — implementations decide which is faster on their transport.
34
- * Generic over `T` so adopters parameterise the structured shape against
35
- * their grammar's transfer-model overlay; passing a string is always
36
- * allowed.
37
- */
38
- export interface TransferUpdateArgs<T> extends TransferClientArgs {
39
- /** Structured model root or its serialised textual form. */
40
- model: T | string;
41
- /**
42
- * Optional based-on version: the text-document version this update
43
- * was authored against. When set, the server compares against its
44
- * current text-document version for `uri` and throws
45
- * `ConflictError` on mismatch. Omit to opt out of the gate — mirrors
46
- * LSP's `OptionalVersionedTextDocumentIdentifier` posture, intended
47
- * for headless / CLI / batch tooling without a meaningful based-on
48
- * version.
49
- */
50
- baseVersion?: number;
51
- }
52
-
53
- /**
54
- * Persist a document to disk via the in-process facade. Same `model`
55
- * shape as {@link TransferUpdateArgs}.
56
- */
57
- export interface TransferSaveArgs<T> extends TransferClientArgs {
58
- /** Structured model root or its serialised textual form. */
59
- model: T | string;
60
- /**
61
- * Optional based-on version — same semantics as
62
- * {@link TransferUpdateArgs.baseVersion}. `save` delegates the gating
63
- * check to its inner `update`, so this field threads through to the
64
- * same `ConflictError` site.
65
- */
66
- baseVersion?: number;
67
- }