@dainprotocol/tunnel 1.1.13 → 1.1.14

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.
@@ -274,6 +274,11 @@ class DainTunnel extends events_1.EventEmitter {
274
274
  }
275
275
  return;
276
276
  }
277
+ // Optimize socket for low-latency streaming
278
+ const socket = res.socket || res.connection;
279
+ if (socket && socket.setNoDelay) {
280
+ socket.setNoDelay(true); // Disable Nagle's algorithm
281
+ }
277
282
  console.log(`[SSE E2E] Client: Service accepted SSE connection ${message.id}, streaming started`);
278
283
  // Process SSE stream
279
284
  let buffer = '';
@@ -20,16 +20,18 @@ const url_1 = require("url");
20
20
  */
21
21
  function flushSSE(res) {
22
22
  try {
23
- // Access underlying socket to force flush
24
- // Type assertion needed as Express types don't expose socket methods
23
+ // Node.js TCP sockets use cork/uncork for buffering control
24
+ // We uncork to force immediate transmission of buffered data
25
25
  const socket = res.socket;
26
- if (socket && typeof socket.flush === 'function') {
27
- socket.flush();
28
- }
29
- // Fallback: try connection property (older Node.js versions)
30
- const connection = res.connection;
31
- if (connection && typeof connection.flush === 'function') {
32
- connection.flush();
26
+ if (socket) {
27
+ // If socket is corked, uncork it to flush immediately
28
+ if (socket.uncork) {
29
+ socket.uncork();
30
+ }
31
+ // Disable Nagle's algorithm for low-latency streaming
32
+ if (socket.setNoDelay && !socket._noDelay) {
33
+ socket.setNoDelay(true);
34
+ }
33
35
  }
34
36
  }
35
37
  catch (error) {
@@ -480,6 +482,11 @@ class DainTunnelServer {
480
482
  // Setup SSE connection
481
483
  const sseId = (0, uuid_1.v4)();
482
484
  console.log(`[SSE E2E] Server: Establishing SSE connection ${sseId} from browser to ${req.url}`);
485
+ // Optimize TCP socket for low-latency streaming
486
+ const socket = req.socket || req.connection;
487
+ if (socket && socket.setNoDelay) {
488
+ socket.setNoDelay(true); // Disable Nagle's algorithm for immediate transmission
489
+ }
483
490
  // Set SSE headers with anti-buffering directives
484
491
  // Note: Do NOT set 'Connection: keep-alive' - it's forbidden in HTTP/2 and will cause immediate disconnect
485
492
  res.writeHead(200, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dainprotocol/tunnel",
3
- "version": "1.1.13",
3
+ "version": "1.1.14",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "private": false,