@livestore/sync-cf 0.4.0-dev.2 → 0.4.0-dev.20
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 +60 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/cf-worker/do/durable-object.d.ts +45 -0
- package/dist/cf-worker/do/durable-object.d.ts.map +1 -0
- package/dist/cf-worker/do/durable-object.js +151 -0
- package/dist/cf-worker/do/durable-object.js.map +1 -0
- package/dist/cf-worker/do/layer.d.ts +34 -0
- package/dist/cf-worker/do/layer.d.ts.map +1 -0
- package/dist/cf-worker/do/layer.js +91 -0
- package/dist/cf-worker/do/layer.js.map +1 -0
- package/dist/cf-worker/do/pull.d.ts +6 -0
- package/dist/cf-worker/do/pull.d.ts.map +1 -0
- package/dist/cf-worker/do/pull.js +47 -0
- package/dist/cf-worker/do/pull.js.map +1 -0
- package/dist/cf-worker/do/push.d.ts +14 -0
- package/dist/cf-worker/do/push.d.ts.map +1 -0
- package/dist/cf-worker/do/push.js +131 -0
- package/dist/cf-worker/do/push.js.map +1 -0
- package/dist/cf-worker/{durable-object.d.ts → do/sqlite.d.ts} +83 -67
- package/dist/cf-worker/do/sqlite.d.ts.map +1 -0
- package/dist/cf-worker/do/sqlite.js +36 -0
- package/dist/cf-worker/do/sqlite.js.map +1 -0
- package/dist/cf-worker/do/sync-storage.d.ts +25 -0
- package/dist/cf-worker/do/sync-storage.d.ts.map +1 -0
- package/dist/cf-worker/do/sync-storage.js +191 -0
- package/dist/cf-worker/do/sync-storage.js.map +1 -0
- package/dist/cf-worker/do/transport/do-rpc-server.d.ts +9 -0
- package/dist/cf-worker/do/transport/do-rpc-server.d.ts.map +1 -0
- package/dist/cf-worker/do/transport/do-rpc-server.js +45 -0
- package/dist/cf-worker/do/transport/do-rpc-server.js.map +1 -0
- package/dist/cf-worker/do/transport/http-rpc-server.d.ts +8 -0
- package/dist/cf-worker/do/transport/http-rpc-server.d.ts.map +1 -0
- package/dist/cf-worker/do/transport/http-rpc-server.js +30 -0
- package/dist/cf-worker/do/transport/http-rpc-server.js.map +1 -0
- package/dist/cf-worker/do/transport/ws-rpc-server.d.ts +4 -0
- package/dist/cf-worker/do/transport/ws-rpc-server.d.ts.map +1 -0
- package/dist/cf-worker/do/transport/ws-rpc-server.js +21 -0
- package/dist/cf-worker/do/transport/ws-rpc-server.js.map +1 -0
- package/dist/cf-worker/mod.d.ts +4 -2
- package/dist/cf-worker/mod.d.ts.map +1 -1
- package/dist/cf-worker/mod.js +3 -2
- package/dist/cf-worker/mod.js.map +1 -1
- package/dist/cf-worker/shared.d.ts +175 -0
- package/dist/cf-worker/shared.d.ts.map +1 -0
- package/dist/cf-worker/shared.js +43 -0
- package/dist/cf-worker/shared.js.map +1 -0
- package/dist/cf-worker/worker.d.ts +59 -51
- package/dist/cf-worker/worker.d.ts.map +1 -1
- package/dist/cf-worker/worker.js +75 -43
- package/dist/cf-worker/worker.js.map +1 -1
- package/dist/client/mod.d.ts +4 -0
- package/dist/client/mod.d.ts.map +1 -0
- package/dist/client/mod.js +4 -0
- package/dist/client/mod.js.map +1 -0
- package/dist/client/transport/do-rpc-client.d.ts +40 -0
- package/dist/client/transport/do-rpc-client.d.ts.map +1 -0
- package/dist/client/transport/do-rpc-client.js +115 -0
- package/dist/client/transport/do-rpc-client.js.map +1 -0
- package/dist/client/transport/http-rpc-client.d.ts +43 -0
- package/dist/client/transport/http-rpc-client.d.ts.map +1 -0
- package/dist/client/transport/http-rpc-client.js +103 -0
- package/dist/client/transport/http-rpc-client.js.map +1 -0
- package/dist/client/transport/ws-rpc-client.d.ts +46 -0
- package/dist/client/transport/ws-rpc-client.d.ts.map +1 -0
- package/dist/client/transport/ws-rpc-client.js +108 -0
- package/dist/client/transport/ws-rpc-client.js.map +1 -0
- package/dist/common/constants.d.ts +7 -0
- package/dist/common/constants.d.ts.map +1 -0
- package/dist/common/constants.js +17 -0
- package/dist/common/constants.js.map +1 -0
- package/dist/common/do-rpc-schema.d.ts +76 -0
- package/dist/common/do-rpc-schema.d.ts.map +1 -0
- package/dist/common/do-rpc-schema.js +48 -0
- package/dist/common/do-rpc-schema.js.map +1 -0
- package/dist/common/http-rpc-schema.d.ts +58 -0
- package/dist/common/http-rpc-schema.d.ts.map +1 -0
- package/dist/common/http-rpc-schema.js +37 -0
- package/dist/common/http-rpc-schema.js.map +1 -0
- package/dist/common/mod.d.ts +8 -1
- package/dist/common/mod.d.ts.map +1 -1
- package/dist/common/mod.js +7 -1
- package/dist/common/mod.js.map +1 -1
- package/dist/common/{ws-message-types.d.ts → sync-message-types.d.ts} +119 -153
- package/dist/common/sync-message-types.d.ts.map +1 -0
- package/dist/common/sync-message-types.js +60 -0
- package/dist/common/sync-message-types.js.map +1 -0
- package/dist/common/ws-rpc-schema.d.ts +55 -0
- package/dist/common/ws-rpc-schema.d.ts.map +1 -0
- package/dist/common/ws-rpc-schema.js +32 -0
- package/dist/common/ws-rpc-schema.js.map +1 -0
- package/package.json +7 -8
- package/src/cf-worker/do/durable-object.ts +238 -0
- package/src/cf-worker/do/layer.ts +128 -0
- package/src/cf-worker/do/pull.ts +75 -0
- package/src/cf-worker/do/push.ts +205 -0
- package/src/cf-worker/do/sqlite.ts +37 -0
- package/src/cf-worker/do/sync-storage.ts +323 -0
- package/src/cf-worker/do/transport/do-rpc-server.ts +84 -0
- package/src/cf-worker/do/transport/http-rpc-server.ts +51 -0
- package/src/cf-worker/do/transport/ws-rpc-server.ts +34 -0
- package/src/cf-worker/mod.ts +4 -2
- package/src/cf-worker/shared.ts +141 -0
- package/src/cf-worker/worker.ts +138 -116
- package/src/client/mod.ts +3 -0
- package/src/client/transport/do-rpc-client.ts +189 -0
- package/src/client/transport/http-rpc-client.ts +225 -0
- package/src/client/transport/ws-rpc-client.ts +202 -0
- package/src/common/constants.ts +18 -0
- package/src/common/do-rpc-schema.ts +54 -0
- package/src/common/http-rpc-schema.ts +40 -0
- package/src/common/mod.ts +10 -1
- package/src/common/sync-message-types.ts +117 -0
- package/src/common/ws-rpc-schema.ts +36 -0
- package/dist/cf-worker/cf-types.d.ts +0 -2
- package/dist/cf-worker/cf-types.d.ts.map +0 -1
- package/dist/cf-worker/cf-types.js +0 -2
- package/dist/cf-worker/cf-types.js.map +0 -1
- package/dist/cf-worker/durable-object.d.ts.map +0 -1
- package/dist/cf-worker/durable-object.js +0 -317
- package/dist/cf-worker/durable-object.js.map +0 -1
- package/dist/common/ws-message-types.d.ts.map +0 -1
- package/dist/common/ws-message-types.js +0 -57
- package/dist/common/ws-message-types.js.map +0 -1
- package/dist/sync-impl/mod.d.ts +0 -2
- package/dist/sync-impl/mod.d.ts.map +0 -1
- package/dist/sync-impl/mod.js +0 -2
- package/dist/sync-impl/mod.js.map +0 -1
- package/dist/sync-impl/ws-impl.d.ts +0 -7
- package/dist/sync-impl/ws-impl.d.ts.map +0 -1
- package/dist/sync-impl/ws-impl.js +0 -175
- package/dist/sync-impl/ws-impl.js.map +0 -1
- package/src/cf-worker/cf-types.ts +0 -12
- package/src/cf-worker/durable-object.ts +0 -478
- package/src/common/ws-message-types.ts +0 -114
- package/src/sync-impl/mod.ts +0 -1
- package/src/sync-impl/ws-impl.ts +0 -274
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Effect, HttpApp, Layer, RpcSerialization, RpcServer } from '@livestore/utils/effect';
|
|
2
|
+
import { SyncHttpRpc } from "../../../common/http-rpc-schema.js";
|
|
3
|
+
import * as SyncMessage from "../../../common/sync-message-types.js";
|
|
4
|
+
import { DoCtx } from "../layer.js";
|
|
5
|
+
import { makeEndingPullStream } from "../pull.js";
|
|
6
|
+
import { makePush } from "../push.js";
|
|
7
|
+
export const createHttpRpcHandler = ({ request, responseHeaders, }) => Effect.gen(function* () {
|
|
8
|
+
const handlerLayer = createHttpRpcLayer;
|
|
9
|
+
const httpApp = RpcServer.toHttpApp(SyncHttpRpc).pipe(Effect.provide(handlerLayer));
|
|
10
|
+
const webHandler = yield* httpApp.pipe(Effect.map(HttpApp.toWebHandler));
|
|
11
|
+
const response = yield* Effect.promise(() => webHandler(request)).pipe(Effect.timeout(10000));
|
|
12
|
+
if (responseHeaders !== undefined) {
|
|
13
|
+
for (const [key, value] of Object.entries(responseHeaders)) {
|
|
14
|
+
response.headers.set(key, value);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return response;
|
|
18
|
+
}).pipe(Effect.withSpan('createHttpRpcHandler'));
|
|
19
|
+
const createHttpRpcLayer =
|
|
20
|
+
// TODO implement admin requests
|
|
21
|
+
SyncHttpRpc.toLayer({
|
|
22
|
+
'SyncHttpRpc.Pull': (req) => makeEndingPullStream(req, req.payload),
|
|
23
|
+
'SyncHttpRpc.Push': (req) => Effect.gen(function* () {
|
|
24
|
+
const { ctx, env, doOptions, storeId } = yield* DoCtx;
|
|
25
|
+
const push = makePush({ payload: undefined, options: doOptions, storeId, ctx, env });
|
|
26
|
+
return yield* push(req);
|
|
27
|
+
}),
|
|
28
|
+
'SyncHttpRpc.Ping': () => Effect.succeed(SyncMessage.Pong.make({})),
|
|
29
|
+
}).pipe(Layer.provideMerge(RpcServer.layerProtocolHttp({ path: '/http-rpc' })), Layer.provideMerge(RpcSerialization.layerJson));
|
|
30
|
+
//# sourceMappingURL=http-rpc-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-rpc-server.js","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/http-rpc-server.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AAC7F,OAAO,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAA;AAChE,OAAO,KAAK,WAAW,MAAM,uCAAuC,CAAA;AACpE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AACnC,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AACjD,OAAO,EAAE,QAAQ,EAAE,MAA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ClB,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC,CAAC,KAAK,CAAA;QACrD,MAAM,IAAI,GAAG,QAAQ,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAA;QAEpF,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IACzB,CAAC,CAAC;IAEJ,kBAAkB,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;CACpE,CAAC,CAAC,IAAI,CACL,KAAK,CAAC,YAAY,CAAC,SAAS,CAAC,iBAAiB,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAC,EACtE,KAAK,CAAC,YAAY,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAC/C,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Layer, RpcServer } from '@livestore/utils/effect';
|
|
2
|
+
import { type DoCtxInput } from '../layer.ts';
|
|
3
|
+
export declare const makeRpcServer: ({ doSelf, doOptions }: Omit<DoCtxInput, "from">) => Layer.Layer<never, never, RpcServer.Protocol>;
|
|
4
|
+
//# sourceMappingURL=ws-rpc-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ws-rpc-server.d.ts","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/ws-rpc-server.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,EAAE,SAAS,EAAU,MAAM,yBAAyB,CAAA;AAEpF,OAAO,EAAS,KAAK,UAAU,EAAE,MAAM,aAAa,CAAA;AAIpD,eAAO,MAAM,aAAa,GAAI,uBAAuB,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,kDA0B5E,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { InvalidPullError, InvalidPushError } from '@livestore/common';
|
|
2
|
+
import { Effect, identity, Layer, RpcServer, Stream } from '@livestore/utils/effect';
|
|
3
|
+
import { SyncWsRpc } from "../../../common/ws-rpc-schema.js";
|
|
4
|
+
import { DoCtx } from "../layer.js";
|
|
5
|
+
import { makeEndingPullStream } from "../pull.js";
|
|
6
|
+
import { makePush } from "../push.js";
|
|
7
|
+
export const makeRpcServer = ({ doSelf, doOptions }) => {
|
|
8
|
+
// TODO implement admin requests
|
|
9
|
+
const handlersLayer = SyncWsRpc.toLayer({
|
|
10
|
+
'SyncWsRpc.Pull': (req) => makeEndingPullStream(req, req.payload).pipe(
|
|
11
|
+
// Needed to keep the stream alive on the client side for phase 2 (i.e. not send the `Exit` stream RPC message)
|
|
12
|
+
req.live ? Stream.concat(Stream.never) : identity, Stream.provideLayer(DoCtx.Default({ doSelf, doOptions, from: { storeId: req.storeId } })), Stream.mapError((cause) => (cause._tag === 'InvalidPullError' ? cause : InvalidPullError.make({ cause })))),
|
|
13
|
+
'SyncWsRpc.Push': (req) => Effect.gen(function* () {
|
|
14
|
+
const { doOptions, storeId, ctx, env } = yield* DoCtx;
|
|
15
|
+
const push = makePush({ options: doOptions, storeId, payload: req.payload, ctx, env });
|
|
16
|
+
return yield* push(req);
|
|
17
|
+
}).pipe(Effect.provide(DoCtx.Default({ doSelf, doOptions, from: { storeId: req.storeId } })), Effect.mapError((cause) => (cause._tag === 'InvalidPushError' ? cause : InvalidPushError.make({ cause }))), Effect.tapCauseLogPretty),
|
|
18
|
+
});
|
|
19
|
+
return RpcServer.layer(SyncWsRpc).pipe(Layer.provide(handlersLayer));
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=ws-rpc-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ws-rpc-server.js","sourceRoot":"","sources":["../../../../src/cf-worker/do/transport/ws-rpc-server.ts"],"names":[],"mappings":"AAAA,OAAO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AE,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC,KAAK,CAAA;YAErD,MAAM,IAAI,GAAG,QAAQ,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAA;YAEtF,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QACzB,CAAC,CAAC,CAAC,IAAI,CACL,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,EACpF,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,KAAK,kBAAkB,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAC1G,MAAM,CAAC,iBAAiB,CACzB;KACJ,CAAC,CAAA;IAEF,OAAO,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAA;AACtE,CAAC,CAAA"}
|
package/dist/cf-worker/mod.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export type { CfTypes } from '@livestore/common-cf';
|
|
2
|
+
export { CfDeclare } from '@livestore/common-cf/declare';
|
|
3
|
+
export * from './do/durable-object.ts';
|
|
4
|
+
export * from './shared.ts';
|
|
3
5
|
export * from './worker.ts';
|
|
4
6
|
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/cf-worker/mod.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/cf-worker/mod.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AACxD,cAAc,wBAAwB,CAAA;AACtC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
|
package/dist/cf-worker/mod.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../src/cf-worker/mod.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../../src/cf-worker/mod.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AACxD,cAAc,wBAAwB,CAAA;AACtC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import type { InvalidPullError, InvalidPushError } from '@livestore/common';
|
|
2
|
+
import type { CfTypes } from '@livestore/common-cf';
|
|
3
|
+
import { Effect, Schema } from '@livestore/utils/effect';
|
|
4
|
+
import type { SearchParams } from '../common/mod.ts';
|
|
5
|
+
import { SyncMessage } from '../common/mod.ts';
|
|
6
|
+
export type Env = {};
|
|
7
|
+
export type MakeDurableObjectClassOptions = {
|
|
8
|
+
onPush?: (message: SyncMessage.PushRequest, context: {
|
|
9
|
+
storeId: StoreId;
|
|
10
|
+
payload?: Schema.JsonValue;
|
|
11
|
+
}) => Effect.SyncOrPromiseOrEffect<void>;
|
|
12
|
+
onPushRes?: (message: SyncMessage.PushAck | InvalidPushError) => Effect.SyncOrPromiseOrEffect<void>;
|
|
13
|
+
onPull?: (message: SyncMessage.PullRequest, context: {
|
|
14
|
+
storeId: StoreId;
|
|
15
|
+
payload?: Schema.JsonValue;
|
|
16
|
+
}) => Effect.SyncOrPromiseOrEffect<void>;
|
|
17
|
+
onPullRes?: (message: SyncMessage.PullResponse | InvalidPullError) => Effect.SyncOrPromiseOrEffect<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Storage engine for event persistence.
|
|
20
|
+
* - Default: `{ _tag: 'do-sqlite' }` (Durable Object SQLite)
|
|
21
|
+
* - D1: `{ _tag: 'd1', binding: string }` where `binding` is the D1 binding name in wrangler.toml.
|
|
22
|
+
*
|
|
23
|
+
* If omitted, the runtime defaults to DO SQLite. For backwards-compatibility, if an env binding named
|
|
24
|
+
* `DB` exists and looks like a D1Database, D1 will be used.
|
|
25
|
+
*
|
|
26
|
+
* Trade-offs:
|
|
27
|
+
* - DO SQLite: simpler deploy, data co-located with DO, not externally queryable
|
|
28
|
+
* - D1: centralized DB, inspectable with DB tools, extra network hop and JSON size limits
|
|
29
|
+
*/
|
|
30
|
+
storage?: {
|
|
31
|
+
_tag: 'do-sqlite';
|
|
32
|
+
} | {
|
|
33
|
+
_tag: 'd1';
|
|
34
|
+
binding: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Enabled transports for sync backend
|
|
38
|
+
* - `http`: HTTP JSON-RPC
|
|
39
|
+
* - `ws`: WebSocket
|
|
40
|
+
* - `do-rpc`: Durable Object RPC calls (only works in combination with `@livestore/adapter-cf`)
|
|
41
|
+
*
|
|
42
|
+
* @default Set(['http', 'ws', 'do-rpc'])
|
|
43
|
+
*/
|
|
44
|
+
enabledTransports?: Set<'http' | 'ws' | 'do-rpc'>;
|
|
45
|
+
/**
|
|
46
|
+
* Custom HTTP response headers for HTTP transport
|
|
47
|
+
* These headers will be added to all HTTP RPC responses (Pull, Push, Ping)
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* {
|
|
52
|
+
* http: {
|
|
53
|
+
* responseHeaders: {
|
|
54
|
+
* 'Access-Control-Allow-Origin': '*',
|
|
55
|
+
* 'Cache-Control': 'no-cache'
|
|
56
|
+
* }
|
|
57
|
+
* }
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
http?: {
|
|
62
|
+
responseHeaders?: Record<string, string>;
|
|
63
|
+
};
|
|
64
|
+
otel?: {
|
|
65
|
+
baseUrl?: string;
|
|
66
|
+
serviceName?: string;
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
export type StoreId = string;
|
|
70
|
+
export type DurableObjectId = string;
|
|
71
|
+
/**
|
|
72
|
+
* CRITICAL: Increment this version whenever you modify the database schema structure.
|
|
73
|
+
*
|
|
74
|
+
* Bump required when:
|
|
75
|
+
* - Adding/removing/renaming columns in eventlogTable or contextTable (see sqlite.ts)
|
|
76
|
+
* - Changing column types or constraints
|
|
77
|
+
* - Modifying primary keys or indexes
|
|
78
|
+
*
|
|
79
|
+
* Bump NOT required when:
|
|
80
|
+
* - Changing query patterns, pagination logic, or streaming behavior
|
|
81
|
+
* - Adding new tables (as long as existing table schemas remain unchanged)
|
|
82
|
+
* - Updating implementation details in sync-storage.ts
|
|
83
|
+
*
|
|
84
|
+
* Impact: Changing this version triggers a "soft reset" - new table names are created
|
|
85
|
+
* and old data becomes inaccessible (but remains in storage).
|
|
86
|
+
*/
|
|
87
|
+
export declare const PERSISTENCE_FORMAT_VERSION = 7;
|
|
88
|
+
export declare const encodeOutgoingMessage: (a: {
|
|
89
|
+
readonly backendId: string;
|
|
90
|
+
readonly batch: readonly {
|
|
91
|
+
readonly metadata: import("effect/Option").Option<{
|
|
92
|
+
readonly createdAt: string;
|
|
93
|
+
readonly _tag: "SyncMessage.SyncMetadata";
|
|
94
|
+
}>;
|
|
95
|
+
readonly eventEncoded: {
|
|
96
|
+
readonly name: string;
|
|
97
|
+
readonly args: any;
|
|
98
|
+
readonly seqNum: any;
|
|
99
|
+
readonly parentSeqNum: any;
|
|
100
|
+
readonly clientId: string;
|
|
101
|
+
readonly sessionId: string;
|
|
102
|
+
};
|
|
103
|
+
}[];
|
|
104
|
+
readonly pageInfo: {
|
|
105
|
+
readonly _tag: "MoreUnknown";
|
|
106
|
+
} | {
|
|
107
|
+
readonly _tag: "MoreKnown";
|
|
108
|
+
readonly remaining: number;
|
|
109
|
+
} | {
|
|
110
|
+
readonly _tag: "NoMore";
|
|
111
|
+
};
|
|
112
|
+
} | {} | {
|
|
113
|
+
readonly _tag: "SyncMessage.Pong";
|
|
114
|
+
} | {
|
|
115
|
+
readonly _tag: "SyncMessage.AdminResetRoomResponse";
|
|
116
|
+
} | {
|
|
117
|
+
readonly info: {
|
|
118
|
+
readonly durableObjectId: string;
|
|
119
|
+
};
|
|
120
|
+
readonly _tag: "SyncMessage.AdminInfoResponse";
|
|
121
|
+
}, overrideOptions?: import("effect/SchemaAST").ParseOptions) => string;
|
|
122
|
+
export declare const encodeIncomingMessage: (a: {
|
|
123
|
+
readonly cursor: import("effect/Option").Option<{
|
|
124
|
+
readonly backendId: string;
|
|
125
|
+
readonly eventSequenceNumber: number & import("effect/Brand").Brand<"GlobalEventSequenceNumber">;
|
|
126
|
+
}>;
|
|
127
|
+
} | {
|
|
128
|
+
readonly backendId: import("effect/Option").Option<string>;
|
|
129
|
+
readonly batch: readonly {
|
|
130
|
+
readonly name: string;
|
|
131
|
+
readonly args: any;
|
|
132
|
+
readonly seqNum: any;
|
|
133
|
+
readonly parentSeqNum: any;
|
|
134
|
+
readonly clientId: string;
|
|
135
|
+
readonly sessionId: string;
|
|
136
|
+
}[];
|
|
137
|
+
} | {
|
|
138
|
+
readonly _tag: "SyncMessage.Ping";
|
|
139
|
+
} | {
|
|
140
|
+
readonly _tag: "SyncMessage.AdminResetRoomRequest";
|
|
141
|
+
readonly adminSecret: string;
|
|
142
|
+
} | {
|
|
143
|
+
readonly _tag: "SyncMessage.AdminInfoRequest";
|
|
144
|
+
readonly adminSecret: string;
|
|
145
|
+
}, overrideOptions?: import("effect/SchemaAST").ParseOptions) => string;
|
|
146
|
+
/**
|
|
147
|
+
* Extracts the LiveStore sync search parameters from a request. Returns
|
|
148
|
+
* `undefined` when the request does not carry valid sync metadata so callers
|
|
149
|
+
* can fall back to custom routing.
|
|
150
|
+
*/
|
|
151
|
+
export declare const matchSyncRequest: (request: CfTypes.Request) => SearchParams | undefined;
|
|
152
|
+
export type RpcSubscription = {
|
|
153
|
+
storeId: StoreId;
|
|
154
|
+
payload?: Schema.JsonValue;
|
|
155
|
+
subscribedAt: number;
|
|
156
|
+
/** Effect RPC request ID */
|
|
157
|
+
requestId: string;
|
|
158
|
+
callerContext: {
|
|
159
|
+
bindingName: string;
|
|
160
|
+
durableObjectId: string;
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Durable Object interface supporting the DO RPC protocol for DO <> DO syncing.
|
|
165
|
+
*/
|
|
166
|
+
export interface SyncBackendRpcInterface {
|
|
167
|
+
__DURABLE_OBJECT_BRAND: never;
|
|
168
|
+
rpc(payload: Uint8Array): Promise<Uint8Array | CfTypes.ReadableStream>;
|
|
169
|
+
}
|
|
170
|
+
export declare const WebSocketAttachmentSchema: Schema.transform<Schema.SchemaClass<unknown, string, never>, Schema.Struct<{
|
|
171
|
+
storeId: typeof Schema.String;
|
|
172
|
+
payload: Schema.optional<Schema.Schema<Schema.JsonValue, Schema.JsonValue, never>>;
|
|
173
|
+
pullRequestIds: Schema.Array$<typeof Schema.String>;
|
|
174
|
+
}>>;
|
|
175
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../src/cf-worker/shared.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAC3E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAa,MAAM,yBAAyB,CAAA;AAEnE,OAAO,KAAK,EAAE,Y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AAI,CAAC,EAAE;QACL,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,WAAW,CAAC,EAAE,MAAM,CAAA;KACrB,CAAA;CACF,CAAA;AAED,MAAM,MAAM,OAAO,GAAG,MAAM,CAAA;AAC5B,MAAM,MAAM,eAAe,GAAG,MAAM,CAAA;AAEpC;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,0BAA0B,IAAI,CAAA;AAE3C,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uEAA0E,CAAA;AAC5G,eAAO,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;uEAA0E,CAAA;AAE5G;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,SAAS,OAAO,CAAC,OAAO,KAAG,YAAY,GAAG,SAU1E,CAAA;AAGD,MAAM,MAAM,eAAe,GAAG;IAC5B,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,SAAS,CAAA;IAC1B,YAAY,EAAE,MAAM,CAAA;IACpB,4BAA4B;IAC5B,SAAS,EAAE,MAAM,CAAA;IACjB,aAAa,EAAE;QACb,WAAW,EAAE,MAAM,CAAA;QACnB,eAAe,EAAE,MAAM,CAAA;KACxB,CAAA;CACF,CAAA;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,sBAAsB,EAAE,KAAK,CAAA;IAC7B,GAAG,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;CACvE;AAED,eAAO,MAAM,yBAAyB;;;;GAQrC,CAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Effect, Schema, UrlParams } from '@livestore/utils/effect';
|
|
2
|
+
import { SearchParamsSchema, SyncMessage } from "../common/mod.js";
|
|
3
|
+
/**
|
|
4
|
+
* CRITICAL: Increment this version whenever you modify the database schema structure.
|
|
5
|
+
*
|
|
6
|
+
* Bump required when:
|
|
7
|
+
* - Adding/removing/renaming columns in eventlogTable or contextTable (see sqlite.ts)
|
|
8
|
+
* - Changing column types or constraints
|
|
9
|
+
* - Modifying primary keys or indexes
|
|
10
|
+
*
|
|
11
|
+
* Bump NOT required when:
|
|
12
|
+
* - Changing query patterns, pagination logic, or streaming behavior
|
|
13
|
+
* - Adding new tables (as long as existing table schemas remain unchanged)
|
|
14
|
+
* - Updating implementation details in sync-storage.ts
|
|
15
|
+
*
|
|
16
|
+
* Impact: Changing this version triggers a "soft reset" - new table names are created
|
|
17
|
+
* and old data becomes inaccessible (but remains in storage).
|
|
18
|
+
*/
|
|
19
|
+
export const PERSISTENCE_FORMAT_VERSION = 7;
|
|
20
|
+
export const encodeOutgoingMessage = Schema.encodeSync(Schema.parseJson(SyncMessage.BackendToClientMessage));
|
|
21
|
+
export const encodeIncomingMessage = Schema.encodeSync(Schema.parseJson(SyncMessage.ClientToBackendMessage));
|
|
22
|
+
/**
|
|
23
|
+
* Extracts the LiveStore sync search parameters from a request. Returns
|
|
24
|
+
* `undefined` when the request does not carry valid sync metadata so callers
|
|
25
|
+
* can fall back to custom routing.
|
|
26
|
+
*/
|
|
27
|
+
export const matchSyncRequest = (request) => {
|
|
28
|
+
const url = new URL(request.url);
|
|
29
|
+
const urlParams = UrlParams.fromInput(url.searchParams);
|
|
30
|
+
const paramsResult = UrlParams.schemaStruct(SearchParamsSchema)(urlParams).pipe(Effect.option, Effect.runSync);
|
|
31
|
+
if (paramsResult._tag === 'None') {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
return paramsResult.value;
|
|
35
|
+
};
|
|
36
|
+
export const WebSocketAttachmentSchema = Schema.parseJson(Schema.Struct({
|
|
37
|
+
// Same across all websocket connections
|
|
38
|
+
storeId: Schema.String,
|
|
39
|
+
// Different for each websocket connection
|
|
40
|
+
payload: Schema.optional(Schema.JsonValue),
|
|
41
|
+
pullRequestIds: Schema.Array(Schema.String),
|
|
42
|
+
}));
|
|
43
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C;IACxC,OAAO,EAAE,MAAM,CAAC,MAAM;IACtB,0CAA0C;IAC1C,OAAO,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC;IAC1C,cAAc,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;CAC5C,CAAC,CACH,CAAA"}
|
|
@@ -1,56 +1,49 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import type { HelperTypes } from '@livestore/common-cf';
|
|
2
|
+
import { Schema } from '@livestore/utils/effect';
|
|
3
|
+
import type { CfTypes, SearchParams } from '../common/mod.ts';
|
|
4
|
+
import { type Env } from './shared.ts';
|
|
5
|
+
export type CFWorker<TEnv extends Env = Env, _T extends CfTypes.Rpc.DurableObjectBranded | undefined = undefined> = {
|
|
6
|
+
fetch: <CFHostMetada = unknown>(request: CfTypes.Request<CFHostMetada>, env: TEnv, ctx: CfTypes.ExecutionContext) => Promise<CfTypes.Response>;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Options accepted by {@link makeWorker}. The Durable Object binding has to be
|
|
10
|
+
* supplied explicitly so we never fall back to deprecated defaults when Cloudflare config changes.
|
|
11
|
+
*/
|
|
12
|
+
export type MakeWorkerOptions<TEnv extends Env = Env, TSyncPayload = Schema.JsonValue> = {
|
|
10
13
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* type PlatformEnv = {
|
|
16
|
-
* DB: D1Database
|
|
17
|
-
* ADMIN_TOKEN: string
|
|
18
|
-
* WEBSOCKET_SERVER: DurableObjectNamespace<WebSocketServer>
|
|
19
|
-
* }
|
|
20
|
-
* export default makeWorker<PlatformEnv>({
|
|
21
|
-
* durableObject: { name: "WEBSOCKET_SERVER" },
|
|
22
|
-
* // ^ (property) name?: "WEBSOCKET_SERVER" | undefined
|
|
23
|
-
* });
|
|
14
|
+
* Binding name of the sync Durable Object declared in wrangler config.
|
|
24
15
|
*/
|
|
25
|
-
|
|
26
|
-
export {};
|
|
27
|
-
}
|
|
28
|
-
export type CFWorker<TEnv extends Env = Env, _T extends CfWorker.Rpc.DurableObjectBranded | undefined = undefined> = {
|
|
29
|
-
fetch: <CFHostMetada = unknown>(request: CfWorker.Request<CFHostMetada>, env: TEnv, ctx: CfWorker.ExecutionContext) => Promise<CfWorker.Response>;
|
|
30
|
-
};
|
|
31
|
-
export type MakeWorkerOptions<TEnv extends Env = Env> = {
|
|
16
|
+
syncBackendBinding: HelperTypes.ExtractDurableObjectKeys<TEnv>;
|
|
32
17
|
/**
|
|
33
18
|
* Validates the payload during WebSocket connection establishment.
|
|
34
19
|
* Note: This runs only at connection time, not for individual push events.
|
|
35
20
|
* For push event validation, use the `onPush` callback in the durable object.
|
|
36
21
|
*/
|
|
37
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Optionally pass a schema to decode the client-provided payload into a typed object
|
|
24
|
+
* before calling {@link validatePayload}. If omitted, the raw JSON value is forwarded.
|
|
25
|
+
*/
|
|
26
|
+
syncPayloadSchema?: Schema.Schema<TSyncPayload>;
|
|
27
|
+
/**
|
|
28
|
+
* Validates the (optionally decoded) payload during WebSocket connection establishment.
|
|
29
|
+
* If {@link syncPayloadSchema} is provided, `payload` will be of the schema’s inferred type.
|
|
30
|
+
*/
|
|
31
|
+
validatePayload?: (payload: TSyncPayload, context: {
|
|
38
32
|
storeId: string;
|
|
39
33
|
}) => void | Promise<void>;
|
|
40
34
|
/** @default false */
|
|
41
35
|
enableCORS?: boolean;
|
|
42
|
-
durableObject?: {
|
|
43
|
-
/**
|
|
44
|
-
* Needs to match the binding name from the wrangler config
|
|
45
|
-
*
|
|
46
|
-
* @default 'WEBSOCKET_SERVER'
|
|
47
|
-
*/
|
|
48
|
-
name?: HelperTypes.ExtractDurableObjectKeys<TEnv>;
|
|
49
|
-
};
|
|
50
36
|
};
|
|
51
|
-
export declare const makeWorker: <TEnv extends Env = Env, TDurableObjectRpc extends CfWorker.Rpc.DurableObjectBranded | undefined = undefined>(options?: MakeWorkerOptions<TEnv>) => CFWorker<TEnv, TDurableObjectRpc>;
|
|
52
37
|
/**
|
|
53
|
-
*
|
|
38
|
+
* Produces a Cloudflare Worker `fetch` handler that delegates sync traffic to the
|
|
39
|
+
* Durable Object identified by `syncBackendBinding`.
|
|
40
|
+
*
|
|
41
|
+
* For more complex setups prefer implementing a custom `fetch` and call {@link handleSyncRequest}
|
|
42
|
+
* from the branch that handles LiveStore sync requests.
|
|
43
|
+
*/
|
|
44
|
+
export declare const makeWorker: <TEnv extends Env = Env, TDurableObjectRpc extends CfTypes.Rpc.DurableObjectBranded | undefined = undefined, TSyncPayload = Schema.JsonValue>(options: MakeWorkerOptions<TEnv, TSyncPayload>) => CFWorker<TEnv, TDurableObjectRpc>;
|
|
45
|
+
/**
|
|
46
|
+
* Handles LiveStore sync requests (e.g. with search params `?storeId=...&transport=...`).
|
|
54
47
|
*
|
|
55
48
|
* @example
|
|
56
49
|
* ```ts
|
|
@@ -63,23 +56,38 @@ export declare const makeWorker: <TEnv extends Env = Env, TDurableObjectRpc exte
|
|
|
63
56
|
*
|
|
64
57
|
* export default {
|
|
65
58
|
* fetch: async (request, env, ctx) => {
|
|
66
|
-
*
|
|
67
|
-
*
|
|
59
|
+
* const searchParams = matchSyncRequest(request)
|
|
60
|
+
*
|
|
61
|
+
* // Is LiveStore sync request
|
|
62
|
+
* if (searchParams !== undefined) {
|
|
63
|
+
* return handleSyncRequest({
|
|
64
|
+
* request,
|
|
65
|
+
* searchParams,
|
|
66
|
+
* env,
|
|
67
|
+
* ctx,
|
|
68
|
+
* syncBackendBinding: 'SYNC_BACKEND_DO',
|
|
69
|
+
* headers: {},
|
|
70
|
+
* validatePayload,
|
|
71
|
+
* })
|
|
68
72
|
* }
|
|
69
73
|
*
|
|
70
74
|
* return new Response('Invalid path', { status: 400 })
|
|
71
|
-
* return new Response('Invalid path', { status: 400 })
|
|
72
75
|
* }
|
|
73
76
|
* }
|
|
74
77
|
* ```
|
|
75
78
|
*
|
|
76
|
-
* @throws {
|
|
79
|
+
* @throws {UnknownError} If the payload is invalid
|
|
77
80
|
*/
|
|
78
|
-
export declare const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
export declare const handleSyncRequest: <TEnv extends Env = Env, TDurableObjectRpc extends CfTypes.Rpc.DurableObjectBranded | undefined = undefined, CFHostMetada = unknown, TSyncPayload = Schema.JsonValue>({ request, searchParams: { storeId, payload, transport }, env: explicitlyProvidedEnv, syncBackendBinding, headers, validatePayload, syncPayloadSchema, }: {
|
|
82
|
+
request: CfTypes.Request<CFHostMetada>;
|
|
83
|
+
searchParams: SearchParams;
|
|
84
|
+
env?: TEnv | undefined;
|
|
85
|
+
/** Only there for type-level reasons */
|
|
86
|
+
ctx: CfTypes.ExecutionContext;
|
|
87
|
+
/** Binding name of the sync backend Durable Object */
|
|
88
|
+
syncBackendBinding: MakeWorkerOptions<TEnv, TSyncPayload>["syncBackendBinding"];
|
|
89
|
+
headers?: CfTypes.HeadersInit | undefined;
|
|
90
|
+
validatePayload?: MakeWorkerOptions<TEnv, TSyncPayload>["validatePayload"];
|
|
91
|
+
syncPayloadSchema?: MakeWorkerOptions<TEnv, TSyncPayload>["syncPayloadSchema"];
|
|
92
|
+
}) => Promise<CfTypes.Response>;
|
|
85
93
|
//# sourceMappingURL=worker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/cf-worker/worker.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../../src/cf-worker/worker.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CA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