@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 +102 -86
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/live-query.svelte.d.ts.map +1 -1
- package/dist/client/status.svelte.d.ts.map +1 -1
- package/dist/cloudflare/handler.d.ts +13 -0
- package/dist/cloudflare/handler.d.ts.map +1 -0
- package/dist/cloudflare/handler.js +99 -0
- package/dist/cloudflare/index.d.ts +5 -11
- package/dist/cloudflare/index.d.ts.map +1 -1
- package/dist/cloudflare/index.js +14 -104
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/server/auth.d.ts +8 -0
- package/dist/server/auth.d.ts.map +1 -0
- package/dist/server/auth.js +19 -0
- package/dist/server/broker.d.ts +1 -3
- package/dist/server/broker.d.ts.map +1 -1
- package/dist/server/broker.js +15 -29
- package/dist/server/dev-engine.d.ts +22 -0
- package/dist/server/dev-engine.d.ts.map +1 -0
- package/dist/server/dev-engine.js +159 -0
- package/dist/server/engine.d.ts.map +1 -1
- package/dist/server/engine.js +1 -10
- package/dist/server/handler.d.ts +4 -14
- package/dist/server/handler.d.ts.map +1 -1
- package/dist/server/handler.js +31 -45
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/sveltekit/index.d.ts +3 -6
- package/dist/sveltekit/index.d.ts.map +1 -1
- package/dist/sveltekit/index.js +16 -20
- package/dist/vite.d.ts +8 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +56 -0
- package/package.json +11 -2
package/dist/server/broker.d.ts
CHANGED
|
@@ -12,14 +12,12 @@ export interface ISyncConnection {
|
|
|
12
12
|
}
|
|
13
13
|
export declare class SyncBroker {
|
|
14
14
|
private handlers;
|
|
15
|
+
private dynamicHandlers;
|
|
15
16
|
private connections;
|
|
16
17
|
constructor(handlers: SyncHandler[]);
|
|
17
18
|
setHandlers(handlers: SyncHandler[]): void;
|
|
18
19
|
registerConnection(conn: ISyncConnection): void;
|
|
19
20
|
removeConnection(conn: ISyncConnection): void;
|
|
20
|
-
/**
|
|
21
|
-
* Resolves the appropriate handler for a channel name.
|
|
22
|
-
*/
|
|
23
21
|
private findHandler;
|
|
24
22
|
handleMessage(conn: ISyncConnection, rawMessage: string, platform: SyncPlatform, request: Request): Promise<void>;
|
|
25
23
|
private broadcastChange;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"broker.d.ts","sourceRoot":"","sources":["../../src/server/broker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAe,YAAY,EAAE,MAAM,YAAY,CAAC;AAGzE,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5C,OAAO,IAAI,GAAG,CAAC;IACf,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,CAAC;IACzB,WAAW,IAAI,MAAM,GAAG,IAAI,CAAC;IAC7B,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;IAC3C,qBAAqB,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC;IACrC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,
|
|
1
|
+
{"version":3,"file":"broker.d.ts","sourceRoot":"","sources":["../../src/server/broker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAe,YAAY,EAAE,MAAM,YAAY,CAAC;AAGzE,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,IAAI,EAAE,MAAM,GA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AkBvF"}
|
package/dist/server/broker.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { parseSyncMessage } from "../protocol.js";
|
|
2
2
|
export class SyncBroker {
|
|
3
|
-
handlers;
|
|
3
|
+
handlers = new Map();
|
|
4
|
+
dynamicHandlers = [];
|
|
4
5
|
connections = new Set();
|
|
5
6
|
constructor(handlers) {
|
|
6
|
-
this.handlers = new Map();
|
|
7
7
|
this.setHandlers(handlers);
|
|
8
8
|
}
|
|
9
9
|
setHandlers(handlers) {
|
|
10
10
|
this.handlers.clear();
|
|
11
|
+
this.dynamicHandlers = [];
|
|
11
12
|
for (const h of handlers) {
|
|
12
|
-
// Resolve static channel to register, or we can register dynamic channel routing
|
|
13
|
-
// If it's a dynamic channel resolver function, we will match channel prefix or resolve dynamically
|
|
14
|
-
// Let's support both static channels and dynamic channel prefixes (or lookups).
|
|
15
|
-
// If config.channel is a function, we register it under a wildcard or handle it dynamically.
|
|
16
13
|
if (typeof h.config.channel === "string") {
|
|
17
14
|
this.handlers.set(h.config.channel, h);
|
|
18
15
|
}
|
|
16
|
+
else {
|
|
17
|
+
this.dynamicHandlers.push(h);
|
|
18
|
+
}
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
registerConnection(conn) {
|
|
@@ -24,33 +24,19 @@ export class SyncBroker {
|
|
|
24
24
|
removeConnection(conn) {
|
|
25
25
|
this.connections.delete(conn);
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
* Resolves the appropriate handler for a channel name.
|
|
29
|
-
*/
|
|
30
|
-
findHandler(channel) {
|
|
31
|
-
// Static match
|
|
27
|
+
findHandler(channel, ctx) {
|
|
32
28
|
const handler = this.handlers.get(channel);
|
|
33
29
|
if (handler)
|
|
34
30
|
return handler;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// We'll check if it matches in some way, but generally dynamic channel is resolved
|
|
39
|
-
// during subscribe and stored.
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
// Fallback: search all handlers to see if they resolve to this channel for a generic context
|
|
43
|
-
// (this is rare, usually channels are static or follow a pattern like channel:userId)
|
|
44
|
-
for (const [, h] of this.handlers) {
|
|
45
|
-
if (typeof h.config.channel === "function") {
|
|
46
|
-
// Let's assume dynamic channels are in format "prefix:id". We can match by prefix.
|
|
47
|
-
const staticChannelPrefix = typeof h.config.channel === "string" ? h.config.channel : "";
|
|
48
|
-
if (staticChannelPrefix && channel.startsWith(staticChannelPrefix)) {
|
|
31
|
+
for (const h of this.dynamicHandlers) {
|
|
32
|
+
try {
|
|
33
|
+
if (h.resolveChannel(ctx) === channel)
|
|
49
34
|
return h;
|
|
50
|
-
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// Ignore handlers that cannot resolve for this connection context.
|
|
51
38
|
}
|
|
52
39
|
}
|
|
53
|
-
// Let's support matching prefix like "todos:" -> match the todos handler
|
|
54
40
|
const colonIndex = channel.indexOf(":");
|
|
55
41
|
if (colonIndex !== -1) {
|
|
56
42
|
const prefix = channel.substring(0, colonIndex);
|
|
@@ -77,7 +63,7 @@ export class SyncBroker {
|
|
|
77
63
|
conn.send("pong");
|
|
78
64
|
break;
|
|
79
65
|
case "subscribe": {
|
|
80
|
-
const handler = this.findHandler(msg.channel);
|
|
66
|
+
const handler = this.findHandler(msg.channel, ctx);
|
|
81
67
|
if (!handler) {
|
|
82
68
|
conn.send(JSON.stringify({
|
|
83
69
|
type: "reject",
|
|
@@ -105,7 +91,7 @@ export class SyncBroker {
|
|
|
105
91
|
conn.getSubscribedChannels().delete(msg.channel);
|
|
106
92
|
break;
|
|
107
93
|
case "mutate": {
|
|
108
|
-
const handler = this.findHandler(msg.channel);
|
|
94
|
+
const handler = this.findHandler(msg.channel, ctx);
|
|
109
95
|
if (!handler) {
|
|
110
96
|
conn.send(JSON.stringify({
|
|
111
97
|
type: "reject",
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { IncomingMessage } from "node:http";
|
|
2
|
+
import type { SyncHandler, SyncPlatform } from "./index.js";
|
|
3
|
+
export type SyncDevAuthOptions<TAuth = unknown> = {
|
|
4
|
+
auth?: (request: Request, platform: SyncPlatform) => Promise<TAuth | null | undefined> | TAuth | null | undefined;
|
|
5
|
+
identity?: (auth: TAuth) => string | number | bigint | null | undefined;
|
|
6
|
+
allowUnauthenticated?: boolean;
|
|
7
|
+
platform?: SyncPlatform | (() => Promise<SyncPlatform> | SyncPlatform);
|
|
8
|
+
wranglerConfigPath?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare function setHandlers(handlers: SyncHandler[]): void;
|
|
11
|
+
export declare function addClient(ws: {
|
|
12
|
+
send: (data: string) => void;
|
|
13
|
+
close: (code?: number, reason?: string) => void;
|
|
14
|
+
on: (event: string, listener: (...args: any[]) => void) => void;
|
|
15
|
+
}, req: IncomingMessage, options?: SyncDevAuthOptions): Promise<boolean>;
|
|
16
|
+
export declare function broadcastExternalChange(channel: string, action: "create" | "update" | "delete", key: string | undefined, data: any): Promise<void>;
|
|
17
|
+
export declare function broadcastExternalBatchChange(channel: string, changes: Array<{
|
|
18
|
+
action: "create" | "update" | "delete";
|
|
19
|
+
key?: string;
|
|
20
|
+
data?: any;
|
|
21
|
+
}>): Promise<void>;
|
|
22
|
+
//# sourceMappingURL=dev-engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dev-engine.d.ts","sourceRoot":"","sources":["../../src/server/dev-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAGjD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAW5D,MAAM,MAAM,kBAAkB,CAAC,KAAK,GAAG,OAAO,IAAI;IAChD,IAAI,CAAC,EAAE,CACL,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,YAAY,KACnB,OAAO,CAAC,KAAK,GAAG,IAAI,GAAG,SAAS,CAAC,GAA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,159 @@
|
|
|
1
|
+
import { resolveIdentity } from "./auth.js";
|
|
2
|
+
import { SyncBroker } from "./broker.js";
|
|
3
|
+
const GLOBAL_BROKER_KEY = "__sveltebase_sync_dev_broker__";
|
|
4
|
+
const GLOBAL_PLATFORM_KEY = "__sveltebase_sync_dev_platform__";
|
|
5
|
+
let devBroker = null;
|
|
6
|
+
function setGlobalBroker(state) {
|
|
7
|
+
const globalObject = globalThis;
|
|
8
|
+
globalObject[GLOBAL_BROKER_KEY] = state;
|
|
9
|
+
}
|
|
10
|
+
function getGlobalBroker() {
|
|
11
|
+
const globalObject = globalThis;
|
|
12
|
+
return globalObject[GLOBAL_BROKER_KEY];
|
|
13
|
+
}
|
|
14
|
+
export function setHandlers(handlers) {
|
|
15
|
+
const existing = getGlobalBroker();
|
|
16
|
+
if (!existing) {
|
|
17
|
+
const state = { broker: new SyncBroker(handlers) };
|
|
18
|
+
setGlobalBroker(state);
|
|
19
|
+
devBroker = state.broker;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
existing.broker.setHandlers(handlers);
|
|
23
|
+
devBroker = existing.broker;
|
|
24
|
+
}
|
|
25
|
+
function getDevBroker() {
|
|
26
|
+
if (devBroker)
|
|
27
|
+
return devBroker;
|
|
28
|
+
const existing = getGlobalBroker();
|
|
29
|
+
if (existing) {
|
|
30
|
+
devBroker = existing.broker;
|
|
31
|
+
return existing.broker;
|
|
32
|
+
}
|
|
33
|
+
throw new Error("Sync dev broker not initialized. Call setHandlers first.");
|
|
34
|
+
}
|
|
35
|
+
function getHeaderValue(value) {
|
|
36
|
+
if (Array.isArray(value))
|
|
37
|
+
return value[0];
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
function headersFromIncomingMessage(req) {
|
|
41
|
+
const headers = new Headers();
|
|
42
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
43
|
+
if (value === undefined)
|
|
44
|
+
continue;
|
|
45
|
+
if (Array.isArray(value)) {
|
|
46
|
+
for (const item of value)
|
|
47
|
+
headers.append(key, item);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
headers.set(key, value);
|
|
51
|
+
}
|
|
52
|
+
return headers;
|
|
53
|
+
}
|
|
54
|
+
function requestFromIncomingMessage(req) {
|
|
55
|
+
const host = getHeaderValue(req.headers.host) ?? "localhost";
|
|
56
|
+
const url = new URL(req.url ?? "", `http://${host}`);
|
|
57
|
+
return new Request(url.toString(), {
|
|
58
|
+
headers: headersFromIncomingMessage(req),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async function resolvePlatform(options) {
|
|
62
|
+
if (options?.platform) {
|
|
63
|
+
return typeof options.platform === "function"
|
|
64
|
+
? await options.platform()
|
|
65
|
+
: options.platform;
|
|
66
|
+
}
|
|
67
|
+
const globalObject = globalThis;
|
|
68
|
+
const existing = globalObject[GLOBAL_PLATFORM_KEY];
|
|
69
|
+
if (existing)
|
|
70
|
+
return existing.platform;
|
|
71
|
+
try {
|
|
72
|
+
const { getPlatformProxy } = await import("wrangler");
|
|
73
|
+
const proxy = await getPlatformProxy(options?.wranglerConfigPath
|
|
74
|
+
? { configPath: options.wranglerConfigPath }
|
|
75
|
+
: undefined);
|
|
76
|
+
const platform = proxy;
|
|
77
|
+
globalObject[GLOBAL_PLATFORM_KEY] = { platform };
|
|
78
|
+
return platform;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
const platform = { env: {} };
|
|
82
|
+
globalObject[GLOBAL_PLATFORM_KEY] = { platform };
|
|
83
|
+
return platform;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export async function addClient(ws, req, options) {
|
|
87
|
+
const broker = getDevBroker();
|
|
88
|
+
const request = requestFromIncomingMessage(req);
|
|
89
|
+
const platform = await resolvePlatform(options);
|
|
90
|
+
const subscribedChannels = new Set();
|
|
91
|
+
let auth = null;
|
|
92
|
+
let identity = null;
|
|
93
|
+
try {
|
|
94
|
+
if (options?.auth) {
|
|
95
|
+
auth = (await options.auth(request, platform)) ?? null;
|
|
96
|
+
if (!auth && options.allowUnauthenticated === false) {
|
|
97
|
+
ws.close(1008, "Unauthorized");
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (auth) {
|
|
101
|
+
identity = resolveIdentity(auth, options.identity);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
ws.close(1011, "Internal server error");
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
const conn = {
|
|
110
|
+
send(data) {
|
|
111
|
+
ws.send(data);
|
|
112
|
+
},
|
|
113
|
+
close(code, reason) {
|
|
114
|
+
ws.close(code, reason);
|
|
115
|
+
},
|
|
116
|
+
getAuth() {
|
|
117
|
+
return auth;
|
|
118
|
+
},
|
|
119
|
+
setAuth(newAuth) {
|
|
120
|
+
auth = newAuth;
|
|
121
|
+
},
|
|
122
|
+
getIdentity() {
|
|
123
|
+
return identity;
|
|
124
|
+
},
|
|
125
|
+
setIdentity(newIdentity) {
|
|
126
|
+
identity = newIdentity;
|
|
127
|
+
},
|
|
128
|
+
getSubscribedChannels() {
|
|
129
|
+
return subscribedChannels;
|
|
130
|
+
},
|
|
131
|
+
headers: request.headers,
|
|
132
|
+
url: request.url,
|
|
133
|
+
};
|
|
134
|
+
broker.registerConnection(conn);
|
|
135
|
+
ws.on("message", async (data) => {
|
|
136
|
+
const message = typeof data === "string" ? data : String(data);
|
|
137
|
+
try {
|
|
138
|
+
await broker.handleMessage(conn, message, platform, request);
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
console.error("sync dev engine: error handling message", err);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
ws.on("close", () => {
|
|
145
|
+
broker.removeConnection(conn);
|
|
146
|
+
});
|
|
147
|
+
ws.on("error", () => {
|
|
148
|
+
broker.removeConnection(conn);
|
|
149
|
+
});
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
export async function broadcastExternalChange(channel, action, key, data) {
|
|
153
|
+
const broker = getDevBroker();
|
|
154
|
+
await broker.handleExternalChange(channel, action, key, data);
|
|
155
|
+
}
|
|
156
|
+
export async function broadcastExternalBatchChange(channel, changes) {
|
|
157
|
+
const broker = getDevBroker();
|
|
158
|
+
await broker.handleExternalBatchChange(channel, changes);
|
|
159
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../src/server/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../src/server/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EAAE,UAAU,EAAwB,MAAM,aAAa,CAAC;AAE/D,OAAO,KAAK,EAAE,WAAW,EAAgB,MAAM,YAAY,CAAC;AAE5D,KAAK,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAqD7C,qBAAa,cAAe,SAAQ,aAAa,CAAC,aAAa,CAAC;IAC9D,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC;IAC7B,OAAO,CAAC,OAAO,CAAkD;gBAG/D,GAAG,EAAE,kBAAkB,EACvB,GAAG,EAAE,aAAa,EAClB,QAAQ,EAAE,WAAW,EAAE;IAMnB,KAAK,CAAC,OAAO,EAAE,OAAO;IAoC5B,OAAO,CAAC,gBAAgB;IA4BlB,gBAAgB,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW;IAoBnE,cAAc,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAa1D,cAAc,CAAC,EAAE,EAAE,SAAS;CAO7B"}
|
package/dist/server/engine.js
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
import { DurableObject } from "cloudflare:workers";
|
|
2
|
+
import { deserializeConnectionAuth } from "./auth.js";
|
|
2
3
|
import { SyncBroker } from "./broker.js";
|
|
3
4
|
import { INTERNAL_AUTH_HEADER } from "./handler.js";
|
|
4
|
-
function deserializeConnectionAuth(value) {
|
|
5
|
-
if (!value)
|
|
6
|
-
return null;
|
|
7
|
-
try {
|
|
8
|
-
return JSON.parse(decodeURIComponent(escape(atob(value))));
|
|
9
|
-
}
|
|
10
|
-
catch {
|
|
11
|
-
return null;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
5
|
class CloudflareSyncConnection {
|
|
15
6
|
ws;
|
|
16
7
|
auth = null;
|
package/dist/server/handler.d.ts
CHANGED
|
@@ -1,26 +1,16 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SyncPlatform } from "./index.js";
|
|
2
2
|
export type SyncAuthResult<TAuth> = TAuth | null | undefined;
|
|
3
3
|
export declare const INTERNAL_AUTH_HEADER = "x-sveltebase-sync-auth";
|
|
4
4
|
export type PublishEventData<TRecord, TAction extends "create" | "update" | "delete"> = TAction extends "create" ? TRecord : TAction extends "update" ? Partial<TRecord> : {
|
|
5
5
|
updatedAt?: string;
|
|
6
6
|
} | undefined;
|
|
7
|
-
export type InferSchemaFromHandlers<T extends SyncHandler[]> = {
|
|
8
|
-
[K in T[number] as K["config"]["channel"] extends string ? K["config"]["channel"] : K["config"]["channel"] extends (...args: any[]) => infer R ? R extends string ? R : string : string]: K extends SyncHandler<infer TRow> ? TRow : never;
|
|
9
|
-
};
|
|
10
|
-
export type SyncPublisherOptions = {
|
|
11
|
-
binding?: string;
|
|
12
|
-
fallbackUrl?: string;
|
|
13
|
-
durableObjectBinding?: string;
|
|
14
|
-
platform?: SyncPlatform;
|
|
15
|
-
};
|
|
16
7
|
export type PublishFn<TSchema extends Record<string, unknown>> = <TChannel extends keyof TSchema & string, TAction extends "create" | "update" | "delete">(channel: TChannel | `${TChannel}:${string}`, action: TAction, key: string | undefined, data: PublishEventData<TSchema[TChannel], TAction>) => Promise<void>;
|
|
17
8
|
export type BulkPublishFn<TSchema extends Record<string, unknown>> = <TChannel extends keyof TSchema & string>(channel: TChannel | `${TChannel}:${string}`, changes: Array<{
|
|
18
9
|
action: "create" | "update" | "delete";
|
|
19
10
|
key?: string;
|
|
20
11
|
data?: any;
|
|
21
12
|
}>) => Promise<void>;
|
|
22
|
-
export declare function
|
|
23
|
-
export declare function createPublisher<
|
|
24
|
-
export declare function createBulkPublisher<TSchema extends Record<string, unknown>>(
|
|
25
|
-
export declare function createBulkPublisher<THandlers extends SyncHandler[]>(options: SyncPublisherOptions, handlers: THandlers): BulkPublishFn<InferSchemaFromHandlers<THandlers>>;
|
|
13
|
+
export declare function configurePublisherPlatform(platform: SyncPlatform): void;
|
|
14
|
+
export declare function createPublisher<TSchema extends Record<string, unknown>>(): PublishFn<TSchema>;
|
|
15
|
+
export declare function createBulkPublisher<TSchema extends Record<string, unknown>>(): BulkPublishFn<TSchema>;
|
|
26
16
|
//# sourceMappingURL=handler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/server/handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,MAAM,cAAc,CAAC,KAAK,IAAI,KAAK,GAAG,IAAI,GAAG,SAAS,CAAC;AAE7D,eAAO,MAAM,oBAAoB,2BAA2B,CAAC;AAE7D,MAAM,MAAM,gBAAgB,CAC1B,OAAO,EACP,OAAO,SAAS,QAAQ,GAAG,QAAQ,GAAG,QAAQ,IAC5C,OAAO,SAAS,QAAQ,GACxB,OAAO,GACP,OAAO,SAAS,QAAQ,GACtB,OAAO,CAAC,OAAO,CAAC,GAChB;IAAE,SAAS,CAAC,EAAE,MAAM,CA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
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
const GLOBAL_PLATFORM_KEY = "__sveltebase_sync_platform__";
|
|
3
|
+
function hasDevBroker() {
|
|
4
|
+
const globalObject = globalThis;
|
|
5
|
+
return Boolean(globalObject.__sveltebase_sync_dev_broker__);
|
|
4
6
|
}
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
+
export function configurePublisherPlatform(platform) {
|
|
8
|
+
const globalObject = globalThis;
|
|
9
|
+
globalObject[GLOBAL_PLATFORM_KEY] = platform;
|
|
7
10
|
}
|
|
8
|
-
|
|
9
|
-
const
|
|
11
|
+
function getPublisherPlatform() {
|
|
12
|
+
const globalObject = globalThis;
|
|
13
|
+
return globalObject[GLOBAL_PLATFORM_KEY];
|
|
14
|
+
}
|
|
15
|
+
async function publishToDurableObject(platform, pathname, body) {
|
|
16
|
+
const namespace = platform.env.SYNC_ENGINE;
|
|
10
17
|
if (!namespace) {
|
|
11
|
-
throw new Error(
|
|
18
|
+
throw new Error("Missing SYNC_ENGINE Durable Object binding");
|
|
12
19
|
}
|
|
13
20
|
const id = namespace.idFromName("global");
|
|
14
21
|
const stub = namespace.get(id);
|
|
@@ -21,49 +28,29 @@ async function publishToDurableObject(platform, durableObjectBinding, pathname,
|
|
|
21
28
|
throw new Error(await response.text());
|
|
22
29
|
}
|
|
23
30
|
}
|
|
24
|
-
async function
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
body: JSON.stringify(body),
|
|
29
|
-
});
|
|
30
|
-
if (!response.ok) {
|
|
31
|
-
throw new Error(await response.text());
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
async function publishToFallbackUrl(fallbackUrl, pathname, body) {
|
|
35
|
-
const response = await fetch(normalizeEndpoint(fallbackUrl, pathname), {
|
|
36
|
-
method: "POST",
|
|
37
|
-
headers: { "Content-Type": "application/json" },
|
|
38
|
-
body: JSON.stringify(body),
|
|
39
|
-
});
|
|
40
|
-
if (!response.ok) {
|
|
41
|
-
throw new Error(await response.text());
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
async function publish(options, pathname, body) {
|
|
45
|
-
const env = getEnv(options);
|
|
46
|
-
const durableObjectBinding = options.durableObjectBinding ?? "SYNC_ENGINE";
|
|
47
|
-
const bindingName = options.binding ?? "SYNC_WORKER";
|
|
48
|
-
if (options.platform && env?.[durableObjectBinding]) {
|
|
49
|
-
await publishToDurableObject(options.platform, durableObjectBinding, pathname, body);
|
|
31
|
+
async function publishToDevBroker(pathname, body) {
|
|
32
|
+
const devEngine = await import("./dev-engine.js");
|
|
33
|
+
if (pathname === "/broadcast-batch") {
|
|
34
|
+
await devEngine.broadcastExternalBatchChange(String(body.channel), Array.isArray(body.changes) ? body.changes : []);
|
|
50
35
|
return;
|
|
51
36
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
37
|
+
await devEngine.broadcastExternalChange(String(body.channel), body.action, body.key, body.data);
|
|
38
|
+
}
|
|
39
|
+
async function publish(pathname, body) {
|
|
40
|
+
const platform = getPublisherPlatform();
|
|
41
|
+
if (platform?.env.SYNC_ENGINE) {
|
|
42
|
+
await publishToDurableObject(platform, pathname, body);
|
|
55
43
|
return;
|
|
56
44
|
}
|
|
57
|
-
if (
|
|
58
|
-
await
|
|
45
|
+
if (hasDevBroker()) {
|
|
46
|
+
await publishToDevBroker(pathname, body);
|
|
59
47
|
return;
|
|
60
48
|
}
|
|
61
|
-
throw new Error(
|
|
49
|
+
throw new Error("Missing sync publisher target: use createSyncAppWorker() in production or syncDevPlugin() in Vite dev");
|
|
62
50
|
}
|
|
63
|
-
export function createPublisher(
|
|
64
|
-
void handlers;
|
|
51
|
+
export function createPublisher() {
|
|
65
52
|
return async (channel, action, key, data) => {
|
|
66
|
-
await publish(
|
|
53
|
+
await publish("/broadcast", {
|
|
67
54
|
channel: String(channel),
|
|
68
55
|
action,
|
|
69
56
|
key,
|
|
@@ -71,10 +58,9 @@ export function createPublisher(options, handlers) {
|
|
|
71
58
|
});
|
|
72
59
|
};
|
|
73
60
|
}
|
|
74
|
-
export function createBulkPublisher(
|
|
75
|
-
void handlers;
|
|
61
|
+
export function createBulkPublisher() {
|
|
76
62
|
return async (channel, changes) => {
|
|
77
|
-
await publish(
|
|
63
|
+
await publish("/broadcast-batch", {
|
|
78
64
|
channel: String(channel),
|
|
79
65
|
changes,
|
|
80
66
|
});
|
package/dist/server/index.d.ts
CHANGED
|
@@ -34,5 +34,5 @@ export interface SyncHandler<TRow = any, TAuth = any> {
|
|
|
34
34
|
}
|
|
35
35
|
export declare function defineSync<TRow = any, TAuth = any>(config: SyncHandlerConfig<TRow, TAuth>): SyncHandler<TRow, TAuth>;
|
|
36
36
|
export { createBulkPublisher, createPublisher, INTERNAL_AUTH_HEADER, } from "./handler.js";
|
|
37
|
-
export type { BulkPublishFn,
|
|
37
|
+
export type { BulkPublishFn, PublishEventData, PublishFn, SyncAuthResult, } from "./handler.js";
|
|
38
38
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAErC,MAAM,MAAM,kBAAkB,CAAC,KAAK,GAAG,OAAO,IAAI;IAChD,IAAI,EAAE,KAAK,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,YAAY,CACtB,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC5D;IACF,GAAG,EAAE,IAAI,CAAC;IACV,GAAG,CAAC,EAAE,gBAAgB,CAAC;IACvB,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,EAAE,CAAC,EAAE,2BAA2B,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,WAAW,CACrB,KAAK,GAAG,GAAG,EACX,IA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Q,CAAC,EAAE;QACT,MAAM,CAAC,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC;QACxB,MAAM,CAAC,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC;KACzB,CAAC;IACF,KAAK,CAAC,EAAE,CACN,GAAG,EAAE,WAAW,CAAC,KAAK,CAAC,EACvB,MAAM,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,EACtC,IAAI,EAAE,IAAI,KACP,OAAO,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,GAAG,MAAM,EAAE,GAAG,KAAK,CAAC;CACnD,CAAC;AAEF,MAAM,WAAW,WAAW,CAAC,IAAI,GAAG,GAAG,EAAE,KAAK,GAAG,GAAG;IAClD,MAAM,EAAE,iBAAiB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACvC,cAAc,CAAC,GAAG,EAAE,WAAW,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC;CACjD;AAED,wBAAgB,UAAU,CAAC,IAAI,GAAG,GAAG,EAAE,KAAK,GAAG,GAAG,EAChD,MAAM,EAAE,iBAAiB,CAAC,IAAI,EAAE,KAAK,CAAC,GACrC,WAAW,CAAC,IAAI,EAAE,KAAK,CAAC,CAS1B;AAED,OAAO,EACL,mBAAmB,EACnB,eAAe,EACf,oBAAoB,GACrB,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,aAAa,EACb,gBAAgB,EAChB,SAAS,EACT,cAAc,GACf,MAAM,cAAc,CAAC"}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import type { RequestHandler } from "@sveltejs/kit";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
};
|
|
6
|
-
export declare function syncProxy(options?: SyncProxyOptions): {
|
|
2
|
+
import { type SyncWorkerOptions } from "../cloudflare/handler.js";
|
|
3
|
+
export type SyncEngineRouteOptions<TAuth = unknown> = SyncWorkerOptions<TAuth>;
|
|
4
|
+
export declare function syncEngineRoute<TAuth = unknown>(options: SyncEngineRouteOptions<TAuth>): {
|
|
7
5
|
GET: RequestHandler;
|
|
8
|
-
POST: RequestHandler;
|
|
9
6
|
};
|
|
10
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sveltekit/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/sveltekit/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,OAAO,EAEL,KAAK,iBAAiB,EACvB,MAAM,0BAA0B,CAAC;AAElC,MAAM,MAAM,sBAAsB,CAAC,KAAK,GAAG,OAAO,IAChD,iBAAiB,CAAC,KAAK,CAAC,CAAC;AAkB3B,wBAAgB,eAAe,CAAC,KAAK,GAAG,OAAO,EAC7C,OAAO,EAAE,sBAAsB,CAAC,KAAK,CAAC,GACrC;IACD,GAAG,EAAE,cAAc,CAAC;CACrB,CAyBA"}
|
package/dist/sveltekit/index.js
CHANGED
|
@@ -1,29 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
1
|
+
import { configurePublisherPlatform } from "../server/handler.js";
|
|
2
|
+
import { handleSyncRequest, } from "../cloudflare/handler.js";
|
|
3
|
+
function getExecutionContext(platform) {
|
|
4
|
+
const context = platform?.context ?? platform?.ctx;
|
|
5
|
+
if (context)
|
|
6
|
+
return context;
|
|
7
|
+
return {
|
|
8
|
+
waitUntil() { },
|
|
9
|
+
passThroughOnException() { },
|
|
10
|
+
};
|
|
11
11
|
}
|
|
12
|
-
export function
|
|
13
|
-
const bindingName = options?.binding ?? "SYNC_WORKER";
|
|
12
|
+
export function syncEngineRoute(options) {
|
|
14
13
|
const handler = async (event) => {
|
|
15
14
|
const platform = event.platform;
|
|
16
|
-
const
|
|
17
|
-
if (
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
if (options?.fallbackUrl) {
|
|
21
|
-
return fetch(buildFallbackRequest(event.request, options.fallbackUrl));
|
|
15
|
+
const env = platform?.env;
|
|
16
|
+
if (!env) {
|
|
17
|
+
return new Response("Missing Cloudflare platform env. Configure adapter-cloudflare platformProxy for Vite dev or run under wrangler.", { status: 500 });
|
|
22
18
|
}
|
|
23
|
-
|
|
19
|
+
configurePublisherPlatform({ env });
|
|
20
|
+
return handleSyncRequest(event.request, env, getExecutionContext(platform), options);
|
|
24
21
|
};
|
|
25
22
|
return {
|
|
26
23
|
GET: handler,
|
|
27
|
-
POST: handler,
|
|
28
24
|
};
|
|
29
25
|
}
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
import type { SyncDevAuthOptions } from "./server/dev-engine.js";
|
|
3
|
+
export type SyncDevPluginOptions<TAuth = unknown> = SyncDevAuthOptions<TAuth> & {
|
|
4
|
+
handlersPath?: string;
|
|
5
|
+
path?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function syncDevPlugin<TAuth = unknown>(options?: SyncDevPluginOptions<TAuth>): Plugin;
|
|
8
|
+
//# sourceMappingURL=vite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../src/vite.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAajE,MAAM,MAAM,oBAAoB,CAAC,KAAK,GAAG,OAAO,IAAI,kBAAkB,CAAC,KAAK,CAAC,GAAG;IAC9E,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,wBAAgB,aAAa,CAAC,KAAK,GAAG,OAAO,EAC3C,OAAO,CAAC,EAAE,oBAAoB,CAAC,KAAK,CAAC,GACpC,MAAM,CA6ER"}
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const DEFAULT_SYNC_PATH = "/api/sync";
|
|
2
|
+
export function syncDevPlugin(options) {
|
|
3
|
+
const handlersPath = options?.handlersPath ?? "/src/lib/server/sync-handlers.ts";
|
|
4
|
+
const syncPath = options?.path ?? DEFAULT_SYNC_PATH;
|
|
5
|
+
return {
|
|
6
|
+
name: "sveltebase-sync-dev-websocket",
|
|
7
|
+
apply: "serve",
|
|
8
|
+
async configureServer(server) {
|
|
9
|
+
const { WebSocketServer } = (await import("ws"));
|
|
10
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
11
|
+
server.httpServer?.on("upgrade", (request, socket, head) => {
|
|
12
|
+
const url = new URL(request.url ?? "", `http://${request.headers.host ?? "localhost"}`);
|
|
13
|
+
if (url.pathname !== syncPath) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
wss.handleUpgrade(request, socket, head, (client) => {
|
|
17
|
+
const messageQueue = [];
|
|
18
|
+
const onMessage = (data) => {
|
|
19
|
+
messageQueue.push(data);
|
|
20
|
+
};
|
|
21
|
+
client.on("message", onMessage);
|
|
22
|
+
void (async () => {
|
|
23
|
+
try {
|
|
24
|
+
const handlersModule = await server.ssrLoadModule(handlersPath);
|
|
25
|
+
const devEngine = await server.ssrLoadModule("@sveltebase/sync/server/dev-engine");
|
|
26
|
+
devEngine.setHandlers(handlersModule.handlers);
|
|
27
|
+
client.off("message", onMessage);
|
|
28
|
+
const connected = await devEngine.addClient(client, request, {
|
|
29
|
+
auth: options?.auth,
|
|
30
|
+
identity: options?.identity,
|
|
31
|
+
allowUnauthenticated: options?.allowUnauthenticated,
|
|
32
|
+
platform: options?.platform,
|
|
33
|
+
wranglerConfigPath: options?.wranglerConfigPath,
|
|
34
|
+
});
|
|
35
|
+
if (!connected)
|
|
36
|
+
return;
|
|
37
|
+
for (const message of messageQueue) {
|
|
38
|
+
client.emit("message", message);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
console.error("sync dev plugin: websocket upgrade failed", err);
|
|
43
|
+
try {
|
|
44
|
+
client.off("message", onMessage);
|
|
45
|
+
client.close(1011, "Internal server error");
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Ignore close errors.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|