asyx 0.2.3 → 0.3.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.
@@ -325,7 +325,7 @@ export class TcpTunnel {
325
325
  this.closeStream(streamId);
326
326
  }
327
327
 
328
- this.log.info({ streamId }, 'tcp.stream.open');
328
+ this.log.info({ streamId, localPort: this.localPort }, 'tcp.stream.open');
329
329
 
330
330
  // Open local TCP socket to 127.0.0.1:localPort
331
331
  const localSocket = net.createConnection({
@@ -335,6 +335,9 @@ export class TcpTunnel {
335
335
  keepAliveInitialDelay: this.tcpKeepaliveInitialDelay
336
336
  });
337
337
 
338
+ // Log when connection attempt starts
339
+ this.log.debug({ streamId, localPort: this.localPort }, 'tcp.stream.local_connecting');
340
+
338
341
  // TCP-S5: Set up idle timeout for this stream (parity with relay)
339
342
  const idleTimer = setTimeout(() => {
340
343
  this.log.warn({ streamId }, 'tcp.stream.idle_timeout');
@@ -344,14 +347,9 @@ export class TcpTunnel {
344
347
  // Track stream activity
345
348
  const lastActivity = Date.now();
346
349
 
347
- // Buffer for STREAM_DATA frames that arrive before socket is connected
348
- const dataBuffer = [];
349
- let socketConnected = false;
350
-
351
350
  // Handle local socket connection
352
351
  localSocket.on('connect', () => {
353
352
  this.log.debug({ streamId }, 'tcp.stream.local_connected');
354
- socketConnected = true;
355
353
 
356
354
  // Update stream state to mark socket as connected
357
355
  const stream = this.activeStreams.get(streamId);
@@ -359,21 +357,20 @@ export class TcpTunnel {
359
357
  stream.socketConnected = true;
360
358
  }
361
359
 
362
- // Flush any buffered STREAM_DATA frames
363
- if (dataBuffer.length > 0) {
364
- this.log.debug({ streamId, bufferedCount: dataBuffer.length }, 'tcp.stream.flushing_buffered_data');
365
- if (stream && stream.localSocket && !stream.localSocket.destroyed) {
366
- for (const bufferedData of dataBuffer) {
367
- const written = stream.localSocket.write(bufferedData);
360
+ // Flush any buffered STREAM_DATA frames from stream.dataBuffer
361
+ if (stream && stream.dataBuffer && stream.dataBuffer.length > 0) {
362
+ this.log.debug({ streamId, bufferedCount: stream.dataBuffer.length }, 'tcp.stream.flushing_buffered_data');
363
+ if (stream.localSocket && !stream.localSocket.destroyed) {
364
+ const buf = stream.dataBuffer;
365
+ while (buf.length > 0) {
366
+ const chunk = buf.shift();
367
+ const written = stream.localSocket.write(chunk);
368
368
  if (!written) {
369
369
  this.log.warn({ streamId }, 'tcp.stream_data.local_socket_backpressure_on_flush');
370
370
  }
371
371
  }
372
372
  }
373
- dataBuffer.length = 0; // Clear buffer
374
- if (stream) {
375
- stream.dataBuffer = []; // Also clear in stream state
376
- }
373
+ stream.dataBuffer = []; // Clear buffer after flush
377
374
  }
378
375
  });
379
376
 
@@ -426,21 +423,21 @@ export class TcpTunnel {
426
423
 
427
424
  // Handle local socket errors
428
425
  localSocket.on('error', (err) => {
429
- this.log.error({ streamId, error: err.message }, 'tcp.stream.local_error');
426
+ this.log.error({ streamId, error: err.message, code: err.code, localPort: this.localPort }, 'tcp.stream.local_error');
430
427
  if (err.code === 'ECONNREFUSED') {
431
428
  this.log.error({ port: this.localPort }, 'Local TCP service refused connection');
432
- // Send STREAM_ERROR to relay
433
- if (this.ws && this.ws.readyState === WebSocket.OPEN && !this.protocolViolation) {
434
- try {
435
- const errorFrame = encodeFrame({
436
- type: FrameType.STREAM_ERROR,
437
- streamId: streamId,
438
- payload: Buffer.from(`ECONNREFUSED: ${err.message}`)
439
- });
440
- this.ws.send(errorFrame, { binary: true });
441
- } catch (frameErr) {
442
- this.log.error({ streamId, error: frameErr.message }, 'tcp.frame_encode_error');
443
- }
429
+ }
430
+ // Send STREAM_ERROR to relay for all errors so it knows the connection failed
431
+ if (this.ws && this.ws.readyState === WebSocket.OPEN && !this.protocolViolation) {
432
+ try {
433
+ const errorFrame = encodeFrame({
434
+ type: FrameType.STREAM_ERROR,
435
+ streamId: streamId,
436
+ payload: Buffer.from(`${err.code || 'ERROR'}: ${err.message}`)
437
+ });
438
+ this.ws.send(errorFrame, { binary: true });
439
+ } catch (frameErr) {
440
+ this.log.error({ streamId, error: frameErr.message }, 'tcp.frame_encode_error');
444
441
  }
445
442
  }
446
443
  this.closeStream(streamId);
@@ -470,7 +467,7 @@ export class TcpTunnel {
470
467
  localSocket,
471
468
  idleTimer,
472
469
  lastActivity,
473
- dataBuffer, // Buffer for data arriving before socket connects
470
+ dataBuffer: [], // Buffer for data arriving before socket connects
474
471
  socketConnected: false // Will be set to true in 'connect' handler
475
472
  });
476
473
  }