@teyik0/furin 0.1.0-alpha.13 → 0.1.0-alpha.14

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 (58) hide show
  1. package/README.md +14 -232
  2. package/dist/await.d.ts +67 -0
  3. package/dist/build/index.js +2013 -191
  4. package/dist/cli/index.js +2007 -190
  5. package/dist/client.d.ts +33 -2
  6. package/dist/client.js +65 -1
  7. package/dist/deferred-ndjson.d.ts +32 -0
  8. package/dist/error.d.ts +14 -0
  9. package/dist/furin.d.ts +3 -0
  10. package/dist/furin.js +2684 -318
  11. package/dist/lang-detect.d.ts +5 -0
  12. package/dist/link.d.ts +1 -0
  13. package/dist/link.js +1453 -95
  14. package/dist/plugin/index.js +96 -25
  15. package/dist/plugin/transform-client.d.ts +2 -1
  16. package/dist/render/assemble.d.ts +32 -0
  17. package/dist/render/cache.d.ts +7 -0
  18. package/dist/render/element.d.ts +17 -1
  19. package/dist/render/index.d.ts +10 -1
  20. package/dist/render/index.ts +247 -44
  21. package/dist/render/loaders.d.ts +46 -3
  22. package/dist/render/shell.d.ts +16 -6
  23. package/dist/render/shell.ts +27 -12
  24. package/dist/router-provider.d.ts +86 -18
  25. package/dist/router.d.ts +63 -0
  26. package/dist/router.js +2768 -317
  27. package/dist/router.ts +543 -54
  28. package/dist/server-error.d.ts +12 -0
  29. package/dist/utils.d.ts +7 -0
  30. package/package.json +8 -7
  31. package/src/adapter/bun.ts +3 -3
  32. package/src/adapter/static.ts +73 -31
  33. package/src/await.tsx +175 -0
  34. package/src/build/client.ts +4 -8
  35. package/src/build/hydrate.ts +67 -1
  36. package/src/build/route-types.ts +5 -1
  37. package/src/build/scan-server.ts +6 -23
  38. package/src/client.ts +58 -1
  39. package/src/deferred-ndjson.ts +212 -0
  40. package/src/dev-page-plugin.ts +2 -0
  41. package/src/error.ts +18 -1
  42. package/src/furin.ts +12 -2
  43. package/src/lang-detect.ts +46 -0
  44. package/src/link.tsx +16 -3
  45. package/src/plugin/index.ts +7 -8
  46. package/src/plugin/transform-client.ts +108 -39
  47. package/src/render/assemble.ts +67 -0
  48. package/src/render/boundaries.tsx +23 -5
  49. package/src/render/cache.ts +7 -0
  50. package/src/render/default-screens.tsx +4 -11
  51. package/src/render/element.tsx +26 -3
  52. package/src/render/index.ts +247 -44
  53. package/src/render/loaders.ts +221 -18
  54. package/src/render/shell.ts +27 -12
  55. package/src/router-provider.tsx +414 -122
  56. package/src/router.ts +543 -54
  57. package/src/server-error.ts +47 -0
  58. package/src/utils.ts +31 -0
package/dist/client.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Cookie, StatusMap } from "elysia";
2
2
  import type { AnySchema, HTTPHeaders, UnwrapSchema } from "elysia/types";
3
3
  import type { RequestLogger } from "evlog";
4
+ export { Await, useAsyncError, useAsyncValue } from "./await.tsx";
4
5
  declare const UNSET: unique symbol;
5
6
  type Unset = typeof UNSET;
6
7
  type ToRecord<T> = {
@@ -100,7 +101,7 @@ export interface HeadOptions {
100
101
  children: string;
101
102
  }>;
102
103
  }
