@sveltebase/sync 1.4.1 → 1.4.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/README.md CHANGED
@@ -1,32 +1,28 @@
1
1
  # @sveltebase/sync
2
2
 
3
- Reactive, local-first database synchronization for Svelte 5 using a separate Cloudflare Worker for realtime sync.
3
+ Reactive, local-first database synchronization for Svelte 5 on Cloudflare Workers.
4
4
 
5
5
  ## Architecture
6
6
 
7
- `@sveltebase/sync` now uses two Workers:
7
+ `@sveltebase/sync` uses one Cloudflare Worker:
8
8
 
9
9
  ```txt
10
10
  browser
11
11
  -> SvelteKit app Worker
12
- /api/sync -> env.SYNC_WORKER.fetch(request)
13
- -> sync Worker
14
- owns SyncEngine Durable Object
15
- owns websocket upgrades
16
- owns broadcasts
17
- owns sync database/runtime bindings
18
- owns sync auth verification
12
+ /api/sync -> SyncEngine Durable Object
13
+ server writes -> platform.env.SYNC_ENGINE
19
14
  ```
20
15
 
21
- The SvelteKit app Worker does not export sync Durable Objects. This lets apps use the official `@sveltejs/adapter-cloudflare`.
16
+ The production Worker wraps the official `@sveltejs/adapter-cloudflare` output and exports the `SyncEngine` Durable Object. Vite dev uses `syncDevPlugin()` for the WebSocket broker and `adapter-cloudflare` platform proxy for local Cloudflare bindings.
22
17
 
23
18
  ## Imports
24
19
 
25
20
  ```ts
26
21
  import { SyncClient, createLiveQuery } from "@sveltebase/sync/client";
27
22
  import { defineSync, createPublisher } from "@sveltebase/sync/server";
28
- import { syncProxy } from "@sveltebase/sync/sveltekit";
29
- import { defineSyncWorker, SyncEngine } from "@sveltebase/sync/cloudflare";
23
+ import { syncEngineRoute } from "@sveltebase/sync/sveltekit";
24
+ import { createSyncAppWorker, SyncEngine } from "@sveltebase/sync/cloudflare";
25
+ import { syncDevPlugin } from "@sveltebase/sync/vite";
30
26
  ```
31
27
 
32
28
  ## Client
@@ -35,16 +31,7 @@ import { defineSyncWorker, SyncEngine } from "@sveltebase/sync/cloudflare";
35
31
  // src/lib/sync-client.ts
36
32
  import { SyncClient } from "@sveltebase/sync/client";
37
33
 
