@barqjs/start 0.0.0-stage → 0.2.0-next.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.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +817 -2
  3. package/dist/client-BsyxVCyu.js +1554 -0
  4. package/dist/client-BsyxVCyu.js.map +1 -0
  5. package/dist/client-DidH0F2e.d.ts +450 -0
  6. package/dist/client-DidH0F2e.d.ts.map +1 -0
  7. package/dist/client.d.ts +25 -0
  8. package/dist/client.js +4 -0
  9. package/dist/env-Bhk_aIkx.d.ts +47 -0
  10. package/dist/env-Bhk_aIkx.d.ts.map +1 -0
  11. package/dist/env.d.ts +2 -0
  12. package/dist/env.js +43 -0
  13. package/dist/env.js.map +1 -0
  14. package/dist/index-B4o7eyCr.d.ts +925 -0
  15. package/dist/index-B4o7eyCr.d.ts.map +1 -0
  16. package/dist/index.d.ts +18 -0
  17. package/dist/index.js +6 -0
  18. package/dist/middleware-B-tI8QHQ.d.ts +574 -0
  19. package/dist/middleware-B-tI8QHQ.d.ts.map +1 -0
  20. package/dist/middleware.d.ts +2 -0
  21. package/dist/middleware.js +281 -0
  22. package/dist/middleware.js.map +1 -0
  23. package/dist/prerender.d.ts +118 -0
  24. package/dist/prerender.d.ts.map +1 -0
  25. package/dist/prerender.js +289 -0
  26. package/dist/prerender.js.map +1 -0
  27. package/dist/protocol.d.ts +56 -0
  28. package/dist/protocol.d.ts.map +1 -0
  29. package/dist/protocol.js +25 -0
  30. package/dist/protocol.js.map +1 -0
  31. package/dist/serve.d.ts +144 -0
  32. package/dist/serve.d.ts.map +1 -0
  33. package/dist/serve.js +221 -0
  34. package/dist/serve.js.map +1 -0
  35. package/dist/server-DG6jb_NA.js +1253 -0
  36. package/dist/server-DG6jb_NA.js.map +1 -0
  37. package/dist/server.d.ts +159 -0
  38. package/dist/server.d.ts.map +1 -0
  39. package/dist/server.js +1 -0
  40. package/dist/src-odXG7kLA.js +1151 -0
  41. package/dist/src-odXG7kLA.js.map +1 -0
  42. package/dist/static-fn-CXjJi-It.js +122 -0
  43. package/dist/static-fn-CXjJi-It.js.map +1 -0
  44. package/package.json +106 -4
  45. package/src/client.ts +698 -0
  46. package/src/context.ts +699 -0
  47. package/src/cookies.ts +229 -0
  48. package/src/env.ts +86 -0
  49. package/src/fn-error.ts +121 -0
  50. package/src/frames-client.ts +1220 -0
  51. package/src/frames-server.ts +464 -0
  52. package/src/frames.ts +285 -0
  53. package/src/index.ts +740 -0
  54. package/src/live-client.ts +50 -0
  55. package/src/live-server.ts +292 -0
  56. package/src/middleware.ts +994 -0
  57. package/src/prerender.ts +477 -0
  58. package/src/protocol.ts +61 -0
  59. package/src/rate-limit.ts +202 -0
  60. package/src/serve.ts +153 -0
  61. package/src/server.ts +1026 -0
  62. package/src/session.ts +449 -0
  63. package/src/start-instance.ts +147 -0
  64. package/src/static-fn.ts +181 -0
  65. package/src/static.ts +302 -0
  66. package/src/stubs.ts +32 -0
  67. package/src/timing.ts +29 -0
  68. package/src/wire-types.ts +138 -0
