@rubriclab/bunl 0.0.23 → 0.0.24

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/build/client.js CHANGED
@@ -437,7 +437,7 @@ var open_default = open;
437
437
 
438
438
  // client.ts
439
439
  async function main({
440
- url,
440
+ port,
441
441
  domain,
442
442
  subdomain,
443
443
  open: open2
@@ -458,6 +458,8 @@ async function main({
458
458
  open_default(data.url);
459
459
  }
460
460
  if (data.method) {
461
+ console.log(`${data.method} ${data.pathname}`);
462
+ const url = `http://localhost:${port}`;
461
463
  const res = await fetch(`${url}${data.pathname || ""}`, {
462
464
  method: data.method,
463
465
  headers: data.headers,
@@ -465,63 +467,37 @@ async function main({
465
467
  });
466
468
  const { status, statusText, headers } = res;
467
469
  const body = await res.text();
468
- const serializedRes = JSON.stringify({
470
+ const payload = {
469
471
  pathname: data.pathname,
470
472
  status,
471
473
  statusText,
472
474
  headers: Object.fromEntries(headers),
473
475
  body
474
- });
475
- socket.send(serializedRes);
476
+ };
477
+ socket.send(JSON.stringify(payload));
476
478
  }
477
479
  });
478
480
  socket.addEventListener("open", (event) => {
479
481
  if (!event.target.readyState)
480
- throw "Not ready";
482
+ throw "not ready";
481
483
  });
482
484
  socket.addEventListener("close", () => {
483
- console.log(`failed to connect to server`);
484
- process.exit();
485
+ throw "server closed connection";
485
486
  });
486
487
  }
487
488
  var { values } = parseArgs({
488
489
  args: process.argv,
489
490
  options: {
490
- port: {
491
- type: "string",
492
- required: true,
493
- short: "p"
494
- },
495
- domain: {
496
- type: "string",
497
- default: "localhost:1234",
498
- short: "d"
499
- },
500
- subdomain: {
501
- type: "string",
502
- short: "s"
503
- },
504
- open: {
505
- type: "boolean",
506
- short: "o"
507
- },
508
- version: {
509
- type: "boolean",
510
- short: "v"
511
- }
491
+ port: { type: "string", short: "p", default: "3000" },
492
+ domain: { type: "string", short: "d", default: "localhost:1234" },
493
+ subdomain: { type: "string", short: "s" },
494
+ open: { type: "boolean", short: "o" },
495
+ version: { type: "boolean", short: "v" }
512
496
  },
513
497
  allowPositionals: true
514
498
  });
515
499
  if (values.version) {
516
- console.log("0.0.23");
500
+ console.log("0.0.24");
517
501
  process.exit();
518
502
  }
519
- if (!values.port)
520
- throw "pass -p 3000";
521
- var { port, domain, subdomain, open: open2 } = values;
522
- main({
523
- url: `localhost:${port}`,
524
- domain,
525
- subdomain,
526
- open: open2
527
- });
503
+ main(values);
package/client.ts CHANGED
@@ -1,13 +1,14 @@
1
1
  import { parseArgs } from "util";
2
2
  import browser from "open";
3
+ import type { Payload } from "./types";
3
4
 
4
5
  async function main({
5
- url,
6
+ port,
6
7
  domain,
7
8
  subdomain,
8
9
  open,
9
10
  }: {
10
- url: string;
11
+ port?: string;
11
12
  domain?: string;
12
13
  subdomain?: string;
13
14
  open?: boolean;
@@ -28,6 +29,9 @@ async function main({
28
29
  }
29
30
 
30
31
  if (data.method) {
32
+ console.log(`\x1b[32m${data.method}\x1b[0m ${data.pathname}`);
33
+
34
+ const url = `http://localhost:${port}`;
31
35
  const res = await fetch(`${url}${data.pathname || ""}`, {
32
36
  method: data.method,
33
37
  headers: data.headers,
@@ -37,25 +41,24 @@ async function main({
37
41
  const { status, statusText, headers } = res;
38
42
  const body = await res.text();
39
43
 
40
- const serializedRes = JSON.stringify({
44
+ const payload: Payload = {
41
45
  pathname: data.pathname,
42
46
  status,
43
47
  statusText,
44
48
  headers: Object.fromEntries(headers),
45
49
  body,
46
- });
50
+ };
47
51
 
48
- socket.send(serializedRes);
52
+ socket.send(JSON.stringify(payload));
49
53
  }
50
54
  });
51
55
 
52
56
  socket.addEventListener("open", (event) => {
53
- if (!event.target.readyState) throw "Not ready";
57
+ if (!event.target.readyState) throw "not ready";
54
58
  });
55
59
 
56
60
  socket.addEventListener("close", () => {
57
- console.log(`\x1b[31mfailed to connect to server\x1b[0m`);
58
- process.exit();
61
+ throw "server closed connection";
59
62
  });
60
63
  }
61
64
 
@@ -67,28 +70,11 @@ async function main({
67
70
  const { values } = parseArgs({
68
71
  args: process.argv,
69
72
  options: {
70
- port: {
71
- type: "string",
72
- required: true,
73
- short: "p",
74
- },
75
- domain: {
76
- type: "string",
77
- default: "localhost:1234",
78
- short: "d",
79
- },
80
- subdomain: {
81
- type: "string",
82
- short: "s",
83
- },
84
- open: {
85
- type: "boolean",
86
- short: "o",
87
- },
88
- version: {
89
- type: "boolean",
90
- short: "v",
91
- },
73
+ port: { type: "string", short: "p", default: "3000" },
74
+ domain: { type: "string", short: "d", default: "localhost:1234" },
75
+ subdomain: { type: "string", short: "s" },
76
+ open: { type: "boolean", short: "o" },
77
+ version: { type: "boolean", short: "v" },
92
78
  },
93
79
  allowPositionals: true,
94
80
  });
@@ -98,13 +84,4 @@ if (values.version) {
98
84
  process.exit();
99
85
  }
100
86
 
101
- if (!values.port) throw "pass -p 3000";
102
-
103
- const { port, domain, subdomain, open } = values;
104
-
105
- main({
106
- url: `localhost:${port}`,
107
- domain,
108
- subdomain,
109
- open,
110
- });
87
+ main(values);
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.23",
7
+ "version": "0.0.24",
8
8
  "license": "MIT",
9
9
  "repository": {
10
10
  "type": "git",
package/server.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { serve, sleep, type ServerWebSocket } from "bun";
2
2
  import { uid } from "./utils";
3
-
4
- type Client = { id: string };
3
+ import type { Client, Payload } from "./types";
5
4
 
6
5
  const port = Bun.env.PORT || 1234;
7
6
  const scheme = Bun.env.SCHEME || "http";
@@ -35,10 +34,14 @@ serve<Client>({
35
34
  const client = clients.get(subdomain)!;
36
35
  const { method, url, headers: reqHeaders } = req;
37
36
  const reqBody = await req.text();
38
- const pathname = new URL(url).pathname || "";
39
- client.send(
40
- JSON.stringify({ method, pathname, body: reqBody, headers: reqHeaders })
41
- );
37
+ const pathname = new URL(url).pathname;
38
+ const payload: Payload = {
39
+ method,
40
+ pathname,
41
+ body: reqBody,
42
+ headers: reqHeaders,
43
+ };
44
+ client.send(JSON.stringify(payload));
42
45
 
43
46
  // Wait for the client to cache its response above
44
47
  await sleep(1);
@@ -67,9 +70,7 @@ serve<Client>({
67
70
  websocket: {
68
71
  open(ws) {
69
72
  clients.set(ws.data.id, ws);
70
- console.log(
71
- `\x1b[32mconnected to ${ws.data.id} (${clients.size} total)\x1b[0m`
72
- );
73
+ console.log(`\x1b[32m+ ${ws.data.id} (${clients.size} total)\x1b[0m`);
73
74
  ws.send(
74
75
  JSON.stringify({
75
76
  url: `${scheme}://${ws.data.id}.${domain}`,
@@ -78,8 +79,7 @@ serve<Client>({
78
79
  },
79
80
  message(ws, message: string) {
80
81
  console.log("message from", ws.data.id);
81
-
82
- const { pathname } = JSON.parse(message);
82
+ const { pathname } = JSON.parse(message) as Payload;
83
83
  clientData.set(`${ws.data.id}/${pathname}`, message);
84
84
  },
85
85
  close(ws) {
@@ -89,4 +89,4 @@ serve<Client>({
89
89
  },
90
90
  });
91
91
 
92
- console.log(`Websocket server up at ws://${domain}`);
92
+ console.log(`websocket server up at ws://${domain}`);
package/types.ts ADDED
@@ -0,0 +1,10 @@
1
+ export type Client = { id: string };
2
+
3
+ export type Payload = {
4
+ status?: number;
5
+ statusText?: string;
6
+ method?: string;
7
+ pathname?: string;
8
+ body: string;
9
+ headers: object;
10
+ };