@rubriclab/bunl 0.0.12 → 0.0.14
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/.env.example +3 -0
- package/LICENSE +1 -1
- package/client.ts +6 -3
- package/package.json +1 -1
- package/server.ts +11 -11
package/.env.example
ADDED
package/LICENSE
CHANGED
package/client.ts
CHANGED
|
@@ -21,14 +21,17 @@ async function main({
|
|
|
21
21
|
|
|
22
22
|
socket.addEventListener("message", async (event) => {
|
|
23
23
|
const data = JSON.parse(event.data as string);
|
|
24
|
-
console.log("message:", data);
|
|
25
24
|
|
|
26
|
-
if (
|
|
25
|
+
if (data.url) {
|
|
26
|
+
console.log(`\n\n↪ Your URL: \x1b[32m${data.url}\x1b[0m\n`);
|
|
27
|
+
if (open) browser(data.url);
|
|
28
|
+
}
|
|
27
29
|
|
|
28
30
|
if (data.method) {
|
|
29
31
|
const res = await fetch(`${url}${data.pathname}`, {
|
|
30
32
|
method: data.method,
|
|
31
33
|
headers: data.headers,
|
|
34
|
+
body: data.body,
|
|
32
35
|
});
|
|
33
36
|
|
|
34
37
|
const { status, statusText, headers } = res;
|
|
@@ -47,7 +50,7 @@ async function main({
|
|
|
47
50
|
});
|
|
48
51
|
|
|
49
52
|
socket.addEventListener("open", (event) => {
|
|
50
|
-
if (!
|
|
53
|
+
if (!event.target.readyState) throw "Not ready";
|
|
51
54
|
});
|
|
52
55
|
|
|
53
56
|
socket.addEventListener("close", () => {
|
package/package.json
CHANGED
package/server.ts
CHANGED
|
@@ -7,9 +7,8 @@ const port = Bun.env.PORT || 1234;
|
|
|
7
7
|
const scheme = Bun.env.SCHEME || "http";
|
|
8
8
|
const domain = Bun.env.DOMAIN || `localhost:${port}`;
|
|
9
9
|
|
|
10
|
-
// TODO: replace this with Redis to preserve sessions across deployments
|
|
11
10
|
const clients = new Map<string, ServerWebSocket<Client>>();
|
|
12
|
-
const clientData = new Map<string,
|
|
11
|
+
const clientData = new Map<string, string>();
|
|
13
12
|
|
|
14
13
|
serve<Client>({
|
|
15
14
|
port,
|
|
@@ -34,9 +33,12 @@ serve<Client>({
|
|
|
34
33
|
|
|
35
34
|
// The magic: forward the req to the client
|
|
36
35
|
const client = clients.get(subdomain)!;
|
|
37
|
-
const { method, url, headers } = req;
|
|
36
|
+
const { method, url, headers: reqHeaders } = req;
|
|
37
|
+
const reqBody = await req.text();
|
|
38
38
|
const { pathname } = new URL(url);
|
|
39
|
-
client.send(
|
|
39
|
+
client.send(
|
|
40
|
+
JSON.stringify({ method, pathname, body: reqBody, headers: reqHeaders })
|
|
41
|
+
);
|
|
40
42
|
|
|
41
43
|
// Wait for the client to cache its response above
|
|
42
44
|
await sleep(1);
|
|
@@ -57,12 +59,10 @@ serve<Client>({
|
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
const { status, statusText, headers
|
|
61
|
-
|
|
62
|
-
delete resHeaders["content-encoding"];
|
|
63
|
-
delete resHeaders["Content-Encoding"];
|
|
62
|
+
const { status, statusText, headers, body } = JSON.parse(res);
|
|
63
|
+
delete headers["content-encoding"];
|
|
64
64
|
|
|
65
|
-
return new Response(body,
|
|
65
|
+
return new Response(body, { status, statusText, headers });
|
|
66
66
|
},
|
|
67
67
|
websocket: {
|
|
68
68
|
open(ws) {
|
|
@@ -76,10 +76,10 @@ serve<Client>({
|
|
|
76
76
|
})
|
|
77
77
|
);
|
|
78
78
|
},
|
|
79
|
-
message(ws, message) {
|
|
79
|
+
message(ws, message: string) {
|
|
80
80
|
console.log("message from", ws.data.id);
|
|
81
81
|
|
|
82
|
-
const { pathname } = JSON.parse(message
|
|
82
|
+
const { pathname } = JSON.parse(message);
|
|
83
83
|
clientData.set(`${ws.data.id}/${pathname}`, message);
|
|
84
84
|
},
|
|
85
85
|
close(ws) {
|