@teyik0/furin 0.2.0-alpha.3 → 0.2.0-alpha.5

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 (98) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +37 -33
  3. package/dist/adapter/bun.d.ts +17 -1
  4. package/dist/adapter/package.d.ts +26 -0
  5. package/dist/build/client.d.ts +1 -1
  6. package/dist/build/compile-entry.d.ts +3 -3
  7. package/dist/build/compile-entry.ts +114 -109
  8. package/dist/build/entry-template.d.ts +35 -9
  9. package/dist/build/entry-template.ts +206 -156
  10. package/dist/build/hydrate.d.ts +1 -1
  11. package/dist/build/index.js +643 -247
  12. package/dist/build/scan-server.d.ts +10 -4
  13. package/dist/build/server-routes-entry.d.ts +3 -3
  14. package/dist/build/server-routes-entry.ts +30 -36
  15. package/dist/build/ssg-cache.d.ts +1 -1
  16. package/dist/build/types.d.ts +36 -1
  17. package/dist/cli/config.d.ts +6 -1
  18. package/dist/cli/index.js +662 -260
  19. package/dist/client/link.js +163 -16
  20. package/dist/client/router/abort.d.ts +1 -0
  21. package/dist/client/router/sync-catch-up.d.ts +29 -0
  22. package/dist/config.d.ts +11 -2
  23. package/dist/config.js +5 -1
  24. package/dist/furin.d.ts +37 -19
  25. package/dist/furin.js +982 -378
  26. package/dist/server/auto-invalidate/registry.d.ts +8 -1
  27. package/dist/server/cache/dev-loader.d.ts +3 -2
  28. package/dist/server/cache/invalidation.d.ts +19 -0
  29. package/dist/server/cache/isr.d.ts +6 -2
  30. package/dist/server/cache/registry.d.ts +3 -3
  31. package/dist/server/cache/ssg.d.ts +6 -2
  32. package/dist/server/cache/store-view.d.ts +11 -0
  33. package/dist/server/instance.d.ts +86 -0
  34. package/dist/server/internal.d.ts +16 -1
  35. package/dist/server/internal.js +72 -5
  36. package/dist/server/render/template.d.ts +3 -2
  37. package/dist/server/sync/adapter.d.ts +62 -0
  38. package/dist/server/sync/config.d.ts +11 -0
  39. package/dist/server/sync/fingerprint.d.ts +6 -0
  40. package/dist/server/sync/index.d.ts +2 -2
  41. package/dist/server/sync/memory-adapter.d.ts +17 -0
  42. package/dist/server/sync/plugin.d.ts +12 -195
  43. package/dist/server/sync/response.d.ts +4 -0
  44. package/dist/server/sync/stream.d.ts +24 -5
  45. package/dist/shared/prefix.d.ts +15 -0
  46. package/package.json +6 -1
  47. package/src/adapter/README.md +13 -13
  48. package/src/adapter/bun.ts +113 -50
  49. package/src/adapter/package.ts +206 -0
  50. package/src/build/client.ts +158 -152
  51. package/src/build/compile-entry.ts +114 -109
  52. package/src/build/entry-template.ts +206 -156
  53. package/src/build/hydrate.ts +342 -340
  54. package/src/build/index.ts +223 -152
  55. package/src/build/route-types.ts +145 -145
  56. package/src/build/scan-server.ts +111 -95
  57. package/src/build/server-routes-entry.ts +30 -36
  58. package/src/build/shared.ts +80 -80
  59. package/src/build/ssg-cache.ts +36 -32
  60. package/src/build/types.ts +125 -89
  61. package/src/cli/config.ts +9 -3
  62. package/src/cli/index.ts +21 -3
  63. package/src/client/router/abort.ts +5 -0
  64. package/src/client/router/link-utils.ts +184 -184
  65. package/src/client/router/provider.tsx +883 -804
  66. package/src/client/router/sync-catch-up.ts +113 -0
  67. package/src/client/router/types.ts +276 -276
  68. package/src/client/sync.ts +98 -98
  69. package/src/client.ts +369 -369
  70. package/src/config.ts +14 -1
  71. package/src/furin.ts +619 -465
  72. package/src/server/auto-invalidate/registry.ts +23 -1
  73. package/src/server/auto-invalidate/runtime.ts +159 -134
  74. package/src/server/cache/dev-loader.ts +77 -38
  75. package/src/server/cache/invalidation.ts +71 -30
  76. package/src/server/cache/isr.ts +20 -5
  77. package/src/server/cache/registry.ts +17 -20
  78. package/src/server/cache/ssg.ts +19 -4
  79. package/src/server/cache/store-view.ts +30 -0
  80. package/src/server/dev-page-plugin.ts +454 -454
  81. package/src/server/instance.ts +252 -0
  82. package/src/server/internal.ts +100 -5
  83. package/src/server/render/assemble.ts +160 -160
  84. package/src/server/render/isr.ts +323 -323
  85. package/src/server/render/not-found.ts +9 -3
  86. package/src/server/render/ssr.ts +603 -585
  87. package/src/server/render/template.ts +57 -24
  88. package/src/server/router/plugin.ts +384 -384
  89. package/src/server/sync/adapter.ts +65 -0
  90. package/src/server/sync/config.ts +60 -29
  91. package/src/server/sync/fingerprint.ts +44 -0
  92. package/src/server/sync/index.ts +15 -14
  93. package/src/server/sync/memory-adapter.ts +196 -0
  94. package/src/server/sync/plugin.ts +199 -108
  95. package/src/server/sync/response.ts +77 -0
  96. package/src/server/sync/stream.ts +162 -144
  97. package/src/shared/deferred-ndjson.ts +7 -0
  98. package/src/shared/prefix.ts +38 -0
