@hydranium/protocol 1.0.0-next.8 → 1.0.0-next.85
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 +35 -1
- package/lib/client/data-connection.d.ts +114 -0
- package/lib/client/data-connection.d.ts.map +1 -0
- package/lib/client/data-connection.js +128 -0
- package/lib/client/data-connection.js.map +1 -0
- package/lib/client/data-events.d.ts +9 -1
- package/lib/client/data-events.d.ts.map +1 -1
- package/lib/client/data-events.js +14 -0
- package/lib/client/data-events.js.map +1 -1
- package/lib/client/data-port.d.ts +17 -22
- package/lib/client/data-port.d.ts.map +1 -1
- package/lib/client/data-session.d.ts +95 -78
- package/lib/client/data-session.d.ts.map +1 -1
- package/lib/client/data-session.js +115 -108
- package/lib/client/data-session.js.map +1 -1
- package/lib/client/index.d.ts +10 -7
- package/lib/client/index.d.ts.map +1 -1
- package/lib/client/index.js +10 -7
- package/lib/client/index.js.map +1 -1
- package/lib/client/message-relay.d.ts +8 -2
- package/lib/client/message-relay.d.ts.map +1 -1
- package/lib/client/message-relay.js +10 -4
- package/lib/client/message-relay.js.map +1 -1
- package/lib/client/rpc-connection.d.ts +139 -0
- package/lib/client/rpc-connection.d.ts.map +1 -0
- package/lib/client/rpc-connection.js +171 -0
- package/lib/client/rpc-connection.js.map +1 -0
- package/lib/client-ids.d.ts +41 -0
- package/lib/client-ids.d.ts.map +1 -0
- package/lib/client-ids.js +44 -0
- package/lib/client-ids.js.map +1 -0
- package/lib/data/data-protocol-methods.d.ts +2 -2
- package/lib/data/data-protocol-methods.d.ts.map +1 -1
- package/lib/data/data-protocol-methods.js +6 -1
- package/lib/data/data-protocol-methods.js.map +1 -1
- package/lib/data/data-server-protocol.d.ts +35 -2
- package/lib/data/data-server-protocol.d.ts.map +1 -1
- package/lib/data/events.d.ts +70 -3
- package/lib/data/events.d.ts.map +1 -1
- package/lib/errors.d.ts +28 -11
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +35 -17
- package/lib/errors.js.map +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +5 -0
- package/lib/index.js.map +1 -1
- package/lib/messages/index.d.ts +28 -0
- package/lib/messages/index.d.ts.map +1 -0
- package/lib/messages/index.js +52 -0
- package/lib/messages/index.js.map +1 -0
- package/lib/messages/primitives.d.ts +141 -0
- package/lib/messages/primitives.d.ts.map +1 -0
- package/lib/messages/primitives.js +138 -0
- package/lib/messages/primitives.js.map +1 -0
- package/lib/model-server.d.ts +6 -5
- package/lib/model-server.d.ts.map +1 -1
- package/lib/model-service/args.d.ts +14 -13
- package/lib/model-service/args.d.ts.map +1 -1
- package/lib/model-service/based-on.d.ts +55 -0
- package/lib/model-service/based-on.d.ts.map +1 -0
- package/lib/model-service/based-on.js +34 -0
- package/lib/model-service/based-on.js.map +1 -0
- package/lib/model-service/index.d.ts +1 -0
- package/lib/model-service/index.d.ts.map +1 -1
- package/lib/model-service/index.js +1 -0
- package/lib/model-service/index.js.map +1 -1
- package/lib/rpc/bind-rpc-methods.d.ts +29 -3
- package/lib/rpc/bind-rpc-methods.d.ts.map +1 -1
- package/lib/rpc/bind-rpc-methods.js +22 -3
- package/lib/rpc/bind-rpc-methods.js.map +1 -1
- package/lib/rpc/create-rpc-proxy.d.ts +7 -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/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 +11 -12
- package/lib/testing/data-doubles.d.ts.map +1 -1
- package/lib/testing/data-doubles.js +14 -5
- package/lib/testing/data-doubles.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 +4 -1
- package/lib/testing/index.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 +15 -5
- package/lib/transfer-document.d.ts.map +1 -1
- package/lib/transfer-document.js +14 -1
- package/lib/transfer-document.js.map +1 -1
- package/package.json +11 -2
- package/src/client/data-connection.ts +181 -0
- package/src/client/data-events.ts +24 -1
- package/src/client/data-port.ts +17 -23
- package/src/client/data-session.ts +172 -131
- package/src/client/index.ts +10 -7
- package/src/client/message-relay.ts +28 -6
- package/src/client/rpc-connection.ts +230 -0
- package/src/client-ids.ts +45 -0
- package/src/data/data-protocol-methods.ts +6 -3
- package/src/data/data-server-protocol.ts +46 -2
- package/src/data/events.ts +74 -3
- package/src/errors.ts +41 -19
- package/src/index.ts +5 -0
- package/src/messages/index.ts +35 -0
- package/src/messages/primitives.ts +215 -0
- package/src/model-server.ts +6 -5
- package/src/model-service/args.ts +15 -13
- package/src/model-service/based-on.ts +60 -0
- package/src/model-service/index.ts +1 -0
- package/src/rpc/bind-rpc-methods.ts +49 -4
- package/src/rpc/create-rpc-proxy.ts +14 -1
- package/src/testing/catalogue-audit.ts +111 -0
- package/src/testing/data-doubles.ts +33 -17
- package/src/testing/index.ts +4 -1
- package/src/transfer-diagnostic.ts +40 -0
- package/src/transfer-document.ts +22 -6
|
@@ -7,106 +7,120 @@
|
|
|
7
7
|
* SPDX-License-Identifier: MIT
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
|
-
import type {
|
|
11
|
-
import
|
|
12
|
-
import { type RpcProxy, createRpcProxy } from '../rpc';
|
|
10
|
+
import type { DataServerProtocol, DiagnosticOf } from '../data';
|
|
11
|
+
import type { RpcProxy } from '../rpc';
|
|
13
12
|
import type { TransferDocument } from '../transfer-document';
|
|
14
13
|
import type { TransferElement } from '../transfer-element';
|
|
15
|
-
import type { DataPort } from './data-port';
|
|
16
14
|
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
15
|
+
/**
|
|
16
|
+
* What one of `TServer`'s document methods takes, minus the `clientId` a
|
|
17
|
+
* {@link DataSession} stamps itself.
|
|
18
|
+
*
|
|
19
|
+
* Read off the SERVER's signature, not off the framework's own arg type: an
|
|
20
|
+
* adopter server widens these, and a wrapper declared against the narrow
|
|
21
|
+
* shape rejects the extra field on a fresh object literal, so that call
|
|
22
|
+
* cannot go through a session at all.
|
|
23
|
+
*/
|
|
24
|
+
export type DataSessionArgs<TMethod extends (args: never) => unknown> = Omit<Parameters<TMethod>[0], 'clientId'>;
|
|
25
|
+
|
|
26
|
+
/** Open a document through a session; the session supplies `clientId`. */
|
|
27
|
+
export type DataSessionOpenArgs<
|
|
28
|
+
TTransfer extends TransferElement,
|
|
29
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
30
|
+
> = DataSessionArgs<TServer['openModelDocument']>;
|
|
28
31
|
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
/** Close a document through a session; the session supplies `clientId`. */
|
|
33
|
+
export type DataSessionCloseArgs<
|
|
34
|
+
TTransfer extends TransferElement,
|
|
35
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
36
|
+
> = DataSessionArgs<TServer['closeModelDocument']>;
|
|
37
|
+
|
|
38
|
+
/** Update a document through a session; the session supplies `clientId`. */
|
|
39
|
+
export type DataSessionUpdateArgs<
|
|
40
|
+
TTransfer extends TransferElement,
|
|
41
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
42
|
+
> = DataSessionArgs<TServer['updateModelDocument']>;
|
|
43
|
+
|
|
44
|
+
/** Persist a document through a session; the session supplies `clientId`. */
|
|
45
|
+
export type DataSessionSaveArgs<
|
|
46
|
+
TTransfer extends TransferElement,
|
|
47
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
48
|
+
> = DataSessionArgs<TServer['saveModelDocument']>;
|
|
49
|
+
|
|
50
|
+
/** The document a session hands back, carrying its server's diagnostic shape. */
|
|
51
|
+
export type DataSessionDocument<
|
|
52
|
+
TTransfer extends TransferElement,
|
|
53
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
54
|
+
> = TransferDocument<TTransfer, DiagnosticOf<TServer>>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* What a {@link DataSession} needs from the connection that minted it.
|
|
58
|
+
*
|
|
59
|
+
* Narrower than the connection itself so the dependency points one way:
|
|
60
|
+
* `DataConnection` constructs sessions, and nothing here imports it back.
|
|
61
|
+
*/
|
|
62
|
+
export interface DataSessionHost<TTransfer extends TransferElement, TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>>> {
|
|
63
|
+
connected(): Promise<RpcProxy<TServer>>;
|
|
64
|
+
releaseSession(session: DataSession<TTransfer, TServer>): void;
|
|
35
65
|
}
|
|
36
66
|
|
|
37
67
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* adapter.
|
|
68
|
+
* One participant on a data connection: a properties panel, a tree, a
|
|
69
|
+
* form editor.
|
|
41
70
|
*
|
|
42
|
-
*
|
|
71
|
+
* The server keys every hold and watch per `(uri, clientId)`, so the identity
|
|
72
|
+
* belongs to the participant rather than to the wire — several sessions share
|
|
73
|
+
* one connection, and two participants forced to share one identity cannot
|
|
74
|
+
* distinguish each other's writes from their own echoes.
|
|
43
75
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
* across concurrent callers. A socket client can connect before the
|
|
48
|
-
* workspace walk finishes, and an early request is then answered correctly
|
|
49
|
-
* from an empty registry, which reads as a broken project tier rather than
|
|
50
|
-
* as a race.
|
|
51
|
-
* 3. **Own the reconnect policy**, by dropping its connection generation when
|
|
52
|
-
* the port disposes and building a fresh one on the next request.
|
|
76
|
+
* Every document operation stamps {@link clientId} itself. A caller that
|
|
77
|
+
* passed its own could pass another participant's, and the server would
|
|
78
|
+
* attribute the write and release the hold accordingly.
|
|
53
79
|
*
|
|
54
|
-
*
|
|
55
|
-
* {@link connected}. The one exception is {@link openDocument}, which exists
|
|
56
|
-
* because the open/watch *order* is silently wrong the other way round — see
|
|
57
|
-
* its own doc.
|
|
80
|
+
* Generic over the transfer root so this file names no grammar.
|
|
58
81
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
82
|
+
* `TServer` is bound to a server answering with ITS OWN diagnostic shape, read
|
|
83
|
+
* back off the parameter being bound. Simplifying that to
|
|
84
|
+
* `DataServerProtocol<TTransfer>` compiles and costs the wrappers their
|
|
85
|
+
* return type: every call through `TServer` would resolve against that looser
|
|
86
|
+
* bound, so an adopter's diagnostics would come back as the framework's and
|
|
87
|
+
* the document would have to be cast on the way out.
|
|
62
88
|
*/
|
|
63
|
-
export class DataSession<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
89
|
+
export class DataSession<
|
|
90
|
+
TTransfer extends TransferElement,
|
|
91
|
+
TServer extends DataServerProtocol<TTransfer, DiagnosticOf<TServer>> = DataServerProtocol<TTransfer>
|
|
92
|
+
> {
|
|
93
|
+
/** URIs this session holds open, so {@link dispose} can release exactly those. */
|
|
94
|
+
protected readonly openUris = new Set<string>();
|
|
67
95
|
protected disposed = false;
|
|
68
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Disposed through {@link detach} rather than {@link dispose}, so nothing may
|
|
98
|
+
* be sent. Folding it into {@link disposed} costs {@link openDocument} the
|
|
99
|
+
* distinction, and its close would then go out on a detached session.
|
|
100
|
+
*/
|
|
101
|
+
protected detached = false;
|
|
69
102
|
|
|
70
103
|
constructor(
|
|
71
|
-
|
|
72
|
-
protected readonly
|
|
73
|
-
|
|
74
|
-
) {
|
|
75
|
-
this.methodNamespace = options.methodNamespace ?? DATA_SERVER_WIRE_PREFIX;
|
|
76
|
-
this.portDisposeListener = this.port.onDispose(() => this.dropGeneration());
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** The identity every request is made under — the port's, not a second one. */
|
|
80
|
-
get clientId(): string {
|
|
81
|
-
return this.port.clientId;
|
|
82
|
-
}
|
|
104
|
+
readonly clientId: string,
|
|
105
|
+
protected readonly host: DataSessionHost<TTransfer, TServer>
|
|
106
|
+
) {}
|
|
83
107
|
|
|
84
108
|
/**
|
|
85
|
-
* The connected, READY server proxy
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* proxy, so a caller that cached one from an earlier call would go on
|
|
89
|
-
* addressing a dead connection with no error — handing it back per call
|
|
90
|
-
* makes the stale reference unrepresentable.
|
|
91
|
-
*
|
|
92
|
-
* Concurrent callers share one readiness promise, so `waitForReady` is
|
|
93
|
-
* awaited once per generation and not once per caller.
|
|
109
|
+
* The connected, READY server proxy, for protocol methods this session does
|
|
110
|
+
* not wrap — the ones carrying no `clientId`, so no identity can be got
|
|
111
|
+
* wrong through them.
|
|
94
112
|
*/
|
|
95
|
-
async connected(): Promise<RpcProxy<
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
generation.ready = this.awaitReady(generation);
|
|
102
|
-
}
|
|
103
|
-
await generation.ready;
|
|
104
|
-
return generation.server;
|
|
113
|
+
async connected(): Promise<RpcProxy<TServer>> {
|
|
114
|
+
// `async` so a disposed session REJECTS rather than throwing
|
|
115
|
+
// synchronously: the connection's own `connected` rejects, and a caller
|
|
116
|
+
// reaching for `.catch` on one of them would not catch the other.
|
|
117
|
+
this.assertLive();
|
|
118
|
+
return this.host.connected();
|
|
105
119
|
}
|
|
106
120
|
|
|
107
121
|
/**
|
|
108
|
-
* Open `uri` for editing and start watching it, in that order,
|
|
109
|
-
* the opened snapshot.
|
|
122
|
+
* Open `args.uri` for editing and start watching it, in that order,
|
|
123
|
+
* returning the opened snapshot.
|
|
110
124
|
*
|
|
111
125
|
* **The order is the whole reason this method exists.**
|
|
112
126
|
* `watchModelDocument` baselines its dedup fingerprint from the *current*
|
|
@@ -121,25 +135,66 @@ export class DataSession<TTransfer extends TransferElement> {
|
|
|
121
135
|
* valid: `open` settles at the integrity landmark, not at validation.
|
|
122
136
|
* Validity arrives asynchronously on `onDocumentUpdated`, or synchronously
|
|
123
137
|
* from `getModelDocument({ includeDiagnostics: true })`.
|
|
138
|
+
*
|
|
139
|
+
* A dispose landing between the two awaits is handled here rather than in
|
|
140
|
+
* {@link dispose}, which cannot release a hold that does not exist yet: it
|
|
141
|
+
* reads {@link openUris}, and this method fills it only once both calls have
|
|
142
|
+
* returned. Left to `dispose`, the hold and its watch outlive the participant
|
|
143
|
+
* and go only when the connection closes.
|
|
124
144
|
*/
|
|
125
|
-
async openDocument(
|
|
145
|
+
async openDocument(args: DataSessionOpenArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
|
|
126
146
|
const server = await this.connected();
|
|
127
|
-
const document = await server.openModelDocument({
|
|
128
|
-
await server.watchModelDocument({ uri, clientId: this.clientId });
|
|
147
|
+
const document = await server.openModelDocument({ ...args, clientId: this.clientId });
|
|
148
|
+
await server.watchModelDocument({ uri: args.uri, clientId: this.clientId });
|
|
149
|
+
if (this.disposed) {
|
|
150
|
+
// Registering the URI BEFORE the open, so `dispose` could close on
|
|
151
|
+
// intent, is NOT the same fix and can invert into the leak it is meant
|
|
152
|
+
// to prevent: the close would then be issued while the open is still in
|
|
153
|
+
// flight, and a peer that does not serialise the two can complete the
|
|
154
|
+
// close first, after which the open re-registers the hold. Closing
|
|
155
|
+
// strictly after the open has resolved is the only ordering with no
|
|
156
|
+
// losing interleaving.
|
|
157
|
+
if (!this.detached) {
|
|
158
|
+
void server.closeModelDocument({ uri: args.uri, clientId: this.clientId }).catch(() => undefined);
|
|
159
|
+
}
|
|
160
|
+
// Returned rather than thrown: the caller that reaches this window is
|
|
161
|
+
// one that never awaited the open, so a rejection here surfaces as an
|
|
162
|
+
// unhandled one against a participant already gone.
|
|
163
|
+
return document;
|
|
164
|
+
}
|
|
165
|
+
this.openUris.add(args.uri);
|
|
129
166
|
return document;
|
|
130
167
|
}
|
|
131
168
|
|
|
132
169
|
/**
|
|
133
|
-
* Close `uri`. The server unwatches implicitly, so this is the dual of
|
|
170
|
+
* Close `args.uri`. The server unwatches implicitly, so this is the dual of
|
|
134
171
|
* {@link openDocument} and needs no separate unwatch.
|
|
135
172
|
*/
|
|
136
|
-
async closeDocument(
|
|
173
|
+
async closeDocument(args: DataSessionCloseArgs<TTransfer, TServer>): Promise<void> {
|
|
137
174
|
const server = await this.connected();
|
|
138
|
-
await server.closeModelDocument({
|
|
175
|
+
await server.closeModelDocument({ ...args, clientId: this.clientId });
|
|
176
|
+
// Untracked only once the close has actually landed. Dropping it first
|
|
177
|
+
// gives a FAILED close the same effect as a successful one: the hold
|
|
178
|
+
// survives on the server and `dispose` no longer knows to retry it.
|
|
179
|
+
// A `dispose` racing this therefore closes the same URI twice, which the
|
|
180
|
+
// server answers as a no-op for a client that no longer holds it.
|
|
181
|
+
this.openUris.delete(args.uri);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Write `args.model` back as this session. */
|
|
185
|
+
async updateDocument(args: DataSessionUpdateArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
|
|
186
|
+
const server = await this.connected();
|
|
187
|
+
return server.updateModelDocument({ ...args, clientId: this.clientId });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Persist `args.model` to disk as this session. */
|
|
191
|
+
async saveDocument(args: DataSessionSaveArgs<TTransfer, TServer>): Promise<DataSessionDocument<TTransfer, TServer>> {
|
|
192
|
+
const server = await this.connected();
|
|
193
|
+
return server.saveModelDocument({ ...args, clientId: this.clientId });
|
|
139
194
|
}
|
|
140
195
|
|
|
141
196
|
/**
|
|
142
|
-
* Whether `
|
|
197
|
+
* Whether `sourceClientId` identifies this session's own write.
|
|
143
198
|
*
|
|
144
199
|
* Every watcher needs this and the check is one comparison, so getting it
|
|
145
200
|
* wrong is cheap to do and expensive to find: an unfiltered echo looks
|
|
@@ -149,61 +204,47 @@ export class DataSession<TTransfer extends TransferElement> {
|
|
|
149
204
|
return sourceClientId === this.clientId;
|
|
150
205
|
}
|
|
151
206
|
|
|
152
|
-
/**
|
|
207
|
+
/**
|
|
208
|
+
* Release this session's holds and detach it from the connection.
|
|
209
|
+
* Idempotent, and leaves the connection usable by its other sessions.
|
|
210
|
+
*
|
|
211
|
+
* The closes are fired without being awaited, because a `Disposable` cannot
|
|
212
|
+
* be: a host disposing a widget has nowhere to put the promise. A close
|
|
213
|
+
* that fails is no worse than the leak this exists to prevent, so the
|
|
214
|
+
* rejection is swallowed rather than surfaced from a teardown.
|
|
215
|
+
*/
|
|
153
216
|
dispose(): void {
|
|
154
217
|
if (this.disposed) {
|
|
155
218
|
return;
|
|
156
219
|
}
|
|
157
220
|
this.disposed = true;
|
|
158
|
-
this.
|
|
159
|
-
this.
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
const connection = this.port.connect();
|
|
168
|
-
// Rejection is reported here rather than left to float: an unhandled
|
|
169
|
-
// rejection on a connection promise is the failure mode that reads as
|
|
170
|
-
// "the model is empty" instead of "the transport never opened".
|
|
171
|
-
connection.catch((error: unknown) => this.port.reportError(error, 'connecting to the data server'));
|
|
172
|
-
const server = createRpcProxy<DataServerProtocol<TTransfer>, DataClientProtocol<TTransfer>>(connection, {
|
|
173
|
-
methodNamespace: this.methodNamespace,
|
|
174
|
-
localTarget: this.client,
|
|
175
|
-
localMethods: DATA_CLIENT_PROTOCOL_METHODS
|
|
176
|
-
});
|
|
177
|
-
this.generation = { connection, server };
|
|
178
|
-
return this.generation;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/** Await the connection and the server's startup gate for one generation. */
|
|
182
|
-
protected async awaitReady(generation: Generation<TTransfer>): Promise<void> {
|
|
183
|
-
try {
|
|
184
|
-
await generation.connection;
|
|
185
|
-
await generation.server.waitForReady();
|
|
186
|
-
} catch (error: unknown) {
|
|
187
|
-
// Drop the generation so the next request retries rather than
|
|
188
|
-
// re-awaiting a settled rejection forever.
|
|
189
|
-
if (this.generation === generation) {
|
|
190
|
-
this.generation = undefined;
|
|
191
|
-
}
|
|
192
|
-
this.port.reportError(error, 'waiting for the data server to become ready');
|
|
193
|
-
throw error;
|
|
221
|
+
const uris = [...this.openUris];
|
|
222
|
+
this.openUris.clear();
|
|
223
|
+
this.host.releaseSession(this);
|
|
224
|
+
for (const uri of uris) {
|
|
225
|
+
void this.host
|
|
226
|
+
.connected()
|
|
227
|
+
.then(server => server.closeModelDocument({ uri, clientId: this.clientId }))
|
|
228
|
+
.catch(() => undefined);
|
|
194
229
|
}
|
|
195
230
|
}
|
|
196
231
|
|
|
197
232
|
/**
|
|
198
|
-
*
|
|
199
|
-
*
|
|
233
|
+
* Come off the connection because it is going away.
|
|
234
|
+
*
|
|
235
|
+
* Sends no close, unlike {@link dispose}: the server releases every hold on
|
|
236
|
+
* a connection it sees close, and the close would travel over the very
|
|
237
|
+
* connection being disposed.
|
|
200
238
|
*/
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
this.
|
|
204
|
-
|
|
205
|
-
|
|
239
|
+
detach(): void {
|
|
240
|
+
this.disposed = true;
|
|
241
|
+
this.detached = true;
|
|
242
|
+
this.openUris.clear();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
protected assertLive(): void {
|
|
246
|
+
if (this.disposed) {
|
|
247
|
+
throw new Error('DataSession is disposed');
|
|
206
248
|
}
|
|
207
|
-
generation.connection.then(connection => connection.dispose()).catch(() => undefined);
|
|
208
249
|
}
|
|
209
250
|
}
|
package/src/client/index.ts
CHANGED
|
@@ -13,8 +13,9 @@
|
|
|
13
13
|
*
|
|
14
14
|
* Where `./data` is the wire *contract* and `./rpc` is the machinery that lowers
|
|
15
15
|
* it onto a connection, this is what a client wraps around both: the seam a host
|
|
16
|
-
* fills in (`DataPort`), the
|
|
17
|
-
*
|
|
16
|
+
* fills in (`DataPort`), the connection above it (`DataConnection` — readiness
|
|
17
|
+
* gate and reconnect), the participants on that connection (`DataSession` —
|
|
18
|
+
* identity, open/watch ordering, echo recognition), the inbound
|
|
18
19
|
* fan-out (`DataEvents`), and the two halves of the hop for hosts whose
|
|
19
20
|
* client cannot hold a socket — `createPostMessageTransport` on the client
|
|
20
21
|
* side and `relayToPostMessageChannel` on the side that does hold it.
|
|
@@ -26,15 +27,17 @@
|
|
|
26
27
|
* carries the entries.
|
|
27
28
|
*
|
|
28
29
|
* The Theia-specific mounting of the same contract lives in
|
|
29
|
-
* `@hydranium/data-client-theia
|
|
30
|
-
*
|
|
31
|
-
* is the Theia-bound counterpart of `DataEvents`.
|
|
32
|
-
*
|
|
33
|
-
*
|
|
30
|
+
* `@hydranium/data-client-theia`, and is now only the TRANSPORT: its
|
|
31
|
+
* `ChannelDataPort` fills the `DataPort` seam over a Theia channel, and its
|
|
32
|
+
* `EmitterDataClient` is the Theia-bound counterpart of `DataEvents`.
|
|
33
|
+
* Everything above the port is here, so a Theia frontend and a browser page
|
|
34
|
+
* differ by which port they construct and nothing else.
|
|
34
35
|
*/
|
|
35
36
|
|
|
37
|
+
export * from './data-connection';
|
|
36
38
|
export * from './data-events';
|
|
37
39
|
export * from './data-port';
|
|
38
40
|
export * from './data-session';
|
|
39
41
|
export * from './message-relay';
|
|
40
42
|
export * from './post-message-transport';
|
|
43
|
+
export * from './rpc-connection';
|
|
@@ -8,8 +8,29 @@
|
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
10
|
import { Emitter, type Disposable, type Event, type Message, type MessageReader, type MessageWriter } from 'vscode-jsonrpc';
|
|
11
|
+
import { defineMessage, describeError, resolve, type ResolvedMessage } from '../messages/primitives';
|
|
11
12
|
import type { PostMessageChannel } from './post-message-transport';
|
|
12
13
|
|
|
14
|
+
export const RELAY_TRANSPORT_OPEN_FAILED = defineMessage(
|
|
15
|
+
'hydranium/protocol/relay-transport-open-failed',
|
|
16
|
+
'Could not open the transport to relay: {detail}'
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
export const RELAY_TRANSPORT_READ_FAILED = defineMessage(
|
|
20
|
+
'hydranium/protocol/relay-transport-read-failed',
|
|
21
|
+
'Could not read from the relayed transport: {detail}'
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
export const RELAY_TRANSPORT_WRITE_FAILED = defineMessage(
|
|
25
|
+
'hydranium/protocol/relay-transport-write-failed',
|
|
26
|
+
'Could not write to the relayed transport: {detail}'
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
export const RELAY_REPLAY_FAILED = defineMessage(
|
|
30
|
+
'hydranium/protocol/relay-replay-failed',
|
|
31
|
+
'Could not replay a buffered message to the relayed transport: {detail}'
|
|
32
|
+
);
|
|
33
|
+
|
|
13
34
|
/**
|
|
14
35
|
* The framed side of a relay: the reader/writer pair over whatever transport the
|
|
15
36
|
* host actually holds — a TCP socket to the data-server, a child process' stdio,
|
|
@@ -30,13 +51,14 @@ export interface RelayTransport {
|
|
|
30
51
|
export interface MessageRelayOptions {
|
|
31
52
|
/**
|
|
32
53
|
* Surface a failure the way the host does. Same contract as
|
|
33
|
-
* `DataPort.reportError`: `
|
|
54
|
+
* `DataPort.reportError`: `reported` is a complete sentence plus the identity
|
|
55
|
+
* needed to render it in another language.
|
|
34
56
|
*
|
|
35
57
|
* A relay has no other way to report — it sits between two transports and
|
|
36
58
|
* owns neither, so a swallowed error here presents as a form that never
|
|
37
59
|
* populates.
|
|
38
60
|
*/
|
|
39
|
-
readonly reportError?: (error: unknown,
|
|
61
|
+
readonly reportError?: (error: unknown, reported: ResolvedMessage) => void;
|
|
40
62
|
}
|
|
41
63
|
|
|
42
64
|
/** A live relay. Dispose to tear both directions down. */
|
|
@@ -143,7 +165,7 @@ export function relayToPostMessageChannel(
|
|
|
143
165
|
bufferSubscription?.dispose();
|
|
144
166
|
bufferSubscription = undefined;
|
|
145
167
|
buffered.length = 0;
|
|
146
|
-
options.reportError?.(error,
|
|
168
|
+
options.reportError?.(error, resolve(RELAY_TRANSPORT_OPEN_FAILED, { detail: describeError(error) }));
|
|
147
169
|
closeFramedSide();
|
|
148
170
|
return false;
|
|
149
171
|
}
|
|
@@ -166,12 +188,12 @@ export function relayToPostMessageChannel(
|
|
|
166
188
|
opened.reader.listen(message => channel.post(message)),
|
|
167
189
|
opened.reader.onClose(() => closeFramedSide()),
|
|
168
190
|
opened.reader.onError(error => {
|
|
169
|
-
options.reportError?.(error,
|
|
191
|
+
options.reportError?.(error, resolve(RELAY_TRANSPORT_READ_FAILED, { detail: describeError(error) }));
|
|
170
192
|
closeFramedSide();
|
|
171
193
|
}),
|
|
172
194
|
channel.onMessage(message => {
|
|
173
195
|
void opened.writer.write(message).catch((error: unknown) => {
|
|
174
|
-
options.reportError?.(error,
|
|
196
|
+
options.reportError?.(error, resolve(RELAY_TRANSPORT_WRITE_FAILED, { detail: describeError(error) }));
|
|
175
197
|
});
|
|
176
198
|
})
|
|
177
199
|
);
|
|
@@ -183,7 +205,7 @@ export function relayToPostMessageChannel(
|
|
|
183
205
|
|
|
184
206
|
for (const message of buffered) {
|
|
185
207
|
void opened.writer.write(message).catch((error: unknown) => {
|
|
186
|
-
options.reportError?.(error,
|
|
208
|
+
options.reportError?.(error, resolve(RELAY_REPLAY_FAILED, { detail: describeError(error) }));
|
|
187
209
|
});
|
|
188
210
|
}
|
|
189
211
|
buffered.length = 0;
|