@dainprotocol/tunnel 1.1.16 → 1.1.18
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/client/index.js +19 -0
- package/dist/server/index.js +9 -1
- package/package.json +1 -1
package/dist/client/index.js
CHANGED
|
@@ -233,6 +233,8 @@ class DainTunnel extends events_1.EventEmitter {
|
|
|
233
233
|
}
|
|
234
234
|
handleSSEConnection(message) {
|
|
235
235
|
var _a;
|
|
236
|
+
const startTime = Date.now();
|
|
237
|
+
console.log(`[SSE E2E] Client: Establishing SSE connection ${message.id} to localhost:${this.port}${message.path}`);
|
|
236
238
|
try {
|
|
237
239
|
const options = {
|
|
238
240
|
hostname: 'localhost',
|
|
@@ -245,16 +247,19 @@ class DainTunnel extends events_1.EventEmitter {
|
|
|
245
247
|
const req = http_1.default.request(options, (res) => {
|
|
246
248
|
var _a;
|
|
247
249
|
if (res.statusCode !== 200) {
|
|
250
|
+
console.log(`[SSE E2E] Client: Service returned status ${res.statusCode} for ${message.id}`);
|
|
248
251
|
res.resume();
|
|
249
252
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.OPEN) {
|
|
250
253
|
this.ws.send(JSON.stringify({ type: 'sse', id: message.id, event: 'error', data: `Status ${res.statusCode}` }));
|
|
251
254
|
}
|
|
252
255
|
return;
|
|
253
256
|
}
|
|
257
|
+
console.log(`[SSE E2E] Client: Service accepted SSE connection ${message.id}, streaming started`);
|
|
254
258
|
const socket = res.socket || res.connection;
|
|
255
259
|
if (socket === null || socket === void 0 ? void 0 : socket.setNoDelay)
|
|
256
260
|
socket.setNoDelay(true);
|
|
257
261
|
let buffer = '';
|
|
262
|
+
let eventCount = 0;
|
|
258
263
|
res.on('data', (chunk) => {
|
|
259
264
|
var _a;
|
|
260
265
|
buffer += chunk.toString();
|
|
@@ -271,13 +276,20 @@ class DainTunnel extends events_1.EventEmitter {
|
|
|
271
276
|
}
|
|
272
277
|
if (data.endsWith('\n'))
|
|
273
278
|
data = data.slice(0, -1);
|
|
279
|
+
eventCount++;
|
|
280
|
+
console.log(`[SSE E2E] Client: Received event #${eventCount} from service for ${message.id}: ${event} (${Date.now() - startTime}ms)`);
|
|
274
281
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.OPEN) {
|
|
275
282
|
this.ws.send(JSON.stringify({ type: 'sse', id: message.id, event, data }));
|
|
283
|
+
console.log(`[SSE E2E] Client: Forwarded event to tunnel server for ${message.id}`);
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
console.log(`[SSE E2E] Client: WebSocket closed, cannot forward event for ${message.id}`);
|
|
276
287
|
}
|
|
277
288
|
}
|
|
278
289
|
});
|
|
279
290
|
res.on('end', () => {
|
|
280
291
|
var _a;
|
|
292
|
+
console.log(`[SSE E2E] Client: Service closed connection for ${message.id} after ${eventCount} events (${Date.now() - startTime}ms)`);
|
|
281
293
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.OPEN) {
|
|
282
294
|
this.ws.send(JSON.stringify({ type: 'sse', id: message.id, event: 'close', data: 'Connection closed' }));
|
|
283
295
|
}
|
|
@@ -285,6 +297,7 @@ class DainTunnel extends events_1.EventEmitter {
|
|
|
285
297
|
});
|
|
286
298
|
req.on('error', (error) => {
|
|
287
299
|
var _a;
|
|
300
|
+
console.log(`[SSE E2E] Client: Request error for ${message.id}: ${error.message}`);
|
|
288
301
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.OPEN) {
|
|
289
302
|
this.ws.send(JSON.stringify({ type: 'sse', id: message.id, event: 'error', data: error.message }));
|
|
290
303
|
}
|
|
@@ -296,17 +309,23 @@ class DainTunnel extends events_1.EventEmitter {
|
|
|
296
309
|
this.emit("sse_connection", { id: message.id, path: message.path });
|
|
297
310
|
}
|
|
298
311
|
catch (error) {
|
|
312
|
+
console.log(`[SSE E2E] Client: Exception for ${message.id}: ${error.message}`);
|
|
299
313
|
if (((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.OPEN) {
|
|
300
314
|
this.ws.send(JSON.stringify({ type: 'sse', id: message.id, event: 'error', data: error.message }));
|
|
301
315
|
}
|
|
302
316
|
}
|
|
303
317
|
}
|
|
304
318
|
handleSSEClose(message) {
|
|
319
|
+
console.log(`[SSE E2E] Client: Received close command from server for ${message.id}`);
|
|
305
320
|
const client = this.sseClients.get(message.id);
|
|
306
321
|
if (client) {
|
|
322
|
+
console.log(`[SSE E2E] Client: Destroying local service connection for ${message.id}`);
|
|
307
323
|
client.destroy();
|
|
308
324
|
this.sseClients.delete(message.id);
|
|
309
325
|
}
|
|
326
|
+
else {
|
|
327
|
+
console.log(`[SSE E2E] Client: No active connection found for ${message.id} (already closed?)`);
|
|
328
|
+
}
|
|
310
329
|
}
|
|
311
330
|
forwardRequest(request) {
|
|
312
331
|
return new Promise((resolve, reject) => {
|
package/dist/server/index.js
CHANGED
|
@@ -324,11 +324,14 @@ class DainTunnelServer {
|
|
|
324
324
|
}
|
|
325
325
|
handleSSEMessage(data) {
|
|
326
326
|
const connection = this.sseConnections.get(data.id);
|
|
327
|
-
if (!connection)
|
|
327
|
+
if (!connection) {
|
|
328
|
+
console.log(`[SSE Debug] handleSSEMessage: No connection found for ${data.id}, event=${data.event}`);
|
|
328
329
|
return;
|
|
330
|
+
}
|
|
329
331
|
const { res, tunnelId } = connection;
|
|
330
332
|
const clientDisconnected = connection.clientDisconnected;
|
|
331
333
|
const isCloseEvent = data.event === 'close';
|
|
334
|
+
console.log(`[SSE Debug] handleSSEMessage: id=${data.id}, event=${data.event}, clientDisconnected=${clientDisconnected}, isClose=${isCloseEvent}`);
|
|
332
335
|
if (isCloseEvent) {
|
|
333
336
|
const tunnel = this.tunnels.get(tunnelId);
|
|
334
337
|
if (tunnel) {
|
|
@@ -480,12 +483,14 @@ class DainTunnelServer {
|
|
|
480
483
|
body: req.method !== "GET" && req.body ? req.body.toString("base64") : undefined
|
|
481
484
|
}));
|
|
482
485
|
req.on('close', () => {
|
|
486
|
+
console.log(`[SSE Debug] req.close fired for ${sseId} - client disconnected`);
|
|
483
487
|
const connection = this.sseConnections.get(sseId);
|
|
484
488
|
if (connection) {
|
|
485
489
|
connection.clientDisconnected = true;
|
|
486
490
|
// Clean up after a short delay to allow any pending events to be handled
|
|
487
491
|
setTimeout(() => {
|
|
488
492
|
if (this.sseConnections.has(sseId)) {
|
|
493
|
+
console.log(`[SSE Debug] Cleaning up SSE connection ${sseId} after client disconnect`);
|
|
489
494
|
// Decrement request counter for backpressure tracking
|
|
490
495
|
const currentCount = this.tunnelRequestCount.get(tunnelId) || 1;
|
|
491
496
|
this.tunnelRequestCount.set(tunnelId, Math.max(0, currentCount - 1));
|
|
@@ -499,6 +504,9 @@ class DainTunnelServer {
|
|
|
499
504
|
}, 100);
|
|
500
505
|
}
|
|
501
506
|
});
|
|
507
|
+
req.on('error', (err) => {
|
|
508
|
+
console.log(`[SSE Debug] req.error fired for ${sseId}:`, err.message);
|
|
509
|
+
});
|
|
502
510
|
}
|
|
503
511
|
removeTunnel(ws) {
|
|
504
512
|
try {
|