@dpkrn/nodetunnel 1.0.2 → 1.0.4
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 +62 -116
- package/cmd/test-lib/main.js +2 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -1,31 +1,58 @@
|
|
|
1
1
|
# @dpkrn/nodetunnel
|
|
2
2
|
|
|
3
|
-
**
|
|
3
|
+
Give your **local** HTTP server a **public URL** from Node.js — useful for webhooks, demos, sharing a dev server, or testing from another device.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
Internet ──► Tunnel Server ──► yamux stream ──► nodetunnel ──► localhost:<port>
|
|
7
|
-
```
|
|
5
|
+
---
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
## Why use it
|
|
10
8
|
|
|
11
|
-
- **
|
|
12
|
-
-
|
|
9
|
+
- **No separate tunnel process** — call one function from your app.
|
|
10
|
+
- **Works with your existing server** — Express, Fastify, or plain `http`.
|
|
11
|
+
- **Simple API** — you get a public `url` and a `stop()` when you are done.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
- **Node.js 18+**
|
|
18
|
+
- Your app listening on a port (e.g. `8080`)
|
|
19
|
+
- A **tunnel server** reachable from your machine (default: `localhost:9000`)
|
|
20
|
+
|
|
21
|
+
---
|
|
13
22
|
|
|
14
|
-
|
|
23
|
+
## Install
|
|
15
24
|
|
|
16
25
|
```bash
|
|
17
26
|
npm install @dpkrn/nodetunnel
|
|
18
27
|
```
|
|
19
28
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
`import { startTunnel } from "./pkg/tunnel/tunnel.js"`.
|
|
29
|
+
This package is **ESM** (`import` / `export`).
|
|
23
30
|
|
|
24
31
|
---
|
|
25
32
|
|
|
26
|
-
## Quick start (ESM)
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
|
|
35
|
+
## Quick Example
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import express from "express";
|
|
39
|
+
import { startTunnel } from "@dpkrn/nodetunnel";
|
|
40
|
+
|
|
41
|
+
const app = express();
|
|
42
|
+
const PORT = 8080;
|
|
43
|
+
|
|
44
|
+
app.get("/", (req, res) => res.send("OK"));
|
|
45
|
+
|
|
46
|
+
app.listen(PORT, async () => {
|
|
47
|
+
const { url, stop } = await startTunnel(String(PORT));
|
|
48
|
+
//url is your public url using you can access publicly your server
|
|
49
|
+
//stop() is method that will close your connection on error
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Easiest example
|
|
29
56
|
|
|
30
57
|
```js
|
|
31
58
|
import http from "node:http";
|
|
@@ -34,8 +61,7 @@ import { startTunnel } from "@dpkrn/nodetunnel";
|
|
|
34
61
|
const PORT = 8080;
|
|
35
62
|
|
|
36
63
|
const server = http.createServer((req, res) => {
|
|
37
|
-
res.
|
|
38
|
-
res.end("Hello from localhost\n");
|
|
64
|
+
res.end("Hello\n");
|
|
39
65
|
});
|
|
40
66
|
|
|
41
67
|
server.listen(PORT, async () => {
|
|
@@ -53,49 +79,22 @@ server.listen(PORT, async () => {
|
|
|
53
79
|
});
|
|
54
80
|
```
|
|
55
81
|
|
|
56
|
-
|
|
57
|
-
2. Run your script: `node app.js`
|
|
58
|
-
3. Open the printed **public URL** in a browser or `curl` it.
|
|
59
|
-
|
|
60
|
-
---
|
|
61
|
-
|
|
62
|
-
## API
|
|
63
|
-
|
|
64
|
-
### `startTunnel(port, options?)`
|
|
65
|
-
|
|
66
|
-
| Argument | Type | Description |
|
|
67
|
-
|----------|------|-------------|
|
|
68
|
-
| `port` | `string` | Local port your HTTP server listens on (e.g. `"8080"`). |
|
|
69
|
-
| `options` | `object` (optional) | `{ host?: string, serverPort?: number }` — tunnel server address (default `localhost:9000`). |
|
|
70
|
-
|
|
71
|
-
**Returns** a `Promise` resolving to:
|
|
72
|
-
|
|
73
|
-
| Field | Type | Description |
|
|
74
|
-
|-------|------|-------------|
|
|
75
|
-
| `url` | `string` | Public URL assigned by the server (e.g. `http://subdomain.localhost:3000`). |
|
|
76
|
-
| `stop` | `() => void` | Stops the tunnel and closes the connection. |
|
|
77
|
-
|
|
78
|
-
On failure (cannot connect, handshake error), the promise **rejects**. Use `try/catch`.
|
|
82
|
+
Run with `node app.js`. Open the printed URL in a browser or share it for webhooks.
|
|
79
83
|
|
|
80
84
|
---
|
|
81
85
|
|
|
82
|
-
## Express
|
|
83
|
-
|
|
84
|
-
Start the HTTP server first, then call `startTunnel` with the **same port**.
|
|
86
|
+
## Express (same idea)
|
|
85
87
|
|
|
86
88
|
```js
|
|
87
89
|
import express from "express";
|
|
88
90
|
import { startTunnel } from "@dpkrn/nodetunnel";
|
|
89
91
|
|
|
90
92
|
const app = express();
|
|
91
|
-
const PORT =
|
|
93
|
+
const PORT = 8080;
|
|
92
94
|
|
|
93
|
-
app.get("/", (req, res) =>
|
|
94
|
-
res.send("OK");
|
|
95
|
-
});
|
|
95
|
+
app.get("/", (req, res) => res.send("OK"));
|
|
96
96
|
|
|
97
97
|
app.listen(PORT, async () => {
|
|
98
|
-
console.log(`http://127.0.0.1:${PORT}`);
|
|
99
98
|
try {
|
|
100
99
|
const { url, stop } = await startTunnel(String(PORT));
|
|
101
100
|
console.log("Public:", url);
|
|
@@ -104,91 +103,38 @@ app.listen(PORT, async () => {
|
|
|
104
103
|
process.exit(0);
|
|
105
104
|
});
|
|
106
105
|
} catch (e) {
|
|
107
|
-
console.error(
|
|
106
|
+
console.error(e.message);
|
|
108
107
|
}
|
|
109
108
|
});
|
|
110
109
|
```
|
|
111
110
|
|
|
112
111
|
---
|
|
113
112
|
|
|
114
|
-
##
|
|
115
|
-
|
|
116
|
-
- **`Tunnel failed` / connection refused** — Start the tunnel server on the host/port you pass in `options` (default `localhost:9000`).
|
|
117
|
-
- **Wrong path when running scripts** — Run from the `nodetunnel` directory, or use `node nodetunnel/cmd/test-lib/main.js` from the repo root.
|
|
118
|
-
- **Port already in use** — Another process may be bound to your app port; stop it or change `PORT`.
|
|
119
|
-
|
|
120
|
-
---
|
|
121
|
-
|
|
122
|
-
## CLI — `mytunnel` (release install, ngrok-style)
|
|
123
|
-
|
|
124
|
-
Besides embedding **nodetunnel** in a Node app, you can install the **`mytunnel`** CLI from **[devtunnel](https://github.com/DpkRn/devtunnel) releases** — same idea as **ngrok**: one binary, one command, expose a local port. It speaks the same tunnel server + yamux protocol as this library.
|
|
125
|
-
|
|
126
|
-
```
|
|
127
|
-
Internet ──► Tunnel Server ──► yamux stream ──► mytunnel ──► localhost:<port>
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
The **`mytunnel`** binary lets you expose any local port with a single command (no Node required for the client).
|
|
131
|
-
|
|
132
|
-
### Install
|
|
133
|
-
|
|
134
|
-
```bash
|
|
135
|
-
curl -fsSL https://raw.githubusercontent.com/DpkRn/devtunnel/master/install.sh | bash
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
Auto-detects your OS and CPU architecture (macOS Apple Silicon, macOS Intel, Linux x86_64) and installs to `/usr/local/bin`.
|
|
139
|
-
|
|
140
|
-
**Or build the Go client from source** (from the [devtunnel](https://github.com/DpkRn/devtunnel) or [gotunnel](https://github.com/DpkRn/gotunnel) repo):
|
|
141
|
-
|
|
142
|
-
```bash
|
|
143
|
-
go build -o mytunnel ./cmd/client
|
|
144
|
-
sudo mv mytunnel /usr/local/bin/
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Usage
|
|
148
|
-
|
|
149
|
-
```bash
|
|
150
|
-
mytunnel http <port>
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
**Example — expose a dev server on port 3000:**
|
|
113
|
+
## API
|
|
154
114
|
|
|
155
|
-
|
|
156
|
-
$ mytunnel http 3000
|
|
157
|
-
|
|
158
|
-
╔══════════════════════════════════════════════════╗
|
|
159
|
-
║ 🚇 mytunnel — tunnel is live ║
|
|
160
|
-
╠══════════════════════════════════════════════════╣
|
|
161
|
-
║ 🌍 Public → http://abc123.example.com ║
|
|
162
|
-
║ 💻 Local → http://localhost:3000 ║
|
|
163
|
-
╠══════════════════════════════════════════════════╣
|
|
164
|
-
║ ⚡ Forwarding requests... ║
|
|
165
|
-
║ 🛑 Press Ctrl+C to stop ║
|
|
166
|
-
╚══════════════════════════════════════════════════╝
|
|
167
|
-
```
|
|
115
|
+
### `await startTunnel(port, options?)`
|
|
168
116
|
|
|
169
|
-
|
|
117
|
+
| Argument | Description |
|
|
118
|
+
|----------|-------------|
|
|
119
|
+
| `port` | String, e.g. `"8080"` — must match the port your HTTP server uses. |
|
|
120
|
+
| `options` | Optional. `{ host?: string, serverPort?: number }` — where to reach your tunnel server (defaults: `localhost` and `9000`). |
|
|
170
121
|
|
|
171
|
-
|
|
122
|
+
**Returns:** `{ url, stop }`
|
|
172
123
|
|
|
173
|
-
|
|
|
174
|
-
|
|
175
|
-
| `
|
|
176
|
-
| `
|
|
124
|
+
| Field | Description |
|
|
125
|
+
|-------|-------------|
|
|
126
|
+
| `url` | Public URL people can hit. |
|
|
127
|
+
| `stop` | Call to tear down the tunnel. |
|
|
177
128
|
|
|
178
|
-
|
|
129
|
+
Errors **reject** the promise — use `try/catch`.
|
|
179
130
|
|
|
180
131
|
---
|
|
181
132
|
|
|
182
|
-
##
|
|
183
|
-
|
|
184
|
-
This package is published as **`@dpkrn/nodetunnel`** ([scoped package](https://docs.npmjs.com/about-scopes)). **`publishConfig.access`** is set to **`"public"`** so anyone can install it without an npm org.
|
|
185
|
-
|
|
186
|
-
1. Bump **`version`** in `package.json` (semver).
|
|
187
|
-
2. Dry run: `npm pack` — inspect the tarball.
|
|
188
|
-
3. Log in: `npm login` (account must have permission to publish under the **`@dpkrn`** scope).
|
|
189
|
-
4. From the **`nodetunnel`** directory: `npm publish`
|
|
133
|
+
## Troubleshooting
|
|
190
134
|
|
|
191
|
-
|
|
135
|
+
- **Connection / tunnel failed** — Confirm your tunnel server is running and that `host` / `serverPort` match your setup.
|
|
136
|
+
- **Nothing loads on the public URL** — Confirm your local server is already listening on the port you passed to `startTunnel`.
|
|
137
|
+
- **Port in use** — Pick another port or stop the other process using it.
|
|
192
138
|
|
|
193
139
|
---
|
|
194
140
|
|
package/cmd/test-lib/main.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dpkrn/nodetunnel",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Expose a local HTTP server through a devtunnel/gotunnel-compatible server (yamux + JSON). Node.js 18+.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"tunnel",
|
|
@@ -13,13 +13,13 @@
|
|
|
13
13
|
"http",
|
|
14
14
|
"multiplex"
|
|
15
15
|
],
|
|
16
|
-
"homepage": "https://github.com/DpkRn/
|
|
16
|
+
"homepage": "https://github.com/DpkRn/nodetunnel#readme",
|
|
17
17
|
"bugs": {
|
|
18
|
-
"url": "https://github.com/DpkRn/
|
|
18
|
+
"url": "https://github.com/DpkRn/nodetunnel/issues"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
|
22
|
-
"url": "git+https://github.com/DpkRn/
|
|
22
|
+
"url": "git+https://github.com/DpkRn/nodetunnel.git",
|
|
23
23
|
"directory": "nodetunnel"
|
|
24
24
|
},
|
|
25
25
|
"license": "MIT",
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"prepublishOnly": "node -e \"import('./pkg/tunnel/tunnel.js').then(() => console.log('pack ok')).catch(e => { console.error(e); process.exit(1); })\""
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
+
"@dpkrn/nodetunnel": "^1.0.2",
|
|
52
53
|
"yamux-js": "^0.2.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|