@replit/river 0.18.5 → 0.19.2
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/dist/{chunk-VH3NGOXQ.js → chunk-D5PVGZPQ.js} +5 -3
- package/dist/{chunk-K7CUSLWL.js → chunk-JH275HID.js} +1 -1
- package/dist/{chunk-WER2DWCP.js → chunk-NBE3D667.js} +0 -4
- package/dist/{chunk-6Q3MSICL.js → chunk-SR4DBLJ6.js} +11 -3
- package/dist/{chunk-UABIFWM7.js → chunk-YFPVQTWL.js} +220 -105
- package/dist/{chunk-PUX3U2SZ.js → chunk-ZWPEZS27.js} +1 -1
- package/dist/{connection-893bd769.d.ts → connection-aa0ea000.d.ts} +1 -1
- package/dist/{connection-89918b74.d.ts → connection-cfec12e6.d.ts} +1 -1
- package/dist/index-e2513701.d.ts +342 -0
- package/dist/logging/index.cjs +0 -4
- package/dist/logging/index.d.cts +2 -1
- package/dist/logging/index.d.ts +2 -1
- package/dist/logging/index.js +1 -1
- package/dist/router/index.cjs +11 -2
- package/dist/router/index.d.cts +7 -383
- package/dist/router/index.d.ts +7 -383
- package/dist/router/index.js +3 -3
- package/dist/services-4bba42d8.d.ts +736 -0
- package/dist/services-5fc5712d.d.ts +736 -0
- package/dist/transport/impls/uds/client.cjs +80 -53
- package/dist/transport/impls/uds/client.d.cts +5 -5
- package/dist/transport/impls/uds/client.d.ts +5 -5
- package/dist/transport/impls/uds/client.js +4 -4
- package/dist/transport/impls/uds/server.cjs +151 -62
- 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 +4 -4
- package/dist/transport/impls/ws/client.cjs +80 -53
- package/dist/transport/impls/ws/client.d.cts +3 -3
- package/dist/transport/impls/ws/client.d.ts +3 -3
- package/dist/transport/impls/ws/client.js +4 -4
- package/dist/transport/impls/ws/server.cjs +151 -62
- 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 +4 -4
- package/dist/transport/index.cjs +188 -71
- package/dist/transport/index.d.cts +295 -3
- package/dist/transport/index.d.ts +295 -3
- package/dist/transport/index.js +3 -4
- package/dist/util/testHelpers.cjs +410 -401
- package/dist/util/testHelpers.d.cts +3 -3
- package/dist/util/testHelpers.d.ts +3 -3
- package/dist/util/testHelpers.js +4 -5
- package/package.json +1 -1
- package/dist/chunk-RPIDSIQG.js +0 -0
- package/dist/index-46ed19d8.d.ts +0 -111
- package/dist/index-d412ca83.d.ts +0 -420
- package/dist/procedures-bfffcb0b.d.ts +0 -324
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Static } from '@sinclair/typebox';
|
|
2
|
-
import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema, d as ProcedureResult } from '../
|
|
3
|
-
import {
|
|
2
|
+
import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema, d as ProcedureResult } from '../services-4bba42d8.js';
|
|
3
|
+
import { Transport } from '../transport/index.cjs';
|
|
4
4
|
import * as it_pushable from 'it-pushable';
|
|
5
|
-
import { P as PartialTransportMessage, O as OpaqueTransportMessage } from '../index-
|
|
5
|
+
import { P as PartialTransportMessage, C as Connection, O as OpaqueTransportMessage, S as SessionOptions, a as Session } from '../index-e2513701.js';
|
|
6
6
|
import WebSocket from 'isomorphic-ws';
|
|
7
7
|
import http from 'node:http';
|
|
8
8
|
import net from 'node:net';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Static } from '@sinclair/typebox';
|
|
2
|
-
import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema, d as ProcedureResult } from '../
|
|
3
|
-
import {
|
|
2
|
+
import { P as PayloadType, R as RiverError, a as Procedure, S as ServiceContext, b as Result, c as RiverUncaughtSchema, d as ProcedureResult } from '../services-5fc5712d.js';
|
|
3
|
+
import { Transport } from '../transport/index.js';
|
|
4
4
|
import * as it_pushable from 'it-pushable';
|
|
5
|
-
import { P as PartialTransportMessage, O as OpaqueTransportMessage } from '../index-
|
|
5
|
+
import { P as PartialTransportMessage, C as Connection, O as OpaqueTransportMessage, S as SessionOptions, a as Session } from '../index-e2513701.js';
|
|
6
6
|
import WebSocket from 'isomorphic-ws';
|
|
7
7
|
import http from 'node:http';
|
|
8
8
|
import net from 'node:net';
|
package/dist/util/testHelpers.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
2
|
UNCAUGHT_ERROR,
|
|
3
3
|
pushable
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-RPIDSIQG.js";
|
|
4
|
+
} from "../chunk-SR4DBLJ6.js";
|
|
6
5
|
import {
|
|
7
6
|
Session,
|
|
8
7
|
defaultTransportOptions
|
|
9
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-YFPVQTWL.js";
|
|
10
9
|
import {
|
|
11
10
|
coerceErrorString
|
|
12
|
-
} from "../chunk-
|
|
13
|
-
import "../chunk-
|
|
11
|
+
} from "../chunk-D5PVGZPQ.js";
|
|
12
|
+
import "../chunk-NBE3D667.js";
|
|
14
13
|
import "../chunk-GZ7HCLLM.js";
|
|
15
14
|
|
|
16
15
|
// util/testHelpers.ts
|
package/package.json
CHANGED
package/dist/chunk-RPIDSIQG.js
DELETED
|
File without changes
|
package/dist/index-46ed19d8.d.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import * as _sinclair_typebox from '@sinclair/typebox';
|
|
2
|
-
import { TSchema } from '@sinclair/typebox';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Generic Typebox schema for a transport message.
|
|
6
|
-
* @template T The type of the payload.
|
|
7
|
-
* @param {T} t The payload schema.
|
|
8
|
-
* @returns The transport message schema.
|
|
9
|
-
*/
|
|
10
|
-
declare const TransportMessageSchema: <T extends TSchema>(t: T) => _sinclair_typebox.TObject<{
|
|
11
|
-
id: _sinclair_typebox.TString;
|
|
12
|
-
from: _sinclair_typebox.TString;
|
|
13
|
-
to: _sinclair_typebox.TString;
|
|
14
|
-
seq: _sinclair_typebox.TInteger;
|
|
15
|
-
ack: _sinclair_typebox.TInteger;
|
|
16
|
-
serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
|
|
17
|
-
procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
|
|
18
|
-
streamId: _sinclair_typebox.TString;
|
|
19
|
-
controlFlags: _sinclair_typebox.TInteger;
|
|
20
|
-
payload: T;
|
|
21
|
-
}>;
|
|
22
|
-
/**
|
|
23
|
-
* Defines the schema for an opaque transport message that is agnostic to any
|
|
24
|
-
* procedure/service.
|
|
25
|
-
* @returns The transport message schema.
|
|
26
|
-
*/
|
|
27
|
-
declare const OpaqueTransportMessageSchema: _sinclair_typebox.TObject<{
|
|
28
|
-
id: _sinclair_typebox.TString;
|
|
29
|
-
from: _sinclair_typebox.TString;
|
|
30
|
-
to: _sinclair_typebox.TString;
|
|
31
|
-
seq: _sinclair_typebox.TInteger;
|
|
32
|
-
ack: _sinclair_typebox.TInteger;
|
|
33
|
-
serviceName: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
|
|
34
|
-
procedureName: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
|
|
35
|
-
streamId: _sinclair_typebox.TString;
|
|
36
|
-
controlFlags: _sinclair_typebox.TInteger;
|
|
37
|
-
payload: _sinclair_typebox.TUnknown;
|
|
38
|
-
}>;
|
|
39
|
-
/**
|
|
40
|
-
* Represents a transport message. This is the same type as {@link TransportMessageSchema} but
|
|
41
|
-
* we can't statically infer generics from generic Typebox schemas so we have to define it again here.
|
|
42
|
-
*
|
|
43
|
-
* TypeScript can't enforce types when a bitmask is involved, so these are the semantics of
|
|
44
|
-
* `controlFlags`:
|
|
45
|
-
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `streamId` must be set to a unique value
|
|
46
|
-
* (suggestion: use `nanoid`).
|
|
47
|
-
* * If `controlFlags & StreamOpenBit == StreamOpenBit`, `serviceName` and `procedureName` must be set.
|
|
48
|
-
* * If `controlFlags & StreamClosedBit == StreamClosedBit` and the kind is `stream` or `subscription`,
|
|
49
|
-
* `payload` should be discarded (usually contains a control message).
|
|
50
|
-
* * If `controlFlags & AckBit == AckBit`, the message is an explicit acknowledgement message and doesn't
|
|
51
|
-
* contain any payload that is relevant to the application so should not be delivered.
|
|
52
|
-
* @template Payload The type of the payload.
|
|
53
|
-
*/
|
|
54
|
-
interface TransportMessage<Payload = unknown> {
|
|
55
|
-
id: string;
|
|
56
|
-
from: string;
|
|
57
|
-
to: string;
|
|
58
|
-
seq: number;
|
|
59
|
-
ack: number;
|
|
60
|
-
serviceName?: string;
|
|
61
|
-
procedureName?: string;
|
|
62
|
-
streamId: string;
|
|
63
|
-
controlFlags: number;
|
|
64
|
-
payload: Payload;
|
|
65
|
-
}
|
|
66
|
-
type PartialTransportMessage<Payload = unknown> = Omit<TransportMessage<Payload>, 'id' | 'from' | 'to' | 'seq' | 'ack'>;
|
|
67
|
-
/**
|
|
68
|
-
* A type alias for a transport message with an opaque payload.
|
|
69
|
-
* @template T - The type of the opaque payload.
|
|
70
|
-
*/
|
|
71
|
-
type OpaqueTransportMessage = TransportMessage;
|
|
72
|
-
type TransportClientId = string;
|
|
73
|
-
/**
|
|
74
|
-
* Checks if the given control flag (usually found in msg.controlFlag) is a stream open message.
|
|
75
|
-
* @param controlFlag - The control flag to check.
|
|
76
|
-
* @returns True if the control flag contains the StreamOpenBit, false otherwise.
|
|
77
|
-
*/
|
|
78
|
-
declare function isStreamOpen(controlFlag: number): boolean;
|
|
79
|
-
/**
|
|
80
|
-
* Checks if the given control flag (usually found in msg.controlFlag) is a stream close message.
|
|
81
|
-
* @param controlFlag - The control flag to check.
|
|
82
|
-
* @returns True if the control flag contains the StreamCloseBit, false otherwise.
|
|
83
|
-
*/
|
|
84
|
-
declare function isStreamClose(controlFlag: number): boolean;
|
|
85
|
-
|
|
86
|
-
declare const LoggingLevels: {
|
|
87
|
-
readonly debug: -1;
|
|
88
|
-
readonly info: 0;
|
|
89
|
-
readonly warn: 1;
|
|
90
|
-
readonly error: 2;
|
|
91
|
-
};
|
|
92
|
-
type LoggingLevel = keyof typeof LoggingLevels;
|
|
93
|
-
type LogFn = (msg: string, ctx?: MessageMetadata, level?: LoggingLevel) => void;
|
|
94
|
-
type Logger = {
|
|
95
|
-
[key in LoggingLevel]: LogFn;
|
|
96
|
-
};
|
|
97
|
-
type MessageMetadata = Record<string, unknown> & Partial<{
|
|
98
|
-
protocolVersion: string;
|
|
99
|
-
clientId: string;
|
|
100
|
-
connectedTo: string;
|
|
101
|
-
sessionId: string;
|
|
102
|
-
connId: string;
|
|
103
|
-
fullTransportMessage: OpaqueTransportMessage;
|
|
104
|
-
partialTransportMessage: Partial<PartialTransportMessage>;
|
|
105
|
-
}>;
|
|
106
|
-
declare const stringLogger: LogFn;
|
|
107
|
-
declare const coloredStringLogger: LogFn;
|
|
108
|
-
declare const jsonLogger: LogFn;
|
|
109
|
-
declare function bindLogger(fn: LogFn | Logger | undefined, level?: LoggingLevel): Logger | undefined;
|
|
110
|
-
|
|
111
|
-
export { Logger as L, MessageMetadata as M, OpaqueTransportMessage as O, PartialTransportMessage as P, TransportClientId as T, TransportMessage as a, TransportMessageSchema as b, OpaqueTransportMessageSchema as c, isStreamClose as d, coloredStringLogger as e, bindLogger as f, LogFn as g, isStreamOpen as i, jsonLogger as j, stringLogger as s };
|
package/dist/index-d412ca83.d.ts
DELETED
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
import { C as Codec } from './types-3e5768ec.js';
|
|
2
|
-
import { T as TransportClientId, M as MessageMetadata, P as PartialTransportMessage, a as TransportMessage, O as OpaqueTransportMessage } from './index-46ed19d8.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A connection is the actual raw underlying transport connection.
|
|
6
|
-
* It’s responsible for dispatching to/from the actual connection itself
|
|
7
|
-
* This should be instantiated as soon as the client/server has a connection
|
|
8
|
-
* It’s tied to the lifecycle of the underlying transport connection (i.e. if the WS drops, this connection should be deleted)
|
|
9
|
-
*/
|
|
10
|
-
declare abstract class Connection {
|
|
11
|
-
debugId: string;
|
|
12
|
-
constructor();
|
|
13
|
-
/**
|
|
14
|
-
* Handle adding a callback for when a message is received.
|
|
15
|
-
* @param msg The message that was received.
|
|
16
|
-
*/
|
|
17
|
-
abstract addDataListener(cb: (msg: Uint8Array) => void): void;
|
|
18
|
-
abstract removeDataListener(cb: (msg: Uint8Array) => void): void;
|
|
19
|
-
/**
|
|
20
|
-
* Handle adding a callback for when the connection is closed.
|
|
21
|
-
* This should also be called if an error happens.
|
|
22
|
-
* @param cb The callback to call when the connection is closed.
|
|
23
|
-
*/
|
|
24
|
-
abstract addCloseListener(cb: () => void): void;
|
|
25
|
-
/**
|
|
26
|
-
* Handle adding a callback for when an error is received.
|
|
27
|
-
* This should only be used for logging errors, all cleanup
|
|
28
|
-
* should be delegated to addCloseListener.
|
|
29
|
-
*
|
|
30
|
-
* The implementer should take care such that the implemented
|
|
31
|
-
* connection will call both the close and error callbacks
|
|
32
|
-
* on an error.
|
|
33
|
-
*
|
|
34
|
-
* @param cb The callback to call when an error is received.
|
|
35
|
-
*/
|
|
36
|
-
abstract addErrorListener(cb: (err: Error) => void): void;
|
|
37
|
-
/**
|
|
38
|
-
* Sends a message over the connection.
|
|
39
|
-
* @param msg The message to send.
|
|
40
|
-
* @returns true if the message was sent, false otherwise.
|
|
41
|
-
*/
|
|
42
|
-
abstract send(msg: Uint8Array): boolean;
|
|
43
|
-
/**
|
|
44
|
-
* Closes the connection.
|
|
45
|
-
*/
|
|
46
|
-
abstract close(): void;
|
|
47
|
-
}
|
|
48
|
-
interface SessionOptions {
|
|
49
|
-
/**
|
|
50
|
-
* Frequency at which to send heartbeat acknowledgements
|
|
51
|
-
*/
|
|
52
|
-
heartbeatIntervalMs: number;
|
|
53
|
-
/**
|
|
54
|
-
* Number of elapsed heartbeats without a response message before we consider
|
|
55
|
-
* the connection dead.
|
|
56
|
-
*/
|
|
57
|
-
heartbeatsUntilDead: number;
|
|
58
|
-
/**
|
|
59
|
-
* Duration to wait between connection disconnect and actual session disconnect
|
|
60
|
-
*/
|
|
61
|
-
sessionDisconnectGraceMs: number;
|
|
62
|
-
/**
|
|
63
|
-
* The codec to use for encoding/decoding messages over the wire
|
|
64
|
-
*/
|
|
65
|
-
codec: Codec;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* A session is a higher-level abstraction that operates over the span of potentially multiple transport-level connections
|
|
69
|
-
* - It’s responsible for tracking any metadata for a particular client that might need to be persisted across connections (i.e. the sendBuffer, ack, seq)
|
|
70
|
-
* - This will only be considered disconnected if
|
|
71
|
-
* - the server tells the client that we’ve reconnected but it doesn’t recognize us anymore (server definitely died) or
|
|
72
|
-
* - we hit a grace period after a connection disconnect
|
|
73
|
-
*/
|
|
74
|
-
declare class Session<ConnType extends Connection> {
|
|
75
|
-
private codec;
|
|
76
|
-
private options;
|
|
77
|
-
/**
|
|
78
|
-
* The buffer of messages that have been sent but not yet acknowledged.
|
|
79
|
-
*/
|
|
80
|
-
private sendBuffer;
|
|
81
|
-
/**
|
|
82
|
-
* The active connection associated with this session
|
|
83
|
-
*/
|
|
84
|
-
connection?: ConnType;
|
|
85
|
-
readonly from: TransportClientId;
|
|
86
|
-
readonly to: TransportClientId;
|
|
87
|
-
/**
|
|
88
|
-
* The unique ID of this session.
|
|
89
|
-
*/
|
|
90
|
-
id: string;
|
|
91
|
-
/**
|
|
92
|
-
* What the other side advertised as their session ID
|
|
93
|
-
* for this session.
|
|
94
|
-
*/
|
|
95
|
-
advertisedSessionId?: string;
|
|
96
|
-
/**
|
|
97
|
-
* Number of messages we've sent along this session (excluding handshake and acks)
|
|
98
|
-
*/
|
|
99
|
-
private seq;
|
|
100
|
-
/**
|
|
101
|
-
* Number of unique messages we've received this session (excluding handshake and acks)
|
|
102
|
-
*/
|
|
103
|
-
private ack;
|
|
104
|
-
/**
|
|
105
|
-
* The grace period between when the inner connection is disconnected
|
|
106
|
-
* and when we should consider the entire session disconnected.
|
|
107
|
-
*/
|
|
108
|
-
private disconnectionGrace?;
|
|
109
|
-
/**
|
|
110
|
-
* Number of heartbeats we've sent without a response.
|
|
111
|
-
*/
|
|
112
|
-
private heartbeatMisses;
|
|
113
|
-
/**
|
|
114
|
-
* The interval for sending heartbeats.
|
|
115
|
-
*/
|
|
116
|
-
private heartbeat;
|
|
117
|
-
constructor(conn: ConnType | undefined, from: TransportClientId, to: TransportClientId, options: SessionOptions);
|
|
118
|
-
get loggingMetadata(): Omit<MessageMetadata, 'parsedMsg'>;
|
|
119
|
-
/**
|
|
120
|
-
* Sends a message over the session's connection.
|
|
121
|
-
* If the connection is not ready or the message fails to send, the message can be buffered for retry unless skipped.
|
|
122
|
-
*
|
|
123
|
-
* @param msg The partial message to be sent, which will be constructed into a full message.
|
|
124
|
-
* @param addToSendBuff Whether to add the message to the send buffer for retry.
|
|
125
|
-
* @returns The full transport ID of the message that was attempted to be sent.
|
|
126
|
-
*/
|
|
127
|
-
send(msg: PartialTransportMessage): string;
|
|
128
|
-
sendHeartbeat(): void;
|
|
129
|
-
resetBufferedMessages(): void;
|
|
130
|
-
sendBufferedMessages(): void;
|
|
131
|
-
updateBookkeeping(ack: number, seq: number): void;
|
|
132
|
-
closeStaleConnection(conn?: ConnType): void;
|
|
133
|
-
replaceWithNewConnection(newConn: ConnType): void;
|
|
134
|
-
beginGrace(cb: () => void): void;
|
|
135
|
-
cancelGrace(): void;
|
|
136
|
-
close(): void;
|
|
137
|
-
get connected(): boolean;
|
|
138
|
-
get nextExpectedSeq(): number;
|
|
139
|
-
constructMsg<Payload>(partialMsg: PartialTransportMessage<Payload>): TransportMessage<Payload>;
|
|
140
|
-
inspectSendBuffer(): ReadonlyArray<OpaqueTransportMessage>;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
type ConnectionStatus = 'connect' | 'disconnect';
|
|
144
|
-
declare const ProtocolError: {
|
|
145
|
-
readonly RetriesExceeded: "conn_retry_exceeded";
|
|
146
|
-
readonly HandshakeFailed: "handshake_failed";
|
|
147
|
-
readonly UseAfterDestroy: "use_after_destroy";
|
|
148
|
-
readonly MessageOrderingViolated: "message_ordering_violated";
|
|
149
|
-
};
|
|
150
|
-
type ProtocolErrorType = (typeof ProtocolError)[keyof typeof ProtocolError];
|
|
151
|
-
interface EventMap {
|
|
152
|
-
message: OpaqueTransportMessage;
|
|
153
|
-
connectionStatus: {
|
|
154
|
-
status: ConnectionStatus;
|
|
155
|
-
conn: Connection;
|
|
156
|
-
};
|
|
157
|
-
sessionStatus: {
|
|
158
|
-
status: ConnectionStatus;
|
|
159
|
-
session: Session<Connection>;
|
|
160
|
-
};
|
|
161
|
-
protocolError: {
|
|
162
|
-
type: ProtocolErrorType;
|
|
163
|
-
message: string;
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
type EventTypes = keyof EventMap;
|
|
167
|
-
type EventHandler<K extends EventTypes> = (event: EventMap[K]) => unknown;
|
|
168
|
-
declare class EventDispatcher<T extends EventTypes> {
|
|
169
|
-
private eventListeners;
|
|
170
|
-
numberOfListeners<K extends T>(eventType: K): number;
|
|
171
|
-
addEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
172
|
-
removeEventListener<K extends T>(eventType: K, handler: EventHandler<K>): void;
|
|
173
|
-
dispatchEvent<K extends T>(eventType: K, event: EventMap[K]): void;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Options to control the backoff and retry behavior of the client transport's connection behaviour.
|
|
178
|
-
*
|
|
179
|
-
* River implements exponential backoff with jitter to prevent flooding the server
|
|
180
|
-
* when there's an issue with connection establishment.
|
|
181
|
-
*
|
|
182
|
-
* The backoff is calculated via the following:
|
|
183
|
-
* backOff = min(jitter + {@link baseIntervalMs} * 2 ^ budget_consumed, {@link maxBackoffMs})
|
|
184
|
-
*
|
|
185
|
-
* We use a leaky bucket rate limit with a budget of {@link attemptBudgetCapacity} reconnection attempts.
|
|
186
|
-
* Budget only starts to restore after a successful handshake at a rate of one budget per {@link budgetRestoreIntervalMs}.
|
|
187
|
-
*/
|
|
188
|
-
interface ConnectionRetryOptions {
|
|
189
|
-
/**
|
|
190
|
-
* The base interval to wait before retrying a connection.
|
|
191
|
-
*/
|
|
192
|
-
baseIntervalMs: number;
|
|
193
|
-
/**
|
|
194
|
-
* The maximum random jitter to add to the total backoff time.
|
|
195
|
-
*/
|
|
196
|
-
maxJitterMs: number;
|
|
197
|
-
/**
|
|
198
|
-
* The maximum amount of time to wait before retrying a connection.
|
|
199
|
-
* This does not include the jitter.
|
|
200
|
-
*/
|
|
201
|
-
maxBackoffMs: number;
|
|
202
|
-
/**
|
|
203
|
-
* The max number of times to attempt a connection before a successful handshake.
|
|
204
|
-
* This persists across connections but starts restoring budget after a successful handshake.
|
|
205
|
-
* The restoration interval depends on {@link budgetRestoreIntervalMs}
|
|
206
|
-
*/
|
|
207
|
-
attemptBudgetCapacity: number;
|
|
208
|
-
/**
|
|
209
|
-
* After a successful connection attempt, how long to wait before we restore a single budget.
|
|
210
|
-
*/
|
|
211
|
-
budgetRestoreIntervalMs: number;
|
|
212
|
-
}
|
|
213
|
-
declare class LeakyBucketRateLimit {
|
|
214
|
-
private budgetConsumed;
|
|
215
|
-
private intervalHandles;
|
|
216
|
-
private readonly options;
|
|
217
|
-
constructor(options: ConnectionRetryOptions);
|
|
218
|
-
getBackoffMs(user: TransportClientId): number;
|
|
219
|
-
get totalBudgetRestoreTime(): number;
|
|
220
|
-
consumeBudget(user: TransportClientId): void;
|
|
221
|
-
getBudgetConsumed(user: TransportClientId): number;
|
|
222
|
-
hasBudget(user: TransportClientId): boolean;
|
|
223
|
-
startRestoringBudget(user: TransportClientId): void;
|
|
224
|
-
private stopLeak;
|
|
225
|
-
close(): void;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Represents the possible states of a transport.
|
|
230
|
-
* @property {'open'} open - The transport is open and operational (note that this doesn't mean it is actively connected)
|
|
231
|
-
* @property {'closed'} closed - The transport is closed and not operational, but can be reopened.
|
|
232
|
-
* @property {'destroyed'} destroyed - The transport is permanently destroyed and cannot be reopened.
|
|
233
|
-
*/
|
|
234
|
-
type TransportStatus = 'open' | 'closed' | 'destroyed';
|
|
235
|
-
type TransportOptions = SessionOptions;
|
|
236
|
-
type ProvidedTransportOptions = Partial<TransportOptions>;
|
|
237
|
-
type ProvidedClientTransportOptions = Partial<ClientTransportOptions>;
|
|
238
|
-
type ClientTransportOptions = SessionOptions & ConnectionRetryOptions;
|
|
239
|
-
/**
|
|
240
|
-
* Transports manage the lifecycle (creation/deletion) of sessions and connections. Its responsibilities include:
|
|
241
|
-
*
|
|
242
|
-
* 1) Constructing a new {@link Session} and {@link Connection} on {@link TransportMessage}s from new clients.
|
|
243
|
-
* After constructing the {@link Connection}, {@link onConnect} is called which adds it to the connection map.
|
|
244
|
-
* 2) Delegating message listening of the connection to the newly created {@link Connection}.
|
|
245
|
-
* From this point on, the {@link Connection} is responsible for *reading* and *writing*
|
|
246
|
-
* messages from the connection.
|
|
247
|
-
* 3) When a connection is closed, the {@link Transport} calls {@link onDisconnect} which closes the
|
|
248
|
-
* connection via {@link Connection.close} and removes it from the {@link connections} map.
|
|
249
|
-
|
|
250
|
-
*
|
|
251
|
-
* ```plaintext
|
|
252
|
-
* ▲
|
|
253
|
-
* incoming │
|
|
254
|
-
* messages │
|
|
255
|
-
* ▼
|
|
256
|
-
* ┌─────────────┐ 1:N ┌───────────┐ 1:1* ┌────────────┐
|
|
257
|
-
* │ Transport │ ◄─────► │ Session │ ◄─────► │ Connection │
|
|
258
|
-
* └─────────────┘ └───────────┘ └────────────┘
|
|
259
|
-
* ▲ * (may or may not be initialized yet)
|
|
260
|
-
* │
|
|
261
|
-
* ▼
|
|
262
|
-
* ┌───────────┐
|
|
263
|
-
* │ Message │
|
|
264
|
-
* │ Listeners │
|
|
265
|
-
* └───────────┘
|
|
266
|
-
* ```
|
|
267
|
-
* @abstract
|
|
268
|
-
*/
|
|
269
|
-
declare abstract class Transport<ConnType extends Connection> {
|
|
270
|
-
/**
|
|
271
|
-
* A flag indicating whether the transport has been destroyed.
|
|
272
|
-
* A destroyed transport will not attempt to reconnect and cannot be used again.
|
|
273
|
-
*/
|
|
274
|
-
state: TransportStatus;
|
|
275
|
-
/**
|
|
276
|
-
* The {@link Codec} used to encode and decode messages.
|
|
277
|
-
*/
|
|
278
|
-
codec: Codec;
|
|
279
|
-
/**
|
|
280
|
-
* The client ID of this transport.
|
|
281
|
-
*/
|
|
282
|
-
clientId: TransportClientId;
|
|
283
|
-
/**
|
|
284
|
-
* The map of {@link Session}s managed by this transport.
|
|
285
|
-
*/
|
|
286
|
-
sessions: Map<TransportClientId, Session<ConnType>>;
|
|
287
|
-
/**
|
|
288
|
-
* The map of {@link Connection}s managed by this transport.
|
|
289
|
-
*/
|
|
290
|
-
get connections(): Map<string, ConnType>;
|
|
291
|
-
/**
|
|
292
|
-
* The event dispatcher for handling events of type EventTypes.
|
|
293
|
-
*/
|
|
294
|
-
eventDispatcher: EventDispatcher<EventTypes>;
|
|
295
|
-
/**
|
|
296
|
-
* The options for this transport.
|
|
297
|
-
*/
|
|
298
|
-
protected options: TransportOptions;
|
|
299
|
-
/**
|
|
300
|
-
* Creates a new Transport instance.
|
|
301
|
-
* This should also set up {@link onConnect}, and {@link onDisconnect} listeners.
|
|
302
|
-
* @param codec The codec used to encode and decode messages.
|
|
303
|
-
* @param clientId The client ID of this transport.
|
|
304
|
-
*/
|
|
305
|
-
constructor(clientId: TransportClientId, providedOptions?: ProvidedTransportOptions);
|
|
306
|
-
/**
|
|
307
|
-
* This is called immediately after a new connection is established and we
|
|
308
|
-
* may or may not know the identity of the connected client.
|
|
309
|
-
* It should attach all the necessary listeners to the connection for lifecycle
|
|
310
|
-
* events (i.e. data, close, error)
|
|
311
|
-
*
|
|
312
|
-
* This method is implemented by {@link ClientTransport} and {@link ServerTransport}.
|
|
313
|
-
*/
|
|
314
|
-
protected abstract handleConnection(conn: ConnType, to: TransportClientId): void;
|
|
315
|
-
/**
|
|
316
|
-
* Called when a new connection is established
|
|
317
|
-
* and we know the identity of the connected client.
|
|
318
|
-
* @param conn The connection object.
|
|
319
|
-
*/
|
|
320
|
-
protected onConnect(conn: ConnType, connectedTo: TransportClientId, advertisedSessionId: string): Session<ConnType>;
|
|
321
|
-
protected createSession(to: TransportClientId, conn?: ConnType): Session<ConnType>;
|
|
322
|
-
protected getOrCreateSession(to: TransportClientId, conn?: ConnType): Session<ConnType>;
|
|
323
|
-
protected deleteSession(session: Session<ConnType>): void;
|
|
324
|
-
/**
|
|
325
|
-
* The downstream implementation needs to call this when a connection is closed.
|
|
326
|
-
* @param conn The connection object.
|
|
327
|
-
* @param connectedTo The peer we are connected to.
|
|
328
|
-
*/
|
|
329
|
-
protected onDisconnect(conn: ConnType, session: Session<ConnType>): void;
|
|
330
|
-
/**
|
|
331
|
-
* Parses a message from a Uint8Array into a {@link OpaqueTransportMessage}.
|
|
332
|
-
* @param msg The message to parse.
|
|
333
|
-
* @returns The parsed message, or null if the message is malformed or invalid.
|
|
334
|
-
*/
|
|
335
|
-
protected parseMsg(msg: Uint8Array): OpaqueTransportMessage | null;
|
|
336
|
-
/**
|
|
337
|
-
* Called when a message is received by this transport.
|
|
338
|
-
* You generally shouldn't need to override this in downstream transport implementations.
|
|
339
|
-
* @param msg The received message.
|
|
340
|
-
*/
|
|
341
|
-
protected handleMsg(msg: OpaqueTransportMessage): void;
|
|
342
|
-
/**
|
|
343
|
-
* Adds a listener to this transport.
|
|
344
|
-
* @param the type of event to listen for
|
|
345
|
-
* @param handler The message handler to add.
|
|
346
|
-
*/
|
|
347
|
-
addEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
|
|
348
|
-
/**
|
|
349
|
-
* Removes a listener from this transport.
|
|
350
|
-
* @param the type of event to un-listen on
|
|
351
|
-
* @param handler The message handler to remove.
|
|
352
|
-
*/
|
|
353
|
-
removeEventListener<K extends EventTypes, T extends EventHandler<K>>(type: K, handler: T): void;
|
|
354
|
-
/**
|
|
355
|
-
* Sends a message over this transport, delegating to the appropriate connection to actually
|
|
356
|
-
* send the message.
|
|
357
|
-
* @param msg The message to send.
|
|
358
|
-
* @returns The ID of the sent message or undefined if it wasn't sent
|
|
359
|
-
*/
|
|
360
|
-
send(to: TransportClientId, msg: PartialTransportMessage): string | undefined;
|
|
361
|
-
sendCloseStream(to: TransportClientId, streamId: string): string | undefined;
|
|
362
|
-
protected protocolError(type: ProtocolErrorType, message: string): void;
|
|
363
|
-
/**
|
|
364
|
-
* Default close implementation for transports. You should override this in the downstream
|
|
365
|
-
* implementation if you need to do any additional cleanup and call super.close() at the end.
|
|
366
|
-
* Closes the transport. Any messages sent while the transport is closed will be silently discarded.
|
|
367
|
-
*/
|
|
368
|
-
close(): void;
|
|
369
|
-
/**
|
|
370
|
-
* Default destroy implementation for transports. You should override this in the downstream
|
|
371
|
-
* implementation if you need to do any additional cleanup and call super.destroy() at the end.
|
|
372
|
-
* Destroys the transport. Any messages sent while the transport is destroyed will throw an error.
|
|
373
|
-
*/
|
|
374
|
-
destroy(): void;
|
|
375
|
-
}
|
|
376
|
-
declare abstract class ClientTransport<ConnType extends Connection> extends Transport<ConnType> {
|
|
377
|
-
/**
|
|
378
|
-
* The options for this transport.
|
|
379
|
-
*/
|
|
380
|
-
protected options: ClientTransportOptions;
|
|
381
|
-
/**
|
|
382
|
-
* The map of reconnect promises for each client ID.
|
|
383
|
-
*/
|
|
384
|
-
inflightConnectionPromises: Map<TransportClientId, Promise<ConnType>>;
|
|
385
|
-
retryBudget: LeakyBucketRateLimit;
|
|
386
|
-
/**
|
|
387
|
-
* A flag indicating whether the transport should automatically reconnect
|
|
388
|
-
* when a connection is dropped.
|
|
389
|
-
* Realistically, this should always be true for clients unless you are writing
|
|
390
|
-
* tests or a special case where you don't want to reconnect.
|
|
391
|
-
*/
|
|
392
|
-
reconnectOnConnectionDrop: boolean;
|
|
393
|
-
constructor(clientId: TransportClientId, providedOptions?: ProvidedClientTransportOptions);
|
|
394
|
-
protected handleConnection(conn: ConnType, to: TransportClientId): void;
|
|
395
|
-
receiveHandshakeResponseMessage(data: Uint8Array, conn: ConnType): Session<ConnType> | false;
|
|
396
|
-
/**
|
|
397
|
-
* Abstract method that creates a new {@link Connection} object.
|
|
398
|
-
* This should call {@link handleConnection} when the connection is created.
|
|
399
|
-
* The downstream client implementation needs to implement this.
|
|
400
|
-
*
|
|
401
|
-
* @param to The client ID of the node to connect to.
|
|
402
|
-
* @returns The new connection object.
|
|
403
|
-
*/
|
|
404
|
-
protected abstract createNewOutgoingConnection(to: TransportClientId): Promise<ConnType>;
|
|
405
|
-
/**
|
|
406
|
-
* Manually attempts to connect to a client.
|
|
407
|
-
* @param to The client ID of the node to connect to.
|
|
408
|
-
*/
|
|
409
|
-
connect(to: TransportClientId): Promise<void>;
|
|
410
|
-
protected deleteSession(session: Session<ConnType>): void;
|
|
411
|
-
protected sendHandshake(to: TransportClientId, conn: ConnType): void;
|
|
412
|
-
close(): void;
|
|
413
|
-
}
|
|
414
|
-
declare abstract class ServerTransport<ConnType extends Connection> extends Transport<ConnType> {
|
|
415
|
-
constructor(clientId: TransportClientId, providedOptions?: ProvidedTransportOptions);
|
|
416
|
-
protected handleConnection(conn: ConnType): void;
|
|
417
|
-
receiveHandshakeRequestMessage(data: Uint8Array, conn: ConnType): Session<ConnType> | false;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
export { Connection as C, EventMap as E, ProvidedClientTransportOptions as P, SessionOptions as S, Transport as T, Session as a, ClientTransport as b, ServerTransport as c, ProvidedTransportOptions as d, TransportStatus as e, EventTypes as f, EventHandler as g, ProtocolError as h, ProtocolErrorType as i };
|