Skip to content

Commit 6724082

Browse files
committed
transformRequest
1 parent 0a20f78 commit 6724082

File tree

14 files changed

+1499
-2362
lines changed

14 files changed

+1499
-2362
lines changed

packages/tunnel/README.md

Lines changed: 0 additions & 231 deletions
This file was deleted.

packages/tunnel/examples/client.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,20 @@
1111
* npx wscat -l 8000
1212
* Then connect via the tunnel URL using wscat or a browser.
1313
*/
14+
/** biome-ignore-all lint/suspicious/noConsole: this is an example file */
1415

1516
import { TunnelClient } from "../src/client";
1617

17-
const SERVER_URL = "https://try.blink.host";
18+
const SERVER_URL = "http://localhost:8080";
1819
const CLIENT_SECRET = crypto.randomUUID();
1920
const LOCAL_SERVER_PORT = 8000;
2021

2122
const client = new TunnelClient({
2223
serverUrl: SERVER_URL,
2324
secret: CLIENT_SECRET,
24-
// Transform WebSocket requests to point to local server
25-
transformWebSocketRequest: ({ url, headers }) => {
25+
transformRequest: async ({ method, url, headers }) => {
2626
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 };
5628
},
5729
onConnect: ({ url, id }) => {
5830
console.log(`Connected to tunnel server!`);

packages/tunnel/examples/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* Run with: npx tsx examples/server.ts
55
*/
6+
/** biome-ignore-all lint/suspicious/noConsole: this is an example file */
67

78
import { createLocalServer } from "../src/server/local";
89

0 commit comments

Comments
 (0)