@teyik0/furin 0.8.0-alpha.0 → 0.9.0-alpha.0
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/README.md +37 -0
- package/dist/adapter/bun/app.d.ts +1 -0
- package/dist/adapter/bun.d.ts +3 -1
- package/dist/adapter/runtime-build.d.ts +8 -5
- package/dist/adapter/vercel.js +8 -8
- package/dist/build/client.d.ts +2 -2
- package/dist/build/discover-mounts-process.d.ts +1 -1
- package/dist/build/discover-pages-process.d.ts +1 -0
- package/dist/build/discover-pages.d.ts +11 -0
- package/dist/build/discovery-module.d.ts +3 -0
- package/dist/build/discovery-plugins.d.ts +34 -0
- package/dist/build/elysia-aot.d.ts +1 -1
- package/dist/build/entry-template.ts +6 -1
- package/dist/build/hydrate.d.ts +3 -2
- package/dist/build/index.js +7688 -8458
- package/dist/build/preload-manifest.d.ts +2 -2
- package/dist/build/route-metadata.d.ts +31 -0
- package/dist/build/route-types.d.ts +1 -1
- package/dist/build/shared.d.ts +2 -2
- package/dist/build/types.d.ts +11 -0
- package/dist/cli/dev-worker.d.ts +1 -0
- package/dist/cli/dev-worker.ts +123 -0
- package/dist/cli/dev.d.ts +1 -0
- package/dist/cli/index.js +7811 -8477
- package/dist/client/browser-events-runtime.d.ts +2 -2
- package/dist/client/browser-events.d.ts +2 -0
- package/dist/client/chunks/{client-brdfd773.js → client-rjhxgt64.js} +187 -4
- package/dist/client/dev-error-overlay.ts +18 -1
- package/dist/client/link.js +110 -156
- package/dist/client/query-store.d.ts +5 -0
- package/dist/client/router/navigation-controller.d.ts +2 -0
- package/dist/client/router/use-fetch-interception.d.ts +1 -1
- package/dist/client/router/use-sync-stream.d.ts +1 -1
- package/dist/client/{router/sync-catch-up.d.ts → sync-catch-up.d.ts} +5 -3
- package/dist/client/sync-client.d.ts +74 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +533 -145
- package/dist/config.d.ts +6 -1
- package/dist/config.js +3 -1
- package/dist/define-route.d.ts +3 -3
- package/dist/furin.d.ts +2 -1
- package/dist/furin.js +1119 -346
- package/dist/plugin/index.js +5 -5
- package/dist/plugin/route-registry.d.ts +22 -2
- package/dist/rsc/build/index.d.ts +4 -3
- package/dist/server/dev/server-only-module.d.ts +5 -0
- package/dist/server/internal.js +201 -91
- package/dist/server/logging/ingest-plugin.d.ts +1 -2
- package/dist/server/production-server.js +36 -33
- package/dist/server/server-entry.d.ts +5 -0
- package/dist/server/server-entry.js +36 -0
- package/dist/server/static-export.js +201 -91
- package/dist/server/sync/adapter.d.ts +4 -3
- package/dist/server/sync/config.d.ts +7 -0
- package/dist/server/sync/connection.d.ts +13 -0
- package/dist/server/sync/drizzle/index.js +5 -4
- package/dist/server/sync/index.d.ts +3 -0
- package/dist/server/sync/index.js +1361 -243
- package/dist/server/sync/invalidate-sync.d.ts +6 -2
- package/dist/server/sync/local-client.d.ts +9 -0
- package/dist/server/sync/postgres/index.d.ts +1 -1
- package/dist/server/sync/postgres/index.js +2 -2
- package/dist/server/sync/prisma/index.js +2 -2
- package/dist/server/sync/redis/index.d.ts +1 -1
- package/dist/server/sync/redis/index.js +4 -3
- package/dist/server/sync/redis/scripts.d.ts +1 -1
- package/dist/server/sync/runtime.d.ts +3 -0
- package/dist/server/sync/sqlite/index.d.ts +1 -1
- package/dist/server/sync/sqlite/index.js +3 -2
- package/dist/shared/browser-events.d.ts +4 -0
- package/dist/sync-client.d.ts +3 -0
- package/dist/sync-client.js +1600 -0
- package/package.json +12 -7
- package/src/adapter/README.md +7 -0
- package/src/adapter/build.ts +34 -33
- package/src/adapter/bun/app.ts +5 -0
- package/src/adapter/bun/server.ts +2 -2
- package/src/adapter/bun.ts +18 -2
- package/src/adapter/furin-app.d.ts +1 -0
- package/src/adapter/package.ts +1 -0
- package/src/adapter/runtime-build.ts +18 -12
- package/src/adapter/static.ts +17 -13
- package/src/build/client.ts +7 -5
- package/src/build/discover-mounts-process.ts +5 -1
- package/src/build/discover-pages-process.ts +151 -0
- package/src/build/discover-pages.ts +69 -0
- package/src/build/discovery-module.ts +70 -0
- package/src/build/discovery-plugins.ts +101 -0
- package/src/build/elysia-aot-worker.ts +9 -8
- package/src/build/elysia-aot.ts +110 -75
- package/src/build/entry-template.ts +6 -1
- package/src/build/hydrate.ts +11 -8
- package/src/build/index.ts +6 -50
- package/src/build/preload-manifest.ts +3 -3
- package/src/build/route-metadata.ts +90 -0
- package/src/build/route-types.ts +1 -1
- package/src/build/shared.ts +6 -8
- package/src/build/types.ts +9 -0
- package/src/cli/config.ts +4 -0
- package/src/cli/dev-worker.ts +123 -0
- package/src/cli/dev.ts +91 -0
- package/src/cli/index.ts +24 -1
- package/src/client/browser-events-runtime.ts +97 -23
- package/src/client/browser-events.ts +8 -0
- package/src/client/dev-error-overlay.ts +18 -1
- package/src/client/query-store.ts +54 -3
- package/src/client/router/navigation-controller.ts +45 -12
- package/src/client/router/provider.tsx +6 -2
- package/src/client/router/use-fetch-interception.ts +1 -1
- package/src/client/router/use-sync-stream.ts +96 -44
- package/src/client/{router/sync-catch-up.ts → sync-catch-up.ts} +28 -12
- package/src/client/sync-client.ts +528 -0
- package/src/client/sync.ts +11 -4
- package/src/client.ts +10 -0
- package/src/config.ts +17 -10
- package/src/furin.ts +5 -0
- package/src/plugin/route-registry.ts +53 -7
- package/src/rsc/build/index.ts +6 -3
- package/src/server/browser-events/plugin.ts +79 -25
- package/src/server/dev/rewrite-module-specifiers.ts +2 -1
- package/src/server/dev/server-only-module.ts +23 -0
- package/src/server/dev/symbolicate.ts +4 -3
- package/src/server/dev-page-imports.ts +3 -1
- package/src/server/dev-page-plugin.ts +18 -1
- package/src/server/logging/ingest-plugin.ts +3 -9
- package/src/server/server-entry.ts +39 -0
- package/src/server/sync/adapter.ts +7 -3
- package/src/server/sync/config.ts +12 -6
- package/src/server/sync/connection.ts +71 -0
- package/src/server/sync/index.ts +3 -0
- package/src/server/sync/invalidate-sync.ts +30 -5
- package/src/server/sync/local-client.ts +84 -0
- package/src/server/sync/plugin.ts +27 -20
- package/src/server/sync/postgres/index.ts +9 -2
- package/src/server/sync/redis/index.ts +5 -1
- package/src/server/sync/redis/scripts.ts +1 -1
- package/src/server/sync/runtime.ts +56 -18
- package/src/server/sync/sqlite/index.ts +6 -2
- package/src/server/sync/stream.ts +17 -5
- package/src/shared/browser-events.ts +1 -1
- package/src/sync-client.ts +14 -0
- package/dist/server/logger.d.ts +0 -10
- package/src/server/logger.ts +0 -34
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { Context } from "elysia";
|
|
2
|
+
import { log } from "evlog";
|
|
3
|
+
import type { FurinSyncOptions, SyncConnectionLease } from "./config.ts";
|
|
4
|
+
|
|
5
|
+
// A function on a Request never crosses HTTP; Symbol.for also works across published bundles.
|
|
6
|
+
export const LOCAL_SYNC_CONNECTION = Symbol.for("furin.sync.local-connection.v1");
|
|
7
|
+
export interface LocalSyncRequest extends Request {
|
|
8
|
+
[LOCAL_SYNC_CONNECTION]?: (context: Context, sync: FurinSyncOptions) => Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Principal resolvers reject signed-out requests by throwing or returning an empty identity. */
|
|
12
|
+
export async function verifiedSyncPrincipal(
|
|
13
|
+
sync: FurinSyncOptions,
|
|
14
|
+
context: Context
|
|
15
|
+
): Promise<string | undefined> {
|
|
16
|
+
try {
|
|
17
|
+
const principal = await sync.principal(context);
|
|
18
|
+
return principal.length > 0 ? principal : undefined;
|
|
19
|
+
} catch {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function releaseLease(lease: SyncConnectionLease | undefined): void {
|
|
25
|
+
try {
|
|
26
|
+
lease?.release();
|
|
27
|
+
} catch (error) {
|
|
28
|
+
log.warn({ action: "sync_connection_release_failed", error: String(error) });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Acquire asynchronously while allowing the transport to close before acquisition finishes. */
|
|
33
|
+
export function createSyncConnectionLifetime(
|
|
34
|
+
sync: FurinSyncOptions,
|
|
35
|
+
context: Context,
|
|
36
|
+
changed: () => void
|
|
37
|
+
) {
|
|
38
|
+
let closed = false;
|
|
39
|
+
let lease: SyncConnectionLease | undefined;
|
|
40
|
+
const onAbort = () => {
|
|
41
|
+
if (!closed) {
|
|
42
|
+
changed();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
async acquire(): Promise<void> {
|
|
47
|
+
if (closed) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const acquired = await sync.connection?.(context);
|
|
51
|
+
if (closed) {
|
|
52
|
+
releaseLease(acquired);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
lease = acquired;
|
|
56
|
+
lease?.signal.addEventListener("abort", onAbort, { once: true });
|
|
57
|
+
if (lease?.signal.aborted) {
|
|
58
|
+
onAbort();
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
close(): void {
|
|
62
|
+
if (closed) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
closed = true;
|
|
66
|
+
lease?.signal.removeEventListener("abort", onAbort);
|
|
67
|
+
releaseLease(lease);
|
|
68
|
+
lease = undefined;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
package/src/server/sync/index.ts
CHANGED
|
@@ -6,8 +6,11 @@ import { furinSync as furinSyncImplementation } from "./plugin.ts";
|
|
|
6
6
|
import { createSyncChangesPlugin as createSyncChangesPluginImplementation } from "./stream.ts";
|
|
7
7
|
|
|
8
8
|
export const createSyncChangesPlugin = createSyncChangesPluginImplementation;
|
|
9
|
+
export type { SyncConnectionLease } from "./config.ts";
|
|
10
|
+
export { createLocalSyncClient, type LocalSyncClientOptions } from "./local-client.ts";
|
|
9
11
|
export const furinSync = furinSyncImplementation;
|
|
10
12
|
export const invalidateSync = invalidateSyncImplementation;
|
|
13
|
+
export type { SyncInvalidationOptions } from "./invalidate-sync.ts";
|
|
11
14
|
export const PollingSyncNotifier = PollingSyncNotifierImplementation;
|
|
12
15
|
|
|
13
16
|
export type PollingSyncNotifier = PollingSyncNotifierImplementation;
|
|
@@ -7,6 +7,11 @@ import { normalizedInvalidations } from "./atomic.ts";
|
|
|
7
7
|
import type { SyncParams, SyncPattern } from "./queries.ts";
|
|
8
8
|
import { publishCursor, resolveSyncRuntime } from "./runtime.ts";
|
|
9
9
|
|
|
10
|
+
export interface SyncInvalidationOptions {
|
|
11
|
+
/** Verified identity selected by trusted server code, never an untrusted request field. */
|
|
12
|
+
principal: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
10
15
|
/**
|
|
11
16
|
* Invalidates outside a request (cron, webhook) what the handler's `invalidate()` takes — a GET
|
|
12
17
|
* pattern and its params, cache tags or a page path: purges the pages built from it, then
|
|
@@ -15,17 +20,37 @@ import { publishCursor, resolveSyncRuntime } from "./runtime.ts";
|
|
|
15
20
|
export function invalidateSync<Target extends SyncPattern>(
|
|
16
21
|
sync: SyncRuntimeOptions,
|
|
17
22
|
pattern: Target,
|
|
18
|
-
params?: NoInfer<SyncParams<Target
|
|
23
|
+
params?: NoInfer<SyncParams<Target>>,
|
|
24
|
+
options?: SyncInvalidationOptions
|
|
25
|
+
): Promise<void>;
|
|
26
|
+
export function invalidateSync(
|
|
27
|
+
sync: SyncRuntimeOptions,
|
|
28
|
+
rule: InvalidationRule,
|
|
29
|
+
options?: SyncInvalidationOptions
|
|
19
30
|
): Promise<void>;
|
|
20
|
-
export function invalidateSync(sync: SyncRuntimeOptions, rule: InvalidationRule): Promise<void>;
|
|
21
31
|
export async function invalidateSync(
|
|
22
32
|
sync: SyncRuntimeOptions,
|
|
23
33
|
target: string | InvalidationRule,
|
|
24
|
-
|
|
34
|
+
paramsOrOptions?: QueryScope | SyncInvalidationOptions,
|
|
35
|
+
options?: SyncInvalidationOptions
|
|
25
36
|
): Promise<void> {
|
|
37
|
+
const provenance =
|
|
38
|
+
typeof target === "string" ? options : (paramsOrOptions as SyncInvalidationOptions | undefined);
|
|
39
|
+
if (provenance?.principal === "") {
|
|
40
|
+
throw new Error("[furin] Sync invalidation principal must not be empty.");
|
|
41
|
+
}
|
|
26
42
|
const runtime = resolveSyncRuntime(sync, IS_DEV);
|
|
27
43
|
const rule =
|
|
28
|
-
typeof target === "string"
|
|
44
|
+
typeof target === "string"
|
|
45
|
+
? {
|
|
46
|
+
tags: [
|
|
47
|
+
queryTag({ id: target, scope: (paramsOrOptions as QueryScope | undefined) ?? {} }),
|
|
48
|
+
],
|
|
49
|
+
}
|
|
50
|
+
: target;
|
|
29
51
|
await runInvalidationRules(rule);
|
|
30
|
-
publishCursor(
|
|
52
|
+
publishCursor(
|
|
53
|
+
runtime,
|
|
54
|
+
await runtime.adapter.appendInvalidations(normalizedInvalidations(rule), provenance?.principal)
|
|
55
|
+
);
|
|
31
56
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { AnyElysia } from "elysia";
|
|
2
|
+
import {
|
|
3
|
+
createSyncClientRuntime,
|
|
4
|
+
type SyncClient,
|
|
5
|
+
SyncClientError,
|
|
6
|
+
type SyncClientOptions,
|
|
7
|
+
} from "../../client/sync-client.ts";
|
|
8
|
+
import { IS_DEV } from "../runtime-env.ts";
|
|
9
|
+
import type { FurinSyncOptions } from "./config.ts";
|
|
10
|
+
import {
|
|
11
|
+
createSyncConnectionLifetime,
|
|
12
|
+
LOCAL_SYNC_CONNECTION,
|
|
13
|
+
type LocalSyncRequest,
|
|
14
|
+
} from "./connection.ts";
|
|
15
|
+
import { subscribeSyncCursor } from "./stream.ts";
|
|
16
|
+
|
|
17
|
+
export interface LocalSyncClientOptions
|
|
18
|
+
extends Pick<SyncClientOptions, "headers" | "fetch" | "syncPath"> {
|
|
19
|
+
app: Pick<AnyElysia, "handle">;
|
|
20
|
+
sync: FurinSyncOptions;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Follow treaty(app) queries through the application's own authorized handlers. */
|
|
24
|
+
export function createLocalSyncClient(options: LocalSyncClientOptions): SyncClient {
|
|
25
|
+
// Eden uses this synthetic origin for treaty(app); it never leaves app.handle().
|
|
26
|
+
const changesUrl = new URL(
|
|
27
|
+
`${options.syncPath ?? options.sync.path ?? "/_furin/sync"}/changes`,
|
|
28
|
+
"http://e.ly"
|
|
29
|
+
);
|
|
30
|
+
return createSyncClientRuntime(options, {
|
|
31
|
+
accepts: (reference) => reference.application === options.app,
|
|
32
|
+
fetchChanges: (after, headers, signal) => {
|
|
33
|
+
const url = new URL(changesUrl);
|
|
34
|
+
url.searchParams.set("after", after ?? "0");
|
|
35
|
+
return options.app.handle(new Request(url, { ...options.fetch, headers, signal }));
|
|
36
|
+
},
|
|
37
|
+
subscribe: async ({ headers, signal, onCursor, onContextChange }) => {
|
|
38
|
+
let access: ReturnType<typeof createSyncConnectionLifetime> | undefined;
|
|
39
|
+
let unsubscribe: (() => void) | undefined;
|
|
40
|
+
const stop = () => {
|
|
41
|
+
signal.removeEventListener("abort", stop);
|
|
42
|
+
access?.close();
|
|
43
|
+
unsubscribe?.();
|
|
44
|
+
};
|
|
45
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
46
|
+
const request = new Request(changesUrl, {
|
|
47
|
+
...options.fetch,
|
|
48
|
+
headers,
|
|
49
|
+
signal,
|
|
50
|
+
}) as LocalSyncRequest;
|
|
51
|
+
request[LOCAL_SYNC_CONNECTION] = async (context, sync) => {
|
|
52
|
+
access = createSyncConnectionLifetime(sync, context, onContextChange);
|
|
53
|
+
await access.acquire();
|
|
54
|
+
if (signal.aborted) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const subscription = await subscribeSyncCursor(sync, IS_DEV, onCursor);
|
|
58
|
+
if (signal.aborted) {
|
|
59
|
+
subscription.unsubscribe();
|
|
60
|
+
} else {
|
|
61
|
+
({ unsubscribe } = subscription);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
try {
|
|
65
|
+
signal.throwIfAborted();
|
|
66
|
+
const response = await options.app.handle(request);
|
|
67
|
+
signal.throwIfAborted();
|
|
68
|
+
if (!response.ok) {
|
|
69
|
+
throw new SyncClientError("Local sync connection refused", response.status);
|
|
70
|
+
}
|
|
71
|
+
if (!access) {
|
|
72
|
+
throw new SyncClientError(
|
|
73
|
+
"Local sync endpoint does not support event connections",
|
|
74
|
+
undefined
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return stop;
|
|
78
|
+
} catch (error) {
|
|
79
|
+
stop();
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -44,7 +44,7 @@ import {
|
|
|
44
44
|
replayResponse,
|
|
45
45
|
storeResponse,
|
|
46
46
|
} from "./response.ts";
|
|
47
|
-
import { publishCursor, resolveSyncRuntime } from "./runtime.ts";
|
|
47
|
+
import { prepareSyncRuntime, publishCursor, resolveSyncRuntime } from "./runtime.ts";
|
|
48
48
|
import { bindSyncValidation, syncValidationApp } from "./validation.ts";
|
|
49
49
|
|
|
50
50
|
interface ActiveMutation {
|
|
@@ -144,15 +144,20 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
144
144
|
owner: SyncOwner
|
|
145
145
|
) {
|
|
146
146
|
// furinSync() belongs to no Furin runtime: it follows the default mode.
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
? (options.adapter as Adapter &
|
|
151
|
-
TransactionalSyncAdapter<SyncTransaction<Adapter>, "async" | "sync">)
|
|
152
|
-
: undefined;
|
|
147
|
+
const development = IS_DEV;
|
|
148
|
+
prepareSyncRuntime(options, development);
|
|
149
|
+
const runtime = () => resolveSyncRuntime(options, development);
|
|
153
150
|
|
|
154
151
|
function mutationFor(ctx: MutationContext & Pick<Context, "set">): SyncMutation<Adapter> {
|
|
155
152
|
const mutation = async (callback: (tx: SyncTransaction<Adapter>) => unknown) => {
|
|
153
|
+
const resolved = runtime();
|
|
154
|
+
const transactional =
|
|
155
|
+
"executeMutation" in resolved.adapter
|
|
156
|
+
? (resolved.adapter as TransactionalSyncAdapter<
|
|
157
|
+
SyncTransaction<Adapter>,
|
|
158
|
+
"async" | "sync"
|
|
159
|
+
>)
|
|
160
|
+
: undefined;
|
|
156
161
|
const active = activeMutations.get(ctx.request);
|
|
157
162
|
if (!(transactional && active)) {
|
|
158
163
|
throw new Error(
|
|
@@ -170,7 +175,7 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
170
175
|
app: syncValidationApp(ctx as Context),
|
|
171
176
|
context: ctx as Context,
|
|
172
177
|
lease: active.lease,
|
|
173
|
-
runtime,
|
|
178
|
+
runtime: resolved,
|
|
174
179
|
onCommit: (result) => atomicResponses.set(ctx.request, result),
|
|
175
180
|
},
|
|
176
181
|
callback
|
|
@@ -214,7 +219,7 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
214
219
|
}
|
|
215
220
|
const key = `${ctx.request.method}:${url.pathname}:${idempotencyKey}`;
|
|
216
221
|
const fingerprint = createMutationFingerprint({ body: ctx.body, request: ctx.request });
|
|
217
|
-
const result = await runtime.adapter.beginMutation({ fingerprint, key, principal });
|
|
222
|
+
const result = await runtime().adapter.beginMutation({ fingerprint, key, principal });
|
|
218
223
|
if (result.kind === "replay") {
|
|
219
224
|
return replayResponse(result.response);
|
|
220
225
|
}
|
|
@@ -228,7 +233,9 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
228
233
|
if (activeMutations.get(ctx.request) === active) {
|
|
229
234
|
scheduleRenewal();
|
|
230
235
|
}
|
|
231
|
-
await runtime
|
|
236
|
+
await runtime()
|
|
237
|
+
.adapter.renewMutation(result.lease)
|
|
238
|
+
.catch(() => undefined);
|
|
232
239
|
}, renewAfter);
|
|
233
240
|
active.renewal.unref?.();
|
|
234
241
|
};
|
|
@@ -248,7 +255,7 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
248
255
|
async function abortMutation(request: Request): Promise<void> {
|
|
249
256
|
const active = releaseMutation(request);
|
|
250
257
|
if (active) {
|
|
251
|
-
await runtime.adapter.abortMutation(active.lease);
|
|
258
|
+
await runtime().adapter.abortMutation(active.lease);
|
|
252
259
|
}
|
|
253
260
|
}
|
|
254
261
|
|
|
@@ -291,7 +298,7 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
291
298
|
? ctx.set.headers
|
|
292
299
|
: effectiveResponseHeaders(ctx.responseValue, ctx.set)
|
|
293
300
|
);
|
|
294
|
-
const completion = await runtime.adapter.completeMutation({
|
|
301
|
+
const completion = await runtime().adapter.completeMutation({
|
|
295
302
|
invalidations,
|
|
296
303
|
lease: active.lease,
|
|
297
304
|
response,
|
|
@@ -300,7 +307,7 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
300
307
|
return leaseLostResponse();
|
|
301
308
|
}
|
|
302
309
|
if (completion.cursor !== undefined) {
|
|
303
|
-
publishCursor(runtime, completion.cursor);
|
|
310
|
+
publishCursor(runtime(), completion.cursor);
|
|
304
311
|
}
|
|
305
312
|
if (result.kind === "unreplayable") {
|
|
306
313
|
return replayResponse(response);
|
|
@@ -314,7 +321,7 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
314
321
|
}
|
|
315
322
|
const invalidations = await applyInvalidations(ctx);
|
|
316
323
|
try {
|
|
317
|
-
publishCursor(runtime, await runtime.adapter.appendInvalidations(invalidations));
|
|
324
|
+
publishCursor(runtime(), await runtime().adapter.appendInvalidations(invalidations));
|
|
318
325
|
} catch {
|
|
319
326
|
getLogger().warn("Sync journal append failed after a non-idempotent mutation");
|
|
320
327
|
}
|
|
@@ -340,12 +347,14 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
340
347
|
}
|
|
341
348
|
try {
|
|
342
349
|
if (!isSuccessfulMutationResponse(ctx)) {
|
|
343
|
-
await runtime.adapter.abortMutation(active.lease);
|
|
350
|
+
await runtime().adapter.abortMutation(active.lease);
|
|
344
351
|
return;
|
|
345
352
|
}
|
|
346
353
|
return await persistMutation(ctx, active);
|
|
347
354
|
} catch (error) {
|
|
348
|
-
await runtime
|
|
355
|
+
await runtime()
|
|
356
|
+
.adapter.abortMutation(active.lease)
|
|
357
|
+
.catch(() => undefined);
|
|
349
358
|
throw error;
|
|
350
359
|
} finally {
|
|
351
360
|
activeMutations.delete(ctx.request);
|
|
@@ -408,7 +417,7 @@ function createSyncPlugin<Adapter extends SyncAdapter>(
|
|
|
408
417
|
}
|
|
409
418
|
const identity: QueryReadIdentity = {
|
|
410
419
|
...urlIdentity(ctx),
|
|
411
|
-
session: principalHash(
|
|
420
|
+
session: principalHash(runtime().adapter, principal),
|
|
412
421
|
};
|
|
413
422
|
ctx.set.headers["x-furin-query"] = serializeQueryHeader(identity);
|
|
414
423
|
})
|
|
@@ -452,9 +461,7 @@ export function furinSync<Adapter extends SyncAdapter>(options: SyncRuntimeOptio
|
|
|
452
461
|
requestOwners.set(request, routeOwner);
|
|
453
462
|
});
|
|
454
463
|
installMutationHandlers(app);
|
|
455
|
-
|
|
456
|
-
bindSyncValidation(app);
|
|
457
|
-
}
|
|
464
|
+
bindSyncValidation(app);
|
|
458
465
|
return app.use(createSyncPlugin(options, owner));
|
|
459
466
|
};
|
|
460
467
|
}
|
|
@@ -235,9 +235,16 @@ export class PostgresSyncAdapter implements SyncAdapter {
|
|
|
235
235
|
});
|
|
236
236
|
}
|
|
237
237
|
|
|
238
|
-
appendInvalidations(
|
|
238
|
+
appendInvalidations(
|
|
239
|
+
invalidations: readonly SyncInvalidation[],
|
|
240
|
+
principal?: string
|
|
241
|
+
): Promise<string> {
|
|
239
242
|
return this.sql.begin(async (tx) => {
|
|
240
|
-
const cursor = await this.appendChangeInTransaction(
|
|
243
|
+
const cursor = await this.appendChangeInTransaction(
|
|
244
|
+
tx,
|
|
245
|
+
invalidations,
|
|
246
|
+
principal === undefined ? null : principalHash(this, principal)
|
|
247
|
+
);
|
|
241
248
|
if (this.publishesNotifications) {
|
|
242
249
|
await tx.notify(this.notificationChannel, cursor);
|
|
243
250
|
}
|
|
@@ -243,7 +243,10 @@ export class RedisSyncAdapter implements SyncAdapter {
|
|
|
243
243
|
return cursor.length === 0 ? { cursor: undefined, kind } : { cursor, kind };
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
-
async appendInvalidations(
|
|
246
|
+
async appendInvalidations(
|
|
247
|
+
invalidations: readonly SyncInvalidation[],
|
|
248
|
+
principal?: string
|
|
249
|
+
): Promise<string> {
|
|
247
250
|
return stringResult(
|
|
248
251
|
await this.client.send("EVAL", [
|
|
249
252
|
APPEND_INVALIDATIONS_SCRIPT,
|
|
@@ -252,6 +255,7 @@ export class RedisSyncAdapter implements SyncAdapter {
|
|
|
252
255
|
this.trimmedKey,
|
|
253
256
|
JSON.stringify(invalidations),
|
|
254
257
|
String(CHANGE_RETENTION),
|
|
258
|
+
principal === undefined ? "" : principalHash(this, principal),
|
|
255
259
|
]),
|
|
256
260
|
"append invalidations cursor"
|
|
257
261
|
);
|
|
@@ -79,5 +79,5 @@ if length >= tonumber(ARGV[2]) then
|
|
|
79
79
|
local oldest = redis.call('XRANGE', KEYS[1], '-', '+', 'COUNT', 1)
|
|
80
80
|
if oldest[1] then redis.call('SET', KEYS[2], oldest[1][1]) end
|
|
81
81
|
end
|
|
82
|
-
return redis.call('XADD', KEYS[1], 'MAXLEN', '=', ARGV[2], '*', 'data', ARGV[1])
|
|
82
|
+
return redis.call('XADD', KEYS[1], 'MAXLEN', '=', ARGV[2], '*', 'data', ARGV[1], 'principal', ARGV[3])
|
|
83
83
|
`;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { inAotBuild } from "elysia";
|
|
1
2
|
import type { SyncAdapter, SyncNotifier, SyncRuntimeOptions } from "./adapter.ts";
|
|
2
3
|
import { PollingSyncNotifier } from "./notifier.ts";
|
|
3
4
|
import { configurePrincipalSecret } from "./principal-journal.ts";
|
|
4
5
|
|
|
5
6
|
const POLL_INTERVAL_MS = 250;
|
|
6
7
|
const pollingNotifiers = new WeakMap<object, PollingSyncNotifier>();
|
|
8
|
+
const factoryAdapters = new WeakMap<SyncRuntimeOptions, SyncAdapter>();
|
|
9
|
+
const runtimes = new WeakMap<SyncRuntimeOptions, Map<boolean, ResolvedSyncRuntime>>();
|
|
7
10
|
|
|
8
11
|
export interface ResolvedSyncRuntime {
|
|
9
12
|
adapter: SyncAdapter;
|
|
@@ -23,34 +26,69 @@ export function publishCursor(runtime: ResolvedSyncRuntime, cursor: string): voi
|
|
|
23
26
|
}
|
|
24
27
|
}
|
|
25
28
|
|
|
29
|
+
/** Prepare resources before listening; AOT only installs the same route behaviors. */
|
|
30
|
+
export function prepareSyncRuntime(options: SyncRuntimeOptions, development: boolean): void {
|
|
31
|
+
if (inAotBuild()) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (typeof options.adapter === "function" && !factoryAdapters.has(options)) {
|
|
35
|
+
const adapter = options.adapter();
|
|
36
|
+
if (adapter instanceof Promise) {
|
|
37
|
+
throw new Error("[furin] Sync adapter factories must return an adapter synchronously.");
|
|
38
|
+
}
|
|
39
|
+
factoryAdapters.set(options, adapter);
|
|
40
|
+
}
|
|
41
|
+
resolveSyncRuntime(options, development);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Reads a prepared factory adapter; request paths never invoke the factory. */
|
|
26
45
|
export function resolveSyncRuntime(
|
|
27
46
|
options: SyncRuntimeOptions,
|
|
28
47
|
development: boolean
|
|
29
48
|
): ResolvedSyncRuntime {
|
|
30
|
-
if (
|
|
49
|
+
if (inAotBuild()) {
|
|
50
|
+
throw new Error("[furin] Sync runtime is unavailable during AOT compilation.");
|
|
51
|
+
}
|
|
52
|
+
const existingRuntime = runtimes.get(options)?.get(development);
|
|
53
|
+
if (existingRuntime) {
|
|
54
|
+
return existingRuntime;
|
|
55
|
+
}
|
|
56
|
+
const adapter =
|
|
57
|
+
typeof options.adapter === "function" ? factoryAdapters.get(options) : options.adapter;
|
|
58
|
+
if (!adapter) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
"[furin] Compose furinSync(sync) or furin({ sync }) before using a Sync adapter factory."
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
if (!development && adapter.scope === "process-local") {
|
|
31
64
|
throw new Error("[furin] Production sync cannot use a process-local SyncAdapter.");
|
|
32
65
|
}
|
|
33
|
-
configurePrincipalSecret(
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
return { adapter: options.adapter, development, notifier: options.notifier };
|
|
66
|
+
configurePrincipalSecret(adapter, options.secret, development);
|
|
67
|
+
if (
|
|
68
|
+
options.notifier &&
|
|
69
|
+
adapter.notificationChannel !== undefined &&
|
|
70
|
+
options.notifier.notificationChannel !== undefined &&
|
|
71
|
+
adapter.notificationChannel !== options.notifier.notificationChannel
|
|
72
|
+
) {
|
|
73
|
+
throw new Error("[furin] SyncAdapter and SyncNotifier notification channels do not match.");
|
|
43
74
|
}
|
|
44
|
-
if (!development &&
|
|
75
|
+
if (!(options.notifier || development) && adapter.scope === "distributed") {
|
|
45
76
|
throw new Error(
|
|
46
77
|
"[furin] Distributed production sync requires an explicit SyncNotifier. Configure the adapter notifier or pass PollingSyncNotifier as a compatibility fallback."
|
|
47
78
|
);
|
|
48
79
|
}
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
80
|
+
let notifier = options.notifier ?? pollingNotifiers.get(adapter);
|
|
81
|
+
if (!notifier) {
|
|
82
|
+
const polling = new PollingSyncNotifier(adapter, POLL_INTERVAL_MS);
|
|
83
|
+
pollingNotifiers.set(adapter, polling);
|
|
84
|
+
notifier = polling;
|
|
85
|
+
}
|
|
86
|
+
const runtime = { adapter, development, notifier };
|
|
87
|
+
let modes = runtimes.get(options);
|
|
88
|
+
if (!modes) {
|
|
89
|
+
modes = new Map();
|
|
90
|
+
runtimes.set(options, modes);
|
|
52
91
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return { adapter: options.adapter, development, notifier };
|
|
92
|
+
modes.set(development, runtime);
|
|
93
|
+
return runtime;
|
|
56
94
|
}
|
|
@@ -228,8 +228,12 @@ export class SqliteSyncAdapter implements SyncAdapter {
|
|
|
228
228
|
return transaction.immediate(input);
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
-
async appendInvalidations(
|
|
232
|
-
|
|
231
|
+
async appendInvalidations(
|
|
232
|
+
invalidations: readonly SyncInvalidation[],
|
|
233
|
+
principal?: string
|
|
234
|
+
): Promise<string> {
|
|
235
|
+
const hash = principal === undefined ? null : principalHash(this, principal);
|
|
236
|
+
return this.database.transaction(() => this.appendChangeSync(invalidations, hash)).immediate();
|
|
233
237
|
}
|
|
234
238
|
|
|
235
239
|
private appendChangeSync(
|
|
@@ -3,9 +3,14 @@ import { IS_DEV } from "../runtime-env.ts";
|
|
|
3
3
|
import type { SyncAdapter, SyncSubscription } from "./adapter.ts";
|
|
4
4
|
import type { FurinSyncOptions } from "./config.ts";
|
|
5
5
|
import { syncRuntimeOptions } from "./config.ts";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
LOCAL_SYNC_CONNECTION,
|
|
8
|
+
type LocalSyncRequest,
|
|
9
|
+
verifiedSyncPrincipal,
|
|
10
|
+
} from "./connection.ts";
|
|
11
|
+
import { principalHash, readPrincipalChanges } from "./principal-journal.ts";
|
|
7
12
|
import { markInternalRead } from "./queries.ts";
|
|
8
|
-
import { type ResolvedSyncRuntime, resolveSyncRuntime } from "./runtime.ts";
|
|
13
|
+
import { prepareSyncRuntime, type ResolvedSyncRuntime, resolveSyncRuntime } from "./runtime.ts";
|
|
9
14
|
|
|
10
15
|
export type { ChangePage as SyncChangePage, SyncChange } from "./adapter.ts";
|
|
11
16
|
|
|
@@ -180,10 +185,11 @@ export function createSyncChangesPlugin(options: FurinSyncOptions) {
|
|
|
180
185
|
/** The changes endpoint of a Furin runtime, validated for that runtime's mode. */
|
|
181
186
|
export function createRuntimeSyncChangesPlugin(options: FurinSyncOptions, development: boolean) {
|
|
182
187
|
const syncPath = options.path ?? defaultSyncPath;
|
|
183
|
-
|
|
188
|
+
prepareSyncRuntime(options, development);
|
|
184
189
|
return new Elysia({ name: `furin-sync-changes-${syncPath}` }).get(
|
|
185
190
|
`${syncPath}/changes`,
|
|
186
191
|
async (context) => {
|
|
192
|
+
const runtime = resolveSyncRuntime(options, development);
|
|
187
193
|
const { request, set } = context;
|
|
188
194
|
markInternalRead(request);
|
|
189
195
|
set.headers["cache-control"] = "no-store";
|
|
@@ -191,10 +197,16 @@ export function createRuntimeSyncChangesPlugin(options: FurinSyncOptions, develo
|
|
|
191
197
|
if ("error" in query) {
|
|
192
198
|
return query.error;
|
|
193
199
|
}
|
|
194
|
-
const principal = await options
|
|
195
|
-
if (principal
|
|
200
|
+
const principal = await verifiedSyncPrincipal(options, context);
|
|
201
|
+
if (principal === undefined) {
|
|
196
202
|
return new Response("Unauthorized", { status: 401 });
|
|
197
203
|
}
|
|
204
|
+
set.headers["x-furin-sync-session"] = principalHash(runtime.adapter, principal);
|
|
205
|
+
const connect = (request as LocalSyncRequest)[LOCAL_SYNC_CONNECTION];
|
|
206
|
+
if (connect) {
|
|
207
|
+
await connect(context, options);
|
|
208
|
+
return new Response(null, { status: 204 });
|
|
209
|
+
}
|
|
198
210
|
return readPrincipalChanges(runtime.adapter, query, principal);
|
|
199
211
|
}
|
|
200
212
|
);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// biome-ignore-all lint/performance/noBarrelFile: public runtime-independent sync entrypoint.
|
|
2
|
+
|
|
3
|
+
export type { LiveQuery } from "./client/query-store.ts";
|
|
4
|
+
export { furinSync } from "./client/sync.ts";
|
|
5
|
+
export {
|
|
6
|
+
createSyncClient,
|
|
7
|
+
type SyncClient,
|
|
8
|
+
SyncClientError,
|
|
9
|
+
type SyncClientOptions,
|
|
10
|
+
type SyncConnectionState,
|
|
11
|
+
type SyncQueryState,
|
|
12
|
+
type SyncReconnectOptions,
|
|
13
|
+
type SyncSocket,
|
|
14
|
+
} from "./client/sync-client.ts";
|
package/dist/server/logger.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { type LoggerConfig } from "evlog";
|
|
2
|
-
/**
|
|
3
|
-
* evlog's logger, and with it head sampling, is process-wide. The first
|
|
4
|
-
* `furin()` initializes it; a later instance may omit `sampling` (it inherits
|
|
5
|
-
* the active one) but must not configure a different one that would be
|
|
6
|
-
* silently ignored.
|
|
7
|
-
*/
|
|
8
|
-
export declare function initializeFurinLogger(config: LoggerConfig): void;
|
|
9
|
-
/** @internal */
|
|
10
|
-
export declare function resetFurinLoggerForTests(): void;
|
package/src/server/logger.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { initLogger, type LoggerConfig } from "evlog";
|
|
2
|
-
|
|
3
|
-
const FURIN_LOGGER_SAMPLING = Symbol.for("@teyik0/furin/logger-sampling");
|
|
4
|
-
|
|
5
|
-
function loggerState(): typeof globalThis & { [key: symbol]: string | undefined } {
|
|
6
|
-
return globalThis as typeof globalThis & { [key: symbol]: string | undefined };
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* evlog's logger, and with it head sampling, is process-wide. The first
|
|
11
|
-
* `furin()` initializes it; a later instance may omit `sampling` (it inherits
|
|
12
|
-
* the active one) but must not configure a different one that would be
|
|
13
|
-
* silently ignored.
|
|
14
|
-
*/
|
|
15
|
-
export function initializeFurinLogger(config: LoggerConfig): void {
|
|
16
|
-
const state = loggerState();
|
|
17
|
-
const sampling = JSON.stringify(config.sampling ?? null);
|
|
18
|
-
const active = state[FURIN_LOGGER_SAMPLING];
|
|
19
|
-
if (active === undefined) {
|
|
20
|
-
initLogger(config);
|
|
21
|
-
state[FURIN_LOGGER_SAMPLING] = sampling;
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
if (config.sampling !== undefined && sampling !== active) {
|
|
25
|
-
throw new Error(
|
|
26
|
-
`[furin] logger.sampling is process-wide (evlog initLogger): this process already samples with ${active}, so ${sampling} would be ignored. Pass the same sampling to every furin() instance, or omit it on later ones.`
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** @internal */
|
|
32
|
-
export function resetFurinLoggerForTests(): void {
|
|
33
|
-
delete loggerState()[FURIN_LOGGER_SAMPLING];
|
|
34
|
-
}
|