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

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 (54) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +37 -33
  3. package/dist/build/compile-entry.ts +109 -109
  4. package/dist/build/entry-template.ts +156 -156
  5. package/dist/build/server-routes-entry.ts +36 -36
  6. package/dist/cli/index.js +0 -0
  7. package/dist/client/link.js +163 -16
  8. package/dist/client/router/abort.d.ts +1 -0
  9. package/dist/client/router/sync-catch-up.d.ts +29 -0
  10. package/dist/furin.d.ts +1 -1
  11. package/dist/furin.js +499 -215
  12. package/dist/server/sync/adapter.d.ts +62 -0
  13. package/dist/server/sync/fingerprint.d.ts +6 -0
  14. package/dist/server/sync/index.d.ts +2 -2
  15. package/dist/server/sync/memory-adapter.d.ts +17 -0
  16. package/dist/server/sync/plugin.d.ts +12 -195
  17. package/dist/server/sync/response.d.ts +4 -0
  18. package/dist/server/sync/stream.d.ts +16 -4
  19. package/package.json +1 -1
  20. package/src/adapter/README.md +13 -13
  21. package/src/build/client.ts +152 -152
  22. package/src/build/compile-entry.ts +109 -109
  23. package/src/build/entry-template.ts +156 -156
  24. package/src/build/hydrate.ts +340 -340
  25. package/src/build/index.ts +152 -152
  26. package/src/build/route-types.ts +145 -145
  27. package/src/build/scan-server.ts +95 -95
  28. package/src/build/server-routes-entry.ts +36 -36
  29. package/src/build/shared.ts +80 -80
  30. package/src/build/ssg-cache.ts +32 -32
  31. package/src/build/types.ts +89 -89
  32. package/src/client/router/abort.ts +5 -0
  33. package/src/client/router/link-utils.ts +184 -184
  34. package/src/client/router/provider.tsx +883 -804
  35. package/src/client/router/sync-catch-up.ts +113 -0
  36. package/src/client/router/types.ts +276 -276
  37. package/src/client/sync.ts +98 -98
  38. package/src/client.ts +369 -369
  39. package/src/furin.ts +465 -465
  40. package/src/server/auto-invalidate/runtime.ts +134 -134
  41. package/src/server/dev-page-plugin.ts +454 -454
  42. package/src/server/render/assemble.ts +160 -160
  43. package/src/server/render/isr.ts +323 -323
  44. package/src/server/render/ssr.ts +591 -585
  45. package/src/server/router/plugin.ts +384 -384
  46. package/src/server/sync/adapter.ts +65 -0
  47. package/src/server/sync/config.ts +29 -29
  48. package/src/server/sync/fingerprint.ts +44 -0
  49. package/src/server/sync/index.ts +15 -14
  50. package/src/server/sync/memory-adapter.ts +196 -0
  51. package/src/server/sync/plugin.ts +200 -108
  52. package/src/server/sync/response.ts +77 -0
  53. package/src/server/sync/stream.ts +152 -144
  54. package/src/shared/deferred-ndjson.ts +7 -0