@@ -1,156 +1,206 @@
1
- import { existsSync } from "node:fs";
2
- import { dirname, join } from "node:path";
3
- import { fileURLToPath } from "node:url";
4
- import type { SsgCacheEntry } from "../server/cache/index.ts";
5
-
6
- // import.meta.resolve() runs at runtime (not inlined at bundle time), resolves
7
- // through package exports, and is the Web-standard API. The main entry is
8
- // `src/furin.ts` (or `dist/furin.js`), so we strip two path segments to reach
9
- // the package root.
10
- const _pkgRoot = dirname(dirname(fileURLToPath(import.meta.resolve("@teyik0/furin"))));
11
- const _pkgSrcDirRaw = existsSync(join(_pkgRoot, "src", "furin.ts"))
12
- ? join(_pkgRoot, "src")
13
- : join(_pkgRoot, "dist");
14
- // Normalize to forward slashes so endsWith checks and template paths work on Windows.
15
- const _pkgSrcDir = _pkgSrcDirRaw.replace(/\\/g, "/");
16
- const _ext = _pkgSrcDir.endsWith("/src") ? ".ts" : ".js";
17
- const INTERNAL_MODULE_PATH = `${_pkgSrcDir}/server/internal${_ext}`;
18
- const RUNTIME_ENV_MODULE_PATH = `${_pkgSrcDir}/server/runtime-env${_ext}`;
19
-
20
- export interface EntryTemplateOptions {
21
- buildId?: string;
22
- extraContext?: string[];
23
- extraImports?: string[];
24
- headerComment: string;
25
- rootConventions?: { errorPath?: string; notFoundPath?: string };
26
- rootPath: string;
27
- routeMetadata?: Record<
28
- string,
29
- {
30
- segmentBoundaries: Array<{
31
- depth: number;
32
- path: string;
33
- errorPath?: string;
34
- notFoundPath?: string;
35
- }>;
36
- }
37
- >;
38
- routes: Array<{ mode: "ssr" | "ssg" | "isr"; path: string; pattern: string }>;
39
- serverEntry: string;
40
- ssgCache?: Record<string, SsgCacheEntry>;
41
- }
42
-
43
- /** Unified options for generating a build entry (compile or disk-based). */
44
- export interface BuildEntryOptions extends Omit<EntryTemplateOptions, "headerComment"> {
45
- outDir: string;
46
- headerComment?: string;
47
- /** Embed mode: bundles client assets into the binary via `with { type: "file" }`. */
48
- embed?: { clientDir: string };
49
- publicDir?: string;
50
- }
51
-
52
- function collectConventionPaths(
53
- rootConventions: EntryTemplateOptions["rootConventions"],
54
- routeMetadata: EntryTemplateOptions["routeMetadata"]
55
- ): string[] {
56
- const paths: string[] = [];
57
- if (rootConventions?.errorPath) {
58
- paths.push(rootConventions.errorPath);
59
- }
60
- if (rootConventions?.notFoundPath) {
61
- paths.push(rootConventions.notFoundPath);
62
- }
63
- if (routeMetadata) {
64
- for (const meta of Object.values(routeMetadata)) {
65
- for (const seg of meta.segmentBoundaries) {
66
- if (seg.errorPath) {
67
- paths.push(seg.errorPath);
68
- }
69
- if (seg.notFoundPath) {
70
- paths.push(seg.notFoundPath);
71
- }
72
- }
73
- }
74
- }
75
- return [...new Set(paths)];
76
- }
77
-
78
- export function buildEntrySource(options: EntryTemplateOptions): string {
79
- const {
80
- buildId,
81
- headerComment,
82
- rootPath,
83
- routes,
84
- serverEntry,
85
- rootConventions,
86
- routeMetadata,
87
- ssgCache,
88
- } = options;
89
- let { extraImports, extraContext } = options;
90
- if (extraImports === undefined) {
91
- extraImports = [];
92
- }
93
- if (extraContext === undefined) {
94
- extraContext = [];
95
- }
96
-
97
- const allModulePaths = [
98
- rootPath,
99
- ...routes.map((r) => r.path),
100
- ...collectConventionPaths(rootConventions, routeMetadata),
101
- ];
102
- const moduleImports: string[] = [];
103
- const moduleEntries: string[] = [];
104
-
105
- for (let i = 0; i < allModulePaths.length; i++) {
106
- const absPath = (allModulePaths[i] as string).replace(/\\/g, "/");
107
- const varName = `_mod${i}`;
108
- moduleImports.push(`import * as ${varName} from ${JSON.stringify(absPath)};`);
109
- moduleEntries.push(` ${JSON.stringify(absPath)}: ${varName},`);
110
- }
111
-
112
- const routeEntries = routes.map(
113
- (r) =>
114
- ` { pattern: ${JSON.stringify(r.pattern)}, path: ${JSON.stringify(r.path.replace(/\\/g, "/"))}, mode: ${JSON.stringify(r.mode)} },`
115
- );
116
-
117
- const rootConventionsLine = rootConventions
118
- ? ` rootConventions: ${JSON.stringify(rootConventions)},`
119
- : "";
120
- const routeMetadataLine = routeMetadata
121
- ? ` routeMetadata: ${JSON.stringify(routeMetadata)},`
122
- : "";
123
- const ssgCacheLine = ssgCache ? ` ssgCache: ${JSON.stringify(ssgCache)},` : "";
124
-
125
- const lines = [
126
- headerComment,
127
- `import { __setCompileContext } from ${JSON.stringify(INTERNAL_MODULE_PATH)};`,
128
- `import { __setDevMode } from ${JSON.stringify(RUNTIME_ENV_MODULE_PATH)};`,
129
- ...moduleImports,
130
- ...(extraImports.length > 0 ? ["", ...extraImports] : []),
131
- "",
132
- "// Force production mode — Bun may inline process.env.NODE_ENV at bundle time.",
133
- "__setDevMode(false);",
134
- 'process.env.NODE_ENV = "production";',
135
- "",
136
- "__setCompileContext({",
137
- ` buildId: ${JSON.stringify(buildId ?? "")},`,
138
- ` rootPath: ${JSON.stringify(rootPath.replace(/\\/g, "/"))},`,
139
- rootConventionsLine,
140
- " modules: {",
141
- ...moduleEntries,
142
- " },",
143
- " routes: [",
144
- ...routeEntries,
145
- " ],",
146
- routeMetadataLine,
147
- ssgCacheLine,
148
- ...extraContext,
149
- "});",
150
- "",
151
- `await import(${JSON.stringify(serverEntry.replace(/\\/g, "/"))});`,
152
- "",
153
- ];
154
-
155
- return lines.join("\n");
156
- }
1
+ import { existsSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import type { SsgCacheEntry } from "../server/cache/index.ts";
5
+
6
+ // import.meta.resolve() runs at runtime (not inlined at bundle time), resolves
7
+ // through package exports, and is the Web-standard API. The main entry is
8
+ // `src/furin.ts` (or `dist/furin.js`), so we strip two path segments to reach
9
+ // the package root.
10
+ const _pkgRoot = dirname(dirname(fileURLToPath(import.meta.resolve("@teyik0/furin"))));
11
+ const _pkgSrcDirRaw = existsSync(join(_pkgRoot, "src", "furin.ts"))
12
+ ? join(_pkgRoot, "src")
13
+ : join(_pkgRoot, "dist");
14
+ // Normalize to forward slashes so endsWith checks and template paths work on Windows.
15
+ const _pkgSrcDir = _pkgSrcDirRaw.replace(/\\/g, "/");
16
+ const _ext = _pkgSrcDir.endsWith("/src") ? ".ts" : ".js";
17
+ const INTERNAL_MODULE_PATH = `${_pkgSrcDir}/server/internal${_ext}`;
18
+ const RUNTIME_ENV_MODULE_PATH = `${_pkgSrcDir}/server/runtime-env${_ext}`;
19
+
20
+ /** One app's compile context payload — the generated entry can carry several. */
21
+ export interface EntryAppContext {
22
+ buildId?: string;
23
+ /** Extra lines injected inside this app's `__setCompileContext({...})` call. */
24
+ extraContext?: string[];
25
+ /** Extra import lines this app needs (embedded asset imports). */
26
+ extraImports?: string[];
27
+ /** Mount prefix baked into the context for runtime lookup (`""` = root). */
28
+ prefix?: string;
29
+ rootConventions?: { errorPath?: string; notFoundPath?: string };
30
+ rootPath: string;
31
+ routeMetadata?: Record<
32
+ string,
33
+ {
34
+ segmentBoundaries: Array<{
35
+ depth: number;
36
+ path: string;
37
+ errorPath?: string;
38
+ notFoundPath?: string;
39
+ }>;
40
+ }
41
+ >;
42
+ routes: Array<{ mode: "ssr" | "ssg" | "isr"; path: string; pattern: string }>;
43
+ ssgCache?: Record<string, SsgCacheEntry>;
44
+ }
45
+
46
+ export interface EntryTemplateOptions {
47
+ apps: EntryAppContext[];
48
+ headerComment: string;
49
+ /**
50
+ * - "boot" (default): forces production mode and dynamically imports the
51
+ * server entry after registering contexts — the whole app's entrypoint.
52
+ * - "register": ONLY registers contexts. Emitted by `--target package` as a
53
+ * side-effect module the HOST app imports; it must not touch dev mode or
54
+ * NODE_ENV (the host decides), and it imports furin internals via the
55
+ * package specifier so the host and the register module share ONE copy of
56
+ * the context registry (`packages: "external"` keeps it unbundled).
57
+ */
58
+ mode?: "boot" | "register";
59
+ /**
60
+ * Server entry to boot after context registration. Omitted in package
61
+ * (register-module) mode where the entry only registers contexts.
62
+ */
63
+ serverEntry?: string;
64
+ }
65
+
66
+ /** Unified options for generating a build entry (compile or disk-based). */
67
+ export interface BuildEntryOptions {
68
+ apps: Array<
69
+ EntryAppContext & {
70
+ /** Embed mode: bundles this app's client assets via `with { type: "file" }`. */
71
+ embed?: { clientDir: string };
72
+ }
73
+ >;
74
+ headerComment?: string;
75
+ outDir: string;
76
+ /** Project-level public/ dir — embedded into EVERY app's assets (keys only; Bun dedupes the file payloads). */
77
+ publicDir?: string;
78
+ serverEntry?: string;
79
+ }
80
+
81
+ function collectConventionPaths(
82
+ rootConventions: EntryAppContext["rootConventions"],
83
+ routeMetadata: EntryAppContext["routeMetadata"]
84
+ ): string[] {
85
+ const paths: string[] = [];
86
+ if (rootConventions?.errorPath) {
87
+ paths.push(rootConventions.errorPath);
88
+ }
89
+ if (rootConventions?.notFoundPath) {
90
+ paths.push(rootConventions.notFoundPath);
91
+ }
92
+ if (routeMetadata) {
93
+ for (const meta of Object.values(routeMetadata)) {
94
+ for (const seg of meta.segmentBoundaries) {
95
+ if (seg.errorPath) {
96
+ paths.push(seg.errorPath);
97
+ }
98
+ if (seg.notFoundPath) {
99
+ paths.push(seg.notFoundPath);
100
+ }
101
+ }
102
+ }
103
+ }
104
+ return [...new Set(paths)];
105
+ }
106
+
107
+ /** Emits one app's module imports + `__setCompileContext({...})` call. */
108
+ function buildAppContextBlock(
109
+ app: EntryAppContext,
110
+ varPrefix: string
111
+ ): { contextLines: string[]; importLines: string[] } {
112
+ const allModulePaths = [
113
+ app.rootPath,
114
+ ...app.routes.map((r) => r.path),
115
+ ...collectConventionPaths(app.rootConventions, app.routeMetadata),
116
+ ];
117
+ const importLines: string[] = [];
118
+ const moduleEntries: string[] = [];
119
+
120
+ for (let i = 0; i < allModulePaths.length; i++) {
121
+ const absPath = (allModulePaths[i] as string).replace(/\\/g, "/");
122
+ const varName = `${varPrefix}mod${i}`;
123
+ importLines.push(`import * as ${varName} from ${JSON.stringify(absPath)};`);
124
+ moduleEntries.push(` ${JSON.stringify(absPath)}: ${varName},`);
125
+ }
126
+
127
+ const routeEntries = app.routes.map(
128
+ (r) =>
129
+ ` { pattern: ${JSON.stringify(r.pattern)}, path: ${JSON.stringify(r.path.replace(/\\/g, "/"))}, mode: ${JSON.stringify(r.mode)} },`
130
+ );
131
+
132
+ const rootConventionsLine = app.rootConventions
133
+ ? ` rootConventions: ${JSON.stringify(app.rootConventions)},`
134
+ : "";
135
+ const routeMetadataLine = app.routeMetadata
136
+ ? ` routeMetadata: ${JSON.stringify(app.routeMetadata)},`
137
+ : "";
138
+ const ssgCacheLine = app.ssgCache ? ` ssgCache: ${JSON.stringify(app.ssgCache)},` : "";
139
+
140
+ const contextLines = [
141
+ "__setCompileContext({",
142
+ ` buildId: ${JSON.stringify(app.buildId ?? "")},`,
143
+ ` prefix: ${JSON.stringify(app.prefix ?? "")},`,
144
+ ` rootPath: ${JSON.stringify(app.rootPath.replace(/\\/g, "/"))},`,
145
+ rootConventionsLine,
146
+ " modules: {",
147
+ ...moduleEntries,
148
+ " },",
149
+ " routes: [",
150
+ ...routeEntries,
151
+ " ],",
152
+ routeMetadataLine,
153
+ ssgCacheLine,
154
+ ...(app.extraContext ?? []),
155
+ "});",
156
+ ];
157
+
158
+ return { contextLines, importLines };
159
+ }
160
+
161
+ export function buildEntrySource(options: EntryTemplateOptions): string {
162
+ const { apps, headerComment, serverEntry } = options;
163
+ const mode = options.mode ?? "boot";
164
+ const internalSpecifier =
165
+ mode === "register" ? "@teyik0/furin/internal" : INTERNAL_MODULE_PATH;
166
+
167
+ const importLines: string[] = [];
168
+ const contextBlocks: string[] = [];
169
+
170
+ for (let appIndex = 0; appIndex < apps.length; appIndex++) {
171
+ const app = apps[appIndex] as EntryAppContext;
172
+ // Per-app variable namespace so several apps' module/asset imports never
173
+ // collide inside the single generated entry.
174
+ const varPrefix = apps.length === 1 ? "_" : `_a${appIndex}_`;
175
+ const block = buildAppContextBlock(app, varPrefix);
176
+ importLines.push(...block.importLines);
177
+ if (app.extraImports && app.extraImports.length > 0) {
178
+ importLines.push("", ...app.extraImports);
179
+ }
180
+ contextBlocks.push("", ...block.contextLines);
181
+ }
182
+
183
+ const lines = [
184
+ headerComment,
185
+ `import { __setCompileContext } from ${JSON.stringify(internalSpecifier)};`,
186
+ ...(mode === "boot"
187
+ ? [`import { __setDevMode } from ${JSON.stringify(RUNTIME_ENV_MODULE_PATH)};`]
188
+ : []),
189
+ ...importLines,
190
+ ...(mode === "boot"
191
+ ? [
192
+ "",
193
+ "// Force production mode — Bun may inline process.env.NODE_ENV at bundle time.",
194
+ "__setDevMode(false);",
195
+ 'process.env.NODE_ENV = "production";',
196
+ ]
197
+ : []),
198
+ ...contextBlocks,
199
+ "",
200
+ ...(serverEntry
201
+ ? [`await import(${JSON.stringify(serverEntry.replace(/\\/g, "/"))});`, ""]
202
+ : []),
203
+ ];
204
+
205
+ return lines.join("\n");
206
+ }
@@ -21,4 +21,4 @@ export declare function generateHydrateEntry(routes: ResolvedRoute[], rootLayout
21
21
  * Only rewrites a file when its content has actually changed so Bun's --hot
22
22
  * watcher does not trigger a spurious reload on every server restart.
23
23
  */
24
- export declare function writeDevFiles(routes: ResolvedRoute[], { outDir, rootLayout, basePath, clientLogging }: BuildClientOptions, projectRoot: string): void;
24
+ export declare function writeDevFiles(routes: ResolvedRoute[], { outDir, rootLayout, basePath, clientLogging, skipRouteTypes }: BuildClientOptions, projectRoot: string): void;