@cosyte/mllp 0.1.9 → 0.1.11

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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.11
4
+ ### Patch Changes
5
+
6
+ - 6808571: A client can now bound how long it waits to establish a connection. New option `ClientOptions.connectTimeoutMs` (also on `createStarterClient`): an integer number of milliseconds from 1 to 2147483647 that covers the TCP connect and, with TLS on, the handshake after it. Unset means no bound, the behaviour so far, in which a connect to an address that drops packets waits on the operating system and a TLS handshake against a peer that accepts the TCP connection and never answers waits indefinitely. On expiry the socket is destroyed and `connect()` rejects with an `MllpConnectionError` whose `phase` is `connect` and whose `connectionCause` is the new `connect-timeout`; nothing was written, so no message can have reached the peer. On an auto-reconnect attempt the expiry feeds the reconnect policy like any other connect failure: `RetryContext.lastError` carries `code: "ETIMEDOUT"` and it is classified transient, so the client backs off and tries again. `ConnectionErrorCause` gains the member `connect-timeout`, so a caller that switches over every member of that union needs a case for it. A `connectTimeoutMs` that is not an integer in range throws a `RangeError` when the client is created.
7
+
8
+ ## 0.1.10
9
+ ### Patch Changes
10
+
11
+ - 84c8ad6: A server can now bound how many connections it holds at once. New option `ServerOptions.maxConnections` (a positive integer; unset means no cap, the behaviour so far): a connection that arrives while the server already holds that many is closed at accept, before any byte is read and before any TLS handshake, and never reaches `onMessage` or the `'connection'` event. Each one is reported on the new `'connectionRejected'` event, a frozen `ConnectionRejectedEvent` carrying the new stable code `MLLP_CONNECTION_LIMIT_REACHED`, the cap and the peer's address and port. The count is of open sockets, so it bounds file descriptors. New exports: `MLLP_CONNECTION_LIMIT_REACHED` and the type `ConnectionRejectedEvent`. Nothing changes for a caller who does not set the option; a `maxConnections` that is not a positive integer throws a `RangeError` when the server is created.
12
+
3
13
  ## 0.1.9
4
14
  ### Patch Changes
5
15
 
package/dist/index.cjs CHANGED
@@ -1744,6 +1744,8 @@ function isWildcardHost(host) {
1744
1744
  }
1745
1745
  return false;
1746
1746
  }
1747
+ var MLLP_CONNECTION_LIMIT_REACHED = "MLLP_CONNECTION_LIMIT_REACHED";
1748
+ var CONNECTION_LIMIT_MESSAGE = "connection closed at accept: the server already holds maxConnections connections";
1747
1749
  var SERVER_DEFAULT_FRAMING = {
1748
1750
  allowFsOnly: true,
1749
1751
  allowLfAfterFs: true,
@@ -1790,6 +1792,12 @@ var MllpServer = class extends events.EventEmitter {
1790
1792
  */
1791
1793
  constructor(opts) {
1792
1794
  super();
1795
+ const maxConnections = opts.maxConnections;
1796
+ if (maxConnections !== void 0 && !(Number.isSafeInteger(maxConnections) && maxConnections >= 1)) {
1797
+ throw new RangeError(
1798
+ "ServerOptions.maxConnections must be a positive integer, or left unset for no cap"
1799
+ );
1800
+ }
1793
1801
  this._opts = opts;
1794
1802
  this._isTls = opts.tls !== void 0;
1795
1803
  let cipherPolicy = null;
@@ -1846,6 +1854,29 @@ var MllpServer = class extends events.EventEmitter {
1846
1854
  this.emit("error", err);
1847
1855
  }
1848
1856
  });
1857
+ if (maxConnections !== void 0) {
1858
+ this._netServer.maxConnections = maxConnections;
1859
+ this._netServer.on("drop", (data) => {
1860
+ this._onConnectionOverCap(maxConnections, data);
1861
+ });
1862
+ }
1863
+ }
1864
+ /**
1865
+ * Report a connection the listener closed at accept because the cap was full.
1866
+ *
1867
+ * Contained: this runs inside the listener's own 'drop' callback, so a throwing subscriber would
1868
+ * otherwise unwind into the accept path. The throw is surfaced on 'error' instead.
1869
+ */
1870
+ _onConnectionOverCap(maxConnections, data) {
1871
+ const event = Object.freeze({
1872
+ code: MLLP_CONNECTION_LIMIT_REACHED,
1873
+ message: CONNECTION_LIMIT_MESSAGE,
1874
+ maxConnections,
1875
+ remoteAddress: data?.remoteAddress ?? null,
1876
+ remotePort: data?.remotePort ?? null,
1877
+ timestamp: /* @__PURE__ */ new Date()
1878
+ });
1879
+ this._emitContained("connectionRejected", event);
1849
1880
  }
