@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 ADDED
@@ -0,0 +1,3 @@
1
+ PORT=1234
2
+ SCHEME=https
3
+ DOMAIN=example.so
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015 Roman Shtylman
3
+ Copyright (c) 2024 Rubric Labs Inc
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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 (open && data.url) browser(data.url);
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 (!(event.target as any).readyState) throw "Not ready";
53
+ if (!event.target.readyState) throw "Not ready";
51
54
  });
52
55
 
53
56
  socket.addEventListener("close", () => {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  },
5
5
  "name": "@rubriclab/bunl",
6
6
  "description": "Expose localhost to the world",
7
- "version": "0.0.12",
7
+ "version": "0.0.14",
8
8
  "license": "MIT",
9
9
  "repository": {
10
10
  "type": "git",
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, any>();
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(JSON.stringify({ method, pathname, headers }));
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: resHeaders, body } = JSON.parse(res);
61
- const init = { headers: resHeaders, status, statusText };
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, init);
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 as string);
82
+ const { pathname } = JSON.parse(message);
83
83
  clientData.set(`${ws.data.id}/${pathname}`, message);
84
84
  },
85
85
  close(ws) {