@livestore/adapter-cloudflare 0.4.0 → 0.5.0-dev.1

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.
@@ -6,6 +6,7 @@ import {
6
6
  makeClientSession,
7
7
  type SyncOptions,
8
8
  UnknownError,
9
+ StateHead,
9
10
  } from '@livestore/common'
10
11
  import type { CfTypes } from '@livestore/common-cf'
11
12
  import {
@@ -15,11 +16,14 @@ import {
15
16
  makeLeaderThreadLayer,
16
17
  streamEventsWithSyncState,
17
18
  } from '@livestore/common/leader-thread'
19
+ import { getStateDbBaseName } from '@livestore/common/schema'
18
20
  import { LiveStoreEvent } from '@livestore/livestore'
19
21
  import { CF_SQL_VFS_REQUIRED_PRAGMAS, sqliteDbFactory } from '@livestore/sqlite-wasm/cf'
20
- import { makeSqliteDb as makeDoSqliteDb } from './make-sqlite-db.ts'
21
22
  import { loadSqlite3Wasm } from '@livestore/sqlite-wasm/load-wasm'
22
- import { Effect, FetchHttpClient, Layer, Schedule, SubscriptionRef, WebChannel } from '@livestore/utils/effect'
23
+ import { Effect, FetchHttpClient, Layer, Queue, Schedule, SubscriptionRef, WebChannel } from '@livestore/utils/effect'
24
+
25
+ import { makeSqliteDb as makeDoSqliteDb } from './make-sqlite-db.ts'
26
+ import * as StateFiles from './state-files.ts'
23
27
 
24
28
  export const makeAdapter =
25
29
  ({
@@ -43,6 +47,7 @@ export const makeAdapter =
43
47
  syncPayloadEncoded,
44
48
  syncPayloadSchema,
45
49
  schema,
50
+ params,
46
51
  } = adapterArgs
47
52
 
48
53
  const devtoolsOptions = { enabled: false } as DevtoolsOptions
@@ -55,23 +60,19 @@ export const makeAdapter =
55
60
  UnknownError.mapToUnknownError,
56
61
  )
57
62
 
58
- const schemaHashSuffix =
59
- schema.state.sqlite.migrations.strategy === 'manual' ? 'fixed' : schema.state.sqlite.hash.toString()
60
-
61
63
  if (resetPersistence === true) {
62
64
  yield* resetDurableObjectPersistence({ storage, storeId })
63
65
  }
64
66
 
65
- const stateDbFileName = getStateDbFileName(schemaHashSuffix)
67
+ const stateFileName = `${getStateDbBaseName(schema)}@${liveStoreStorageFormatVersion}.db`
68
+ yield* StateFiles.register(storage, stateFileName).pipe(UnknownError.mapToUnknownError)
66
69
 
67
70
  const dbState = yield* makeSqliteDb({
68
71
  _tag: 'storage',
69
72
  storage,
70
- fileName: stateDbFileName,
73
+ fileName: stateFileName,
71
74
  configureDb: (db) =>
72
- db.execute(
73
- [...CF_SQL_VFS_REQUIRED_PRAGMAS, 'cache_size=-8000'].map((p) => `PRAGMA ${p}`).join(';\n'),
74
- ),
75
+ db.execute([...CF_SQL_VFS_REQUIRED_PRAGMAS, 'cache_size=-8000'].map((p) => `PRAGMA ${p}`).join(';\n')),
75
76
  }).pipe(UnknownError.mapToUnknownError)
76
77
 
77
78
  // dbEventlog runs on DO SQLite directly (not through the VFS). SQL-level transaction
@@ -98,7 +99,8 @@ export const makeAdapter =
98
99
  shutdownChannel,
99
100
  syncPayloadEncoded,
100
101
  syncPayloadSchema,
101
- }),
102
+ params,
103
+ }).pipe(Layer.provide(StateHead.layer({ dbState }))),
102
104
  )
103
105
 