@@ -0,0 +1,5 @@
1
+ export function isAbortError(error: unknown): boolean {
2
+ return (
3
+ error !== null && typeof error === "object" && "name" in error && error.name === "AbortError"
4
+ );
5
+ }
@@ -1,184 +1,184 @@
1
- import { buildSearchParams, type SearchParamsInput } from "../../shared/search-params.ts";
2
- import type { CacheEntry } from "./types.ts";
3
-
4
- const HASH_FRAGMENT_RE = /#.*$/;
5
- /** Strips one or more trailing slashes — used by `buildDataEndpoint` in static mode. */
6
- export const TRAILING_SLASHES_RE = /\/+$/;
7
-
8
- /**
9
- * Builds a full href string from a pathname, optional search params, and optional hash.
10
- * Null/undefined search values are omitted.
11
- *
12
- * @internal Exported for unit testing only.
13
- */
14
- export function buildHref(
15
- to: string,
16
- search: SearchParamsInput | null | undefined,
17
- hash: string | undefined,
18
- searchDefaults?: SearchParamsInput
19
- ): string {
20
- let url = to;
21
- if (search && Object.keys(search).length > 0) {
22
- const params = buildSearchParams(search, searchDefaults);
23
- const qs = params.toString();
24
- if (qs) {
25
- url += `?${qs}`;
26
- }
27
- }
28
- if (hash) {
29
- url += `#${hash}`;
30
- }
31
- return url;
32
- }
33
-
34
- /**
35
- * Parses the `X-Furin-Revalidate` response header and calls `invalidate` for
36
- * each path entry. Header format: `"/path1,/path2:layout,/path3"`
37
- */
38
- export function applyRevalidateHeader(
39
- headers: Headers,
40
- invalidate: (path: string, type: "page" | "layout" | undefined) => void
41
- ): void {
42
- const headerValue = headers.get("x-furin-revalidate");
43
- if (!headerValue) {
44
- return;
45
- }
46
- applyRevalidateEntries(headerValue.split(","), invalidate);
47
- }
48
-
49
- export function applyRevalidateEntries(
50
- entries: Iterable<string>,
51
- invalidate: (path: string, type: "page" | "layout" | undefined) => void
52
- ): void {
53
- for (const entry of entries) {
54
- const trimmed = entry.trim();
55
- if (!trimmed) {
56
- continue;
57
- }
58
- if (trimmed.endsWith(":layout")) {
59
- invalidate(trimmed.slice(0, -":layout".length), "layout");
60
- } else {
61
- invalidate(trimmed, "page");
62
- }
63
- }
64
- }
65
-
66
- /**
67
- * Returns `true` when `currentPath` is targeted by at least one invalidation entry.
68
- *
69
- * @internal Exported for unit testing only.
70
- */
71
- export function shouldAutoRefreshPath(
72
- currentPath: string,
73
- invalidations: ReadonlyArray<{ path: string; type: "page" | "layout" }>
74
- ): boolean {
75
- const normalizedCurrent = currentPath.split("?")[0] ?? currentPath;
76
- return invalidations.some(({ path, type }) => {
77
- if (type === "page") {
78
- return path === normalizedCurrent;
79
- }
80
- const prefix = path === "/" || path.endsWith("/") ? path : `${path}/`;
81
- return normalizedCurrent === path || normalizedCurrent.startsWith(prefix);
82
- });
83
- }
84
-
85
- export function stripHashFromHref(href: string): string {
86
- return href.replace(HASH_FRAGMENT_RE, "");
87
- }
88
-
89
- /** Strips trailing slashes from a pathname or href. Root `/` is preserved.
90
- * Query strings and hashes are preserved and re-attached after normalization. */
91
- export function normalizeHref(href: string): string {
92
- const url = new URL(href, "http://localhost");
93
- let pathname = url.pathname;
94
- if (pathname !== "/") {
95
- pathname = pathname.replace(/\/+$/g, "");
96
- }
97
- return pathname + url.search + url.hash;
98
- }
99
-
100
- /** Strips a single trailing slash from a pathname. Root `/` and empty string are preserved. */
101
- export function normalizePath(path: string): string {
102
- if (path === "/" || path === "") {
103
- return path;
104
- }
105
- return path.endsWith("/") ? path.slice(0, -1) : path;
106
- }
107
-
108
- /** @internal Exported for unit testing only. */
109
- export function shouldRefetch(entry: CacheEntry): boolean {
110
- return Date.now() - entry.createdAt > entry.staleTime;
111
- }
112
-
113
- /**
114
- * Decides whether a native `<a>` click should be intercepted and converted
115
- * to an SPA navigation. Returns the logical href to navigate to, or `null`
116
- * when the browser should handle the click normally.
117
- *
118
- * @internal Exported for unit testing only.
119
- */
120
- export function shouldInterceptClick(
121
- anchor: { href: string; target: string; hasAttribute(name: string): boolean },
122
- event: { metaKey: boolean; ctrlKey: boolean; shiftKey: boolean; altKey: boolean },
123
- basePath: string,
124
- currentOrigin: string,
125
- currentPathname: string
126
- ): string | null {
127
- if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
128
- return null;
129
- }
130
-
131
- if (anchor.target && anchor.target !== "_self") {
132
- return null;
133
- }
134
-
135
- if (anchor.hasAttribute("download")) {
136
- return null;
137
- }
138
-
139
- const href = anchor.href;
140
- if (!href) {
141
- return null;
142
- }
143
-
144
- let url: URL;
145
- try {
146
- url = new URL(href, currentOrigin);
147
- } catch {
148
- return null;
149
- }
150
-
151
- if (url.origin !== currentOrigin) {
152
- return null;
153
- }
154
-
155
- // Hash-only navigation on the same page — let the browser handle scrolling.
156
- if (normalizePath(url.pathname) === normalizePath(currentPathname) && url.hash) {
157
- return null;
158
- }
159
-
160
- const logicalPath = normalizeHref(toLogical(url.pathname, basePath));
161
- const normalizedBase = normalizePath(basePath);
162
- const normalizedPathname = normalizePath(url.pathname);
163
- if (
164
- normalizedBase &&
165
- normalizedPathname !== normalizedBase &&
166
- !normalizedPathname.startsWith(`${normalizedBase}/`)
167
- ) {
168
- return null;
169
- }
170
- return logicalPath + url.search + url.hash;
171
- }
172
-
173
- /** Strips basePath prefix from a physical pathname, returning the logical path. */
174
- export function toLogical(physicalPathname: string, basePath: string): string {
175
- if (
176
- basePath &&
177
- physicalPathname.startsWith(basePath) &&
178
- // Require a path boundary after the prefix so "/furin" doesn't match "/furinity/foo"
179
- (physicalPathname.length === basePath.length || physicalPathname[basePath.length] === "/")
180
- ) {
181
- return physicalPathname.slice(basePath.length) || "/";
182
- }
183
- return physicalPathname;
184
- }
1
+ import { buildSearchParams, type SearchParamsInput } from "../../shared/search-params.ts";
2
+ import type { CacheEntry } from "./types.ts";
3
+
4
+ const HASH_FRAGMENT_RE = /#.*$/;
5
+ /** Strips one or more trailing slashes — used by `buildDataEndpoint` in static mode. */
6
+ export const TRAILING_SLASHES_RE = /\/+$/;
7
+
8
+ /**
9
+ * Builds a full href string from a pathname, optional search params, and optional hash.
10
+ * Null/undefined search values are omitted.
11
+ *
12
+ * @internal Exported for unit testing only.
13
+ */
14
+ export function buildHref(
15
+ to: string,
16
+ search: SearchParamsInput | null | undefined,
17
+ hash: string | undefined,
18
+ searchDefaults?: SearchParamsInput
19
+ ): string {
20
+ let url = to;
21
+ if (search && Object.keys(search).length > 0) {
22
+ const params = buildSearchParams(search, searchDefaults);
23
+ const qs = params.toString();
24
+ if (qs) {
25
+ url += `?${qs}`;
26
+ }
27
+ }
28
+ if (hash) {
29
+ url += `#${hash}`;
30
+ }
31
+ return url;
32
+ }
33
+
34
+ /**
35
+ * Parses the `X-Furin-Revalidate` response header and calls `invalidate` for
36
+ * each path entry. Header format: `"/path1,/path2:layout,/path3"`
37
+ */
38
+ export function applyRevalidateHeader(
39
+ headers: Headers,
40
+ invalidate: (path: string, type: "page" | "layout" | undefined) => void
41
+ ): void {
42
+ const headerValue = headers.get("x-furin-revalidate");
43
+ if (!headerValue) {
44
+ return;
45
+ }
46
+ applyRevalidateEntries(headerValue.split(","), invalidate);
47
+ }
48
+
49
+ export function applyRevalidateEntries(
50
+ entries: Iterable<string>,
51
+ invalidate: (path: string, type: "page" | "layout" | undefined) => void
52
+ ): void {
53
+ for (const entry of entries) {
54
+ const trimmed = entry.trim();
55
+ if (!trimmed) {
56
+ continue;
57
+ }
58
+ if (trimmed.endsWith(":layout")) {
59
+ invalidate(trimmed.slice(0, -":layout".length), "layout");
60
+ } else {
61
+ invalidate(trimmed, "page");
62
+ }
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Returns `true` when `currentPath` is targeted by at least one invalidation entry.
68
+ *
69
+ * @internal Exported for unit testing only.
70
+ */
71
+ export function shouldAutoRefreshPath(
72
+ currentPath: string,
73
+ invalidations: ReadonlyArray<{ path: string; type: "page" | "layout" }>
74
+ ): boolean {
75
+ const normalizedCurrent = currentPath.split("?")[0] ?? currentPath;
76
+ return invalidations.some(({ path, type }) => {
77
+ if (type === "page") {
78
+ return path === normalizedCurrent;
79
+ }
80
+ const prefix = path === "/" || path.endsWith("/") ? path : `${path}/`;
81
+ return normalizedCurrent === path || normalizedCurrent.startsWith(prefix);
82
+ });
83
+ }
84
+
85
+ export function stripHashFromHref(href: string): string {
86
+ return href.replace(HASH_FRAGMENT_RE, "");
87
+ }
88
+
89
+ /** Strips trailing slashes from a pathname or href. Root `/` is preserved.
90
+ * Query strings and hashes are preserved and re-attached after normalization. */
91
+ export function normalizeHref(href: string): string {
92
+ const url = new URL(href, "http://localhost");
93
+ let pathname = url.pathname;
94
+ if (pathname !== "/") {
95
+ pathname = pathname.replace(/\/+$/g, "");
96
+ }
97
+ return pathname + url.search + url.hash;
98
+ }
99
+
100
+ /** Strips a single trailing slash from a pathname. Root `/` and empty string are preserved. */
101
+ export function normalizePath(path: string): string {
102
+ if (path === "/" || path === "") {
103
+ return path;
104
+ }
105
+ return path.endsWith("/") ? path.slice(0, -1) : path;
106
+ }
107
+
108
+ /** @internal Exported for unit testing only. */
109
+ export function shouldRefetch(entry: CacheEntry): boolean {
110
+ return Date.now() - entry.createdAt > entry.staleTime;
111
+ }
112
+
113
+ /**
114
+ * Decides whether a native `<a>` click should be intercepted and converted
115
+ * to an SPA navigation. Returns the logical href to navigate to, or `null`
116
+ * when the browser should handle the click normally.
117
+ *
118
+ * @internal Exported for unit testing only.
119
+ */
120
+ export function shouldInterceptClick(
121
+ anchor: { href: string; target: string; hasAttribute(name: string): boolean },
122
+ event: { metaKey: boolean; ctrlKey: boolean; shiftKey: boolean; altKey: boolean },
123
+ basePath: string,
124
+ currentOrigin: string,
125
+ currentPathname: string
126
+ ): string | null {
127
+ if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
128
+ return null;
129
+ }
130
+
131
+ if (anchor.target && anchor.target !== "_self") {
132
+ return null;
133
+ }
134
+
135
+ if (anchor.hasAttribute("download")) {
136
+ return null;
137
+ }
138
+
139
+ const href = anchor.href;
140
+ if (!href) {
141
+ return null;
142
+ }
143
+
144
+ let url: URL;
145
+ try {
146
+ url = new URL(href, currentOrigin);
147
+ } catch {
148
+ return null;
149
+ }
150
+
151
+ if (url.origin !== currentOrigin) {
152
+ return null;
153
+ }
154
+
155
+ // Hash-only navigation on the same page — let the browser handle scrolling.
156
+ if (normalizePath(url.pathname) === normalizePath(currentPathname) && url.hash) {
157
+ return null;
158
+ }
159
+
160
+ const logicalPath = normalizeHref(toLogical(url.pathname, basePath));
161
+ const normalizedBase = normalizePath(basePath);
162
+ const normalizedPathname = normalizePath(url.pathname);
163
+ if (
164
+ normalizedBase &&
165
+ normalizedPathname !== normalizedBase &&
166
+ !normalizedPathname.startsWith(`${normalizedBase}/`)
167
+ ) {
168
+ return null;
169
+ }
170
+ return logicalPath + url.search + url.hash;
171
+ }
172
+
173
+ /** Strips basePath prefix from a physical pathname, returning the logical path. */
174
+ export function toLogical(physicalPathname: string, basePath: string): string {
175
+ if (
176
+ basePath &&
177
+ physicalPathname.startsWith(basePath) &&
178
+ // Require a path boundary after the prefix so "/furin" doesn't match "/furinity/foo"
179
+ (physicalPathname.length === basePath.length || physicalPathname[basePath.length] === "/")
180
+ ) {
181
+ return physicalPathname.slice(basePath.length) || "/";
182
+ }
183
+ return physicalPathname;
184
+ }