@replit/river 0.23.18 → 0.24.0
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 +17 -16
- package/dist/{chunk-AVL32IMG.js → chunk-AASMR3CQ.js} +20 -16
- package/dist/chunk-AASMR3CQ.js.map +1 -0
- package/dist/chunk-JA57I7MG.js +653 -0
- package/dist/chunk-JA57I7MG.js.map +1 -0
- package/dist/chunk-KX5PQRVN.js +382 -0
- package/dist/chunk-KX5PQRVN.js.map +1 -0
- package/dist/{chunk-EV5HW4IC.js → chunk-KYYB4DUR.js} +65 -53
- package/dist/chunk-KYYB4DUR.js.map +1 -0
- package/dist/chunk-NLQPPDOT.js +399 -0
- package/dist/chunk-NLQPPDOT.js.map +1 -0
- package/dist/{chunk-R2HAS3GM.js → chunk-PJGGC3LV.js} +55 -41
- package/dist/chunk-PJGGC3LV.js.map +1 -0
- package/dist/{chunk-7MJYOL32.js → chunk-RXJLI2OP.js} +15 -23
- package/dist/chunk-RXJLI2OP.js.map +1 -0
- package/dist/{chunk-6LCL2ZZF.js → chunk-TAH2GVTJ.js} +1 -1
- package/dist/chunk-TAH2GVTJ.js.map +1 -0
- package/dist/chunk-ZAT3R4CU.js +277 -0
- package/dist/chunk-ZAT3R4CU.js.map +1 -0
- package/dist/{client-5776a6bb.d.ts → client-ba0d3315.d.ts} +12 -15
- package/dist/{connection-bd35d442.d.ts → connection-c3a96d09.d.ts} +1 -5
- package/dist/connection-d33e3246.d.ts +11 -0
- package/dist/{handshake-a947c234.d.ts → handshake-cdead82a.d.ts} +148 -183
- package/dist/logging/index.cjs.map +1 -1
- package/dist/logging/index.d.cts +1 -1
- package/dist/logging/index.d.ts +1 -1
- package/dist/logging/index.js +1 -1
- package/dist/{index-ea74cdbb.d.ts → message-e6c560fd.d.ts} +2 -2
- package/dist/router/index.cjs +104 -63
- package/dist/router/index.cjs.map +1 -1
- package/dist/router/index.d.cts +11 -10
- package/dist/router/index.d.ts +11 -10
- package/dist/router/index.js +2 -2
- package/dist/server-2ef5e6ec.d.ts +42 -0
- package/dist/{services-38b3f758.d.ts → services-e1417b33.d.ts} +3 -3
- package/dist/transport/impls/uds/client.cjs +1246 -1230
- package/dist/transport/impls/uds/client.cjs.map +1 -1
- package/dist/transport/impls/uds/client.d.cts +4 -4
- package/dist/transport/impls/uds/client.d.ts +4 -4
- package/dist/transport/impls/uds/client.js +7 -13
- package/dist/transport/impls/uds/client.js.map +1 -1
- package/dist/transport/impls/uds/server.cjs +1298 -1151
- package/dist/transport/impls/uds/server.cjs.map +1 -1
- package/dist/transport/impls/uds/server.d.cts +4 -4
- package/dist/transport/impls/uds/server.d.ts +4 -4
- package/dist/transport/impls/uds/server.js +6 -6
- package/dist/transport/impls/ws/client.cjs +976 -965
- package/dist/transport/impls/ws/client.cjs.map +1 -1
- package/dist/transport/impls/ws/client.d.cts +4 -4
- package/dist/transport/impls/ws/client.d.ts +4 -4
- package/dist/transport/impls/ws/client.js +6 -7
- package/dist/transport/impls/ws/client.js.map +1 -1
- package/dist/transport/impls/ws/server.cjs +1182 -1047
- package/dist/transport/impls/ws/server.cjs.map +1 -1
- package/dist/transport/impls/ws/server.d.cts +4 -4
- package/dist/transport/impls/ws/server.d.ts +4 -4
- package/dist/transport/impls/ws/server.js +6 -6
- package/dist/transport/index.cjs +1433 -1360
- package/dist/transport/index.cjs.map +1 -1
- package/dist/transport/index.d.cts +4 -4
- package/dist/transport/index.d.ts +4 -4
- package/dist/transport/index.js +9 -9
- package/dist/util/testHelpers.cjs +743 -310
- package/dist/util/testHelpers.cjs.map +1 -1
- package/dist/util/testHelpers.d.cts +9 -6
- package/dist/util/testHelpers.d.ts +9 -6
- package/dist/util/testHelpers.js +33 -10
- package/dist/util/testHelpers.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-6LCL2ZZF.js.map +0 -1
- package/dist/chunk-7MJYOL32.js.map +0 -1
- package/dist/chunk-AVL32IMG.js.map +0 -1
- package/dist/chunk-DPKOJQWF.js +0 -476
- package/dist/chunk-DPKOJQWF.js.map +0 -1
- package/dist/chunk-EV5HW4IC.js.map +0 -1
- package/dist/chunk-J6N6H2WU.js +0 -476
- package/dist/chunk-J6N6H2WU.js.map +0 -1
- package/dist/chunk-MW5JXLHY.js +0 -348
- package/dist/chunk-MW5JXLHY.js.map +0 -1
- package/dist/chunk-R2HAS3GM.js.map +0 -1
- package/dist/chunk-RJOWZIWB.js +0 -335
- package/dist/chunk-RJOWZIWB.js.map +0 -1
- package/dist/connection-df85db7e.d.ts +0 -17
- package/dist/server-53cd5b7e.d.ts +0 -24
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ProtocolError,
|
|
3
|
+
Transport
|
|
4
|
+
} from "./chunk-ZAT3R4CU.js";
|
|
5
|
+
import {
|
|
6
|
+
SessionStateGraph,
|
|
7
|
+
defaultClientTransportOptions
|
|
8
|
+
} from "./chunk-JA57I7MG.js";
|
|
9
|
+
import {
|
|
10
|
+
ControlMessageHandshakeResponseSchema,
|
|
11
|
+
HandshakeErrorRetriableResponseCodes,
|
|
12
|
+
coerceErrorString,
|
|
13
|
+
getPropagationContext,
|
|
14
|
+
handshakeRequestMessage,
|
|
15
|
+
tracing_default
|
|
16
|
+
} from "./chunk-PJGGC3LV.js";
|
|
17
|
+
|
|
18
|
+
// transport/client.ts
|
|
19
|
+
import { SpanStatusCode } from "@opentelemetry/api";
|
|
20
|
+
|
|
21
|
+
// transport/rateLimit.ts
|
|
22
|
+
var LeakyBucketRateLimit = class {
|
|
23
|
+
budgetConsumed;
|
|
24
|
+
intervalHandles;
|
|
25
|
+
options;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
this.options = options;
|
|
28
|
+
this.budgetConsumed = /* @__PURE__ */ new Map();
|
|
29
|
+
this.intervalHandles = /* @__PURE__ */ new Map();
|
|
30
|
+
}
|
|
31
|
+
getBackoffMs(user) {
|
|
32
|
+
if (!this.budgetConsumed.has(user))
|
|
33
|
+
return 0;
|
|
34
|
+
const exponent = Math.max(0, this.getBudgetConsumed(user) - 1);
|
|
35
|
+
const jitter = Math.floor(Math.random() * this.options.maxJitterMs);
|
|
36
|
+
const backoffMs = Math.min(
|
|
37
|
+
this.options.baseIntervalMs * 2 ** exponent,
|
|
38
|
+
this.options.maxBackoffMs
|
|
39
|
+
);
|
|
40
|
+
return backoffMs + jitter;
|
|
41
|
+
}
|
|
42
|
+
get totalBudgetRestoreTime() {
|
|
43
|
+
return this.options.budgetRestoreIntervalMs * this.options.attemptBudgetCapacity;
|
|
44
|
+
}
|
|
45
|
+
consumeBudget(user) {
|
|
46
|
+
this.stopLeak(user);
|
|
47
|
+
this.budgetConsumed.set(user, this.getBudgetConsumed(user) + 1);
|
|
48
|
+
}
|
|
49
|
+
getBudgetConsumed(user) {
|
|
50
|
+
return this.budgetConsumed.get(user) ?? 0;
|
|
51
|
+
}
|
|
52
|
+
hasBudget(user) {
|
|
53
|
+
return this.getBudgetConsumed(user) < this.options.attemptBudgetCapacity;
|
|
54
|
+
}
|
|
55
|
+
startRestoringBudget(user) {
|
|
56
|
+
if (this.intervalHandles.has(user)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const restoreBudgetForUser = () => {
|
|
60
|
+
const currentBudget = this.budgetConsumed.get(user);
|
|
61
|
+
if (!currentBudget) {
|
|
62
|
+
this.stopLeak(user);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const newBudget = currentBudget - 1;
|
|
66
|
+
if (newBudget === 0) {
|
|
67
|
+
this.budgetConsumed.delete(user);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.budgetConsumed.set(user, newBudget);
|
|
71
|
+
};
|
|
72
|
+
const intervalHandle = setInterval(
|
|
73
|
+
restoreBudgetForUser,
|
|
74
|
+
this.options.budgetRestoreIntervalMs
|
|
75
|
+
);
|
|
76
|
+
this.intervalHandles.set(user, intervalHandle);
|
|
77
|
+
}
|
|
78
|
+
stopLeak(user) {
|
|
79
|
+
if (!this.intervalHandles.has(user)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
clearInterval(this.intervalHandles.get(user));
|
|
83
|
+
this.intervalHandles.delete(user);
|
|
84
|
+
}
|
|
85
|
+
close() {
|
|
86
|
+
for (const user of this.intervalHandles.keys()) {
|
|
87
|
+
this.stopLeak(user);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// transport/client.ts
|
|
93
|
+
import { Value } from "@sinclair/typebox/value";
|
|
94
|
+
var ClientTransport = class extends Transport {
|
|
95
|
+
/**
|
|
96
|
+
* The options for this transport.
|
|
97
|
+
*/
|
|
98
|
+
options;
|
|
99
|
+
retryBudget;
|
|
100
|
+
/**
|
|
101
|
+
* A flag indicating whether the transport should automatically reconnect
|
|
102
|
+
* when a connection is dropped.
|
|
103
|
+
* Realistically, this should always be true for clients unless you are writing
|
|
104
|
+
* tests or a special case where you don't want to reconnect.
|
|
105
|
+
*/
|
|
106
|
+
reconnectOnConnectionDrop = true;
|
|
107
|
+
/**
|
|
108
|
+
* Optional handshake options for this client.
|
|
109
|
+
*/
|
|
110
|
+
handshakeExtensions;
|
|
111
|
+
constructor(clientId, providedOptions) {
|
|
112
|
+
super(clientId, providedOptions);
|
|
113
|
+
this.options = {
|
|
114
|
+
...defaultClientTransportOptions,
|
|
115
|
+
...providedOptions
|
|
116
|
+
};
|
|
117
|
+
this.retryBudget = new LeakyBucketRateLimit(this.options);
|
|
118
|
+
}
|
|
119
|
+
extendHandshake(options) {
|
|
120
|
+
this.handshakeExtensions = options;
|
|
121
|
+
}
|
|
122
|
+
tryReconnecting(to) {
|
|
123
|
+
if (this.reconnectOnConnectionDrop && this.getStatus() === "open") {
|
|
124
|
+
this.connect(to);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
send(to, msg) {
|
|
128
|
+
if (this.getStatus() === "closed") {
|
|
129
|
+
const err = "transport is closed, cant send";
|
|
130
|
+
this.log?.error(err, {
|
|
131
|
+
clientId: this.clientId,
|
|
132
|
+
transportMessage: msg,
|
|
133
|
+
tags: ["invariant-violation"]
|
|
134
|
+
});
|
|
135
|
+
throw new Error(err);
|
|
136
|
+
}
|
|
137
|
+
let session = this.sessions.get(to);
|
|
138
|
+
if (!session) {
|
|
139
|
+
session = this.createUnconnectedSession(to);
|
|
140
|
+
}
|
|
141
|
+
return session.send(msg);
|
|
142
|
+
}
|
|
143
|
+
createUnconnectedSession(to) {
|
|
144
|
+
const session = SessionStateGraph.entrypoints.NoConnection(
|
|
145
|
+
to,
|
|
146
|
+
this.clientId,
|
|
147
|
+
{
|
|
148
|
+
onSessionGracePeriodElapsed: () => {
|
|
149
|
+
this.onSessionGracePeriodElapsed(session);
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
this.options,
|
|
153
|
+
this.log
|
|
154
|
+
);
|
|
155
|
+
this.updateSession(session);
|
|
156
|
+
return session;
|
|
157
|
+
}
|
|
158
|
+
// listeners
|
|
159
|
+
onConnectingFailed(session) {
|
|
160
|
+
const noConnectionSession = super.onConnectingFailed(session);
|
|
161
|
+
this.tryReconnecting(noConnectionSession.to);
|
|
162
|
+
return noConnectionSession;
|
|
163
|
+
}
|
|
164
|
+
onConnClosed(session) {
|
|
165
|
+
const noConnectionSession = super.onConnClosed(session);
|
|
166
|
+
this.tryReconnecting(noConnectionSession.to);
|
|
167
|
+
return noConnectionSession;
|
|
168
|
+
}
|
|
169
|
+
onConnectionEstablished(session, conn) {
|
|
170
|
+
const handshakingSession = SessionStateGraph.transition.ConnectingToHandshaking(session, conn, {
|
|
171
|
+
onConnectionErrored: (err) => {
|
|
172
|
+
const errStr = coerceErrorString(err);
|
|
173
|
+
this.log?.error(
|
|
174
|
+
`connection to ${handshakingSession.to} errored during handshake: ${errStr}`,
|
|
175
|
+
handshakingSession.loggingMetadata
|
|
176
|
+
);
|
|
177
|
+
},
|
|
178
|
+
onConnectionClosed: () => {
|
|
179
|
+
this.log?.warn(
|
|
180
|
+
`connection to ${handshakingSession.to} closed during handshake`,
|
|
181
|
+
handshakingSession.loggingMetadata
|
|
182
|
+
);
|
|
183
|
+
this.onConnClosed(handshakingSession);
|
|
184
|
+
},
|
|
185
|
+
onHandshake: (msg) => {
|
|
186
|
+
this.onHandshakeResponse(handshakingSession, msg);
|
|
187
|
+
},
|
|
188
|
+
onInvalidHandshake: (reason) => {
|
|
189
|
+
this.log?.error(
|
|
190
|
+
`invalid handshake: ${reason}`,
|
|
191
|
+
handshakingSession.loggingMetadata
|
|
192
|
+
);
|
|
193
|
+
this.deleteSession(session);
|
|
194
|
+
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
195
|
+
},
|
|
196
|
+
onHandshakeTimeout: () => {
|
|
197
|
+
this.log?.error(
|
|
198
|
+
`connection to ${handshakingSession.to} timed out during handshake`,
|
|
199
|
+
handshakingSession.loggingMetadata
|
|
200
|
+
);
|
|
201
|
+
this.onConnClosed(handshakingSession);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
this.updateSession(handshakingSession);
|
|
205
|
+
void this.sendHandshake(handshakingSession);
|
|
206
|
+
return handshakingSession;
|
|
207
|
+
}
|
|
208
|
+
rejectHandshakeResponse(session, reason, metadata) {
|
|
209
|
+
session.conn.telemetry?.span.setStatus({
|
|
210
|
+
code: SpanStatusCode.ERROR,
|
|
211
|
+
message: reason
|
|
212
|
+
});
|
|
213
|
+
this.log?.warn(reason, metadata);
|
|
214
|
+
this.deleteSession(session);
|
|
215
|
+
}
|
|
216
|
+
onHandshakeResponse(session, msg) {
|
|
217
|
+
if (!Value.Check(ControlMessageHandshakeResponseSchema, msg.payload)) {
|
|
218
|
+
const reason = `received invalid handshake response`;
|
|
219
|
+
this.rejectHandshakeResponse(session, reason, {
|
|
220
|
+
...session.loggingMetadata,
|
|
221
|
+
transportMessage: msg,
|
|
222
|
+
validationErrors: [
|
|
223
|
+
...Value.Errors(ControlMessageHandshakeResponseSchema, msg.payload)
|
|
224
|
+
]
|
|
225
|
+
});
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (!msg.payload.status.ok) {
|
|
229
|
+
const retriable = msg.payload.status.code ? Value.Check(
|
|
230
|
+
HandshakeErrorRetriableResponseCodes,
|
|
231
|
+
msg.payload.status.code
|
|
232
|
+
) : false;
|
|
233
|
+
const reason = `handshake failed: ${msg.payload.status.reason}`;
|
|
234
|
+
this.rejectHandshakeResponse(session, reason, {
|
|
235
|
+
...session.loggingMetadata,
|
|
236
|
+
transportMessage: msg
|
|
237
|
+
});
|
|
238
|
+
if (retriable) {
|
|
239
|
+
this.tryReconnecting(session.to);
|
|
240
|
+
} else {
|
|
241
|
+
this.deleteSession(session);
|
|
242
|
+
this.protocolError(ProtocolError.HandshakeFailed, reason);
|
|
243
|
+
}
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (msg.payload.status.sessionId !== session.id) {
|
|
247
|
+
const reason = `session id mismatch: expected ${session.id}, got ${msg.payload.status.sessionId}`;
|
|
248
|
+
this.rejectHandshakeResponse(session, reason, {
|
|
249
|
+
...session.loggingMetadata,
|
|
250
|
+
transportMessage: msg
|
|
251
|
+
});
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
this.log?.info(`handshake from ${msg.from} ok`, {
|
|
255
|
+
...session.loggingMetadata,
|
|
256
|
+
transportMessage: msg
|
|
257
|
+
});
|
|
258
|
+
const connectedSession = SessionStateGraph.transition.HandshakingToConnected(session, {
|
|
259
|
+
onConnectionErrored: (err) => {
|
|
260
|
+
const errStr = coerceErrorString(err);
|
|
261
|
+
this.log?.warn(
|
|
262
|
+
`connection to ${connectedSession.to} errored: ${errStr}`,
|
|
263
|
+
connectedSession.loggingMetadata
|
|
264
|
+
);
|
|
265
|
+
},
|
|
266
|
+
onConnectionClosed: () => {
|
|
267
|
+
this.log?.info(
|
|
268
|
+
`connection to ${connectedSession.to} closed`,
|
|
269
|
+
connectedSession.loggingMetadata
|
|
270
|
+
);
|
|
271
|
+
this.onConnClosed(connectedSession);
|
|
272
|
+
},
|
|
273
|
+
onMessage: (msg2) => this.handleMsg(msg2),
|
|
274
|
+
onInvalidMessage: (reason) => {
|
|
275
|
+
this.deleteSession(connectedSession);
|
|
276
|
+
this.protocolError(ProtocolError.MessageOrderingViolated, reason);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
this.updateSession(connectedSession);
|
|
280
|
+
this.retryBudget.startRestoringBudget(connectedSession.to);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Manually attempts to connect to a client.
|
|
284
|
+
* @param to The client ID of the node to connect to.
|
|
285
|
+
*/
|
|
286
|
+
connect(to) {
|
|
287
|
+
let session = this.sessions.get(to);
|
|
288
|
+
session ??= this.createUnconnectedSession(to);
|
|
289
|
+
if (session.state !== "NoConnection" /* NoConnection */) {
|
|
290
|
+
this.log?.debug(
|
|
291
|
+
`session to ${to} has state ${session.state}, skipping connect attempt`,
|
|
292
|
+
session.loggingMetadata
|
|
293
|
+
);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (this.getStatus() !== "open") {
|
|
297
|
+
this.log?.info(
|
|
298
|
+
`transport state is no longer open, cancelling attempt to connect to ${to}`,
|
|
299
|
+
session.loggingMetadata
|
|
300
|
+
);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (!this.retryBudget.hasBudget(to)) {
|
|
304
|
+
const budgetConsumed = this.retryBudget.getBudgetConsumed(to);
|
|
305
|
+
const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;
|
|
306
|
+
this.log?.error(errMsg, session.loggingMetadata);
|
|
307
|
+
this.protocolError(ProtocolError.RetriesExceeded, errMsg);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
let sleep = Promise.resolve();
|
|
311
|
+
const backoffMs = this.retryBudget.getBackoffMs(to);
|
|
312
|
+
if (backoffMs > 0) {
|
|
313
|
+
sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));
|
|
314
|
+
}
|
|
315
|
+
this.log?.info(
|
|
316
|
+
`attempting connection to ${to} (${backoffMs}ms backoff)`,
|
|
317
|
+
session.loggingMetadata
|
|
318
|
+
);
|
|
319
|
+
this.retryBudget.consumeBudget(to);
|
|
320
|
+
const reconnectPromise = tracing_default.startActiveSpan("connect", async (span) => {
|
|
321
|
+
try {
|
|
322
|
+
span.addEvent("backoff", { backoffMs });
|
|
323
|
+
await sleep;
|
|
324
|
+
if (this.getStatus() !== "open") {
|
|
325
|
+
throw new Error("transport state is no longer open");
|
|
326
|
+
}
|
|
327
|
+
span.addEvent("connecting");
|
|
328
|
+
return await this.createNewOutgoingConnection(to);
|
|
329
|
+
} catch (err) {
|
|
330
|
+
const errStr = coerceErrorString(err);
|
|
331
|
+
span.recordException(errStr);
|
|
332
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
333
|
+
throw err;
|
|
334
|
+
} finally {
|
|
335
|
+
span.end();
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
const connectingSession = SessionStateGraph.transition.NoConnectionToConnecting(
|
|
339
|
+
session,
|
|
340
|
+
reconnectPromise,
|
|
341
|
+
{
|
|
342
|
+
onConnectionEstablished: (conn) => {
|
|
343
|
+
this.log?.debug(
|
|
344
|
+
`connection to ${connectingSession.to} established`,
|
|
345
|
+
connectingSession.loggingMetadata
|
|
346
|
+
);
|
|
347
|
+
this.onConnectionEstablished(connectingSession, conn);
|
|
348
|
+
},
|
|
349
|
+
onConnectionFailed: (error) => {
|
|
350
|
+
const errStr = coerceErrorString(error);
|
|
351
|
+
this.log?.error(
|
|
352
|
+
`error connecting to ${connectingSession.to}: ${errStr}`,
|
|
353
|
+
connectingSession.loggingMetadata
|
|
354
|
+
);
|
|
355
|
+
this.onConnectingFailed(connectingSession);
|
|
356
|
+
},
|
|
357
|
+
onConnectionTimeout: () => {
|
|
358
|
+
this.log?.error(
|
|
359
|
+
`connection to ${connectingSession.to} timed out`,
|
|
360
|
+
connectingSession.loggingMetadata
|
|
361
|
+
);
|
|
362
|
+
this.onConnectingFailed(connectingSession);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
);
|
|
366
|
+
this.updateSession(connectingSession);
|
|
367
|
+
}
|
|
368
|
+
async sendHandshake(session) {
|
|
369
|
+
let metadata = void 0;
|
|
370
|
+
if (this.handshakeExtensions) {
|
|
371
|
+
metadata = await this.handshakeExtensions.construct();
|
|
372
|
+
}
|
|
373
|
+
const requestMsg = handshakeRequestMessage({
|
|
374
|
+
from: this.clientId,
|
|
375
|
+
to: session.to,
|
|
376
|
+
sessionId: session.id,
|
|
377
|
+
expectedSessionState: {
|
|
378
|
+
nextExpectedSeq: session.ack,
|
|
379
|
+
nextSentSeq: session.nextSeq()
|
|
380
|
+
},
|
|
381
|
+
metadata,
|
|
382
|
+
tracing: getPropagationContext(session.telemetry.ctx)
|
|
383
|
+
});
|
|
384
|
+
this.log?.debug(`sending handshake request to ${session.to}`, {
|
|
385
|
+
...session.loggingMetadata,
|
|
386
|
+
transportMessage: requestMsg
|
|
387
|
+
});
|
|
388
|
+
session.sendHandshake(requestMsg);
|
|
389
|
+
}
|
|
390
|
+
close() {
|
|
391
|
+
this.retryBudget.close();
|
|
392
|
+
super.close();
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
export {
|
|
397
|
+
ClientTransport
|
|
398
|
+
};
|
|
399
|
+
//# sourceMappingURL=chunk-NLQPPDOT.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../transport/client.ts","../transport/rateLimit.ts"],"sourcesContent":["import { SpanStatusCode } from '@opentelemetry/api';\nimport { ClientHandshakeOptions } from '../router/handshake';\nimport {\n ControlMessageHandshakeResponseSchema,\n HandshakeErrorRetriableResponseCodes,\n OpaqueTransportMessage,\n PartialTransportMessage,\n TransportClientId,\n handshakeRequestMessage,\n} from './message';\nimport {\n ClientTransportOptions,\n ProvidedClientTransportOptions,\n defaultClientTransportOptions,\n} from './options';\nimport { LeakyBucketRateLimit } from './rateLimit';\nimport { Transport } from './transport';\nimport { coerceErrorString } from '../util/stringify';\nimport { ProtocolError } from './events';\nimport { Value } from '@sinclair/typebox/value';\nimport tracer, { getPropagationContext } from '../tracing';\nimport { Connection } from './connection';\nimport { MessageMetadata } from '../logging';\nimport { SessionConnecting } from './sessionStateMachine/SessionConnecting';\nimport { SessionHandshaking } from './sessionStateMachine/SessionHandshaking';\nimport { SessionConnected } from './sessionStateMachine/SessionConnected';\nimport { SessionStateGraph } from './sessionStateMachine/transitions';\nimport { SessionState } from './sessionStateMachine/common';\nimport { SessionNoConnection } from './sessionStateMachine/SessionNoConnection';\n\nexport abstract class ClientTransport<\n ConnType extends Connection,\n> extends Transport<ConnType> {\n /**\n * The options for this transport.\n */\n protected options: ClientTransportOptions;\n\n retryBudget: LeakyBucketRateLimit;\n\n /**\n * A flag indicating whether the transport should automatically reconnect\n * when a connection is dropped.\n * Realistically, this should always be true for clients unless you are writing\n * tests or a special case where you don't want to reconnect.\n */\n reconnectOnConnectionDrop = true;\n\n /**\n * Optional handshake options for this client.\n */\n handshakeExtensions?: ClientHandshakeOptions;\n\n constructor(\n clientId: TransportClientId,\n providedOptions?: ProvidedClientTransportOptions,\n ) {\n super(clientId, providedOptions);\n this.options = {\n ...defaultClientTransportOptions,\n ...providedOptions,\n };\n this.retryBudget = new LeakyBucketRateLimit(this.options);\n }\n\n extendHandshake(options: ClientHandshakeOptions) {\n this.handshakeExtensions = options;\n }\n\n /**\n * Abstract method that creates a new {@link Connection} object.\n * This should call {@link handleConnection} when the connection is created.\n * The downstream client implementation needs to implement this.\n *\n * @param to The client ID of the node to connect to.\n * @returns The new connection object.\n */\n protected abstract createNewOutgoingConnection(\n to: TransportClientId,\n ): Promise<ConnType>;\n\n private tryReconnecting(to: string) {\n if (this.reconnectOnConnectionDrop && this.getStatus() === 'open') {\n this.connect(to);\n }\n }\n\n send(to: string, msg: PartialTransportMessage): string {\n if (this.getStatus() === 'closed') {\n const err = 'transport is closed, cant send';\n this.log?.error(err, {\n clientId: this.clientId,\n transportMessage: msg,\n tags: ['invariant-violation'],\n });\n\n throw new Error(err);\n }\n\n let session = this.sessions.get(to);\n if (!session) {\n session = this.createUnconnectedSession(to);\n }\n\n return session.send(msg);\n }\n\n private createUnconnectedSession(to: string): SessionNoConnection {\n const session = SessionStateGraph.entrypoints.NoConnection(\n to,\n this.clientId,\n {\n onSessionGracePeriodElapsed: () => {\n this.onSessionGracePeriodElapsed(session);\n },\n },\n this.options,\n this.log,\n );\n\n this.updateSession(session);\n return session;\n }\n\n // listeners\n protected onConnectingFailed(session: SessionConnecting<ConnType>) {\n const noConnectionSession = super.onConnectingFailed(session);\n this.tryReconnecting(noConnectionSession.to);\n return noConnectionSession;\n }\n\n protected onConnClosed(\n session: SessionHandshaking<ConnType> | SessionConnected<ConnType>,\n ) {\n const noConnectionSession = super.onConnClosed(session);\n this.tryReconnecting(noConnectionSession.to);\n return noConnectionSession;\n }\n\n protected onConnectionEstablished(\n session: SessionConnecting<ConnType>,\n conn: ConnType,\n ): SessionHandshaking<ConnType> {\n // transition to handshaking\n const handshakingSession =\n SessionStateGraph.transition.ConnectingToHandshaking(session, conn, {\n onConnectionErrored: (err) => {\n // just log, when we error we also emit close\n const errStr = coerceErrorString(err);\n this.log?.error(\n `connection to ${handshakingSession.to} errored during handshake: ${errStr}`,\n handshakingSession.loggingMetadata,\n );\n },\n onConnectionClosed: () => {\n this.log?.warn(\n `connection to ${handshakingSession.to} closed during handshake`,\n handshakingSession.loggingMetadata,\n );\n this.onConnClosed(handshakingSession);\n },\n onHandshake: (msg) => {\n this.onHandshakeResponse(handshakingSession, msg);\n },\n onInvalidHandshake: (reason) => {\n this.log?.error(\n `invalid handshake: ${reason}`,\n handshakingSession.loggingMetadata,\n );\n this.deleteSession(session);\n this.protocolError(ProtocolError.HandshakeFailed, reason);\n },\n onHandshakeTimeout: () => {\n this.log?.error(\n `connection to ${handshakingSession.to} timed out during handshake`,\n handshakingSession.loggingMetadata,\n );\n this.onConnClosed(handshakingSession);\n },\n });\n\n this.updateSession(handshakingSession);\n void this.sendHandshake(handshakingSession);\n return handshakingSession;\n }\n\n private rejectHandshakeResponse(\n session: SessionHandshaking<ConnType>,\n reason: string,\n metadata: MessageMetadata,\n ) {\n session.conn.telemetry?.span.setStatus({\n code: SpanStatusCode.ERROR,\n message: reason,\n });\n\n this.log?.warn(reason, metadata);\n this.deleteSession(session);\n }\n\n protected onHandshakeResponse(\n session: SessionHandshaking<ConnType>,\n msg: OpaqueTransportMessage,\n ) {\n // invariant: msg is a handshake response\n if (!Value.Check(ControlMessageHandshakeResponseSchema, msg.payload)) {\n const reason = `received invalid handshake response`;\n this.rejectHandshakeResponse(session, reason, {\n ...session.loggingMetadata,\n transportMessage: msg,\n validationErrors: [\n ...Value.Errors(ControlMessageHandshakeResponseSchema, msg.payload),\n ],\n });\n return;\n }\n\n // invariant: handshake response should be ok\n if (!msg.payload.status.ok) {\n // TODO: remove conditional check after we know code is always present\n const retriable = msg.payload.status.code\n ? Value.Check(\n HandshakeErrorRetriableResponseCodes,\n msg.payload.status.code,\n )\n : false;\n\n const reason = `handshake failed: ${msg.payload.status.reason}`;\n this.rejectHandshakeResponse(session, reason, {\n ...session.loggingMetadata,\n transportMessage: msg,\n });\n\n if (retriable) {\n this.tryReconnecting(session.to);\n } else {\n this.deleteSession(session);\n this.protocolError(ProtocolError.HandshakeFailed, reason);\n }\n\n return;\n }\n\n // invariant: session id should match between client + server\n if (msg.payload.status.sessionId !== session.id) {\n const reason = `session id mismatch: expected ${session.id}, got ${msg.payload.status.sessionId}`;\n this.rejectHandshakeResponse(session, reason, {\n ...session.loggingMetadata,\n transportMessage: msg,\n });\n return;\n }\n\n // transition to connected!\n this.log?.info(`handshake from ${msg.from} ok`, {\n ...session.loggingMetadata,\n transportMessage: msg,\n });\n\n const connectedSession =\n SessionStateGraph.transition.HandshakingToConnected(session, {\n onConnectionErrored: (err) => {\n // just log, when we error we also emit close\n const errStr = coerceErrorString(err);\n this.log?.warn(\n `connection to ${connectedSession.to} errored: ${errStr}`,\n connectedSession.loggingMetadata,\n );\n },\n onConnectionClosed: () => {\n this.log?.info(\n `connection to ${connectedSession.to} closed`,\n connectedSession.loggingMetadata,\n );\n this.onConnClosed(connectedSession);\n },\n onMessage: (msg) => this.handleMsg(msg),\n onInvalidMessage: (reason) => {\n this.deleteSession(connectedSession);\n this.protocolError(ProtocolError.MessageOrderingViolated, reason);\n },\n });\n\n this.updateSession(connectedSession);\n this.retryBudget.startRestoringBudget(connectedSession.to);\n }\n\n /**\n * Manually attempts to connect to a client.\n * @param to The client ID of the node to connect to.\n */\n connect(to: TransportClientId) {\n // create a new session if one does not exist\n let session = this.sessions.get(to);\n session ??= this.createUnconnectedSession(to);\n\n if (session.state !== SessionState.NoConnection) {\n // already trying to connect\n this.log?.debug(\n `session to ${to} has state ${session.state}, skipping connect attempt`,\n session.loggingMetadata,\n );\n return;\n }\n\n if (this.getStatus() !== 'open') {\n this.log?.info(\n `transport state is no longer open, cancelling attempt to connect to ${to}`,\n session.loggingMetadata,\n );\n return;\n }\n\n // check budget\n if (!this.retryBudget.hasBudget(to)) {\n const budgetConsumed = this.retryBudget.getBudgetConsumed(to);\n const errMsg = `tried to connect to ${to} but retry budget exceeded (more than ${budgetConsumed} attempts in the last ${this.retryBudget.totalBudgetRestoreTime}ms)`;\n this.log?.error(errMsg, session.loggingMetadata);\n this.protocolError(ProtocolError.RetriesExceeded, errMsg);\n return;\n }\n\n let sleep = Promise.resolve();\n const backoffMs = this.retryBudget.getBackoffMs(to);\n if (backoffMs > 0) {\n sleep = new Promise((resolve) => setTimeout(resolve, backoffMs));\n }\n\n this.log?.info(\n `attempting connection to ${to} (${backoffMs}ms backoff)`,\n session.loggingMetadata,\n );\n\n this.retryBudget.consumeBudget(to);\n const reconnectPromise = tracer.startActiveSpan('connect', async (span) => {\n try {\n span.addEvent('backoff', { backoffMs });\n await sleep;\n if (this.getStatus() !== 'open') {\n throw new Error('transport state is no longer open');\n }\n\n span.addEvent('connecting');\n return await this.createNewOutgoingConnection(to);\n } catch (err) {\n // rethrow the error so that the promise is rejected\n // as it was before we wrapped it in a span\n const errStr = coerceErrorString(err);\n span.recordException(errStr);\n span.setStatus({ code: SpanStatusCode.ERROR });\n throw err;\n } finally {\n span.end();\n }\n });\n\n const connectingSession =\n SessionStateGraph.transition.NoConnectionToConnecting(\n session,\n reconnectPromise,\n {\n onConnectionEstablished: (conn) => {\n this.log?.debug(\n `connection to ${connectingSession.to} established`,\n connectingSession.loggingMetadata,\n );\n\n // cast here because conn can't be narrowed to ConnType\n // in the callback due to variance rules\n this.onConnectionEstablished(connectingSession, conn as ConnType);\n },\n onConnectionFailed: (error: unknown) => {\n const errStr = coerceErrorString(error);\n this.log?.error(\n `error connecting to ${connectingSession.to}: ${errStr}`,\n connectingSession.loggingMetadata,\n );\n this.onConnectingFailed(connectingSession);\n },\n onConnectionTimeout: () => {\n this.log?.error(\n `connection to ${connectingSession.to} timed out`,\n connectingSession.loggingMetadata,\n );\n this.onConnectingFailed(connectingSession);\n },\n },\n );\n\n this.updateSession(connectingSession);\n }\n\n private async sendHandshake(session: SessionHandshaking<ConnType>) {\n let metadata: unknown = undefined;\n\n if (this.handshakeExtensions) {\n metadata = await this.handshakeExtensions.construct();\n }\n\n const requestMsg = handshakeRequestMessage({\n from: this.clientId,\n to: session.to,\n sessionId: session.id,\n expectedSessionState: {\n nextExpectedSeq: session.ack,\n nextSentSeq: session.nextSeq(),\n },\n metadata,\n tracing: getPropagationContext(session.telemetry.ctx),\n });\n\n this.log?.debug(`sending handshake request to ${session.to}`, {\n ...session.loggingMetadata,\n transportMessage: requestMsg,\n });\n\n session.sendHandshake(requestMsg);\n }\n\n close() {\n this.retryBudget.close();\n super.close();\n }\n}\n","import { TransportClientId } from './message';\n\n/**\n * Options to control the backoff and retry behavior of the client transport's connection behaviour.\n *\n * River implements exponential backoff with jitter to prevent flooding the server\n * when there's an issue with connection establishment.\n *\n * The backoff is calculated via the following:\n * backOff = min(jitter + {@link baseIntervalMs} * 2 ^ budget_consumed, {@link maxBackoffMs})\n *\n * We use a leaky bucket rate limit with a budget of {@link attemptBudgetCapacity} reconnection attempts.\n * Budget only starts to restore after a successful handshake at a rate of one budget per {@link budgetRestoreIntervalMs}.\n */\nexport interface ConnectionRetryOptions {\n /**\n * The base interval to wait before retrying a connection.\n */\n baseIntervalMs: number;\n\n /**\n * The maximum random jitter to add to the total backoff time.\n */\n maxJitterMs: number;\n\n /**\n * The maximum amount of time to wait before retrying a connection.\n * This does not include the jitter.\n */\n maxBackoffMs: number;\n\n /**\n * The max number of times to attempt a connection before a successful handshake.\n * This persists across connections but starts restoring budget after a successful handshake.\n * The restoration interval depends on {@link budgetRestoreIntervalMs}\n */\n attemptBudgetCapacity: number;\n\n /**\n * After a successful connection attempt, how long to wait before we restore a single budget.\n */\n budgetRestoreIntervalMs: number;\n}\n\nexport class LeakyBucketRateLimit {\n private budgetConsumed: Map<TransportClientId, number>;\n private intervalHandles: Map<\n TransportClientId,\n ReturnType<typeof setInterval>\n >;\n private readonly options: ConnectionRetryOptions;\n\n constructor(options: ConnectionRetryOptions) {\n this.options = options;\n this.budgetConsumed = new Map();\n this.intervalHandles = new Map();\n }\n\n getBackoffMs(user: TransportClientId) {\n if (!this.budgetConsumed.has(user)) return 0;\n\n const exponent = Math.max(0, this.getBudgetConsumed(user) - 1);\n const jitter = Math.floor(Math.random() * this.options.maxJitterMs);\n const backoffMs = Math.min(\n this.options.baseIntervalMs * 2 ** exponent,\n this.options.maxBackoffMs,\n );\n\n return backoffMs + jitter;\n }\n\n get totalBudgetRestoreTime() {\n return (\n this.options.budgetRestoreIntervalMs * this.options.attemptBudgetCapacity\n );\n }\n\n consumeBudget(user: TransportClientId) {\n // If we're consuming again, let's ensure that we're not leaking\n this.stopLeak(user);\n this.budgetConsumed.set(user, this.getBudgetConsumed(user) + 1);\n }\n\n getBudgetConsumed(user: TransportClientId) {\n return this.budgetConsumed.get(user) ?? 0;\n }\n\n hasBudget(user: TransportClientId) {\n return this.getBudgetConsumed(user) < this.options.attemptBudgetCapacity;\n }\n\n startRestoringBudget(user: TransportClientId) {\n if (this.intervalHandles.has(user)) {\n return;\n }\n\n const restoreBudgetForUser = () => {\n const currentBudget = this.budgetConsumed.get(user);\n if (!currentBudget) {\n this.stopLeak(user);\n return;\n }\n\n const newBudget = currentBudget - 1;\n if (newBudget === 0) {\n this.budgetConsumed.delete(user);\n return;\n }\n\n this.budgetConsumed.set(user, newBudget);\n };\n\n const intervalHandle = setInterval(\n restoreBudgetForUser,\n this.options.budgetRestoreIntervalMs,\n );\n\n this.intervalHandles.set(user, intervalHandle);\n }\n\n private stopLeak(user: TransportClientId) {\n if (!this.intervalHandles.has(user)) {\n return;\n }\n\n clearInterval(this.intervalHandles.get(user));\n this.intervalHandles.delete(user);\n }\n\n close() {\n for (const user of this.intervalHandles.keys()) {\n this.stopLeak(user);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA,SAAS,sBAAsB;;;AC4CxB,IAAM,uBAAN,MAA2B;AAAA,EACxB;AAAA,EACA;AAAA,EAIS;AAAA,EAEjB,YAAY,SAAiC;AAC3C,SAAK,UAAU;AACf,SAAK,iBAAiB,oBAAI,IAAI;AAC9B,SAAK,kBAAkB,oBAAI,IAAI;AAAA,EACjC;AAAA,EAEA,aAAa,MAAyB;AACpC,QAAI,CAAC,KAAK,eAAe,IAAI,IAAI;AAAG,aAAO;AAE3C,UAAM,WAAW,KAAK,IAAI,GAAG,KAAK,kBAAkB,IAAI,IAAI,CAAC;AAC7D,UAAM,SAAS,KAAK,MAAM,KAAK,OAAO,IAAI,KAAK,QAAQ,WAAW;AAClE,UAAM,YAAY,KAAK;AAAA,MACrB,KAAK,QAAQ,iBAAiB,KAAK;AAAA,MACnC,KAAK,QAAQ;AAAA,IACf;AAEA,WAAO,YAAY;AAAA,EACrB;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,qBAAqB,MAAM;AACzB,eAAK,KAAK;AAAA,YACR,iBAAiB,kBAAkB,EAAE;AAAA,YACrC,kBAAkB;AAAA,UACpB;AACA,eAAK,mBAAmB,iBAAiB;AAAA,QAC3C;AAAA,MACF;AAAA,IACF;AAEF,SAAK,cAAc,iBAAiB;AAAA,EACtC;AAAA,EAEA,MAAc,cAAc,SAAuC;AACjE,QAAI,WAAoB;AAExB,QAAI,KAAK,qBAAqB;AAC5B,iBAAW,MAAM,KAAK,oBAAoB,UAAU;AAAA,IACtD;AAEA,UAAM,aAAa,wBAAwB;AAAA,MACzC,MAAM,KAAK;AAAA,MACX,IAAI,QAAQ;AAAA,MACZ,WAAW,QAAQ;AAAA,MACnB,sBAAsB;AAAA,QACpB,iBAAiB,QAAQ;AAAA,QACzB,aAAa,QAAQ,QAAQ;AAAA,MAC/B;AAAA,MACA;AAAA,MACA,SAAS,sBAAsB,QAAQ,UAAU,GAAG;AAAA,IACtD,CAAC;AAED,SAAK,KAAK,MAAM,gCAAgC,QAAQ,EAAE,IAAI;AAAA,MAC5D,GAAG,QAAQ;AAAA,MACX,kBAAkB;AAAA,IACpB,CAAC;AAED,YAAQ,cAAc,UAAU;AAAA,EAClC;AAAA,EAEA,QAAQ;AACN,SAAK,YAAY,MAAM;AACvB,UAAM,MAAM;AAAA,EACd;AACF;","names":["msg"]}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
// transport/id.ts
|
|
2
|
+
import { customAlphabet } from "nanoid";
|
|
3
|
+
var alphabet = customAlphabet(
|
|
4
|
+
"1234567890abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ"
|
|
5
|
+
);
|
|
6
|
+
var generateId = () => alphabet(12);
|
|
7
|
+
|
|
1
8
|
// transport/message.ts
|
|
2
9
|
import { Type } from "@sinclair/typebox";
|
|
3
|
-
import { nanoid } from "nanoid";
|
|
4
10
|
var TransportMessageSchema = (t) => Type.Object({
|
|
5
11
|
id: Type.String(),
|
|
6
12
|
from: Type.String(),
|
|
@@ -35,18 +41,29 @@ var ControlMessageHandshakeRequestSchema = Type.Object({
|
|
|
35
41
|
* used by the server to know whether this is a new or a reestablished connection, and whether it
|
|
36
42
|
* is compatible with what it already has.
|
|
37
43
|
*/
|
|
38
|
-
expectedSessionState: Type.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
})
|
|
47
|
-
),
|
|
44
|
+
expectedSessionState: Type.Object({
|
|
45
|
+
// what the client expects the server to send next
|
|
46
|
+
nextExpectedSeq: Type.Integer(),
|
|
47
|
+
// TODO: remove optional once we know all servers
|
|
48
|
+
// are nextSentSeq here
|
|
49
|
+
// what the server expects the client to send next
|
|
50
|
+
nextSentSeq: Type.Optional(Type.Integer())
|
|
51
|
+
}),
|
|
48
52
|
metadata: Type.Optional(Type.Unknown())
|
|
49
53
|
});
|
|
54
|
+
var HandshakeErrorRetriableResponseCodes = Type.Union([
|
|
55
|
+
Type.Literal("SESSION_STATE_MISMATCH")
|
|
56
|
+
]);
|
|
57
|
+
var HandshakeErrorFatalResponseCodes = Type.Union([
|
|
58
|
+
Type.Literal("MALFORMED_HANDSHAKE_META"),
|
|
59
|
+
Type.Literal("MALFORMED_HANDSHAKE"),
|
|
60
|
+
Type.Literal("PROTOCOL_VERSION_MISMATCH"),
|
|
61
|
+
Type.Literal("REJECTED_BY_CUSTOM_HANDLER")
|
|
62
|
+
]);
|
|
63
|
+
var HandshakeErrorResponseCodes = Type.Union([
|
|
64
|
+
HandshakeErrorRetriableResponseCodes,
|
|
65
|
+
HandshakeErrorFatalResponseCodes
|
|
66
|
+
]);
|
|
50
67
|
var ControlMessageHandshakeResponseSchema = Type.Object({
|
|
51
68
|
type: Type.Literal("HANDSHAKE_RESP"),
|
|
52
69
|
status: Type.Union([
|
|
@@ -56,7 +73,10 @@ var ControlMessageHandshakeResponseSchema = Type.Object({
|
|
|
56
73
|
}),
|
|
57
74
|
Type.Object({
|
|
58
75
|
ok: Type.Literal(false),
|
|
59
|
-
reason: Type.String()
|
|
76
|
+
reason: Type.String(),
|
|
77
|
+
// TODO: remove optional once we know all servers
|
|
78
|
+
// are sending code here
|
|
79
|
+
code: Type.Optional(HandshakeErrorResponseCodes)
|
|
60
80
|
})
|
|
61
81
|
])
|
|
62
82
|
});
|
|
@@ -78,12 +98,12 @@ function handshakeRequestMessage({
|
|
|
78
98
|
tracing
|
|
79
99
|
}) {
|
|
80
100
|
return {
|
|
81
|
-
id:
|
|
101
|
+
id: generateId(),
|
|
82
102
|
from,
|
|
83
103
|
to,
|
|
84
104
|
seq: 0,
|
|
85
105
|
ack: 0,
|
|
86
|
-
streamId:
|
|
106
|
+
streamId: generateId(),
|
|
87
107
|
controlFlags: 0,
|
|
88
108
|
tracing,
|
|
89
109
|
payload: {
|
|
@@ -95,19 +115,18 @@ function handshakeRequestMessage({
|
|
|
95
115
|
}
|
|
96
116
|
};
|
|
97
117
|
}
|
|
98
|
-
var SESSION_STATE_MISMATCH = "session state mismatch";
|
|
99
118
|
function handshakeResponseMessage({
|
|
100
119
|
from,
|
|
101
120
|
to,
|
|
102
121
|
status
|
|
103
122
|
}) {
|
|
104
123
|
return {
|
|
105
|
-
id:
|
|
124
|
+
id: generateId(),
|
|
106
125
|
from,
|
|
107
126
|
to,
|
|
108
127
|
seq: 0,
|
|
109
128
|
ack: 0,
|
|
110
|
-
streamId:
|
|
129
|
+
streamId: generateId(),
|
|
111
130
|
controlFlags: 0,
|
|
112
131
|
payload: {
|
|
113
132
|
type: "HANDSHAKE_RESP",
|
|
@@ -115,6 +134,15 @@ function handshakeResponseMessage({
|
|
|
115
134
|
}
|
|
116
135
|
};
|
|
117
136
|
}
|
|
137
|
+
function closeStreamMessage(streamId) {
|
|
138
|
+
return {
|
|
139
|
+
streamId,
|
|
140
|
+
controlFlags: 4 /* StreamClosedBit */,
|
|
141
|
+
payload: {
|
|
142
|
+
type: "CLOSE"
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
118
146
|
function isAck(controlFlag) {
|
|
119
147
|
return (controlFlag & 1 /* AckBit */) === 1 /* AckBit */;
|
|
120
148
|
}
|
|
@@ -132,7 +160,7 @@ function isStreamClose(controlFlag) {
|
|
|
132
160
|
}
|
|
133
161
|
|
|
134
162
|
// package.json
|
|
135
|
-
var version = "0.
|
|
163
|
+
var version = "0.24.0";
|
|
136
164
|
|
|
137
165
|
// util/stringify.ts
|
|
138
166
|
function coerceErrorString(err) {
|
|
@@ -157,16 +185,16 @@ function getPropagationContext(ctx) {
|
|
|
157
185
|
propagation.inject(ctx, tracing);
|
|
158
186
|
return tracing;
|
|
159
187
|
}
|
|
160
|
-
function createSessionTelemetryInfo(
|
|
188
|
+
function createSessionTelemetryInfo(sessionId, to, from, propagationCtx) {
|
|
161
189
|
const parentCtx = propagationCtx ? propagation.extract(context.active(), propagationCtx) : context.active();
|
|
162
190
|
const span = tracer.startSpan(
|
|
163
|
-
`session ${
|
|
191
|
+
`session ${sessionId}`,
|
|
164
192
|
{
|
|
165
193
|
attributes: {
|
|
166
194
|
component: "river",
|
|
167
|
-
"river.session.id":
|
|
168
|
-
"river.session.to":
|
|
169
|
-
"river.session.from":
|
|
195
|
+
"river.session.id": sessionId,
|
|
196
|
+
"river.session.to": to,
|
|
197
|
+
"river.session.from": from
|
|
170
198
|
}
|
|
171
199
|
},
|
|
172
200
|
parentCtx
|
|
@@ -174,21 +202,6 @@ function createSessionTelemetryInfo(session, propagationCtx) {
|
|
|
174
202
|
const ctx = trace.setSpan(parentCtx, span);
|
|
175
203
|
return { span, ctx };
|
|
176
204
|
}
|
|
177
|
-
function createConnectionTelemetryInfo(connection, info) {
|
|
178
|
-
const span = tracer.startSpan(
|
|
179
|
-
`connection ${connection.id}`,
|
|
180
|
-
{
|
|
181
|
-
attributes: {
|
|
182
|
-
component: "river",
|
|
183
|
-
"river.connection.id": connection.id
|
|
184
|
-
},
|
|
185
|
-
links: [{ context: info.span.spanContext() }]
|
|
186
|
-
},
|
|
187
|
-
info.ctx
|
|
188
|
-
);
|
|
189
|
-
const ctx = trace.setSpan(info.ctx, span);
|
|
190
|
-
return { span, ctx };
|
|
191
|
-
}
|
|
192
205
|
function createProcTelemetryInfo(transport, kind, serviceName, procedureName, streamId) {
|
|
193
206
|
const baseCtx = context.active();
|
|
194
207
|
const span = tracer.startSpan(
|
|
@@ -243,25 +256,26 @@ var tracer = trace.getTracer("river", version);
|
|
|
243
256
|
var tracing_default = tracer;
|
|
244
257
|
|
|
245
258
|
export {
|
|
259
|
+
generateId,
|
|
246
260
|
TransportMessageSchema,
|
|
247
261
|
PROTOCOL_VERSION,
|
|
248
262
|
ControlMessageHandshakeRequestSchema,
|
|
263
|
+
HandshakeErrorRetriableResponseCodes,
|
|
249
264
|
ControlMessageHandshakeResponseSchema,
|
|
250
265
|
ControlMessagePayloadSchema,
|
|
251
266
|
OpaqueTransportMessageSchema,
|
|
252
267
|
handshakeRequestMessage,
|
|
253
|
-
SESSION_STATE_MISMATCH,
|
|
254
268
|
handshakeResponseMessage,
|
|
269
|
+
closeStreamMessage,
|
|
255
270
|
isAck,
|
|
256
271
|
isStreamOpen,
|
|
257
272
|
isStreamClose,
|
|
258
273
|
version,
|
|
259
274
|
getPropagationContext,
|
|
260
275
|
createSessionTelemetryInfo,
|
|
261
|
-
createConnectionTelemetryInfo,
|
|
262
276
|
createProcTelemetryInfo,
|
|
263
277
|
createHandlerSpan,
|
|
264
278
|
tracing_default,
|
|
265
279
|
coerceErrorString
|
|
266
280
|
};
|
|
267
|
-
//# sourceMappingURL=chunk-
|
|
281
|
+
//# sourceMappingURL=chunk-PJGGC3LV.js.map
|