|
11 | 11 | * npx wscat -l 8000 |
12 | 12 | * Then connect via the tunnel URL using wscat or a browser. |
13 | 13 | */ |
| 14 | +/** biome-ignore-all lint/suspicious/noConsole: this is an example file */ |
14 | 15 |
|
15 | 16 | import { TunnelClient } from "../src/client"; |
16 | 17 |
|
17 | | -const SERVER_URL = "https://try.blink.host"; |
| 18 | +const SERVER_URL = "http://localhost:8080"; |
18 | 19 | const CLIENT_SECRET = crypto.randomUUID(); |
19 | 20 | const LOCAL_SERVER_PORT = 8000; |
20 | 21 |
|
21 | 22 | const client = new TunnelClient({ |
22 | 23 | serverUrl: SERVER_URL, |
23 | 24 | secret: CLIENT_SECRET, |
24 | | - // Transform WebSocket requests to point to local server |
25 | | - transformWebSocketRequest: ({ url, headers }) => { |
| 25 | + transformRequest: async ({ method, url, headers }) => { |
26 | 26 | url.host = `localhost:${LOCAL_SERVER_PORT}`; |
27 | | - url.protocol = "ws:"; |
28 | | - return { url, headers }; |
29 | | - }, |
30 | | - onRequest: async (request) => { |
31 | | - // Forward requests to the local server |
32 | | - const url = new URL(request.url); |
33 | | - url.host = `localhost:${LOCAL_SERVER_PORT}`; |
34 | | - url.protocol = "http:"; |
35 | | - |
36 | | - const newRequest = new Request(url.toString(), { |
37 | | - method: request.method, |
38 | | - headers: request.headers, |
39 | | - body: request.body, |
40 | | - // @ts-expect-error duplex is needed for streaming bodies |
41 | | - duplex: "half", |
42 | | - }); |
43 | | - |
44 | | - try { |
45 | | - return await fetch(newRequest); |
46 | | - } catch (error) { |
47 | | - console.error("Error forwarding request:", error); |
48 | | - return new Response( |
49 | | - JSON.stringify({ error: "Failed to connect to local server" }), |
50 | | - { |
51 | | - status: 502, |
52 | | - headers: { "Content-Type": "application/json" }, |
53 | | - } |
54 | | - ); |
55 | | - } |
| 27 | + return { method, url, headers }; |
56 | 28 | }, |
57 | 29 | onConnect: ({ url, id }) => { |
58 | 30 | console.log(`Connected to tunnel server!`); |
|
0 commit comments