38
- type AppSchema = {
39
- todos: {
40
- id: string;
41
- title: string;
42
- completed: boolean;
43
- updatedAt: string;
44
- };
45
- };
46
-
47
- export const sync = new SyncClient<AppSchema>({
34
+ export const sync = new SyncClient({
48
35
  name: "app-sync",
49
36
  url: "/api/sync",
50
37
  tables: {
@@ -58,7 +45,7 @@ export const sync = new SyncClient<AppSchema>({
58
45
 
59
46
  ## Sync Handlers
60
47
 
61
- Handlers run in the sync Worker. Use `ctx.platform.env` for Cloudflare bindings, `ctx.auth` for verified auth data, and `ctx.identity` for ownership/scoped fanout.
48
+ Handlers run in the sync engine. Use `ctx.platform.env` for Cloudflare bindings, `ctx.auth` for verified auth data, and `ctx.identity` for ownership/scoped fanout.
62
49
 
63
50
  ```ts
64
51
  // src/lib/server/sync-handlers.ts
@@ -69,14 +56,11 @@ export const todoSync = defineSync({
69
56
 
70
57
  fetch: async (ctx, since) => {
71
58
  const db = ctx.platform.env.DB;
72
- // Query any database here.
73
59
  return [];
74
60
  },
75
61
 
76
62
  authorize: async (ctx) => {
77
- if (!ctx.auth) {
78
- throw new Error("Unauthorized");
79
- }
63
+ if (!ctx.auth) throw new Error("Unauthorized");
80
64
  },
81
65
 
82
66
  scope: (ctx) => {
@@ -87,97 +71,115 @@ export const todoSync = defineSync({
87
71
  export const handlers = [todoSync];
88
72
  ```
89
73
 
90
- ## Sync Worker
91
-
92
- Create a standalone Worker entrypoint that owns the Durable Object:
74
+ ## SvelteKit Route For Vite Dev
93
75
 
94
76
  ```ts
95
- // src/worker/sync.ts
77
+ // src/routes/api/sync/+server.ts
96
78
  import { jwtCookieAuth } from "@sveltebase/auth/sync";
97
- import { defineSyncWorker, SyncEngine } from "@sveltebase/sync/cloudflare";
79
+ import { syncEngineRoute } from "@sveltebase/sync/sveltekit";
98
80
  import { handlers } from "$lib/server/sync-handlers";
99
81
 
100
- export default defineSyncWorker({
82
+ export const { GET } = syncEngineRoute({
101
83
  handlers,
102
84
  auth: jwtCookieAuth(),
85
+ allowUnauthenticated: true,
103
86
  });
104
-
105
- export { SyncEngine };
106
87
  ```
107
88
 
108
- `defineSyncWorker()` handles:
109
-
110
- - `GET /api/sync`: public websocket upgrade endpoint
111
- - `POST /broadcast`: publish one external change
112
- - `POST /broadcast-batch`: publish a batch of external changes
113
-
114
- `GET /websocket` is internal to the sync Worker and Durable Object.
115
-
116
- ## SvelteKit Proxy Route
117
-
118
- Keep browsers connecting to the app origin so existing cookies are sent:
89
+ ## Worker Wrapper For Wrangler And Production
119
90
 
120
91
  ```ts
121
- // src/routes/api/sync/+server.ts
122
- import { SYNC_WORKER_URL } from "$env/static/private";
123
- import { syncProxy } from "@sveltebase/sync/sveltekit";
92
+ // src/worker/app.ts
93
+ import app from "../../.svelte-kit/cloudflare/_worker.js";
94
+ import { jwtCookieAuth } from "@sveltebase/auth/sync";
95
+ import { createSyncAppWorker, SyncEngine } from "@sveltebase/sync/cloudflare";
96
+ import { handlers } from "$lib/server/sync-handlers";
124
97
 
125
- export const { GET, POST } = syncProxy({
126
- fallbackUrl: SYNC_WORKER_URL,
98
+ export default createSyncAppWorker(app, {
99
+ handlers,
100
+ auth: jwtCookieAuth(),
101
+ allowUnauthenticated: true,
127
102
  });
103
+
104
+ export { SyncEngine };
128
105
  ```
129
106
 
130
- In production, configure a Cloudflare service binding named `SYNC_WORKER`. In local development, use `fallbackUrl` such as `http://localhost:8788/api/sync`.
107
+ `createSyncAppWorker()` handles `GET /api/sync` and internal broadcast routes, then delegates all other requests to the adapter output.
131
108
 
132
109
  ## Publishing Server Events
133
110
 
134
- Publishing is explicit. It never reads SvelteKit request context implicitly.
111
+ Publishing targets the current one-worker sync runtime automatically. In production, `createSyncAppWorker()` registers `env.SYNC_ENGINE`; in Vite dev, `syncDevPlugin()` provides the in-process broker.
135
112
 
136
113
  ```ts
137
- import { createPublisher } from "@sveltebase/sync/server";
138
-
139
- type AppSchema = {
140
- todos: { id: string; title: string; updatedAt: string };
141
- };
114
+ import { createBulkPublisher, createPublisher } from "@sveltebase/sync/server";
142
115
 
143
- const publish = createPublisher<AppSchema>({
144
- platform: ctx.platform,
145
- binding: "SYNC_WORKER",
146
- fallbackUrl: env.SYNC_WORKER_URL,
147
- });
116
+ const publish = createPublisher();
117
+ const publishBulk = createBulkPublisher();
148
118
 
149
119
  await publish("todos", "update", todo.id, todo);
120
+ await publishBulk("todos", [
121
+ { action: "update", key: todo.id, data: todo },
122
+ ]);
123
+ ```
124
+
125
+ ## Vite Dev
126
+
127
+ ```ts
128
+ // vite.config.ts
129
+ import { sveltekit } from "@sveltejs/kit/vite";
130
+ import { syncDevPlugin } from "@sveltebase/sync/vite";
131
+ import { jwtCookieAuth } from "@sveltebase/auth/sync";
132
+ import { defineConfig } from "vite";
133
+
134
+ export default defineConfig({
135
+ plugins: [
136
+ syncDevPlugin({
137
+ auth: jwtCookieAuth(),
138
+ allowUnauthenticated: true,
139
+ wranglerConfigPath: "wrangler.local.jsonc",
140
+ }),
141
+ sveltekit(),
142
+ ],
143
+ });
150
144
  ```
151
145
 
152
- Inside the sync Worker, `createPublisher()` publishes directly to `platform.env.SYNC_ENGINE`. Inside the app Worker, it publishes through `platform.env.SYNC_WORKER.fetch()`. Without a binding, it uses `fallbackUrl`.
146
+ ## Svelte Config
147
+
148
+ ```js
149
+ // svelte.config.js
150
+ import adapter from "@sveltejs/adapter-cloudflare";
151
+ import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";
152
+
153
+ export default {
154
+ preprocess: vitePreprocess(),
155
+ kit: {
156
+ adapter: adapter({
157
+ platformProxy: {
158
+ configPath: "wrangler.local.jsonc",
159
+ },
160
+ }),
161
+ },
162
+ };
163
+ ```
153
164
 
154
- ## Cloudflare Configuration
165
+ ## Wrangler Configuration
155
166
 
156
- App Worker:
167
+ Main config for Wrangler dev with remote bindings and production deploy:
157
168
 
158
169
  ```jsonc
170
+ // wrangler.jsonc
159
171
  {
160
172
  "name": "my-app",
161
- "main": ".svelte-kit/cloudflare/_worker.js",
173
+ "main": "src/worker/app.ts",
162
174
  "compatibility_date": "2026-06-07",
163
175
  "compatibility_flags": ["nodejs_compat"],
164
- "services": [
176
+ "d1_databases": [
165
177
  {
166
- "binding": "SYNC_WORKER",
167
- "service": "my-app-sync"
178
+ "binding": "DB",
179
+ "database_name": "my-app",
180
+ "database_id": "..."
168
181
  }
169
- ]
170
- }
171
- ```
172
-
173
- Sync Worker:
174
-
175
- ```jsonc
176
- {
177
- "name": "my-app-sync",
178
- "main": "./src/worker/sync.ts",
179
- "compatibility_date": "2026-06-07",
180
- "compatibility_flags": ["nodejs_compat"],
182
+ ],
181
183
  "durable_objects": {
182
184
  "bindings": [
183
185
  {
@@ -195,9 +197,23 @@ Sync Worker:
195
197
  }
196
198
  ```
197
199
 
198
- Both Workers need the same session secret when using `@sveltebase/auth/sync`:
200
+ Local config for adapter platform proxy in Vite dev:
199
201
 
200
- ```bash
201
- wrangler secret put JWT_SECRET --config wrangler.jsonc
202
- wrangler secret put JWT_SECRET --config wrangler.sync.jsonc
202
+ ```jsonc
203
+ // wrangler.local.jsonc
204
+ {
205
+ "name": "my-app-local",
206
+ "main": ".svelte-kit/cloudflare/_worker.js",
207
+ "compatibility_date": "2026-06-07",
208
+ "compatibility_flags": ["nodejs_compat"],
209
+ "d1_databases": [
210
+ {
211
+ "binding": "DB",
212
+ "database_name": "my-app",
213
+ "database_id": "..."
214
+ }
215
+ ]
216
+ }
203
217
  ```
218
+
219
+ Use `wrangler secret put JWT_SECRET --config wrangler.jsonc` for remote/prod secrets. Use `.env` for Vite dev secrets loaded by the platform proxy.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,KAAK,KAAK,EAAE,MAAM,OAAO,CAAC;AAI1C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,YAAY,EAAE,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CAAC,OAAO,CAAC,GAAG;KAC5G,CAAC,IAAI,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;CACxC,GAAG;IACF,CAAC,SAAS,EAAE,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,UAAU,EAAE,KACvB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACzD,OAAO,EAAE;IACT,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/C,MAAM,EAAE,MAAM,CAAC,MAAM,OAAO,GAAG,MAAM,EAAE,WAAW,CAAC,CAAC;CACrD,KAAK,UAAU,CAAC,OAAO,CAA0B,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"live-query.svelte.d.ts","sourceRoot":"","sources":["live-query.svelte.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI;IAC9B,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,GAAG,CAAC;CACb,CAAC;AAEF,wBAAgB,eAAe,CAAC,CAAC,EAC/B,OAAO,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,EAC7B,YAAY,CAAC,EAAE,MAAM,OAAO,EAAE,GAC7B,cAAc,CAAC,CAAC,CAAC,CA6BnB"}
1
+ {"version":3,"file":"live-query.svelte.d.ts","sourceRoot":"","sources":["../../src/client/live-query.svelte.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI;IAC9B,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,GAAG,CAAC;CACb,CAAC;AAEF,wBAAgB,eAAe,CAAC,CAAC,EAC/B,OAAO,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,EAC7B,YAAY,CAAC,EAAE,MAAM,OAAO,EAAE,GAC7B,cAAc,CAAC,CAAC,CAAC,CA6BnB"}
@@ -1 +1 @@
1
- {"version":3,"file":"status.svelte.d.ts","sourceRoot":"","sources":["status.svelte.ts"],"names":[],"mappings":"AAAA,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,MAAM,CAAqE;IAEnF,IAAI,KAAK,IAIW,YAAY,GAAG,WAAW,GAAG,cAAc,CAF9D;IAED,IAAI,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,WAAW,GAAG,cAAc,EAE9D;CACF"}
1
+ {"version":3,"file":"status.svelte.d.ts","sourceRoot":"","sources":["../../src/client/status.svelte.ts"],"names":[],"mappings":"AAAA,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,MAAM,CAAqE;IAEnF,IAAI,KAAK,IAIW,YAAY,GAAG,WAAW,GAAG,cAAc,CAF9D;IAED,IAAI,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,WAAW,GAAG,cAAc,EAE9D;CACF"}
@@ -0,0 +1,13 @@
1
+ import { type SyncAuthResult } from "../server/handler.js";
2
+ import type { SyncHandler, SyncPlatform } from "../server/index.js";
3
+ export declare function configureSyncEngine(handlers: SyncHandler[]): void;
4
+ export declare function getSyncEngineHandlers(): SyncHandler<any, any>[];
5
+ export type SyncWorkerOptions<TAuth = unknown> = {
6
+ handlers: SyncHandler[];
7
+ websocketPath?: string;
8
+ auth?: (request: Request, platform: SyncPlatform) => Promise<SyncAuthResult<TAuth>> | SyncAuthResult<TAuth>;
9
+ identity?: (auth: TAuth) => string | number | bigint | null | undefined;
10
+ allowUnauthenticated?: boolean;
11
+ };
12
+ export declare function handleSyncRequest<TAuth = unknown>(request: Request, env: Record<string, unknown>, ctx: ExecutionContext, options: SyncWorkerOptions<TAuth>): Promise<Response>;
13
+ //# sourceMappingURL=handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/cloudflare/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,cAAc,EACpB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AA4BpE,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,WAAW,EAAE,QAE1D;AAED,wBAAgB,qBAAqB,4BAEpC;AAED,MAAM,MAAM,iBAAiB,CAAC,KAAK,GAAG,OAAO,IAAI;IAC/C,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,IAAI,CAAC,EAAE,CACL,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,YAAY,KACnB,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IAC5D,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IACxE,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAkFF,wBAAsB,iBAAiB,CAAC,KAAK,GAAG,OAAO,EACrD,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,GAAG,EAAE,gBAAgB,EACrB,OAAO,EAAE,iBAAiB,CAAC,KAAK,CAAC,qBAsClC"}
@@ -0,0 +1,99 @@
1
+ import { configurePublisherPlatform, INTERNAL_AUTH_HEADER, } from "../server/handler.js";
2
+ import { resolveIdentity, serializeConnectionAuth } from "../server/auth.js";
3
+ let activeHandlers = [];
4
+ function createPlatform(request, env, ctx) {
5
+ return {
6
+ env,
7
+ ctx,
8
+ context: ctx,
9
+ caches,
10
+ cf: request.cf,
11
+ };
12
+ }
13
+ function withPath(request, pathname) {
14
+ const url = new URL(request.url);
15
+ url.pathname = pathname;
16
+ return url.toString();
17
+ }
18
+ function isWebSocketRequest(request) {
19
+ return request.headers.get("Upgrade")?.toLowerCase() === "websocket";
20
+ }
21
+ export function configureSyncEngine(handlers) {
22
+ activeHandlers = handlers;
23
+ }
24
+ export function getSyncEngineHandlers() {
25
+ return activeHandlers;
26
+ }
27
+ async function forwardToEngine(request, env) {
28
+ const namespace = env.SYNC_ENGINE;
29
+ if (!namespace) {
30
+ return new Response("Missing SYNC_ENGINE Durable Object binding", {
31
+ status: 500,
32
+ });
33
+ }
34
+ const id = namespace.idFromName("global");
35
+ return namespace.get(id).fetch(request);
36
+ }
37
+ async function handleWebSocket(request, env, ctx, options) {
38
+ if (!isWebSocketRequest(request)) {
39
+ return new Response("Expected Upgrade: websocket", { status: 426 });
40
+ }
41
+ const publicHeaders = new Headers(request.headers);
42
+ publicHeaders.delete(INTERNAL_AUTH_HEADER);
43
+ const publicRequest = new Request(request, {
44
+ headers: publicHeaders,
45
+ });
46
+ const platform = createPlatform(publicRequest, env, ctx);
47
+ let resolvedAuth = null;
48
+ let identity = null;
49
+ if (options.auth) {
50
+ resolvedAuth = (await options.auth(publicRequest, platform)) ?? null;
51
+ if (!resolvedAuth && options.allowUnauthenticated === false) {
52
+ return new Response("Unauthorized", { status: 401 });
53
+ }
54
+ if (resolvedAuth) {
55
+ identity = resolveIdentity(resolvedAuth, options.identity);
56
+ }
57
+ }
58
+ else if (options.allowUnauthenticated === false) {
59
+ return new Response("Unauthorized", { status: 401 });
60
+ }
61
+ const forwardedHeaders = new Headers(publicRequest.headers);
62
+ forwardedHeaders.delete(INTERNAL_AUTH_HEADER);
63
+ if (resolvedAuth) {
64
+ forwardedHeaders.set(INTERNAL_AUTH_HEADER, serializeConnectionAuth(resolvedAuth, identity));
65
+ }
66
+ const forwardedRequest = new Request(withPath(publicRequest, "/websocket"), publicRequest);
67
+ for (const [key, value] of forwardedHeaders) {
68
+ forwardedRequest.headers.set(key, value);
69
+ }
70
+ return forwardToEngine(forwardedRequest, env);
71
+ }
72
+ export async function handleSyncRequest(request, env, ctx, options) {
73
+ configureSyncEngine(options.handlers);
74
+ configurePublisherPlatform({ env });
75
+ const url = new URL(request.url);
76
+ const websocketPath = options.websocketPath ?? "/api/sync";
77
+ const authMetadata = options.auth;
78
+ const allowUnauthenticated = options.allowUnauthenticated ??
79
+ authMetadata?.allowUnauthenticated ??
80
+ true;
81
+ if (url.pathname === websocketPath && request.method === "GET") {
82
+ return handleWebSocket(request, env, ctx, {
83
+ ...options,
84
+ websocketPath,
85
+ allowUnauthenticated,
86
+ });
87
+ }
88
+ if (url.pathname === "/websocket") {
89
+ return new Response("Not found", { status: 404 });
90
+ }
91
+ if ((url.pathname === "/broadcast" ||
92
+ url.pathname === "/broadcast-batch") &&
93
+ request.method === "POST") {
94
+ const headers = new Headers(request.headers);
95
+ headers.delete(INTERNAL_AUTH_HEADER);
96
+ return forwardToEngine(new Request(request, { headers }), env);
97
+ }
98
+ return new Response("Not found", { status: 404 });
99
+ }
@@ -1,16 +1,10 @@
1
1
  import { SyncEngineBase } from "../server/engine.js";
2
- import { type SyncAuthResult } from "../server/handler.js";
3
- import type { SyncHandler, SyncPlatform } from "../server/index.js";
4
- export type SyncWorkerOptions<TAuth = unknown> = {
5
- handlers: SyncHandler[];
6
- durableObjectBinding?: string;
7
- websocketPath?: string;
8
- auth?: (request: Request, platform: SyncPlatform) => Promise<SyncAuthResult<TAuth>> | SyncAuthResult<TAuth>;
9
- identity?: (auth: TAuth) => string | number | bigint | null | undefined;
10
- allowUnauthenticated?: boolean;
2
+ import { type SyncWorkerOptions } from "./handler.js";
3
+ export { configureSyncEngine, handleSyncRequest, type SyncWorkerOptions, } from "./handler.js";
4
+ export type SyncAppWorker = {
5
+ fetch: NonNullable<ExportedHandler["fetch"]>;
11
6
  };
12
- export declare function createSyncWorker<TAuth = unknown>(options: SyncWorkerOptions<TAuth>): ExportedHandler;
13
- export declare function defineSyncWorker<TAuth = unknown>(options: SyncWorkerOptions<TAuth>): ExportedHandler;
7
+ export declare function createSyncAppWorker<TAuth = unknown>(app: SyncAppWorker, options: SyncWorkerOptions<TAuth>): ExportedHandler;
14
8
  export declare class SyncEngine extends SyncEngineBase {
15
9
  constructor(ctx: DurableObjectState, env: Record<string, unknown>);
16
10
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cloudflare/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAEL,KAAK,cAAc,EACpB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AA2CpE,MAAM,MAAM,iBAAiB,CAAC,KAAK,GAAG,OAAO,IAAI;IAC/C,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,IAAI,CAAC,EAAE,CACL,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,YAAY,KACnB,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IAC5D,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IACxE,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AA6FF,wBAAgB,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAC9C,OAAO,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAChC,eAAe,CAgDjB;AAED,wBAAgB,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAC9C,OAAO,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAChC,eAAe,CAEjB;AAED,qBAAa,UAAW,SAAQ,cAAc;gBAChC,GAAG,EAAE,kBAAkB,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAGlE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cloudflare/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,OAAO,EAIL,KAAK,iBAAiB,EACvB,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,KAAK,iBAAiB,GACvB,MAAM,cAAc,CAAC;AAEtB,MAAM,MAAM,aAAa,GAAG;IAC1B,KAAK,EAAE,WAAW,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC;CAC9C,CAAC;AAEF,wBAAgB,mBAAmB,CAAC,KAAK,GAAG,OAAO,EACjD,GAAG,EAAE,aAAa,EAClB,OAAO,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAChC,eAAe,CA0BjB;AAED,qBAAa,UAAW,SAAQ,cAAc;gBAChC,GAAG,EAAE,kBAAkB,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAGlE"}
@@ -1,116 +1,26 @@
1
1
  import { SyncEngineBase } from "../server/engine.js";
2
- import { INTERNAL_AUTH_HEADER, } from "../server/handler.js";
3
- let activeHandlers = [];
4
- function defaultIdentity(auth) {
5
- const value = auth?.identity ?? auth?.user?.id ?? auth?.userId;
6
- return value == null ? null : String(value);
7
- }
8
- function serializeConnectionAuth(auth, identity) {
9
- const payload = { auth, identity };
10
- return btoa(unescape(encodeURIComponent(JSON.stringify(payload))));
11
- }
12
- function createPlatform(request, env, ctx) {
13
- return {
14
- env,
15
- ctx,
16
- context: ctx,
17
- caches,
18
- cf: request.cf,
19
- };
20
- }
21
- function withPath(request, pathname) {
22
- const url = new URL(request.url);
23
- url.pathname = pathname;
24
- return url.toString();
25
- }
26
- function isWebSocketRequest(request) {
27
- return request.headers.get("Upgrade")?.toLowerCase() === "websocket";
28
- }
29
- async function forwardToEngine(request, env, durableObjectBinding) {
30
- const namespace = env[durableObjectBinding];
31
- if (!namespace) {
32
- return new Response(`Missing ${durableObjectBinding} Durable Object binding`, { status: 500 });
33
- }
34
- const id = namespace.idFromName("global");
35
- return namespace.get(id).fetch(request);
36
- }
37
- async function handleWebSocket(request, env, ctx, options) {
38
- if (!isWebSocketRequest(request)) {
39
- return new Response("Expected Upgrade: websocket", { status: 426 });
40
- }
41
- const publicHeaders = new Headers(request.headers);
42
- publicHeaders.delete(INTERNAL_AUTH_HEADER);
43
- const publicRequest = new Request(request, {
44
- headers: publicHeaders,
45
- });
46
- const platform = createPlatform(publicRequest, env, ctx);
47
- let resolvedAuth = null;
48
- let identity = null;
49
- if (options.auth) {
50
- resolvedAuth = (await options.auth(publicRequest, platform)) ?? null;
51
- if (!resolvedAuth && options.allowUnauthenticated === false) {
52
- return new Response("Unauthorized", { status: 401 });
53
- }
54
- if (resolvedAuth) {
55
- const identityValue = options.identity
56
- ? options.identity(resolvedAuth)
57
- : defaultIdentity(resolvedAuth);
58
- identity = identityValue == null ? null : String(identityValue);
59
- }
60
- }
61
- else if (options.allowUnauthenticated === false) {
62
- return new Response("Unauthorized", { status: 401 });
63
- }
64
- const forwardedHeaders = new Headers(publicRequest.headers);
65
- forwardedHeaders.delete(INTERNAL_AUTH_HEADER);
66
- if (resolvedAuth) {
67
- forwardedHeaders.set(INTERNAL_AUTH_HEADER, serializeConnectionAuth(resolvedAuth, identity));
68
- }
69
- const forwardedRequest = new Request(withPath(publicRequest, "/websocket"), publicRequest);
70
- for (const [key, value] of forwardedHeaders) {
71
- forwardedRequest.headers.set(key, value);
72
- }
73
- return forwardToEngine(forwardedRequest, env, options.durableObjectBinding);
74
- }
75
- export function createSyncWorker(options) {
76
- activeHandlers = options.handlers;
77
- const durableObjectBinding = options.durableObjectBinding ?? "SYNC_ENGINE";
78
- const websocketPath = options.websocketPath ?? "/api/sync";
79
- const authMetadata = options.auth;
80
- const allowUnauthenticated = options.allowUnauthenticated ??
81
- authMetadata?.allowUnauthenticated ??
82
- true;
2
+ import { configurePublisherPlatform } from "../server/handler.js";
3
+ import { configureSyncEngine, getSyncEngineHandlers, handleSyncRequest, } from "./handler.js";
4
+ export { configureSyncEngine, handleSyncRequest, } from "./handler.js";
5
+ export function createSyncAppWorker(app, options) {
6
+ configureSyncEngine(options.handlers);
83
7
  return {
84
8
  async fetch(request, env, ctx) {
9
+ configurePublisherPlatform({ env: env });
85
10
  const url = new URL(request.url);
86
- const workerEnv = env;
87
- if (url.pathname === websocketPath && request.method === "GET") {
88
- return handleWebSocket(request, workerEnv, ctx, {
89
- ...options,
90
- durableObjectBinding,
91
- websocketPath,
92
- allowUnauthenticated,
93
- });
94
- }
95
- if (url.pathname === "/websocket") {
96
- return new Response("Not found", { status: 404 });
11
+ const websocketPath = options.websocketPath ?? "/api/sync";
12
+ if ((url.pathname === websocketPath && request.method === "GET") ||
13
+ ((url.pathname === "/broadcast" ||
14
+ url.pathname === "/broadcast-batch") &&
15
+ request.method === "POST")) {
16
+ return handleSyncRequest(request, env, ctx, options);
97
17
  }
98
- if ((url.pathname === "/broadcast" ||
99
- url.pathname === "/broadcast-batch") &&
100
- request.method === "POST") {
101
- const headers = new Headers(request.headers);
102
- headers.delete(INTERNAL_AUTH_HEADER);
103
- return forwardToEngine(new Request(request, { headers }), workerEnv, durableObjectBinding);
104
- }
105
- return new Response("Not found", { status: 404 });
18
+ return app.fetch(request, env, ctx);
106
19
  },
107
20
  };
108
21
  }
109
- export function defineSyncWorker(options) {
110
- return createSyncWorker(options);
111
- }
112
22
  export class SyncEngine extends SyncEngineBase {
113
23
  constructor(ctx, env) {
114
- super(ctx, env, activeHandlers);
24
+ super(ctx, env, getSyncEngineHandlers());
115
25
  }
116
26
  }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { SyncClient, createLiveQuery } from "./client/index.js";
2
2
  export type { LiveQueryState } from "./client/index.js";
3
3
  export { defineSync } from "./server/index.js";
4
- export type { BulkPublishFn, InferSchemaFromHandlers, PublishEventData, PublishFn, SyncAuthResult, SyncConnectionAuth, SyncContext, SyncHandler, SyncPlatform, SyncPublisherOptions, } from "./server/index.js";
4
+ export type { BulkPublishFn, PublishEventData, PublishFn, SyncAuthResult, SyncConnectionAuth, SyncContext, SyncHandler, SyncPlatform, } from "./server/index.js";
5
5
  export type { SyncMessage } from "./protocol.js";
6
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAChE,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,YAAY,EACV,aAAa,EACb,uBAAuB,EACvB,gBAAgB,EAChB,SAAS,EACT,cAAc,EACd,kBAAkB,EAClB,WAAW,EACX,WAAW,EACX,YAAY,EACZ,oBAAoB,GACrB,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAChE,YAAY,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,YAAY,EACV,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,cAAc,EACd,kBAAkB,EAClB,WAAW,EACX,WAAW,EACX,YAAY,GACb,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"protocol.d.ts","source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EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;IACvC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GACD;IACE,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,KAAK,CAAC;QACb,MAAM,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAC;QACvC,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,IAAI,CAAC,EAAE,GAAG,CAAC;KACZ,CAAC,CAAC;CACJ,CAAC;AAEN,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAcjE"}
@@ -0,0 +1,8 @@
1
+ export type SerializedConnectionAuth = {
2
+ auth: any;
3
+ identity: string | null;
4
+ };
5
+ export declare function resolveIdentity(auth: any, identity?: (auth: any) => string | number | bigint | null | undefined): string | null;
6
+ export declare function serializeConnectionAuth(auth: any, identity: string | null): string;
7
+ export declare function deserializeConnectionAuth(value: string | null): SerializedConnectionAuth | null;
8
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/server/auth.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,wBAAwB,GAAG;IACrC,IAAI,EAAE,GAAG,CAAC;IACV,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB,CAAC;AAEF,wBAAgB,eAAe,CAC7B,IAAI,EAAE,GAAG,EACT,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,GACpE,MAAM,GAAG,IAAI,CAKf;AAED,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,GAAG,EACT,QAAQ,EAAE,MAAM,GAAG,IAAI,GACtB,MAAM,CAIR;AAED,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,MAAM,GAAG,IAAI,GACnB,wBAAwB,GAAG,IAAI,CAQjC"}
@@ -0,0 +1,19 @@
1
+ export function resolveIdentity(auth, identity) {
2
+ const value = identity
3
+ ? identity(auth)
4
+ : (auth?.identity ?? auth?.user?.id ?? auth?.userId);
5
+ return value == null ? null : String(value);
6
+ }
7
+ export function serializeConnectionAuth(auth, identity) {
8
+ return btoa(unescape(encodeURIComponent(JSON.stringify({ auth, identity }))));
9
+ }
10
+ export function deserializeConnectionAuth(value) {
11
+ if (!value)
12
+ return null;
13
+ try {
14
+ return JSON.parse(decodeURIComponent(escape(atob(value))));
15
+ }
16
+ catch {
17
+ return null;
18
+ }
19
+ }