103
- type ExtractLoaderReturn<TLoader> = TLoader extends (...args: never[]) => unknown ? Awaited<ReturnType<TLoader>> extends object ? ToRecord<Awaited<ReturnType<TLoader>>> : {} : {};
104
+ type ExtractLoaderReturn<TLoader> = TLoader extends (...args: never[]) => unknown ? Awaited<ReturnType<TLoader>> extends object ? ToRecord<Omit<Awaited<ReturnType<TLoader>>, "__isDeferred">> : {} : {};
104
105
  export interface PageConfig<TParentData extends Record<string, unknown>, TParams, TQuery, TPageLoaderData extends object = {}> {
105
106
  component: React.FC<TParentData & TPageLoaderData & ComponentProps<TParams, TQuery>>;
106
107
  head?: (ctx: ComponentProps<TParams, TQuery> & TParentData & TPageLoaderData) => HeadOptions;
@@ -192,4 +193,34 @@ export type InferProps<T> = T extends {
192
193
  } ? P : T extends Route<infer D, infer P, infer Q> ? D & {
193
194
  children: React.ReactNode;
194
195
  } & ComponentProps<P, Q> : never;
195
- export {};
196
+ /**
197
+ * Brand key that marks an object as deferred. Using a `const` string avoids
198
+ * a `unique symbol` that would complicate cross-module inference.
199
+ */
200
+ declare const DEFERRED_BRAND: "__isDeferred";
201
+ /**
202
+ * A loader return value that contains a mix of synchronous scalar fields and
203
+ * lazy `Promise<T>` fields. Scalar fields are serialised into the initial HTML
204
+ * shell; Promise fields are streamed as late `<script>` resolution chunks.
205
+ *
206
+ * @example
207
+ * loader: () => defer({
208
+ * title: "My Board", // synchronous — available immediately
209
+ * stats: fetchStats(), // Promise — streamed when it resolves
210
+ * })
211
+ */
212
+ export type DeferredData<T extends Record<string, unknown>> = T & {
213
+ readonly [DEFERRED_BRAND]: true;
214
+ };
215
+ /**
216
+ * Wraps loader data so that Promise-valued fields are streamed lazily while
217
+ * scalar fields are embedded in the initial HTML shell immediately.
218
+ *
219
+ * Use inside a page loader only (not in route / layout loaders — v1 restriction).
220
+ */
221
+ export declare function defer<T extends Record<string, unknown>>(data: T): DeferredData<T>;
222
+ /**
223
+ * Type guard for DeferredData. Used by the render pipeline to distinguish a
224
+ * plain loader return from a deferred one.
225
+ */
226
+ export declare function isDeferred(v: unknown): v is DeferredData<Record<string, unknown>>;
package/dist/client.js CHANGED
@@ -1,4 +1,53 @@
1
1
  // @bun
2
+ // src/await.tsx
3
+ import { Component, createContext, createElement, use } from "react";
4
+ var AsyncErrorContext = createContext(undefined);
5
+ function useAsyncError() {
6
+ return use(AsyncErrorContext);
7
+ }
8
+ function useAsyncValue() {
9
+ const ctx = use(AwaitValueContext);
10
+ if (!ctx) {
11
+ throw new Error("useAsyncValue must be used inside <Await> children");
12
+ }
13
+ return use(ctx.promise);
14
+ }
15
+ var AwaitValueContext = createContext(undefined);
16
+
17
+ class AsyncErrorBoundary extends Component {
18
+ constructor(props) {
19
+ super(props);
20
+ this.state = { hasError: false, error: undefined, resolve: props.resolve };
21
+ }
22
+ static getDerivedStateFromProps(props, state) {
23
+ if (props.resolve !== state.resolve) {
24
+ return { hasError: false, error: undefined, resolve: props.resolve };
25
+ }
26
+ return null;
27
+ }
28
+ static getDerivedStateFromError(error) {
29
+ return { hasError: true, error };
30
+ }
31
+ render() {
32
+ const { hasError, error } = this.state;
33
+ const { children, errorElement } = this.props;
34
+ if (hasError) {
35
+ if (errorElement === undefined) {
36
+ throw error;
37
+ }
38
+ return createElement(AsyncErrorContext.Provider, { value: error }, errorElement);
39
+ }
40
+ return children;
41
+ }
42
+ }
43
+ function Await({ resolve, errorElement, children }) {
44
+ return createElement(AsyncErrorBoundary, { errorElement, resolve }, createElement(AwaitInner, { resolve, children }));
45
+ }
46
+ function AwaitInner({ resolve, children }) {
47
+ const value = use(resolve);
48
+ return createElement(AwaitValueContext.Provider, { value: { promise: resolve } }, children ? children(value) : null);
49
+ }
50
+
2
51
  // src/client.ts
3
52
  function createRoute(config) {
4
53
  const route = {
@@ -15,6 +64,21 @@ function createRoute(config) {
15
64
  };
16
65
  return route;
17
66
  }
67
+ var DEFERRED_BRAND = "__isDeferred";
68
+ function defer(data) {
69
+ if (Object.hasOwn(data, DEFERRED_BRAND)) {
70
+ throw new Error(`[furin] defer() received an object with a reserved key "${DEFERRED_BRAND}". Rename this field to avoid conflicts with the deferred-data runtime.`);
71
+ }
72
+ return { ...data, [DEFERRED_BRAND]: true };
73
+ }
74
+ function isDeferred(v) {
75
+ return typeof v === "object" && v !== null && Object.hasOwn(v, DEFERRED_BRAND) && v[DEFERRED_BRAND] === true;
76
+ }
18
77
  export {
19
- createRoute
78
+ useAsyncValue,
79
+ useAsyncError,
80
+ isDeferred,
81
+ defer,
82
+ createRoute,
83
+ Await
20
84
  };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Result returned by `parseDeferredNdjson`.
3
+ */
4
+ export interface DeferredNdjsonResult {
5
+ /**
6
+ * Deferred promises keyed by their field name. Each Promise resolves (or
7
+ * rejects) once the corresponding data is available. In the SSR case the
8
+ * promises are already settled (serialised by `toCrossJSONAsync`). For
9
+ * streaming endpoints they will settle as the server flushes resolution
10
+ * chunks.
11
+ */
12
+ deferredPromises: Record<string, Promise<unknown>>;
13
+ /** Scalar/sync values from the loader — available immediately. */
14
+ syncData: Record<string, unknown>;
15
+ }
16
+ /**
17
+ * Parses an NDJSON stream produced by `/_furin/data`.
18
+ *
19
+ * Protocol (v1 — single-line, all-resolved):
20
+ * Line 0 — `JSON.stringify(await toCrossJSONAsync(loaderData))`
21
+ *
22
+ * The CrossJSON node is deserialised with `fromCrossJSON`. Values that are
23
+ * Promises (seroval type 12) land in `deferredPromises`; everything else
24
+ * lands in `syncData`.
25
+ *
26
+ * @param stream - A `ReadableStream<Uint8Array>` from `fetch().body`.
27
+ * @param signal - AbortSignal that, when aborted, cancels the reader and
28
+ * rejects every still-pending deferred promise with an
29
+ * `AbortError`. Pass `undefined` to opt out (no implicit
30
+ * default — see CLAUDE.md).
31
+ */
32
+ export declare function parseDeferredNdjson(stream: ReadableStream<Uint8Array>, signal: AbortSignal | undefined): Promise<DeferredNdjsonResult>;
package/dist/error.d.ts CHANGED
@@ -10,6 +10,20 @@ export interface ErrorProps {
10
10
  * without leaking stack traces to the browser.
11
11
  */
12
12
  digest: string;
13
+ /**
14
+ * HTTP status associated with the error.
15
+ * - For thrown `Response` objects with a 4xx/5xx status: the response's
16
+ * status (e.g. 401, 403, 404, 500).
17
+ * - For all other errors (thrown `Error`, non-Error throws, shell-render
18
+ * crashes): always `500`.
19
+ *
20
+ * Always present so consumers never need a `?? 500` fallback. Note that
21
+ * `prepared.status` is honoured ONLY for loader-time errors. If a Suspense
22
+ * boundary throws AFTER the SSR shell has flushed (i.e. during streaming),
23
+ * the HTTP status cannot be changed and the user-visible status here may
24
+ * differ from the actual response code.
25
+ */
26
+ status: number;
13
27
  };
14
28
  /**
15
29
  * Clears the nearest FurinErrorBoundary's error state, remounting its
package/dist/furin.d.ts CHANGED
@@ -45,6 +45,9 @@ export declare function furin({ pagesDir, logger, }: {
45
45
  standaloneSchema: {};
46
46
  response: {};
47
47
  }>>;
48
+ export { Await, useAsyncError, useAsyncValue } from "./await.tsx";
49
+ export type { DeferredData } from "./client.ts";
50
+ export { defer, isDeferred } from "./client.ts";
48
51
  export type { ErrorComponent, ErrorProps } from "./error.ts";
49
52
  export type { NotFoundComponent, NotFoundOptions, NotFoundProps, } from "./not-found.ts";
50
53
  export { isNotFoundError, notFound } from "./not-found.ts";