@tinkered/bun 0.0.0-stage → 0.18.1

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.
@@ -0,0 +1,194 @@
1
+ import { Capsule as Capsule$1, Scope, transport } from "@tinkered/core";
2
+ import { HTMLBundle, Server, ServerWebSocket, WebSocketHandler } from "bun";
3
+ //#region src/ws.d.ts
4
+ type Codec = transport.Codec;
5
+ type Service = transport.Service;
6
+ type ServerConfig = {
7
+ path: string;
8
+ services: readonly Service[];
9
+ codec?: Codec;
10
+ origin?: string | null;
11
+ maxPayload?: number;
12
+ maxWatches?: number;
13
+ maxUploads?: number;
14
+ uploadDeadline?: number;
15
+ strict?: boolean;
16
+ };
17
+ type Msg = {
18
+ i: number;
19
+ a?: string;
20
+ t?: string;
21
+ m?: Record<string, string>;
22
+ w?: 1;
23
+ x?: 1;
24
+ };
25
+ /** What each socket holds: its upgrade request, scope, live watches, this tick's frame, steps. */
26
+ type Socket = {
27
+ req: Request;
28
+ scope: Scope.Handle;
29
+ gone: AbortController;
30
+ watches: Map<number, AbortController>;
31
+ out: unknown[];
32
+ emit: (ws: ServerWebSocket<Socket>, message: unknown) => void;
33
+ pusher: (ws: ServerWebSocket<Socket>, i: number) => (text: string) => void;
34
+ one: (ws: ServerWebSocket<Socket>, m: Msg) => Promise<unknown>;
35
+ run: (ws: ServerWebSocket<Socket>, m: Msg) => Promise<unknown>;
36
+ };
37
+ /** Makes one WebSocket server. */
38
+ export declare function createWs(): {
39
+ server: Omit<import("@tinkered/core").Capsule.Handle<{
40
+ readonly path: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
41
+ readonly origin: import("@tinkered/core").Tag.Defaulted<string | null>;
42
+ readonly codec: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.Codec>>;
43
+ readonly services: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<readonly transport.Service[]>>;
44
+ readonly strict: import("@tinkered/core").Tag.Defaulted<boolean>;
45
+ readonly maxPayload: import("@tinkered/core").Tag.Defaulted<number>;
46
+ readonly maxWatches: import("@tinkered/core").Tag.Defaulted<number>;
47
+ readonly maxUploads: import("@tinkered/core").Tag.Defaulted<number>;
48
+ readonly uploadDeadline: import("@tinkered/core").Tag.Defaulted<number>;
49
+ }, {}, {
50
+ upgrade: (req: Request, scope: Scope.Handle, bun: Server<Socket>) => Response | null | undefined;
51
+ post: (req: Request, scope: Scope.Handle) => Promise<Response | undefined>;
52
+ fetch: (req: Request, scope: Scope.Handle, bun: Server<never>) => Promise<Response | null | undefined>;
53
+ websocket: WebSocketHandler<Socket>;
54
+ maxBody: number;
55
+ }>, never> & {
56
+ (values: ServerConfig): ReturnType<import("@tinkered/core").Capsule.Handle<{
57
+ readonly path: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
58
+ readonly origin: import("@tinkered/core").Tag.Defaulted<string | null>;
59
+ readonly codec: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.Codec>>;
60
+ readonly services: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<readonly transport.Service[]>>;
61
+ readonly strict: import("@tinkered/core").Tag.Defaulted<boolean>;
62
+ readonly maxPayload: import("@tinkered/core").Tag.Defaulted<number>;
63
+ readonly maxWatches: import("@tinkered/core").Tag.Defaulted<number>;
64
+ readonly maxUploads: import("@tinkered/core").Tag.Defaulted<number>;
65
+ readonly uploadDeadline: import("@tinkered/core").Tag.Defaulted<number>;
66
+ }, {}, {
67
+ upgrade: (req: Request, scope: Scope.Handle, bun: Server<Socket>) => Response | null | undefined;
68
+ post: (req: Request, scope: Scope.Handle) => Promise<Response | undefined>;
69
+ fetch: (req: Request, scope: Scope.Handle, bun: Server<never>) => Promise<Response | null | undefined>;
70
+ websocket: WebSocketHandler<Socket>;
71
+ maxBody: number;
72
+ }>>;
73
+ };
74
+ };
75
+ /** The default WebSocket server. */
76
+ export declare const ws: {
77
+ server: Omit<import("@tinkered/core").Capsule.Handle<{
78
+ readonly path: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
79
+ readonly origin: import("@tinkered/core").Tag.Defaulted<string | null>;
80
+ readonly codec: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.Codec>>;
81
+ readonly services: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<readonly transport.Service[]>>;
82
+ readonly strict: import("@tinkered/core").Tag.Defaulted<boolean>;
83
+ readonly maxPayload: import("@tinkered/core").Tag.Defaulted<number>;
84
+ readonly maxWatches: import("@tinkered/core").Tag.Defaulted<number>;
85
+ readonly maxUploads: import("@tinkered/core").Tag.Defaulted<number>;
86
+ readonly uploadDeadline: import("@tinkered/core").Tag.Defaulted<number>;
87
+ }, {}, {
88
+ upgrade: (req: Request, scope: Scope.Handle, bun: Server<Socket>) => Response | null | undefined;
89
+ post: (req: Request, scope: Scope.Handle) => Promise<Response | undefined>;
90
+ fetch: (req: Request, scope: Scope.Handle, bun: Server<never>) => Promise<Response | null | undefined>;
91
+ websocket: WebSocketHandler<Socket>;
92
+ maxBody: number;
93
+ }>, never> & {
94
+ (values: ServerConfig): ReturnType<import("@tinkered/core").Capsule.Handle<{
95
+ readonly path: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<string>>;
96
+ readonly origin: import("@tinkered/core").Tag.Defaulted<string | null>;
97
+ readonly codec: import("@tinkered/core").Edge<"optional", import("@tinkered/core").Tag.Handle<transport.Codec>>;
98
+ readonly services: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<readonly transport.Service[]>>;
99
+ readonly strict: import("@tinkered/core").Tag.Defaulted<boolean>;
100
+ readonly maxPayload: import("@tinkered/core").Tag.Defaulted<number>;
101
+ readonly maxWatches: import("@tinkered/core").Tag.Defaulted<number>;
102
+ readonly maxUploads: import("@tinkered/core").Tag.Defaulted<number>;
103
+ readonly uploadDeadline: import("@tinkered/core").Tag.Defaulted<number>;
104
+ }, {}, {
105
+ upgrade: (req: Request, scope: Scope.Handle, bun: Server<Socket>) => Response | null | undefined;
106
+ post: (req: Request, scope: Scope.Handle) => Promise<Response | undefined>;
107
+ fetch: (req: Request, scope: Scope.Handle, bun: Server<never>) => Promise<Response | null | undefined>;
108
+ websocket: WebSocketHandler<Socket>;
109
+ maxBody: number;
110
+ }>>;
111
+ };
112
+ };
113
+ //#endregion
114
+ //#region src/serve.d.ts
115
+ /**
116
+ * What a handler capsule's `start` returns, in Bun's own `{ fetch, websocket }` shape. `fetch`
117
+ * answers a Response, null when it upgraded the request to a WebSocket (Bun owns it now), or
118
+ * undefined when the request is not its own: the next handler gets it.
119
+ */
120
+ type Handler = {
121
+ fetch: (request: Request, scope: Scope.Handle, server: Server<never>) => Response | null | undefined | Promise<Response | null | undefined>;
122
+ /** Bun's socket handler, typed by the handler that owns it. */
123
+ websocket?: WebSocketHandler<never> | object;
124
+ /** The largest request body this handler takes. */
125
+ maxBody?: number;
126
+ };
127
+ /**
128
+ * `Bun.serve` for the life of the scope (Effect's `BunHttpServer.layer`, Go's `http.Server`).
129
+ * It goes first in `capsules`, so its start wraps the others: it listens once every handler has
130
+ * started, and its close stops the port before the handlers close. Its config comes from the cmd
131
+ * (the seam): it reads no `process.env`. Signals and exit codes stay with the host, `run`.
132
+ * - `websocket`: the one handler that brings Bun's `websocket` (ws.server); asked first;
133
+ * - `handlers`: capsules whose start returns a {@link Handler}, asked in order;
134
+ * - `pages`: the client's routes, for every path no handler takes;
135
+ * - `paths`: more Bun route patterns for the handlers;
136
+ * - `stdout`: where it prints `tinker: listening on <url>`.
137
+ */
138
+ export declare const serve: Capsule$1.Handle<{
139
+ readonly port: import("@tinkered/core").Edge<"required", import("@tinkered/core").Tag.Handle<number>>;
140
+ readonly pages: import("@tinkered/core").Tag.Defaulted<Readonly<Record<string, HTMLBundle | Response>>>;
141
+ readonly websocket: import("@tinkered/core").Tag.Defaulted<Capsule$1.Binding<Handler, unknown> | null>;
142
+ readonly handlers: import("@tinkered/core").Tag.Defaulted<readonly Capsule$1.Binding<Handler, unknown>[]>;
143
+ readonly paths: import("@tinkered/core").Tag.Defaulted<readonly string[]>;
144
+ readonly development: import("@tinkered/core").Tag.Defaulted<boolean>;
145
+ readonly stdout: import("@tinkered/core").Tag.Defaulted<{
146
+ write(chunk: string): unknown;
147
+ } | null>;
148
+ }, {
149
+ readonly url: import("@tinkered/core").Data.Cell<URL | null>;
150
+ }, void>;
151
+ //#endregion
152
+ //#region src/run.d.ts
153
+ /**
154
+ * What the host gives a command: its inputs, and the stop. A command picks its own exit codes;
155
+ * a startup throw exits 78 for a config error, else 1, so a command just lets it throw.
156
+ */
157
+ type MainContext = {
158
+ env: Readonly<Record<string, string | undefined>>;
159
+ args: readonly string[];
160
+ /** The folder the process runs in; the host reads it, so commands read no process global. */
161
+ cwd: string;
162
+ signal: AbortSignal;
163
+ stdout: {
164
+ write(chunk: string): unknown;
165
+ };
166
+ stderr: {
167
+ write(chunk: string): unknown;
168
+ };
169
+ exit: (code: number, reason?: unknown) => void;
170
+ };
171
+ /** The root close results, app first. A failed root exits 1, else its first teardown error. */
172
+ type MainClosed = Promise<void | Scope.Result | readonly Scope.Result[]>;
173
+ /**
174
+ * A command: it builds its root and answers it, or anything with its `closed`. A server command
175
+ * puts `serve` in its root; the host opens no port of its own.
176
+ */
177
+ type Main = (ctx: MainContext) => MainStarted | Promise<MainStarted>;
178
+ /** What a command answers: its root, as `createScope` gives it, or its `ready` and `closed`. */
179
+ type MainStarted = {
180
+ readonly ready?: PromiseLike<unknown>;
181
+ readonly closed: MainClosed;
182
+ };
183
+ /**
184
+ * Runs one command, and owns OS signals and the exit code (Effect's `runMain`, Go's `main`):
185
+ * - a startup throw exits 78 for a config error (a missing tag or config key, a value that
186
+ * failed its schema, a bad URL), else 1;
187
+ * - a failed root exits 1; a signal exits 128 + its number;
188
+ * - SIGHUP (a closed terminal) stops it like SIGTERM, so `nohup` does not keep it alive;
189
+ * - a stop during start ends with the stop's own code, and no second line.
190
+ * A command that calls `exit` during start gets that code. The host waits for its root to close.
191
+ */
192
+ export declare function run(main: Main): Promise<never>;
193
+ //#endregion
194
+ export type { Handler, Main, MainClosed, MainContext, MainStarted };