@rstreamlabs/webtty 1.11.0 → 1.11.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/README.md CHANGED
@@ -52,6 +52,18 @@ webtty.connect();
52
52
 
53
53
  ## Interactive Sessions
54
54
 
55
+ Connection failures are delivered once through `onError`, including normal
56
+ WebTransport closure before the stream opens. Disconnecting also releases a
57
+ stream that becomes available after the opening was cancelled. A normal close
58
+ after opening still drains buffered output before reporting the session result.
59
+
60
+ Error messages redact values from the configured endpoint's query parameters,
61
+ userinfo and fragment, including their common URL-encoded forms. Avoid logging
62
+ the endpoint separately. An identifiable browser `SecurityError` points to
63
+ browser policy and `connect-src`; an empty browser error reports that diagnostic
64
+ details are unavailable. Such an error does not establish that CSP is the cause.
65
+ Invalid endpoint configuration still throws synchronously without echoing the URL.
66
+
55
67
  For an interactive terminal, write encoded input to stdin and forward terminal
56
68
  resize events.
57
69
 
@@ -8658,10 +8658,61 @@ var WebTTYFrameDecoder = class {
8658
8658
  }
8659
8659
  };
8660
8660
 
8661
- // src/webtty.ts
8661
+ // src/transport-diagnostics.ts
8662
8662
  function getErrorMessage(error) {
8663
- return error instanceof Error ? error.message : String(error);
8663
+ const message = error instanceof Error ? error.message.trim() : String(error ?? "").trim();
8664
+ if (error instanceof Error && error.name === "SecurityError") {
8665
+ return `The browser blocked the connection under its security policy. Check connect-src and browser restrictions.${message ? ` ${message}` : ""}`;
8666
+ }
8667
+ return message || "No diagnostic details were provided.";
8668
+ }
8669
+ function decodeCredential(value) {
8670
+ try {
8671
+ return decodeURIComponent(value);
8672
+ } catch {
8673
+ return value;
8674
+ }
8675
+ }
8676
+ function credentialForms(value) {
8677
+ const encoded = encodeURIComponent(value);
8678
+ const formEncoded = new URLSearchParams({ value }).toString().slice(6);
8679
+ return [value, encoded, formEncoded, encodeURIComponent(encoded)];
8680
+ }
8681
+ function credentialPattern(value) {
8682
+ const escaped = value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
8683
+ return new RegExp(
8684
+ escaped.replace(
8685
+ /%[\da-f]{2}/gi,
8686
+ (escape) => escape.replace(
8687
+ /[a-f]/gi,
8688
+ (hex) => `[${hex.toLowerCase()}${hex.toUpperCase()}]`
8689
+ )
8690
+ ),
8691
+ "g"
8692
+ );
8664
8693
  }
8694
+ function redactEndpointCredentials(message, endpoint) {
8695
+ try {
8696
+ const url = new URL(endpoint);
8697
+ const credentials = [
8698
+ ...url.searchParams.values(),
8699
+ decodeCredential(url.username),
8700
+ decodeCredential(url.password),
8701
+ decodeCredential(url.hash.slice(1))
8702
+ ];
8703
+ const forms = [
8704
+ ...new Set(credentials.filter(Boolean).flatMap(credentialForms))
8705
+ ].sort((a, b) => b.length - a.length);
8706
+ return forms.reduce(
8707
+ (redacted, value) => redacted.replace(credentialPattern(value), "[redacted]"),
8708
+ message
8709
+ );
8710
+ } catch {
8711
+ return "Connection failed: invalid endpoint URL.";
8712
+ }
8713
+ }
8714
+
8715
+ // src/webtty.ts
8665
8716
  function getWebSocketPayload(payload) {
8666
8717
  return new Uint8Array(payload);
8667
8718
  }
@@ -8994,7 +9045,7 @@ function attachCapabilitiesToProto(capabilities) {
8994
9045
  }
