@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.
- package/LICENSE +21 -21
- package/README.md +37 -33
- package/dist/build/compile-entry.ts +109 -109
- package/dist/build/entry-template.ts +156 -156
- package/dist/build/server-routes-entry.ts +36 -36
- package/dist/cli/index.js +0 -0
- 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/furin.d.ts +1 -1
- package/dist/furin.js +499 -215
- package/dist/server/sync/adapter.d.ts +62 -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 +16 -4
- package/package.json +1 -1
- package/src/adapter/README.md +13 -13
- package/src/build/client.ts +152 -152
- package/src/build/compile-entry.ts +109 -109
- package/src/build/entry-template.ts +156 -156
- package/src/build/hydrate.ts +340 -340
- package/src/build/index.ts +152 -152
- package/src/build/route-types.ts +145 -145
- package/src/build/scan-server.ts +95 -95
- package/src/build/server-routes-entry.ts +36 -36
- package/src/build/shared.ts +80 -80
- package/src/build/ssg-cache.ts +32 -32
- package/src/build/types.ts +89 -89
- 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/furin.ts +465 -465
- package/src/server/auto-invalidate/runtime.ts +134 -134
- package/src/server/dev-page-plugin.ts +454 -454
- package/src/server/render/assemble.ts +160 -160
- package/src/server/render/isr.ts +323 -323
- package/src/server/render/ssr.ts +591 -585
- package/src/server/router/plugin.ts +384 -384
- package/src/server/sync/adapter.ts +65 -0
- package/src/server/sync/config.ts +29 -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 +200 -108
- package/src/server/sync/response.ts +77 -0
- package/src/server/sync/stream.ts +152 -144
- package/src/shared/deferred-ndjson.ts +7 -0
|
@@ -1,144 +1,152 @@
|
|
|
1
|
-
import { Elysia } from "elysia";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
1
|
+
import { Elysia } from "elysia";
|
|
2
|
+
import type { SyncChange } from "./adapter.ts";
|
|
3
|
+
import { getSyncStreamPath } from "./config.ts";
|
|
4
|
+
import { memorySyncAdapter } from "./memory-adapter.ts";
|
|
5
|
+
|
|
6
|
+
export type { ChangePage as SyncChangePage, SyncChange } from "./adapter.ts";
|
|
7
|
+
|
|
8
|
+
const DEFAULT_CHANGE_LIMIT = 100;
|
|
9
|
+
const MAX_CHANGE_LIMIT = 500;
|
|
10
|
+
const CURSOR_PATTERN = /^\d+$/;
|
|
11
|
+
const defaultStreamPath = "/_furin/sync";
|
|
12
|
+
const encoder = new TextEncoder();
|
|
13
|
+
|
|
14
|
+
interface StreamState {
|
|
15
|
+
clients: Set<ReadableStreamDefaultController<Uint8Array>>;
|
|
16
|
+
heartbeats: Set<ReturnType<typeof setInterval>>;
|
|
17
|
+
unsubscribe: (() => void) | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const streams = new Map<string, StreamState>();
|
|
21
|
+
|
|
22
|
+
function encodeSseChange(change: SyncChange): Uint8Array {
|
|
23
|
+
return encoder.encode(
|
|
24
|
+
`id: ${change.cursor}\nevent: furin.sync\nretry: 3000\ndata: ${JSON.stringify({ cursor: change.cursor })}\n\n`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function getStreamState(path: string): StreamState {
|
|
29
|
+
const existing = streams.get(path);
|
|
30
|
+
if (existing) {
|
|
31
|
+
return existing;
|
|
32
|
+
}
|
|
33
|
+
const state: StreamState = {
|
|
34
|
+
clients: new Set(),
|
|
35
|
+
heartbeats: new Set(),
|
|
36
|
+
unsubscribe: undefined,
|
|
37
|
+
};
|
|
38
|
+
state.unsubscribe = memorySyncAdapter.subscribe(path, (change) => {
|
|
39
|
+
const chunk = encodeSseChange(change);
|
|
40
|
+
for (const client of [...state.clients]) {
|
|
41
|
+
try {
|
|
42
|
+
client.enqueue(chunk);
|
|
43
|
+
} catch {
|
|
44
|
+
state.clients.delete(client);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
streams.set(path, state);
|
|
49
|
+
return state;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function parseChangeQuery(
|
|
53
|
+
request: Request
|
|
54
|
+
): { after: string | undefined; limit: number } | { error: Response } {
|
|
55
|
+
const url = new URL(request.url);
|
|
56
|
+
const after = url.searchParams.get("after") ?? undefined;
|
|
57
|
+
if (after !== undefined && !CURSOR_PATTERN.test(after)) {
|
|
58
|
+
return { error: Response.json({ code: "FURIN_INVALID_SYNC_CURSOR" }, { status: 400 }) };
|
|
59
|
+
}
|
|
60
|
+
const limitValue = url.searchParams.get("limit");
|
|
61
|
+
if (limitValue !== null && !CURSOR_PATTERN.test(limitValue)) {
|
|
62
|
+
return { error: Response.json({ code: "FURIN_INVALID_SYNC_LIMIT" }, { status: 400 }) };
|
|
63
|
+
}
|
|
64
|
+
const limit = limitValue === null ? DEFAULT_CHANGE_LIMIT : Number.parseInt(limitValue, 10);
|
|
65
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > MAX_CHANGE_LIMIT) {
|
|
66
|
+
return { error: Response.json({ code: "FURIN_INVALID_SYNC_LIMIT" }, { status: 400 }) };
|
|
67
|
+
}
|
|
68
|
+
return { after, limit };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function publishSyncInvalidation(invalidations: readonly string[]): void {
|
|
72
|
+
if (invalidations.length === 0) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
memorySyncAdapter.appendChanges({
|
|
76
|
+
invalidations,
|
|
77
|
+
path: getSyncStreamPath() ?? defaultStreamPath,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function createSyncStreamPlugin(path?: string) {
|
|
82
|
+
const streamPath = path ?? defaultStreamPath;
|
|
83
|
+
return new Elysia({ name: `furin-sync-stream-${streamPath}` })
|
|
84
|
+
.get(`${streamPath}/changes`, ({ request, set }) => {
|
|
85
|
+
set.headers["cache-control"] = "no-store";
|
|
86
|
+
const query = parseChangeQuery(request);
|
|
87
|
+
if ("error" in query) {
|
|
88
|
+
return query.error;
|
|
89
|
+
}
|
|
90
|
+
return memorySyncAdapter.readChanges({ ...query, path: streamPath });
|
|
91
|
+
})
|
|
92
|
+
.get(streamPath, () => {
|
|
93
|
+
const state = getStreamState(streamPath);
|
|
94
|
+
let controllerRef: ReadableStreamDefaultController<Uint8Array> | undefined;
|
|
95
|
+
let heartbeat: ReturnType<typeof setInterval> | undefined;
|
|
96
|
+
const stream = new ReadableStream<Uint8Array>({
|
|
97
|
+
start(controller) {
|
|
98
|
+
controllerRef = controller;
|
|
99
|
+
state.clients.add(controller);
|
|
100
|
+
controller.enqueue(encoder.encode(": connected\nretry: 3000\n\n"));
|
|
101
|
+
heartbeat = setInterval(() => {
|
|
102
|
+
try {
|
|
103
|
+
controller.enqueue(encoder.encode(": heartbeat\n\n"));
|
|
104
|
+
} catch {
|
|
105
|
+
state.clients.delete(controller);
|
|
106
|
+
if (heartbeat) {
|
|
107
|
+
clearInterval(heartbeat);
|
|
108
|
+
state.heartbeats.delete(heartbeat);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}, 15_000);
|
|
112
|
+
state.heartbeats.add(heartbeat);
|
|
113
|
+
},
|
|
114
|
+
cancel() {
|
|
115
|
+
if (controllerRef) {
|
|
116
|
+
state.clients.delete(controllerRef);
|
|
117
|
+
}
|
|
118
|
+
if (heartbeat) {
|
|
119
|
+
clearInterval(heartbeat);
|
|
120
|
+
state.heartbeats.delete(heartbeat);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
return new Response(stream, {
|
|
126
|
+
headers: {
|
|
127
|
+
"cache-control": "no-cache, no-transform",
|
|
128
|
+
connection: "keep-alive",
|
|
129
|
+
"content-type": "text/event-stream; charset=utf-8",
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** @internal — resets process-local sync state between tests. */
|
|
136
|
+
export function __resetSyncState(): void {
|
|
137
|
+
for (const state of streams.values()) {
|
|
138
|
+
state.unsubscribe?.();
|
|
139
|
+
for (const heartbeat of state.heartbeats) {
|
|
140
|
+
clearInterval(heartbeat);
|
|
141
|
+
}
|
|
142
|
+
for (const client of state.clients) {
|
|
143
|
+
try {
|
|
144
|
+
client.close();
|
|
145
|
+
} catch {
|
|
146
|
+
// already closed
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
streams.clear();
|
|
151
|
+
memorySyncAdapter.reset();
|
|
152
|
+
}
|
|
@@ -149,6 +149,13 @@ export async function parseDeferredNdjson(
|
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
// A superseded navigation may discard deferred values before React observes
|
|
153
|
+
// them. Mark each rejection as handled without changing the original
|
|
154
|
+
// promise, so consumers that do await it still receive the same error.
|
|
155
|
+
for (const promise of Object.values(deferredPromises)) {
|
|
156
|
+
promise.catch(() => undefined);
|
|
157
|
+
}
|
|
158
|
+
|
|
152
159
|
if (deferredKeys.length === 0) {
|
|
153
160
|
cleanupAbortHandler();
|
|
154
161
|
try {
|