@hydranium/protocol 1.0.0-next.38 → 1.0.0-next.380
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 +44 -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 +18 -1
- package/lib/client/data-events.d.ts.map +1 -1
- package/lib/client/data-events.js +26 -0
- 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 +694 -89
- package/lib/client/data-session.d.ts.map +1 -1
- package/lib/client/data-session.js +1158 -115
- package/lib/client/data-session.js.map +1 -1
- package/lib/client/index.d.ts +15 -9
- package/lib/client/index.d.ts.map +1 -1
- package/lib/client/index.js +15 -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-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 +13 -1
- package/lib/data/data-protocol-methods.js.map +1 -1
- package/lib/data/data-server-protocol.d.ts +176 -49
- package/lib/data/data-server-protocol.d.ts.map +1 -1
- package/lib/data/events.d.ts +144 -33
- 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 +243 -41
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +400 -55
- 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 +60 -13
- package/lib/model-server.d.ts.map +1 -1
- package/lib/model-server.js +4 -2
- package/lib/model-server.js.map +1 -1
- package/lib/model-service/base-version.d.ts +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 +30 -0
- package/lib/pending-work-tracker.d.ts.map +1 -0
- package/lib/pending-work-tracker.js +47 -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 +41 -12
- package/lib/testing/data-doubles.d.ts.map +1 -1
- package/lib/testing/data-doubles.js +56 -8
- 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/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 +1 -0
- package/lib/testing/node/index.d.ts.map +1 -1
- package/lib/testing/node/index.js +2 -2
- package/lib/testing/node/index.js.map +1 -1
- package/lib/testing/node/message-port-pair.d.ts +25 -0
- package/lib/testing/node/message-port-pair.d.ts.map +1 -0
- package/lib/testing/node/message-port-pair.js +26 -0
- package/lib/testing/node/message-port-pair.js.map +1 -0
- package/lib/testing/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 +96 -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 +38 -1
- package/src/client/data-port.ts +36 -25
- package/src/client/data-session.ts +1479 -137
- package/src/client/index.ts +15 -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-ids.ts +49 -0
- package/src/clock.ts +68 -10
- package/src/console-logger.ts +39 -0
- package/src/data/data-protocol-methods.ts +14 -4
- package/src/data/data-server-protocol.ts +203 -49
- package/src/data/events.ts +150 -33
- package/src/data/requests.ts +90 -11
- package/src/debouncer.ts +11 -11
- package/src/errors.ts +500 -65
- 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 +64 -19
- 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 +47 -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 +141 -19
- package/src/testing/fake-clock.ts +76 -48
- package/src/testing/index.ts +4 -3
- package/src/testing/node/duplex-connection.ts +3 -2
- package/src/testing/node/index.ts +2 -2
- package/src/testing/node/message-port-pair.ts +40 -0
- package/src/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 +116 -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
package/src/errors.ts
CHANGED
|
@@ -9,62 +9,104 @@
|
|
|
9
9
|
|
|
10
10
|
import { ResponseError } from 'vscode-jsonrpc';
|
|
11
11
|
import { defineMessage, type HydraniumMessageData, messageData } from './messages/primitives';
|
|
12
|
+
import { asModelVersion, type ModelVersion, type TextVersion } from './model-service/base-version';
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
*
|
|
14
|
+
/*
|
|
15
|
+
* Every error here is a typed error: a caller reacts to it, so it has a class,
|
|
16
|
+
* a JSON-RPC code and an `is*` guard, and an English message. A typed error
|
|
17
|
+
* also carries a message identity where the framework can write an end-user
|
|
18
|
+
* sentence that needs no context. A URI may stay in that sentence; a client id
|
|
19
|
+
* or a raw version stays out of it, worded or left to `data`.
|
|
15
20
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
* An error nothing reacts to does not belong here: it stays a plain `Error`, or
|
|
22
|
+
* a named subclass without a code, at its throw site, since a code and a guard
|
|
23
|
+
* are public API that no caller would use.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The catalogue declaration behind {@link ConflictError}'s sentence. It words
|
|
28
|
+
* the version mismatch rather than stating it: the two numbers mean nothing to
|
|
29
|
+
* an end user, and they stay in {@link ConflictErrorData}.
|
|
30
|
+
*/
|
|
31
|
+
export const STALE_BASE_VERSION_UPDATE = defineMessage(
|
|
32
|
+
'hydranium/protocol/stale-base-version-update',
|
|
33
|
+
'The edit to {uri} was not applied: it was made to an older version of the document.'
|
|
24
34
|
);
|
|
25
35
|
|
|
26
36
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
37
|
+
* {@link STALE_BASE_VERSION_UPDATE}'s counterpart for an editor's save the
|
|
38
|
+
* server refused, since the editor's buffer is not the text it holds.
|
|
39
|
+
*/
|
|
40
|
+
export const STALE_BASE_VERSION_SAVE = defineMessage(
|
|
41
|
+
'hydranium/protocol/stale-base-version-save',
|
|
42
|
+
'The save of {uri} was not applied: it was made to an older version of the document.'
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Every JSON-RPC error code the framework raises, from any package, keyed by
|
|
47
|
+
* the error it identifies.
|
|
48
|
+
*
|
|
49
|
+
* The framework reserves the block 42000 to 42999 for these, and an adopter's
|
|
50
|
+
* own codes stay outside it: after an RPC the proxy revives an error by its
|
|
51
|
+
* code, so an adopter's error carrying one of these can read as the
|
|
52
|
+
* framework's. A new code takes the next unused number in the block, whichever
|
|
53
|
+
* package raises it, and is declared here, where tests hold every code distinct
|
|
54
|
+
* and inside the block.
|
|
55
|
+
* The block sits clear of the range JSON-RPC 2.0 reserves for itself, -32768
|
|
56
|
+
* to -32000.
|
|
57
|
+
*/
|
|
58
|
+
export const HYDRANIUM_ERROR_CODES = {
|
|
59
|
+
conflict: 42001,
|
|
60
|
+
noActiveProfile: 42002,
|
|
61
|
+
referenceSettleTimeout: 42003,
|
|
62
|
+
sessionClosed: 42004,
|
|
63
|
+
documentNotOpen: 42005,
|
|
64
|
+
duplicateClientId: 42006,
|
|
65
|
+
reservedClientId: 42007,
|
|
66
|
+
saveFailed: 42008,
|
|
67
|
+
documentSyntaxError: 42009
|
|
68
|
+
} as const;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* JSON-RPC error code for {@link ConflictError}.
|
|
29
72
|
*
|
|
30
73
|
* The code is the load-bearing identifier across realm boundaries —
|
|
31
74
|
* `code` is a first-class field on the JSON-RPC error envelope and
|
|
32
75
|
* survives wire reconstruction; the custom `Error` subclass name does
|
|
33
76
|
* not.
|
|
34
77
|
*/
|
|
35
|
-
export const CONFLICT_ERROR_CODE =
|
|
78
|
+
export const CONFLICT_ERROR_CODE = HYDRANIUM_ERROR_CODES.conflict;
|
|
36
79
|
|
|
37
|
-
/**
|
|
38
|
-
* Structured payload carried in {@link ConflictError.data}, and the only place
|
|
39
|
-
* a post-RPC caller can read the version mismatch from.
|
|
40
|
-
*/
|
|
80
|
+
/** Structured payload carried in {@link ConflictError.data}. */
|
|
41
81
|
export interface ConflictErrorData extends HydraniumMessageData {
|
|
82
|
+
/**
|
|
83
|
+
* The document whose version moved. A write of several documents names
|
|
84
|
+
* the first stale one in request order, and others may be stale too.
|
|
85
|
+
*/
|
|
42
86
|
readonly uri: string;
|
|
43
|
-
/** The
|
|
44
|
-
readonly
|
|
87
|
+
/** The base version the caller authored against. */
|
|
88
|
+
readonly baseVersion: ModelVersion;
|
|
45
89
|
/** The server's current text-document version at the time of the throw. */
|
|
46
|
-
readonly
|
|
90
|
+
readonly actualVersion: TextVersion;
|
|
47
91
|
}
|
|
48
92
|
|
|
49
93
|
/**
|
|
50
|
-
* Thrown by `
|
|
51
|
-
* supplied
|
|
94
|
+
* Thrown by a client session's `update` / `save` when the caller-
|
|
95
|
+
* supplied base version no longer matches the server's current text-
|
|
52
96
|
* document version for the same URI — i.e. the snapshot the caller
|
|
53
97
|
* authored against has been superseded by an intervening edit.
|
|
54
98
|
*
|
|
55
99
|
* Extends vscode-jsonrpc's {@link ResponseError} so the typed
|
|
56
100
|
* {@link ConflictErrorData} payload rides on the standard JSON-RPC
|
|
57
101
|
* error envelope (`code`, `message`, `data`) — all three fields are
|
|
58
|
-
* preserved by RPC reconstruction.
|
|
59
|
-
* the receiving side
|
|
60
|
-
* `err.data`
|
|
61
|
-
*
|
|
102
|
+
* preserved by RPC reconstruction. `createRpcProxy` revives it into
|
|
103
|
+
* this class on the receiving side; a path that does not leaves a
|
|
104
|
+
* plain `ResponseError`, readable through `err.data` only and refused
|
|
105
|
+
* by {@link isConflictError}.
|
|
62
106
|
*
|
|
63
|
-
* Detection is
|
|
64
|
-
*
|
|
65
|
-
* no gating.
|
|
66
|
-
* posture, so headless / CLI / batch tooling with no meaningful based-on
|
|
67
|
-
* version can opt out explicitly.
|
|
107
|
+
* Detection is driven by the required `baseVersion` field of every write
|
|
108
|
+
* request; a caller with no meaningful base version passes
|
|
109
|
+
* `'any'` and gets no gating.
|
|
68
110
|
*
|
|
69
111
|
* Three reasonable adopter recovery strategies:
|
|
70
112
|
*
|
|
@@ -78,64 +120,457 @@ export interface ConflictErrorData extends HydraniumMessageData {
|
|
|
78
120
|
* No auto-retry or auto-merge ships by default.
|
|
79
121
|
*/
|
|
80
122
|
export class ConflictError extends ResponseError<ConflictErrorData> {
|
|
81
|
-
|
|
82
|
-
|
|
123
|
+
/** `message` words what was refused: an edit, or with {@link STALE_BASE_VERSION_SAVE} a save. */
|
|
124
|
+
constructor(
|
|
125
|
+
uri: string,
|
|
126
|
+
baseVersion: ModelVersion,
|
|
127
|
+
actualVersion: TextVersion,
|
|
128
|
+
message: typeof STALE_BASE_VERSION_UPDATE | typeof STALE_BASE_VERSION_SAVE = STALE_BASE_VERSION_UPDATE
|
|
129
|
+
) {
|
|
83
130
|
// The identity rides alongside the typed payload rather than replacing
|
|
84
|
-
// it
|
|
85
|
-
//
|
|
86
|
-
super(CONFLICT_ERROR_CODE,
|
|
131
|
+
// it. The getters, the guard and the reviver read the payload's fields,
|
|
132
|
+
// so adding the identity widens the payload rather than reshaping it.
|
|
133
|
+
super(CONFLICT_ERROR_CODE, message.format({ uri }), {
|
|
134
|
+
uri,
|
|
135
|
+
baseVersion,
|
|
136
|
+
actualVersion,
|
|
137
|
+
...messageData(message, { uri })
|
|
138
|
+
});
|
|
87
139
|
this.name = 'ConflictError';
|
|
88
140
|
// ResponseError's constructor calls `Object.setPrototypeOf(this,
|
|
89
141
|
// ResponseError.prototype)` to keep its own prototype chain intact across
|
|
90
142
|
// transpilation targets; that resets us to ResponseError, hiding the
|
|
91
143
|
// ConflictError-specific getters. Restore the prototype here so
|
|
92
|
-
// `err.uri` / `.
|
|
144
|
+
// `err.uri` / `.baseVersion` / `.actualVersion` resolve through this
|
|
145
|
+
// class.
|
|
93
146
|
Object.setPrototypeOf(this, ConflictError.prototype);
|
|
94
147
|
}
|
|
95
148
|
|
|
149
|
+
/** The document whose version moved, see {@link ConflictErrorData.uri}. */
|
|
96
150
|
get uri(): string {
|
|
97
151
|
return this.data!.uri;
|
|
98
152
|
}
|
|
99
153
|
|
|
100
|
-
|
|
101
|
-
|
|
154
|
+
/**
|
|
155
|
+
* The base version the caller authored against. Not `expected`, and its
|
|
156
|
+
* sibling not `actual`: a test reporter reads that pair as an assertion
|
|
157
|
+
* failure and reports nothing of the real one.
|
|
158
|
+
*/
|
|
159
|
+
get baseVersion(): ModelVersion {
|
|
160
|
+
return this.data!.baseVersion;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The server's version at the time of the throw. Not `actual`, see {@link baseVersion}. */
|
|
164
|
+
get actualVersion(): TextVersion {
|
|
165
|
+
return this.data!.actualVersion;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* JSON-RPC code for {@link SessionClosedError}, beside {@link CONFLICT_ERROR_CODE}
|
|
171
|
+
* and for the same reason: the code survives reconstruction, the class does not.
|
|
172
|
+
*/
|
|
173
|
+
export const SESSION_CLOSED_ERROR_CODE = HYDRANIUM_ERROR_CODES.sessionClosed;
|
|
174
|
+
/** JSON-RPC code for {@link DocumentNotOpenError}. */
|
|
175
|
+
export const DOCUMENT_NOT_OPEN_ERROR_CODE = HYDRANIUM_ERROR_CODES.documentNotOpen;
|
|
176
|
+
/** JSON-RPC code for {@link DuplicateClientIdError}. */
|
|
177
|
+
export const DUPLICATE_CLIENT_ID_ERROR_CODE = HYDRANIUM_ERROR_CODES.duplicateClientId;
|
|
178
|
+
/** JSON-RPC code for {@link ReservedClientIdError}. */
|
|
179
|
+
export const RESERVED_CLIENT_ID_ERROR_CODE = HYDRANIUM_ERROR_CODES.reservedClientId;
|
|
180
|
+
/** JSON-RPC code for {@link SaveFailedError}. */
|
|
181
|
+
export const SAVE_FAILED_ERROR_CODE = HYDRANIUM_ERROR_CODES.saveFailed;
|
|
182
|
+
/** JSON-RPC code for {@link DocumentSyntaxError}. */
|
|
183
|
+
export const DOCUMENT_SYNTAX_ERROR_CODE = HYDRANIUM_ERROR_CODES.documentSyntaxError;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The catalogue declaration behind {@link SessionClosedError}'s default
|
|
187
|
+
* sentence. It names no client id: the sentence can reach an end user, and the
|
|
188
|
+
* id stays in {@link SessionClosedErrorData.clientId} for whoever needs it.
|
|
189
|
+
*/
|
|
190
|
+
export const SESSION_CLOSED = defineMessage('hydranium/protocol/session-closed', 'The editing session has ended.');
|
|
191
|
+
|
|
192
|
+
/** Structured payload carried in {@link SessionClosedError.data}. */
|
|
193
|
+
export interface SessionClosedErrorData extends HydraniumMessageData {
|
|
194
|
+
readonly clientId: string;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Thrown by every call on a client session after it ended, and by an open that
|
|
199
|
+
* was still in flight when its session ended: by the server for its sessions,
|
|
200
|
+
* and by a client-side `DataSession` once disposed, so a caller handles both
|
|
201
|
+
* the same way. A caller recognises it with {@link isSessionClosedError}, never
|
|
202
|
+
* by its sentence.
|
|
203
|
+
*/
|
|
204
|
+
export class SessionClosedError extends ResponseError<SessionClosedErrorData> {
|
|
205
|
+
/**
|
|
206
|
+
* `message` replaces the default English, for a caller that knows more than
|
|
207
|
+
* that the session is gone. The identity stays {@link SESSION_CLOSED}'s, so a
|
|
208
|
+
* translating renderer renders the catalogue sentence in its place.
|
|
209
|
+
*/
|
|
210
|
+
constructor(clientId: string, message = SESSION_CLOSED.format()) {
|
|
211
|
+
super(SESSION_CLOSED_ERROR_CODE, message, { clientId, ...messageData(SESSION_CLOSED) });
|
|
212
|
+
this.name = 'SessionClosedError';
|
|
213
|
+
// `ResponseError` resets the prototype to its own; see `ConflictError`.
|
|
214
|
+
Object.setPrototypeOf(this, SessionClosedError.prototype);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
get clientId(): string {
|
|
218
|
+
return this.data!.clientId;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* The catalogue declaration behind {@link DocumentNotOpenError}'s sentence. The
|
|
224
|
+
* client id stays in {@link DocumentNotOpenErrorData.clientId}.
|
|
225
|
+
*/
|
|
226
|
+
export const DOCUMENT_NOT_OPEN = defineMessage(
|
|
227
|
+
'hydranium/protocol/document-not-open',
|
|
228
|
+
'The document {uri} is not open in this editing session.'
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
/** Structured payload carried in {@link DocumentNotOpenError.data}. */
|
|
232
|
+
export interface DocumentNotOpenErrorData extends HydraniumMessageData {
|
|
233
|
+
readonly uri: string;
|
|
234
|
+
readonly clientId: string;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Thrown when a client session writes a document it does not have open.
|
|
239
|
+
*
|
|
240
|
+
* A session writes only what it has open, so this is the answer both to a write
|
|
241
|
+
* that never opened and to one whose open was closed underneath it — by the
|
|
242
|
+
* session itself, or by the document being deleted.
|
|
243
|
+
*/
|
|
244
|
+
export class DocumentNotOpenError extends ResponseError<DocumentNotOpenErrorData> {
|
|
245
|
+
constructor(uri: string, clientId: string) {
|
|
246
|
+
super(DOCUMENT_NOT_OPEN_ERROR_CODE, DOCUMENT_NOT_OPEN.format({ uri }), {
|
|
247
|
+
uri,
|
|
248
|
+
clientId,
|
|
249
|
+
...messageData(DOCUMENT_NOT_OPEN, { uri })
|
|
250
|
+
});
|
|
251
|
+
this.name = 'DocumentNotOpenError';
|
|
252
|
+
Object.setPrototypeOf(this, DocumentNotOpenError.prototype);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
get uri(): string {
|
|
256
|
+
return this.data!.uri;
|
|
102
257
|
}
|
|
103
258
|
|
|
104
|
-
get
|
|
105
|
-
return this.data!.
|
|
259
|
+
get clientId(): string {
|
|
260
|
+
return this.data!.clientId;
|
|
106
261
|
}
|
|
107
262
|
}
|
|
108
263
|
|
|
109
|
-
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
|
|
264
|
+
/**
|
|
265
|
+
* The catalogue declaration behind {@link DuplicateClientIdError}'s sentence.
|
|
266
|
+
* The client id stays in {@link DuplicateClientIdErrorData.clientId}.
|
|
267
|
+
*/
|
|
268
|
+
export const DUPLICATE_CLIENT_ID = defineMessage(
|
|
269
|
+
'hydranium/protocol/duplicate-client-id',
|
|
270
|
+
'Could not start an editing session: its identifier is still in use by another editor.'
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
/** Structured payload carried in {@link DuplicateClientIdError.data}. */
|
|
274
|
+
export interface DuplicateClientIdErrorData extends HydraniumMessageData {
|
|
275
|
+
readonly clientId: string;
|
|
276
|
+
}
|
|
113
277
|
|
|
114
278
|
/**
|
|
115
|
-
*
|
|
279
|
+
* Thrown when a client session is started under an id that is already live in
|
|
280
|
+
* the process. The id frees up once its holder ends or closes its last
|
|
281
|
+
* document, so a caller may retry.
|
|
116
282
|
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
283
|
+
* Ids are unique process-wide because the id is also the author label on every
|
|
284
|
+
* version and the key a client recognises its own echoes by; two participants
|
|
285
|
+
* sharing one would each take the other's writes for their own.
|
|
286
|
+
*/
|
|
287
|
+
export class DuplicateClientIdError extends ResponseError<DuplicateClientIdErrorData> {
|
|
288
|
+
constructor(clientId: string) {
|
|
289
|
+
super(DUPLICATE_CLIENT_ID_ERROR_CODE, DUPLICATE_CLIENT_ID.format(), { clientId, ...messageData(DUPLICATE_CLIENT_ID) });
|
|
290
|
+
this.name = 'DuplicateClientIdError';
|
|
291
|
+
Object.setPrototypeOf(this, DuplicateClientIdError.prototype);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
get clientId(): string {
|
|
295
|
+
return this.data!.clientId;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Thrown when a client session is started under an id the framework reserves
|
|
301
|
+
* for one of its own participants. A reserved id never frees up, so a caller
|
|
302
|
+
* that retries on {@link DuplicateClientIdError} stops on this one.
|
|
125
303
|
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
304
|
+
* It carries no message identity: a host that picks a reserved id has a bug,
|
|
305
|
+
* and no sentence addressed to an end user is true for it.
|
|
128
306
|
*/
|
|
129
|
-
export
|
|
130
|
-
|
|
131
|
-
|
|
307
|
+
export class ReservedClientIdError extends ResponseError<{ readonly clientId: string }> {
|
|
308
|
+
constructor(clientId: string) {
|
|
309
|
+
super(RESERVED_CLIENT_ID_ERROR_CODE, `Client id ${clientId} is reserved for a framework participant`, { clientId });
|
|
310
|
+
this.name = 'ReservedClientIdError';
|
|
311
|
+
Object.setPrototypeOf(this, ReservedClientIdError.prototype);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
get clientId(): string {
|
|
315
|
+
return this.data!.clientId;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* The catalogue declaration behind {@link SaveFailedError}'s message. Its
|
|
321
|
+
* `failures` names each document with the writer's own reason, which stays as
|
|
322
|
+
* written.
|
|
323
|
+
*/
|
|
324
|
+
export const SAVE_FAILED = defineMessage('hydranium/protocol/save-failed', 'Could not save {failures}.');
|
|
325
|
+
|
|
326
|
+
/** One document a save could not write, and why. */
|
|
327
|
+
export interface SaveFailure {
|
|
328
|
+
readonly uri: string;
|
|
329
|
+
readonly reason: string;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Structured payload carried in {@link SaveFailedError.data}. */
|
|
333
|
+
export interface SaveFailedErrorData extends HydraniumMessageData {
|
|
334
|
+
readonly failures: readonly SaveFailure[];
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Thrown by a save that could not write one or more of its documents. A save
|
|
339
|
+
* of several documents writes the others, so the files on disk can be a mix
|
|
340
|
+
* until a later save succeeds.
|
|
341
|
+
*/
|
|
342
|
+
export class SaveFailedError extends ResponseError<SaveFailedErrorData> {
|
|
343
|
+
constructor(failures: readonly SaveFailure[]) {
|
|
344
|
+
const named = failures.map(failure => `${failure.uri} (${failure.reason})`).join(', ');
|
|
345
|
+
super(SAVE_FAILED_ERROR_CODE, SAVE_FAILED.format({ failures: named }), {
|
|
346
|
+
failures,
|
|
347
|
+
...messageData(SAVE_FAILED, { failures: named })
|
|
348
|
+
});
|
|
349
|
+
this.name = 'SaveFailedError';
|
|
350
|
+
Object.setPrototypeOf(this, SaveFailedError.prototype);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
get failures(): readonly SaveFailure[] {
|
|
354
|
+
return this.data!.failures;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** The catalogue declaration behind {@link DocumentSyntaxError}'s sentence. */
|
|
359
|
+
export const DOCUMENT_SYNTAX_ERROR = defineMessage(
|
|
360
|
+
'hydranium/protocol/document-syntax-error',
|
|
361
|
+
'The edit to {uri} was not applied: the document has a syntax error. Fix it in the text first.'
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
/** Structured payload carried in {@link DocumentSyntaxError.data}. */
|
|
365
|
+
export interface DocumentSyntaxErrorData extends HydraniumMessageData {
|
|
366
|
+
readonly uri: string;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Thrown when a write would serialize a model over text that does not parse.
|
|
371
|
+
* A model read from a build with lexer or parser errors lacks what the parser
|
|
372
|
+
* skipped, so writing it would delete that text. A client session refuses a
|
|
373
|
+
* model write while the document's last build has such errors and its text
|
|
374
|
+
* is not blank, unless the write is a `restore`. A diagram refuses an edit,
|
|
375
|
+
* undo or redo that would write such a document, or whose merge or collision
|
|
376
|
+
* reads one. A client's write of text is not refused, since that is how the
|
|
377
|
+
* text is repaired.
|
|
378
|
+
*/
|
|
379
|
+
export class DocumentSyntaxError extends ResponseError<DocumentSyntaxErrorData> {
|
|
380
|
+
constructor(uri: string) {
|
|
381
|
+
super(DOCUMENT_SYNTAX_ERROR_CODE, DOCUMENT_SYNTAX_ERROR.format({ uri }), { uri, ...messageData(DOCUMENT_SYNTAX_ERROR, { uri }) });
|
|
382
|
+
this.name = 'DocumentSyntaxError';
|
|
383
|
+
Object.setPrototypeOf(this, DocumentSyntaxError.prototype);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
get uri(): string {
|
|
387
|
+
return this.data!.uri;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Rebuilds one typed error from the `data` that crossed the wire, or
|
|
393
|
+
* `undefined` when `data` lacks the fields the class reads.
|
|
394
|
+
*/
|
|
395
|
+
type ProtocolErrorReviver = (data: Readonly<Record<string, unknown>>) => ResponseError<unknown> | undefined;
|
|
396
|
+
|
|
397
|
+
/** One entry per typed error class, keyed by its code; a class without one reaches a caller as a plain `ResponseError`. */
|
|
398
|
+
const PROTOCOL_ERROR_REVIVERS: ReadonlyMap<number, ProtocolErrorReviver> = new Map<number, ProtocolErrorReviver>([
|
|
399
|
+
[
|
|
400
|
+
CONFLICT_ERROR_CODE,
|
|
401
|
+
data => (hasConflictFields(data) ? new ConflictError(data.uri, asModelVersion(data.baseVersion), data.actualVersion) : undefined)
|
|
402
|
+
],
|
|
403
|
+
[SESSION_CLOSED_ERROR_CODE, data => (hasClientId(data) ? new SessionClosedError(data.clientId) : undefined)],
|
|
404
|
+
[DOCUMENT_NOT_OPEN_ERROR_CODE, data => (hasDocumentNotOpenFields(data) ? new DocumentNotOpenError(data.uri, data.clientId) : undefined)],
|
|
405
|
+
[DUPLICATE_CLIENT_ID_ERROR_CODE, data => (hasClientId(data) ? new DuplicateClientIdError(data.clientId) : undefined)],
|
|
406
|
+
[RESERVED_CLIENT_ID_ERROR_CODE, data => (hasClientId(data) ? new ReservedClientIdError(data.clientId) : undefined)],
|
|
407
|
+
[SAVE_FAILED_ERROR_CODE, data => (hasSaveFailures(data) ? new SaveFailedError(data.failures) : undefined)],
|
|
408
|
+
[DOCUMENT_SYNTAX_ERROR_CODE, data => (hasUri(data) ? new DocumentSyntaxError(data.uri) : undefined)]
|
|
409
|
+
]);
|
|
410
|
+
|
|
411
|
+
// One check per class of the fields its getters read, run by its reviver on
|
|
412
|
+
// `data` and by its guard on the error, through the getters.
|
|
413
|
+
function hasConflictFields(value: unknown): value is { uri: string; baseVersion: number; actualVersion: TextVersion } {
|
|
414
|
+
return (
|
|
415
|
+
isRecord(value) && typeof value.uri === 'string' && typeof value.baseVersion === 'number' && typeof value.actualVersion === 'number'
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function hasClientId(value: unknown): value is { clientId: string } {
|
|
420
|
+
return isRecord(value) && typeof value.clientId === 'string';
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function hasDocumentNotOpenFields(value: unknown): value is { uri: string; clientId: string } {
|
|
424
|
+
return isRecord(value) && typeof value.uri === 'string' && typeof value.clientId === 'string';
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function hasUri(value: unknown): value is { uri: string } {
|
|
428
|
+
return isRecord(value) && typeof value.uri === 'string';
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function hasSaveFailures(value: unknown): value is { failures: SaveFailure[] } {
|
|
432
|
+
return isRecord(value) && isSaveFailureList(value.failures);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function isRecord(value: unknown): value is Readonly<Record<string, unknown>> {
|
|
436
|
+
return typeof value === 'object' && value !== null;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function isSaveFailureList(value: unknown): value is SaveFailure[] {
|
|
440
|
+
return (
|
|
441
|
+
Array.isArray(value) &&
|
|
442
|
+
value.every(
|
|
443
|
+
(failure: unknown) =>
|
|
444
|
+
typeof failure === 'object' &&
|
|
445
|
+
failure !== null &&
|
|
446
|
+
'uri' in failure &&
|
|
447
|
+
typeof failure.uri === 'string' &&
|
|
448
|
+
'reason' in failure &&
|
|
449
|
+
typeof failure.reason === 'string'
|
|
450
|
+
)
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Whether `error` is a `ResponseError` from any copy of `vscode-jsonrpc`. An
|
|
456
|
+
* install holds several copies, and `instanceof` recognises only its own. An
|
|
457
|
+
* error that merely carries an integer `code` has no `toJson`, and is not one.
|
|
458
|
+
*/
|
|
459
|
+
export function isResponseError(error: unknown): error is ResponseError<unknown> {
|
|
460
|
+
return (
|
|
461
|
+
error instanceof Error && 'code' in error && Number.isInteger(error.code) && 'toJson' in error && typeof error.toJson === 'function'
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* `error` as an instance of the typed error class its code names, with its
|
|
467
|
+
* message, data and stack kept, or `error` itself when its code names none or
|
|
468
|
+
* its data lacks the fields that class reads.
|
|
469
|
+
* An RPC reconstructs every rejection as a plain `ResponseError`, which has
|
|
470
|
+
* none of the class getters.
|
|
471
|
+
*/
|
|
472
|
+
export function reviveProtocolError(error: unknown): unknown {
|
|
473
|
+
if (!isResponseError(error) || error.data === null || typeof error.data !== 'object') {
|
|
474
|
+
return error;
|
|
132
475
|
}
|
|
133
|
-
|
|
134
|
-
|
|
476
|
+
const revived = PROTOCOL_ERROR_REVIVERS.get(error.code)?.({ ...error.data });
|
|
477
|
+
if (!revived) {
|
|
478
|
+
return error;
|
|
135
479
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
480
|
+
return Object.assign(revived, { message: error.message, data: error.data, stack: error.stack });
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* The error the JSON text `json` describes, as `JSON.stringify(error.toJson())`
|
|
485
|
+
* writes it, revived to its typed class as {@link reviveProtocolError} does, or
|
|
486
|
+
* `undefined` when `json` is not a JSON-RPC error envelope. For a transport
|
|
487
|
+
* that carries the error as text, such as a GLSP `RejectAction`'s `detail`.
|
|
488
|
+
*/
|
|
489
|
+
export function reviveProtocolErrorJson(json: string): ResponseError<unknown> | undefined {
|
|
490
|
+
let envelope: unknown;
|
|
491
|
+
try {
|
|
492
|
+
envelope = JSON.parse(json);
|
|
493
|
+
} catch {
|
|
494
|
+
return undefined;
|
|
139
495
|
}
|
|
140
|
-
|
|
496
|
+
if (typeof envelope !== 'object' || envelope === null || !('code' in envelope) || !('message' in envelope)) {
|
|
497
|
+
return undefined;
|
|
498
|
+
}
|
|
499
|
+
const { code, message } = envelope;
|
|
500
|
+
if (typeof code !== 'number' || !Number.isInteger(code) || typeof message !== 'string') {
|
|
501
|
+
return undefined;
|
|
502
|
+
}
|
|
503
|
+
const revived = reviveProtocolError(new ResponseError(code, message, 'data' in envelope ? envelope.data : undefined));
|
|
504
|
+
return isResponseError(revived) ? revived : undefined;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Mark `errorClass`'s prototype with a brand its guard checks, so every
|
|
509
|
+
* instance and every subclass carries it, and nothing else does. `Symbol.for`
|
|
510
|
+
* gives each copy of this package the same symbol, which `instanceof` does
|
|
511
|
+
* not, so the key must stay the same across releases.
|
|
512
|
+
*/
|
|
513
|
+
function brand(errorClass: { readonly prototype: object }, name: string): symbol {
|
|
514
|
+
const key = Symbol.for(`hydranium/protocol/${name}`);
|
|
515
|
+
Object.defineProperty(errorClass.prototype, key, { value: true });
|
|
516
|
+
return key;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const CONFLICT_BRAND = brand(ConflictError, 'ConflictError');
|
|
520
|
+
const SESSION_CLOSED_BRAND = brand(SessionClosedError, 'SessionClosedError');
|
|
521
|
+
const DOCUMENT_NOT_OPEN_BRAND = brand(DocumentNotOpenError, 'DocumentNotOpenError');
|
|
522
|
+
const DUPLICATE_CLIENT_ID_BRAND = brand(DuplicateClientIdError, 'DuplicateClientIdError');
|
|
523
|
+
const RESERVED_CLIENT_ID_BRAND = brand(ReservedClientIdError, 'ReservedClientIdError');
|
|
524
|
+
const SAVE_FAILED_BRAND = brand(SaveFailedError, 'SaveFailedError');
|
|
525
|
+
const DOCUMENT_SYNTAX_ERROR_BRAND = brand(DocumentSyntaxError, 'DocumentSyntaxError');
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Whether `error` is a {@link SessionClosedError} whose getters read valid
|
|
529
|
+
* fields: one thrown, or one {@link reviveProtocolError revived}, from any
|
|
530
|
+
* copy of this package, or a subclass. A bare `ResponseError` is not one,
|
|
531
|
+
* whatever its name and fields, since it lacks the class's brand.
|
|
532
|
+
* `createRpcProxy` revives every rejection, so only a caller on a connection
|
|
533
|
+
* of its own needs to revive first. Each guard below works the same way.
|
|
534
|
+
*/
|
|
535
|
+
export function isSessionClosedError(error: unknown): error is SessionClosedError {
|
|
536
|
+
return hasBrand(error, SESSION_CLOSED_BRAND) && hasClientId(error);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** Whether `error` is a {@link DocumentNotOpenError}. See {@link isSessionClosedError}. */
|
|
540
|
+
export function isDocumentNotOpenError(error: unknown): error is DocumentNotOpenError {
|
|
541
|
+
return hasBrand(error, DOCUMENT_NOT_OPEN_BRAND) && hasDocumentNotOpenFields(error);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** Whether `error` is a {@link DuplicateClientIdError}. See {@link isSessionClosedError}. */
|
|
545
|
+
export function isDuplicateClientIdError(error: unknown): error is DuplicateClientIdError {
|
|
546
|
+
return hasBrand(error, DUPLICATE_CLIENT_ID_BRAND) && hasClientId(error);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/** Whether `error` is a {@link ReservedClientIdError}. See {@link isSessionClosedError}. */
|
|
550
|
+
export function isReservedClientIdError(error: unknown): error is ReservedClientIdError {
|
|
551
|
+
return hasBrand(error, RESERVED_CLIENT_ID_BRAND) && hasClientId(error);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/** Whether `error` is a {@link SaveFailedError}. See {@link isSessionClosedError}. */
|
|
555
|
+
export function isSaveFailedError(error: unknown): error is SaveFailedError {
|
|
556
|
+
return hasBrand(error, SAVE_FAILED_BRAND) && hasSaveFailures(error);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** Whether `error` is a {@link DocumentSyntaxError}. See {@link isSessionClosedError}. */
|
|
560
|
+
export function isDocumentSyntaxError(error: unknown): error is DocumentSyntaxError {
|
|
561
|
+
return hasBrand(error, DOCUMENT_SYNTAX_ERROR_BRAND) && hasUri(error);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** Whether `error` is a {@link ConflictError}. See {@link isSessionClosedError}. */
|
|
565
|
+
export function isConflictError(error: unknown): error is ConflictError {
|
|
566
|
+
return hasBrand(error, CONFLICT_BRAND) && hasConflictFields(error);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Whether `error` carries the brand `key` and the `data` record its getters
|
|
571
|
+
* read, so a guard can check the fields through them. A copy whose class
|
|
572
|
+
* lacks a getter fails that check, since the brand alone is public.
|
|
573
|
+
*/
|
|
574
|
+
function hasBrand(error: unknown, key: symbol): error is ResponseError<unknown> {
|
|
575
|
+
return isResponseError(error) && Reflect.get(error, key) === true && isRecord(error.data);
|
|
141
576
|
}
|