@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.
- package/LICENSE +21 -21
- package/README.md +37 -33
- package/dist/adapter/bun.d.ts +17 -1
- package/dist/adapter/package.d.ts +26 -0
- package/dist/build/client.d.ts +1 -1
- package/dist/build/compile-entry.d.ts +3 -3
- package/dist/build/compile-entry.ts +114 -109
- package/dist/build/entry-template.d.ts +35 -9
- package/dist/build/entry-template.ts +206 -156
- package/dist/build/hydrate.d.ts +1 -1
- package/dist/build/index.js +643 -247
- package/dist/build/scan-server.d.ts +10 -4
- package/dist/build/server-routes-entry.d.ts +3 -3
- package/dist/build/server-routes-entry.ts +30 -36
- package/dist/build/ssg-cache.d.ts +1 -1
- package/dist/build/types.d.ts +36 -1
- package/dist/cli/config.d.ts +6 -1
- package/dist/cli/index.js +662 -260
- package/dist/client/link.js +163 -16
- package/dist/client/router/abort.d.ts +1 -0
- package/dist/client/router/sync-catch-up.d.ts +29 -0
- package/dist/config.d.ts +11 -2
- package/dist/config.js +5 -1
- package/dist/furin.d.ts +37 -19
- package/dist/furin.js +982 -378
- package/dist/server/auto-invalidate/registry.d.ts +8 -1
- package/dist/server/cache/dev-loader.d.ts +3 -2
- package/dist/server/cache/invalidation.d.ts +19 -0
- package/dist/server/cache/isr.d.ts +6 -2
- package/dist/server/cache/registry.d.ts +3 -3
- package/dist/server/cache/ssg.d.ts +6 -2
- package/dist/server/cache/store-view.d.ts +11 -0
- package/dist/server/instance.d.ts +86 -0
- package/dist/server/internal.d.ts +16 -1
- package/dist/server/internal.js +72 -5
- package/dist/server/render/template.d.ts +3 -2
- package/dist/server/sync/adapter.d.ts +62 -0
- package/dist/server/sync/config.d.ts +11 -0
- package/dist/server/sync/fingerprint.d.ts +6 -0
- package/dist/server/sync/index.d.ts +2 -2
- package/dist/server/sync/memory-adapter.d.ts +17 -0
- package/dist/server/sync/plugin.d.ts +12 -195
- package/dist/server/sync/response.d.ts +4 -0
- package/dist/server/sync/stream.d.ts +24 -5
- package/dist/shared/prefix.d.ts +15 -0
- package/package.json +6 -1
- package/src/adapter/README.md +13 -13
- package/src/adapter/bun.ts +113 -50
- package/src/adapter/package.ts +206 -0
- package/src/build/client.ts +158 -152
- package/src/build/compile-entry.ts +114 -109
- package/src/build/entry-template.ts +206 -156
- package/src/build/hydrate.ts +342 -340
- package/src/build/index.ts +223 -152
- package/src/build/route-types.ts +145 -145
- package/src/build/scan-server.ts +111 -95
- package/src/build/server-routes-entry.ts +30 -36
- package/src/build/shared.ts +80 -80
- package/src/build/ssg-cache.ts +36 -32
- package/src/build/types.ts +125 -89
- package/src/cli/config.ts +9 -3
- package/src/cli/index.ts +21 -3
- package/src/client/router/abort.ts +5 -0
- package/src/client/router/link-utils.ts +184 -184
- package/src/client/router/provider.tsx +883 -804
- package/src/client/router/sync-catch-up.ts +113 -0
- package/src/client/router/types.ts +276 -276
- package/src/client/sync.ts +98 -98
- package/src/client.ts +369 -369
- package/src/config.ts +14 -1
- package/src/furin.ts +619 -465
- package/src/server/auto-invalidate/registry.ts +23 -1
- package/src/server/auto-invalidate/runtime.ts +159 -134
- package/src/server/cache/dev-loader.ts +77 -38
- package/src/server/cache/invalidation.ts +71 -30
- package/src/server/cache/isr.ts +20 -5
- package/src/server/cache/registry.ts +17 -20
- package/src/server/cache/ssg.ts +19 -4
- package/src/server/cache/store-view.ts +30 -0
- package/src/server/dev-page-plugin.ts +454 -454
- package/src/server/instance.ts +252 -0
- package/src/server/internal.ts +100 -5
- package/src/server/render/assemble.ts +160 -160
- package/src/server/render/isr.ts +323 -323
- package/src/server/render/not-found.ts +9 -3
- package/src/server/render/ssr.ts +603 -585
- package/src/server/render/template.ts +57 -24
- package/src/server/router/plugin.ts +384 -384
- package/src/server/sync/adapter.ts +65 -0
- package/src/server/sync/config.ts +60 -29
- package/src/server/sync/fingerprint.ts +44 -0
- package/src/server/sync/index.ts +15 -14
- package/src/server/sync/memory-adapter.ts +196 -0
- package/src/server/sync/plugin.ts +199 -108
- package/src/server/sync/response.ts +77 -0
- package/src/server/sync/stream.ts +162 -144
- package/src/shared/deferred-ndjson.ts +7 -0
- package/src/shared/prefix.ts +38 -0
|
@@ -1,160 +1,160 @@
|
|
|
1
|
-
import type { toCrossJSON } from "seroval";
|
|
2
|
-
import { getSyncStreamPath } from "../sync/config.ts";
|
|
3
|
-
import type { buildHeadInjection } from "./shell";
|
|
4
|
-
import { safeJson } from "./shell";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Single source of truth for the global registry name used by the SSR
|
|
8
|
-
* streaming script, the client hydration entry, and any test helper. Keeping
|
|
9
|
-
* it centralised here avoids `<script>`/JS source drift if the name is ever
|
|
10
|
-
* renamed.
|
|
11
|
-
*/
|
|
12
|
-
export const FURIN_DEFERRED_GLOBAL = "__FURIN_DEFERRED__";
|
|
13
|
-
|
|
14
|
-
// ── Deferred streaming helpers ─────────────────────────────────────────────────
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Generates the `window.__FURIN_DEFERRED__` registry script that is injected
|
|
18
|
-
* BEFORE the React stream starts. It sets up the resolve/reject/getPromise API
|
|
19
|
-
* for late chunks.
|
|
20
|
-
*
|
|
21
|
-
* Client-side `<Await>` components call `getPromise(key)` to obtain a Promise
|
|
22
|
-
* they can pass to React 19's `use()`. The late resolution scripts call
|
|
23
|
-
* `resolve(key, chunk)` / `reject(key, chunk)` to settle those Promises.
|
|
24
|
-
*
|
|
25
|
-
* Synchronous loader data is NOT carried here — it lives exclusively in the
|
|
26
|
-
* `__FURIN_DATA__` JSON script that the hydrate entry reads.
|
|
27
|
-
*
|
|
28
|
-
* CSP note: this is an inline `<script>` — nonce support is a v2 concern.
|
|
29
|
-
*/
|
|
30
|
-
export function buildDeferredScript(deferredKeys: string[]): string {
|
|
31
|
-
const keysJson = safeJson(deferredKeys);
|
|
32
|
-
return `<script id="${FURIN_DEFERRED_GLOBAL}">
|
|
33
|
-
window.${FURIN_DEFERRED_GLOBAL} = {
|
|
34
|
-
_deferredKeys: ${keysJson},
|
|
35
|
-
_chunks: {},
|
|
36
|
-
_resolvers: {},
|
|
37
|
-
resolve(key, chunk) {
|
|
38
|
-
this._chunks[key] = { a: 0, v: chunk };
|
|
39
|
-
},
|
|
40
|
-
reject(key, chunk) {
|
|
41
|
-
this._chunks[key] = { a: 1, v: chunk };
|
|
42
|
-
},
|
|
43
|
-
getPromise(key) {
|
|
44
|
-
if (!this._resolvers[key]) {
|
|
45
|
-
var res, rej;
|
|
46
|
-
var p = new Promise(function(resolve, reject) { res = resolve; rej = reject; });
|
|
47
|
-
this._resolvers[key] = { resolve: res, reject: rej, promise: p };
|
|
48
|
-
}
|
|
49
|
-
return this._resolvers[key].promise;
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
</script>`;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Generates a late `<script>` that settles one deferred Promise on the client.
|
|
57
|
-
* The `chunk` must be a seroval CrossJSON value produced by `toCrossJSON(value)`.
|
|
58
|
-
*
|
|
59
|
-
* @param key - The Promise key (matches what `getPromise(key)` was called with)
|
|
60
|
-
* @param chunk - The already-serialised CrossJSON value
|
|
61
|
-
* @param action - "resolve" or "reject"
|
|
62
|
-
*/
|
|
63
|
-
export function buildDeferredResolution(
|
|
64
|
-
key: string,
|
|
65
|
-
chunk: ReturnType<typeof toCrossJSON>,
|
|
66
|
-
action: "resolve" | "reject"
|
|
67
|
-
): string {
|
|
68
|
-
const chunkJson = safeJson(chunk);
|
|
69
|
-
return `<script>window.${FURIN_DEFERRED_GLOBAL}.${action}(${safeJson(key)},${chunkJson})</script>`;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Minimal context passed to background / synthetic render helpers — only `request` is needed. */
|
|
73
|
-
export interface LoaderContext {
|
|
74
|
-
request: Request;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function resolvePath(pattern: string, params: Record<string, string>): string {
|
|
78
|
-
let path = pattern;
|
|
79
|
-
for (const [key, val] of Object.entries(params ?? {})) {
|
|
80
|
-
path = path.replace(key === "*" ? "*" : `:${key}`, val);
|
|
81
|
-
}
|
|
82
|
-
return path;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export async function streamToString(stream: ReadableStream): Promise<string> {
|
|
86
|
-
const reader = stream.getReader();
|
|
87
|
-
const decoder = new TextDecoder();
|
|
88
|
-
let html = "";
|
|
89
|
-
|
|
90
|
-
for (;;) {
|
|
91
|
-
const { done, value } = await reader.read();
|
|
92
|
-
if (done) {
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
html += decoder.decode(value, { stream: true });
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
html += decoder.decode();
|
|
99
|
-
return html;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
interface SplitTemplate {
|
|
103
|
-
bodyPost: string;
|
|
104
|
-
bodyPre: string;
|
|
105
|
-
headPre: string;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function splitTemplate(template: string): SplitTemplate {
|
|
109
|
-
const [headPre, afterHead = ""] = template.split("<!--ssr-head-->");
|
|
110
|
-
const [bodyPre, bodyPost = ""] = afterHead.split("<!--ssr-outlet-->");
|
|
111
|
-
return { headPre, bodyPre, bodyPost } as SplitTemplate;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function assembleHTML(
|
|
115
|
-
template: string,
|
|
116
|
-
headData: ReturnType<typeof buildHeadInjection>,
|
|
117
|
-
reactHtml: string,
|
|
118
|
-
data: Record<string, unknown> | undefined
|
|
119
|
-
): string {
|
|
120
|
-
const { headPre, bodyPre, bodyPost } = splitTemplate(template);
|
|
121
|
-
|
|
122
|
-
const dataScript = data
|
|
123
|
-
? `<script id="__FURIN_DATA__" type="application/json">${safeJson(data)}</script>`
|
|
124
|
-
: "";
|
|
125
|
-
const syncScript = buildSyncRuntimeScript();
|
|
126
|
-
const runtimeScripts = `${syncScript}${dataScript}`;
|
|
127
|
-
|
|
128
|
-
let injectedBodyPost = bodyPost;
|
|
129
|
-
if (runtimeScripts) {
|
|
130
|
-
injectedBodyPost = bodyPost.includes("</body>")
|
|
131
|
-
? bodyPost.replace("</body>", `${runtimeScripts}</body>`)
|
|
132
|
-
: bodyPost + runtimeScripts;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return headPre + headData + bodyPre + reactHtml + injectedBodyPost;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function buildSyncRuntimeScript(): string {
|
|
139
|
-
const syncStreamPath = getSyncStreamPath();
|
|
140
|
-
if (!syncStreamPath) {
|
|
141
|
-
return "";
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return `<script id="__FURIN_SYNC__" type="application/json">${safeJson({ stream: syncStreamPath })}</script>`;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export function injectSyncRuntimeScript(html: string): string {
|
|
148
|
-
if (html.includes('id="__FURIN_SYNC__"')) {
|
|
149
|
-
return html;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const syncScript = buildSyncRuntimeScript();
|
|
153
|
-
if (!syncScript) {
|
|
154
|
-
return html;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return html.includes("</body>")
|
|
158
|
-
? html.replace("</body>", `${syncScript}</body>`)
|
|
159
|
-
: html + syncScript;
|
|
160
|
-
}
|
|
1
|
+
import type { toCrossJSON } from "seroval";
|
|
2
|
+
import { getSyncStreamPath } from "../sync/config.ts";
|
|
3
|
+
import type { buildHeadInjection } from "./shell";
|
|
4
|
+
import { safeJson } from "./shell";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Single source of truth for the global registry name used by the SSR
|
|
8
|
+
* streaming script, the client hydration entry, and any test helper. Keeping
|
|
9
|
+
* it centralised here avoids `<script>`/JS source drift if the name is ever
|
|
10
|
+
* renamed.
|
|
11
|
+
*/
|
|
12
|
+
export const FURIN_DEFERRED_GLOBAL = "__FURIN_DEFERRED__";
|
|
13
|
+
|
|
14
|
+
// ── Deferred streaming helpers ─────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Generates the `window.__FURIN_DEFERRED__` registry script that is injected
|
|
18
|
+
* BEFORE the React stream starts. It sets up the resolve/reject/getPromise API
|
|
19
|
+
* for late chunks.
|
|
20
|
+
*
|
|
21
|
+
* Client-side `<Await>` components call `getPromise(key)` to obtain a Promise
|
|
22
|
+
* they can pass to React 19's `use()`. The late resolution scripts call
|
|
23
|
+
* `resolve(key, chunk)` / `reject(key, chunk)` to settle those Promises.
|
|
24
|
+
*
|
|
25
|
+
* Synchronous loader data is NOT carried here — it lives exclusively in the
|
|
26
|
+
* `__FURIN_DATA__` JSON script that the hydrate entry reads.
|
|
27
|
+
*
|
|
28
|
+
* CSP note: this is an inline `<script>` — nonce support is a v2 concern.
|
|
29
|
+
*/
|
|
30
|
+
export function buildDeferredScript(deferredKeys: string[]): string {
|
|
31
|
+
const keysJson = safeJson(deferredKeys);
|
|
32
|
+
return `<script id="${FURIN_DEFERRED_GLOBAL}">
|
|
33
|
+
window.${FURIN_DEFERRED_GLOBAL} = {
|
|
34
|
+
_deferredKeys: ${keysJson},
|
|
35
|
+
_chunks: {},
|
|
36
|
+
_resolvers: {},
|
|
37
|
+
resolve(key, chunk) {
|
|
38
|
+
this._chunks[key] = { a: 0, v: chunk };
|
|
39
|
+
},
|
|
40
|
+
reject(key, chunk) {
|
|
41
|
+
this._chunks[key] = { a: 1, v: chunk };
|
|
42
|
+
},
|
|
43
|
+
getPromise(key) {
|
|
44
|
+
if (!this._resolvers[key]) {
|
|
45
|
+
var res, rej;
|
|
46
|
+
var p = new Promise(function(resolve, reject) { res = resolve; rej = reject; });
|
|
47
|
+
this._resolvers[key] = { resolve: res, reject: rej, promise: p };
|
|
48
|
+
}
|
|
49
|
+
return this._resolvers[key].promise;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
</script>`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Generates a late `<script>` that settles one deferred Promise on the client.
|
|
57
|
+
* The `chunk` must be a seroval CrossJSON value produced by `toCrossJSON(value)`.
|
|
58
|
+
*
|
|
59
|
+
* @param key - The Promise key (matches what `getPromise(key)` was called with)
|
|
60
|
+
* @param chunk - The already-serialised CrossJSON value
|
|
61
|
+
* @param action - "resolve" or "reject"
|
|
62
|
+
*/
|
|
63
|
+
export function buildDeferredResolution(
|
|
64
|
+
key: string,
|
|
65
|
+
chunk: ReturnType<typeof toCrossJSON>,
|
|
66
|
+
action: "resolve" | "reject"
|
|
67
|
+
): string {
|
|
68
|
+
const chunkJson = safeJson(chunk);
|
|
69
|
+
return `<script>window.${FURIN_DEFERRED_GLOBAL}.${action}(${safeJson(key)},${chunkJson})</script>`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Minimal context passed to background / synthetic render helpers — only `request` is needed. */
|
|
73
|
+
export interface LoaderContext {
|
|
74
|
+
request: Request;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function resolvePath(pattern: string, params: Record<string, string>): string {
|
|
78
|
+
let path = pattern;
|
|
79
|
+
for (const [key, val] of Object.entries(params ?? {})) {
|
|
80
|
+
path = path.replace(key === "*" ? "*" : `:${key}`, val);
|
|
81
|
+
}
|
|
82
|
+
return path;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function streamToString(stream: ReadableStream): Promise<string> {
|
|
86
|
+
const reader = stream.getReader();
|
|
87
|
+
const decoder = new TextDecoder();
|
|
88
|
+
let html = "";
|
|
89
|
+
|
|
90
|
+
for (;;) {
|
|
91
|
+
const { done, value } = await reader.read();
|
|
92
|
+
if (done) {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
html += decoder.decode(value, { stream: true });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
html += decoder.decode();
|
|
99
|
+
return html;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
interface SplitTemplate {
|
|
103
|
+
bodyPost: string;
|
|
104
|
+
bodyPre: string;
|
|
105
|
+
headPre: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function splitTemplate(template: string): SplitTemplate {
|
|
109
|
+
const [headPre, afterHead = ""] = template.split("<!--ssr-head-->");
|
|
110
|
+
const [bodyPre, bodyPost = ""] = afterHead.split("<!--ssr-outlet-->");
|
|
111
|
+
return { headPre, bodyPre, bodyPost } as SplitTemplate;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function assembleHTML(
|
|
115
|
+
template: string,
|
|
116
|
+
headData: ReturnType<typeof buildHeadInjection>,
|
|
117
|
+
reactHtml: string,
|
|
118
|
+
data: Record<string, unknown> | undefined
|
|
119
|
+
): string {
|
|
120
|
+
const { headPre, bodyPre, bodyPost } = splitTemplate(template);
|
|
121
|
+
|
|
122
|
+
const dataScript = data
|
|
123
|
+
? `<script id="__FURIN_DATA__" type="application/json">${safeJson(data)}</script>`
|
|
124
|
+
: "";
|
|
125
|
+
const syncScript = buildSyncRuntimeScript();
|
|
126
|
+
const runtimeScripts = `${syncScript}${dataScript}`;
|
|
127
|
+
|
|
128
|
+
let injectedBodyPost = bodyPost;
|
|
129
|
+
if (runtimeScripts) {
|
|
130
|
+
injectedBodyPost = bodyPost.includes("</body>")
|
|
131
|
+
? bodyPost.replace("</body>", `${runtimeScripts}</body>`)
|
|
132
|
+
: bodyPost + runtimeScripts;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return headPre + headData + bodyPre + reactHtml + injectedBodyPost;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function buildSyncRuntimeScript(): string {
|
|
139
|
+
const syncStreamPath = getSyncStreamPath();
|
|
140
|
+
if (!syncStreamPath) {
|
|
141
|
+
return "";
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return `<script id="__FURIN_SYNC__" type="application/json">${safeJson({ stream: syncStreamPath })}</script>`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function injectSyncRuntimeScript(html: string): string {
|
|
148
|
+
if (html.includes('id="__FURIN_SYNC__"')) {
|
|
149
|
+
return html;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const syncScript = buildSyncRuntimeScript();
|
|
153
|
+
if (!syncScript) {
|
|
154
|
+
return html;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return html.includes("</body>")
|
|
158
|
+
? html.replace("</body>", `${syncScript}</body>`)
|
|
159
|
+
: html + syncScript;
|
|
160
|
+
}
|