@livestore/adapter-cloudflare 0.4.0-dev.7 → 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.
@@ -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"}
@@ -1,9 +1,10 @@
1
1
  import { type Adapter, type SyncOptions } from '@livestore/common';
2
2
  import type * as CfWorker from './cf-types.ts';
3
- export declare const makeAdapter: ({ storage, clientId, syncOptions, sessionId, }: {
3
+ export declare const makeAdapter: ({ storage, clientId, syncOptions, sessionId, resetPersistence, }: {
4
4
  storage: CfWorker.DurableObjectStorage;
5
5
  clientId: string;
6
6
  syncOptions: SyncOptions;
7
7
  sessionId: string;
8
+ resetPersistence?: boolean;
8
9
  }) => Adapter;
9
10
  //# sourceMappingURL=make-adapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"make-adapter.d.ts","sourceRoot":"","sources":["../src/make-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EAKZ,KAAK,WAAW,EAEjB,MAAM,mBAAmB,CAAA;AAM1B,OAAO,KAAK,KAAK,QAAQ,MAAM,eAAe,CAAA;AAE9C,eAAO,MAAM,WAAW,GACrB,gDAKE;IACD,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAA;IACtC,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,WAAW,CAAA;IACxB,SAAS,EAAE,MAAM,CAAA;CAClB,KAAG,OAkHD,CAAA"}
1
+ {"version":3,"file":"make-adapter.d.ts","sourceRoot":"","sources":["../src/make-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EAKZ,KAAK,WAAW,EAEjB,MAAM,mBAAmB,CAAA;AAM1B,OAAO,KAAK,KAAK,QAAQ,MAAM,eAAe,CAAA;AAE9C,eAAO,MAAM,WAAW,GACrB,kEAME;IACD,OAAO,EAAE,QAAQ,CAAC,oBAAoB,CAAA;IACtC,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,WAAW,CAAA;IACxB,SAAS,EAAE,MAAM,CAAA;IACjB,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAC3B,KAAG,OA6HD,CAAA"}
@@ -3,24 +3,33 @@ import { Eventlog, LeaderThreadCtx, makeLeaderThreadLayer } from '@livestore/com
3
3
  import { LiveStoreEvent } from '@livestore/livestore';
4
4
  import { sqliteDbFactory } from '@livestore/sqlite-wasm/cf';
5
5
  import { loadSqlite3Wasm } from '@livestore/sqlite-wasm/load-wasm';
6
- import { Effect, FetchHttpClient, Layer, SubscriptionRef, WebChannel } from '@livestore/utils/effect';
7
- export const makeAdapter = ({ storage, clientId, syncOptions, sessionId, }) => (adapterArgs) => Effect.gen(function* () {
6
+ import { Effect, FetchHttpClient, Layer, Schedule, SubscriptionRef, WebChannel } from '@livestore/utils/effect';
7
+ export const makeAdapter = ({ storage, clientId, syncOptions, sessionId, resetPersistence = false, }) => (adapterArgs) => Effect.gen(function* () {
8
8
  const { storeId, /* devtoolsEnabled, shutdown, bootStatusQueue, */ syncPayload, schema } = adapterArgs;
9
9
  const devtoolsOptions = { enabled: false };
10
10
  const sqlite3 = yield* Effect.promise(() => loadSqlite3Wasm());
11
11
  const makeSqliteDb = sqliteDbFactory({ sqlite3 });
12
12
  const syncInMemoryDb = yield* makeSqliteDb({ _tag: 'in-memory', storage, configureDb: () => { } }).pipe(UnexpectedError.mapToUnexpectedError);
13
13
  const schemaHashSuffix = schema.state.sqlite.migrations.strategy === 'manual' ? 'fixed' : schema.state.sqlite.hash.toString();
14
+ const stateDbFileName = getStateDbFileName(schemaHashSuffix);
15
+ const eventlogDbFileName = getEventlogDbFileName();
16
+ if (resetPersistence === true) {
17
+ yield* resetDurableObjectPersistence({
18
+ storage,
19
+ storeId,
20
+ dbFileNames: [stateDbFileName, eventlogDbFileName],
21
+ });
22
+ }
14
23
  const dbState = yield* makeSqliteDb({
15
24
  _tag: 'storage',
16
25
  storage,
17
- fileName: getStateDbFileName(schemaHashSuffix),
26
+ fileName: stateDbFileName,
18
27
  configureDb: () => { },
19
28
  }).pipe(UnexpectedError.mapToUnexpectedError);
20
29
  const dbEventlog = yield* makeSqliteDb({
21
30
  _tag: 'storage',
22
31
  storage,
23
- fileName: `eventlog@${liveStoreStorageFormatVersion}.db`,
32
+ fileName: eventlogDbFileName,
24
33
  configureDb: () => { },
25
34
  }).pipe(UnexpectedError.mapToUnexpectedError);
26
35
  const shutdownChannel = yield* WebChannel.noopChannel();
@@ -84,4 +93,30 @@ export const makeAdapter = ({ storage, clientId, syncOptions, sessionId, }) => (
84
93
  return clientSession;
85
94
  }).pipe(Effect.withSpan('@livestore/adapter-cloudflare:makeAdapter', { attributes: { clientId, sessionId } }), Effect.provide(FetchHttpClient.layer));
86
95
  const getStateDbFileName = (suffix) => `state${suffix}@${liveStoreStorageFormatVersion}.db`;
96
+ const getEventlogDbFileName = () => `eventlog@${liveStoreStorageFormatVersion}.db`;
97
+ const resetDurableObjectPersistence = ({ storage, storeId, dbFileNames, }) => Effect.try({
98
+ try: () => storage.transactionSync(() => {
99
+ for (const baseName of dbFileNames) {
100
+ const likePattern = `${baseName}%`;
101
+ safeSqlExec(storage, 'DELETE FROM vfs_blocks WHERE file_path LIKE ?', likePattern);
102
+ safeSqlExec(storage, 'DELETE FROM vfs_files WHERE file_path LIKE ?', likePattern);
103
+ }
104
+ }),
105
+ catch: (cause) => new UnexpectedError({
106
+ cause,
107
+ note: `@livestore/adapter-cloudflare: Failed to reset persistence for store ${storeId}`,
108
+ }),
109
+ }).pipe(Effect.retry({ schedule: Schedule.exponentialBackoff10Sec }), Effect.withSpan('@livestore/adapter-cloudflare:resetPersistence', { attributes: { storeId } }));
110
+ const safeSqlExec = (storage, query, binding) => {
111
+ try {
112
+ storage.sql.exec(query, binding);
113
+ }
114
+ catch (error) {
115
+ if (isMissingVfsTableError(error)) {
116
+ return;
117
+ }
118
+ throw error;
119
+ }
120
+ };
121
+ const isMissingVfsTableError = (error) => error instanceof Error && error.message.toLowerCase().includes('no such table');
87
122
  //# sourceMappingURL=make-adapter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"make-adapter.js","sourceRoot":"","sources":["../src/make-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,8BAA8B,EAE9B,6BAA6B,EAC7B,iBAAiB,EAEjB,eAAe,GAChB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAwB,QAAQ,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAA;AACxH,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAA;AAClE,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,KAAK,EAAE,eAAe,EAAE,UAAU,EAA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,IAAI,CAAC;QACH,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAClC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,sBAAsB,CAAC,KAAK,CAAC,EAAE,CAAC;YAClC,OAAM;QACR,CAAC;QAED,MAAM,KAAK,CAAA;IACb,CAAC;AACH,CAAC,CAAA;AAED,MAAM,sBAAsB,GAAG,CAAC,KAAc,EAAW,EAAE,CACzD,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAA"}
@@ -18,17 +18,31 @@ export type Env = {
18
18
  export type MakeDurableObjectClass = <TSchema extends LiveStoreSchema = LiveStoreSchema.Any>(options: MakeDurableObjectClassOptions<TSchema>) => {
19
19
  new (ctx: CfWorker.DurableObjectState, env: Env): CfWorker.DurableObject & CfWorker.Rpc.DurableObjectBranded;
20
20
  };
21
+ /**
22
+ * Options used to initialize the LiveStore Durable Object runtime.
23
+ */
21
24
  export type CreateStoreDoOptions<TSchema extends LiveStoreSchema = LiveStoreSchema.Any> = {
25
+ /** LiveStore schema that defines state, migrations, and validators. */
22
26
  schema: TSchema;
27
+ /** Logical identifier for the store instance persisted inside the Durable Object. */
23
28
  storeId: string;
29
+ /** Unique identifier for the client that owns the Durable Object instance. */
24
30
  clientId: string;
31
+ /** Identifier for the LiveStore session running inside the Durable Object. */
25
32
  sessionId: string;
33
+ /** Cloudflare Durable Object storage binding backing the local SQLite files. */
26
34
  storage: CfWorker.DurableObjectStorage;
35
+ /** RPC stub pointing at the sync backend Durable Object used for replication. */
27
36
  syncBackendDurableObject: CfWorker.DurableObjectStub<CfSyncBackend.SyncBackendRpcInterface>;
37
+ /** Durable Object identifier for the current instance, forwarded to the sync backend. */
28
38
  durableObjectId: string;
39
+ /** Binding name Cloudflare uses to reach this Durable Object from other workers. */
29
40
  bindingName: string;
41
+ /** Enables live pull mode to receive sync updates via Durable Object RPC callbacks. */
30
42
  livePull?: boolean;
43
+ /** Clears existing Durable Object persistence before bootstrapping the store. */
44
+ resetPersistence?: boolean;
31
45
  };
32
- export declare const createStoreDo: <TSchema extends LiveStoreSchema = LiveStoreSchema.Any>({ schema, storeId, clientId, sessionId, storage, syncBackendDurableObject, durableObjectId, bindingName, livePull, }: CreateStoreDoOptions<TSchema>) => Effect.Effect<Store<TSchema, {}>, UnexpectedError, never>;
46
+ export declare const createStoreDo: <TSchema extends LiveStoreSchema = LiveStoreSchema.Any>({ schema, storeId, clientId, sessionId, storage, syncBackendDurableObject, durableObjectId, bindingName, livePull, resetPersistence, }: CreateStoreDoOptions<TSchema>) => Effect.Effect<Store<TSchema, {}>, UnexpectedError, never>;
33
47
  export declare const createStoreDoPromise: <TSchema extends LiveStoreSchema = LiveStoreSchema.Any>(options: CreateStoreDoOptions<TSchema>) => Promise<Store<TSchema, {}>>;
34
48
  //# sourceMappingURL=make-client-durable-object.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"make-client-durable-object.d.ts","sourceRoot":"","sources":["../src/make-client-durable-object.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,EAAe,KAAK,eAAe,EAAe,KAAK,KAAK,EAAE,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AACnH,OAAO,KAAK,KAAK,aAAa,MAAM,8BAA8B,CAAA;AAElE,OAAO,EAAE,MAAM,EAA2B,MAAM,yBAAyB,CAAA;AACzE,OAAO,KAAK,KAAK,QAAQ,MAAM,eAAe,CAAA;AAK9C,MAAM,MAAM,6BAA6B,CAAC,OAAO,SAAS,eAAe,GAAG,eAAe,CAAC,GAAG,IAAI;IACjG,MAAM,EAAE,OAAO,CAAA;IAEf,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,CA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,CAAA;AAED,eAAO,MAAM,aAAa,GAAI,OAAO,SAAS,eAAe,GAAG,eAAe,CAAC,GAAG,EAAE,wIAWlF,oBAAoB,CAAC,OAAO,CAAC,8DAsB5B,CAAA;AAEJ,eAAO,MAAM,oBAAoB,GAAI,OAAO,SAAS,eAAe,GAAG,eAAe,CAAC,GAAG,EACxF,SAAS,oBAAoB,CAAC,OAAO,CAAC,gCAOrC,CAAA"}
@@ -2,12 +2,13 @@ import { createStore, provideOtel } from '@livestore/livestore';
2
2
  import { makeDoRpcSync } from '@livestore/sync-cf/client';
3
3
  import { Effect, Logger, LogLevel, Scope } from '@livestore/utils/effect';
4
4
  import { makeAdapter } from "./make-adapter.js";
5
- export const createStoreDo = ({ schema, storeId, clientId, sessionId, storage, syncBackendDurableObject, durableObjectId, bindingName, livePull = false, }) => Effect.gen(function* () {
5
+ export const createStoreDo = ({ schema, storeId, clientId, sessionId, storage, syncBackendDurableObject, durableObjectId, bindingName, livePull = false, resetPersistence = false, }) => Effect.gen(function* () {
6
6
  const scope = yield* Scope.make();
7
7
  const adapter = makeAdapter({
8
8
  clientId,
9
9
  sessionId,
10
10
  storage,
11
+ resetPersistence,
11
12
  syncOptions: {
12
13
  backend: makeDoRpcSync({
13
14
  syncBackendStub: syncBackendDurableObject,
@@ -1 +1 @@
1
- {"version":3,"file":"make-client-durable-object.js","sourceRoot":"","sources":["../src/make-client-durable-object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAwB,WAAW,EAAgC,MAAM,sBAAsB,CAAA;AAEnH,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;AA2C/C,MAAM,CAAC,MAAM,aAAa,GAAG,CAAwD,EACnF,MAAM,EACN,OAAO,EACP,QAAQ,EACR,SAAS,EACT,OAAO,EACP,wBAAwB,EACxB,eAAe,EACf,WAAW,EACX,QAAQ,GAAG,KAAK,GACc,EAAE,EAAE,CAClC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IAClB,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;IAEjC,MAAM,OAAO,GAAG,WAAW,CAAC;QAC1B,QAAQ;QACR,SAAS;QACT,OAAO;QACP,WAAW,EAAE;YACX,OAAO,EAAE,aAAa,CAAC;gBACrB,eAAe,EAAE,wBAAwB;gBACzC,oBAAoB,EAAE,EAAE,WAAW,EAAE,eAAe,EAAE;aACvD,CAAC;YACF,QAAQ,EAAE,0CAA0C;YACpD,4FAA4F;YAC5F,kBAAkB,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,EAAE;YACtD,kFAAkF;SACnF;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,CAClC,OAAsC,EACtC,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"}
1
+ {"version":3,"file":"make-client-durable-object.js","sourceRoot":"","sources":["../src/make-client-durable-object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAwB,WAAW,EAAgC,MAAM,sBAAsB,CAAA;AAEnH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C,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,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EACL,KAAK,oBAAoB,EACzB,aAAa,EACb,oBAAoB,EACpB,KAAK,GAAG,EACR,KAAK,sBAAsB,EAC3B,KAAK,6BAA6B,GACnC,MAAM,iCAAiC,CAAA"}
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,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAEL,aAAa,EACb,oBAAoB,GAIrB,MAAM,iCAAiC,CAAA"}
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.7",
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.20250823.0",
15
- "@livestore/common": "0.4.0-dev.7",
16
- "@livestore/livestore": "0.4.0-dev.7",
17
- "@livestore/common-cf": "0.4.0-dev.7",
18
- "@livestore/sqlite-wasm": "0.4.0-dev.7",
19
- "@livestore/sync-cf": "0.4.0-dev.7",
20
- "@livestore/utils": "0.4.0-dev.7"
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.32.0"
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
+ )
@@ -11,7 +11,7 @@ import { type DevtoolsOptions, Eventlog, LeaderThreadCtx, makeLeaderThreadLayer
11
11
  import { LiveStoreEvent } from '@livestore/livestore'
12
12
  import { sqliteDbFactory } from '@livestore/sqlite-wasm/cf'
13
13
  import { loadSqlite3Wasm } from '@livestore/sqlite-wasm/load-wasm'
14
- import { Effect, FetchHttpClient, Layer, SubscriptionRef, WebChannel } from '@livestore/utils/effect'
14
+ import { Effect, FetchHttpClient, Layer, Schedule, SubscriptionRef, WebChannel } from '@livestore/utils/effect'
15
15
  import type * as CfWorker from './cf-types.ts'
16
16
 
17
17
  export const makeAdapter =
@@ -20,11 +20,13 @@ export const makeAdapter =
20
20
  clientId,
21
21
  syncOptions,
22
22
  sessionId,
23
+ resetPersistence = false,
23
24
  }: {
24
25
  storage: CfWorker.DurableObjectStorage
25
26
  clientId: string
26
27
  syncOptions: SyncOptions
27
28
  sessionId: string
29
+ resetPersistence?: boolean
28
30
  }): Adapter =>
29
31
  (adapterArgs) =>
30
32
  Effect.gen(function* () {
@@ -43,17 +45,28 @@ export const makeAdapter =
43
45
  const schemaHashSuffix =
44
46
  schema.state.sqlite.migrations.strategy === 'manual' ? 'fixed' : schema.state.sqlite.hash.toString()
45
47
 
48
+ const stateDbFileName = getStateDbFileName(schemaHashSuffix)
49
+ const eventlogDbFileName = getEventlogDbFileName()
50
+
51
+ if (resetPersistence === true) {
52
+ yield* resetDurableObjectPersistence({
53
+ storage,
54
+ storeId,
55
+ dbFileNames: [stateDbFileName, eventlogDbFileName],
56
+ })
57
+ }
58
+
46
59
  const dbState = yield* makeSqliteDb({
47
60
  _tag: 'storage',
48
61
  storage,
49
- fileName: getStateDbFileName(schemaHashSuffix),
62
+ fileName: stateDbFileName,
50
63
  configureDb: () => {},
51
64
  }).pipe(UnexpectedError.mapToUnexpectedError)
52
65
 
53
66
  const dbEventlog = yield* makeSqliteDb({
54
67
  _tag: 'storage',
55
68
  storage,
56
- fileName: `eventlog@${liveStoreStorageFormatVersion}.db`,
69
+ fileName: eventlogDbFileName,
57
70
  configureDb: () => {},
58
71
  }).pipe(UnexpectedError.mapToUnexpectedError)
59
72
 
@@ -142,3 +155,48 @@ export const makeAdapter =
142
155
  )
143
156
 
144
157
  const getStateDbFileName = (suffix: string) => `state${suffix}@${liveStoreStorageFormatVersion}.db`
158
+
159
+ const getEventlogDbFileName = () => `eventlog@${liveStoreStorageFormatVersion}.db`
160
+
161
+ const resetDurableObjectPersistence = ({
162
+ storage,
163
+ storeId,
164
+ dbFileNames,
165
+ }: {
166
+ storage: CfWorker.DurableObjectStorage
167
+ storeId: string
168
+ dbFileNames: ReadonlyArray<string>
169
+ }) =>
170
+ Effect.try({
171
+ try: () =>
172
+ storage.transactionSync(() => {
173
+ for (const baseName of dbFileNames) {
174
+ const likePattern = `${baseName}%`
175
+ safeSqlExec(storage, 'DELETE FROM vfs_blocks WHERE file_path LIKE ?', likePattern)
176
+ safeSqlExec(storage, 'DELETE FROM vfs_files WHERE file_path LIKE ?', likePattern)
177
+ }
178
+ }),
179
+ catch: (cause) =>
180
+ new UnexpectedError({
181
+ cause,
182
+ note: `@livestore/adapter-cloudflare: Failed to reset persistence for store ${storeId}`,
183
+ }),
184
+ }).pipe(
185
+ Effect.retry({ schedule: Schedule.exponentialBackoff10Sec }),
186
+ Effect.withSpan('@livestore/adapter-cloudflare:resetPersistence', { attributes: { storeId } }),
187
+ )
188
+
189
+ const safeSqlExec = (storage: CfWorker.DurableObjectStorage, query: string, binding: string) => {
190
+ try {
191
+ storage.sql.exec(query, binding)
192
+ } catch (error) {
193
+ if (isMissingVfsTableError(error)) {
194
+ return
195
+ }
196
+
197
+ throw error
198
+ }
199
+ }
200
+
201
+ const isMissingVfsTableError = (error: unknown): boolean =>
202
+ error instanceof Error && error.message.toLowerCase().includes('no such table')
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
- type MakeDurableObjectClass,
11
- type MakeDurableObjectClassOptions,
12
- } from './make-client-durable-object.ts'
9
+ } from './create-store-do.ts'
10
+ export { makeAdapter } from './make-adapter.ts'
@@ -1,91 +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
- export type CreateStoreDoOptions<TSchema extends LiveStoreSchema = LiveStoreSchema.Any> = {
39
- schema: TSchema
40
- storeId: string
41
- clientId: string
42
- sessionId: string
43
- storage: CfWorker.DurableObjectStorage
44
- syncBackendDurableObject: CfWorker.DurableObjectStub<CfSyncBackend.SyncBackendRpcInterface>
45
- durableObjectId: string
46
- bindingName: string
47
- livePull?: boolean
48
- }
49
-
50
- export const createStoreDo = <TSchema extends LiveStoreSchema = LiveStoreSchema.Any>({
51
- schema,
52
- storeId,
53
- clientId,
54
- sessionId,
55
- storage,
56
- syncBackendDurableObject,
57
- durableObjectId,
58
- bindingName,
59
- livePull = false,
60
- }: CreateStoreDoOptions<TSchema>) =>
61
- Effect.gen(function* () {
62
- const scope = yield* Scope.make()
63
-
64
- const adapter = makeAdapter({
65
- clientId,
66
- sessionId,
67
- storage,
68
- syncOptions: {
69
- backend: makeDoRpcSync({
70
- syncBackendStub: syncBackendDurableObject,
71
- durableObjectContext: { bindingName, durableObjectId },
72
- }),
73
- livePull, // Uses DO RPC callbacks for reactive pull
74
- // backend: makeHttpSync({ url: `http://localhost:8787`, livePull: { pollInterval: 500 } }),
75
- initialSyncOptions: { _tag: 'Blocking', timeout: 500 },
76
- // backend: makeWsSyncProviderClient({ durableObject: syncBackendDurableObject }),
77
- },
78
- })
79
-
80
- return yield* createStore({ schema, adapter, storeId }).pipe(Scope.extend(scope), provideOtel({}))
81
- })
82
-
83
- export const createStoreDoPromise = <TSchema extends LiveStoreSchema = LiveStoreSchema.Any>(
84
- options: CreateStoreDoOptions<TSchema>,
85
- ) =>
86
- createStoreDo(options).pipe(
87
- Logger.withMinimumLogLevel(LogLevel.Debug),
88
- Effect.provide(Logger.consoleWithThread('DoClient')),
89
- Effect.tapCauseLogPretty,
90
- Effect.runPromise,
91
- )