@dpkrn/nodetunnel 1.0.0 → 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.
package/README.md CHANGED
@@ -1,31 +1,60 @@
1
1
  # @dpkrn/nodetunnel
2
2
 
3
- **nodetunnel** is a Node.js library for exposing a local HTTP server through a compatible tunnel server (yamux + JSON wire format). It is published on npm as **`@dpkrn/nodetunnel`**.
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
+ ---
6
+
7
+ ## Why use it
8
+
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
+ You need a **tunnel server** running that this library can connect to (host and port are configurable; defaults suit a typical local setup).
14
+
15
+ ---
16
+
17
+ ## Requirements
8
18
 
9
- **Requirements**
19
+ - **Node.js 18+**
20
+ - Your app listening on a port (e.g. `8080`)
21
+ - A **tunnel server** reachable from your machine (default: `localhost:9000`)
10
22
 
11
- - **Node.js 18+** (uses global `fetch` / `Headers`)
12
- - A running tunnel **server** reachable at `localhost:9000` (for example the [devtunnel](https://github.com/DpkRn/devtunnel) server)
23
+ ---
13
24
 
14
- ### Install from npm
25
+ ## Install
15
26
 
16
27
  ```bash
17
28
  npm install @dpkrn/nodetunnel
18
29
  ```
19
30
 
20
- When developing **in this repository**, import with a relative path instead, for example:
21
-
22
- `import { startTunnel } from "./pkg/tunnel/tunnel.js"`.
31
+ This package is **ESM** (`import` / `export`).
23
32
 
24
33
  ---
25
34
 
26
- ## Quick start (ESM)
27
35
 
28
- This package is **`"type": "module"`**. Use `import`, not `require`.
36
+
37
+ ## Quick Example
38
+
39
+ ```js
40
+ import express from "express";
41
+ import { startTunnel } from "@dpkrn/nodetunnel";
42
+
43
+ const app = express();
44
+ const PORT = 8080;
45
+
46
+ app.get("/", (req, res) => res.send("OK"));
47
+
48
+ app.listen(PORT, async () => {
49
+ const { url, stop } = await startTunnel(String(PORT));
50
+ //url is your public url using you can access publicly your server
51
+ //stop() is method that will close your connection on error
52
+ });
53
+ ```
54
+
55
+ ---
56
+
57
+ ## Easiest example
29
58
 
30
59
  ```js
31
60
  import http from "node:http";
@@ -34,8 +63,7 @@ import { startTunnel } from "@dpkrn/nodetunnel";
34
63
  const PORT = 8080;
35
64
 
36
65
  const server = http.createServer((req, res) => {
37
- res.writeHead(200, { "Content-Type": "text/plain" });
38
- res.end("Hello from localhost\n");
66
+ res.end("Hello\n");
39
67
  });
40
68
 
41
69
  server.listen(PORT, async () => {
@@ -53,49 +81,22 @@ server.listen(PORT, async () => {
53
81
  });
54
82
  ```
55
83
 
56
- 1. Start your tunnel **server** (port **9000**).
57
- 2. Run your script: `node app.js`
58
- 3. Open the printed **public URL** in a browser or `curl` it.
84
+ Run with `node app.js`. Open the printed URL in a browser or share it for webhooks.
59
85
 
60
86
  ---
61
87
 
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`.
79
-
80
- ---
81
-
82
- ## Express example
83
-
84
- Start the HTTP server first, then call `startTunnel` with the **same port**.
88
+ ## Express (same idea)
85
89
 
86
90
  ```js
87
91
  import express from "express";
88
92
  import { startTunnel } from "@dpkrn/nodetunnel";
89
93
 
90
94
  const app = express();
91
- const PORT = process.env.PORT || 8080;
95
+ const PORT = 8080;
92
96
 
93
- app.get("/", (req, res) => {
94
- res.send("OK");
95
- });
97
+ app.get("/", (req, res) => res.send("OK"));
96
98
 
97
99
  app.listen(PORT, async () => {
98
- console.log(`http://127.0.0.1:${PORT}`);
99
100
  try {
100
101
  const { url, stop } = await startTunnel(String(PORT));
101
102
  console.log("Public:", url);
@@ -104,91 +105,38 @@ app.listen(PORT, async () => {
104
105
  process.exit(0);
105
106
  });
106
107
  } catch (e) {
107
- console.error("Tunnel error:", e.message);
108
+ console.error(e.message);
108
109
  }
109
110
  });
110
111
  ```
111
112
 
112
113
  ---
113
114
 
114
- ## Troubleshooting
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:**
115
+ ## API
154
116
 
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
- ```
117
+ ### `await startTunnel(port, options?)`
168
118
 
169
- Press `Ctrl+C` to stop.
119
+ | Argument | Description |
120
+ |----------|-------------|
121
+ | `port` | String, e.g. `"8080"` — must match the port your HTTP server uses. |
122
+ | `options` | Optional. `{ host?: string, serverPort?: number }` — where to reach your tunnel server (defaults: `localhost` and `9000`). |
170
123
 
171
- ### Commands
124
+ **Returns:** `{ url, stop }`
172
125
 
173
- | Command | Description |
174
- |---------|-------------|
175
- | `mytunnel http <port>` | Forward public HTTP traffic to `localhost:<port>` |
176
- | `mytunnel help` | Show help |
126
+ | Field | Description |
127
+ |-------|-------------|
128
+ | `url` | Public URL people can hit. |
129
+ | `stop` | Call to tear down the tunnel. |
177
130
 
178
- You still need the **tunnel server** running (e.g. on `localhost:9000`) for both **nodetunnel** and **mytunnel**.
131
+ Errors **reject** the promise use `try/catch`.
179
132
 
180
133
  ---
181
134
 
182
- ## Publishing to npm (maintainers)
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`
135
+ ## Troubleshooting
190
136
 
191
- Adjust **`repository`**, **`bugs`**, and **`homepage`** in `package.json` if your Git remote or default branch differs.
137
+ - **Connection / tunnel failed** Confirm your tunnel server is running and that `host` / `serverPort` match your setup.
138
+ - **Nothing loads on the public URL** — Confirm your local server is already listening on the port you passed to `startTunnel`.
139
+ - **Port in use** — Pick another port or stop the other process using it.
192
140
 
193
141
  ---
194
142
 
@@ -1,5 +1,6 @@
1
1
  import express from "express";
2
- import { startTunnel } from "../../pkg/tunnel/tunnel.js";
2
+ import { startTunnel } from "@dpkrn/nodetunnel";
3
+
3
4
 
4
5
  const app = express();
5
6
  app.set("etag", false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dpkrn/nodetunnel",
3
- "version": "1.0.0",
3
+ "version": "1.0.3",
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",
@@ -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": {