@barqjs/start 0.0.0-stage → 0.2.0-next.0
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/LICENSE +21 -0
- package/README.md +791 -2
- package/dist/client-BHyIiYxt.js +1527 -0
- package/dist/client-BHyIiYxt.js.map +1 -0
- package/dist/client-DHCV7yvQ.d.ts +440 -0
- package/dist/client-DHCV7yvQ.d.ts.map +1 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.js +4 -0
- package/dist/env-qA8EyOr1.d.ts +49 -0
- package/dist/env-qA8EyOr1.d.ts.map +1 -0
- package/dist/env.d.ts +2 -0
- package/dist/env.js +44 -0
- package/dist/env.js.map +1 -0
- package/dist/index-Bm_-MTj6.d.ts +915 -0
- package/dist/index-Bm_-MTj6.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +6 -0
- package/dist/middleware-BdVlE8qm.d.ts +576 -0
- package/dist/middleware-BdVlE8qm.d.ts.map +1 -0
- package/dist/middleware.d.ts +2 -0
- package/dist/middleware.js +282 -0
- package/dist/middleware.js.map +1 -0
- package/dist/prerender.d.ts +98 -0
- package/dist/prerender.d.ts.map +1 -0
- package/dist/prerender.js +289 -0
- package/dist/prerender.js.map +1 -0
- package/dist/protocol.d.ts +58 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +26 -0
- package/dist/protocol.js.map +1 -0
- package/dist/serve.d.ts +135 -0
- package/dist/serve.d.ts.map +1 -0
- package/dist/serve.js +223 -0
- package/dist/serve.js.map +1 -0
- package/dist/server-BV3xKmLa.js +1148 -0
- package/dist/server-BV3xKmLa.js.map +1 -0
- package/dist/server.d.ts +142 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1 -0
- package/dist/src-ChnB_xug.js +1140 -0
- package/dist/src-ChnB_xug.js.map +1 -0
- package/dist/static-fn-DgjNArBe.js +123 -0
- package/dist/static-fn-DgjNArBe.js.map +1 -0
- package/package.json +106 -4
- package/src/client.ts +672 -0
- package/src/context.ts +666 -0
- package/src/cookies.ts +229 -0
- package/src/env.ts +86 -0
- package/src/fn-error.ts +97 -0
- package/src/frames-client.ts +1216 -0
- package/src/frames-server.ts +464 -0
- package/src/frames.ts +285 -0
- package/src/index.ts +714 -0
- package/src/live-client.ts +50 -0
- package/src/live-server.ts +182 -0
- package/src/middleware.ts +994 -0
- package/src/prerender.ts +477 -0
- package/src/protocol.ts +61 -0
- package/src/rate-limit.ts +202 -0
- package/src/serve.ts +153 -0
- package/src/server.ts +990 -0
- package/src/session.ts +449 -0
- package/src/start-instance.ts +147 -0
- package/src/static-fn.ts +181 -0
- package/src/static.ts +302 -0
- package/src/stubs.ts +32 -0
- package/src/timing.ts +29 -0
- package/src/wire-types.ts +139 -0
|
@@ -0,0 +1,440 @@
|
|
|
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-BdVlE8qm.js";
|
|
2
|
+
import { AnySerializationAdapter } from "@barqjs/server/codec";
|
|
3
|
+
import "@barqjs/web";
|
|
4
|
+
import { Child, Incoming, JSXElement } from "@barqjs/core";
|
|
5
|
+
//#region src/fn-error.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* How a server function's failure reaches its caller in the browser. Its own
|
|
8
|
+
* module, so every module that answers a call can raise it and none of them
|
|
9
|
+
* has to import the stub to do so.
|
|
10
|
+
*/
|
|
11
|
+
/** What a handler threw, as a development server tells its caller. */
|
|
12
|
+
interface ServerFnThrown {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly message: string;
|
|
15
|
+
/** Mapped to the source files by the development server. */
|
|
16
|
+
readonly stack?: string;
|
|
17
|
+
}
|
|
18
|
+
/** One thing wrong with a call's input, and the path to the field it is about. */
|
|
19
|
+
interface ServerFnIssue {
|
|
20
|
+
readonly message: string;
|
|
21
|
+
readonly path?: readonly (string | number)[];
|
|
22
|
+
}
|
|
23
|
+
/** What a failed call's JSON body may carry. */
|
|
24
|
+
interface FailedAnswer {
|
|
25
|
+
readonly message?: string;
|
|
26
|
+
readonly issues?: readonly ServerFnIssue[];
|
|
27
|
+
readonly thrown?: ServerFnThrown;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A server function answered with a failure.
|
|
31
|
+
*
|
|
32
|
+
* THE MESSAGE IS NOT THE HANDLER'S, and that is deliberate rather than
|
|
33
|
+
* unfinished. `server.ts` refuses to put a handler's error text on the wire
|
|
34
|
+
* because it can name a table, a column or a filesystem path, and none of that
|
|
35
|
+
* is the caller's business — TanStack serialises the whole error object to the
|
|
36
|
+
* client instead. What a caller legitimately needs is the STATUS, so it is here
|
|
37
|
+
* as a field rather than something to parse back out of a string.
|
|
38
|
+
*/
|
|
39
|
+
declare class ServerFnError extends Error {
|
|
40
|
+
readonly status: number;
|
|
41
|
+
/**
|
|
42
|
+
* What was wrong with the input, for a 400 the validator refused: one entry
|
|
43
|
+
* per issue, with the path to the field it is about. Sent in every build,
|
|
44
|
+
* because it describes the caller's own input.
|
|
45
|
+
*/
|
|
46
|
+
readonly issues?: readonly ServerFnIssue[];
|
|
47
|
+
/**
|
|
48
|
+
* What the handler threw: its name, message and source-mapped stack. Only a
|
|
49
|
+
* development server sends it; in production this is `undefined`, so handle
|
|
50
|
+
* a failure by its `status` rather than by this.
|
|
51
|
+
*/
|
|
52
|
+
readonly thrown?: ServerFnThrown;
|
|
53
|
+
/**
|
|
54
|
+
* The answer itself, unread.
|
|
55
|
+
*
|
|
56
|
+
* The server's own refusals land here: a 400 for input, a 403 for an origin,
|
|
57
|
+
* a 404, a 405, a 413. A `Response` a middleware or a handler THREW is not
|
|
58
|
+
* wrapped at all; the call rejects with it. A handler's own failure has
|
|
59
|
+
* nothing here worth reading: the server answers those with one fixed line,
|
|
60
|
+
* for the reason above.
|
|
61
|
+
*/
|
|
62
|
+
readonly response?: Response;
|
|
63
|
+
constructor(id: string, status: number, response?: Response, answer?: FailedAnswer);
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/static-fn.d.ts
|
|
67
|
+
/** Where a static answer is served from, under the client output. */
|
|
68
|
+
declare const STATIC_PREFIX = "/_barq/static/";
|
|
69
|
+
/** Where the answer for one call is, as a URL. */
|
|
70
|
+
declare function staticUrl(id: string, input: unknown): string;
|
|
71
|
+
/**
|
|
72
|
+
* Make a server function static: answered at prerender time, read from a file
|
|
73
|
+
* afterwards.
|
|
74
|
+
*
|
|
75
|
+
* List it LAST: the key is the input as this middleware sees it, which is the
|
|
76
|
+
* input the caller passed only when nothing before it has validated or
|
|
77
|
+
* reshaped it. An input no prerendered page called with has no file, and the
|
|
78
|
+
* call goes to the server as any other would.
|
|
79
|
+
*
|
|
80
|
+
* Only the RESULT is stored. What a server middleware sends back with
|
|
81
|
+
* `sendContext` is not, and a `Response` or a stream is never written.
|
|
82
|
+
*/
|
|
83
|
+
declare const staticFunctionMiddleware: BuiltMiddleware<WithServer<WithClient<FreshMiddleware, Promise<{} | null>>, Promise<unknown>>>;
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/stubs.d.ts
|
|
86
|
+
/**
|
|
87
|
+
* The stubs a page has made, and how `live` calls one: a module of its own so
|
|
88
|
+
* the frames client finds a stub by id without importing the stub's module.
|
|
89
|
+
*/
|
|
90
|
+
/** What the registry holds: a callable stub that knows its id. */
|
|
91
|
+
type Stub = ((options?: unknown) => Promise<unknown>) & {
|
|
92
|
+
readonly meta: {
|
|
93
|
+
readonly id: string;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
/** A call at the live address: where the last connection left off, and how to end this one. */
|
|
97
|
+
interface LiveRequest {
|
|
98
|
+
readonly lastEventId: string | null;
|
|
99
|
+
readonly signal: AbortSignal;
|
|
100
|
+
}
|
|
101
|
+
/** How `live` calls a stub at the live address, its client middleware around the fetch. */
|
|
102
|
+
declare const LIVE_CALL: unique symbol;
|
|
103
|
+
declare function clientStub(id: string): Stub | undefined;
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/frames.d.ts
|
|
106
|
+
/**
|
|
107
|
+
* A slot on a server component's props: a call the browser answers with its
|
|
108
|
+
* own content. What the call is given crosses as data, and its `children` as a
|
|
109
|
+
* region of server markup. `$key` names the occurrence, so its content follows
|
|
110
|
+
* it across updates rather than its position.
|
|
111
|
+
*/
|
|
112
|
+
type Slot<P extends object = {}> = (props: Incoming<P & {
|
|
113
|
+
readonly $key?: string | number;
|
|
114
|
+
readonly children?: Child;
|
|
115
|
+
}>) => JSXElement;
|
|
116
|
+
/**
|
|
117
|
+
* A slot the server component reads as data: called once per data context,
|
|
118
|
+
* `const like = props.like({ cid })`, and each property placed whole at a
|
|
119
|
+
* position of the server's own markup, an attribute, a class name or style
|
|
120
|
+
* property in object form, a handler, a ref or a text child. The page's fill
|
|
121
|
+
* takes the call's arguments and returns the object those positions read; the
|
|
122
|
+
* server never has its values to compute with.
|
|
123
|
+
*/
|
|
124
|
+
type BindingSlot<A extends object = {}, B extends object = Record<string, unknown>> = keyof A extends never ? (args?: {
|
|
125
|
+
readonly $key?: string | number;
|
|
126
|
+
}) => Readonly<B> : (args: A & {
|
|
127
|
+
readonly $key?: string | number;
|
|
128
|
+
}) => Readonly<B>;
|
|
129
|
+
/** The brand on a frame, on either side, carrying where it lives. */
|
|
130
|
+
declare const FRAME: unique symbol;
|
|
131
|
+
/** How a frame is named across the wire: its server function and its address. */
|
|
132
|
+
interface FrameRef {
|
|
133
|
+
readonly id: string;
|
|
134
|
+
readonly address: string;
|
|
135
|
+
}
|
|
136
|
+
/** Whether a value is a frame: a server function's component result, on either side. */
|
|
137
|
+
declare function isFrame(value: unknown): boolean;
|
|
138
|
+
/** Dispatched on `document` once a frame update has been applied to the page. */
|
|
139
|
+
declare const FRAME_APPLIED_EVENT = "barq:frame-applied";
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/frames-client.d.ts
|
|
142
|
+
/** What a frame renders as: a component taking the slots' content as props. */
|
|
143
|
+
type FrameComponent = ((props: Record<string, unknown>) => Node) & {
|
|
144
|
+
readonly [FRAME]: FrameRef;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* How a frame named in the page's seed becomes the component for the region
|
|
148
|
+
* the page already carries. The client entry installs it beside the
|
|
149
|
+
* application's own adapters, before the seed is read.
|
|
150
|
+
*/
|
|
151
|
+
declare const frameAdapter: AnySerializationAdapter;
|
|
152
|
+
/**
|
|
153
|
+
* Read a frame answer and apply it: the component for its address, the same
|
|
154
|
+
* one every time, with every place it is mounted morphed to the new markup
|
|
155
|
+
* once the region's stylesheets have loaded.
|
|
156
|
+
*/
|
|
157
|
+
declare function applyFrameResponse(response: Response): Promise<FrameComponent>;
|
|
158
|
+
/** What the loop says about its connection. */
|
|
159
|
+
type LiveStatus = "connected" | "reconnecting" | "closed";
|
|
160
|
+
interface LiveIterable<T> extends AsyncIterable<T> {
|
|
161
|
+
/** Told `"connected"` per connection, `"reconnecting"` with the error per death, `"closed"` at the end. */
|
|
162
|
+
onstatus?: (status: LiveStatus, error?: unknown) => void;
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/live-client.d.ts
|
|
166
|
+
/**
|
|
167
|
+
* Declare a server function's answer a standing one. In the browser, calling
|
|
168
|
+
* the result returns an iterable that stays connected; on the server it is the
|
|
169
|
+
* call itself, a frame it answers with marked for the page to connect.
|
|
170
|
+
*/
|
|
171
|
+
declare function live<In, Out>(fn: ServerFn<In, Out, never>): (...args: Parameters<ServerFn<In, Out, never>>) => LiveIterable<Awaited<Out>>;
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/client.d.ts
|
|
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
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* The request header every GET call the stub makes carries, and is refused
|
|
329
|
+
* without.
|
|
330
|
+
*
|
|
331
|
+
* A link, an `<img>`, a prefetch and a navigation cannot set a header; only
|
|
332
|
+
* script can, and a cross-origin script that sets one is preflighted and
|
|
333
|
+
* refused by the origin check. So a `GET` server function stays out of reach
|
|
334
|
+
* of everything that is not this application's own code, which is the reason
|
|
335
|
+
* `GET` was once refused outright: a mutation reachable by navigation is a
|
|
336
|
+
* one-click CSRF carrying `SameSite=Lax` cookies.
|
|
337
|
+
*/
|
|
338
|
+
declare const RPC_CALLER = "x-barq-fn";
|
|
339
|
+
/** Where a GET call's payload rides, since a GET has no body. */
|
|
340
|
+
declare const PAYLOAD_PARAM = "payload";
|
|
341
|
+
/**
|
|
342
|
+
* How a server function is CALLED: `fn({ data })`, and `fn()` when it takes
|
|
343
|
+
* none.
|
|
344
|
+
*
|
|
345
|
+
* TanStack's convention, and worth matching for a reason beyond fidelity: a
|
|
346
|
+
* named field leaves room for the options that
|
|
347
|
+
* come after it without ever changing the call shape again, and it removes the
|
|
348
|
+
* `adminStats(undefined)` the bare form forced on every no-argument call.
|
|
349
|
+
*/
|
|
350
|
+
/**
|
|
351
|
+
* What a call may carry beside its `data`. TanStack's three, under their names.
|
|
352
|
+
*/
|
|
353
|
+
interface ServerFnCallOptions {
|
|
354
|
+
/**
|
|
355
|
+
* Aborts the call. In the browser the fetch is cancelled, and the server sees
|
|
356
|
+
* its request go away, which is what the handler's own `signal` follows.
|
|
357
|
+
*/
|
|
358
|
+
readonly signal?: AbortSignal;
|
|
359
|
+
/**
|
|
360
|
+
* Sent with the request. A name set here wins over the same name from a
|
|
361
|
+
* client middleware's `next({ headers })`, since the call is the more
|
|
362
|
+
* specific of the two.
|
|
363
|
+
*/
|
|
364
|
+
readonly headers?: HeadersInit;
|
|
365
|
+
/** Used in place of the global `fetch`, for this call alone. */
|
|
366
|
+
readonly fetch?: typeof fetch;
|
|
367
|
+
}
|
|
368
|
+
type ServerFnArgs<In, Method extends ServerFnMethod = "POST"> = [undefined] extends [In] ? [options?: ({
|
|
369
|
+
readonly data?: In;
|
|
370
|
+
} & ServerFnCallOptions) | FormFor<Method>] : [options: ({
|
|
371
|
+
readonly data: In;
|
|
372
|
+
} & ServerFnCallOptions) | FormFor<Method>];
|
|
373
|
+
/** A form is a body, so only a `POST` function takes one. */
|
|
374
|
+
type FormFor<Method extends ServerFnMethod> = Method extends "POST" ? FormData : never;
|
|
375
|
+
/**
|
|
376
|
+
* A server function, from either side.
|
|
377
|
+
*
|
|
378
|
+
* A bare `FormData` is accepted BESIDE the options object, and only there. It is
|
|
379
|
+
* what `<form action={fn}>` hands the function on the enhanced path, and a
|
|
380
|
+
* `FormData` can never be mistaken for an options object — so the no-JS
|
|
381
|
+
* submission and the JS one reach the handler identically, which is the whole
|
|
382
|
+
* point of that path. TanStack has no counterpart to match here.
|
|
383
|
+
*/
|
|
384
|
+
type ServerFn<In, Out, Method extends ServerFnMethod = "POST"> = ((...args: ServerFnArgs<In, Method>) => Promise<Out>) & {
|
|
385
|
+
readonly [SERVER_FN]: true;
|
|
386
|
+
readonly meta: ServerFnMeta;
|
|
387
|
+
/** TanStack's name for `meta`: the id this function is mounted at. */
|
|
388
|
+
readonly serverFnMeta: ServerFnMeta;
|
|
389
|
+
/** The method this function answers. */
|
|
390
|
+
readonly method: ServerFnMethod;
|
|
391
|
+
/**
|
|
392
|
+
* Where a form posts to reach this function: what `<form action={fn}>`
|
|
393
|
+
* writes. Empty for a function that is not mounted, which has no endpoint.
|
|
394
|
+
*/
|
|
395
|
+
readonly url: string;
|
|
396
|
+
};
|
|
397
|
+
/**
|
|
398
|
+
* Any server function, whatever it takes and returns.
|
|
399
|
+
*
|
|
400
|
+
* `never` in, because the input sits in a parameter: a function that takes a
|
|
401
|
+
* string does not accept `unknown`, and every function accepts `never`. The
|
|
402
|
+
* method is `never` for the same reason: it decides whether a form is a
|
|
403
|
+
* parameter.
|
|
404
|
+
*/
|
|
405
|
+
type AnyServerFn = ServerFn<never, unknown, never>;
|
|
406
|
+
/** The form endpoint of a mounted id, or nothing for a function with none. */
|
|
407
|
+
declare function urlOf(id: string): string;
|
|
408
|
+
/** The `data` out of a call, whichever of the two shapes it arrived in. */
|
|
409
|
+
declare function dataOf(options: unknown): unknown;
|
|
410
|
+
/** What a call asked for beside its `data`. A bare `FormData` asks for nothing. */
|
|
411
|
+
declare function callOptionsOf(options: unknown): ServerFnCallOptions | undefined;
|
|
412
|
+
/**
|
|
413
|
+
* The client half. The compiler emits this in a module compiled for the client,
|
|
414
|
+
* with the id, the chain, and nothing else — no handler body, no validator, no
|
|
415
|
+
* imports the body needed.
|
|
416
|
+
*
|
|
417
|
+
* `chain` is the middlewares the function declared, and it is here so a
|
|
418
|
+
* `.client()` half can run around the fetch. Only the CLIENT halves survive the
|
|
419
|
+
* compile: `createMiddleware(...).server(fn)` has its `server` argument deleted
|
|
420
|
+
* in the client build, so a middleware may hold both without the server body —
|
|
421
|
+
* or the database import it needed — reaching the browser.
|
|
422
|
+
*/
|
|
423
|
+
declare function clientRpc<In, Out, Method extends ServerFnMethod = "POST">(id: string, declaredChain?: readonly ChainStep[], declared?: {
|
|
424
|
+
readonly method?: Method;
|
|
425
|
+
}): ServerFn<In, Out, Method>;
|
|
426
|
+
/** Where a server function keeps the middleware it declared, for another to inline. */
|
|
427
|
+
declare const SERVER_FN_CHAIN: unique symbol;
|
|
428
|
+
/** What `.middleware([...])` takes: a middleware, or a server function whose chain it inlines. */
|
|
429
|
+
type ChainStep = Middleware | AnyServerFn;
|
|
430
|
+
/**
|
|
431
|
+
* A chain with each server function in it replaced by the middleware that
|
|
432
|
+
* function declared, which is TanStack's rule: listing a function lists its
|
|
433
|
+
* guards, never its handler.
|
|
434
|
+
*/
|
|
435
|
+
declare function inlineServerFns(chain: readonly ChainStep[]): readonly Middleware[];
|
|
436
|
+
/** Whether a value is a server function, by brand rather than by shape. */
|
|
437
|
+
declare function isServerFn(value: unknown): value is ServerFn<unknown, unknown>;
|
|
438
|
+
//#endregion
|
|
439
|
+
export { inlineServerFns as A, FRAME_APPLIED_EVENT as B, ServerFnInputAnswer as C, callOptionsOf as D, Streamed as E, LiveIterable as F, clientStub as G, isFrame as H, LiveStatus as I, staticUrl as J, STATIC_PREFIX as K, applyFrameResponse as L, urlOf as M, live as N, clientRpc as O, FrameComponent as P, frameAdapter as R, ServerFnCallOptions as S, ServerFnMethod as T, LIVE_CALL as U, Slot as V, LiveRequest as W, ServerFnIssue as X, ServerFnError as Y, ServerFnThrown as Z, RpcStreamLine as _, PAYLOAD_PARAM as a, ServerFn as b, RPC_CONTROL as c, RPC_STREAM as d, RPC_THROWN as f, RpcRedirect as g, RpcNotFound as h, LIVE_SUFFIX as i, isServerFn as j, dataOf as k, RPC_PREFIX as l, RpcEnvelope as m, ChainStep as n, RPC_CALLER as o, RpcControl as p, staticFunctionMiddleware as q, DATA_SUFFIX as r, RPC_CONTEXT as s, AnyServerFn as t, RPC_RESPONSE as u, SERVER_FN as v, ServerFnMeta as w, ServerFnArgs as x, SERVER_FN_CHAIN as y, BindingSlot as z };
|
|
440
|
+
//# sourceMappingURL=client-DHCV7yvQ.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-DHCV7yvQ.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;;;UAIM;WACN;WACA,kBAAkB;WAClB,SAAS;;;;;;;;;;;;cAaP,sBAAsB;WACxB;;;;;;WAMA,kBAAkB;;;;;;WAMlB,SAAS;;;;;;;;;;WAUT,WAAW;EACpB,YAAY,YAAY,gBAAgB,WAAW,UAAU,SAAS;;;;;cClC3D;;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;;;;;;iBA6wBL,mBAAmB,UAAU,WAAW,QAAQ;;KA6F1D;UAEK,aAAa,WAAW,cAAc;;EAErD,YAAY,QAAQ,YAAY;;;;;;;;;iBCjgClB,KAAK,IAAI,KACvB,IAAI,SAAS,IAAI,kBACZ,MAAM,WAAW,SAAS,IAAI,iBAAiB,aAAa,QAAQ;;;cCc9D;;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;;;;;;;;;;;;;cAcP;;cAGA;;;;;;;;;;;;;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;;cA6QR;;KAGD,YAAY,aAAa;;;;;;iBAOrB,gBAAgB,gBAAgB,uBAAuB;;iBAavD,WAAW,iBAAiB,SAAS"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { A as inlineServerFns, B as FRAME_APPLIED_EVENT, C as ServerFnInputAnswer, D as callOptionsOf, E as Streamed, F as LiveIterable, G as clientStub, H as isFrame, I as LiveStatus, J as staticUrl, K as STATIC_PREFIX, L as applyFrameResponse, M as urlOf, N as live, O as clientRpc, P as FrameComponent, R as frameAdapter, S as ServerFnCallOptions, T as ServerFnMethod, U as LIVE_CALL, V as Slot, W as LiveRequest, X as ServerFnIssue, Y as ServerFnError, Z as ServerFnThrown, _ as RpcStreamLine, a as PAYLOAD_PARAM, b as ServerFn, c as RPC_CONTROL, d as RPC_STREAM, f as RPC_THROWN, g as RpcRedirect, h as RpcNotFound, i as LIVE_SUFFIX, j as isServerFn, k as dataOf, l as RPC_PREFIX, m as RpcEnvelope, n as ChainStep, o as RPC_CALLER, p as RpcControl, q as staticFunctionMiddleware, r as DATA_SUFFIX, s as RPC_CONTEXT, t as AnyServerFn, u as RPC_RESPONSE, v as SERVER_FN, w as ServerFnMeta, x as ServerFnArgs, y as SERVER_FN_CHAIN, z as BindingSlot } from "./client-DHCV7yvQ.js";
|
|
2
|
+
import { S as NOT_FOUND, k as REDIRECT } from "./middleware-BdVlE8qm.js";
|
|
3
|
+
export { AnyServerFn, 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, applyFrameResponse, callOptionsOf, clientRpc, clientStub, dataOf, frameAdapter, inlineServerFns, isFrame, isServerFn, live, staticFunctionMiddleware, staticUrl, urlOf };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { A as FRAME_APPLIED_EVENT, E as frameAdapter, G as ServerFnError, T as applyFrameResponse, U as LIVE_CALL, W as clientStub, _ as dataOf, a as RPC_CONTEXT, b as urlOf, c as RPC_RESPONSE, d as RpcNotFound, f as RpcRedirect, g as clientRpc, h as callOptionsOf, i as RPC_CALLER, l as RPC_STREAM, m as SERVER_FN_CHAIN, n as LIVE_SUFFIX, o as RPC_CONTROL, p as SERVER_FN, r as PAYLOAD_PARAM, s as RPC_PREFIX, t as DATA_SUFFIX, u as RPC_THROWN, v as inlineServerFns, x as live, y as isServerFn, z as isFrame } from "./client-BHyIiYxt.js";
|
|
2
|
+
import { NOT_FOUND, REDIRECT } from "./middleware.js";
|
|
3
|
+
import { i as staticUrl, r as staticFunctionMiddleware, t as STATIC_PREFIX } from "./static-fn-DgjNArBe.js";
|
|
4
|
+
export { 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, applyFrameResponse, callOptionsOf, clientRpc, clientStub, dataOf, frameAdapter, inlineServerFns, isFrame, isServerFn, live, staticFunctionMiddleware, staticUrl, urlOf };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
//#region src/env.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Functions whose body belongs to ONE side, resolved by the compiler.
|
|
4
|
+
*
|
|
5
|
+
* A codebase with a server and a browser in it has three shapes that keep
|
|
6
|
+
* recurring: something the server does and the browser does differently,
|
|
7
|
+
* something only the server may do, and something only the browser can. Written
|
|
8
|
+
* by hand each of them is a `typeof window === "undefined"` branch, and every
|
|
9
|
+
* one of those ships BOTH bodies to both bundles — the server's database call
|
|
10
|
+
* sits in the browser's JavaScript, unreachable and readable.
|
|
11
|
+
*
|
|
12
|
+
* The compiler replaces these calls outright, so the half that does not belong
|
|
13
|
+
* is absent by construction rather than unreachable. What is left here is the
|
|
14
|
+
* UNCOMPILED path: a bun test, a script run with no plugin, a package's own
|
|
15
|
+
* suite. It answers as a server would, because that is the only environment
|
|
16
|
+
* anything uncompiled runs in.
|
|
17
|
+
*
|
|
18
|
+
* ITS OWN MODULE, and nothing here imports anything. A client build that keeps
|
|
19
|
+
* the import must not reach the package index, which re-exports `context.ts`
|
|
20
|
+
* and `node:async_hooks` with it — the leak `middleware.ts` records at length.
|
|
21
|
+
*/
|
|
22
|
+
type IsomorphicFn<Args extends readonly unknown[] = [], Out = undefined> = (...args: Args) => Out;
|
|
23
|
+
interface IsomorphicBuilder<Args extends readonly unknown[] = [], Out = undefined> extends IsomorphicFn<Args, Out> {
|
|
24
|
+
server<A extends readonly unknown[], R>(fn: (...args: A) => R): IsomorphicBuilder<A, R>;
|
|
25
|
+
client<A extends readonly unknown[], R>(fn: (...args: A) => R): IsomorphicBuilder<A, R>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* One name, two bodies, one of which survives the compile.
|
|
29
|
+
*
|
|
30
|
+
* `createIsomorphicFn().server(readFile).client(fetchIt)` is `readFile` in the
|
|
31
|
+
* server bundle and `fetchIt` in the client one. A half that is not declared
|
|
32
|
+
* becomes a no-op rather than a throw: an isomorphic function is one an author
|
|
33
|
+
* expects to call from anywhere, and refusing where they did not write a body
|
|
34
|
+
* would make every call site test the environment again.
|
|
35
|
+
*/
|
|
36
|
+
declare function createIsomorphicFn(): IsomorphicBuilder;
|
|
37
|
+
/**
|
|
38
|
+
* A function the CLIENT bundle does not get a body for.
|
|
39
|
+
*
|
|
40
|
+
* The compiler leaves the body in the server build and replaces the call with a
|
|
41
|
+
* throwing stub in the client one, so reading a secret or opening a database
|
|
42
|
+
* connection cannot reach the browser even as dead code.
|
|
43
|
+
*/
|
|
44
|
+
declare function createServerOnlyFn<Args extends readonly unknown[], Out>(fn: (...args: Args) => Out): (...args: Args) => Out;
|
|
45
|
+
/** The mirror: the body reaches the browser and the server gets a throw. */
|
|
46
|
+
declare function createClientOnlyFn<Args extends readonly unknown[], Out>(fn: (...args: Args) => Out): (...args: Args) => Out;
|
|
47
|
+
//#endregion
|
|
48
|
+
export { createServerOnlyFn as a, createIsomorphicFn as i, IsomorphicFn as n, createClientOnlyFn as r, IsomorphicBuilder as t };
|
|
49
|
+
//# sourceMappingURL=env-qA8EyOr1.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env-qA8EyOr1.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
package/dist/env.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region 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
|
+
//#endregion
|
|
42
|
+
export { createClientOnlyFn, createIsomorphicFn, createServerOnlyFn };
|
|
43
|
+
|
|
44
|
+
//# sourceMappingURL=env.js.map
|
package/dist/env.js.map
ADDED
|
@@ -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"}
|