@hydranium/protocol 1.0.0-next.24 → 1.0.0-next.27
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/lib/client/data-port.d.ts +10 -3
- package/lib/client/data-port.d.ts.map +1 -1
- package/lib/client/data-session.d.ts +8 -0
- package/lib/client/data-session.d.ts.map +1 -1
- package/lib/client/data-session.js +12 -3
- package/lib/client/data-session.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/errors.d.ts +11 -1
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +16 -6
- package/lib/errors.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -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 +135 -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/testing/data-doubles.d.ts +5 -3
- package/lib/testing/data-doubles.d.ts.map +1 -1
- package/lib/testing/data-doubles.js +2 -2
- package/lib/testing/data-doubles.js.map +1 -1
- package/package.json +9 -1
- package/src/client/data-port.ts +10 -3
- package/src/client/data-session.ts +21 -2
- package/src/client/message-relay.ts +28 -6
- package/src/errors.ts +20 -6
- package/src/index.ts +4 -0
- package/src/messages/index.ts +35 -0
- package/src/messages/primitives.ts +209 -0
- package/src/testing/data-doubles.ts +8 -6
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/********************************************************************************
|
|
3
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
4
|
+
*
|
|
5
|
+
* This program and the accompanying materials are made available under the
|
|
6
|
+
* terms of the MIT License which is available in the project root.
|
|
7
|
+
*
|
|
8
|
+
* SPDX-License-Identifier: MIT
|
|
9
|
+
********************************************************************************/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.defineMessage = defineMessage;
|
|
12
|
+
exports.interpolate = interpolate;
|
|
13
|
+
exports.messageData = messageData;
|
|
14
|
+
exports.resolve = resolve;
|
|
15
|
+
exports.hasMessageIdentity = hasMessageIdentity;
|
|
16
|
+
exports.messageError = messageError;
|
|
17
|
+
exports.renderFrameworkMessage = renderFrameworkMessage;
|
|
18
|
+
exports.resolvedFromResponseError = resolvedFromResponseError;
|
|
19
|
+
exports.describeError = describeError;
|
|
20
|
+
exports.isMessageDeclaration = isMessageDeclaration;
|
|
21
|
+
exports.collectMessages = collectMessages;
|
|
22
|
+
const vscode_jsonrpc_1 = require("vscode-jsonrpc");
|
|
23
|
+
/**
|
|
24
|
+
* Declare a message. Placeholder names are inferred from `text` rather than
|
|
25
|
+
* declared again in a type argument: a second spelling of every name is the
|
|
26
|
+
* repetition that drifts, since adding a placeholder to the sentence and not to
|
|
27
|
+
* the type compiles.
|
|
28
|
+
*/
|
|
29
|
+
function defineMessage(code, text) {
|
|
30
|
+
const literal = text;
|
|
31
|
+
return { code, text: literal, format: (...args) => interpolate(literal, args[0] ?? {}) };
|
|
32
|
+
}
|
|
33
|
+
const PLACEHOLDER = /\{([^}]+)\}/g;
|
|
34
|
+
/**
|
|
35
|
+
* Substitute `{name}` tokens, leaving an unfilled token in place.
|
|
36
|
+
*
|
|
37
|
+
* It must not throw. This runs over an adopter's translation as well as our own
|
|
38
|
+
* text, so a typo in a foreign catalogue has to degrade to a slightly wrong
|
|
39
|
+
* sentence rather than raise inside a toast render. Re-scanning the result to
|
|
40
|
+
* detect an unfilled token is what an earlier form did, and it cannot work:
|
|
41
|
+
* `String.replace` does not rescan replacement text, so the check could not tell
|
|
42
|
+
* an unfilled placeholder from user data shaped like one — an element literally
|
|
43
|
+
* named `{separator}` crashed at the authoring site.
|
|
44
|
+
*/
|
|
45
|
+
function interpolate(template, params) {
|
|
46
|
+
// Indexed rather than `key in params`: a hand-built or version-skewed
|
|
47
|
+
// identity can arrive with no params at all, and `in` throws on a non-object
|
|
48
|
+
// where a lookup degrades.
|
|
49
|
+
const lookup = params;
|
|
50
|
+
return template.replace(PLACEHOLDER, (match, key) => {
|
|
51
|
+
const value = lookup?.[key];
|
|
52
|
+
return value === undefined ? match : String(value);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function messageData(message, ...args) {
|
|
56
|
+
return { hydranium: { code: message.code, params: args[0] ?? {} } };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Identity plus resolved English, for a hand-off carrying a value rather than a
|
|
60
|
+
* protocol field. The result is structured-clone safe, so it survives a process
|
|
61
|
+
* hop where one intervenes and costs nothing where none does.
|
|
62
|
+
*/
|
|
63
|
+
function resolve(message, ...args) {
|
|
64
|
+
return { code: message.code, text: message.format(...args), params: args[0] ?? {} };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Validates every field {@link MessageIdentity} declares, `params` included.
|
|
68
|
+
* A guard over foreign input that checks only `code` while declaring `params`
|
|
69
|
+
* non-optional hands `undefined` to the renderer, which fails with the worst
|
|
70
|
+
* polarity available: invisible in English, crashing only once a translation is
|
|
71
|
+
* loaded.
|
|
72
|
+
*/
|
|
73
|
+
function hasMessageIdentity(data) {
|
|
74
|
+
if (typeof data !== 'object' || data === null || Array.isArray(data) || !('hydranium' in data)) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const identity = data.hydranium;
|
|
78
|
+
if (typeof identity !== 'object' || identity === null || Array.isArray(identity)) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const candidate = identity;
|
|
82
|
+
return typeof candidate.code === 'string' && typeof candidate.params === 'object' && candidate.params !== null;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The numeric `code` and the message's catalogue code are unrelated and both are
|
|
86
|
+
* needed: `ResponseError.code` is an `integer`, so it cannot hold a
|
|
87
|
+
* `hydranium/…` key, and it is what a caller switches on after reconstruction.
|
|
88
|
+
*/
|
|
89
|
+
function messageError(code, message, ...params) {
|
|
90
|
+
return new vscode_jsonrpc_1.ResponseError(code, message.format(...params), messageData(message, ...params));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Render on the side that knows the reading user's locale. `translations` is
|
|
94
|
+
* whatever flat `code → template` map the host exposes; omitting it is how an
|
|
95
|
+
* adopter without i18n opts out, and yields the English.
|
|
96
|
+
*/
|
|
97
|
+
function renderFrameworkMessage(message, translations) {
|
|
98
|
+
const template = translations?.[message.code];
|
|
99
|
+
return template ? interpolate(template, message.params) : message.text;
|
|
100
|
+
}
|
|
101
|
+
function resolvedFromResponseError(error) {
|
|
102
|
+
return hasMessageIdentity(error.data) ? { ...error.data.hydranium, text: error.message } : undefined;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The detail half of a `{detail}` placeholder. A technical error string is safe
|
|
106
|
+
* to pass as a parameter for the same reason a number is: it is not itself
|
|
107
|
+
* translatable text, so it needs no code of its own. A PROSE fragment is not,
|
|
108
|
+
* and must become one code per value instead.
|
|
109
|
+
*/
|
|
110
|
+
function describeError(error) {
|
|
111
|
+
return error instanceof Error ? error.message : String(error);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Recognises a declaration among a barrel's exports. The `format` check is what
|
|
115
|
+
* discriminates: a `code` + `text` pair alone admits any object that happens to
|
|
116
|
+
* carry both.
|
|
117
|
+
*/
|
|
118
|
+
function isMessageDeclaration(value) {
|
|
119
|
+
const candidate = value;
|
|
120
|
+
return (typeof candidate === 'object' &&
|
|
121
|
+
candidate !== null &&
|
|
122
|
+
typeof candidate.code === 'string' &&
|
|
123
|
+
typeof candidate.text === 'string' &&
|
|
124
|
+
typeof candidate.format === 'function');
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Every declaration a `./messages` barrel exports.
|
|
128
|
+
*
|
|
129
|
+
* A caller cannot get there with `Object.values(barrel).filter(isMessageDeclaration)`:
|
|
130
|
+
* a barrel's value type is a union of its declarations AND its functions, and
|
|
131
|
+
* `filter` will not narrow a function type down to a `MessageDefinition`, so the
|
|
132
|
+
* result stays the union and reading `.code` off it does not compile. Taking the
|
|
133
|
+
* barrel as an opaque object is what makes the one-liner work.
|
|
134
|
+
*/
|
|
135
|
+
function collectMessages(barrel) {
|
|
136
|
+
return Object.values(barrel).filter(isMessageDeclaration);
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=primitives.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"primitives.js","sourceRoot":"","sources":["../../src/messages/primitives.ts"],"names":[],"mappings":";AAAA;;;;;;;kFAOkF;;AAkDlF,sCAGC;AAeD,kCASC;AAoBD,kCAEC;AAOD,0BAEC;AASD,gDAUC;AAeD,oCAMC;AAOD,wDAGC;AAED,8DAEC;AAQD,sCAEC;AAOD,oDASC;AAWD,0CAEC;AAvMD,mDAA+C;AA0C/C;;;;;GAKG;AACH,SAAgB,aAAa,CAAmB,IAAY,EAAE,IAAoB;IAC/E,MAAM,OAAO,GAAG,IAAS,CAAC;IAC1B,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;AAC5F,CAAC;AAED,MAAM,WAAW,GAAG,cAAc,CAAC;AAEnC;;;;;;;;;;GAUG;AACH,SAAgB,WAAW,CAAC,QAAgB,EAAE,MAAqB;IAChE,sEAAsE;IACtE,6EAA6E;IAC7E,2BAA2B;IAC3B,MAAM,MAAM,GAAG,MAAiE,CAAC;IACjF,OAAO,QAAQ,CAAC,OAAO,CAAC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AO,SAAS,CAAC,IAAI,KAAK,QAAQ;QAClC,OAAO,SAAS,CAAC,MAAM,KAAK,UAAU,CACxC,CAAC;AACL,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,eAAe,CAAC,MAAc;IAC3C,OAAQ,MAAM,CAAC,MAAM,CAAC,MAAM,CAAe,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;AAC5E,CAAC"}
|
|
@@ -33,6 +33,7 @@ import { type MessageConnection } from 'vscode-jsonrpc';
|
|
|
33
33
|
import type { DataPort } from '../client/data-port';
|
|
34
34
|
import type { DataClientProtocol } from '../data/data-server-protocol';
|
|
35
35
|
import type { ProjectsChangedEvent, TransferDocumentSavedEvent, TransferDocumentUpdatedEvent } from '../data/events';
|
|
36
|
+
import type { ResolvedMessage } from '../messages/primitives';
|
|
36
37
|
import type { Project } from '../project';
|
|
37
38
|
import type { TransferDiagnostic } from '../transfer-diagnostic';
|
|
38
39
|
import type { TransferElement } from '../transfer-element';
|
|
@@ -69,12 +70,13 @@ export interface FakeDataPort extends DataPort {
|
|
|
69
70
|
/**
|
|
70
71
|
* Every {@link DataPort.reportError} call, in order. Read from outside: this
|
|
71
72
|
* is the only place a transport failure surfaces, so a test for the failure
|
|
72
|
-
* path asserts
|
|
73
|
-
*
|
|
73
|
+
* path asserts here rather than on a rejection the consumer may legitimately
|
|
74
|
+
* swallow. Prefer asserting on `message.code`, which is stable, over
|
|
75
|
+
* `message.text`, which is the English default and may be reworded.
|
|
74
76
|
*/
|
|
75
77
|
readonly reported: readonly {
|
|
76
78
|
readonly error: unknown;
|
|
77
|
-
readonly
|
|
79
|
+
readonly message: ResolvedMessage;
|
|
78
80
|
}[];
|
|
79
81
|
/**
|
|
80
82
|
* Fire {@link DataPort.onDispose} — the host tearing the transport down, a
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-doubles.d.ts","sourceRoot":"","sources":["../../src/testing/data-doubles.ts"],"names":[],"mappings":"AAAA;;;;;;;kFAOk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Q,CAAC,KAAK,EAAE,0BAA0B,CAAC,SAAS,EAAE,WAAW,CAAC,EAAE,CAAC;IACrE,yDAAyD;IACzD,QAAQ,CAAC,eAAe,EAAE,oBAAoB,CAAC,QAAQ,CAAC,EAAE,CAAC;CAC7D;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CACpC,SAAS,SAAS,eAAe,EACjC,WAAW,SAAS,kBAAkB,GAAG,kBAAkB,EAC3D,QAAQ,SAAS,OAAO,GAAG,OAAO,EACnC,SAAS,GAAE,OAAO,CAAC,kBAAkB,CAAC,SAAS,EAAE,WAAW,EAAE,QAAQ,CAAC,CAAM,GAAG,mBAAmB,CAAC,SAAS,EAAE,WAAW,EAAE,QAAQ,CAAC,CAsBtI"}
|
|
@@ -55,8 +55,8 @@ function makeFakeDataPort(options) {
|
|
|
55
55
|
connections.push(connection);
|
|
56
56
|
return connection;
|
|
57
57
|
},
|
|
58
|
-
reportError(error,
|
|
59
|
-
reported.push({ error,
|
|
58
|
+
reportError(error, message) {
|
|
59
|
+
reported.push({ error, message });
|
|
60
60
|
},
|
|
61
61
|
fireDispose() {
|
|
62
62
|
disposeEmitter.fire(undefined);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data-doubles.js","sourceRoot":"","sources":["../../src/testing/data-doubles.ts"],"names":[],"mappings":";AAAA;;;;;;;kFAOkF;;
|
|
1
|
+
{"version":3,"file":"data-doubles.js","sourceRoot":"","sources":["../../src/testing/data-doubles.ts"],"names":[],"mappings":";AAAA;;;;;;;kFAOkF;;AA2FlF,4CAwBC;AA+BD,0DA0BC;AA1KD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,mDAAiE;AA0DjE;;;;;;GAMG;AACH,SAAgB,gBAAgB,CAAC,OAA4B;IAC1D,MAAM,WAAW,GAAwB,EAAE,CAAC;IAC5C,MAAM,QAAQ,GAAmD,EAAE,CAAC;IACpE,MAAM,cAAc,GAAG,IAAI,wBAAO,EAAQ,CAA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EAAE,KAAK,EAAE,eAAe,EAAE,CAAC;AACtD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hydranium/protocol",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.27",
|
|
4
4
|
"description": "Generic, language-agnostic types, constants, and pure utilities for the hydranium framework.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hydranium",
|
|
@@ -41,6 +41,14 @@
|
|
|
41
41
|
"types": "./lib/data/index.d.ts",
|
|
42
42
|
"default": "./lib/data/index.js"
|
|
43
43
|
},
|
|
44
|
+
"./messages": {
|
|
45
|
+
"types": "./lib/messages/index.d.ts",
|
|
46
|
+
"default": "./lib/messages/index.js"
|
|
47
|
+
},
|
|
48
|
+
"./lib/messages": {
|
|
49
|
+
"types": "./lib/messages/index.d.ts",
|
|
50
|
+
"default": "./lib/messages/index.js"
|
|
51
|
+
},
|
|
44
52
|
"./testing": {
|
|
45
53
|
"types": "./lib/testing/index.d.ts",
|
|
46
54
|
"default": "./lib/testing/index.js"
|
package/src/client/data-port.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
10
|
import type { Event, MessageConnection } from 'vscode-jsonrpc';
|
|
11
|
+
import type { ResolvedMessage } from '../messages/primitives';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* The one thing a host has to supply for the data head: a live JSON-RPC
|
|
@@ -77,10 +78,16 @@ export interface DataPort {
|
|
|
77
78
|
*
|
|
78
79
|
* It exists because the alternative is worse in both directions: this tier
|
|
79
80
|
* cannot import a host's UI, and swallowing the error makes a dead
|
|
80
|
-
* connection look like an empty model.
|
|
81
|
-
*
|
|
81
|
+
* connection look like an empty model.
|
|
82
|
+
*
|
|
83
|
+
* `reported` is a complete sentence plus the identity needed to render it in
|
|
84
|
+
* another language. It carries a value rather than using a protocol field
|
|
85
|
+
* because this tier does not know whether a process hop intervenes — in a
|
|
86
|
+
* webview host the render happens across one — and a `ResolvedMessage` is
|
|
87
|
+
* structured-clone safe either way. Render it with `renderFrameworkMessage`;
|
|
88
|
+
* passing no translation map yields the English.
|
|
82
89
|
*/
|
|
83
|
-
reportError(error: unknown,
|
|
90
|
+
reportError(error: unknown, reported: ResolvedMessage): void;
|
|
84
91
|
|
|
85
92
|
/**
|
|
86
93
|
* Fires when the host tears the transport down and the current connection
|
|
@@ -9,11 +9,28 @@
|
|
|
9
9
|
|
|
10
10
|
import type { MessageConnection } from 'vscode-jsonrpc';
|
|
11
11
|
import { DATA_CLIENT_PROTOCOL_METHODS, DATA_SERVER_WIRE_PREFIX, type DataClientProtocol, type DataServerProtocol } from '../data';
|
|
12
|
+
import { defineMessage, describeError, resolve } from '../messages/primitives';
|
|
12
13
|
import { type RpcProxy, createRpcProxy } from '../rpc';
|
|
13
14
|
import type { TransferDocument } from '../transfer-document';
|
|
14
15
|
import type { TransferElement } from '../transfer-element';
|
|
15
16
|
import type { DataPort } from './data-port';
|
|
16
17
|
|
|
18
|
+
/**
|
|
19
|
+
* The transport never opened. A complete sentence rather than a fragment: a
|
|
20
|
+
* fragment is nested inside a sentence the framework does not own, so no
|
|
21
|
+
* translator controls the whole and the composition cannot be made to read
|
|
22
|
+
* correctly in every language.
|
|
23
|
+
*/
|
|
24
|
+
export const DATA_SERVER_CONNECT_FAILED = defineMessage(
|
|
25
|
+
'hydranium/protocol/data-server-connect-failed',
|
|
26
|
+
'Could not connect to the data server: {detail}'
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
export const DATA_SERVER_NOT_READY = defineMessage(
|
|
30
|
+
'hydranium/protocol/data-server-not-ready',
|
|
31
|
+
'The data server did not become ready: {detail}'
|
|
32
|
+
);
|
|
33
|
+
|
|
17
34
|
/** Options for {@link DataSession}. */
|
|
18
35
|
export interface DataSessionOptions {
|
|
19
36
|
/**
|
|
@@ -168,7 +185,9 @@ export class DataSession<TTransfer extends TransferElement> {
|
|
|
168
185
|
// Rejection is reported here rather than left to float: an unhandled
|
|
169
186
|
// rejection on a connection promise is the failure mode that reads as
|
|
170
187
|
// "the model is empty" instead of "the transport never opened".
|
|
171
|
-
connection.catch((error: unknown) =>
|
|
188
|
+
connection.catch((error: unknown) =>
|
|
189
|
+
this.port.reportError(error, resolve(DATA_SERVER_CONNECT_FAILED, { detail: describeError(error) }))
|
|
190
|
+
);
|
|
172
191
|
const server = createRpcProxy<DataServerProtocol<TTransfer>, DataClientProtocol<TTransfer>>(connection, {
|
|
173
192
|
methodNamespace: this.methodNamespace,
|
|
174
193
|
localTarget: this.client,
|
|
@@ -189,7 +208,7 @@ export class DataSession<TTransfer extends TransferElement> {
|
|
|
189
208
|
if (this.generation === generation) {
|
|
190
209
|
this.generation = undefined;
|
|
191
210
|
}
|
|
192
|
-
this.port.reportError(error,
|
|
211
|
+
this.port.reportError(error, resolve(DATA_SERVER_NOT_READY, { detail: describeError(error) }));
|
|
193
212
|
throw error;
|
|
194
213
|
}
|
|
195
214
|
}
|
|
@@ -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;
|
package/src/errors.ts
CHANGED
|
@@ -8,6 +8,20 @@
|
|
|
8
8
|
********************************************************************************/
|
|
9
9
|
|
|
10
10
|
import { ResponseError } from 'vscode-jsonrpc';
|
|
11
|
+
import { defineMessage, type HydraniumMessageData, messageData } from './messages/primitives';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The catalogue declaration behind {@link ConflictError}'s sentence.
|
|
15
|
+
*
|
|
16
|
+
* Its English must keep containing {@link CONFLICT_ERROR_MESSAGE_MARKER}: the
|
|
17
|
+
* marker is tier 3 of {@link isConflictError}'s ladder, and it matches on text.
|
|
18
|
+
* That tier only ever works untranslated, which is why it is the last resort
|
|
19
|
+
* behind the numeric code rather than the primary check.
|
|
20
|
+
*/
|
|
21
|
+
export const STALE_BASED_UPDATE = defineMessage(
|
|
22
|
+
'hydranium/protocol/stale-based-update',
|
|
23
|
+
'Stale-based update for {uri}: expected v{expected}, server is at v{actual}'
|
|
24
|
+
);
|
|
11
25
|
|
|
12
26
|
/**
|
|
13
27
|
* Application-specific JSON-RPC error code for {@link ConflictError}.
|
|
@@ -24,7 +38,7 @@ export const CONFLICT_ERROR_CODE = 1001;
|
|
|
24
38
|
* Structured payload carried in {@link ConflictError.data}, and the only place
|
|
25
39
|
* a post-RPC caller can read the version mismatch from.
|
|
26
40
|
*/
|
|
27
|
-
export interface ConflictErrorData {
|
|
41
|
+
export interface ConflictErrorData extends HydraniumMessageData {
|
|
28
42
|
readonly uri: string;
|
|
29
43
|
/** The based-on version the caller authored against. */
|
|
30
44
|
readonly expected: number;
|
|
@@ -65,11 +79,11 @@ export interface ConflictErrorData {
|
|
|
65
79
|
*/
|
|
66
80
|
export class ConflictError extends ResponseError<ConflictErrorData> {
|
|
67
81
|
constructor(uri: string, expected: number, actual: number) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
});
|
|
82
|
+
const params = { uri, expected, actual };
|
|
83
|
+
// The identity rides alongside the typed payload rather than replacing
|
|
84
|
+
// it: this class's getters and `isConflictError`'s name check are surface
|
|
85
|
+
// an adopter may bind, so the payload widens rather than changing shape.
|
|
86
|
+
super(CONFLICT_ERROR_CODE, STALE_BASED_UPDATE.format(params), { ...params, ...messageData(STALE_BASED_UPDATE, params) });
|
|
73
87
|
this.name = 'ConflictError';
|
|
74
88
|
// ResponseError's constructor calls `Object.setPrototypeOf(this,
|
|
75
89
|
// ResponseError.prototype)` to keep its own prototype chain intact across
|
package/src/index.ts
CHANGED
|
@@ -24,6 +24,10 @@ export * from './errors';
|
|
|
24
24
|
export * from './host-diagnostics';
|
|
25
25
|
export * from './logger';
|
|
26
26
|
export * from './latency-collector';
|
|
27
|
+
// The primitives only. The `./messages` subpath additionally enumerates this
|
|
28
|
+
// package's own declarations, which the root barrel already re-exports through
|
|
29
|
+
// the modules that raise them.
|
|
30
|
+
export * from './messages/primitives';
|
|
27
31
|
export * from './patch-merge';
|
|
28
32
|
export * from './noop-logger';
|
|
29
33
|
export * from './observable-value';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The message-externalization mechanism, plus every user-facing message
|
|
12
|
+
* `@hydranium/protocol` itself raises.
|
|
13
|
+
*
|
|
14
|
+
* Declarations stay beside their call sites and are re-exported here, so this is
|
|
15
|
+
* enumeration rather than centralization: a code's package segment has to name
|
|
16
|
+
* the package that raises it, and a shared module would make that segment a lie
|
|
17
|
+
* for every message in it. Adding a message therefore touches the file that
|
|
18
|
+
* raises it and this list, and nothing else.
|
|
19
|
+
*
|
|
20
|
+
* A barrel makes every code and English default public API, so renaming a code
|
|
21
|
+
* is a breaking change. That was already true — an adopter's catalogue keys on
|
|
22
|
+
* these codes either way — but it is now in the type system rather than implicit.
|
|
23
|
+
* The English is a fallback, not a contract; the code is the contract.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export * from './primitives';
|
|
27
|
+
|
|
28
|
+
export { STALE_BASED_UPDATE } from '../errors';
|
|
29
|
+
export { DATA_SERVER_CONNECT_FAILED, DATA_SERVER_NOT_READY } from '../client/data-session';
|
|
30
|
+
export {
|
|
31
|
+
RELAY_REPLAY_FAILED,
|
|
32
|
+
RELAY_TRANSPORT_OPEN_FAILED,
|
|
33
|
+
RELAY_TRANSPORT_READ_FAILED,
|
|
34
|
+
RELAY_TRANSPORT_WRITE_FAILED
|
|
35
|
+
} from '../client/message-relay';
|