-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmessage.ts
More file actions
340 lines (313 loc) Β· 10.3 KB
/
message.ts
File metadata and controls
340 lines (313 loc) Β· 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { Type, TSchema, Static } from '@sinclair/typebox';
import { PropagationContext } from '../tracing';
import { generateId } from './id';
import { ErrResult, ReaderErrorSchema } from '../router';
/**
* Control flags for transport messages.
*/
export const enum ControlFlags {
/**
* Used in heartbeat messages.
*/
AckBit = 0b00001,
/**
* Used in stream open requests.
*/
StreamOpenBit = 0b00010,
/**
* Used when a stream is cancelled due errors or to explicit cancellation
*/
StreamCancelBit = 0b00100,
/**
* Used when writer closes the stream.
*/
StreamClosedBit = 0b01000,
}
/**
* Generic Typebox schema for a transport message.
* @template T The type of the payload.
* @param {T} t The payload schema.
* @returns The transport message schema.
*/
export const TransportMessageSchema = <T extends TSchema>(t: T) =>
Type.Object({
id: Type.String(),
from: Type.String(),
to: Type.String(),
seq: Type.Integer(),
ack: Type.Integer(),
serviceName: Type.Optional(Type.String()),
procedureName: Type.Optional(Type.String()),
streamId: Type.String(),
controlFlags: Type.Integer(),
tracing: Type.Optional(
Type.Object({
traceparent: Type.String(),
tracestate: Type.String(),
}),
),
payload: t,
});
/**
* Defines the schema for a transport acknowledgement message. This is never constructed manually
* and is only used internally by the library for tracking inflight messages.
* @returns The transport message schema.
*/
export const ControlMessageAckSchema = Type.Object({
type: Type.Literal('ACK'),
});
/**
* Defines the schema for a transport close message. This is never constructed manually and is only
* used internally by the library for closing and cleaning up streams.
*/
export const ControlMessageCloseSchema = Type.Object({
type: Type.Literal('CLOSE'),
});
export type ProtocolVersion = 'v1.1' | 'v2.0';
export const currentProtocolVersion = 'v2.0' satisfies ProtocolVersion;
export const acceptedProtocolVersions = ['v1.1', currentProtocolVersion];
export function isAcceptedProtocolVersion(
version: string,
): version is ProtocolVersion {
return acceptedProtocolVersions.includes(version);
}
export const ControlMessageHandshakeRequestSchema = Type.Object({
type: Type.Literal('HANDSHAKE_REQ'),
protocolVersion: Type.String(),
sessionId: Type.String(),
/**
* Specifies what the server's expected session state (from the pov of the client). This can be
* used by the server to know whether this is a new or a reestablished connection, and whether it
* is compatible with what it already has.
*/
expectedSessionState: Type.Object({
// what the client expects the server to send next
nextExpectedSeq: Type.Integer(),
nextSentSeq: Type.Integer(),
}),
metadata: Type.Optional(Type.Unknown()),
});
export const HandshakeErrorRetriableResponseCodes = Type.Union([
Type.Literal('SESSION_STATE_MISMATCH'),
]);
export const HandshakeErrorCustomHandlerFatalResponseCodes = Type.Union([
// The custom validation handler rejected the handler because the client is unsupported.
Type.Literal('REJECTED_UNSUPPORTED_CLIENT'),
// The custom validation handler rejected the handshake.
Type.Literal('REJECTED_BY_CUSTOM_HANDLER'),
]);
export const HandshakeErrorFatalResponseCodes = Type.Union([
HandshakeErrorCustomHandlerFatalResponseCodes,
// The ciient sent a handshake that doesn't comply with the extended handshake metadata.
Type.Literal('MALFORMED_HANDSHAKE_META'),
// The ciient sent a handshake that doesn't comply with ControlMessageHandshakeRequestSchema.
Type.Literal('MALFORMED_HANDSHAKE'),
// The client's protocol version does not match the server's.
Type.Literal('PROTOCOL_VERSION_MISMATCH'),
]);
export const HandshakeErrorResponseCodes = Type.Union([
HandshakeErrorRetriableResponseCodes,
HandshakeErrorFatalResponseCodes,
]);
export const ControlMessageHandshakeResponseSchema = Type.Object({
type: Type.Literal('HANDSHAKE_RESP'),
status: Type.Union([
Type.Object({
ok: Type.Literal(true),
sessionId: Type.String(),
}),
Type.Object({
ok: Type.Literal(false),
reason: Type.String(),
code: HandshakeErrorResponseCodes,
}),
]),
});
export const ControlMessagePayloadSchema = Type.Union([
ControlMessageCloseSchema,
ControlMessageAckSchema,
ControlMessageHandshakeRequestSchema,
ControlMessageHandshakeResponseSchema,
]);
/**
* Defines the schema for an opaque transport message that is agnostic to any
* procedure/service.
* @returns The transport message schema.
*/
export const OpaqueTransportMessageSchema = TransportMessageSchema(
Type.Unknown(),
);
/**
* Represents a transport message. This is the same type as {@link TransportMessageSchema} but
* we can't statically infer generics from generic Typebox schemas so we have to define it again here.
*
* TypeScript can't enforce types when a bitmask is involved, so these are the semantics of
* `controlFlags`:
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `streamId` must be set to a unique value
* (suggestion: use `nanoid`).
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `serviceName` and `procedureName` must be set.
* * If `controlFlags & StreamClosedBit == StreamClosedBit` and the kind is `stream` or `subscription`,
* `payload` should be discarded (usually contains a control message).
* * If `controlFlags & AckBit == AckBit`, the message is an explicit acknowledgement message and doesn't
* contain any payload that is relevant to the application so should not be delivered.
* @template Payload The type of the payload.
*/
export interface TransportMessage<Payload = unknown> {
id: string;
from: TransportClientId;
to: TransportClientId;
seq: number;
ack: number;
serviceName?: string;
procedureName?: string;
streamId: string;
controlFlags: number;
tracing?: PropagationContext;
payload: Payload;
}
export type PartialTransportMessage<Payload = unknown> = Omit<
TransportMessage<Payload>,
'id' | 'from' | 'to' | 'seq' | 'ack'
>;
export function handshakeRequestMessage({
from,
to,
sessionId,
expectedSessionState,
metadata,
tracing,
}: {
from: TransportClientId;
to: TransportClientId;
sessionId: string;
expectedSessionState: Static<
typeof ControlMessageHandshakeRequestSchema
>['expectedSessionState'];
metadata?: unknown;
tracing?: PropagationContext;
}): TransportMessage<Static<typeof ControlMessageHandshakeRequestSchema>> {
return {
id: generateId(),
from,
to,
seq: 0,
ack: 0,
streamId: generateId(),
controlFlags: 0,
tracing,
payload: {
type: 'HANDSHAKE_REQ',
protocolVersion: currentProtocolVersion,
sessionId,
expectedSessionState,
metadata,
} satisfies Static<typeof ControlMessageHandshakeRequestSchema>,
};
}
/**
* This is a reason that can be given during the handshake to indicate that the peer has the wrong
* session state.
*/
export const SESSION_STATE_MISMATCH = 'session state mismatch';
export function handshakeResponseMessage({
from,
to,
status,
}: {
from: TransportClientId;
to: TransportClientId;
status: Static<typeof ControlMessageHandshakeResponseSchema>['status'];
}): TransportMessage<Static<typeof ControlMessageHandshakeResponseSchema>> {
return {
id: generateId(),
from,
to,
seq: 0,
ack: 0,
streamId: generateId(),
controlFlags: 0,
payload: {
type: 'HANDSHAKE_RESP',
status,
} satisfies Static<typeof ControlMessageHandshakeResponseSchema>,
};
}
export function closeStreamMessage(streamId: string): PartialTransportMessage {
return {
streamId,
controlFlags: ControlFlags.StreamClosedBit,
payload: {
type: 'CLOSE' as const,
} satisfies Static<typeof ControlMessagePayloadSchema>,
};
}
export function cancelMessage(
streamId: string,
payload: ErrResult<Static<typeof ReaderErrorSchema>>,
) {
return {
streamId,
controlFlags: ControlFlags.StreamCancelBit,
payload,
};
}
/**
* A type alias for a transport message with an opaque payload.
* @template T - The type of the opaque payload.
*/
export type OpaqueTransportMessage = TransportMessage;
export type TransportClientId = string;
/**
* An encoded message that is ready to be sent over the transport.
* The seq number is kept to track which messages have been
* acked by the peer and can be dropped from the send buffer.
*/
export interface EncodedTransportMessage {
id: string;
seq: number;
data: Uint8Array;
}
/**
* Checks if the given control flag (usually found in msg.controlFlag) is an ack message.
* @param controlFlag - The control flag to check.
* @returns True if the control flag contains the AckBit, false otherwise.
*/
export function isAck(controlFlag: number): boolean {
/* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */
return (controlFlag & ControlFlags.AckBit) === ControlFlags.AckBit;
}
/**
* Checks if the given control flag (usually found in msg.controlFlag) is a stream open message.
* @param controlFlag - The control flag to check.
* @returns True if the control flag contains the StreamOpenBit, false otherwise.
*/
export function isStreamOpen(controlFlag: number): boolean {
return (
/* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */
(controlFlag & ControlFlags.StreamOpenBit) === ControlFlags.StreamOpenBit
);
}
/**
* Checks if the given control flag (usually found in msg.controlFlag) is a stream close message.
* @param controlFlag - The control flag to check.
* @returns True if the control flag contains the StreamCloseBit, false otherwise.
*/
export function isStreamClose(controlFlag: number): boolean {
return (
/* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */
(controlFlag & ControlFlags.StreamClosedBit) ===
ControlFlags.StreamClosedBit
);
}
/**
* Checks if the given control flag (usually found in msg.controlFlag) is an cancel message.
* @param controlFlag - The control flag to check.
* @returns True if the control flag contains the CancelBit, false otherwise
*/
export function isStreamCancel(controlFlag: number): boolean {
return (
/* eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison */
(controlFlag & ControlFlags.StreamCancelBit) ===
ControlFlags.StreamCancelBit
);
}