@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,108 +1,199 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
appendPendingInvalidationHeader,
|
|
4
|
-
isSuccessfulMutationResponse,
|
|
5
|
-
runInvalidationRules,
|
|
6
|
-
} from "../auto-invalidate/runtime.ts";
|
|
7
|
-
import type { InvalidationInput } from "../auto-invalidate/types.ts";
|
|
8
|
-
import {
|
|
9
|
-
|
|
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
|
-
return
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
}
|
|
1
|
+
import { Elysia } from "elysia";
|
|
2
|
+
import {
|
|
3
|
+
appendPendingInvalidationHeader,
|
|
4
|
+
isSuccessfulMutationResponse,
|
|
5
|
+
runInvalidationRules,
|
|
6
|
+
} from "../auto-invalidate/runtime.ts";
|
|
7
|
+
import type { InvalidationInput } from "../auto-invalidate/types.ts";
|
|
8
|
+
import { createMutationFingerprint } from "./fingerprint.ts";
|
|
9
|
+
import { memorySyncAdapter } from "./memory-adapter.ts";
|
|
10
|
+
import { replayResponse, storeResponse } from "./response.ts";
|
|
11
|
+
import { publishSyncInvalidation } from "./stream.ts";
|
|
12
|
+
|
|
13
|
+
export type SyncRouteOption =
|
|
14
|
+
| false
|
|
15
|
+
| InvalidationInput
|
|
16
|
+
| {
|
|
17
|
+
invalidate: InvalidationInput;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** @deprecated Use SyncRouteOption. */
|
|
21
|
+
export type SyncInput = Exclude<SyncRouteOption, false>;
|
|
22
|
+
|
|
23
|
+
interface RouteSyncMetadata {
|
|
24
|
+
disabled: boolean;
|
|
25
|
+
invalidate?: InvalidationInput;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ActiveMutation {
|
|
29
|
+
mutationId: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface MutationContext {
|
|
33
|
+
body?: unknown;
|
|
34
|
+
headers: {
|
|
35
|
+
"idempotency-key"?: string | undefined;
|
|
36
|
+
};
|
|
37
|
+
request: Request;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const routeMetadata = new WeakMap<Request, RouteSyncMetadata>();
|
|
41
|
+
const activeMutations = new WeakMap<Request, ActiveMutation>();
|
|
42
|
+
|
|
43
|
+
function invalidationInputFromSync(input: Exclude<SyncRouteOption, false>): InvalidationInput {
|
|
44
|
+
if (input && typeof input === "object" && "invalidate" in input) {
|
|
45
|
+
return input.invalidate;
|
|
46
|
+
}
|
|
47
|
+
return input;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isMutationMethod(method: string): boolean {
|
|
51
|
+
return method !== "GET" && method !== "HEAD" && method !== "OPTIONS";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function getIdempotencyKey(ctx: MutationContext): string | undefined {
|
|
55
|
+
const fromCtxHeaders = ctx.headers["idempotency-key"];
|
|
56
|
+
if (typeof fromCtxHeaders === "string" && fromCtxHeaders.length > 0) {
|
|
57
|
+
return fromCtxHeaders;
|
|
58
|
+
}
|
|
59
|
+
const fromRequest = ctx.request.headers.get("Idempotency-Key");
|
|
60
|
+
return fromRequest || undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function getPrincipalScope(request: Request): Promise<string> {
|
|
64
|
+
const authorization = request.headers.get("authorization");
|
|
65
|
+
const credentials = authorization
|
|
66
|
+
? JSON.stringify(["authorization", authorization])
|
|
67
|
+
: JSON.stringify(["cookie", request.headers.get("cookie") ?? ""]);
|
|
68
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(credentials));
|
|
69
|
+
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function supportsReplayBody(request: Request): boolean {
|
|
73
|
+
const contentType = request.headers.get("content-type")?.toLowerCase();
|
|
74
|
+
const mediaType = contentType?.split(";", 1)[0]?.trim();
|
|
75
|
+
return (
|
|
76
|
+
contentType === undefined ||
|
|
77
|
+
mediaType === "application/json" ||
|
|
78
|
+
(mediaType?.startsWith("application/") === true && mediaType.endsWith("+json")) ||
|
|
79
|
+
contentType.startsWith("application/x-www-form-urlencoded") ||
|
|
80
|
+
contentType.startsWith("text/")
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function conflictResponse(reason: "in-progress" | "payload-mismatch"): Response {
|
|
85
|
+
const inProgress = reason === "in-progress";
|
|
86
|
+
return Response.json(
|
|
87
|
+
{
|
|
88
|
+
code: inProgress ? "FURIN_MUTATION_IN_PROGRESS" : "FURIN_IDEMPOTENCY_MISMATCH",
|
|
89
|
+
message: inProgress
|
|
90
|
+
? "A mutation with this Idempotency-Key is still running."
|
|
91
|
+
: "The Idempotency-Key was already used with a different request.",
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
status: 409,
|
|
95
|
+
headers: inProgress ? { "retry-after": "1" } : undefined,
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function beginMutation(ctx: MutationContext): Promise<Response | undefined> {
|
|
101
|
+
if (!isMutationMethod(ctx.request.method) || routeMetadata.get(ctx.request)?.disabled) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const idempotencyKey = getIdempotencyKey(ctx);
|
|
105
|
+
if (!idempotencyKey) {
|
|
106
|
+
return new Response("Missing Idempotency-Key header", {
|
|
107
|
+
status: 428,
|
|
108
|
+
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
if (!supportsReplayBody(ctx.request)) {
|
|
112
|
+
return Response.json(
|
|
113
|
+
{
|
|
114
|
+
code: "FURIN_UNSUPPORTED_SYNC_BODY",
|
|
115
|
+
message: "This request body cannot be replayed. Set sync: false on the route.",
|
|
116
|
+
},
|
|
117
|
+
{ status: 415 }
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const url = new URL(ctx.request.url);
|
|
122
|
+
const principal = await getPrincipalScope(ctx.request);
|
|
123
|
+
const key = `${ctx.request.method}:${url.pathname}:${idempotencyKey}`;
|
|
124
|
+
const fingerprint = await createMutationFingerprint({ body: ctx.body, request: ctx.request });
|
|
125
|
+
const result = await memorySyncAdapter.beginMutation({ fingerprint, key, principal });
|
|
126
|
+
if (result.kind === "replay") {
|
|
127
|
+
return replayResponse(result.response);
|
|
128
|
+
}
|
|
129
|
+
if (result.kind === "conflict") {
|
|
130
|
+
return conflictResponse(result.reason);
|
|
131
|
+
}
|
|
132
|
+
if (result.kind === "unavailable") {
|
|
133
|
+
return Response.json(
|
|
134
|
+
{
|
|
135
|
+
code: "FURIN_SYNC_CAPACITY_EXCEEDED",
|
|
136
|
+
message: "The mutation replay store is temporarily full.",
|
|
137
|
+
},
|
|
138
|
+
{ headers: { "retry-after": "1" }, status: 503 }
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
activeMutations.set(ctx.request, { mutationId: result.mutationId });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function abortMutation(request: Request): Promise<void> {
|
|
145
|
+
const active = activeMutations.get(request);
|
|
146
|
+
if (!active) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
activeMutations.delete(request);
|
|
150
|
+
await memorySyncAdapter.abortMutation(active);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function furinSync() {
|
|
154
|
+
return new Elysia({ name: "furin-sync" })
|
|
155
|
+
.macro({
|
|
156
|
+
sync(input: SyncRouteOption) {
|
|
157
|
+
return {
|
|
158
|
+
transform({ request }) {
|
|
159
|
+
routeMetadata.set(
|
|
160
|
+
request,
|
|
161
|
+
input === false
|
|
162
|
+
? { disabled: true }
|
|
163
|
+
: { disabled: false, invalidate: invalidationInputFromSync(input) }
|
|
164
|
+
);
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
.onBeforeHandle({ as: "global" }, beginMutation)
|
|
170
|
+
.onAfterHandle({ as: "global" }, async (ctx) => {
|
|
171
|
+
const active = activeMutations.get(ctx.request);
|
|
172
|
+
if (!active) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (!isSuccessfulMutationResponse(ctx)) {
|
|
176
|
+
await abortMutation(ctx.request);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const invalidate = routeMetadata.get(ctx.request)?.invalidate;
|
|
181
|
+
if (invalidate) {
|
|
182
|
+
runInvalidationRules(invalidate);
|
|
183
|
+
}
|
|
184
|
+
const pending = appendPendingInvalidationHeader(ctx.set);
|
|
185
|
+
if (pending.length > 0) {
|
|
186
|
+
ctx.set.headers["x-furin-sync"] = "1";
|
|
187
|
+
}
|
|
188
|
+
const response = await storeResponse(ctx.responseValue, ctx.set);
|
|
189
|
+
await memorySyncAdapter.commitMutation({ ...active, response });
|
|
190
|
+
activeMutations.delete(ctx.request);
|
|
191
|
+
|
|
192
|
+
if (pending.length > 0) {
|
|
193
|
+
// Notify every mounted app's sync stream — a mutation on a shared API
|
|
194
|
+
// may invalidate pages rendered by any of them.
|
|
195
|
+
publishSyncInvalidation(pending);
|
|
196
|
+
}
|
|
197
|
+
})
|
|
198
|
+
.onError({ as: "global" }, ({ request }) => abortMutation(request));
|
|
199
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { type Context, ElysiaCustomStatusResponse, StatusMap } from "elysia";
|
|
2
|
+
import type { StoredResponse } from "./adapter.ts";
|
|
3
|
+
|
|
4
|
+
const HOP_BY_HOP_HEADERS = new Set(["connection", "keep-alive", "transfer-encoding", "upgrade"]);
|
|
5
|
+
|
|
6
|
+
function statusCode(status: Context["set"]["status"]): number {
|
|
7
|
+
if (typeof status === "number") {
|
|
8
|
+
return status;
|
|
9
|
+
}
|
|
10
|
+
return status === undefined ? 200 : StatusMap[status];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function unwrapStatusResponse(value: unknown): { status: number; value: unknown } | undefined {
|
|
14
|
+
if (!(value instanceof ElysiaCustomStatusResponse)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
return { status: value.code, value: value.response };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function responseHeaders(headers: Context["set"]["headers"]): Headers {
|
|
21
|
+
const result = new Headers();
|
|
22
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
23
|
+
if (value !== undefined && !HOP_BY_HOP_HEADERS.has(name.toLowerCase())) {
|
|
24
|
+
for (const entry of Array.isArray(value) ? value : [value]) {
|
|
25
|
+
result.append(name, String(entry));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function storeResponse(
|
|
33
|
+
responseValue: unknown,
|
|
34
|
+
set: Context["set"]
|
|
35
|
+
): Promise<StoredResponse> {
|
|
36
|
+
if (responseValue instanceof Response) {
|
|
37
|
+
const clone = responseValue.clone();
|
|
38
|
+
const headers = [...clone.headers.entries()].filter(
|
|
39
|
+
([name]) => !HOP_BY_HOP_HEADERS.has(name.toLowerCase())
|
|
40
|
+
);
|
|
41
|
+
return {
|
|
42
|
+
body: new Uint8Array(await clone.arrayBuffer()),
|
|
43
|
+
headers,
|
|
44
|
+
status: clone.status,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const headers = responseHeaders(set.headers);
|
|
49
|
+
const statusResponse = unwrapStatusResponse(responseValue);
|
|
50
|
+
const value = statusResponse?.value ?? responseValue;
|
|
51
|
+
let body: Uint8Array;
|
|
52
|
+
if (value === undefined || value === null) {
|
|
53
|
+
body = new Uint8Array();
|
|
54
|
+
} else if (typeof value === "string") {
|
|
55
|
+
headers.set("content-type", headers.get("content-type") ?? "text/plain;charset=utf-8");
|
|
56
|
+
body = new TextEncoder().encode(value);
|
|
57
|
+
} else {
|
|
58
|
+
headers.set("content-type", headers.get("content-type") ?? "application/json");
|
|
59
|
+
body = new TextEncoder().encode(JSON.stringify(value));
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
body,
|
|
63
|
+
headers: [...headers.entries()],
|
|
64
|
+
status: statusResponse?.status ?? statusCode(set.status),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function replayResponse(stored: StoredResponse): Response {
|
|
69
|
+
const body =
|
|
70
|
+
stored.status === 204 || stored.status === 205 || stored.status === 304
|
|
71
|
+
? null
|
|
72
|
+
: stored.body.slice();
|
|
73
|
+
return new Response(body, {
|
|
74
|
+
headers: new Headers(stored.headers.map(([name, value]) => [name, value] as [string, string])),
|
|
75
|
+
status: stored.status,
|
|
76
|
+
});
|
|
77
|
+
}
|