@@ -0,0 +1,450 @@
1
+ import { G as WithClient, K as WithServer, S as NOT_FOUND, f as FreshMiddleware, g as Middleware, k as REDIRECT, t as BuiltMiddleware } from "./middleware-B-tI8QHQ.js";
2
+ import { AnySerializationAdapter } from "@barqjs/server/codec";
3
+ import { Child, Incoming, JSXElement } from "@barqjs/core";
4
+ import "@barqjs/web";
5
+ /**
6
+ * How a server function's failure reaches its caller in the browser. Its own
7
+ * module, so every module that answers a call can raise it and none of them
8
+ * has to import the stub to do so.
9
+ */
10
+ /** What a handler threw, as a development server tells its caller. */
11
+ interface ServerFnThrown {
12
+ readonly name: string;
13
+ readonly message: string;
14
+ /** Mapped to the source files by the development server. */
15
+ readonly stack?: string;
16
+ }
17
+ /** One thing wrong with a call's input, and the path to the field it is about. */
18
+ interface ServerFnIssue {
19
+ readonly message: string;
20
+ readonly path?: readonly (string | number)[];
21
+ }
22
+ /**
23
+ * The label on an answer from a deployment that does not know the call: an
24
+ * unknown id (a tab holding the previous build's ids across a deploy, or a
25
+ * removed function), or a call from another build. A CDN 404 and a skew 404
26
+ * look alike without it.
27
+ */
28
+ declare const UNKNOWN_HEADER = "x-barq-fn-unknown";
29
+ /** What a failed call's JSON body may carry. */
30
+ interface FailedAnswer {
31
+ readonly message?: string;
32
+ readonly issues?: readonly ServerFnIssue[];
33
+ readonly thrown?: ServerFnThrown;
34
+ }
35
+ /**
36
+ * A server function answered with a failure.
37
+ *
38
+ * THE MESSAGE IS NOT THE HANDLER'S, and that is deliberate rather than
39
+ * unfinished. `server.ts` refuses to put a handler's error text on the wire
40
+ * because it can name a table, a column or a filesystem path, and none of that
41
+ * is the caller's business — TanStack serialises the whole error object to the
42
+ * client instead. What a caller legitimately needs is the STATUS, so it is here
43
+ * as a field rather than something to parse back out of a string.
44
+ */
45
+ declare class ServerFnError extends Error {
46
+ readonly status: number;
47
+ /**
48
+ * What was wrong with the input, for a 400 the validator refused: one entry
49
+ * per issue, with the path to the field it is about. Sent in every build,
50
+ * because it describes the caller's own input.
51
+ */
52
+ readonly issues?: readonly ServerFnIssue[];
53
+ /**
54
+ * What the handler threw: its name, message and source-mapped stack. Only a
55
+ * development server sends it; in production this is `undefined`, so handle
56
+ * a failure by its `status` rather than by this.
57
+ */
58
+ readonly thrown?: ServerFnThrown;
59
+ /**
60
+ * The answer itself, unread.
61
+ *
62
+ * The server's own refusals land here: a 400 for input, a 403 for an origin,
63
+ * a 404, a 405, a 413. A `Response` a middleware or a handler THREW is not
64
+ * wrapped at all; the call rejects with it. A handler's own failure has
65
+ * nothing here worth reading: the server answers those with one fixed line,
66
+ * for the reason above.
67
+ */
68
+ readonly response?: Response;
69
+ /**
70
+ * The deployment that answered does not know this call: the page came from
71
+ * another build, or the function was removed. Retrying cannot help; reloading
72
+ * the document onto the current build can.
73
+ */
74
+ readonly unknownFunction?: true;
75
+ constructor(id: string, status: number, response?: Response, answer?: FailedAnswer);
76
+ }
77
+ /** Where a static answer is served from, under the client output. */
78
+ declare const STATIC_PREFIX = "/_barq/static/";
79
+ /** Where the answer for one call is, as a URL. */
80
+ declare function staticUrl(id: string, input: unknown): string;
81
+ /**
82
+ * Make a server function static: answered at prerender time, read from a file
83
+ * afterwards.
84
+ *
85
+ * List it LAST: the key is the input as this middleware sees it, which is the
86
+ * input the caller passed only when nothing before it has validated or
87
+ * reshaped it. An input no prerendered page called with has no file, and the
88
+ * call goes to the server as any other would.
89
+ *
90
+ * Only the RESULT is stored. What a server middleware sends back with
91
+ * `sendContext` is not, and a `Response` or a stream is never written.
92
+ */
93
+ declare const staticFunctionMiddleware: BuiltMiddleware<WithServer<WithClient<FreshMiddleware, Promise<{} | null>>, Promise<unknown>>>;
94
+ /**
95
+ * The stubs a page has made, and how `live` calls one: a module of its own so
96
+ * the frames client finds a stub by id without importing the stub's module.
97
+ */
98
+ /** What the registry holds: a callable stub that knows its id. */
99
+ type Stub = ((options?: unknown) => Promise<unknown>) & {
100
+ readonly meta: {
101
+ readonly id: string;
102
+ };
103
+ };
104
+ /** A call at the live address: where the last connection left off, and how to end this one. */
105
+ interface LiveRequest {
106
+ readonly lastEventId: string | null;
107
+ readonly signal: AbortSignal;
108
+ }
109
+ /** How `live` calls a stub at the live address, its client middleware around the fetch. */
110
+ declare const LIVE_CALL: unique symbol;
111
+ declare function clientStub(id: string): Stub | undefined;
112
+ /**
113
+ * A slot on a server component's props: a call the browser answers with its
114
+ * own content. What the call is given crosses as data, and its `children` as a
115
+ * region of server markup. `$key` names the occurrence, so its content follows
116
+ * it across updates rather than its position.
117
+ */
118
+ type Slot<P extends object = {}> = (props: Incoming<P & {
119
+ readonly $key?: string | number;
120
+ readonly children?: Child;
121
+ }>) => JSXElement;
122
+ /**
123
+ * A slot the server component reads as data: called once per data context,
124
+ * `const like = props.like({ cid })`, and each property placed whole at a
125
+ * position of the server's own markup, an attribute, a class name or style
126
+ * property in object form, a handler, a ref or a text child. The page's fill
127
+ * takes the call's arguments and returns the object those positions read; the
128
+ * server never has its values to compute with.
129
+ */
130
+ type BindingSlot<A extends object = {}, B extends object = Record<string, unknown>> = keyof A extends never ? (args?: {
131
+ readonly $key?: string | number;
132
+ }) => Readonly<B> : (args: A & {
133
+ readonly $key?: string | number;
134
+ }) => Readonly<B>;
135
+ /** The brand on a frame, on either side, carrying where it lives. */
136
+ declare const FRAME: unique symbol;
137
+ /** How a frame is named across the wire: its server function and its address. */
138
+ interface FrameRef {
139
+ readonly id: string;
140
+ readonly address: string;
141
+ }
142
+ /** Whether a value is a frame: a server function's component result, on either side. */
143
+ declare function isFrame(value: unknown): boolean;
144
+ /** Dispatched on `document` once a frame update has been applied to the page. */
145
+ declare const FRAME_APPLIED_EVENT = "barq:frame-applied";
146
+ /** What a frame renders as: a component taking the slots' content as props. */
147
+ type FrameComponent = ((props: Record<string, unknown>) => Node) & {
148
+ readonly [FRAME]: FrameRef;
149
+ };
150
+ /**
151
+ * How a frame named in the page's seed becomes the component for the region
152
+ * the page already carries. The client entry installs it beside the
153
+ * application's own adapters, before the seed is read.
154
+ */
155
+ declare const frameAdapter: AnySerializationAdapter;
156
+ /**
157
+ * Read a frame answer and apply it: the component for its address, the same
158
+ * one every time, with every place it is mounted morphed to the new markup
159
+ * once the region's stylesheets have loaded.
160
+ */
161
+ declare function applyFrameResponse(response: Response): Promise<FrameComponent>;
162
+ /** What the loop says about its connection. */
163
+ type LiveStatus = "connected" | "reconnecting" | "closed";
164
+ interface LiveIterable<T> extends AsyncIterable<T> {
165
+ /** Told `"connected"` per connection, `"reconnecting"` with the error per death, `"closed"` at the end. */
166
+ onstatus?: (status: LiveStatus, error?: unknown) => void;
167
+ }
168
+ /**
169
+ * Declare a server function's answer a standing one. In the browser, calling
170
+ * the result returns an iterable that stays connected; on the server it is the
171
+ * call itself, a frame it answers with marked for the page to connect.
172
+ */
173
+ declare function live<In, Out>(fn: ServerFn<In, Out, never>): (...args: Parameters<ServerFn<In, Out, never>>) => LiveIterable<Awaited<Out>>;
174
+ declare const SERVER_FN: unique symbol;
175
+ /** Where mounted server functions answer. */
176
+ declare const RPC_PREFIX = "/_barq/fn/";
177
+ /**
178
+ * What distinguishes an RPC call from a form submission: the URL, not a header.
179
+ *
180
+ * `/_barq/fn/<id>` is what `<form action={fn}>` writes, takes `FormData`, and
181
+ * answers with a redirect a browser can follow with JS disabled.
182
+ * `/_barq/fn/<id>.data` is the JSON channel and answers with a value.
183
+ *
184
+ * React Router's `.data` suffix, and the reason is its reason: a header decides
185
+ * the response shape invisibly, and a form cannot set one. RedwoodSDK shows the
186
+ * failure — it emits the right hidden fields for a no-JS submit and never reads
187
+ * them, so the form posts, returns 200, and nothing happens.
188
+ */
189
+ declare const DATA_SUFFIX = ".data";
190
+ /**
191
+ * `/_barq/fn/<id>.live` is the live address: the data address's answer, framed
192
+ * as server-sent events for as long as it stands. Its own URL, because a cache
193
+ * keys on the URL and not on a header nothing tells it to vary by.
194
+ */
195
+ declare const LIVE_SUFFIX = ".live";
196
+ /**
197
+ * How the data channel says "the answer is an instruction, not a value".
198
+ *
199
+ * A HEADER rather than a body sniff, so a caller that does not care never
200
+ * parses the body to find out, and so a handler returning a plain object that
201
+ * happens to have a `to` field is never mistaken for a redirect.
202
+ */
203
+ declare const RPC_CONTROL = "x-barq-rpc";
204
+ /**
205
+ * The {@link RPC_CONTROL} value for a `Response` the handler returned.
206
+ *
207
+ * The response IS the result then, and its body is whatever the handler wrote,
208
+ * so the stub hands it back whole rather than reading it as a wire value. No
209
+ * body rides under this one, which is why it is not an {@link RpcControl} kind.
210
+ */
211
+ declare const RPC_RESPONSE = "response";
212
+ /**
213
+ * The {@link RPC_CONTROL} value for a `Response` a handler, a validator or a
214
+ * middleware THREW: the call rejects with that response itself, as TanStack's
215
+ * does. Its body and status are what the author wrote for the caller.
216
+ */
217
+ declare const RPC_THROWN = "thrown";
218
+ /**
219
+ * The {@link RPC_CONTROL} value for an answer that arrives over time.
220
+ *
221
+ * The body is then one JSON line for each value the function yielded, each
222
+ * holding a wire-encoded value, and the stub hands the caller an async iterable
223
+ * of them. A line that says the function failed ends it with a
224
+ * {@link ServerFnError}.
225
+ */
226
+ declare const RPC_STREAM = "stream";
227
+ /** One line of a streamed answer: a value, the chain's `sendContext`, or a failure. */
228
+ type RpcStreamLine = {
229
+ readonly v: unknown;
230
+ } | {
231
+ readonly c: unknown;
232
+ } | {
233
+ readonly e: 1;
234
+ readonly t?: ServerFnThrown;
235
+ };
236
+ /** The body of a 400 for refused input. */
237
+ interface ServerFnInputAnswer {
238
+ readonly error: "invalid input";
239
+ readonly message: string;
240
+ readonly issues: readonly ServerFnIssue[];
241
+ }
242
+ /**
243
+ * What a caller is handed for a handler's result.
244
+ *
245
+ * A function that returns a `ReadableStream` or an async iterable answers over
246
+ * time, and both arrive as an async iterable of the values, in the browser and
247
+ * in process alike.
248
+ */
249
+ type Streamed<Out> = Out extends ReadableStream<infer Value> ? AsyncIterableIterator<Value> : Out extends AsyncIterable<infer Value> ? AsyncIterableIterator<Value> : Out;
250
+ /**
251
+ * Present when a middleware's `sendContext` rides back with the result.
252
+ *
253
+ * A HEADER flagging a different body shape, which is what `RPC_CONTROL` already
254
+ * does and for the same reason: an ordinary call — every call, in almost every
255
+ * application — must not pay a second parse or a wrapper object to carry
256
+ * something it does not have. With the flag the body is `{ result, context }`;
257
+ * without it the body IS the result, byte for byte as before.
258
+ */
259
+ declare const RPC_CONTEXT = "x-barq-ctx";
260
+ /** The body shape {@link RPC_CONTEXT} announces. */
261
+ interface RpcEnvelope {
262
+ readonly result: unknown;
263
+ readonly context: unknown;
264
+ }
265
+ /**
266
+ * What rides in the body under {@link RPC_CONTROL}.
267
+ *
268
+ * `options` and `data` are wire-encoded, so a `Date` in a redirect's search or
269
+ * in a not-found's data arrives as one. Each is absent when the thrown value
270
+ * carried none, which leaves the body the two fields it otherwise is.
271
+ */
272
+ type RpcControl = {
273
+ readonly kind: "redirect";
274
+ readonly to: string;
275
+ readonly status: number;
276
+ readonly options?: unknown;
277
+ } | {
278
+ readonly kind: "not-found";
279
+ readonly message: string;
280
+ readonly data?: unknown;
281
+ readonly routeId?: string;
282
+ };
283
+ /**
284
+ * A redirect that arrived over the wire, rebuilt on the client.
285
+ *
286
+ * NOT `@barqjs/router`'s `Redirect` — this package cannot construct that one.
287
+ * It carries the same brand, so `isRedirect` from the router accepts it and the
288
+ * router navigates on it exactly as it would on a redirect a loader threw. That
289
+ * interchangeability is the whole reason those predicates are brand checks
290
+ * rather than `instanceof`.
291
+ */
292
+ declare class RpcRedirect extends Error {
293
+ readonly [REDIRECT]: true;
294
+ readonly to: string;
295
+ readonly status: number;
296
+ readonly statusCode: number;
297
+ /**
298
+ * The navigation an options-form redirect described: `to`, `params`,
299
+ * `search`, `hash`, `replace`, `reloadDocument`, `href`. The router goes
300
+ * there in place of `to` when it is present.
301
+ */
302
+ readonly options?: Record<string, unknown>;
303
+ constructor(to: string, status: number, options?: Record<string, unknown>);
304
+ }
305
+ /** The `notFound()` counterpart, branded for the same reason. */
306
+ declare class RpcNotFound extends Error {
307
+ readonly [NOT_FOUND]: true;
308
+ readonly isNotFound: true;
309
+ /** What `notFound({ data })` carried, for the `notFoundComponent` that renders this. */
310
+ readonly data?: unknown;
311
+ /** The route whose `notFoundComponent` answers, when the thrower named one. */
312
+ readonly routeId?: string;
313
+ constructor(message: string, data?: unknown, routeId?: string);
314
+ }
315
+ /**
316
+ * The HTTP methods a server function answers. `GET` is the default, for a read
317
+ * the browser and a CDN may cache; see {@link RPC_CALLER} for what keeps a link
318
+ * from reaching one. `POST` is for a write and is the only one a form can reach.
319
+ */
320
+ type ServerFnMethod = "GET" | "POST";
321
+ interface ServerFnMeta {
322
+ /** Stable across edits and deploys: the module path and the export name. */
323
+ id: string;
324
+ /** Absent for `POST`, which is every function that did not ask for `GET`. */
325
+ readonly method?: ServerFnMethod;
326
+ /** Declared `createServerFn({ broadcast: true })`; the server half alone knows it. */
327
+ readonly broadcast?: true;
328
+ }
329
+ /**
330
+ * The request header every GET call the stub makes carries, and is refused
331
+ * without.
332
+ *
333
+ * A link, an `<img>`, a prefetch and a navigation cannot set a header; only
334
+ * script can, and a cross-origin script that sets one is preflighted and
335
+ * refused by the origin check. So a `GET` server function stays out of reach
336
+ * of everything that is not this application's own code, which is the reason
337
+ * `GET` was once refused outright: a mutation reachable by navigation is a
338
+ * one-click CSRF carrying `SameSite=Lax` cookies.
339
+ */
340
+ declare const RPC_CALLER = "x-barq-fn";
341
+ /**
342
+ * The build a call was bundled with. A server stamped with another build
343
+ * answers it with a 409 rather than running a function whose arguments or
344
+ * result may have changed shape under the same id. A GET read does not send
345
+ * it: its url is its identity to every cache in between.
346
+ */
347
+ declare const BUILD_HEADER = "x-barq-build";
348
+ /** The build `@barqjs/vite` stamped this bundle with, or none outside a build. */
349
+ declare const BUNDLED_BUILD: string | undefined;
350
+ /** Where a GET call's payload rides, since a GET has no body. */
351
+ declare const PAYLOAD_PARAM = "payload";
352
+ /**
353
+ * How a server function is CALLED: `fn({ data })`, and `fn()` when it takes
354
+ * none.
355
+ *
356
+ * TanStack's convention, and worth matching for a reason beyond fidelity: a
357
+ * named field leaves room for the options that
358
+ * come after it without ever changing the call shape again, and it removes the
359
+ * `adminStats(undefined)` the bare form forced on every no-argument call.
360
+ */
361
+ /**
362
+ * What a call may carry beside its `data`. TanStack's three, under their names.
363
+ */
364
+ interface ServerFnCallOptions {
365
+ /**
366
+ * Aborts the call. In the browser the fetch is cancelled, and the server sees
367
+ * its request go away, which is what the handler's own `signal` follows.
368
+ */
369
+ readonly signal?: AbortSignal;
370
+ /**
371
+ * Sent with the request. A name set here wins over the same name from a
372
+ * client middleware's `next({ headers })`, since the call is the more
373
+ * specific of the two.
374
+ */
375
+ readonly headers?: HeadersInit;
376
+ /** Used in place of the global `fetch`, for this call alone. */
377
+ readonly fetch?: typeof fetch;
378
+ }
379
+ type ServerFnArgs<In, Method extends ServerFnMethod = "POST"> = [undefined] extends [In] ? [options?: ({
380
+ readonly data?: In;
381
+ } & ServerFnCallOptions) | FormFor<Method>] : [options: ({
382
+ readonly data: In;
383
+ } & ServerFnCallOptions) | FormFor<Method>];
384
+ /** A form is a body, so only a `POST` function takes one. */
385
+ type FormFor<Method extends ServerFnMethod> = Method extends "POST" ? FormData : never;
386
+ /**
387
+ * A server function, from either side.
388
+ *
389
+ * A bare `FormData` is accepted BESIDE the options object, and only there. It is
390
+ * what `<form action={fn}>` hands the function on the enhanced path, and a
391
+ * `FormData` can never be mistaken for an options object — so the no-JS
392
+ * submission and the JS one reach the handler identically, which is the whole
393
+ * point of that path. TanStack has no counterpart to match here.
394
+ */
395
+ type ServerFn<In, Out, Method extends ServerFnMethod = "POST"> = ((...args: ServerFnArgs<In, Method>) => Promise<Out>) & {
396
+ readonly [SERVER_FN]: true;
397
+ readonly meta: ServerFnMeta;
398
+ /** TanStack's name for `meta`: the id this function is mounted at. */
399
+ readonly serverFnMeta: ServerFnMeta;
400
+ /** The method this function answers. */
401
+ readonly method: ServerFnMethod;
402
+ /**
403
+ * Where a form posts to reach this function: what `<form action={fn}>`
404
+ * writes. Empty for a function that is not mounted, which has no endpoint.
405
+ */
406
+ readonly url: string;
407
+ };
408
+ /**
409
+ * Any server function, whatever it takes and returns.
410
+ *
411
+ * `never` in, because the input sits in a parameter: a function that takes a
412
+ * string does not accept `unknown`, and every function accepts `never`. The
413
+ * method is `never` for the same reason: it decides whether a form is a
414
+ * parameter.
415
+ */
416
+ type AnyServerFn = ServerFn<never, unknown, never>;
417
+ /** The form endpoint of a mounted id, or nothing for a function with none. */
418
+ declare function urlOf(id: string): string;
419
+ /** The `data` out of a call, whichever of the two shapes it arrived in. */
420
+ declare function dataOf(options: unknown): unknown;
421
+ /** What a call asked for beside its `data`. A bare `FormData` asks for nothing. */
422
+ declare function callOptionsOf(options: unknown): ServerFnCallOptions | undefined;
423
+ /**
424
+ * The client half. The compiler emits this in a module compiled for the client,
425
+ * with the id, the chain, and nothing else — no handler body, no validator, no
426
+ * imports the body needed.
427
+ *
428
+ * `chain` is the middlewares the function declared, and it is here so a
429
+ * `.client()` half can run around the fetch. Only the CLIENT halves survive the
430
+ * compile: `createMiddleware(...).server(fn)` has its `server` argument deleted
431
+ * in the client build, so a middleware may hold both without the server body —
432
+ * or the database import it needed — reaching the browser.
433
+ */
434
+ declare function clientRpc<In, Out, Method extends ServerFnMethod = "POST">(id: string, declaredChain?: readonly ChainStep[], declared?: {
435
+ readonly method?: Method;
436
+ }): ServerFn<In, Out, Method>;
437
+ /** Where a server function keeps the middleware it declared, for another to inline. */
438
+ declare const SERVER_FN_CHAIN: unique symbol;
439
+ /** What `.middleware([...])` takes: a middleware, or a server function whose chain it inlines. */
440
+ type ChainStep = Middleware | AnyServerFn;
441
+ /**
442
+ * A chain with each server function in it replaced by the middleware that
443
+ * function declared, which is TanStack's rule: listing a function lists its
444
+ * guards, never its handler.
445
+ */
446
+ declare function inlineServerFns(chain: readonly ChainStep[]): readonly Middleware[];
447
+ /** Whether a value is a server function, by brand rather than by shape. */
448
+ declare function isServerFn(value: unknown): value is ServerFn<unknown, unknown>;
449
+ export { ServerFnThrown as $, clientRpc as A, frameAdapter as B, ServerFnArgs as C, ServerFnMethod as D, ServerFnMeta as E, live as F, LIVE_CALL as G, FRAME_APPLIED_EVENT as H, FrameComponent as I, STATIC_PREFIX as J, LiveRequest as K, LiveIterable as L, inlineServerFns as M, isServerFn as N, Streamed as O, urlOf as P, ServerFnIssue as Q, LiveStatus as R, ServerFn as S, ServerFnInputAnswer as T, Slot as U, BindingSlot as V, isFrame as W, staticUrl as X, staticFunctionMiddleware as Y, ServerFnError as Z, RpcNotFound as _, DATA_SUFFIX as a, SERVER_FN as b, RPC_CALLER as c, RPC_PREFIX as d, UNKNOWN_HEADER as et, RPC_RESPONSE as f, RpcEnvelope as g, RpcControl as h, ChainStep as i, dataOf as j, callOptionsOf as k, RPC_CONTEXT as l, RPC_THROWN as m, BUILD_HEADER as n, LIVE_SUFFIX as o, RPC_STREAM as p, clientStub as q, BUNDLED_BUILD as r, PAYLOAD_PARAM as s, AnyServerFn as t, RPC_CONTROL as u, RpcRedirect as v, ServerFnCallOptions as w, SERVER_FN_CHAIN as x, RpcStreamLine as y, applyFrameResponse as z };
450
+ //# sourceMappingURL=client-DidH0F2e.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-DidH0F2e.d.ts","names":[],"sources":["../src/fn-error.ts","../src/static-fn.ts","../src/stubs.ts","../src/frames.ts","../src/frames-client.ts","../src/live-client.ts","../src/client.ts"],"mappings":";;;;;;;;;;UAOiB;WACN;WACA;;WAEA;;;UAIM;WACN;WACA;;;;;;;;cASE;;UAGI;WACN;WACA,kBAAkB;WAClB,SAAS;;;;;;;;;;;;cAaP,sBAAsB;WACxB;;;;;;WAMA,kBAAkB;;;;;;WAMlB,SAAS;;;;;;;;;;WAUT,WAAW;;;;;;WAMX;EACT,YAAY,YAAY,gBAAgB,WAAW,UAAU,SAAS;;;cChD3D;;iBA8EG,UAAU,YAAY;;;;;;;;;;;;;cA+CzB,0BAA0B,gBACrC,WAAW,WAAW,iBAAiB,qBAAqB;;;;;;KCnJlD,SAAS,sBAAsB;WAChC;aAAiB;;;;UAIX;WACN;WACA,QAAQ;;;cAIN;iBAYG,WAAW,aAAa;;;;;;;KCO5B,KAAK,0BACf,OAAO,SAAS;WAAe;WAAiC,WAAW;OACxE;;;;;;;;;KAUO,YACV,uBACA,mBAAmB,iCACX,mBACL;WAAkB;MAA6B,SAAS,MACxD,MAAM;WAAe;MAA6B,SAAS;;cAGnD;;UAGI;WACN;WACA;;;iBAIK,QAAQ;;cA0DX;;KCrDD,mBAAmB,OAAO,4BAA4B;YACtD,QAAQ;;;;;;;cA8GP,cAAc;;;;;;iBAixBL,mBAAmB,UAAU,WAAW,QAAQ;;KA6F1D;UAEK,aAAa,WAAW,cAAc;;EAErD,YAAY,QAAQ,YAAY;;;;;;;iBCrgClB,KAAK,IAAI,KACvB,IAAI,SAAS,IAAI,kBACZ,MAAM,WAAW,SAAS,IAAI,iBAAiB,aAAa,QAAQ;cCmB9D;;cAGA;;;;;;;;;;;;;cAcA;;;;;;cAOA;;;;;;;;cASA;;;;;;;;cASA;;;;;;cAOA;;;;;;;;;cAUA;;KAGD;WACG;;WACA;;WACA;WAAe,IAAI;;;UAGjB;WACN;WACA;WACA,iBAAiB;;;;;;;;;KAUhB,SAAS,OACnB,YAAY,qBAAqB,SAC7B,sBAAsB,SACtB,YAAY,oBAAoB,SAC9B,sBAAsB,SACtB;;;;;;;;;;cAWK;;UAGI;WACN;WACA;;;;;;;;;KAUC;WAEG;WACA;WACA;WACA;;WAGA;WACA;WACA;WACA;;;;;;;;;;;cAYF,oBAAoB;YACrB;WACD;WACA;WACA;;;;;;WAMA,UAAU;EACnB,YAAY,YAAY,gBAAgB,UAAU;;;cAWvC,oBAAoB;YACrB;WACD;;WAEA;;WAEA;EACT,YAAY,iBAAiB,gBAAgB;;;;;;;KAuBnC;UAEK;;EAEf;;WAES,SAAS;;WAET;;;;;;;;;;;;;cAcE;;;;;;;cAQA;;cAKA;;cAIA;;;;;;;;;;;;;UAcI;;;;;WAKN,SAAS;;;;;;WAMT,UAAU;;WAEV,eAAe;;KAGd,aAAa,IAAI,eAAe,gDAAgD,OAWvF;WAAsB,OAAO;IAAO,uBAAuB,QAAQ,YACnE;WAAqB,MAAM;IAAO,uBAAuB,QAAQ;;KAGjE,QAAQ,eAAe,kBAAkB,wBAAwB;;;;;;;;;;KAW1D,SAAS,IAAI,KAAK,eAAe,gCACxC,MAAM,aAAa,IAAI,YACvB,QAAQ;YACD;WACD,MAAM;;WAEN,cAAc;;WAEd,QAAQ;;;;;WAKR;;;;;;;;;;KAWC,cAAc;;iBAGV,MAAM;;iBAKN,OAAO;;iBAOP,cAAc,mBAAmB;;;;;;;;;;;;iBAgBjC,UAAU,IAAI,KAAK,eAAe,yBAChD,YACA,yBAAwB,aACxB;WAAsB,SAAS;IAC9B,SAAS,IAAI,KAAK;;cAkRR;;KAGD,YAAY,aAAa;;;;;;iBAOrB,gBAAgB,gBAAgB,uBAAuB;;iBAavD,WAAW,iBAAiB,SAAS"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * The client half of a server function, and NOTHING the server half needs.
3
+ *
4
+ * Its own module because of what importing it costs. The compiler replaces a
5
+ * server-function module's contents with `export const x = clientRpc("<id>")`,
6
+ * and that stub used to import from `@barqjs/start` — whose index re-exports
7
+ * `context.ts`, which is `node:async_hooks`. Every client bundle that reached
8
+ * one server function therefore pulled the request-context machinery, the
9
+ * middleware runner, the validators and the error classes, and Vite printed
10
+ * "node:async_hooks has been externalized for browser compatibility" on every
11
+ * build.
12
+ *
13
+ * That was survivable while a route module was `lazy()`, because the cost sat
14
+ * in that route's own chunk. It stopped being survivable when `routeTree.gen.ts`
15
+ * started importing route modules STATICALLY: measured on
16
+ * `packages/kitchen-sink`, 25.7 kB of it moved into the set every page
17
+ * preloads, for an application with two server functions on one route.
18
+ *
19
+ * So the stub imports from here. `index.ts` re-exports every name below, so an
20
+ * application still writes `import { createServerFn } from "@barqjs/start"` and
21
+ * nothing about the authoring surface changes.
22
+ */
23
+ import { $ as ServerFnThrown, A as clientRpc, B as frameAdapter, C as ServerFnArgs, D as ServerFnMethod, E as ServerFnMeta, F as live, G as LIVE_CALL, H as FRAME_APPLIED_EVENT, I as FrameComponent, J as STATIC_PREFIX, K as LiveRequest, L as LiveIterable, M as inlineServerFns, N as isServerFn, O as Streamed, P as urlOf, Q as ServerFnIssue, R as LiveStatus, S as ServerFn, T as ServerFnInputAnswer, U as Slot, V as BindingSlot, W as isFrame, X as staticUrl, Y as staticFunctionMiddleware, Z as ServerFnError, _ as RpcNotFound, a as DATA_SUFFIX, b as SERVER_FN, c as RPC_CALLER, d as RPC_PREFIX, et as UNKNOWN_HEADER, f as RPC_RESPONSE, g as RpcEnvelope, h as RpcControl, i as ChainStep, j as dataOf, k as callOptionsOf, l as RPC_CONTEXT, m as RPC_THROWN, n as BUILD_HEADER, o as LIVE_SUFFIX, p as RPC_STREAM, q as clientStub, r as BUNDLED_BUILD, s as PAYLOAD_PARAM, t as AnyServerFn, u as RPC_CONTROL, v as RpcRedirect, w as ServerFnCallOptions, x as SERVER_FN_CHAIN, y as RpcStreamLine, z as applyFrameResponse } from "./client-DidH0F2e.js";
24
+ import { S as NOT_FOUND, k as REDIRECT } from "./middleware-B-tI8QHQ.js";
25
+ export { AnyServerFn, BUILD_HEADER, BUNDLED_BUILD, type BindingSlot, ChainStep, DATA_SUFFIX, FRAME_APPLIED_EVENT, type FrameComponent, LIVE_CALL, LIVE_SUFFIX, type LiveIterable, type LiveRequest, type LiveStatus, NOT_FOUND, PAYLOAD_PARAM, REDIRECT, RPC_CALLER, RPC_CONTEXT, RPC_CONTROL, RPC_PREFIX, RPC_RESPONSE, RPC_STREAM, RPC_THROWN, RpcControl, RpcEnvelope, RpcNotFound, RpcRedirect, RpcStreamLine, SERVER_FN, SERVER_FN_CHAIN, STATIC_PREFIX, ServerFn, ServerFnArgs, ServerFnCallOptions, ServerFnError, ServerFnInputAnswer, type ServerFnIssue, ServerFnMeta, ServerFnMethod, type ServerFnThrown, type Slot, Streamed, UNKNOWN_HEADER, applyFrameResponse, callOptionsOf, clientRpc, clientStub, dataOf, frameAdapter, inlineServerFns, isFrame, isServerFn, live, staticFunctionMiddleware, staticUrl, urlOf };
package/dist/client.js ADDED
@@ -0,0 +1,4 @@
1
+ import { C as live, D as applyFrameResponse, G as LIVE_CALL, J as UNKNOWN_HEADER, K as clientStub, M as FRAME_APPLIED_EVENT, O as frameAdapter, S as urlOf, V as isFrame, _ as callOptionsOf, a as PAYLOAD_PARAM, b as inlineServerFns, c as RPC_CONTROL, d as RPC_STREAM, f as RPC_THROWN, g as SERVER_FN_CHAIN, h as SERVER_FN, i as LIVE_SUFFIX, l as RPC_PREFIX, m as RpcRedirect, n as BUNDLED_BUILD, o as RPC_CALLER, p as RpcNotFound, q as ServerFnError, r as DATA_SUFFIX, s as RPC_CONTEXT, t as BUILD_HEADER, u as RPC_RESPONSE, v as clientRpc, x as isServerFn, y as dataOf } from "./client-BsyxVCyu.js";
2
+ import { NOT_FOUND, REDIRECT } from "./middleware.js";
3
+ import { i as staticUrl, r as staticFunctionMiddleware, t as STATIC_PREFIX } from "./static-fn-CXjJi-It.js";
4
+ export { BUILD_HEADER, BUNDLED_BUILD, DATA_SUFFIX, FRAME_APPLIED_EVENT, LIVE_CALL, LIVE_SUFFIX, NOT_FOUND, PAYLOAD_PARAM, REDIRECT, RPC_CALLER, RPC_CONTEXT, RPC_CONTROL, RPC_PREFIX, RPC_RESPONSE, RPC_STREAM, RPC_THROWN, RpcNotFound, RpcRedirect, SERVER_FN, SERVER_FN_CHAIN, STATIC_PREFIX, ServerFnError, UNKNOWN_HEADER, applyFrameResponse, callOptionsOf, clientRpc, clientStub, dataOf, frameAdapter, inlineServerFns, isFrame, isServerFn, live, staticFunctionMiddleware, staticUrl, urlOf };
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Functions whose body belongs to ONE side, resolved by the compiler.
3
+ *
4
+ * A codebase with a server and a browser in it has three shapes that keep
5
+ * recurring: something the server does and the browser does differently,
6
+ * something only the server may do, and something only the browser can. Written
7
+ * by hand each of them is a `typeof window === "undefined"` branch, and every
8
+ * one of those ships BOTH bodies to both bundles — the server's database call
9
+ * sits in the browser's JavaScript, unreachable and readable.
10
+ *
11
+ * The compiler replaces these calls outright, so the half that does not belong
12
+ * is absent by construction rather than unreachable. What is left here is the
13
+ * UNCOMPILED path: a bun test, a script run with no plugin, a package's own
14
+ * suite. It answers as a server would, because that is the only environment
15
+ * anything uncompiled runs in.
16
+ *
17
+ * ITS OWN MODULE, and nothing here imports anything. A client build that keeps
18
+ * the import must not reach the package index, which re-exports `context.ts`
19
+ * and `node:async_hooks` with it — the leak `middleware.ts` records at length.
20
+ */
21
+ type IsomorphicFn<Args extends readonly unknown[] = [], Out = undefined> = (...args: Args) => Out;
22
+ interface IsomorphicBuilder<Args extends readonly unknown[] = [], Out = undefined> extends IsomorphicFn<Args, Out> {
23
+ server<A extends readonly unknown[], R>(fn: (...args: A) => R): IsomorphicBuilder<A, R>;
24
+ client<A extends readonly unknown[], R>(fn: (...args: A) => R): IsomorphicBuilder<A, R>;
25
+ }
26
+ /**
27
+ * One name, two bodies, one of which survives the compile.
28
+ *
29
+ * `createIsomorphicFn().server(readFile).client(fetchIt)` is `readFile` in the
30
+ * server bundle and `fetchIt` in the client one. A half that is not declared
31
+ * becomes a no-op rather than a throw: an isomorphic function is one an author
32
+ * expects to call from anywhere, and refusing where they did not write a body
33
+ * would make every call site test the environment again.
34
+ */
35
+ declare function createIsomorphicFn(): IsomorphicBuilder;
36
+ /**
37
+ * A function the CLIENT bundle does not get a body for.
38
+ *
39
+ * The compiler leaves the body in the server build and replaces the call with a
40
+ * throwing stub in the client one, so reading a secret or opening a database
41
+ * connection cannot reach the browser even as dead code.
42
+ */
43
+ declare function createServerOnlyFn<Args extends readonly unknown[], Out>(fn: (...args: Args) => Out): (...args: Args) => Out;
44
+ /** The mirror: the body reaches the browser and the server gets a throw. */
45
+ declare function createClientOnlyFn<Args extends readonly unknown[], Out>(fn: (...args: Args) => Out): (...args: Args) => Out;
46
+ export { createServerOnlyFn as a, createIsomorphicFn as i, IsomorphicFn as n, createClientOnlyFn as r, IsomorphicBuilder as t };
47
+ //# sourceMappingURL=env-Bhk_aIkx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env-Bhk_aIkx.d.ts","names":[],"sources":["../src/env.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;KAqBY,aAAa,sCAAsC,uBAC1D,MAAM,SACN;UAEY,kBACf,sCACA,yBACQ,aAAa,MAAM;EAC3B,OAAO,8BAA8B,GAAG,QAAQ,MAAM,MAAM,IAAI,kBAAkB,GAAG;EACrF,OAAO,8BAA8B,GAAG,QAAQ,MAAM,MAAM,IAAI,kBAAkB,GAAG;;;;;;;;;;;iBAYvE,sBAAsB;;;;;;;;iBAgCtB,mBAAmB,iCAAiC,KAClE,QAAQ,MAAM,SAAS,UAClB,MAAM,SAAS;;iBAKN,mBAAmB,iCAAiC,KAClE,QAAQ,MAAM,SAAS,UAClB,MAAM,SAAS"}
package/dist/env.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { a as createServerOnlyFn, i as createIsomorphicFn, n as IsomorphicFn, r as createClientOnlyFn, t as IsomorphicBuilder } from "./env-Bhk_aIkx.js";
2
+ export { IsomorphicBuilder, IsomorphicFn, createClientOnlyFn, createIsomorphicFn, createServerOnlyFn };
package/dist/env.js ADDED
@@ -0,0 +1,43 @@
1
+ // src/env.ts
2
+ /**
3
+ * One name, two bodies, one of which survives the compile.
4
+ *
5
+ * `createIsomorphicFn().server(readFile).client(fetchIt)` is `readFile` in the
6
+ * server bundle and `fetchIt` in the client one. A half that is not declared
7
+ * becomes a no-op rather than a throw: an isomorphic function is one an author
8
+ * expects to call from anywhere, and refusing where they did not write a body
9
+ * would make every call site test the environment again.
10
+ */
11
+ function createIsomorphicFn() {
12
+ return build(() => void 0, false);
13
+ }
14
+ /**
15
+ * `served` is whether THIS chain has declared a server half. It travels with
16
+ * the chain: kept in one place for the module, the first isomorphic function
17
+ * anywhere set it for good, and every `.client(b)` written after that answered
18
+ * `undefined` where it is documented to answer `b`.
19
+ */
20
+ function build(current, served) {
21
+ const chain = Object.assign(current, {
22
+ server: (fn) => build(fn, true),
23
+ client: (fn) => served ? chain : build(fn, false)
24
+ });
25
+ return chain;
26
+ }
27
+ /**
28
+ * A function the CLIENT bundle does not get a body for.
29
+ *
30
+ * The compiler leaves the body in the server build and replaces the call with a
31
+ * throwing stub in the client one, so reading a secret or opening a database
32
+ * connection cannot reach the browser even as dead code.
33
+ */
34
+ function createServerOnlyFn(fn) {
35
+ return fn;
36
+ }
37
+ /** The mirror: the body reaches the browser and the server gets a throw. */
38
+ function createClientOnlyFn(fn) {
39
+ return fn;
40
+ }
41
+ export { createClientOnlyFn, createIsomorphicFn, createServerOnlyFn };
42
+
43
+ //# sourceMappingURL=env.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"env.js","names":[],"sources":["../src/env.ts"],"sourcesContent":["/**\n * Functions whose body belongs to ONE side, resolved by the compiler.\n *\n * A codebase with a server and a browser in it has three shapes that keep\n * recurring: something the server does and the browser does differently,\n * something only the server may do, and something only the browser can. Written\n * by hand each of them is a `typeof window === \"undefined\"` branch, and every\n * one of those ships BOTH bodies to both bundles — the server's database call\n * sits in the browser's JavaScript, unreachable and readable.\n *\n * The compiler replaces these calls outright, so the half that does not belong\n * is absent by construction rather than unreachable. What is left here is the\n * UNCOMPILED path: a bun test, a script run with no plugin, a package's own\n * suite. It answers as a server would, because that is the only environment\n * anything uncompiled runs in.\n *\n * ITS OWN MODULE, and nothing here imports anything. A client build that keeps\n * the import must not reach the package index, which re-exports `context.ts`\n * and `node:async_hooks` with it — the leak `middleware.ts` records at length.\n */\n\nexport type IsomorphicFn<Args extends readonly unknown[] = [], Out = undefined> = (\n ...args: Args\n) => Out;\n\nexport interface IsomorphicBuilder<\n Args extends readonly unknown[] = [],\n Out = undefined,\n> extends IsomorphicFn<Args, Out> {\n server<A extends readonly unknown[], R>(fn: (...args: A) => R): IsomorphicBuilder<A, R>;\n client<A extends readonly unknown[], R>(fn: (...args: A) => R): IsomorphicBuilder<A, R>;\n}\n\n/**\n * One name, two bodies, one of which survives the compile.\n *\n * `createIsomorphicFn().server(readFile).client(fetchIt)` is `readFile` in the\n * server bundle and `fetchIt` in the client one. A half that is not declared\n * becomes a no-op rather than a throw: an isomorphic function is one an author\n * expects to call from anywhere, and refusing where they did not write a body\n * would make every call site test the environment again.\n */\nexport function createIsomorphicFn(): IsomorphicBuilder {\n return build(() => undefined as never, false);\n}\n\n/**\n * `served` is whether THIS chain has declared a server half. It travels with\n * the chain: kept in one place for the module, the first isomorphic function\n * anywhere set it for good, and every `.client(b)` written after that answered\n * `undefined` where it is documented to answer `b`.\n */\nfunction build<Args extends readonly unknown[], Out>(\n current: (...args: Args) => Out,\n served: boolean,\n): IsomorphicBuilder<Args, Out> {\n // The SERVER half wins if both are declared, because nothing uncompiled runs\n // anywhere else. Theirs keeps the server implementation for the same reason\n // and says so.\n const chain = Object.assign(current, {\n server: (fn: (...args: never[]) => unknown) => build(fn as never, true),\n // `.server(a).client(b)` uncompiled is `a`, and `.client(b)` alone is `b`.\n client: (fn: (...args: never[]) => unknown) => (served ? chain : build(fn as never, false)),\n }) as unknown as IsomorphicBuilder<Args, Out>;\n return chain;\n}\n\n/**\n * A function the CLIENT bundle does not get a body for.\n *\n * The compiler leaves the body in the server build and replaces the call with a\n * throwing stub in the client one, so reading a secret or opening a database\n * connection cannot reach the browser even as dead code.\n */\nexport function createServerOnlyFn<Args extends readonly unknown[], Out>(\n fn: (...args: Args) => Out,\n): (...args: Args) => Out {\n return fn;\n}\n\n/** The mirror: the body reaches the browser and the server gets a throw. */\nexport function createClientOnlyFn<Args extends readonly unknown[], Out>(\n fn: (...args: Args) => Out,\n): (...args: Args) => Out {\n return fn;\n}\n"],"mappings":";;;;;;;;;;AA0CA,SAAgB,qBAAwC;CACtD,OAAO,YAAY,KAAA,GAAoB,KAAK;AAC9C;;;;;;;AAQA,SAAS,MACP,SACA,QAC8B;CAI9B,MAAM,QAAQ,OAAO,OAAO,SAAS;EACnC,SAAS,OAAsC,MAAM,IAAa,IAAI;EAEtE,SAAS,OAAuC,SAAS,QAAQ,MAAM,IAAa,KAAK;CAC3F,CAAC;CACD,OAAO;AACT;;;;;;;;AASA,SAAgB,mBACd,IACwB;CACxB,OAAO;AACT;;AAGA,SAAgB,mBACd,IACwB;CACxB,OAAO;AACT"}