@dainprotocol/tunnel 1.0.2 → 1.0.3

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.
@@ -47,6 +47,7 @@ class DainTunnelServer {
47
47
  ws.on("message", (message) => {
48
48
  try {
49
49
  const data = JSON.parse(message);
50
+ console.log(`Received WebSocket message: ${data.type}`);
50
51
  if (data.type === "challenge_request") {
51
52
  this.handleChallengeRequest(ws);
52
53
  }
@@ -63,8 +64,12 @@ class DainTunnelServer {
63
64
  }
64
65
  });
65
66
  ws.on("close", () => {
67
+ console.log("WebSocket connection closed");
66
68
  this.removeTunnel(ws);
67
69
  });
70
+ ws.on("error", (error) => {
71
+ console.error("WebSocket error:", error);
72
+ });
68
73
  });
69
74
  }
70
75
  handleChallengeRequest(ws) {
@@ -87,6 +92,21 @@ class DainTunnelServer {
87
92
  return;
88
93
  }
89
94
  this.tunnels.set(tunnelId, { id: tunnelId, ws });
95
+ console.log(`Tunnel added: ${tunnelId}`);
96
+ console.log(`Current tunnels: ${Array.from(this.tunnels.keys()).join(', ')}`);
97
+ // Add a periodic check to ensure the tunnel is still in the map
98
+ const intervalId = setInterval(() => {
99
+ if (this.tunnels.has(tunnelId)) {
100
+ console.log(`Tunnel ${tunnelId} still active`);
101
+ }
102
+ else {
103
+ console.log(`Tunnel ${tunnelId} not found in periodic check`);
104
+ clearInterval(intervalId);
105
+ }
106
+ }, 5000); // Check every 5 seconds
107
+ ws.on("close", () => {
108
+ clearInterval(intervalId);
109
+ });
90
110
  let tunnelUrl = `${this.hostname}`;
91
111
  if (process.env.SKIP_PORT !== "true") {
92
112
  tunnelUrl += `:${this.port}`;
@@ -115,9 +135,19 @@ class DainTunnelServer {
115
135
  }
116
136
  async handleHttpRequest(req, res) {
117
137
  const tunnelId = req.params.tunnelId;
118
- const tunnel = this.tunnels.get(tunnelId);
138
+ let tunnel;
139
+ let retries = 3;
140
+ while (retries > 0 && !tunnel) {
141
+ tunnel = this.tunnels.get(tunnelId);
142
+ if (!tunnel) {
143
+ console.log(`Tunnel not found: ${tunnelId}, retrying... (${retries} attempts left)`);
144
+ console.log(`Current tunnels: ${Array.from(this.tunnels.keys()).join(', ')}`);
145
+ await new Promise(resolve => setTimeout(resolve, 100)); // Wait 100ms before retrying
146
+ retries--;
147
+ }
148
+ }
119
149
  if (!tunnel) {
120
- console.log(`Tunnel not found: ${tunnelId}`);
150
+ console.log(`Tunnel not found after retries: ${tunnelId}`);
121
151
  return res.status(404).send("Tunnel not found");
122
152
  }
123
153
  const requestId = (0, uuid_1.v4)();
@@ -137,13 +167,21 @@ class DainTunnelServer {
137
167
  console.log(`Request forwarded: ${requestId}, Method: ${req.method}, Path: ${req.url}`);
138
168
  }
139
169
  removeTunnel(ws) {
170
+ let removedTunnelId;
140
171
  for (const [id, tunnel] of this.tunnels.entries()) {
141
172
  if (tunnel.ws === ws) {
142
173
  this.tunnels.delete(id);
174
+ removedTunnelId = id;
143
175
  console.log(`Tunnel removed: ${id}`);
144
176
  break;
145
177
  }
146
178
  }
179
+ if (removedTunnelId) {
180
+ console.log(`Tunnel ${removedTunnelId} removed. Current tunnels: ${Array.from(this.tunnels.keys()).join(', ')}`);
181
+ }
182
+ else {
183
+ console.log(`No tunnel found to remove for the closed WebSocket connection`);
184
+ }
147
185
  // Also remove any pending challenges for this WebSocket
148
186
  for (const [challenge, challengeObj] of this.challenges.entries()) {
149
187
  if (challengeObj.ws === ws) {
package/package.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "name": "@dainprotocol/tunnel",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
+ "private": false,
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+
6
11
  "scripts": {
7
12
  "build": "tsc",
8
13
  "build:types": "tsc --emitDeclarationOnly",