Skip to content

Commit ff2c802

Browse files
authored
refactor(session-broker): split broker into reusable runtime-neutral packages (#205)
1 parent 4bdc211 commit ff2c802

50 files changed

Lines changed: 3051 additions & 390 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/benchmarks.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ on:
1111
- "LICENSE"
1212
workflow_dispatch:
1313

14+
env:
15+
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "1"
16+
1417
concurrency:
1518
group: benchmarks-${{ github.workflow }}-${{ github.ref }}
1619
cancel-in-progress: true

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ on:
1010
- "assets/**"
1111
- "LICENSE"
1212

13+
env:
14+
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "1"
15+
1316
concurrency:
1417
group: main-ci-${{ github.workflow }}-${{ github.ref }}
1518
cancel-in-progress: true

.github/workflows/pr-ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
- "assets/**"
99
- "LICENSE"
1010

11+
env:
12+
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "1"
13+
1114
concurrency:
1215
group: pr-ci-${{ github.workflow }}-${{ github.ref }}
1316
cancel-in-progress: true

.github/workflows/release-prebuilt-npm.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ on:
1717
tags:
1818
- "v*"
1919

20+
env:
21+
SKIP_INSTALL_SIMPLE_GIT_HOOKS: "1"
22+
2023
concurrency:
2124
group: release-prebuilt-${{ github.ref }}
2225
cancel-in-progress: false

bun.lock

Lines changed: 49 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"bin": {
2323
"hunk": "./bin/hunk.cjs"
2424
},
25+
"workspaces": [
26+
"packages/*"
27+
],
2528
"files": [
2629
"bin",
2730
"dist/npm",
@@ -55,7 +58,7 @@
5558
"lint": "oxlint . --deny-warnings",
5659
"lint:fix": "oxlint . --fix",
5760
"prepare": "simple-git-hooks",
58-
"test": "\"${npm_execpath:-bun}\" test ./src ./scripts ./test/cli ./test/session",
61+
"test": "\"${npm_execpath:-bun}\" test ./src ./packages ./scripts ./test/cli ./test/session",
5962
"test:integration": "\"${npm_execpath:-bun}\" test ./test/pty",
6063
"test:tty-smoke": "HUNK_RUN_TTY_SMOKE=1 \"${npm_execpath:-bun}\" test ./test/smoke",
6164
"check:pack": "bun run ./scripts/check-pack.ts",
@@ -76,10 +79,15 @@
7679
"zod": "^4.3.6"
7780
},
7881
"devDependencies": {
82+
"@hunk/session-broker": "workspace:*",
83+
"@hunk/session-broker-bun": "workspace:*",
84+
"@hunk/session-broker-core": "workspace:*",
85+
"@hunk/session-broker-node": "workspace:*",
7986
"@opentui/core": "^0.1.88",
8087
"@opentui/react": "^0.1.88",
8188
"@types/bun": "latest",
8289
"@types/react": "^19.2.14",
90+
"@types/ws": "^8.18.1",
8391
"lint-staged": "^16.4.0",
8492
"oxfmt": "^0.41.0",
8593
"oxlint": "^1.56.0",
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# @hunk/session-broker-bun
2+
3+
Bun HTTP and websocket adapter for `@hunk/session-broker`.
4+
5+
Use this package when you want to serve a runtime-neutral `SessionBrokerDaemon` through `Bun.serve(...)`.
6+
7+
## What it does
8+
9+
- binds a broker daemon to a Bun HTTP server
10+
- upgrades websocket requests on the daemon socket path
11+
- forwards websocket messages and close events into the daemon
12+
- exposes a `stopped` promise compatible with Hunk's daemon lifecycle
13+
- lets callers override or add custom HTTP routes before the generic broker routes
14+
15+
## Usage
16+
17+
```ts
18+
import { SessionBroker, createSessionBrokerDaemon } from "@hunk/session-broker";
19+
import { serveSessionBrokerDaemon } from "@hunk/session-broker-bun";
20+
21+
const broker = new SessionBroker({
22+
parseRegistration,
23+
parseSnapshot,
24+
});
25+
26+
const daemon = createSessionBrokerDaemon({
27+
broker,
28+
capabilities: { version: 1, name: "example-broker" },
29+
});
30+
31+
const server = serveSessionBrokerDaemon({
32+
daemon,
33+
hostname: "127.0.0.1",
34+
port: 47657,
35+
});
36+
```
37+
38+
## Custom routes
39+
40+
You can override or extend request handling with `handleRequest`.
41+
42+
```ts
43+
const server = serveSessionBrokerDaemon({
44+
daemon,
45+
hostname: "127.0.0.1",
46+
port: 47657,
47+
handleRequest: async (request) => {
48+
const url = new URL(request.url);
49+
if (url.pathname === "/health") {
50+
return Response.json({ ok: true, overridden: true });
51+
}
52+
53+
return undefined;
54+
},
55+
});
56+
```
57+
58+
Return `undefined` to fall through to the generic broker routes.
59+
60+
## License
61+
62+
MIT
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@hunk/session-broker-bun",
3+
"version": "0.0.0",
4+
"private": true,
5+
"description": "Bun HTTP and websocket adapter for @hunk/session-broker.",
6+
"license": "MIT",
7+
"files": [
8+
"src"
9+
],
10+
"type": "module",
11+
"sideEffects": false,
12+
"exports": {
13+
".": {
14+
"types": "./src/index.ts",
15+
"import": "./src/index.ts"
16+
}
17+
},
18+
"dependencies": {
19+
"@hunk/session-broker": "workspace:*"
20+
},
21+
"engines": {
22+
"bun": ">=1.0.0",
23+
"node": ">=18"
24+
}
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./serve";

0 commit comments

Comments
 (0)