8995
9046
  function resolveWebTTYClientTransport(url, transport) {
8996
9047
  if (transport !== void 0) return transport;
8997
- const protocol = typeof url === "string" && url.includes("://") ? new URL(url).protocol : url instanceof URL ? url.protocol : "";
9048
+ const protocol = typeof url === "string" && url.includes("://") ? parseEndpointURL(url).protocol : url instanceof URL ? url.protocol : "";
8998
9049
  switch (protocol.replace(":", "").toLowerCase()) {
8999
9050
  case "webtransport":
9000
9051
  case "wts":
@@ -9003,8 +9054,15 @@ function resolveWebTTYClientTransport(url, transport) {
9003
9054
  return "websocket";
9004
9055
  }
9005
9056
  }
9057
+ function parseEndpointURL(url) {
9058
+ try {
9059
+ return new URL(url);
9060
+ } catch {
9061
+ throw new TypeError("Invalid WebTTY endpoint URL.");
9062
+ }
9063
+ }
9006
9064
  function normalizeWebTransportURL(url) {
9007
- const parsed = typeof url === "string" ? new URL(url) : new URL(url);
9065
+ const parsed = parseEndpointURL(url);
9008
9066
  switch (parsed.protocol) {
9009
9067
  case "https:":
9010
9068
  return parsed.toString();
@@ -9020,7 +9078,7 @@ function normalizeWebTransportURL(url) {
9020
9078
  }
9021
9079
  }
9022
9080
  function normalizeWebSocketURL(url) {
9023
- const parsed = typeof url === "string" ? new URL(url) : new URL(url);
9081
+ const parsed = parseEndpointURL(url);
9024
9082
  switch (parsed.protocol) {
9025
9083
  case "ws:":
9026
9084
  case "wss:":
@@ -9108,18 +9166,28 @@ var WebTransportMessageConnection = class _WebTransportMessageConnection {
9108
9166
  signal.throwIfAborted();
9109
9167
  const Transport = resolveWebTransportConstructor();
9110
9168
  const transport = new Transport(url, options);
9111
- transport.closed.catch((error) => {
9112
- if (!signal.aborted) {
9113
- handlers.error(`WebTransport closed: ${getErrorMessage(error)}`);
9169
+ const opening = { complete: false };
9170
+ void transport.closed.then(
9171
+ () => {
9172
+ if (!opening.complete && !signal.aborted) {
9173
+ handlers.error("WebTransport closed before the session could open.");
9174
+ }
9175
+ },
9176
+ (error) => {
9177
+ if (!signal.aborted) {
9178
+ handlers.error(`WebTransport closed: ${getErrorMessage(error)}`);
9179
+ }
9114
9180
  }
9115
- });
9181
+ );
9116
9182
  try {
9117
9183
  await waitForWebTransportOpen(transport.ready, signal);
9118
9184
  signal.throwIfAborted();
9119
9185
  const stream = await waitForWebTransportOpen(
9120
9186
  transport.createBidirectionalStream(),
9121
- signal
9187
+ signal,
9188
+ disposeWebTransportStream
9122
9189
  );
9190
+ if (signal.aborted) disposeWebTransportStream(stream);
9123
9191
  signal.throwIfAborted();
9124
9192
  const writer = stream.writable.getWriter();
9125
9193
  const connection = new _WebTransportMessageConnection(
@@ -9129,6 +9197,7 @@ var WebTransportMessageConnection = class _WebTransportMessageConnection {
9129
9197
  maxMessageSize,
9130
9198
  handlers
9131
9199
  );
9200
+ opening.complete = true;
9132
9201
  connection.startReadLoop();
9133
9202
  return connection;
9134
9203
  } catch (error) {
@@ -9219,7 +9288,11 @@ var WebTransportMessageConnection = class _WebTransportMessageConnection {
9219
9288
  }
9220
9289
  }
9221
9290
  };
9222
- function waitForWebTransportOpen(pending, signal) {
9291
+ function disposeWebTransportStream(stream) {
9292
+ void stream.readable.cancel().catch(() => void 0);
9293
+ void stream.writable.abort().catch(() => void 0);
9294
+ }
9295
+ function waitForWebTransportOpen(pending, signal, dispose) {
9223
9296
  return new Promise((resolve, reject) => {
9224
9297
  const abort = () => reject(new Error("WebTransport connection cancelled."));
9225
9298
  signal.addEventListener("abort", abort, { once: true });
@@ -9227,6 +9300,10 @@ function waitForWebTransportOpen(pending, signal) {
9227
9300
  pending.then(
9228
9301
  (value) => {
9229
9302
  signal.removeEventListener("abort", abort);
9303
+ if (signal.aborted) {
9304
+ dispose?.(value);
9305
+ return;
9306
+ }
9230
9307
  resolve(value);
9231
9308
  },
9232
9309
  (error) => {
@@ -9290,7 +9367,7 @@ var WebTTY = class {
9290
9367
  maxMessageSize,
9291
9368
  sendHeartbeat: clientConfig2.sendHeartbeat ?? true,
9292
9369
  transport,
9293
- url: clientConfig2.url,
9370
+ url: clientConfig2.url.toString(),
9294
9371
  webSocketFactory: clientConfig2.webSocketFactory,
9295
9372
  webTransportOptions: clientConfig2.webTransportOptions
9296
9373
  };
@@ -9313,25 +9390,29 @@ var WebTTY = class {
9313
9390
  throw new Error("Invalid state for connect().");
9314
9391
  }
9315
9392
  this.connectionState = "connecting";
9316
- if (this.clientConfig.transport === "webtransport") {
9317
- void this.connectWebTransport(
9318
- normalizeWebTransportURL(this.clientConfig.url)
9393
+ const url = this.clientConfig.transport === "webtransport" ? normalizeWebTransportURL(this.clientConfig.url) : normalizeWebSocketURL(this.clientConfig.url);
9394
+ try {
9395
+ if (this.clientConfig.transport === "webtransport") {
9396
+ void this.connectWebTransport(url);
9397
+ return;
9398
+ }
9399
+ this.connection = new WebSocketMessageConnection(
9400
+ url,
9401
+ {
9402
+ close: this.handleClose,
9403
+ error: this.handleError,
9404
+ message: this.handleConnectionMessage,
9405
+ open: this.handleOpen
9406
+ },
9407
+ this.clientConfig.maxMessageSize * 4,
9408
+ this.clientConfig.webSocketFactory,
9409
+ this.clientConfig.maxMessageSize
9410
+ );
9411
+ } catch (error) {
9412
+ this.close(
9413
+ `${this.clientConfig.transport} connection failed: ${getErrorMessage(error)}`
9319
9414
  );
9320
- return;
9321
9415
  }
9322
- const wsUrl = normalizeWebSocketURL(this.clientConfig.url);
9323
- this.connection = new WebSocketMessageConnection(
9324
- wsUrl,
9325
- {
9326
- close: this.handleClose,
9327
- error: this.handleError,
9328
- message: this.handleConnectionMessage,
9329
- open: this.handleOpen
9330
- },
9331
- this.clientConfig.maxMessageSize * 4,
9332
- this.clientConfig.webSocketFactory,
9333
- this.clientConfig.maxMessageSize
9334
- );
9335
9416
  }
9336
9417
  /**
9337
9418
  * Sends data to the remote server's STDIN.
@@ -9987,7 +10068,9 @@ var WebTTY = class {
9987
10068
  if (typeof result === "number") {
9988
10069
  this.events.onComplete?.(result);
9989
10070
  } else if (typeof result === "string") {
9990
- this.events.onError?.(result);
10071
+ this.events.onError?.(
10072
+ redactEndpointCredentials(result, this.clientConfig.url)
10073
+ );
9991
10074
  } else {
9992
10075
  this.events.onError?.("Connection closed without a known reason.");
9993
10076
  }
package/dist/index.js CHANGED
@@ -8732,10 +8732,61 @@ var WebTTYFrameDecoder = class {
8732
8732
  }
8733
8733
  };
8734
8734
 
8735
- // src/webtty.ts
8735
+ // src/transport-diagnostics.ts
8736
8736
  function getErrorMessage(error) {
8737
- return error instanceof Error ? error.message : String(error);
8737
+ const message = error instanceof Error ? error.message.trim() : String(error ?? "").trim();
8738
+ if (error instanceof Error && error.name === "SecurityError") {
8739
+ return `The browser blocked the connection under its security policy. Check connect-src and browser restrictions.${message ? ` ${message}` : ""}`;
8740
+ }
8741
+ return message || "No diagnostic details were provided.";
8742
+ }
8743
+ function decodeCredential(value) {
8744
+ try {
8745
+ return decodeURIComponent(value);
8746
+ } catch {
8747
+ return value;
8748
+ }
8749
+ }
8750
+ function credentialForms(value) {
8751
+ const encoded = encodeURIComponent(value);
8752
+ const formEncoded = new URLSearchParams({ value }).toString().slice(6);
8753
+ return [value, encoded, formEncoded, encodeURIComponent(encoded)];
8754
+ }
8755
+ function credentialPattern(value) {
8756
+ const escaped = value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
8757
+ return new RegExp(
8758
+ escaped.replace(
8759
+ /%[\da-f]{2}/gi,
8760
+ (escape) => escape.replace(
8761
+ /[a-f]/gi,
8762
+ (hex) => `[${hex.toLowerCase()}${hex.toUpperCase()}]`
8763
+ )
8764
+ ),
8765
+ "g"
8766
+ );
8738
8767
  }
8768
+ function redactEndpointCredentials(message, endpoint) {
8769
+ try {
8770
+ const url = new URL(endpoint);
8771
+ const credentials = [
8772
+ ...url.searchParams.values(),
8773
+ decodeCredential(url.username),
8774
+ decodeCredential(url.password),
8775
+ decodeCredential(url.hash.slice(1))
8776
+ ];
8777
+ const forms = [
8778
+ ...new Set(credentials.filter(Boolean).flatMap(credentialForms))
8779
+ ].sort((a, b) => b.length - a.length);
8780
+ return forms.reduce(
8781
+ (redacted, value) => redacted.replace(credentialPattern(value), "[redacted]"),
8782
+ message
8783
+ );
8784
+ } catch {
8785
+ return "Connection failed: invalid endpoint URL.";
8786
+ }
8787
+ }
8788
+
8789
+ // src/webtty.ts
8739
8790
  function getWebSocketPayload(payload) {
8740
8791
  return new Uint8Array(payload);
8741
8792
  }
@@ -9068,7 +9119,7 @@ function attachCapabilitiesToProto(capabilities) {
9068
9119
  }
9069
9120
  function resolveWebTTYClientTransport(url, transport) {
9070
9121
  if (transport !== void 0) return transport;
9071
- const protocol = typeof url === "string" && url.includes("://") ? new URL(url).protocol : url instanceof URL ? url.protocol : "";
9122
+ const protocol = typeof url === "string" && url.includes("://") ? parseEndpointURL(url).protocol : url instanceof URL ? url.protocol : "";
9072
9123
  switch (protocol.replace(":", "").toLowerCase()) {
9073
9124
  case "webtransport":
9074
9125
  case "wts":
@@ -9077,8 +9128,15 @@ function resolveWebTTYClientTransport(url, transport) {
9077
9128
  return "websocket";
9078
9129
  }
9079
9130
  }
9131
+ function parseEndpointURL(url) {
9132
+ try {
9133
+ return new URL(url);
9134
+ } catch {
9135
+ throw new TypeError("Invalid WebTTY endpoint URL.");
9136
+ }
9137
+ }
9080
9138
  function normalizeWebTransportURL(url) {
9081
- const parsed = typeof url === "string" ? new URL(url) : new URL(url);
9139
+ const parsed = parseEndpointURL(url);
9082
9140
  switch (parsed.protocol) {
9083
9141
  case "https:":
9084
9142
  return parsed.toString();
@@ -9094,7 +9152,7 @@ function normalizeWebTransportURL(url) {
9094
9152
  }
9095
9153
  }
9096
9154
  function normalizeWebSocketURL(url) {
9097
- const parsed = typeof url === "string" ? new URL(url) : new URL(url);
9155
+ const parsed = parseEndpointURL(url);
9098
9156
  switch (parsed.protocol) {
9099
9157
  case "ws:":
9100
9158
  case "wss:":
@@ -9182,18 +9240,28 @@ var WebTransportMessageConnection = class _WebTransportMessageConnection {
9182
9240
  signal.throwIfAborted();
9183
9241
  const Transport = resolveWebTransportConstructor();
9184
9242
  const transport = new Transport(url, options);
9185
- transport.closed.catch((error) => {
9186
- if (!signal.aborted) {
9187
- handlers.error(`WebTransport closed: ${getErrorMessage(error)}`);
9243
+ const opening = { complete: false };
9244
+ void transport.closed.then(
9245
+ () => {
9246
+ if (!opening.complete && !signal.aborted) {
9247
+ handlers.error("WebTransport closed before the session could open.");
9248
+ }
9249
+ },
9250
+ (error) => {
9251
+ if (!signal.aborted) {
9252
+ handlers.error(`WebTransport closed: ${getErrorMessage(error)}`);
9253
+ }
9188
9254
  }
9189
- });
9255
+ );
9190
9256
  try {
9191
9257
  await waitForWebTransportOpen(transport.ready, signal);
9192
9258
  signal.throwIfAborted();
9193
9259
  const stream = await waitForWebTransportOpen(
9194
9260
  transport.createBidirectionalStream(),
9195
- signal
9261
+ signal,
9262
+ disposeWebTransportStream
9196
9263
  );
9264
+ if (signal.aborted) disposeWebTransportStream(stream);
9197
9265
  signal.throwIfAborted();
9198
9266
  const writer = stream.writable.getWriter();
9199
9267
  const connection = new _WebTransportMessageConnection(
@@ -9203,6 +9271,7 @@ var WebTransportMessageConnection = class _WebTransportMessageConnection {
9203
9271
  maxMessageSize,
9204
9272
  handlers
9205
9273
  );
9274
+ opening.complete = true;
9206
9275
  connection.startReadLoop();
9207
9276
  return connection;
9208
9277
  } catch (error) {
@@ -9293,7 +9362,11 @@ var WebTransportMessageConnection = class _WebTransportMessageConnection {
9293
9362
  }
9294
9363
  }
9295
9364
  };
9296
- function waitForWebTransportOpen(pending, signal) {
9365
+ function disposeWebTransportStream(stream) {
9366
+ void stream.readable.cancel().catch(() => void 0);
9367
+ void stream.writable.abort().catch(() => void 0);
9368
+ }
9369
+ function waitForWebTransportOpen(pending, signal, dispose) {
9297
9370
  return new Promise((resolve, reject) => {
9298
9371
  const abort = () => reject(new Error("WebTransport connection cancelled."));
9299
9372
  signal.addEventListener("abort", abort, { once: true });
@@ -9301,6 +9374,10 @@ function waitForWebTransportOpen(pending, signal) {
9301
9374
  pending.then(
9302
9375
  (value) => {
9303
9376
  signal.removeEventListener("abort", abort);
9377
+ if (signal.aborted) {
9378
+ dispose?.(value);
9379
+ return;
9380
+ }
9304
9381
  resolve(value);
9305
9382
  },
9306
9383
  (error) => {
@@ -9364,7 +9441,7 @@ var WebTTY = class {
9364
9441
  maxMessageSize,
9365
9442
  sendHeartbeat: clientConfig2.sendHeartbeat ?? true,
9366
9443
  transport,
9367
- url: clientConfig2.url,
9444
+ url: clientConfig2.url.toString(),
9368
9445
  webSocketFactory: clientConfig2.webSocketFactory,
9369
9446
  webTransportOptions: clientConfig2.webTransportOptions
9370
9447
  };
@@ -9387,25 +9464,29 @@ var WebTTY = class {
9387
9464
  throw new Error("Invalid state for connect().");
9388
9465
  }
9389
9466
  this.connectionState = "connecting";
9390
- if (this.clientConfig.transport === "webtransport") {
9391
- void this.connectWebTransport(
9392
- normalizeWebTransportURL(this.clientConfig.url)
9467
+ const url = this.clientConfig.transport === "webtransport" ? normalizeWebTransportURL(this.clientConfig.url) : normalizeWebSocketURL(this.clientConfig.url);
9468
+ try {
9469
+ if (this.clientConfig.transport === "webtransport") {
9470
+ void this.connectWebTransport(url);
9471
+ return;
9472
+ }
9473
+ this.connection = new WebSocketMessageConnection(
9474
+ url,
9475
+ {
9476
+ close: this.handleClose,
9477
+ error: this.handleError,
9478
+ message: this.handleConnectionMessage,
9479
+ open: this.handleOpen
9480
+ },
9481
+ this.clientConfig.maxMessageSize * 4,
9482
+ this.clientConfig.webSocketFactory,
9483
+ this.clientConfig.maxMessageSize
9484
+ );
9485
+ } catch (error) {
9486
+ this.close(
9487
+ `${this.clientConfig.transport} connection failed: ${getErrorMessage(error)}`
9393
9488
  );
9394
- return;
9395
9489
  }
9396
- const wsUrl = normalizeWebSocketURL(this.clientConfig.url);
9397
- this.connection = new WebSocketMessageConnection(
9398
- wsUrl,
9399
- {
9400
- close: this.handleClose,
9401
- error: this.handleError,
9402
- message: this.handleConnectionMessage,
9403
- open: this.handleOpen
9404
- },
9405
- this.clientConfig.maxMessageSize * 4,
9406
- this.clientConfig.webSocketFactory,
9407
- this.clientConfig.maxMessageSize
9408
- );
9409
9490
  }
9410
9491
  /**
9411
9492
  * Sends data to the remote server's STDIN.
@@ -10061,7 +10142,9 @@ var WebTTY = class {
10061
10142
  if (typeof result === "number") {
10062
10143
  this.events.onComplete?.(result);
10063
10144
  } else if (typeof result === "string") {
10064
- this.events.onError?.(result);
10145
+ this.events.onError?.(
10146
+ redactEndpointCredentials(result, this.clientConfig.url)
10147
+ );
10065
10148
  } else {
10066
10149
  this.events.onError?.("Connection closed without a known reason.");
10067
10150
  }
package/dist/index.mjs CHANGED
@@ -38,7 +38,7 @@ import {
38
38
  webTTYRecordedEventEncryptedPayload,
39
39
  webTTYServerProofTranscriptBytes,
40
40
  webTTYSigningKeyID
41
- } from "./chunk-E7OURVTY.mjs";
41
+ } from "./chunk-2XNLPDKP.mjs";
42
42
  export {
43
43
  WebTTY,
44
44
  WebTTYCommand,
package/dist/node.js CHANGED
@@ -8738,10 +8738,61 @@ var WebTTYFrameDecoder = class {
8738
8738
  }
8739
8739
  };
8740
8740
 
8741
- // src/webtty.ts
8741
+ // src/transport-diagnostics.ts
8742
8742
  function getErrorMessage(error) {
8743
- return error instanceof Error ? error.message : String(error);
8743
+ const message = error instanceof Error ? error.message.trim() : String(error ?? "").trim();
8744
+ if (error instanceof Error && error.name === "SecurityError") {
8745
+ return `The browser blocked the connection under its security policy. Check connect-src and browser restrictions.${message ? ` ${message}` : ""}`;
8746
+ }
8747
+ return message || "No diagnostic details were provided.";
8748
+ }
8749
+ function decodeCredential(value) {
8750
+ try {
8751
+ return decodeURIComponent(value);
8752
+ } catch {
8753
+ return value;
8754
+ }
8755
+ }
8756
+ function credentialForms(value) {
8757
+ const encoded = encodeURIComponent(value);
8758
+ const formEncoded = new URLSearchParams({ value }).toString().slice(6);
8759
+ return [value, encoded, formEncoded, encodeURIComponent(encoded)];
8760
+ }
8761
+ function credentialPattern(value) {
8762
+ const escaped = value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
8763
+ return new RegExp(
8764
+ escaped.replace(
8765
+ /%[\da-f]{2}/gi,
8766
+ (escape) => escape.replace(
8767
+ /[a-f]/gi,
8768
+ (hex) => `[${hex.toLowerCase()}${hex.toUpperCase()}]`
8769
+ )
8770
+ ),
8771
+ "g"
8772
+ );
8773
+ }
8774
+ function redactEndpointCredentials(message, endpoint) {
8775
+ try {
8776
+ const url = new URL(endpoint);
8777
+ const credentials = [
8778
+ ...url.searchParams.values(),
8779
+ decodeCredential(url.username),
8780
+ decodeCredential(url.password),
8781
+ decodeCredential(url.hash.slice(1))
8782
+ ];
8783
+ const forms = [
8784
+ ...new Set(credentials.filter(Boolean).flatMap(credentialForms))
8785
+ ].sort((a, b) => b.length - a.length);
8786
+ return forms.reduce(
8787
+ (redacted, value) => redacted.replace(credentialPattern(value), "[redacted]"),
8788
+ message
8789
+ );
8790
+ } catch {
8791
+ return "Connection failed: invalid endpoint URL.";
8792
+ }
8744
8793
  }
8794
+
8795
+ // src/webtty.ts
8745
8796
  function getWebSocketPayload(payload) {
8746
8797
  return new Uint8Array(payload);
8747
8798
  }
@@ -9074,7 +9125,7 @@ function attachCapabilitiesToProto(capabilities) {
9074
9125
  }
9075
9126
  function resolveWebTTYClientTransport(url, transport) {
9076
9127
  if (transport !== void 0) return transport;
9077
- const protocol = typeof url === "string" && url.includes("://") ? new URL(url).protocol : url instanceof URL ? url.protocol : "";
9128
+ const protocol = typeof url === "string" && url.includes("://") ? parseEndpointURL(url).protocol : url instanceof URL ? url.protocol : "";
9078
9129
  switch (protocol.replace(":", "").toLowerCase()) {
9079
9130
  case "webtransport":
9080
9131
  case "wts":
@@ -9083,8 +9134,15 @@ function resolveWebTTYClientTransport(url, transport) {
9083
9134
  return "websocket";
9084
9135
  }
9085
9136
  }
9137
+ function parseEndpointURL(url) {
9138
+ try {
9139
+ return new URL(url);
9140
+ } catch {
9141
+ throw new TypeError("Invalid WebTTY endpoint URL.");
9142
+ }
9143
+ }
9086
9144
  function normalizeWebTransportURL(url) {
9087
- const parsed = typeof url === "string" ? new URL(url) : new URL(url);
9145
+ const parsed = parseEndpointURL(url);
9088
9146
  switch (parsed.protocol) {
9089
9147
  case "https:":
9090
9148
  return parsed.toString();
@@ -9100,7 +9158,7 @@ function normalizeWebTransportURL(url) {
9100
9158
  }
9101
9159
  }
9102
9160
  function normalizeWebSocketURL(url) {
9103
- const parsed = typeof url === "string" ? new URL(url) : new URL(url);
9161
+ const parsed = parseEndpointURL(url);
9104
9162
  switch (parsed.protocol) {
9105
9163
  case "ws:":
9106
9164
  case "wss:":
@@ -9188,18 +9246,28 @@ var WebTransportMessageConnection = class _WebTransportMessageConnection {
9188
9246
  signal.throwIfAborted();
9189
9247
  const Transport = resolveWebTransportConstructor();
9190
9248
  const transport = new Transport(url, options);
9191
- transport.closed.catch((error) => {
9192
- if (!signal.aborted) {
9193
- handlers.error(`WebTransport closed: ${getErrorMessage(error)}`);
9249
+ const opening = { complete: false };
9250
+ void transport.closed.then(
9251
+ () => {
9252
+ if (!opening.complete && !signal.aborted) {
9253
+ handlers.error("WebTransport closed before the session could open.");
9254
+ }
9255
+ },
9256
+ (error) => {
9257
+ if (!signal.aborted) {
9258
+ handlers.error(`WebTransport closed: ${getErrorMessage(error)}`);
9259
+ }
9194
9260
  }
9195
- });
9261
+ );
9196
9262
  try {
9197
9263
  await waitForWebTransportOpen(transport.ready, signal);
9198
9264
  signal.throwIfAborted();
9199
9265
  const stream = await waitForWebTransportOpen(
9200
9266
  transport.createBidirectionalStream(),
9201
- signal
9267
+ signal,
9268
+ disposeWebTransportStream
9202
9269
  );
9270
+ if (signal.aborted) disposeWebTransportStream(stream);
9203
9271
  signal.throwIfAborted();
9204
9272
  const writer = stream.writable.getWriter();
9205
9273
  const connection = new _WebTransportMessageConnection(
@@ -9209,6 +9277,7 @@ var WebTransportMessageConnection = class _WebTransportMessageConnection {
9209
9277
  maxMessageSize,
9210
9278
  handlers
9211
9279
  );
9280
+ opening.complete = true;
9212
9281
  connection.startReadLoop();
9213
9282
  return connection;
9214
9283
  } catch (error) {
@@ -9299,7 +9368,11 @@ var WebTransportMessageConnection = class _WebTransportMessageConnection {
9299
9368
  }
9300
9369
  }
9301
9370
  };
9302
- function waitForWebTransportOpen(pending, signal) {
9371
+ function disposeWebTransportStream(stream) {
9372
+ void stream.readable.cancel().catch(() => void 0);
9373
+ void stream.writable.abort().catch(() => void 0);
9374
+ }
9375
+ function waitForWebTransportOpen(pending, signal, dispose) {
9303
9376
  return new Promise((resolve, reject) => {
9304
9377
  const abort = () => reject(new Error("WebTransport connection cancelled."));
9305
9378
  signal.addEventListener("abort", abort, { once: true });
@@ -9307,6 +9380,10 @@ function waitForWebTransportOpen(pending, signal) {
9307
9380
  pending.then(
9308
9381
  (value) => {
9309
9382
  signal.removeEventListener("abort", abort);
9383
+ if (signal.aborted) {
9384
+ dispose?.(value);
9385
+ return;
9386
+ }
9310
9387
  resolve(value);
9311
9388
  },
9312
9389
  (error) => {
@@ -9370,7 +9447,7 @@ var WebTTY = class {
9370
9447
  maxMessageSize,
9371
9448
  sendHeartbeat: clientConfig2.sendHeartbeat ?? true,
9372
9449
  transport,
9373
- url: clientConfig2.url,
9450
+ url: clientConfig2.url.toString(),
9374
9451
  webSocketFactory: clientConfig2.webSocketFactory,
9375
9452
  webTransportOptions: clientConfig2.webTransportOptions
9376
9453
  };
@@ -9393,25 +9470,29 @@ var WebTTY = class {
9393
9470
  throw new Error("Invalid state for connect().");
9394
9471
  }
9395
9472
  this.connectionState = "connecting";
9396
- if (this.clientConfig.transport === "webtransport") {
9397
- void this.connectWebTransport(
9398
- normalizeWebTransportURL(this.clientConfig.url)
9473
+ const url = this.clientConfig.transport === "webtransport" ? normalizeWebTransportURL(this.clientConfig.url) : normalizeWebSocketURL(this.clientConfig.url);
9474
+ try {
9475
+ if (this.clientConfig.transport === "webtransport") {
9476
+ void this.connectWebTransport(url);
9477
+ return;
9478
+ }
9479
+ this.connection = new WebSocketMessageConnection(
9480
+ url,
9481
+ {
9482
+ close: this.handleClose,
9483
+ error: this.handleError,
9484
+ message: this.handleConnectionMessage,
9485
+ open: this.handleOpen
9486
+ },
9487
+ this.clientConfig.maxMessageSize * 4,
9488
+ this.clientConfig.webSocketFactory,
9489
+ this.clientConfig.maxMessageSize
9490
+ );
9491
+ } catch (error) {
9492
+ this.close(
9493
+ `${this.clientConfig.transport} connection failed: ${getErrorMessage(error)}`
9399
9494
  );
9400
- return;
9401
9495
  }
9402
- const wsUrl = normalizeWebSocketURL(this.clientConfig.url);
9403
- this.connection = new WebSocketMessageConnection(
9404
- wsUrl,
9405
- {
9406
- close: this.handleClose,
9407
- error: this.handleError,
9408
- message: this.handleConnectionMessage,
9409
- open: this.handleOpen
9410
- },
9411
- this.clientConfig.maxMessageSize * 4,
9412
- this.clientConfig.webSocketFactory,
9413
- this.clientConfig.maxMessageSize
9414
- );
9415
9496
  }
9416
9497
  /**
9417
9498
  * Sends data to the remote server's STDIN.
@@ -10067,7 +10148,9 @@ var WebTTY = class {
10067
10148
  if (typeof result === "number") {
10068
10149
  this.events.onComplete?.(result);
10069
10150
  } else if (typeof result === "string") {
10070
- this.events.onError?.(result);
10151
+ this.events.onError?.(
10152
+ redactEndpointCredentials(result, this.clientConfig.url)
10153
+ );
10071
10154
  } else {
10072
10155
  this.events.onError?.("Connection closed without a known reason.");
10073
10156
  }
package/dist/node.mjs CHANGED
@@ -38,7 +38,7 @@ import {
38
38
  webTTYRecordedEventEncryptedPayload,
39
39
  webTTYServerProofTranscriptBytes,
40
40
  webTTYSigningKeyID
41
- } from "./chunk-E7OURVTY.mjs";
41
+ } from "./chunk-2XNLPDKP.mjs";
42
42
 
43
43
  // src/local-e2e.ts
44
44
  var localKeyFileVersion = 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rstreamlabs/webtty",
3
- "version": "1.11.0",
3
+ "version": "1.11.2",
4
4
  "description": "Client-side implementation of the rstream WebTTY protocol.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -54,7 +54,7 @@
54
54
  "dependencies": {
55
55
  "protobufjs": "^8.7.2",
56
56
  "ws": "^8.21.3",
57
- "@rstreamlabs/filesystem": "^0.2.0"
57
+ "@rstreamlabs/filesystem": "^0.3.0"
58
58
  },
59
59
  "publishConfig": {
60
60
  "access": "public"