@livestore/adapter-cloudflare 0.4.0-dev.8 → 0.4.0-dev.9
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/dist/.tsbuildinfo +1 -1
- package/dist/create-store-do.d.ts +44 -0
- package/dist/create-store-do.d.ts.map +1 -0
- package/dist/create-store-do.js +28 -0
- package/dist/create-store-do.js.map +1 -0
- package/dist/mod.d.ts +1 -1
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +1 -1
- package/dist/mod.js.map +1 -1
- package/package.json +9 -9
- package/src/create-store-do.ts +98 -0
- package/src/mod.ts +2 -4
- package/src/make-client-durable-object.ts +0 -107
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { HelperTypes } from '@livestore/common-cf';
|
|
2
|
+
import { type LiveStoreSchema } from '@livestore/livestore';
|
|
3
|
+
import type * as CfSyncBackend from '@livestore/sync-cf/cf-worker';
|
|
4
|
+
import { Effect } from '@livestore/utils/effect';
|
|
5
|
+
import type * as CfWorker from './cf-types.ts';
|
|
6
|
+
export type Env = {
|
|
7
|
+
SYNC_BACKEND_DO: CfWorker.DurableObjectNamespace<CfSyncBackend.SyncBackendRpcInterface>;
|
|
8
|
+
};
|
|
9
|
+
export type CreateStoreDoOptions<TSchema extends LiveStoreSchema, TEnv, TState> = {
|
|
10
|
+
/** LiveStore schema that defines state, migrations, and validators. */
|
|
11
|
+
schema: TSchema;
|
|
12
|
+
/** Logical identifier for the store instance persisted inside the Durable Object. */
|
|
13
|
+
storeId: string;
|
|
14
|
+
/** Unique identifier for the client that owns the Durable Object instance. */
|
|
15
|
+
clientId: string;
|
|
16
|
+
/** Identifier for the LiveStore session running inside the Durable Object. */
|
|
17
|
+
sessionId: string;
|
|
18
|
+
/** Runtime details about the Durable Object this store runs inside. Needed for sync backend to call back to this instance. */
|
|
19
|
+
durableObject: {
|
|
20
|
+
/** Durable Object state handle (e.g. `this.ctx`). */
|
|
21
|
+
ctx: TState;
|
|
22
|
+
/** Environment bindings associated with the Durable Object. */
|
|
23
|
+
env: TEnv;
|
|
24
|
+
/** Binding name Cloudflare uses to reach this Durable Object from other workers. */
|
|
25
|
+
bindingName: HelperTypes.ExtractDurableObjectKeys<NoInfer<TEnv>>;
|
|
26
|
+
};
|
|
27
|
+
/** RPC stub pointing at the sync backend Durable Object used for replication. */
|
|
28
|
+
syncBackendStub: CfWorker.DurableObjectStub<CfSyncBackend.SyncBackendRpcInterface>;
|
|
29
|
+
/**
|
|
30
|
+
* Enables live pull mode to receive sync updates via Durable Object RPC callbacks.
|
|
31
|
+
*
|
|
32
|
+
* @default false
|
|
33
|
+
*/
|
|
34
|
+
livePull?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Clears existing Durable Object persistence before bootstrapping the store.
|
|
37
|
+
*
|
|
38
|
+
* Note: Only use this for development purposes.
|
|
39
|
+
*/
|
|
40
|
+
resetPersistence?: boolean;
|
|
41
|
+
};
|
|
42
|
+
export declare const createStoreDo: <TSchema extends LiveStoreSchema, TEnv, TState extends CfWorker.DurableObjectState = CfWorker.DurableObjectState>({ schema, storeId, clientId, sessionId, durableObject, syncBackendStub, livePull, resetPersistence, }: CreateStoreDoOptions<TSchema, TEnv, TState>) => Effect.Effect<import("@livestore/livestore").Store<TSchema, {}>, import("@livestore/common").UnexpectedError, never>;
|
|
43
|
+
export declare const createStoreDoPromise: <TSchema extends LiveStoreSchema, TEnv, TState extends CfWorker.DurableObjectState = CfWorker.DurableObjectState>(options: CreateStoreDoOptions<TSchema, TEnv, TState>) => Promise<import("@livestore/livestore").Store<TSchema, {}>>;
|
|
44
|
+
//# sourceMappingURL=create-store-do.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-store-do.d.ts","sourceRoot":"","sources":["../src/create-store-do.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAe,KAAK,eAAe,EAAe,MAAM,sBAAsB,CAAA;AACrF,OAAO,KAAK,KAAK,aAAa,MAAM,8BAA8B,CAAA;AAElE,OAAO,EAAE,MAAM,EAA2B,MAAM,yBAAyB,CAAA;AACzE,OAAO,KAAK,KAAK,QAAQ,MAAM,eAAe,CAAA;AAG9C,MAAM,MAAM,GAAG,GAAG;IAChB,eAAe,EAAE,QAAQ,CAAC,sBAAsB,CAAC,aAAa,CAAC,uBAAuB,CAAC,CAAA;CACxF,CAAA;AAED,MAAM,MAAM,oBAAoB,CAAC,OAAO,SAAS,eAAe,EAAE,IAAI,EAAE,MAAM,IAAI;IAChF,uEAAuE;IACvE,MAAM,EAAE,OAAO,CAAA;IACf,qFAAqF;IACrF,OAAO,EAAE,MAAM,CAAA;IACf,8EAA8E;IAC9E,QAAQ,EAAE,MAAM,CAAA;IAChB,8EAA8E;IAC9E,SAAS,EAAE,MAAM,CAAA;IACjB,8HAA8H;IAC9H,aAAa,EAAE;QACb,qDAAqD;QACrD,GAAG,EAAE,MAAM,CAAA;QACX,+DAA+D;QAC/D,GAAG,EAAE,IAAI,CAAA;QACT,oFAAoF;QACpF,WAAW,EAAE,WAAW,CAAC,wBAAwB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAA;KACjE,CAAA;IACD,iFAAiF;IACjF,eAAe,EAAE,QAAQ,CAAC,iBAAiB,CAAC,aAAa,CAAC,uBAAuB,CAAC,CAAA;IAClF;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAC3B,CAAA;AAGD,eAAO,MAAM,aAAa,GACxB,OAAO,SAAS,eAAe,EAC/B,IAAI,EACJ,MAAM,SAAS,QAAQ,CAAC,kBAAkB,GAAG,QAAQ,CAAC,kBAAkB,EACxE,uGASC,oBAAoB,CAAC,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,yHAuB1C,CAAA;AAEJ,eAAO,MAAM,oBAAoB,GAC/B,OAAO,SAAS,eAAe,EAC/B,IAAI,EACJ,MAAM,SAAS,QAAQ,CAAC,kBAAkB,GAAG,QAAQ,CAAC,kBAAkB,EAExE,SAAS,oBAAoB,CAAC,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,+DAOnD,CAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createStore, provideOtel } from '@livestore/livestore';
|
|
2
|
+
import { makeDoRpcSync } from '@livestore/sync-cf/client';
|
|
3
|
+
import { Effect, Logger, LogLevel, Scope } from '@livestore/utils/effect';
|
|
4
|
+
import { makeAdapter } from "./make-adapter.js";
|
|
5
|
+
// TODO Also support in Cloudflare workers outside of a durable object context.
|
|
6
|
+
export const createStoreDo = ({ schema, storeId, clientId, sessionId, durableObject, syncBackendStub, livePull = false, resetPersistence = false, }) => Effect.gen(function* () {
|
|
7
|
+
const { ctx, bindingName } = durableObject;
|
|
8
|
+
const storage = ctx.storage;
|
|
9
|
+
const durableObjectId = ctx.id.toString();
|
|
10
|
+
const scope = yield* Scope.make();
|
|
11
|
+
const adapter = makeAdapter({
|
|
12
|
+
clientId,
|
|
13
|
+
sessionId,
|
|
14
|
+
storage,
|
|
15
|
+
resetPersistence,
|
|
16
|
+
syncOptions: {
|
|
17
|
+
backend: makeDoRpcSync({
|
|
18
|
+
syncBackendStub,
|
|
19
|
+
durableObjectContext: { bindingName, durableObjectId },
|
|
20
|
+
}),
|
|
21
|
+
livePull, // Uses DO RPC callbacks for reactive pull
|
|
22
|
+
initialSyncOptions: { _tag: 'Blocking', timeout: 500 },
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
return yield* createStore({ schema, adapter, storeId }).pipe(Scope.extend(scope), provideOtel({}));
|
|
26
|
+
});
|
|
27
|
+
export const createStoreDoPromise = (options) => createStoreDo(options).pipe(Logger.withMinimumLogLevel(LogLevel.Debug), Effect.provide(Logger.consoleWithThread('DoClient')), Effect.tapCauseLogPretty, Effect.runPromise);
|
|
28
|
+
//# sourceMappingURL=create-store-do.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-store-do.js","sourceRoot":"","sources":["../src/create-store-do.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAwB,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAErF,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AAEzE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAwC/C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,aAAa,GAAG,CAI3B,EACA,MAAM,EACN,OAAO,EACP,QAAQ,EACR,SAAS,EACT,aAAa,EACb,eAAe,EACf,QAAQ,GAAG,KAAK,EAChB,gBAAgB,GAAG,KAAK,GACoB,EAAE,EAAE,CAChD,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IAClB,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,aAAa,CAAA;IAC1C,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAA;IAC3B,MAAM,eAAe,GAAG,GAAG,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAA;IACzC,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;IAEjC,MAAM,OAAO,GAAG,WAAW,CAAC;QAC1B,QAAQ;QACR,SAAS;QACT,OAAO;QACP,gBAAgB;QAChB,WAAW,EAAE;YACX,OAAO,EAAE,aAAa,CAAC;gBACrB,eAAe;gBACf,oBAAoB,EAAE,EAAE,WAAW,EAAE,eAAe,EAAE;aACvD,CAAC;YACF,QAAQ,EAAE,0CAA0C;YACpD,kBAAkB,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,EAAE;SACvD;KACF,CAAC,CAAA;IAEF,OAAO,KAAK,CAAC,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,WAAW,CAAC,EAAE,CAAC,CAAC,CAAA;AACpG,CAAC,CAAC,CAAA;AAEJ,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAKlC,OAAoD,EACpD,EAAE,CACF,aAAa,CAAC,OAAO,CAAC,CAAC,IAAI,CACzB,MAAM,CAAC,mBAAmB,CAAC,QAAQ,CAAC,KAAK,CAAC,EAC1C,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,UAAU,CAAC,CAAC,EACpD,MAAM,CAAC,iBAAiB,EACxB,MAAM,CAAC,UAAU,CAClB,CAAA"}
|
package/dist/mod.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import './polyfill.ts';
|
|
2
2
|
export type { ClientDoWithRpcCallback } from '@livestore/common-cf';
|
|
3
|
+
export { type CreateStoreDoOptions, createStoreDo, createStoreDoPromise, type Env, } from './create-store-do.ts';
|
|
3
4
|
export { makeAdapter } from './make-adapter.ts';
|
|
4
|
-
export { type CreateStoreDoOptions, createStoreDo, createStoreDoPromise, type Env, type MakeDurableObjectClass, type MakeDurableObjectClassOptions, } from './make-client-durable-object.ts';
|
|
5
5
|
//# sourceMappingURL=mod.d.ts.map
|
package/dist/mod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAA;AAEtB,YAAY,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AACnE,OAAO,
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAA;AAEtB,YAAY,EAAE,uBAAuB,EAAE,MAAM,sBAAsB,CAAA;AACnE,OAAO,EACL,KAAK,oBAAoB,EACzB,aAAa,EACb,oBAAoB,EACpB,KAAK,GAAG,GACT,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA"}
|
package/dist/mod.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import "./polyfill.js";
|
|
2
|
+
export { createStoreDo, createStoreDoPromise, } from "./create-store-do.js";
|
|
2
3
|
export { makeAdapter } from "./make-adapter.js";
|
|
3
|
-
export { createStoreDo, createStoreDoPromise, } from "./make-client-durable-object.js";
|
|
4
4
|
//# sourceMappingURL=mod.js.map
|
package/dist/mod.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAA;AAGtB,OAAO,
|
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,CAAA;AAGtB,OAAO,EAEL,aAAa,EACb,oBAAoB,GAErB,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livestore/adapter-cloudflare",
|
|
3
|
-
"version": "0.4.0-dev.
|
|
3
|
+
"version": "0.4.0-dev.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"./src/polyfill.ts",
|
|
@@ -11,16 +11,16 @@
|
|
|
11
11
|
"./polyfill": "./dist/polyfill.js"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@cloudflare/workers-types": "4.
|
|
15
|
-
"@livestore/common": "0.4.0-dev.
|
|
16
|
-
"@livestore/
|
|
17
|
-
"@livestore/
|
|
18
|
-
"@livestore/
|
|
19
|
-
"@livestore/
|
|
20
|
-
"@livestore/
|
|
14
|
+
"@cloudflare/workers-types": "4.20250923.0",
|
|
15
|
+
"@livestore/common": "0.4.0-dev.9",
|
|
16
|
+
"@livestore/livestore": "0.4.0-dev.9",
|
|
17
|
+
"@livestore/sqlite-wasm": "0.4.0-dev.9",
|
|
18
|
+
"@livestore/sync-cf": "0.4.0-dev.9",
|
|
19
|
+
"@livestore/utils": "0.4.0-dev.9",
|
|
20
|
+
"@livestore/common-cf": "0.4.0-dev.9"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"wrangler": "4.
|
|
23
|
+
"wrangler": "4.38.0"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
26
|
"package.json",
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { HelperTypes } from '@livestore/common-cf'
|
|
2
|
+
import { createStore, type LiveStoreSchema, provideOtel } from '@livestore/livestore'
|
|
3
|
+
import type * as CfSyncBackend from '@livestore/sync-cf/cf-worker'
|
|
4
|
+
import { makeDoRpcSync } from '@livestore/sync-cf/client'
|
|
5
|
+
import { Effect, Logger, LogLevel, Scope } from '@livestore/utils/effect'
|
|
6
|
+
import type * as CfWorker from './cf-types.ts'
|
|
7
|
+
import { makeAdapter } from './make-adapter.ts'
|
|
8
|
+
|
|
9
|
+
export type Env = {
|
|
10
|
+
SYNC_BACKEND_DO: CfWorker.DurableObjectNamespace<CfSyncBackend.SyncBackendRpcInterface>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type CreateStoreDoOptions<TSchema extends LiveStoreSchema, TEnv, TState> = {
|
|
14
|
+
/** LiveStore schema that defines state, migrations, and validators. */
|
|
15
|
+
schema: TSchema
|
|
16
|
+
/** Logical identifier for the store instance persisted inside the Durable Object. */
|
|
17
|
+
storeId: string
|
|
18
|
+
/** Unique identifier for the client that owns the Durable Object instance. */
|
|
19
|
+
clientId: string
|
|
20
|
+
/** Identifier for the LiveStore session running inside the Durable Object. */
|
|
21
|
+
sessionId: string
|
|
22
|
+
/** Runtime details about the Durable Object this store runs inside. Needed for sync backend to call back to this instance. */
|
|
23
|
+
durableObject: {
|
|
24
|
+
/** Durable Object state handle (e.g. `this.ctx`). */
|
|
25
|
+
ctx: TState
|
|
26
|
+
/** Environment bindings associated with the Durable Object. */
|
|
27
|
+
env: TEnv
|
|
28
|
+
/** Binding name Cloudflare uses to reach this Durable Object from other workers. */
|
|
29
|
+
bindingName: HelperTypes.ExtractDurableObjectKeys<NoInfer<TEnv>>
|
|
30
|
+
}
|
|
31
|
+
/** RPC stub pointing at the sync backend Durable Object used for replication. */
|
|
32
|
+
syncBackendStub: CfWorker.DurableObjectStub<CfSyncBackend.SyncBackendRpcInterface>
|
|
33
|
+
/**
|
|
34
|
+
* Enables live pull mode to receive sync updates via Durable Object RPC callbacks.
|
|
35
|
+
*
|
|
36
|
+
* @default false
|
|
37
|
+
*/
|
|
38
|
+
livePull?: boolean
|
|
39
|
+
/**
|
|
40
|
+
* Clears existing Durable Object persistence before bootstrapping the store.
|
|
41
|
+
*
|
|
42
|
+
* Note: Only use this for development purposes.
|
|
43
|
+
*/
|
|
44
|
+
resetPersistence?: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// TODO Also support in Cloudflare workers outside of a durable object context.
|
|
48
|
+
export const createStoreDo = <
|
|
49
|
+
TSchema extends LiveStoreSchema,
|
|
50
|
+
TEnv,
|
|
51
|
+
TState extends CfWorker.DurableObjectState = CfWorker.DurableObjectState,
|
|
52
|
+
>({
|
|
53
|
+
schema,
|
|
54
|
+
storeId,
|
|
55
|
+
clientId,
|
|
56
|
+
sessionId,
|
|
57
|
+
durableObject,
|
|
58
|
+
syncBackendStub,
|
|
59
|
+
livePull = false,
|
|
60
|
+
resetPersistence = false,
|
|
61
|
+
}: CreateStoreDoOptions<TSchema, TEnv, TState>) =>
|
|
62
|
+
Effect.gen(function* () {
|
|
63
|
+
const { ctx, bindingName } = durableObject
|
|
64
|
+
const storage = ctx.storage
|
|
65
|
+
const durableObjectId = ctx.id.toString()
|
|
66
|
+
const scope = yield* Scope.make()
|
|
67
|
+
|
|
68
|
+
const adapter = makeAdapter({
|
|
69
|
+
clientId,
|
|
70
|
+
sessionId,
|
|
71
|
+
storage,
|
|
72
|
+
resetPersistence,
|
|
73
|
+
syncOptions: {
|
|
74
|
+
backend: makeDoRpcSync({
|
|
75
|
+
syncBackendStub,
|
|
76
|
+
durableObjectContext: { bindingName, durableObjectId },
|
|
77
|
+
}),
|
|
78
|
+
livePull, // Uses DO RPC callbacks for reactive pull
|
|
79
|
+
initialSyncOptions: { _tag: 'Blocking', timeout: 500 },
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
return yield* createStore({ schema, adapter, storeId }).pipe(Scope.extend(scope), provideOtel({}))
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
export const createStoreDoPromise = <
|
|
87
|
+
TSchema extends LiveStoreSchema,
|
|
88
|
+
TEnv,
|
|
89
|
+
TState extends CfWorker.DurableObjectState = CfWorker.DurableObjectState,
|
|
90
|
+
>(
|
|
91
|
+
options: CreateStoreDoOptions<TSchema, TEnv, TState>,
|
|
92
|
+
) =>
|
|
93
|
+
createStoreDo(options).pipe(
|
|
94
|
+
Logger.withMinimumLogLevel(LogLevel.Debug),
|
|
95
|
+
Effect.provide(Logger.consoleWithThread('DoClient')),
|
|
96
|
+
Effect.tapCauseLogPretty,
|
|
97
|
+
Effect.runPromise,
|
|
98
|
+
)
|
package/src/mod.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import './polyfill.ts'
|
|
2
2
|
|
|
3
3
|
export type { ClientDoWithRpcCallback } from '@livestore/common-cf'
|
|
4
|
-
export { makeAdapter } from './make-adapter.ts'
|
|
5
4
|
export {
|
|
6
5
|
type CreateStoreDoOptions,
|
|
7
6
|
createStoreDo,
|
|
8
7
|
createStoreDoPromise,
|
|
9
8
|
type Env,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} from './make-client-durable-object.ts'
|
|
9
|
+
} from './create-store-do.ts'
|
|
10
|
+
export { makeAdapter } from './make-adapter.ts'
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import type { UnexpectedError } from '@livestore/common'
|
|
2
|
-
import { createStore, type LiveStoreSchema, provideOtel, type Store, type Unsubscribe } from '@livestore/livestore'
|
|
3
|
-
import type * as CfSyncBackend from '@livestore/sync-cf/cf-worker'
|
|
4
|
-
import { makeDoRpcSync } from '@livestore/sync-cf/client'
|
|
5
|
-
import { Effect, Logger, LogLevel, Scope } from '@livestore/utils/effect'
|
|
6
|
-
import type * as CfWorker from './cf-types.ts'
|
|
7
|
-
import { makeAdapter } from './make-adapter.ts'
|
|
8
|
-
|
|
9
|
-
declare class Response extends CfWorker.Response {}
|
|
10
|
-
|
|
11
|
-
export type MakeDurableObjectClassOptions<TSchema extends LiveStoreSchema = LiveStoreSchema.Any> = {
|
|
12
|
-
schema: TSchema
|
|
13
|
-
// storeId: string
|
|
14
|
-
clientId: string
|
|
15
|
-
sessionId: string
|
|
16
|
-
onStoreReady?: (store: Store<TSchema>) => Effect.SyncOrPromiseOrEffect<void, UnexpectedError>
|
|
17
|
-
// makeStore?: (adapter: Adapter) => Promise<Store<TSchema>>
|
|
18
|
-
// onLiveStoreEvent?: (event: LiveStoreEvent.ForSchema<TSchema>) => Promise<void>
|
|
19
|
-
registerQueries?: (store: Store<TSchema>) => Effect.SyncOrPromiseOrEffect<ReadonlyArray<Unsubscribe>>
|
|
20
|
-
syncBackendUrl?: string
|
|
21
|
-
// Hook for custom request handling (e.g., testing endpoints)
|
|
22
|
-
handleCustomRequest?: (
|
|
23
|
-
request: CfWorker.Request,
|
|
24
|
-
ensureStore: Effect.Effect<Store<TSchema>, UnexpectedError, never>,
|
|
25
|
-
) => Effect.SyncOrPromiseOrEffect<CfWorker.Response | undefined, UnexpectedError>
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type Env = {
|
|
29
|
-
SYNC_BACKEND_DO: CfWorker.DurableObjectNamespace
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type MakeDurableObjectClass = <TSchema extends LiveStoreSchema = LiveStoreSchema.Any>(
|
|
33
|
-
options: MakeDurableObjectClassOptions<TSchema>,
|
|
34
|
-
) => {
|
|
35
|
-
new (ctx: CfWorker.DurableObjectState, env: Env): CfWorker.DurableObject & CfWorker.Rpc.DurableObjectBranded
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Options used to initialize the LiveStore Durable Object runtime.
|
|
40
|
-
*/
|
|
41
|
-
export type CreateStoreDoOptions<TSchema extends LiveStoreSchema = LiveStoreSchema.Any> = {
|
|
42
|
-
/** LiveStore schema that defines state, migrations, and validators. */
|
|
43
|
-
schema: TSchema
|
|
44
|
-
/** Logical identifier for the store instance persisted inside the Durable Object. */
|
|
45
|
-
storeId: string
|
|
46
|
-
/** Unique identifier for the client that owns the Durable Object instance. */
|
|
47
|
-
clientId: string
|
|
48
|
-
/** Identifier for the LiveStore session running inside the Durable Object. */
|
|
49
|
-
sessionId: string
|
|
50
|
-
/** Cloudflare Durable Object storage binding backing the local SQLite files. */
|
|
51
|
-
storage: CfWorker.DurableObjectStorage
|
|
52
|
-
/** RPC stub pointing at the sync backend Durable Object used for replication. */
|
|
53
|
-
syncBackendDurableObject: CfWorker.DurableObjectStub<CfSyncBackend.SyncBackendRpcInterface>
|
|
54
|
-
/** Durable Object identifier for the current instance, forwarded to the sync backend. */
|
|
55
|
-
durableObjectId: string
|
|
56
|
-
/** Binding name Cloudflare uses to reach this Durable Object from other workers. */
|
|
57
|
-
bindingName: string
|
|
58
|
-
/** Enables live pull mode to receive sync updates via Durable Object RPC callbacks. */
|
|
59
|
-
livePull?: boolean
|
|
60
|
-
/** Clears existing Durable Object persistence before bootstrapping the store. */
|
|
61
|
-
resetPersistence?: boolean
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export const createStoreDo = <TSchema extends LiveStoreSchema = LiveStoreSchema.Any>({
|
|
65
|
-
schema,
|
|
66
|
-
storeId,
|
|
67
|
-
clientId,
|
|
68
|
-
sessionId,
|
|
69
|
-
storage,
|
|
70
|
-
syncBackendDurableObject,
|
|
71
|
-
durableObjectId,
|
|
72
|
-
bindingName,
|
|
73
|
-
livePull = false,
|
|
74
|
-
resetPersistence = false,
|
|
75
|
-
}: CreateStoreDoOptions<TSchema>) =>
|
|
76
|
-
Effect.gen(function* () {
|
|
77
|
-
const scope = yield* Scope.make()
|
|
78
|
-
|
|
79
|
-
const adapter = makeAdapter({
|
|
80
|
-
clientId,
|
|
81
|
-
sessionId,
|
|
82
|
-
storage,
|
|
83
|
-
resetPersistence,
|
|
84
|
-
syncOptions: {
|
|
85
|
-
backend: makeDoRpcSync({
|
|
86
|
-
syncBackendStub: syncBackendDurableObject,
|
|
87
|
-
durableObjectContext: { bindingName, durableObjectId },
|
|
88
|
-
}),
|
|
89
|
-
livePull, // Uses DO RPC callbacks for reactive pull
|
|
90
|
-
// backend: makeHttpSync({ url: `http://localhost:8787`, livePull: { pollInterval: 500 } }),
|
|
91
|
-
initialSyncOptions: { _tag: 'Blocking', timeout: 500 },
|
|
92
|
-
// backend: makeWsSyncProviderClient({ durableObject: syncBackendDurableObject }),
|
|
93
|
-
},
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
return yield* createStore({ schema, adapter, storeId }).pipe(Scope.extend(scope), provideOtel({}))
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
export const createStoreDoPromise = <TSchema extends LiveStoreSchema = LiveStoreSchema.Any>(
|
|
100
|
-
options: CreateStoreDoOptions<TSchema>,
|
|
101
|
-
) =>
|
|
102
|
-
createStoreDo(options).pipe(
|
|
103
|
-
Logger.withMinimumLogLevel(LogLevel.Debug),
|
|
104
|
-
Effect.provide(Logger.consoleWithThread('DoClient')),
|
|
105
|
-
Effect.tapCauseLogPretty,
|
|
106
|
-
Effect.runPromise,
|
|
107
|
-
)
|