@rubriclab/bunl 0.0.22 → 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 +15 -87
- package/client.ts +18 -44
- package/package.json +1 -1
- package/server.ts +12 -12
- package/types.ts +10 -0
package/build/client.js
CHANGED
|
@@ -434,57 +434,10 @@ defineLazyProperty(apps, "edge", () => detectPlatformBinary({
|
|
|
434
434
|
defineLazyProperty(apps, "browser", () => "browser");
|
|
435
435
|
defineLazyProperty(apps, "browserPrivate", () => "browserPrivate");
|
|
436
436
|
var open_default = open;
|
|
437
|
-
// package.json
|
|
438
|
-
var package_default = {
|
|
439
|
-
bin: {
|
|
440
|
-
bunl: "build/client.js"
|
|
441
|
-
},
|
|
442
|
-
name: "@rubriclab/bunl",
|
|
443
|
-
description: "Expose localhost to the world",
|
|
444
|
-
version: "0.0.22",
|
|
445
|
-
license: "MIT",
|
|
446
|
-
repository: {
|
|
447
|
-
type: "git",
|
|
448
|
-
url: "git+https://github.com/RubricLab/bunl.git"
|
|
449
|
-
},
|
|
450
|
-
dependencies: {
|
|
451
|
-
"human-id": "^4.1.1",
|
|
452
|
-
open: "^10.1.0"
|
|
453
|
-
},
|
|
454
|
-
devDependencies: {
|
|
455
|
-
"@types/bun": "latest"
|
|
456
|
-
},
|
|
457
|
-
main: "build/client.js",
|
|
458
|
-
scripts: {
|
|
459
|
-
server: "bun server.ts",
|
|
460
|
-
"dev:server": "bun --hot server.ts",
|
|
461
|
-
client: "bun --hot client.ts",
|
|
462
|
-
demo: "bun --hot demo.ts",
|
|
463
|
-
"client:upgrade": "bun rm -g @rubriclab/bunl && bun i -g @rubriclab/bunl@latest",
|
|
464
|
-
build: "BUILD=build/client.js && bun build client.ts --outdir build --target bun && echo -e \"#! /usr/bin/env bun
|
|
465
|
-
$(cat $BUILD)\" > $BUILD",
|
|
466
|
-
"npm:publish": "bun run build && npm publish"
|
|
467
|
-
},
|
|
468
|
-
type: "module",
|
|
469
|
-
peerDependencies: {
|
|
470
|
-
typescript: "^5.0.0"
|
|
471
|
-
},
|
|
472
|
-
publishConfig: {
|
|
473
|
-
"@RubricLab:registry": "https://registry.npmjs.org",
|
|
474
|
-
access: "public",
|
|
475
|
-
registry: "https://registry.npmjs.org"
|
|
476
|
-
},
|
|
477
|
-
homepage: "https://github.com/RubricLab/bunl#readme",
|
|
478
|
-
keywords: [
|
|
479
|
-
"local",
|
|
480
|
-
"tunnel",
|
|
481
|
-
"rubric"
|
|
482
|
-
]
|
|
483
|
-
};
|
|
484
437
|
|
|
485
438
|
// client.ts
|
|
486
439
|
async function main({
|
|
487
|
-
|
|
440
|
+
port,
|
|
488
441
|
domain,
|
|
489
442
|
subdomain,
|
|
490
443
|
open: open2
|
|
@@ -504,8 +457,9 @@ async function main({
|
|
|
504
457
|
if (open2)
|
|
505
458
|
open_default(data.url);
|
|
506
459
|
}
|
|
507
|
-
console.log(data.pathname);
|
|
508
460
|
if (data.method) {
|
|
461
|
+
console.log(`[32m${data.method}[0m ${data.pathname}`);
|
|
462
|
+
const url = `http://localhost:${port}`;
|
|
509
463
|
const res = await fetch(`${url}${data.pathname || ""}`, {
|
|
510
464
|
method: data.method,
|
|
511
465
|
headers: data.headers,
|
|
@@ -513,63 +467,37 @@ async function main({
|
|
|
513
467
|
});
|
|
514
468
|
const { status, statusText, headers } = res;
|
|
515
469
|
const body = await res.text();
|
|
516
|
-
const
|
|
470
|
+
const payload = {
|
|
517
471
|
pathname: data.pathname,
|
|
518
472
|
status,
|
|
519
473
|
statusText,
|
|
520
474
|
headers: Object.fromEntries(headers),
|
|
521
475
|
body
|
|
522
|
-
}
|
|
523
|
-
socket.send(
|
|
476
|
+
};
|
|
477
|
+
socket.send(JSON.stringify(payload));
|
|
524
478
|
}
|
|
525
479
|
});
|
|
526
480
|
socket.addEventListener("open", (event) => {
|
|
527
481
|
if (!event.target.readyState)
|
|
528
|
-
throw "
|
|
482
|
+
throw "not ready";
|
|
529
483
|
});
|
|
530
484
|
socket.addEventListener("close", () => {
|
|
531
|
-
|
|
532
|
-
process.exit();
|
|
485
|
+
throw "server closed connection";
|
|
533
486
|
});
|
|
534
487
|
}
|
|
535
488
|
var { values } = parseArgs({
|
|
536
489
|
args: process.argv,
|
|
537
490
|
options: {
|
|
538
|
-
port: {
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
}
|
|
543
|
-
domain: {
|
|
544
|
-
type: "string",
|
|
545
|
-
default: "localhost:1234",
|
|
546
|
-
short: "d"
|
|
547
|
-
},
|
|
548
|
-
subdomain: {
|
|
549
|
-
type: "string",
|
|
550
|
-
short: "s"
|
|
551
|
-
},
|
|
552
|
-
open: {
|
|
553
|
-
type: "boolean",
|
|
554
|
-
short: "o"
|
|
555
|
-
},
|
|
556
|
-
version: {
|
|
557
|
-
type: "boolean",
|
|
558
|
-
short: "v"
|
|
559
|
-
}
|
|
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" }
|
|
560
496
|
},
|
|
561
497
|
allowPositionals: true
|
|
562
498
|
});
|
|
563
499
|
if (values.version) {
|
|
564
|
-
console.log(
|
|
500
|
+
console.log("0.0.24");
|
|
565
501
|
process.exit();
|
|
566
502
|
}
|
|
567
|
-
|
|
568
|
-
throw "pass -p 3000";
|
|
569
|
-
var { port, domain, subdomain, open: open2 } = values;
|
|
570
|
-
main({
|
|
571
|
-
url: `localhost:${port}`,
|
|
572
|
-
domain,
|
|
573
|
-
subdomain,
|
|
574
|
-
open: open2
|
|
575
|
-
});
|
|
503
|
+
main(values);
|
package/client.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { parseArgs } from "util";
|
|
2
2
|
import browser from "open";
|
|
3
|
-
import
|
|
3
|
+
import type { Payload } from "./types";
|
|
4
4
|
|
|
5
5
|
async function main({
|
|
6
|
-
|
|
6
|
+
port,
|
|
7
7
|
domain,
|
|
8
8
|
subdomain,
|
|
9
9
|
open,
|
|
10
10
|
}: {
|
|
11
|
-
|
|
11
|
+
port?: string;
|
|
12
12
|
domain?: string;
|
|
13
13
|
subdomain?: string;
|
|
14
14
|
open?: boolean;
|
|
@@ -28,9 +28,10 @@ async function main({
|
|
|
28
28
|
if (open) browser(data.url);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
console.log(data.pathname);
|
|
32
|
-
|
|
33
31
|
if (data.method) {
|
|
32
|
+
console.log(`\x1b[32m${data.method}\x1b[0m ${data.pathname}`);
|
|
33
|
+
|
|
34
|
+
const url = `http://localhost:${port}`;
|
|
34
35
|
const res = await fetch(`${url}${data.pathname || ""}`, {
|
|
35
36
|
method: data.method,
|
|
36
37
|
headers: data.headers,
|
|
@@ -40,25 +41,24 @@ async function main({
|
|
|
40
41
|
const { status, statusText, headers } = res;
|
|
41
42
|
const body = await res.text();
|
|
42
43
|
|
|
43
|
-
const
|
|
44
|
+
const payload: Payload = {
|
|
44
45
|
pathname: data.pathname,
|
|
45
46
|
status,
|
|
46
47
|
statusText,
|
|
47
48
|
headers: Object.fromEntries(headers),
|
|
48
49
|
body,
|
|
49
|
-
}
|
|
50
|
+
};
|
|
50
51
|
|
|
51
|
-
socket.send(
|
|
52
|
+
socket.send(JSON.stringify(payload));
|
|
52
53
|
}
|
|
53
54
|
});
|
|
54
55
|
|
|
55
56
|
socket.addEventListener("open", (event) => {
|
|
56
|
-
if (!event.target.readyState) throw "
|
|
57
|
+
if (!event.target.readyState) throw "not ready";
|
|
57
58
|
});
|
|
58
59
|
|
|
59
60
|
socket.addEventListener("close", () => {
|
|
60
|
-
|
|
61
|
-
process.exit();
|
|
61
|
+
throw "server closed connection";
|
|
62
62
|
});
|
|
63
63
|
}
|
|
64
64
|
|
|
@@ -70,44 +70,18 @@ async function main({
|
|
|
70
70
|
const { values } = parseArgs({
|
|
71
71
|
args: process.argv,
|
|
72
72
|
options: {
|
|
73
|
-
port: {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
},
|
|
78
|
-
domain: {
|
|
79
|
-
type: "string",
|
|
80
|
-
default: "localhost:1234",
|
|
81
|
-
short: "d",
|
|
82
|
-
},
|
|
83
|
-
subdomain: {
|
|
84
|
-
type: "string",
|
|
85
|
-
short: "s",
|
|
86
|
-
},
|
|
87
|
-
open: {
|
|
88
|
-
type: "boolean",
|
|
89
|
-
short: "o",
|
|
90
|
-
},
|
|
91
|
-
version: {
|
|
92
|
-
type: "boolean",
|
|
93
|
-
short: "v",
|
|
94
|
-
},
|
|
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" },
|
|
95
78
|
},
|
|
96
79
|
allowPositionals: true,
|
|
97
80
|
});
|
|
98
81
|
|
|
99
82
|
if (values.version) {
|
|
100
|
-
console.log(
|
|
83
|
+
console.log(process.env.npm_package_version);
|
|
101
84
|
process.exit();
|
|
102
85
|
}
|
|
103
86
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const { port, domain, subdomain, open } = values;
|
|
107
|
-
|
|
108
|
-
main({
|
|
109
|
-
url: `localhost:${port}`,
|
|
110
|
-
domain,
|
|
111
|
-
subdomain,
|
|
112
|
-
open,
|
|
113
|
-
});
|
|
87
|
+
main(values);
|
package/package.json
CHANGED
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
|
-
|
|
40
|
-
|
|
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(`
|
|
92
|
+
console.log(`websocket server up at ws://${domain}`);
|