1850
1881
  /**
1851
1882
  * Start listening on the given port.
@@ -3208,6 +3239,14 @@ var OUTBOUND_WARNING_CONTEXT = Object.freeze({
3208
3239
  byteOffset: 0,
3209
3240
  elapsedSinceSendMs: 0
3210
3241
  });
3242
+ var MAX_CONNECT_TIMEOUT_MS = 2147483647;
3243
+ var CONNECT_TIMEOUT_MESSAGE = "connect timed out: the connection, including its TLS handshake when TLS is on, was not established within connectTimeoutMs";
3244
+ var ConnectTimeoutCause = class extends Error {
3245
+ code = "ETIMEDOUT";
3246
+ constructor() {
3247
+ super(CONNECT_TIMEOUT_MESSAGE);
3248
+ }
3249
+ };
3211
3250
  function identityOptions(host, servername) {
3212
3251
  const target = servername ?? host;
3213
3252
  if (net.isIP(target) === 0) return { servername: target };
@@ -3338,6 +3377,12 @@ var MllpClient = class extends events.EventEmitter {
3338
3377
  */
3339
3378
  constructor(opts) {
3340
3379
  super();
3380
+ const connectTimeoutMs = opts.connectTimeoutMs;
3381
+ if (connectTimeoutMs !== void 0 && !(Number.isInteger(connectTimeoutMs) && connectTimeoutMs >= 1 && connectTimeoutMs <= MAX_CONNECT_TIMEOUT_MS)) {
3382
+ throw new RangeError(
3383
+ "ClientOptions.connectTimeoutMs must be an integer from 1 to 2147483647, or left unset for no bound"
3384
+ );
3385
+ }
3341
3386
  this._opts = opts;
3342
3387
  this._ackTimeoutMs = opts.ackTimeoutMs ?? 3e4;
3343
3388
  this._applicationAckTimeoutMs = opts.applicationAckTimeoutMs;
@@ -3381,6 +3426,9 @@ var MllpClient = class extends events.EventEmitter {
3381
3426
  * TLS failures carry a `connectionCause`: `'tls-verify'` for certificate
3382
3427
  * verification failures, `'tls-handshake'` for TLS-protocol-shaped failures
3383
3428
  * ({@link isTlsProtocolError}); pure TCP failures carry none.
3429
+ * - `MllpConnectionError({ phase: 'connect', connectionCause: 'connect-timeout' })` if
3430
+ * {@link ClientOptions.connectTimeoutMs} is set and the TCP connect, or the TLS handshake
3431
+ * after it, did not complete in time. The socket is destroyed; nothing was written.
3384
3432
  * - `MllpTlsConfigurationError` if the configured cipher-suite list cannot be
3385
3433
  * honoured (the runtime rejects it, or `atnaTransportSecurity` and `ciphers`
3386
3434
  * both declare one). Raised **before** a socket is opened, so nothing is left
@@ -3439,6 +3487,7 @@ var MllpClient = class extends events.EventEmitter {
3439
3487
  const conn = new Connection(connOpts);
3440
3488
  this._attachConnection(conn);
3441
3489
  const connectEventName = this._opts.tls !== void 0 ? "secureConnect" : "connect";
3490
+ this._armConnectTimeout(socket, connectEventName);
3442
3491
  const cleanup = () => {
3443
3492
  if (signal !== void 0) {
3444
3493
  signal.removeEventListener("abort", abortHandler);
@@ -3513,6 +3562,29 @@ var MllpClient = class extends events.EventEmitter {
3513
3562
  });
3514
3563
  return { socket, transport: new TlsTransport(socket) };
3515
3564
  }
3565
+ /**
3566
+ * Bound one connection attempt by {@link ClientOptions.connectTimeoutMs}, when it is set.
3567
+ *
3568
+ * Armed after the `Connection` has registered its own socket listeners (its set-once
3569
+ * `onClose`/`onError` would otherwise remove ours). The timer stops at the socket's connect
3570
+ * event (`'secureConnect'` for TLS, so the handshake is inside the bound) or at its `'close'`,
3571
+ * whichever comes first; a refused connect or an abort therefore leaves no timer behind. On
3572
+ * expiry the socket is destroyed with a {@link ConnectTimeoutCause}, which reaches the same
3573
+ * `'error'` listeners as any other connect failure: `connect()` rejects, and a reconnect
3574
+ * attempt hands it to the reconnect classifier.
3575
+ */
3576
+ _armConnectTimeout(socket, connectEventName) {
3577
+ const timeoutMs = this._opts.connectTimeoutMs;
3578
+ if (timeoutMs === void 0) return;
3579
+ const timer = setTimeout(() => {
3580
+ socket.destroy(new ConnectTimeoutCause());
3581
+ }, timeoutMs);
3582
+ const stop = () => {
3583
+ clearTimeout(timer);
3584
+ };
3585
+ socket.once(connectEventName, stop);
3586
+ socket.once("close", stop);
3587
+ }
3516
3588
  /**
3517
3589
  * Wrap a connect-phase socket error as `MllpConnectionError`, classifying
3518
3590
  * TLS failures into the additive `connectionCause`:
@@ -3527,6 +3599,13 @@ var MllpClient = class extends events.EventEmitter {
3527
3599
  * same shape as plaintext connect failures.
3528
3600
  */
3529
3601
  _wrapConnectError(err, phase) {
3602
+ if (err instanceof ConnectTimeoutCause) {
3603
+ return new MllpConnectionError(err.message, {
3604
+ cause: err,
3605
+ phase,
3606
+ connectionCause: "connect-timeout"
3607
+ });
3608
+ }
3530
3609
  if (this._opts.tls === void 0) {
3531
3610
  return new MllpConnectionError(err.message, { cause: err, phase });
3532
3611
  }
@@ -4019,6 +4098,7 @@ var MllpClient = class extends events.EventEmitter {
4019
4098
  this._emitNegotiatedTlsParameters(socket);
4020
4099
  };
4021
4100
  const connectEventName = this._opts.tls !== void 0 ? "secureConnect" : "connect";
4101
+ this._armConnectTimeout(socket, connectEventName);
4022
4102
  socket.once("error", (sErr) => {
4023
4103
  if (this._lastError === null || this._lastError.message !== sErr.message) {
4024
4104
  this._lastError = sErr;
@@ -4831,6 +4911,7 @@ async function createStarterClient(opts) {
4831
4911
  ...opts.retryStrategy !== void 0 ? { retryStrategy: opts.retryStrategy } : {},
4832
4912
  ...opts.keepaliveIntervalMs !== void 0 ? { keepaliveIntervalMs: opts.keepaliveIntervalMs } : {},
4833
4913
  ...opts.deadPeerTimeoutMs !== void 0 ? { deadPeerTimeoutMs: opts.deadPeerTimeoutMs } : {},
4914
+ ...opts.connectTimeoutMs !== void 0 ? { connectTimeoutMs: opts.connectTimeoutMs } : {},
4834
4915
  ...opts.tls !== void 0 ? { tls: opts.tls } : {}
4835
4916
  };
4836
4917
  const client = createClient(clientOpts);
@@ -5151,13 +5232,14 @@ async function runDifferential(options = {}) {
5151
5232
  }
5152
5233
 
5153
5234
  // src/index.ts
5154
- var VERSION = "0.1.9";
5235
+ var VERSION = "0.1.11";
5155
5236
 
5156
5237
  exports.ATNA_CIPHER_LIST = ATNA_CIPHER_LIST;
5157
5238
  exports.ATNA_CIPHER_SUITES = ATNA_CIPHER_SUITES;
5158
5239
  exports.Connection = Connection;
5159
5240
  exports.FrameReader = FrameReader;
5160
5241
  exports.MLLP_BIND_ALL_INTERFACES = MLLP_BIND_ALL_INTERFACES;
5242
+ exports.MLLP_CONNECTION_LIMIT_REACHED = MLLP_CONNECTION_LIMIT_REACHED;
5161
5243
  exports.MLLP_DIFF_PEER_UNPARSEABLE = MLLP_DIFF_PEER_UNPARSEABLE;
5162
5244
  exports.MLLP_TLS_CIPHER_LIST_REJECTED = MLLP_TLS_CIPHER_LIST_REJECTED;
5163
5245
  exports.MLLP_TLS_CIPHER_OPTION_CONFLICT = MLLP_TLS_CIPHER_OPTION_CONFLICT;