@hydranium/protocol 1.0.0-next.39 → 1.0.0-next.391
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 +46 -83
- package/lib/abstract-logger.d.ts +11 -0
- package/lib/abstract-logger.d.ts.map +1 -1
- package/lib/abstract-logger.js +16 -0
- package/lib/abstract-logger.js.map +1 -1
- package/lib/client/data-connection.d.ts +303 -0
- package/lib/client/data-connection.d.ts.map +1 -0
- package/lib/client/data-connection.js +534 -0
- package/lib/client/data-connection.js.map +1 -0
- package/lib/client/data-events.d.ts +11 -2
- package/lib/client/data-events.d.ts.map +1 -1
- package/lib/client/data-events.js +13 -1
- package/lib/client/data-events.js.map +1 -1
- package/lib/client/data-port.d.ts +34 -24
- package/lib/client/data-port.d.ts.map +1 -1
- package/lib/client/data-session.d.ts +762 -89
- package/lib/client/data-session.d.ts.map +1 -1
- package/lib/client/data-session.js +1253 -115
- package/lib/client/data-session.js.map +1 -1
- package/lib/client/index.d.ts +16 -9
- package/lib/client/index.d.ts.map +1 -1
- package/lib/client/index.js +16 -9
- package/lib/client/index.js.map +1 -1
- package/lib/client/message-relay.d.ts +6 -4
- package/lib/client/message-relay.d.ts.map +1 -1
- package/lib/client/message-relay.js +4 -4
- package/lib/client/message-relay.js.map +1 -1
- package/lib/client/post-message-transport.d.ts +69 -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 +173 -0
- package/lib/client/rpc-connection.d.ts.map +1 -0
- package/lib/client/rpc-connection.js +225 -0
- package/lib/client/rpc-connection.js.map +1 -0
- package/lib/client/worker-handshake.d.ts +94 -0
- package/lib/client/worker-handshake.d.ts.map +1 -0
- package/lib/client/worker-handshake.js +51 -0
- package/lib/client/worker-handshake.js.map +1 -0
- package/lib/client/working-copy.d.ts +420 -0
- package/lib/client/working-copy.d.ts.map +1 -0
- package/lib/client/working-copy.js +958 -0
- package/lib/client/working-copy.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 +50 -10
- 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 +7 -0
- package/lib/data/data-protocol-methods.js.map +1 -1
- package/lib/data/data-server-protocol.d.ts +160 -50
- package/lib/data/data-server-protocol.d.ts.map +1 -1
- package/lib/data/events.d.ts +75 -31
- package/lib/data/events.d.ts.map +1 -1
- package/lib/data/requests.d.ts +84 -11
- package/lib/data/requests.d.ts.map +1 -1
- package/lib/debouncer.d.ts +7 -4
- package/lib/debouncer.d.ts.map +1 -1
- package/lib/debouncer.js +10 -10
- package/lib/debouncer.js.map +1 -1
- package/lib/errors.d.ts +241 -48
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +395 -60
- package/lib/errors.js.map +1 -1
- package/lib/format-log-arg.d.ts +33 -0
- package/lib/format-log-arg.d.ts.map +1 -0
- package/lib/format-log-arg.js +158 -0
- package/lib/format-log-arg.js.map +1 -0
- package/lib/glsp-edits-settled-actions.d.ts +54 -0
- package/lib/glsp-edits-settled-actions.d.ts.map +1 -0
- package/lib/glsp-edits-settled-actions.js +28 -0
- package/lib/glsp-edits-settled-actions.js.map +1 -0
- 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 +55 -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 +11 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -1
- package/lib/latency-collector.d.ts +10 -4
- package/lib/latency-collector.d.ts.map +1 -1
- package/lib/latency-collector.js +2 -0
- package/lib/latency-collector.js.map +1 -1
- package/lib/logger.d.ts +38 -7
- package/lib/logger.d.ts.map +1 -1
- package/lib/logger.js +42 -4
- package/lib/logger.js.map +1 -1
- package/lib/messages/index.d.ts +4 -2
- package/lib/messages/index.d.ts.map +1 -1
- package/lib/messages/index.js +17 -4
- package/lib/messages/index.js.map +1 -1
- package/lib/messages/primitives.d.ts +59 -4
- package/lib/messages/primitives.d.ts.map +1 -1
- package/lib/messages/primitives.js +97 -4
- package/lib/messages/primitives.js.map +1 -1
- package/lib/model-server.d.ts +58 -11
- 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 +67 -0
- package/lib/model-service/base-version.d.ts.map +1 -0
- package/lib/model-service/base-version.js +44 -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/node/index.d.ts +10 -0
- 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 +63 -35
- package/lib/patch-merge.d.ts.map +1 -1
- package/lib/patch-merge.js +171 -25
- package/lib/patch-merge.js.map +1 -1
- package/lib/pending-work-tracker.d.ts +33 -0
- package/lib/pending-work-tracker.d.ts.map +1 -0
- package/lib/pending-work-tracker.js +48 -0
- package/lib/pending-work-tracker.js.map +1 -0
- package/lib/profile-session.d.ts +24 -8
- package/lib/profile-session.d.ts.map +1 -1
- package/lib/profile-session.js +13 -4
- package/lib/profile-session.js.map +1 -1
- package/lib/profiling.d.ts +15 -2
- package/lib/profiling.d.ts.map +1 -1
- package/lib/profiling.js.map +1 -1
- package/lib/project.d.ts +6 -2
- package/lib/project.d.ts.map +1 -1
- package/lib/project.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 +70 -0
- package/lib/reconcile-write.d.ts.map +1 -0
- package/lib/reconcile-write.js +69 -0
- package/lib/reconcile-write.js.map +1 -0
- package/lib/rpc/bind-rpc-methods.d.ts +6 -2
- package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
- package/lib/rpc/bind-rpc-methods.js +17 -7
- package/lib/rpc/bind-rpc-methods.js.map +1 -1
- package/lib/rpc/create-rpc-proxy.d.ts +3 -0
- package/lib/rpc/create-rpc-proxy.d.ts.map +1 -1
- package/lib/rpc/create-rpc-proxy.js +6 -1
- 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/save-text-document.d.ts +40 -0
- package/lib/save-text-document.d.ts.map +1 -0
- package/lib/save-text-document.js +34 -0
- package/lib/save-text-document.js.map +1 -0
- package/lib/testing/catalogue-audit.js.map +1 -1
- package/lib/testing/data-doubles.d.ts +36 -11
- package/lib/testing/data-doubles.d.ts.map +1 -1
- package/lib/testing/data-doubles.js +47 -9
- package/lib/testing/data-doubles.js.map +1 -1
- package/lib/testing/fake-clock.d.ts +17 -2
- package/lib/testing/fake-clock.d.ts.map +1 -1
- package/lib/testing/fake-clock.js +61 -46
- package/lib/testing/fake-clock.js.map +1 -1
- package/lib/testing/harness.d.ts +9 -0
- package/lib/testing/harness.d.ts.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 +4 -3
- 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 +2 -0
- package/lib/testing/node/index.d.ts.map +1 -1
- package/lib/testing/node/index.js +3 -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/node/unhandled-rejections.d.ts +20 -0
- package/lib/testing/node/unhandled-rejections.d.ts.map +1 -0
- package/lib/testing/node/unhandled-rejections.js +35 -0
- package/lib/testing/node/unhandled-rejections.js.map +1 -0
- package/lib/testing/transfer-node-builder.d.ts +81 -0
- package/lib/testing/transfer-node-builder.d.ts.map +1 -0
- package/lib/testing/transfer-node-builder.js +47 -0
- package/lib/testing/transfer-node-builder.js.map +1 -0
- package/lib/testing/wait-for.d.ts +9 -3
- package/lib/testing/wait-for.d.ts.map +1 -1
- package/lib/testing/wait-for.js +57 -11
- package/lib/testing/wait-for.js.map +1 -1
- package/lib/tracer.d.ts.map +1 -1
- package/lib/tracer.js +5 -9
- package/lib/tracer.js.map +1 -1
- package/lib/transfer-diagnostic.d.ts +11 -0
- package/lib/transfer-diagnostic.d.ts.map +1 -1
- package/lib/transfer-diagnostic.js +17 -0
- package/lib/transfer-diagnostic.js.map +1 -1
- package/lib/transfer-document.d.ts +108 -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/transfer-element.d.ts +4 -4
- package/lib/types.d.ts +32 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/{model-service/args.js → types.js} +2 -2
- package/lib/types.js.map +1 -0
- package/lib/uri.d.ts.map +1 -1
- package/lib/uri.js.map +1 -1
- package/lib/util.d.ts +8 -2
- package/lib/util.d.ts.map +1 -1
- package/lib/util.js +32 -4
- package/lib/util.js.map +1 -1
- package/package.json +24 -40
- package/src/abstract-logger.ts +18 -0
- package/src/client/data-connection.ts +654 -0
- package/src/client/data-events.ts +15 -1
- package/src/client/data-port.ts +36 -25
- package/src/client/data-session.ts +1606 -137
- package/src/client/index.ts +16 -9
- package/src/client/message-relay.ts +6 -4
- package/src/client/post-message-transport.ts +224 -4
- package/src/client/rpc-connection.ts +302 -0
- package/src/client/worker-handshake.ts +121 -0
- package/src/client/working-copy.ts +1116 -0
- package/src/client-ids.ts +49 -0
- package/src/clock.ts +68 -10
- package/src/console-logger.ts +39 -0
- package/src/data/data-protocol-methods.ts +8 -1
- package/src/data/data-server-protocol.ts +184 -49
- package/src/data/events.ts +77 -31
- package/src/data/requests.ts +90 -11
- package/src/debouncer.ts +11 -11
- package/src/errors.ts +496 -71
- package/src/format-log-arg.ts +176 -0
- package/src/glsp-edits-settled-actions.ts +62 -0
- package/src/glsp-request-model-args.ts +16 -0
- package/src/glsp-save-model-actions.ts +64 -0
- package/src/index.ts +11 -0
- package/src/latency-collector.ts +10 -3
- package/src/logger.ts +44 -8
- package/src/messages/index.ts +12 -2
- package/src/messages/primitives.ts +129 -5
- package/src/model-server.ts +62 -17
- package/src/model-service/base-version.ts +75 -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 +230 -45
- package/src/pending-work-tracker.ts +48 -0
- package/src/profile-session.ts +31 -11
- package/src/profiling.ts +15 -2
- package/src/project.ts +6 -2
- package/src/random-uuid.ts +21 -0
- package/src/reconcile-write.ts +131 -0
- package/src/rpc/README.md +6 -6
- package/src/rpc/bind-rpc-methods.ts +18 -8
- package/src/rpc/create-rpc-proxy.ts +6 -1
- package/src/rpc/index.ts +1 -0
- package/src/rpc/send-by-method-name.ts +140 -0
- package/src/save-text-document.ts +39 -0
- package/src/testing/data-doubles.ts +119 -19
- package/src/testing/fake-clock.ts +76 -48
- package/src/testing/harness.ts +10 -0
- package/src/testing/index.ts +4 -3
- package/src/testing/node/duplex-connection.ts +3 -2
- package/src/testing/node/index.ts +3 -2
- package/src/testing/node/message-port-pair.ts +40 -0
- package/src/testing/node/unhandled-rejections.ts +31 -0
- package/src/testing/transfer-node-builder.ts +107 -0
- package/src/testing/wait-for.ts +61 -13
- package/src/tracer.ts +5 -9
- package/src/transfer-diagnostic.ts +17 -0
- package/src/transfer-document.ts +130 -34
- package/src/transfer-element.ts +4 -4
- package/src/types.ts +30 -0
- package/src/util.ts +33 -6
- 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,131 @@
|
|
|
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 { type ConflictError, isConflictError } from './errors';
|
|
11
|
+
import { type BaseVersion, type ModelVersion } from './model-service/base-version';
|
|
12
|
+
import { type ConflictResolver } from './patch-merge';
|
|
13
|
+
|
|
14
|
+
const DEFAULT_MAX_WRITES = 3;
|
|
15
|
+
|
|
16
|
+
/** A model and the version of the text it was read from, which a write based on it names. */
|
|
17
|
+
export interface VersionedModel<TModel> {
|
|
18
|
+
readonly model: TModel;
|
|
19
|
+
readonly baseVersion: ModelVersion;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The I/O and policy a {@link reconcileWrite} call needs. */
|
|
23
|
+
export interface ReconcileWriteHooks<TModel> {
|
|
24
|
+
/**
|
|
25
|
+
* Write `model`. Called with the caller's own `baseVersion` first, with the
|
|
26
|
+
* refetch's on a merged write, and with `'any'` for a merge that refetched
|
|
27
|
+
* nothing, where the resolver has already decided to win.
|
|
28
|
+
*/
|
|
29
|
+
persist(model: TModel, baseVersion: BaseVersion): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Current server-side model, or `undefined` when unavailable. Its version is
|
|
32
|
+
* read in the tick its text is: a later one lets a merged write overwrite an
|
|
33
|
+
* edit the merge never saw.
|
|
34
|
+
*/
|
|
35
|
+
refetch(): Promise<VersionedModel<TModel> | undefined>;
|
|
36
|
+
/** The last in-sync model the user's intent is measured against. */
|
|
37
|
+
readonly base: TModel;
|
|
38
|
+
/** Asked for each conflict while writes remain. Its refetch callback runs {@link refetch} only when called. */
|
|
39
|
+
readonly conflictResolver: ConflictResolver;
|
|
40
|
+
/** Writes, the first included, after which a still-conflicting edit is dropped; 3 when absent. */
|
|
41
|
+
readonly maxWrites?: number;
|
|
42
|
+
/** Runs when the refetch is unavailable; absent, the edit is left unwritten. */
|
|
43
|
+
onUnavailable?(model: TModel, conflict: ConflictError): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* What {@link reconcileWrite} did. Every status but `persisted` follows a
|
|
48
|
+
* conflict, and carries the last one and the writes made, the first included.
|
|
49
|
+
*/
|
|
50
|
+
export type ReconcileWriteOutcome =
|
|
51
|
+
/** The first write landed. */
|
|
52
|
+
| { readonly status: 'persisted' }
|
|
53
|
+
| {
|
|
54
|
+
/**
|
|
55
|
+
* `merged`: a merged write landed. `already-applied`: the refetch
|
|
56
|
+
* already held the edit, so nothing more was written. `unchanged`: the
|
|
57
|
+
* edit was empty, so the caller is behind the server. `conflict`: the
|
|
58
|
+
* resolver found a same-path collision. `dropped`: the writes ran out.
|
|
59
|
+
* `unavailable`: the refetch produced nothing and
|
|
60
|
+
* {@link ReconcileWriteHooks.onUnavailable} ran.
|
|
61
|
+
*/
|
|
62
|
+
readonly status: 'merged' | 'already-applied' | 'unchanged' | 'conflict' | 'dropped' | 'unavailable';
|
|
63
|
+
/** The last conflict a write raised, which on `merged` is the one before the write that landed. */
|
|
64
|
+
readonly conflict: ConflictError;
|
|
65
|
+
/** Writes made, the first included. `dropped` is reported once they reach {@link ReconcileWriteHooks.maxWrites}. */
|
|
66
|
+
readonly writes: number;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Persist `model`, and on a `ConflictError` reconcile the user's `base → model`
|
|
71
|
+
* intent onto the refetched server model and write the merge, gated on the
|
|
72
|
+
* refetch's version, again on each conflict up to `maxWrites` writes. A
|
|
73
|
+
* non-conflict error is re-thrown.
|
|
74
|
+
*/
|
|
75
|
+
export async function reconcileWrite<TModel extends object>(
|
|
76
|
+
model: TModel,
|
|
77
|
+
baseVersion: BaseVersion,
|
|
78
|
+
hooks: ReconcileWriteHooks<TModel>
|
|
79
|
+
): Promise<ReconcileWriteOutcome> {
|
|
80
|
+
let candidate = model;
|
|
81
|
+
let candidateBaseVersion = baseVersion;
|
|
82
|
+
let previous: ConflictError | undefined;
|
|
83
|
+
const maxWrites = hooks.maxWrites ?? DEFAULT_MAX_WRITES;
|
|
84
|
+
for (let writes = 1; ; writes++) {
|
|
85
|
+
const conflict = await persistOrConflict(hooks, candidate, candidateBaseVersion);
|
|
86
|
+
if (!conflict) {
|
|
87
|
+
return previous ? { status: 'merged', conflict: previous, writes } : { status: 'persisted' };
|
|
88
|
+
}
|
|
89
|
+
previous = conflict;
|
|
90
|
+
if (writes >= maxWrites) {
|
|
91
|
+
return { status: 'dropped', conflict, writes };
|
|
92
|
+
}
|
|
93
|
+
let refetched: VersionedModel<TModel> | undefined;
|
|
94
|
+
const outcome = await hooks.conflictResolver.resolve(hooks.base, model, async () => {
|
|
95
|
+
refetched = await hooks.refetch();
|
|
96
|
+
return refetched?.model;
|
|
97
|
+
});
|
|
98
|
+
switch (outcome.status) {
|
|
99
|
+
case 'merged':
|
|
100
|
+
candidate = outcome.merged;
|
|
101
|
+
candidateBaseVersion = refetched?.baseVersion ?? 'any';
|
|
102
|
+
continue;
|
|
103
|
+
case 'already-applied':
|
|
104
|
+
return { status: 'already-applied', conflict, writes };
|
|
105
|
+
case 'no-op':
|
|
106
|
+
return { status: 'unchanged', conflict, writes };
|
|
107
|
+
case 'conflict':
|
|
108
|
+
return { status: 'conflict', conflict, writes };
|
|
109
|
+
case 'unavailable':
|
|
110
|
+
await hooks.onUnavailable?.(model, conflict);
|
|
111
|
+
return { status: 'unavailable', conflict, writes };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** The `ConflictError` the write raised, or `undefined` when it landed. */
|
|
117
|
+
async function persistOrConflict<TModel>(
|
|
118
|
+
hooks: ReconcileWriteHooks<TModel>,
|
|
119
|
+
model: TModel,
|
|
120
|
+
baseVersion: BaseVersion
|
|
121
|
+
): Promise<ConflictError | undefined> {
|
|
122
|
+
try {
|
|
123
|
+
await hooks.persist(model, baseVersion);
|
|
124
|
+
return undefined;
|
|
125
|
+
} catch (err: unknown) {
|
|
126
|
+
if (!isConflictError(err)) {
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
return err;
|
|
130
|
+
}
|
|
131
|
+
}
|
package/src/rpc/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
# JSON-RPC primitives
|
|
2
2
|
|
|
3
3
|
Exported from the package root, `@hydranium/protocol`. There is no
|
|
4
|
-
`@hydranium/protocol/rpc` subpath: the package's `exports` map
|
|
5
|
-
|
|
6
|
-
this directory by path fails with `ERR_PACKAGE_PATH_NOT_EXPORTED`.
|
|
4
|
+
`@hydranium/protocol/rpc` subpath: the package's `exports` map declares none,
|
|
5
|
+
so importing this directory by path fails with `ERR_PACKAGE_PATH_NOT_EXPORTED`.
|
|
7
6
|
|
|
8
7
|
Generic JSON-RPC primitives for typed protocol heads over a vscode-jsonrpc
|
|
9
8
|
`MessageConnection`. This page is the reference; the shape of the pattern and
|
|
@@ -44,8 +43,8 @@ data-server head ships with `'data-server/'` by default; adopters that
|
|
|
44
43
|
combine the data-server with their own protocol head under one prefix pass
|
|
45
44
|
their adopter namespace (e.g. `'myapp/'`) on both sides.
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
A namespace ends in `/` or is empty: `bindRpcMethods` and `createRpcProxy`
|
|
47
|
+
throw a `TypeError` for `'foo'`.
|
|
49
48
|
|
|
50
49
|
### Notification discrimination: `isNotification`
|
|
51
50
|
|
|
@@ -95,7 +94,8 @@ behaviour instead of dispatching a wire call. Adopters defining wire
|
|
|
95
94
|
methods should avoid:
|
|
96
95
|
|
|
97
96
|
- `onDidOpenConnection` / `onDidCloseConnection` — return the lifecycle events
|
|
98
|
-
- `then` — returns `undefined` so the proxy isn't auto-awaited by
|
|
97
|
+
- `then` — returns `undefined` so the proxy isn't auto-awaited by
|
|
98
|
+
host-environment promise detection
|
|
99
99
|
- `toJSON` — returns `undefined` so serialisers don't try to flatten the proxy
|
|
100
100
|
|
|
101
101
|
Symbol property accesses also return `undefined` (the proxy is not
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
10
|
import { ResponseError, type MessageConnection } from 'vscode-jsonrpc';
|
|
11
|
+
import { isResponseError } from '../errors';
|
|
11
12
|
import type { LatencyCollector } from '../latency-collector';
|
|
12
13
|
import { type Disposable, DisposableCollection } from '../util';
|
|
13
14
|
import { defaultIsNotification } from './create-rpc-proxy';
|
|
@@ -97,9 +98,17 @@ export interface BindRpcMethodsOptions {
|
|
|
97
98
|
readonly renderErrorMessage?: (error: ResponseError<unknown>) => string;
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
/**
|
|
101
|
-
|
|
102
|
-
|
|
101
|
+
/**
|
|
102
|
+
* The rejection to throw in place of `err`. A `ResponseError` from another copy
|
|
103
|
+
* of `vscode-jsonrpc` is rebuilt on this package's, since a connection keeps the
|
|
104
|
+
* code only of its own copy's errors; one from this copy is thrown as is, so a
|
|
105
|
+
* subclass keeps its `toJson`, unless `render` rewrites its message.
|
|
106
|
+
*/
|
|
107
|
+
function renderRejection(err: unknown, render?: (error: ResponseError<unknown>) => string): unknown {
|
|
108
|
+
if (!isResponseError(err) || (!render && err instanceof ResponseError)) {
|
|
109
|
+
return err;
|
|
110
|
+
}
|
|
111
|
+
return new ResponseError(err.code, render ? render(err) : err.message, err.data);
|
|
103
112
|
}
|
|
104
113
|
|
|
105
114
|
/**
|
|
@@ -121,8 +130,12 @@ function renderRejection(err: unknown, render: (error: ResponseError<unknown>) =
|
|
|
121
130
|
*
|
|
122
131
|
* Errors thrown synchronously from a request handler — or surfaced as a
|
|
123
132
|
* rejected promise — propagate back to the caller through vscode-jsonrpc's
|
|
124
|
-
* standard error envelope,
|
|
125
|
-
* {@link BindRpcMethodsOptions.renderErrorMessage} is supplied.
|
|
133
|
+
* standard error envelope, its message rendered when
|
|
134
|
+
* {@link BindRpcMethodsOptions.renderErrorMessage} is supplied. A
|
|
135
|
+
* `ResponseError` from another copy of `vscode-jsonrpc` is rebuilt on this
|
|
136
|
+
* package's copy, so its code and data reach the caller when `connection` comes
|
|
137
|
+
* from that copy too, as the framework's own connections do; over a connection
|
|
138
|
+
* from another copy the caller receives a generic `InternalError`. Errors from a
|
|
126
139
|
* notification handler cannot propagate, and are routed to
|
|
127
140
|
* {@link BindRpcMethodsOptions.onNotificationError} instead.
|
|
128
141
|
*
|
|
@@ -189,9 +202,6 @@ export function bindRpcMethods<T extends object>(
|
|
|
189
202
|
const render = options.renderErrorMessage;
|
|
190
203
|
disposables.push(
|
|
191
204
|
resolved.onRequest(wireName, async (params: unknown) => {
|
|
192
|
-
if (!render) {
|
|
193
|
-
return dispatch(params);
|
|
194
|
-
}
|
|
195
205
|
try {
|
|
196
206
|
// Awaited inside the try, or a rejected promise escapes it.
|
|
197
207
|
return await dispatch(params);
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
10
|
import { Emitter, type Event, type MessageConnection } from 'vscode-jsonrpc';
|
|
11
|
+
import { reviveProtocolError } from '../errors';
|
|
11
12
|
import { type BindRpcMethodsOptions, bindRpcMethods } from './bind-rpc-methods';
|
|
12
13
|
import { assertValidMethodNamespace } from './wire-prefix';
|
|
13
14
|
|
|
@@ -198,6 +199,9 @@ function assertSingleArg(wireName: string, args: unknown[]): void {
|
|
|
198
199
|
* params, results and errors, and a second layer here would double every
|
|
199
200
|
* traced line.
|
|
200
201
|
*
|
|
202
|
+
* A rejection whose code names one of the framework's typed errors is rethrown
|
|
203
|
+
* as that class, through {@link reviveProtocolError}.
|
|
204
|
+
*
|
|
201
205
|
* Accepts either a ready connection or a `Promise<MessageConnection>` —
|
|
202
206
|
* proxy methods called before the promise resolves queue until it does,
|
|
203
207
|
* then dispatch, so adopters can wire the proxy before its underlying
|
|
@@ -293,7 +297,8 @@ export function createRpcProxy<T extends object, TLocal extends object = never>(
|
|
|
293
297
|
const capturedError = new Error(`RPC request '${wireName}' failed`);
|
|
294
298
|
return resolvedConnection
|
|
295
299
|
.then(connection => connection.sendRequest(wireName, args[0]))
|
|
296
|
-
.catch((
|
|
300
|
+
.catch((rejection: unknown) => {
|
|
301
|
+
const err = reviveProtocolError(rejection);
|
|
297
302
|
if (err instanceof Error && capturedError.stack) {
|
|
298
303
|
err.stack = `${err.stack ?? err.message}\nCaused by request from:\n${capturedError.stack}`;
|
|
299
304
|
}
|
package/src/rpc/index.ts
CHANGED
|
@@ -0,0 +1,140 @@
|
|
|
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 { CancellationToken } from 'vscode-jsonrpc';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* `connection` with `sendRequest` and `sendNotification` sending a typed message
|
|
14
|
+
* by its method name, so it survives a message type built by another copy of
|
|
15
|
+
* `vscode-jsonrpc`. Wrap a connection wherever it is handed to code that sends
|
|
16
|
+
* another copy's typed messages, GLSP's clients and launchers in particular.
|
|
17
|
+
*
|
|
18
|
+
* **Why an install holds several copies.** Upstream pins exactly:
|
|
19
|
+
* - `@eclipse-glsp/*` 2.x pin `vscode-jsonrpc` `8.2.0`, and npm can nest a copy
|
|
20
|
+
* under each GLSP package, so GLSP's packages can split from each other: a
|
|
21
|
+
* connection one of them creates rejects the typed messages another builds.
|
|
22
|
+
* GLSP's upgrade to 9.x, which could let it share the framework's copy, is
|
|
23
|
+
* open as https://github.com/eclipse-glsp/glsp/issues/1720.
|
|
24
|
+
* - `vscode-languageserver@10.0.1` pins `vscode-languageserver-protocol`
|
|
25
|
+
* `3.18.1`, which pins `vscode-jsonrpc` `9.0.0`.
|
|
26
|
+
* - Theia brings protocol `3.17.5` and with it `vscode-jsonrpc` 8.
|
|
27
|
+
*
|
|
28
|
+
* npm hoists one version per name and nests the rest. `overrides` are read from
|
|
29
|
+
* the root manifest alone and never ship with a package, so the framework cannot
|
|
30
|
+
* collapse an adopter's tree.
|
|
31
|
+
*
|
|
32
|
+
* **What breaks across copies.**
|
|
33
|
+
* - Sending: a connection compares a typed message's parameter structure with
|
|
34
|
+
* its own copy's `ParameterStructures.auto` singleton by identity, so a
|
|
35
|
+
* `RequestType` or `NotificationType` built by another copy throws
|
|
36
|
+
* `Unknown parameter structure auto`. GLSP sends typed messages, in
|
|
37
|
+
* `BaseJsonrpcGLSPClient` and in `JsonrpcClientProxy.process`.
|
|
38
|
+
* `vscode-languageserver`'s connection resends by method name already, which
|
|
39
|
+
* is why LSP sends are unaffected.
|
|
40
|
+
* - Types: `ParameterStructures` has a private member, so TypeScript treats each
|
|
41
|
+
* copy's `MessageConnection` as a different type.
|
|
42
|
+
* - Errors: a connection keeps a thrown `ResponseError`'s code and data only
|
|
43
|
+
* when the error is an instance of its own copy's class, and sends a returned
|
|
44
|
+
* one of another copy as a successful result.
|
|
45
|
+
*
|
|
46
|
+
* **What is copy-safe.** Receiving a typed message from another copy, since the
|
|
47
|
+
* receive path compares only against its own `byName` and `byPosition`. With
|
|
48
|
+
* sends by method name, the LSP, data and GLSP heads run over a tree whose GLSP
|
|
49
|
+
* packages keep `vscode-jsonrpc` `8.2.0`.
|
|
50
|
+
*
|
|
51
|
+
* **How it works.** A `Proxy` whose `sendRequest` and `sendNotification` pass the
|
|
52
|
+
* typed message's `method` string; every other member is the connection's own.
|
|
53
|
+
* The parameters go out as the typed send packs them: the first
|
|
54
|
+
* `numberOfParams`, missing ones `null`, and a request's cancellation token
|
|
55
|
+
* from the position after them. Sent by method name, a single parameter goes by
|
|
56
|
+
* name when it is an object and by position otherwise, which matches `auto` and
|
|
57
|
+
* `byName` with an object, as `vscode-languageserver-protocol`'s types use it. A
|
|
58
|
+
* `byPosition` object or a `byName` non-object throws rather than going out in
|
|
59
|
+
* another shape, the packing read from the type's `toString()` since it cannot
|
|
60
|
+
* be compared by identity. So wrapping narrows what a connection accepts: its
|
|
61
|
+
* own copy sends such a type unwrapped. The result is typed as whichever copy's
|
|
62
|
+
* connection its context expects, or as `connection`'s own type without one.
|
|
63
|
+
* That type is the caller's assertion: it holds for the members both copies
|
|
64
|
+
* share, since the sends go by name and the receives are copy-safe.
|
|
65
|
+
*
|
|
66
|
+
* **Where it stops.** Errors: a rejection it produces is still its own copy's
|
|
67
|
+
* `ResponseError`, so recognise errors with `isResponseError`, and build a
|
|
68
|
+
* connection whose errors must keep their code from the framework's copy.
|
|
69
|
+
*
|
|
70
|
+
* See https://github.com/eclipse-emfcloud/hydranium/issues/279.
|
|
71
|
+
*/
|
|
72
|
+
export function sendByMethodName<
|
|
73
|
+
In extends {
|
|
74
|
+
sendRequest<R>(method: string, ...params: unknown[]): Promise<R>;
|
|
75
|
+
sendNotification(method: string, ...params: unknown[]): Promise<void>;
|
|
76
|
+
},
|
|
77
|
+
Out extends {
|
|
78
|
+
sendRequest<R>(method: string, ...params: unknown[]): Promise<R>;
|
|
79
|
+
sendNotification(method: string, ...params: unknown[]): Promise<void>;
|
|
80
|
+
} = In
|
|
81
|
+
>(connection: In): Out {
|
|
82
|
+
const sendRequest = (type: string | TypedMessage, ...params: unknown[]) =>
|
|
83
|
+
typeof type === 'string'
|
|
84
|
+
? connection.sendRequest(type, ...params)
|
|
85
|
+
: connection.sendRequest(type.method, ...argsOf(type, params, true));
|
|
86
|
+
const sendNotification = (type: string | TypedMessage, ...params: unknown[]) =>
|
|
87
|
+
typeof type === 'string'
|
|
88
|
+
? connection.sendNotification(type, ...params)
|
|
89
|
+
: connection.sendNotification(type.method, ...argsOf(type, params, false));
|
|
90
|
+
const wrapped: Pick<In, 'sendRequest' | 'sendNotification'> = new Proxy(connection, {
|
|
91
|
+
get(target, property, receiver) {
|
|
92
|
+
if (property === 'sendRequest') {
|
|
93
|
+
return sendRequest;
|
|
94
|
+
}
|
|
95
|
+
if (property === 'sendNotification') {
|
|
96
|
+
return sendNotification;
|
|
97
|
+
}
|
|
98
|
+
return Reflect.get(target, property, receiver);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
// Typed as the connection its context expects, which the doc above justifies.
|
|
102
|
+
return wrapped as Out;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A typed message as any copy of `vscode-jsonrpc` builds it, read by shape rather than identity. */
|
|
106
|
+
interface TypedMessage {
|
|
107
|
+
readonly method: string;
|
|
108
|
+
readonly numberOfParams: number;
|
|
109
|
+
readonly parameterStructures: { toString(): string };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Whether `vscode-jsonrpc` sends `param` by name under `auto`. */
|
|
113
|
+
function isNamedParam(param: unknown): boolean {
|
|
114
|
+
return param !== undefined && param !== null && !Array.isArray(param) && typeof param === 'object';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The arguments that send `type` by method name with the typed send's params:
|
|
119
|
+
* the first `numberOfParams` of `params`, missing ones `null`, then a request's
|
|
120
|
+
* cancellation token, which the typed send takes from that position. A request
|
|
121
|
+
* always ends in a token, `CancellationToken.None` when it has none, since a
|
|
122
|
+
* send by method name takes a token-shaped last argument for its token. Throws
|
|
123
|
+
* for the one-parameter packing a send by method name cannot express.
|
|
124
|
+
*/
|
|
125
|
+
function argsOf(type: TypedMessage, params: unknown[], isRequest: boolean): unknown[] {
|
|
126
|
+
const args = Array.from({ length: type.numberOfParams }, (_, i) => (i < params.length ? params[i] : null));
|
|
127
|
+
if (type.numberOfParams === 1) {
|
|
128
|
+
const packing = String(type.parameterStructures);
|
|
129
|
+
if ((packing === 'byPosition' && isNamedParam(args[0])) || (packing === 'byName' && !isNamedParam(args[0]))) {
|
|
130
|
+
throw new Error(
|
|
131
|
+
`sendByMethodName cannot send '${type.method}' ${packing}: by method name, a single object goes by name and anything else by position.`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (!isRequest) {
|
|
136
|
+
return args;
|
|
137
|
+
}
|
|
138
|
+
const token = params[type.numberOfParams];
|
|
139
|
+
return [...args, CancellationToken.is(token) ? token : CancellationToken.None];
|
|
140
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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 { RequestType } from 'vscode-jsonrpc';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* LSP's `VersionedTextDocumentIdentifier`, declared field by field: this
|
|
14
|
+
* package carries no LSP dependency, and the fields are LSP's own.
|
|
15
|
+
*/
|
|
16
|
+
export interface SaveTextDocumentParams {
|
|
17
|
+
readonly textDocument: { readonly uri: string; readonly version: number };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Ask the LSP head to persist the text it holds for a document, which it
|
|
22
|
+
* answers with `null` once the text is written. LSP itself has none: its
|
|
23
|
+
* `didSave` reports a write the editor made, so a host whose files only the
|
|
24
|
+
* server holds saves through this.
|
|
25
|
+
*
|
|
26
|
+
* It travels the connection the `didChange`s travel, so it arrives after
|
|
27
|
+
* them. The server refuses it with a `ConflictError` unless the version is the
|
|
28
|
+
* one the editor last declared and its buffer holds the server's current
|
|
29
|
+
* text, which a `workspace/applyEdit` still in flight breaks.
|
|
30
|
+
*
|
|
31
|
+
* A server that answers it advertises it as
|
|
32
|
+
* `ServerCapabilities.experimental[SaveTextDocumentRequest.method] = true`;
|
|
33
|
+
* a client checks that before sending, since a server without a handler
|
|
34
|
+
* answers `MethodNotFound`.
|
|
35
|
+
*/
|
|
36
|
+
export namespace SaveTextDocumentRequest {
|
|
37
|
+
export const method = 'hydranium/saveTextDocument';
|
|
38
|
+
export const type = new RequestType<SaveTextDocumentParams, null, void>(method);
|
|
39
|
+
}
|
|
@@ -33,15 +33,19 @@
|
|
|
33
33
|
|
|
34
34
|
import { Emitter, type MessageConnection } from 'vscode-jsonrpc';
|
|
35
35
|
import type { DataPort } from '../client/data-port';
|
|
36
|
+
import { DATA_CLIENT_PROTOCOL_METHODS } from '../data/data-protocol-methods';
|
|
36
37
|
import type { DataClientProtocol } from '../data/data-server-protocol';
|
|
37
38
|
import type {
|
|
38
39
|
ProjectsChangedEvent,
|
|
39
40
|
TransferDocumentDeletedEvent,
|
|
41
|
+
TransferDocumentDirtyChangedEvent,
|
|
40
42
|
TransferDocumentSavedEvent,
|
|
41
43
|
TransferDocumentsBuiltEvent,
|
|
42
44
|
TransferDocumentUpdatedEvent
|
|
43
45
|
} from '../data/events';
|
|
46
|
+
import type { Logger } from '../logger';
|
|
44
47
|
import type { ResolvedMessage } from '../messages/primitives';
|
|
48
|
+
import type { RpcProxyLifecycle } from '../rpc/create-rpc-proxy';
|
|
45
49
|
import type { Project } from '../project';
|
|
46
50
|
import type { TransferDiagnostic } from '../transfer-diagnostic';
|
|
47
51
|
import type { TransferElement } from '../transfer-element';
|
|
@@ -56,15 +60,14 @@ export interface FakeDataPortOptions {
|
|
|
56
60
|
*
|
|
57
61
|
* Throwing (or rejecting) here is the transport-construction failure path,
|
|
58
62
|
* which the consumer surfaces through {@link FakeDataPort.reported}.
|
|
63
|
+
*
|
|
64
|
+
* The double disposes none of these connections and goes on connecting
|
|
65
|
+
* after its dispose, where a real port releases its connection and refuses:
|
|
66
|
+
* the test owns them.
|
|
59
67
|
*/
|
|
60
68
|
connect(): MessageConnection | Promise<MessageConnection>;
|
|
61
|
-
/**
|
|
62
|
-
|
|
63
|
-
* three sentinels the framework reserves (`'language-client'`, `'unknown'`,
|
|
64
|
-
* `'revert-on-close'`); override it when a test needs two distinguishable
|
|
65
|
-
* clients on one server.
|
|
66
|
-
*/
|
|
67
|
-
clientId?: string;
|
|
69
|
+
/** The port's {@link DataPort.logger}. */
|
|
70
|
+
logger?: Logger;
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
/** A {@link DataPort} that records what passed through it. */
|
|
@@ -107,9 +110,9 @@ export function makeFakeDataPort(options: FakeDataPortOptions): FakeDataPort {
|
|
|
107
110
|
const reported: { error: unknown; message: ResolvedMessage }[] = [];
|
|
108
111
|
const disposeEmitter = new Emitter<void>();
|
|
109
112
|
return {
|
|
110
|
-
clientId: options.clientId ?? 'fake-data-port',
|
|
111
113
|
connections,
|
|
112
114
|
reported,
|
|
115
|
+
logger: options.logger,
|
|
113
116
|
onDispose: disposeEmitter.event,
|
|
114
117
|
async connect(): Promise<MessageConnection> {
|
|
115
118
|
const connection = await options.connect();
|
|
@@ -128,24 +131,72 @@ export function makeFakeDataPort(options: FakeDataPortOptions): FakeDataPort {
|
|
|
128
131
|
};
|
|
129
132
|
}
|
|
130
133
|
|
|
134
|
+
type UpperLetter =
|
|
135
|
+
| 'A'
|
|
136
|
+
| 'B'
|
|
137
|
+
| 'C'
|
|
138
|
+
| 'D'
|
|
139
|
+
| 'E'
|
|
140
|
+
| 'F'
|
|
141
|
+
| 'G'
|
|
142
|
+
| 'H'
|
|
143
|
+
| 'I'
|
|
144
|
+
| 'J'
|
|
145
|
+
| 'K'
|
|
146
|
+
| 'L'
|
|
147
|
+
| 'M'
|
|
148
|
+
| 'N'
|
|
149
|
+
| 'O'
|
|
150
|
+
| 'P'
|
|
151
|
+
| 'Q'
|
|
152
|
+
| 'R'
|
|
153
|
+
| 'S'
|
|
154
|
+
| 'T'
|
|
155
|
+
| 'U'
|
|
156
|
+
| 'V'
|
|
157
|
+
| 'W'
|
|
158
|
+
| 'X'
|
|
159
|
+
| 'Y'
|
|
160
|
+
| 'Z';
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* A notification name `TClient` adds to {@link DataClientProtocol}. Limited to
|
|
164
|
+
* `on` plus a letter A–Z, the names the proxy sends as notifications; any other
|
|
165
|
+
* name would be bound as a request. The {@link RpcProxyLifecycle} names are
|
|
166
|
+
* excluded too: the proxy returns its own events for them and sends nothing.
|
|
167
|
+
*/
|
|
168
|
+
export type AdditionalClientMethod<TClient> = Exclude<keyof TClient, keyof DataClientProtocol<TransferElement> | keyof RpcProxyLifecycle> &
|
|
169
|
+
`on${UpperLetter}${string}`;
|
|
170
|
+
|
|
131
171
|
/** A {@link DataClientProtocol} plus the arrays it records into. */
|
|
132
172
|
export interface CapturingDataClient<
|
|
133
173
|
TTransfer extends TransferElement,
|
|
134
174
|
TDiagnostic extends TransferDiagnostic = TransferDiagnostic,
|
|
135
|
-
TProject extends Project = Project
|
|
175
|
+
TProject extends Project = Project,
|
|
176
|
+
TClient extends DataClientProtocol<TTransfer, TDiagnostic, TProject> = DataClientProtocol<TTransfer, TDiagnostic, TProject>
|
|
136
177
|
> {
|
|
137
178
|
/** The client to hand to a `DataSession` or bind as an RPC `localTarget`. */
|
|
138
|
-
readonly client:
|
|
179
|
+
readonly client: TClient;
|
|
139
180
|
/** Every `onDocumentUpdated` event, in arrival order. */
|
|
140
181
|
readonly updates: TransferDocumentUpdatedEvent<TTransfer, TDiagnostic>[];
|
|
141
182
|
/** Every `onDocumentSaved` event, in arrival order. */
|
|
142
183
|
readonly saves: TransferDocumentSavedEvent<TTransfer, TDiagnostic>[];
|
|
184
|
+
/** Every `onDocumentDirtyChanged` event, in arrival order. */
|
|
185
|
+
readonly dirtyChanges: TransferDocumentDirtyChangedEvent[];
|
|
143
186
|
/** Every `onDocumentDeleted` event, in arrival order. */
|
|
144
187
|
readonly deletions: TransferDocumentDeletedEvent[];
|
|
145
188
|
/** Every `onDocumentsBuilt` event, in arrival order. */
|
|
146
189
|
readonly builds: TransferDocumentsBuiltEvent[];
|
|
147
190
|
/** Every `onProjectsChanged` event, in arrival order. */
|
|
148
191
|
readonly projectsChanges: ProjectsChangedEvent<TProject>[];
|
|
192
|
+
/**
|
|
193
|
+
* Every event of each additional client method, keyed by method, in arrival
|
|
194
|
+
* order. A method of `TClient` left out of `additionalClientMethods` has no
|
|
195
|
+
* entry.
|
|
196
|
+
*/
|
|
197
|
+
readonly additionalNotifications: {
|
|
198
|
+
readonly [K in AdditionalClientMethod<TClient>]: TClient[K] extends (event: infer TEvent) => void ? TEvent[] : never;
|
|
199
|
+
};
|
|
149
200
|
}
|
|
150
201
|
|
|
151
202
|
/**
|
|
@@ -160,43 +211,92 @@ export interface CapturingDataClient<
|
|
|
160
211
|
* the corresponding array stays empty — that is what makes an override usable
|
|
161
212
|
* as a barrier (rejecting, counting differently, throwing) rather than only as
|
|
162
213
|
* a spy.
|
|
214
|
+
*
|
|
215
|
+
* `additionalClientMethods` names the notifications `TClient` adds to
|
|
216
|
+
* {@link DataClientProtocol}. Each is recorded and overridable the same way;
|
|
217
|
+
* one left out is not on the client at all, and overriding it throws, since
|
|
218
|
+
* nothing would call the override.
|
|
163
219
|
*/
|
|
164
220
|
export function makeCapturingDataClient<
|
|
165
221
|
TTransfer extends TransferElement,
|
|
166
222
|
TDiagnostic extends TransferDiagnostic = TransferDiagnostic,
|
|
167
|
-
TProject extends Project = Project
|
|
168
|
-
|
|
223
|
+
TProject extends Project = Project,
|
|
224
|
+
TClient extends DataClientProtocol<TTransfer, TDiagnostic, TProject> = DataClientProtocol<TTransfer, TDiagnostic, TProject>
|
|
225
|
+
>(
|
|
226
|
+
overrides: Partial<TClient> = {},
|
|
227
|
+
additionalClientMethods: readonly AdditionalClientMethod<TClient>[] = []
|
|
228
|
+
): CapturingDataClient<TTransfer, TDiagnostic, TProject, TClient> {
|
|
229
|
+
const bound = new Set<string>([...DATA_CLIENT_PROTOCOL_METHODS, ...additionalClientMethods]);
|
|
230
|
+
for (const name of Object.keys(overrides)) {
|
|
231
|
+
if (!bound.has(name)) {
|
|
232
|
+
throw new Error(
|
|
233
|
+
`Override '${name}' is neither a DataClientProtocol method nor listed in additionalClientMethods, so nothing would call it.`
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const frameworkOverrides: Partial<DataClientProtocol<TTransfer, TDiagnostic, TProject>> = overrides;
|
|
169
238
|
const updates: TransferDocumentUpdatedEvent<TTransfer, TDiagnostic>[] = [];
|
|
170
239
|
const saves: TransferDocumentSavedEvent<TTransfer, TDiagnostic>[] = [];
|
|
240
|
+
const dirtyChanges: TransferDocumentDirtyChangedEvent[] = [];
|
|
171
241
|
const deletions: TransferDocumentDeletedEvent[] = [];
|
|
172
242
|
const builds: TransferDocumentsBuiltEvent[] = [];
|
|
173
243
|
const projectsChanges: ProjectsChangedEvent<TProject>[] = [];
|
|
174
|
-
const
|
|
244
|
+
const frameworkClient: DataClientProtocol<TTransfer, TDiagnostic, TProject> = {
|
|
175
245
|
onDocumentUpdated:
|
|
176
|
-
|
|
246
|
+
frameworkOverrides.onDocumentUpdated ??
|
|
177
247
|
(event => {
|
|
178
248
|
updates.push(event);
|
|
179
249
|
}),
|
|
180
250
|
onDocumentSaved:
|
|
181
|
-
|
|
251
|
+
frameworkOverrides.onDocumentSaved ??
|
|
182
252
|
(event => {
|
|
183
253
|
saves.push(event);
|
|
184
254
|
}),
|
|
255
|
+
onDocumentDirtyChanged:
|
|
256
|
+
frameworkOverrides.onDocumentDirtyChanged ??
|
|
257
|
+
(event => {
|
|
258
|
+
dirtyChanges.push(event);
|
|
259
|
+
}),
|
|
185
260
|
onDocumentDeleted:
|
|
186
|
-
|
|
261
|
+
frameworkOverrides.onDocumentDeleted ??
|
|
187
262
|
(event => {
|
|
188
263
|
deletions.push(event);
|
|
189
264
|
}),
|
|
190
265
|
onDocumentsBuilt:
|
|
191
|
-
|
|
266
|
+
frameworkOverrides.onDocumentsBuilt ??
|
|
192
267
|
(event => {
|
|
193
268
|
builds.push(event);
|
|
194
269
|
}),
|
|
195
270
|
onProjectsChanged:
|
|
196
|
-
|
|
271
|
+
frameworkOverrides.onProjectsChanged ??
|
|
197
272
|
(event => {
|
|
198
273
|
projectsChanges.push(event);
|
|
199
274
|
})
|
|
200
275
|
};
|
|
201
|
-
|
|
276
|
+
const additionalNotifications: Record<string, unknown[]> = {};
|
|
277
|
+
const additionalHandlers: Record<string, unknown> = {};
|
|
278
|
+
for (const method of additionalClientMethods) {
|
|
279
|
+
const events: unknown[] = [];
|
|
280
|
+
additionalNotifications[method] = events;
|
|
281
|
+
additionalHandlers[method] =
|
|
282
|
+
overrides[method] ??
|
|
283
|
+
((event: unknown) => {
|
|
284
|
+
events.push(event);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
client: { ...frameworkClient, ...additionalHandlers } as unknown as TClient,
|
|
289
|
+
updates,
|
|
290
|
+
saves,
|
|
291
|
+
dirtyChanges,
|
|
292
|
+
deletions,
|
|
293
|
+
builds,
|
|
294
|
+
projectsChanges,
|
|
295
|
+
additionalNotifications: additionalNotifications as CapturingDataClient<
|
|
296
|
+
TTransfer,
|
|
297
|
+
TDiagnostic,
|
|
298
|
+
TProject,
|
|
299
|
+
TClient
|
|
300
|
+
>['additionalNotifications']
|
|
301
|
+
};
|
|
202
302
|
}
|