104
106
  const { leaderThread, initialSnapshot } = yield* Effect.gen(function* () {
@@ -112,11 +114,7 @@ export const makeAdapter =
112
114
  {
113
115
  events: {
114
116
  pull: ({ cursor }) => syncProcessor.pull({ cursor }),
115
- push: (batch) =>
116
- syncProcessor.push(
117
- batch.map((item) => new LiveStoreEvent.Client.EncodedWithMeta(item)),
118
- { waitForProcessing: true },
119
- ),
117
+ push: (batch) => syncProcessor.push(batch.map((item) => new LiveStoreEvent.Client.EncodedWithMeta(item))),
120
118
  stream: (options) =>
121
119
  streamEventsWithSyncState({
122
120
  dbEventlog,
@@ -132,7 +130,7 @@ export const makeAdapter =
132
130
  export: Effect.sync(() => dbState.export()),
133
131
  getEventlogData: Effect.sync(() => dbEventlog.export()),
134
132
  syncState: syncProcessor.syncState,
135
- sendDevtoolsMessage: (message) => extraIncomingMessagesQueue.offer(message),
133
+ sendDevtoolsMessage: (message) => Queue.offer(extraIncomingMessagesQueue, message),
136
134
  networkStatus,
137
135
  },
138
136
  {
@@ -173,14 +171,16 @@ export const makeAdapter =
173
171
  origin: undefined,
174
172
  })
175
173
 
174
+ yield* StateFiles.cleanup(storage, stateFileName).pipe(
175
+ Effect.catch((cause) => Effect.logWarning('Failed to clean up obsolete state databases', cause)),
176
+ )
177
+
176
178
  return clientSession
177
179
  }).pipe(
178
180
  Effect.withSpan('@livestore/adapter-cloudflare:makeAdapter', { attributes: { clientId, sessionId } }),
179
181
  Effect.provide(FetchHttpClient.layer),
180
182
  )
181
183
 
182
- const getStateDbFileName = (suffix: string) => `state${suffix}@${liveStoreStorageFormatVersion}.db`
183
-
184
184
  const resetDurableObjectPersistence = ({
185
185
  storage,
186
186
  storeId,
@@ -190,12 +190,14 @@ const resetDurableObjectPersistence = ({
190
190
  }) =>
191
191
  Effect.try({
192
192
  try: () =>
193
- // All three tables live in the DO's single storage.sql database but are
193
+ // All four tables live in the DO's single storage.sql database but are
194
194
  // owned by different layers during normal operation:
195
195
  // - vfs_pages: written by the wa-sqlite VFS layer (backs dbState)
196
196
  // - eventlog, __livestore_sync_status: written directly by dbEventlog via storage.sql
197
+ // - __livestore_state_files: adapter-owned state file registry
197
198
  storage.transactionSync(() => {
198
199
  safeSqlExec(storage, 'DELETE FROM vfs_pages')
200
+ safeSqlExec(storage, 'DELETE FROM __livestore_state_files')
199
201
  safeSqlExec(storage, 'DELETE FROM eventlog')
200
202
  safeSqlExec(storage, 'DELETE FROM __livestore_sync_status')
201
203
  }),
@@ -26,7 +26,6 @@ class CloudflarePreparedStatement implements PreparedStatement {
26
26
  try {
27
27
  if (isTransactionControlStatement(this.sql) === true) return
28
28
 
29
-
30
29
  const cursor = this.sqlStorage.exec(this.sql, ...(bindValues !== undefined ? Object.values(bindValues) : []))
31
30
 
32
31
  // Count affected rows by iterating through cursor
@@ -0,0 +1,23 @@
1
+ import type { CfTypes } from '@livestore/common-cf'
2
+ import { Effect } from '@livestore/utils/effect'
3
+
4
+ /** Record ownership before opening state so interrupted rebuilds remain eligible for cleanup. */
5
+ export const register = (storage: CfTypes.DurableObjectStorage, fileName: string) =>
6
+ Effect.try(() => {
7
+ storage.sql.exec(`CREATE TABLE IF NOT EXISTS __livestore_state_files (file_path TEXT PRIMARY KEY) WITHOUT ROWID`)
8
+ storage.sql.exec('INSERT OR IGNORE INTO __livestore_state_files (file_path) VALUES (?)', `/${fileName}`)
9
+ })
10
+
11
+ /** Keep ownership records until their pages are deleted, so failed cleanup can be retried. */
12
+ export const cleanup = (storage: CfTypes.DurableObjectStorage, fileName: string) =>
13
+ Effect.try(() =>
14
+ storage.transactionSync(() => {
15
+ storage.sql.exec(
16
+ `DELETE FROM vfs_pages WHERE file_path IN (
17
+ SELECT file_path FROM __livestore_state_files WHERE file_path != ?
18
+ )`,
19
+ `/${fileName}`,
20
+ )
21
+ storage.sql.exec('DELETE FROM __livestore_state_files WHERE file_path != ?', `/${fileName}`)
22
+ }),
23
+ )