@hydranium/protocol 1.0.0-next.23 → 1.0.0-next.230
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -3
- package/lib/abstract-logger.d.ts +5 -0
- package/lib/abstract-logger.d.ts.map +1 -1
- package/lib/abstract-logger.js +7 -0
- package/lib/abstract-logger.js.map +1 -1
- package/lib/client/data-connection.d.ts +236 -0
- package/lib/client/data-connection.d.ts.map +1 -0
- package/lib/client/data-connection.js +404 -0
- package/lib/client/data-connection.js.map +1 -0
- package/lib/client/data-events.d.ts +13 -1
- package/lib/client/data-events.d.ts.map +1 -1
- package/lib/client/data-events.js +21 -0
- package/lib/client/data-events.js.map +1 -1
- package/lib/client/data-port.d.ts +27 -22
- package/lib/client/data-port.d.ts.map +1 -1
- package/lib/client/data-session.d.ts +473 -81
- package/lib/client/data-session.d.ts.map +1 -1
- package/lib/client/data-session.js +743 -108
- package/lib/client/data-session.js.map +1 -1
- package/lib/client/index.d.ts +14 -9
- package/lib/client/index.d.ts.map +1 -1
- package/lib/client/index.js +14 -9
- package/lib/client/index.js.map +1 -1
- package/lib/client/message-relay.d.ts +9 -3
- package/lib/client/message-relay.d.ts.map +1 -1
- package/lib/client/message-relay.js +11 -5
- package/lib/client/message-relay.js.map +1 -1
- package/lib/client/post-message-transport.d.ts +64 -3
- package/lib/client/post-message-transport.d.ts.map +1 -1
- package/lib/client/post-message-transport.js +175 -1
- package/lib/client/post-message-transport.js.map +1 -1
- package/lib/client/rpc-connection.d.ts +157 -0
- package/lib/client/rpc-connection.d.ts.map +1 -0
- package/lib/client/rpc-connection.js +214 -0
- package/lib/client/rpc-connection.js.map +1 -0
- package/lib/client-ids.d.ts +45 -0
- package/lib/client-ids.d.ts.map +1 -0
- package/lib/client-ids.js +48 -0
- package/lib/client-ids.js.map +1 -0
- package/lib/clock.d.ts +38 -0
- package/lib/clock.d.ts.map +1 -1
- package/lib/clock.js +36 -1
- package/lib/clock.js.map +1 -1
- package/lib/console-logger.d.ts +23 -0
- package/lib/console-logger.d.ts.map +1 -0
- package/lib/console-logger.js +39 -0
- package/lib/console-logger.js.map +1 -0
- package/lib/data/data-protocol-methods.d.ts +4 -4
- package/lib/data/data-protocol-methods.d.ts.map +1 -1
- package/lib/data/data-protocol-methods.js +12 -1
- package/lib/data/data-protocol-methods.js.map +1 -1
- package/lib/data/data-server-protocol.d.ts +132 -41
- package/lib/data/data-server-protocol.d.ts.map +1 -1
- package/lib/data/events.d.ts +117 -21
- package/lib/data/events.d.ts.map +1 -1
- package/lib/data/requests.d.ts +69 -11
- package/lib/data/requests.d.ts.map +1 -1
- package/lib/debouncer.d.ts.map +1 -1
- package/lib/debouncer.js.map +1 -1
- package/lib/errors.d.ts +187 -29
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +270 -29
- package/lib/errors.js.map +1 -1
- package/lib/glsp-request-model-args.d.ts +16 -0
- package/lib/glsp-request-model-args.d.ts.map +1 -0
- package/lib/glsp-request-model-args.js +19 -0
- package/lib/glsp-request-model-args.js.map +1 -0
- package/lib/glsp-save-model-actions.d.ts +50 -0
- package/lib/glsp-save-model-actions.d.ts.map +1 -0
- package/lib/glsp-save-model-actions.js +28 -0
- package/lib/glsp-save-model-actions.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +10 -0
- package/lib/index.js.map +1 -1
- package/lib/latency-collector.d.ts +8 -4
- package/lib/latency-collector.d.ts.map +1 -1
- package/lib/latency-collector.js.map +1 -1
- package/lib/logger.d.ts +22 -1
- package/lib/logger.d.ts.map +1 -1
- package/lib/logger.js +31 -3
- package/lib/logger.js.map +1 -1
- package/lib/messages/index.d.ts +30 -0
- package/lib/messages/index.d.ts.map +1 -0
- package/lib/messages/index.js +62 -0
- package/lib/messages/index.js.map +1 -0
- package/lib/messages/primitives.d.ts +188 -0
- package/lib/messages/primitives.d.ts.map +1 -0
- package/lib/messages/primitives.js +161 -0
- package/lib/messages/primitives.js.map +1 -0
- package/lib/model-server.d.ts +60 -13
- package/lib/model-server.d.ts.map +1 -1
- package/lib/model-server.js +4 -2
- package/lib/model-server.js.map +1 -1
- package/lib/model-service/base-version.d.ts +64 -0
- package/lib/model-service/base-version.d.ts.map +1 -0
- package/lib/model-service/base-version.js +43 -0
- package/lib/model-service/base-version.js.map +1 -0
- package/lib/model-service/index.d.ts +1 -1
- package/lib/model-service/index.d.ts.map +1 -1
- package/lib/model-service/index.js +4 -5
- package/lib/model-service/index.js.map +1 -1
- package/lib/model-service/reference-candidate.d.ts +5 -3
- package/lib/model-service/reference-candidate.d.ts.map +1 -1
- package/lib/{model-service/args.js → node/index.d.ts} +2 -3
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +29 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/process-memory.d.ts +66 -0
- package/lib/node/process-memory.d.ts.map +1 -0
- package/lib/node/process-memory.js +291 -0
- package/lib/node/process-memory.js.map +1 -0
- package/lib/noop-logger.d.ts.map +1 -1
- package/lib/noop-logger.js.map +1 -1
- package/lib/observable-value.js.map +1 -1
- package/lib/patch-merge.d.ts +35 -32
- package/lib/patch-merge.d.ts.map +1 -1
- package/lib/patch-merge.js +67 -23
- package/lib/patch-merge.js.map +1 -1
- package/lib/profile-session.d.ts +8 -4
- package/lib/profile-session.d.ts.map +1 -1
- package/lib/profile-session.js.map +1 -1
- package/lib/random-uuid.d.ts +14 -0
- package/lib/random-uuid.d.ts.map +1 -0
- package/lib/random-uuid.js +24 -0
- package/lib/random-uuid.js.map +1 -0
- package/lib/reconcile-write.d.ts +65 -0
- package/lib/reconcile-write.d.ts.map +1 -0
- package/lib/reconcile-write.js +67 -0
- package/lib/reconcile-write.js.map +1 -0
- package/lib/rpc/bind-rpc-methods.d.ts +33 -3
- package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
- package/lib/rpc/bind-rpc-methods.js +32 -3
- package/lib/rpc/bind-rpc-methods.js.map +1 -1
- package/lib/rpc/create-rpc-proxy.d.ts +10 -0
- package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
- package/lib/rpc/create-rpc-proxy.js +12 -2
- package/lib/rpc/create-rpc-proxy.js.map +1 -1
- package/lib/rpc/index.d.ts +1 -0
- package/lib/rpc/index.d.ts.map +1 -1
- package/lib/rpc/index.js +1 -0
- package/lib/rpc/index.js.map +1 -1
- package/lib/rpc/send-by-method-name.d.ts +76 -0
- package/lib/rpc/send-by-method-name.d.ts.map +1 -0
- package/lib/rpc/send-by-method-name.js +120 -0
- package/lib/rpc/send-by-method-name.js.map +1 -0
- package/lib/rpc/wire-prefix.js.map +1 -1
- package/lib/testing/catalogue-audit.d.ts +80 -0
- package/lib/testing/catalogue-audit.d.ts.map +1 -0
- package/lib/testing/catalogue-audit.js +94 -0
- package/lib/testing/catalogue-audit.js.map +1 -0
- package/lib/testing/data-doubles.d.ts +42 -15
- package/lib/testing/data-doubles.d.ts.map +1 -1
- package/lib/testing/data-doubles.js +58 -10
- package/lib/testing/data-doubles.js.map +1 -1
- package/lib/testing/fake-clock.d.ts +9 -1
- package/lib/testing/fake-clock.d.ts.map +1 -1
- package/lib/testing/fake-clock.js +54 -45
- package/lib/testing/fake-clock.js.map +1 -1
- package/lib/testing/index.d.ts +1 -0
- package/lib/testing/index.d.ts.map +1 -1
- package/lib/testing/index.js +5 -2
- package/lib/testing/index.js.map +1 -1
- package/lib/testing/node/duplex-connection.d.ts.map +1 -1
- package/lib/testing/node/duplex-connection.js +3 -2
- package/lib/testing/node/duplex-connection.js.map +1 -1
- package/lib/testing/node/duplex-stream.js.map +1 -1
- package/lib/testing/node/index.d.ts +1 -0
- package/lib/testing/node/index.d.ts.map +1 -1
- package/lib/testing/node/index.js +2 -2
- package/lib/testing/node/index.js.map +1 -1
- package/lib/testing/node/message-port-pair.d.ts +25 -0
- package/lib/testing/node/message-port-pair.d.ts.map +1 -0
- package/lib/testing/node/message-port-pair.js +26 -0
- package/lib/testing/node/message-port-pair.js.map +1 -0
- package/lib/testing/wait-for.js.map +1 -1
- package/lib/tracer.d.ts.map +1 -1
- package/lib/tracer.js.map +1 -1
- package/lib/transfer-diagnostic.d.ts +33 -0
- package/lib/transfer-diagnostic.d.ts.map +1 -1
- package/lib/transfer-diagnostic.js +23 -0
- package/lib/transfer-diagnostic.js.map +1 -1
- package/lib/transfer-document.d.ts +70 -32
- package/lib/transfer-document.d.ts.map +1 -1
- package/lib/transfer-document.js +17 -9
- package/lib/transfer-document.js.map +1 -1
- package/lib/uri.d.ts.map +1 -1
- package/lib/uri.js.map +1 -1
- package/lib/util.d.ts +8 -0
- package/lib/util.d.ts.map +1 -1
- package/lib/util.js +32 -0
- package/lib/util.js.map +1 -1
- package/package.json +29 -37
- package/src/abstract-logger.ts +8 -0
- package/src/client/data-connection.ts +502 -0
- package/src/client/data-events.ts +33 -1
- package/src/client/data-port.ts +29 -23
- package/src/client/data-session.ts +951 -126
- package/src/client/index.ts +14 -9
- package/src/client/message-relay.ts +29 -7
- package/src/client/post-message-transport.ts +219 -4
- package/src/client/rpc-connection.ts +281 -0
- package/src/client-ids.ts +49 -0
- package/src/clock.ts +56 -0
- package/src/console-logger.ts +39 -0
- package/src/data/data-protocol-methods.ts +13 -4
- package/src/data/data-server-protocol.ts +157 -41
- package/src/data/events.ts +123 -21
- package/src/data/requests.ts +74 -11
- package/src/errors.ts +322 -36
- package/src/glsp-request-model-args.ts +16 -0
- package/src/glsp-save-model-actions.ts +59 -0
- package/src/index.ts +10 -0
- package/src/latency-collector.ts +8 -3
- package/src/logger.ts +28 -2
- package/src/messages/index.ts +37 -0
- package/src/messages/primitives.ts +271 -0
- package/src/model-server.ts +63 -18
- package/src/model-service/base-version.ts +72 -0
- package/src/model-service/index.ts +4 -5
- package/src/model-service/reference-candidate.ts +5 -3
- package/src/node/index.ts +14 -0
- package/src/node/process-memory.ts +299 -0
- package/src/patch-merge.ts +97 -42
- package/src/profile-session.ts +9 -4
- package/src/random-uuid.ts +21 -0
- package/src/reconcile-write.ts +124 -0
- package/src/rpc/README.md +4 -5
- package/src/rpc/bind-rpc-methods.ts +59 -4
- package/src/rpc/create-rpc-proxy.ts +20 -2
- package/src/rpc/index.ts +1 -0
- package/src/rpc/send-by-method-name.ts +140 -0
- package/src/testing/catalogue-audit.ts +111 -0
- package/src/testing/data-doubles.ts +145 -25
- package/src/testing/fake-clock.ts +62 -47
- package/src/testing/index.ts +5 -2
- package/src/testing/node/duplex-connection.ts +3 -2
- package/src/testing/node/index.ts +2 -2
- package/src/testing/node/message-port-pair.ts +40 -0
- package/src/transfer-diagnostic.ts +40 -0
- package/src/transfer-document.ts +87 -34
- package/src/util.ts +33 -0
- package/lib/model-service/args.d.ts +0 -64
- package/lib/model-service/args.d.ts.map +0 -1
- package/lib/model-service/args.js.map +0 -1
- package/src/model-service/args.ts +0 -67
|
@@ -0,0 +1,271 @@
|
|
|
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 { ResponseError } from 'vscode-jsonrpc';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The framework externalizes user-facing strings and SELECTS no locale: it
|
|
14
|
+
* relays the one its client declared and renders with whatever templates the
|
|
15
|
+
* adopter installed, defaulting to its English. Every such string carries a
|
|
16
|
+
* stable code beside that English, and exactly one side renders it — the side
|
|
17
|
+
* that knows the reading user's language.
|
|
18
|
+
*
|
|
19
|
+
* Which side that is depends on the message, not on the package. A server
|
|
20
|
+
* message is rendered by the server, at the one seam every carrier passes
|
|
21
|
+
* through, in the locale it was handed at init. A message the client tier raises
|
|
22
|
+
* is rendered there, because those fire when the server is unreachable. Nothing
|
|
23
|
+
* is rendered twice: two renders of one sentence are two authorities over it,
|
|
24
|
+
* and they diverge on the first reword.
|
|
25
|
+
*
|
|
26
|
+
* Codes are `hydranium/<unscoped-package>/<name>`. The package segment locates
|
|
27
|
+
* the declaration, so a message is declared in the package that raises it and a
|
|
28
|
+
* code never names a package it does not live in. `.` and `:` are forbidden in a
|
|
29
|
+
* segment: they are i18next's default key and namespace separators, where either
|
|
30
|
+
* silently becomes a nested lookup that misses.
|
|
31
|
+
*/
|
|
32
|
+
export type MessageParams = Readonly<Record<string, string | number>>;
|
|
33
|
+
|
|
34
|
+
type Placeholder<S extends string> = S extends `${string}{${infer Name}}${infer Rest}` ? Name | Placeholder<Rest> : never;
|
|
35
|
+
|
|
36
|
+
export type ParamsOf<S extends string> = [Placeholder<S>] extends [never]
|
|
37
|
+
? Record<never, never>
|
|
38
|
+
: Readonly<Record<Placeholder<S>, string | number>>;
|
|
39
|
+
|
|
40
|
+
/** Required exactly when the text has placeholders, absent when it does not. */
|
|
41
|
+
export type ParamsArg<S extends string> = [Placeholder<S>] extends [never] ? [] : [params: ParamsOf<S>];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Rejects a text argument already widened to `string`. Load-bearing rather than
|
|
45
|
+
* defensive: the whole compile-time guarantee is conditional on `S` inferring a
|
|
46
|
+
* literal, and for a concatenated or pre-widened text the placeholder set
|
|
47
|
+
* silently becomes empty, `format()` accepts no arguments, and the missing
|
|
48
|
+
* substitution surfaces only at runtime.
|
|
49
|
+
*/
|
|
50
|
+
export type LiteralText<S extends string> = string extends S ? never : S;
|
|
51
|
+
|
|
52
|
+
export interface MessageDefinition<S extends string> {
|
|
53
|
+
readonly code: string;
|
|
54
|
+
readonly text: S;
|
|
55
|
+
format(...args: ParamsArg<S>): string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Declare a message. Placeholder names are inferred from `text` rather than
|
|
60
|
+
* declared again in a type argument: a second spelling of every name is the
|
|
61
|
+
* repetition that drifts, since adding a placeholder to the sentence and not to
|
|
62
|
+
* the type compiles.
|
|
63
|
+
*/
|
|
64
|
+
export function defineMessage<S extends string>(code: string, text: LiteralText<S>): MessageDefinition<S> {
|
|
65
|
+
const literal = text as S;
|
|
66
|
+
return { code, text: literal, format: (...args) => interpolate(literal, args[0] ?? {}) };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const PLACEHOLDER = /\{([^}]+)\}/g;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Substitute `{name}` tokens, leaving an unfilled token in place.
|
|
73
|
+
*
|
|
74
|
+
* It must not throw. This runs over an adopter's translation as well as our own
|
|
75
|
+
* text, so a typo in a foreign catalogue has to degrade to a slightly wrong
|
|
76
|
+
* sentence rather than raise inside a toast render. Re-scanning the result to
|
|
77
|
+
* detect an unfilled token is what an earlier form did, and it cannot work:
|
|
78
|
+
* `String.replace` does not rescan replacement text, so the check could not tell
|
|
79
|
+
* an unfilled placeholder from user data shaped like one — an element literally
|
|
80
|
+
* named `{separator}` crashed at the authoring site.
|
|
81
|
+
*/
|
|
82
|
+
export function interpolate(template: string, params: MessageParams): string {
|
|
83
|
+
// Indexed rather than `key in params`: a hand-built or version-skewed
|
|
84
|
+
// identity can arrive with no params at all, and `in` throws on a non-object
|
|
85
|
+
// where a lookup degrades.
|
|
86
|
+
const lookup = params as Record<string, string | number | undefined> | undefined;
|
|
87
|
+
return template.replace(PLACEHOLDER, (match, key: string) => {
|
|
88
|
+
const value = lookup?.[key];
|
|
89
|
+
return value === undefined ? match : String(value);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface MessageIdentity {
|
|
94
|
+
readonly code: string;
|
|
95
|
+
readonly params: MessageParams;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Envelope for a protocol `data` field. Namespaced under one key so it co-exists
|
|
100
|
+
* with a carrier's own `data` conventions rather than occupying `data` itself.
|
|
101
|
+
*/
|
|
102
|
+
export interface HydraniumMessageData {
|
|
103
|
+
readonly hydranium: MessageIdentity;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** An identity plus its resolved English — everything a renderer needs, on any carrier. */
|
|
107
|
+
export interface ResolvedMessage extends MessageIdentity {
|
|
108
|
+
readonly text: string;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function messageData<S extends string>(message: MessageDefinition<S>, ...args: ParamsArg<S>): HydraniumMessageData {
|
|
112
|
+
return { hydranium: { code: message.code, params: args[0] ?? {} } };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Identity plus resolved English, for a hand-off carrying a value rather than a
|
|
117
|
+
* protocol field. The result is structured-clone safe, so it survives a process
|
|
118
|
+
* hop where one intervenes and costs nothing where none does.
|
|
119
|
+
*/
|
|
120
|
+
export function resolve<S extends string>(message: MessageDefinition<S>, ...args: ParamsArg<S>): ResolvedMessage {
|
|
121
|
+
return { code: message.code, text: message.format(...args), params: args[0] ?? {} };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Validates every field {@link MessageIdentity} declares, `params` included.
|
|
126
|
+
* A guard over foreign input that checks only `code` while declaring `params`
|
|
127
|
+
* non-optional hands `undefined` to the renderer, which fails with the worst
|
|
128
|
+
* polarity available: invisible in English, crashing only once a translation is
|
|
129
|
+
* loaded.
|
|
130
|
+
*/
|
|
131
|
+
export function hasMessageIdentity(data: unknown): data is HydraniumMessageData {
|
|
132
|
+
if (typeof data !== 'object' || data === null || Array.isArray(data) || !('hydranium' in data)) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
const identity = (data as { hydranium: unknown }).hydranium;
|
|
136
|
+
if (typeof identity !== 'object' || identity === null || Array.isArray(identity)) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
const candidate = identity as Partial<MessageIdentity>;
|
|
140
|
+
return typeof candidate.code === 'string' && typeof candidate.params === 'object' && candidate.params !== null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A type alias rather than a subclass. Only `code`, `message` and `data` cross
|
|
145
|
+
* the wire, so a subclass buys nothing there: `instanceof` does not survive
|
|
146
|
+
* reconstruction, and the subclass costs an `Object.setPrototypeOf` in every
|
|
147
|
+
* constructor purely to undo what `ResponseError`'s own constructor does.
|
|
148
|
+
*/
|
|
149
|
+
export type HydraniumResponseError = ResponseError<HydraniumMessageData>;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The numeric `code` and the message's catalogue code are unrelated and both are
|
|
153
|
+
* needed: `ResponseError.code` is an `integer`, so it cannot hold a
|
|
154
|
+
* `hydranium/…` key, and it is what a caller switches on after reconstruction.
|
|
155
|
+
*/
|
|
156
|
+
export function messageError<S extends string>(
|
|
157
|
+
code: number,
|
|
158
|
+
message: MessageDefinition<S>,
|
|
159
|
+
...params: ParamsArg<S>
|
|
160
|
+
): HydraniumResponseError {
|
|
161
|
+
return new ResponseError(code, message.format(...params), messageData(message, ...params));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The reading user's language, as the tag a client declared.
|
|
166
|
+
*
|
|
167
|
+
* Neither validated nor normalised anywhere: rejecting an unfamiliar tag would
|
|
168
|
+
* be selecting a locale, which the framework does not do.
|
|
169
|
+
*
|
|
170
|
+
* NOT a Langium language id, which names a grammar. The two are strings of the
|
|
171
|
+
* same shape reachable from the same services, so one used where the other
|
|
172
|
+
* belongs misses every lookup rather than failing.
|
|
173
|
+
*/
|
|
174
|
+
export type Locale = string;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* A host's templates for one locale, keyed by the whole message code.
|
|
178
|
+
*
|
|
179
|
+
* **Flat, not nested.** A code is one key here, `/` separators included; a host
|
|
180
|
+
* whose own catalogue format nests — Theia's does — flattens on the way in. A
|
|
181
|
+
* map that nests instead misses every lookup while type-checking, because the
|
|
182
|
+
* value this is indexed by is `ResolvedMessage.code`.
|
|
183
|
+
*
|
|
184
|
+
* **Partial by nature rather than by convention.** A code with no entry renders
|
|
185
|
+
* its English default, so a catalogue covering none of a package's codes is a
|
|
186
|
+
* valid catalogue and not a broken one — which is what lets an adopter translate
|
|
187
|
+
* as much or as little as they like.
|
|
188
|
+
*
|
|
189
|
+
* Deliberately NOT narrowed to the codes that exist, and a `Code extends string`
|
|
190
|
+
* parameter would not buy what it appears to: a catalogue is loaded as JSON, so
|
|
191
|
+
* it reaches a checked position as a variable rather than as an object literal,
|
|
192
|
+
* and excess-property checking — the only thing that would reject a mistyped
|
|
193
|
+
* key — does not run there. The gain is a compile error for a catalogue with no
|
|
194
|
+
* correct key at all; the cost is a code union to maintain by hand, since a
|
|
195
|
+
* declaration does not carry its code as a literal type.
|
|
196
|
+
*/
|
|
197
|
+
export type MessageCatalogue = Readonly<Record<string, string>>;
|
|
198
|
+
|
|
199
|
+
export namespace MessageCatalogue {
|
|
200
|
+
/**
|
|
201
|
+
* One catalogue from several, for a renderer whose override supplies entries
|
|
202
|
+
* over the ones it inherits.
|
|
203
|
+
*
|
|
204
|
+
* **Later wins**, so a caller passes the inherited answer before its own.
|
|
205
|
+
*
|
|
206
|
+
* **No source at all answers `undefined`, not `{}`.** A renderer holds that
|
|
207
|
+
* answer as the locale having no catalogue and renders straight through; an
|
|
208
|
+
* empty object is a catalogue that misses every lookup, so returning one
|
|
209
|
+
* turns the opt-out into a per-message search that yields the same English.
|
|
210
|
+
*/
|
|
211
|
+
export function merge(...catalogues: ReadonlyArray<MessageCatalogue | undefined>): MessageCatalogue | undefined {
|
|
212
|
+
const present = catalogues.filter((catalogue): catalogue is MessageCatalogue => catalogue !== undefined);
|
|
213
|
+
if (present.length <= 1) {
|
|
214
|
+
return present[0];
|
|
215
|
+
}
|
|
216
|
+
return Object.assign({}, ...present) as MessageCatalogue;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Render on the side that knows the reading user's locale. Omitting
|
|
222
|
+
* `translations` is how an adopter without i18n opts out, and yields the
|
|
223
|
+
* English.
|
|
224
|
+
*/
|
|
225
|
+
export function renderFrameworkMessage(message: ResolvedMessage, translations?: MessageCatalogue): string {
|
|
226
|
+
const template = translations?.[message.code];
|
|
227
|
+
return template ? interpolate(template, message.params) : message.text;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function resolvedFromResponseError(error: ResponseError<unknown>): ResolvedMessage | undefined {
|
|
231
|
+
return hasMessageIdentity(error.data) ? { ...error.data.hydranium, text: error.message } : undefined;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The detail half of a `{detail}` placeholder. A technical error string is safe
|
|
236
|
+
* to pass as a parameter for the same reason a number is: it is not itself
|
|
237
|
+
* translatable text, so it needs no code of its own. A PROSE fragment is not,
|
|
238
|
+
* and must become one code per value instead.
|
|
239
|
+
*/
|
|
240
|
+
export function describeError(error: unknown): string {
|
|
241
|
+
return error instanceof Error ? error.message : String(error);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Recognises a declaration among a barrel's exports. The `format` check is what
|
|
246
|
+
* discriminates: a `code` + `text` pair alone admits any object that happens to
|
|
247
|
+
* carry both.
|
|
248
|
+
*/
|
|
249
|
+
export function isMessageDeclaration(value: unknown): value is MessageDefinition<string> {
|
|
250
|
+
const candidate = value as { code?: unknown; text?: unknown; format?: unknown } | null;
|
|
251
|
+
return (
|
|
252
|
+
typeof candidate === 'object' &&
|
|
253
|
+
candidate !== null &&
|
|
254
|
+
typeof candidate.code === 'string' &&
|
|
255
|
+
typeof candidate.text === 'string' &&
|
|
256
|
+
typeof candidate.format === 'function'
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Every declaration a `./messages` barrel exports.
|
|
262
|
+
*
|
|
263
|
+
* A caller cannot get there with `Object.values(barrel).filter(isMessageDeclaration)`:
|
|
264
|
+
* a barrel's value type is a union of its declarations AND its functions, and
|
|
265
|
+
* `filter` will not narrow a function type down to a `MessageDefinition`, so the
|
|
266
|
+
* result stays the union and reading `.code` off it does not compile. Taking the
|
|
267
|
+
* barrel as an opaque object is what makes the one-liner work.
|
|
268
|
+
*/
|
|
269
|
+
export function collectMessages(barrel: object): MessageDefinition<string>[] {
|
|
270
|
+
return (Object.values(barrel) as unknown[]).filter(isMessageDeclaration);
|
|
271
|
+
}
|
package/src/model-server.ts
CHANGED
|
@@ -14,17 +14,19 @@
|
|
|
14
14
|
* over `TRoot`.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import type { TransferClientArgs } from './model-service/args';
|
|
18
17
|
import type { ReferenceCandidate } from './model-service/reference-candidate';
|
|
19
18
|
|
|
20
19
|
// ---------------------------------------------------------------------------
|
|
21
20
|
// Client / server arguments
|
|
22
21
|
// ---------------------------------------------------------------------------
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
/** Identifies a client-document binding. */
|
|
24
|
+
export interface TransferClientArgs {
|
|
25
|
+
/** Document URI. */
|
|
26
|
+
uri: string;
|
|
27
|
+
/** Stable identifier for the client invoking the operation. */
|
|
28
|
+
clientId: string;
|
|
29
|
+
}
|
|
28
30
|
|
|
29
31
|
/** Open a document on behalf of a client. */
|
|
30
32
|
export interface OpenModelArgs extends TransferClientArgs {
|
|
@@ -47,10 +49,18 @@ export interface OpenModelArgs extends TransferClientArgs {
|
|
|
47
49
|
* means read from the filesystem, which is the normal case.
|
|
48
50
|
*
|
|
49
51
|
* Honoured only on the first open of a URI: opening an already-open document
|
|
50
|
-
*
|
|
51
|
-
* through the update path instead.
|
|
52
|
+
* attaches the caller to the existing shared entry and does not replace its
|
|
53
|
+
* content with this seed. Write through the update path instead. The open
|
|
54
|
+
* response reads the current shared content after registration; a textual
|
|
55
|
+
* `didOpen` attach may separately refresh the build.
|
|
52
56
|
*/
|
|
53
57
|
text?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Kept on the server for this client's open until it closes, for the
|
|
60
|
+
* server's own open path to read. Only a client session's open keeps them,
|
|
61
|
+
* and a repeat open keeps the first open's.
|
|
62
|
+
*/
|
|
63
|
+
options?: object;
|
|
54
64
|
}
|
|
55
65
|
|
|
56
66
|
/** Close a previously-opened document for the client. */
|
|
@@ -63,9 +73,25 @@ export interface CloseModelArgs extends TransferClientArgs {}
|
|
|
63
73
|
|
|
64
74
|
export interface TransferUpdatedEvent<TDocument> {
|
|
65
75
|
document: TDocument;
|
|
76
|
+
/**
|
|
77
|
+
* The client whose write this event echoes: the author of the version on a
|
|
78
|
+
* `'changed'`, and the unknown-client id on a `'rebuilt'`, which echoes no
|
|
79
|
+
* write. A recipient compares it against its own id to recognise its echo.
|
|
80
|
+
*/
|
|
66
81
|
sourceClientId: string;
|
|
67
|
-
/** See `
|
|
68
|
-
reason: 'changed' | '
|
|
82
|
+
/** See `TransferDocumentUpdateReason` in `./data/events` for the canonical reason set + semantics. */
|
|
83
|
+
reason: 'changed' | 'rebuilt' | 'saved';
|
|
84
|
+
/**
|
|
85
|
+
* The client whose write caused the build that produced this event, or the
|
|
86
|
+
* unknown-client id when no single client's did. On a `'changed'` it is the
|
|
87
|
+
* version's author. A `'rebuilt'` names the writer of another document, so
|
|
88
|
+
* it is a cause, never an echo: its diagnostics and references are new to
|
|
89
|
+
* that writer too.
|
|
90
|
+
*
|
|
91
|
+
* In-process only: the data head does not send it, and an event built
|
|
92
|
+
* elsewhere may leave it out, which a reader takes as "not known".
|
|
93
|
+
*/
|
|
94
|
+
causedBy?: string;
|
|
69
95
|
}
|
|
70
96
|
|
|
71
97
|
export interface TransferSavedEvent<TDocument> {
|
|
@@ -147,10 +173,10 @@ export function isElementSource(object: unknown): object is ElementSource {
|
|
|
147
173
|
/** An element of a document that does not yet exist on disk — used during element creation flows. */
|
|
148
174
|
export interface SyntheticSource {
|
|
149
175
|
/**
|
|
150
|
-
* The document the element would belong to.
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
176
|
+
* The document the element would belong to. It need not be loaded, or
|
|
177
|
+
* exist — an unloaded URI materialises a transient empty document, so a
|
|
178
|
+
* folder is a valid anchor. A URI that IS loaded contributes that
|
|
179
|
+
* document's parse root as the container, putting its contents in scope.
|
|
154
180
|
*/
|
|
155
181
|
uri: string;
|
|
156
182
|
/**
|
|
@@ -159,6 +185,16 @@ export interface SyntheticSource {
|
|
|
159
185
|
* it is what scoping filters candidates against.
|
|
160
186
|
*/
|
|
161
187
|
type: string;
|
|
188
|
+
/**
|
|
189
|
+
* The grammar the element will belong to, as a language id.
|
|
190
|
+
*
|
|
191
|
+
* Optional because `uri` and `type` each answer on their own when
|
|
192
|
+
* unambiguous. Set it when neither is: a URI naming no file carries no
|
|
193
|
+
* extension to route on, and a `type` two grammars can produce identifies
|
|
194
|
+
* neither. Unset, such a source reaches the server's own policy, which
|
|
195
|
+
* answers the same way for every caller.
|
|
196
|
+
*/
|
|
197
|
+
language?: string;
|
|
162
198
|
}
|
|
163
199
|
|
|
164
200
|
export function isSyntheticSource(object: unknown): object is SyntheticSource {
|
|
@@ -183,8 +219,10 @@ export namespace ReferenceSource {
|
|
|
183
219
|
export function element(name: string, type?: string): ElementSource {
|
|
184
220
|
return { name, type };
|
|
185
221
|
}
|
|
186
|
-
export function synthetic(uri: string, type: string): SyntheticSource {
|
|
187
|
-
|
|
222
|
+
export function synthetic(uri: string, type: string, language?: string): SyntheticSource {
|
|
223
|
+
// The key is omitted rather than set to `undefined`, so a source built
|
|
224
|
+
// without a language stays deep-equal to the two-argument form.
|
|
225
|
+
return language === undefined ? { uri, type } : { uri, type, language };
|
|
188
226
|
}
|
|
189
227
|
}
|
|
190
228
|
|
|
@@ -457,9 +495,10 @@ export interface FindNextNameArgs {
|
|
|
457
495
|
*/
|
|
458
496
|
uri: string;
|
|
459
497
|
/**
|
|
460
|
-
* The AST `$type` of the element being named. Collisions are
|
|
461
|
-
* among elements of that
|
|
462
|
-
*
|
|
498
|
+
* The AST `$type` of the element being named. Collisions are looked for
|
|
499
|
+
* among elements of that type and of any subtype of it, so a supertype
|
|
500
|
+
* names a uniqueness scope its concrete types share; the returned name may
|
|
501
|
+
* still be taken by an element outside that hierarchy.
|
|
463
502
|
*/
|
|
464
503
|
type: string;
|
|
465
504
|
/**
|
|
@@ -476,4 +515,10 @@ export interface FindNextNameArgs {
|
|
|
476
515
|
* See {@link NameTier} for what each tier covers.
|
|
477
516
|
*/
|
|
478
517
|
tier?: NameTier;
|
|
518
|
+
/**
|
|
519
|
+
* The grammar to name under, routed as {@link SyntheticSource.language} —
|
|
520
|
+
* this call builds one. A caller that sets it there must set it here, or
|
|
521
|
+
* candidates and the name it proposes come from different grammars.
|
|
522
|
+
*/
|
|
523
|
+
language?: string;
|
|
479
524
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
declare const snapshotMarker: unique symbol;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The version of the text a model was parsed from: a snapshot of the store's
|
|
14
|
+
* {@link TextVersion}, not a counter of its own. A document envelope carries
|
|
15
|
+
* it, and it cannot move once the envelope exists.
|
|
16
|
+
*
|
|
17
|
+
* A plain number at runtime, usable anywhere a number is, and it travels the
|
|
18
|
+
* wire as one. The marker exists only so the compiler can tell it apart from a
|
|
19
|
+
* version read off a LIVE document, which is the same number type and is the
|
|
20
|
+
* defect the conflict gate exists to prevent: read at write time it is whatever
|
|
21
|
+
* the server is at now, which is the number the gate is about to compare it
|
|
22
|
+
* against, so the gate passes unconditionally and a concurrent edit is
|
|
23
|
+
* overwritten with nothing logged.
|
|
24
|
+
*/
|
|
25
|
+
export type ModelVersion = number & { readonly [snapshotMarker]: true };
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The store's version of a document's text, which moves with every change of it.
|
|
29
|
+
* Unbranded, so a write refuses it as `baseVersion`: it can name text newer than
|
|
30
|
+
* the caller's model, and a write based on it would pass the gate with older content.
|
|
31
|
+
*/
|
|
32
|
+
export type TextVersion = number;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The version a write was authored against, from a snapshot read, or `'any'`: a
|
|
36
|
+
* precondition that always holds, so the write overwrites whatever the server has.
|
|
37
|
+
* `'any'` is a lie for a write authored against a document a read handed over.
|
|
38
|
+
*/
|
|
39
|
+
export type BaseVersion = ModelVersion | 'any';
|
|
40
|
+
|
|
41
|
+
/** Whether `baseVersion` names a version, and so arms the gate. */
|
|
42
|
+
export function isModelVersion(baseVersion: BaseVersion): baseVersion is ModelVersion {
|
|
43
|
+
return typeof baseVersion === 'number';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Mark a version as having come from a snapshot read.
|
|
48
|
+
*
|
|
49
|
+
* **For the envelope constructors, not for callers.** Every door that builds a
|
|
50
|
+
* document envelope applies it on the way in, so anything a read returns
|
|
51
|
+
* already carries it and a writer never needs this. A caller reaching for it is
|
|
52
|
+
* asserting a provenance the compiler was about to deny — the read-late defect
|
|
53
|
+
* written out where a reviewer can see it.
|
|
54
|
+
*/
|
|
55
|
+
export function asModelVersion(version: number): ModelVersion {
|
|
56
|
+
return version as ModelVersion;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The model version of a root no document factory path built, so nothing
|
|
61
|
+
* records the text it came from. No write matches it, so a write based on it
|
|
62
|
+
* conflicts.
|
|
63
|
+
*/
|
|
64
|
+
export const UNRECORDED_VERSION: ModelVersion = asModelVersion(-2);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The model version of a root parsed from text the store holds under no
|
|
68
|
+
* version: the file, read while the store held the document open with other
|
|
69
|
+
* text. Below every text version, so the model reads as behind and a write
|
|
70
|
+
* based on it conflicts.
|
|
71
|
+
*/
|
|
72
|
+
export const STALE_VERSION: ModelVersion = asModelVersion(-1);
|
|
@@ -7,10 +7,9 @@
|
|
|
7
7
|
* SPDX-License-Identifier: MIT
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
// protocol can structurally agree on the lifecycle shape.
|
|
10
|
+
// Generic payload types the data protocol shares with core: the base
|
|
11
|
+
// version, which the server's model service checks writes against, and the
|
|
12
|
+
// reference candidates, which core's scope and completion code produce.
|
|
14
13
|
|
|
15
|
-
export * from './
|
|
14
|
+
export * from './base-version';
|
|
16
15
|
export * from './reference-candidate';
|
|
@@ -50,9 +50,11 @@ export interface ReferenceCandidate {
|
|
|
50
50
|
* node's subtree, NOT the whole document root — callers that want the full
|
|
51
51
|
* document fetch it via `getModelDocument(uri)`.
|
|
52
52
|
*
|
|
53
|
-
*
|
|
53
|
+
* `TElement` is the type of the target node, not of a document root. A server
|
|
54
|
+
* answers with the structural base, since a reference can name any node; a
|
|
55
|
+
* caller that knows what the reference targets may narrow it.
|
|
54
56
|
*/
|
|
55
|
-
export interface ReferenceTarget<
|
|
57
|
+
export interface ReferenceTarget<TElement extends TransferElement = TransferElement> extends ReferenceCandidate {
|
|
56
58
|
/** The resolved target node, encoded as a transfer subtree. */
|
|
57
|
-
element:
|
|
59
|
+
element: TElement;
|
|
58
60
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
// Subpath barrel for `@hydranium/protocol/node` — the diagnostics helpers that
|
|
11
|
+
// need a Node runtime, kept out of the root barrel so the root stays
|
|
12
|
+
// bundleable for a browser.
|
|
13
|
+
|
|
14
|
+
export * from './process-memory';
|