@livestore/adapter-web 0.4.0-dev.9 → 0.5.0-dev.0
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 +5 -5
- package/dist/.tsbuildinfo +1 -1
- package/dist/in-memory/in-memory-adapter.d.ts +50 -8
- package/dist/in-memory/in-memory-adapter.d.ts.map +1 -1
- package/dist/in-memory/in-memory-adapter.js +100 -39
- package/dist/in-memory/in-memory-adapter.js.map +1 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/single-tab/mod.d.ts +15 -0
- package/dist/single-tab/mod.d.ts.map +1 -0
- package/dist/single-tab/mod.js +15 -0
- package/dist/single-tab/mod.js.map +1 -0
- package/dist/single-tab/single-tab-adapter.d.ts +108 -0
- package/dist/single-tab/single-tab-adapter.d.ts.map +1 -0
- package/dist/single-tab/single-tab-adapter.js +252 -0
- package/dist/single-tab/single-tab-adapter.js.map +1 -0
- package/dist/web-worker/client-session/client-session-devtools.d.ts +4 -5
- package/dist/web-worker/client-session/client-session-devtools.d.ts.map +1 -1
- package/dist/web-worker/client-session/client-session-devtools.js +27 -14
- package/dist/web-worker/client-session/client-session-devtools.js.map +1 -1
- package/dist/web-worker/client-session/devtools-web-channel.d.ts +20 -0
- package/dist/web-worker/client-session/devtools-web-channel.d.ts.map +1 -0
- package/dist/web-worker/client-session/devtools-web-channel.js +34 -0
- package/dist/web-worker/client-session/devtools-web-channel.js.map +1 -0
- package/dist/web-worker/client-session/persisted-adapter.d.ts +18 -0
- package/dist/web-worker/client-session/persisted-adapter.d.ts.map +1 -1
- package/dist/web-worker/client-session/persisted-adapter.js +170 -103
- package/dist/web-worker/client-session/persisted-adapter.js.map +1 -1
- package/dist/web-worker/client-session/sqlite-loader.d.ts +2 -0
- package/dist/web-worker/client-session/sqlite-loader.d.ts.map +1 -0
- package/dist/web-worker/client-session/sqlite-loader.js +16 -0
- package/dist/web-worker/client-session/sqlite-loader.js.map +1 -0
- package/dist/web-worker/common/persisted-sqlite.d.ts +15 -35
- package/dist/web-worker/common/persisted-sqlite.d.ts.map +1 -1
- package/dist/web-worker/common/persisted-sqlite.js +91 -201
- package/dist/web-worker/common/persisted-sqlite.js.map +1 -1
- package/dist/web-worker/common/rpc-worker.d.ts +14 -0
- package/dist/web-worker/common/rpc-worker.d.ts.map +1 -0
- package/dist/web-worker/common/rpc-worker.js +8 -0
- package/dist/web-worker/common/rpc-worker.js.map +1 -0
- package/dist/web-worker/common/shutdown-channel.d.ts +3 -2
- package/dist/web-worker/common/shutdown-channel.d.ts.map +1 -1
- package/dist/web-worker/common/shutdown-channel.js +2 -2
- package/dist/web-worker/common/shutdown-channel.js.map +1 -1
- package/dist/web-worker/common/webmesh-node-names.d.ts +4 -0
- package/dist/web-worker/common/webmesh-node-names.d.ts.map +1 -0
- package/dist/web-worker/common/webmesh-node-names.js +2 -0
- package/dist/web-worker/common/webmesh-node-names.js.map +1 -0
- package/dist/web-worker/common/worker-disconnect-channel.d.ts +3 -10
- package/dist/web-worker/common/worker-disconnect-channel.d.ts.map +1 -1
- package/dist/web-worker/common/worker-disconnect-channel.js +4 -4
- package/dist/web-worker/common/worker-disconnect-channel.js.map +1 -1
- package/dist/web-worker/common/worker-schema.d.ts +295 -146
- package/dist/web-worker/common/worker-schema.d.ts.map +1 -1
- package/dist/web-worker/common/worker-schema.js +89 -54
- package/dist/web-worker/common/worker-schema.js.map +1 -1
- package/dist/web-worker/leader-worker/make-leader-worker.d.ts +6 -6
- package/dist/web-worker/leader-worker/make-leader-worker.d.ts.map +1 -1
- package/dist/web-worker/leader-worker/make-leader-worker.js +191 -80
- package/dist/web-worker/leader-worker/make-leader-worker.js.map +1 -1
- package/dist/web-worker/shared-worker/make-shared-worker.d.ts +2 -1
- package/dist/web-worker/shared-worker/make-shared-worker.d.ts.map +1 -1
- package/dist/web-worker/shared-worker/make-shared-worker.js +91 -94
- package/dist/web-worker/shared-worker/make-shared-worker.js.map +1 -1
- package/dist/web-worker/vite-dev-polyfill.js +1 -2
- package/dist/web-worker/vite-dev-polyfill.js.map +1 -1
- package/package.json +47 -19
- package/src/in-memory/in-memory-adapter.ts +147 -73
- package/src/index.ts +15 -1
- package/src/single-tab/mod.ts +15 -0
- package/src/single-tab/single-tab-adapter.ts +499 -0
- package/src/web-worker/ambient.d.ts +7 -24
- package/src/web-worker/client-session/client-session-devtools.ts +47 -25
- package/src/web-worker/client-session/devtools-web-channel.ts +57 -0
- package/src/web-worker/client-session/persisted-adapter.ts +259 -163
- package/src/web-worker/client-session/sqlite-loader.ts +19 -0
- package/src/web-worker/common/persisted-sqlite.ts +204 -309
- package/src/web-worker/common/rpc-worker.ts +39 -0
- package/src/web-worker/common/shutdown-channel.ts +10 -3
- package/src/web-worker/common/webmesh-node-names.ts +1 -0
- package/src/web-worker/common/worker-disconnect-channel.ts +11 -4
- package/src/web-worker/common/worker-schema.ts +138 -131
- package/src/web-worker/leader-worker/make-leader-worker.ts +396 -194
- package/src/web-worker/shared-worker/make-shared-worker.ts +143 -146
- package/src/web-worker/vite-dev-polyfill.ts +1 -2
- package/dist/opfs-utils.d.ts +0 -7
- package/dist/opfs-utils.d.ts.map +0 -1
- package/dist/opfs-utils.js +0 -43
- package/dist/opfs-utils.js.map +0 -1
- package/src/opfs-utils.ts +0 -61
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { loadSqlite3Wasm } from '@livestore/sqlite-wasm/load-wasm'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Browser sessions benefit from downloading and compiling the wasm binary as soon as
|
|
5
|
+
* possible to hide network and IO latency behind the rest of the boot process. We kick
|
|
6
|
+
* that work off eagerly on the client while still returning the shared promise.
|
|
7
|
+
*
|
|
8
|
+
* The Cloudflare / Workerd runtime has stricter rules: async fetches during module
|
|
9
|
+
* evaluation are blocked, so we defer loading until the worker asks for it.
|
|
10
|
+
*/
|
|
11
|
+
const isServerRuntime = String(import.meta.env?.SSR) === 'true' || typeof window === 'undefined'
|
|
12
|
+
|
|
13
|
+
let sqlite3Promise: ReturnType<typeof loadSqlite3Wasm> | undefined
|
|
14
|
+
|
|
15
|
+
if (isServerRuntime === false) {
|
|
16
|
+
sqlite3Promise = loadSqlite3Wasm()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const loadSqlite3 = () => (isServerRuntime === true ? loadSqlite3Wasm() : (sqlite3Promise ?? loadSqlite3Wasm()))
|
|
@@ -1,150 +1,131 @@
|
|
|
1
|
-
import { liveStoreStorageFormatVersion
|
|
2
|
-
import type
|
|
3
|
-
import {
|
|
1
|
+
import { liveStoreStorageFormatVersion } from '@livestore/common'
|
|
2
|
+
import { getStateDbBaseName, type LiveStoreSchema } from '@livestore/common/schema'
|
|
3
|
+
import {
|
|
4
|
+
decodeAccessHandlePoolFilename,
|
|
5
|
+
HEADER_OFFSET_DATA,
|
|
6
|
+
type WebDatabaseMetadataOpfs,
|
|
7
|
+
} from '@livestore/sqlite-wasm/browser'
|
|
4
8
|
import { isDevEnv } from '@livestore/utils'
|
|
5
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
Effect,
|
|
11
|
+
Option,
|
|
12
|
+
Order,
|
|
13
|
+
pipe,
|
|
14
|
+
ReadonlyArray as EffectArray,
|
|
15
|
+
Schedule,
|
|
16
|
+
Schema,
|
|
17
|
+
Stream,
|
|
18
|
+
} from '@livestore/utils/effect'
|
|
19
|
+
import { Opfs, type WebError } from '@livestore/utils/effect/browser'
|
|
6
20
|
|
|
7
|
-
import * as OpfsUtils from '../../opfs-utils.ts'
|
|
8
21
|
import type * as WorkerSchema from './worker-schema.ts'
|
|
9
22
|
|
|
10
|
-
export class PersistedSqliteError extends Schema.TaggedError<PersistedSqliteError>(
|
|
23
|
+
export class PersistedSqliteError extends Schema.TaggedError<PersistedSqliteError>(
|
|
24
|
+
'~@livestore/adapter-web/PersistedSqliteError',
|
|
25
|
+
)('PersistedSqliteError', {
|
|
11
26
|
message: Schema.String,
|
|
12
|
-
cause: Schema.Defect,
|
|
27
|
+
cause: Schema.optional(Schema.Defect()),
|
|
13
28
|
}) {}
|
|
14
29
|
|
|
15
|
-
export const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
export const readPersistedStateDbFromClientSession = Effect.fn(
|
|
31
|
+
'@livestore/adapter-web:readPersistedStateDbFromClientSession',
|
|
32
|
+
)(
|
|
33
|
+
function* ({
|
|
34
|
+
storageOptions,
|
|
35
|
+
storeId,
|
|
36
|
+
schema,
|
|
37
|
+
}: {
|
|
38
|
+
storageOptions: WorkerSchema.StorageType
|
|
39
|
+
storeId: string
|
|
40
|
+
schema: LiveStoreSchema
|
|
41
|
+
}) {
|
|
42
|
+
const accessHandlePoolDirString = yield* sanitizeOpfsDir(storageOptions.directory, storeId)
|
|
43
|
+
|
|
44
|
+
const accessHandlePoolDirHandle = yield* Opfs.getDirectoryHandleByPath(accessHandlePoolDirString)
|
|
45
|
+
|
|
46
|
+
const stateDbFileName = `/${getStateDbFileName(schema)}`
|
|
47
|
+
|
|
48
|
+
const opfs = yield* Opfs.Opfs
|
|
49
|
+
const handlesStream = opfs.values(accessHandlePoolDirHandle)
|
|
50
|
+
|
|
51
|
+
const stateDbFileOption = yield* handlesStream.pipe(
|
|
52
|
+
Stream.filter((handle): handle is FileSystemFileHandle => handle.kind === 'file'),
|
|
53
|
+
Stream.mapEffect(
|
|
54
|
+
(fileHandle) =>
|
|
55
|
+
Effect.gen(function* () {
|
|
56
|
+
const file = yield* opfs.getFile(fileHandle)
|
|
57
|
+
const fileName = yield* Effect.promise(() => decodeAccessHandlePoolFilename(file))
|
|
58
|
+
return { file, fileName }
|
|
59
|
+
}),
|
|
60
|
+
{ concurrency: 'unbounded' },
|
|
61
|
+
),
|
|
62
|
+
Stream.filter(({ fileName }) => fileName === stateDbFileName),
|
|
63
|
+
Stream.runFirst,
|
|
64
|
+
)
|
|
37
65
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
results.push(value as FileSystemFileHandle)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return results
|
|
66
|
+
if (Option.isNone(stateDbFileOption) === true) {
|
|
67
|
+
return yield* new PersistedSqliteError({
|
|
68
|
+
message: `State database file not found in client session (expected '${stateDbFileName}' in '${accessHandlePoolDirString}')`,
|
|
69
|
+
})
|
|
46
70
|
}
|
|
47
71
|
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const appDbFileName = `/${getStateDbFileName(schema)}`
|
|
53
|
-
|
|
54
|
-
const dbFileRes = fileResults.find((_) => _?.fileName === appDbFileName)
|
|
55
|
-
// console.debug('fileResults', fileResults, 'dbFileRes', dbFileRes)
|
|
56
|
-
|
|
57
|
-
if (dbFileRes !== undefined) {
|
|
58
|
-
const data = await dbFileRes.file.slice(HEADER_OFFSET_DATA).arrayBuffer()
|
|
59
|
-
// console.debug('readPersistedAppDbFromClientSession', data.byteLength, data)
|
|
60
|
-
|
|
61
|
-
// Given the SAH pool always eagerly creates files with empty non-header data,
|
|
62
|
-
// we want to return undefined if the file exists but is empty
|
|
63
|
-
if (data.byteLength === 0) {
|
|
64
|
-
return undefined
|
|
65
|
-
}
|
|
72
|
+
const stateDbBuffer = yield* Effect.promise(() =>
|
|
73
|
+
stateDbFileOption.value.file.slice(HEADER_OFFSET_DATA).arrayBuffer(),
|
|
74
|
+
)
|
|
66
75
|
|
|
67
|
-
|
|
76
|
+
// Given the access handle pool always eagerly creates files with empty non-header data,
|
|
77
|
+
// we want to return undefined if the file exists but is empty
|
|
78
|
+
if (stateDbBuffer.byteLength === 0) {
|
|
79
|
+
return yield* new PersistedSqliteError({
|
|
80
|
+
message: `State database file is empty in client session (expected '${stateDbFileName}' in '${accessHandlePoolDirString}')`,
|
|
81
|
+
})
|
|
68
82
|
}
|
|
69
83
|
|
|
70
|
-
return
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
export const resetPersistedDataFromClientSession = ({
|
|
81
|
-
storageOptions,
|
|
82
|
-
storeId,
|
|
83
|
-
}: {
|
|
84
|
-
storageOptions: WorkerSchema.StorageType
|
|
85
|
-
storeId: string
|
|
86
|
-
}) =>
|
|
87
|
-
Effect.gen(function* () {
|
|
88
|
-
const directory = sanitizeOpfsDir(storageOptions.directory, storeId)
|
|
89
|
-
yield* opfsDeleteAbs(directory)
|
|
90
|
-
}).pipe(
|
|
91
|
-
Effect.retry({
|
|
92
|
-
schedule: Schedule.exponentialBackoff10Sec,
|
|
93
|
-
}),
|
|
94
|
-
Effect.withSpan('@livestore/adapter-web:resetPersistedDataFromClientSession'),
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
const opfsDeleteAbs = (absPath: string) =>
|
|
98
|
-
Effect.promise(async () => {
|
|
99
|
-
// Get the root directory handle
|
|
100
|
-
const root = await OpfsUtils.rootHandlePromise
|
|
101
|
-
|
|
102
|
-
// Split the absolute path to traverse directories
|
|
103
|
-
const pathParts = absPath.split('/').filter((part) => part.length)
|
|
104
|
-
|
|
105
|
-
try {
|
|
106
|
-
// Traverse to the target file handle
|
|
107
|
-
let currentDir = root
|
|
108
|
-
for (let i = 0; i < pathParts.length - 1; i++) {
|
|
109
|
-
currentDir = await currentDir.getDirectoryHandle(pathParts[i]!)
|
|
110
|
-
}
|
|
84
|
+
return new Uint8Array(stateDbBuffer)
|
|
85
|
+
},
|
|
86
|
+
Effect.logWarnIfTakesLongerThan({
|
|
87
|
+
duration: 1000,
|
|
88
|
+
label: '@livestore/adapter-web:readPersistedStateDbFromClientSession',
|
|
89
|
+
}),
|
|
90
|
+
Effect.withPerformanceMeasure('@livestore/adapter-web:readPersistedStateDbFromClientSession'),
|
|
91
|
+
)
|
|
111
92
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
93
|
+
export const resetPersistedDataFromClientSession = Effect.fn(
|
|
94
|
+
'@livestore/adapter-web:resetPersistedDataFromClientSession',
|
|
95
|
+
)(
|
|
96
|
+
function* ({ storageOptions, storeId }: { storageOptions: WorkerSchema.StorageType; storeId: string }) {
|
|
97
|
+
const directory = yield* sanitizeOpfsDir(storageOptions.directory, storeId)
|
|
98
|
+
yield* Opfs.remove(directory, { recursive: true }).pipe(
|
|
99
|
+
// We ignore NotFoundError here as it may not exist or have already been deleted
|
|
100
|
+
Effect.catchTag('NotFoundError', () => Effect.void),
|
|
101
|
+
)
|
|
102
|
+
},
|
|
103
|
+
Effect.retry({
|
|
104
|
+
schedule: Schedule.exponentialBackoff10Sec,
|
|
105
|
+
}),
|
|
106
|
+
)
|
|
126
107
|
|
|
127
|
-
export const sanitizeOpfsDir = (
|
|
128
|
-
|
|
129
|
-
|
|
108
|
+
export const sanitizeOpfsDir = Effect.fn('@livestore/adapter-web:sanitizeOpfsDir')(function* (
|
|
109
|
+
directory: string | undefined,
|
|
110
|
+
storeId: string,
|
|
111
|
+
) {
|
|
112
|
+
if (directory === undefined || directory === '' || directory === '/') {
|
|
130
113
|
return `livestore-${storeId}@${liveStoreStorageFormatVersion}`
|
|
114
|
+
}
|
|
131
115
|
|
|
132
|
-
if (directory.includes('/')) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
)
|
|
116
|
+
if (directory.includes('/') === true) {
|
|
117
|
+
return yield* new PersistedSqliteError({
|
|
118
|
+
message: `Nested directories are not yet supported ('${directory}')`,
|
|
119
|
+
})
|
|
136
120
|
}
|
|
137
121
|
|
|
138
122
|
return `${directory}@${liveStoreStorageFormatVersion}`
|
|
139
|
-
}
|
|
123
|
+
})
|
|
140
124
|
|
|
141
|
-
export const getStateDbFileName = (schema: LiveStoreSchema) => {
|
|
142
|
-
const schemaHashSuffix =
|
|
143
|
-
schema.state.sqlite.migrations.strategy === 'manual' ? 'fixed' : schema.state.sqlite.hash.toString()
|
|
144
|
-
return `state${schemaHashSuffix}.db`
|
|
145
|
-
}
|
|
125
|
+
export const getStateDbFileName = (schema: LiveStoreSchema) => `${getStateDbBaseName(schema)}.db`
|
|
146
126
|
|
|
147
127
|
export const MAX_ARCHIVED_STATE_DBS_IN_DEV = 3
|
|
128
|
+
export const ARCHIVE_DIR_NAME = 'archive'
|
|
148
129
|
|
|
149
130
|
/**
|
|
150
131
|
* Cleanup old state database files after successful migration.
|
|
@@ -153,210 +134,124 @@ export const MAX_ARCHIVED_STATE_DBS_IN_DEV = 3
|
|
|
153
134
|
* @param vfs - The AccessHandlePoolVFS instance for safe file operations
|
|
154
135
|
* @param currentSchema - Current schema (to avoid deleting the active database)
|
|
155
136
|
*/
|
|
156
|
-
export const cleanupOldStateDbFiles
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
137
|
+
export const cleanupOldStateDbFiles: (options: {
|
|
138
|
+
vfs: WebDatabaseMetadataOpfs['vfs']
|
|
139
|
+
currentSchema: LiveStoreSchema
|
|
140
|
+
opfsDirectory: string
|
|
141
|
+
}) => Effect.Effect<
|
|
142
|
+
void,
|
|
143
|
+
// All the following errors could actually happen:
|
|
144
|
+
| WebError.AbortError
|
|
145
|
+
| WebError.DataCloneError
|
|
146
|
+
| WebError.EvalError
|
|
147
|
+
| WebError.InvalidModificationError
|
|
148
|
+
| WebError.InvalidStateError
|
|
149
|
+
| WebError.NoModificationAllowedError
|
|
150
|
+
| WebError.NotAllowedError
|
|
151
|
+
| WebError.NotFoundError
|
|
152
|
+
| WebError.QuotaExceededError
|
|
153
|
+
| WebError.RangeError
|
|
154
|
+
| WebError.ReferenceError
|
|
155
|
+
| WebError.SecurityError
|
|
156
|
+
| WebError.TypeError
|
|
157
|
+
| WebError.TypeMismatchError
|
|
158
|
+
| WebError.URIError
|
|
159
|
+
| WebError.UnknownError
|
|
160
|
+
| Opfs.OpfsError
|
|
161
|
+
| PersistedSqliteError,
|
|
162
|
+
Opfs.Opfs
|
|
163
|
+
> = Effect.fn('@livestore/adapter-web:cleanupOldStateDbFiles')(function* ({ vfs, currentSchema, opfsDirectory }) {
|
|
164
|
+
const isDev = isDevEnv()
|
|
165
|
+
const currentDbFileName = getStateDbFileName(currentSchema)
|
|
166
|
+
const currentPath = `/${currentDbFileName}`
|
|
167
|
+
|
|
168
|
+
const allPaths = yield* Effect.sync(() => vfs.getTrackedFilePaths())
|
|
169
|
+
const oldStateDbPaths = allPaths.filter(
|
|
170
|
+
(path) => path.startsWith('/state') && path.endsWith('.db') && path !== currentPath,
|
|
171
|
+
)
|
|
191
172
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
173
|
+
if (oldStateDbPaths.length === 0) {
|
|
174
|
+
yield* Effect.logDebug('No old database files found')
|
|
175
|
+
return
|
|
176
|
+
}
|
|
195
177
|
|
|
196
|
-
|
|
197
|
-
|
|
178
|
+
const absoluteArchiveDirName = `${opfsDirectory}/${ARCHIVE_DIR_NAME}`
|
|
179
|
+
if (isDev === true && (yield* Opfs.exists(absoluteArchiveDirName)) === false)
|
|
180
|
+
yield* Opfs.makeDirectory(absoluteArchiveDirName)
|
|
198
181
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
try: () => OpfsUtils.getDirHandle(`${opfsDirectory}/archive`, { create: true }),
|
|
202
|
-
catch: (cause) => new ArchiveStateDbError({ message: 'Failed to ensure archive directory', cause }),
|
|
203
|
-
})
|
|
182
|
+
for (const path of oldStateDbPaths) {
|
|
183
|
+
const fileName = path.startsWith('/') === true ? path.slice(1) : path
|
|
204
184
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
fileName,
|
|
208
|
-
archiveDirHandle,
|
|
209
|
-
})
|
|
185
|
+
if (isDev === true) {
|
|
186
|
+
const archiveFileData = yield* vfs.readFilePayload(fileName)
|
|
210
187
|
|
|
211
|
-
|
|
212
|
-
}
|
|
188
|
+
const archiveFileName = `${Date.now()}-${fileName}`
|
|
189
|
+
const archivePath = `${opfsDirectory}/archive/${archiveFileName}`
|
|
190
|
+
const archiveData = new Uint8Array(archiveFileData)
|
|
213
191
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
192
|
+
// Prefer writeFile (atomic) when createWritable is available (Chrome, Firefox, Safari 26+),
|
|
193
|
+
// fall back to syncWriteFile (non-atomic) for Safari 18.x compatibility.
|
|
194
|
+
// TODO: Remove feature detection and use writeFile directly when Safari >= 26 is widely available.
|
|
195
|
+
const supportsCreateWritable =
|
|
196
|
+
typeof FileSystemFileHandle !== 'undefined' && 'createWritable' in FileSystemFileHandle.prototype
|
|
218
197
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
operation: 'jDelete',
|
|
224
|
-
fileName,
|
|
225
|
-
vfsResultCode,
|
|
226
|
-
})
|
|
198
|
+
if (supportsCreateWritable === true) {
|
|
199
|
+
yield* Opfs.writeFile(archivePath, archiveData)
|
|
200
|
+
} else {
|
|
201
|
+
yield* Opfs.syncWriteFile(archivePath, archiveData)
|
|
227
202
|
}
|
|
228
|
-
|
|
229
|
-
deletedCount++
|
|
230
|
-
yield* Effect.logDebug(`Successfully deleted old state database file: ${fileName}`)
|
|
231
203
|
}
|
|
232
204
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
205
|
+
const vfsResultCode = yield* Effect.try({
|
|
206
|
+
try: () => vfs.jDelete(fileName, 0),
|
|
207
|
+
catch: (cause) =>
|
|
208
|
+
new PersistedSqliteError({ message: `Failed to delete old state database file: ${fileName}`, cause }),
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
// 0 indicates a successful result in SQLite.
|
|
212
|
+
// See https://www.sqlite.org/c3ref/c_abort.html
|
|
213
|
+
if (vfsResultCode !== 0) {
|
|
214
|
+
return yield* new PersistedSqliteError({
|
|
215
|
+
message: `Failed to delete old state database file: ${fileName}, got result code: ${vfsResultCode}`,
|
|
237
216
|
})
|
|
238
|
-
|
|
239
|
-
yield* Effect.logDebug(
|
|
240
|
-
`State db cleanup completed: archived ${archivedFileNames.length} file(s); removed ${deletedCount} old database file(s) from active pool; archive retained ${pruneResult.retained.length} file(s)`,
|
|
241
|
-
)
|
|
242
|
-
} else {
|
|
243
|
-
yield* Effect.logDebug(`State db cleanup completed: removed ${deletedCount} old database file(s)`)
|
|
244
217
|
}
|
|
245
|
-
},
|
|
246
|
-
Effect.mapError(
|
|
247
|
-
(error) =>
|
|
248
|
-
new PersistedSqliteError({
|
|
249
|
-
message: 'Failed to clean up old state database file(s)',
|
|
250
|
-
cause: error,
|
|
251
|
-
}),
|
|
252
|
-
),
|
|
253
|
-
)
|
|
254
218
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
fileName,
|
|
258
|
-
archiveDirHandle,
|
|
259
|
-
}: {
|
|
260
|
-
vfs: WebDatabaseMetadataOpfs['vfs']
|
|
261
|
-
fileName: string
|
|
262
|
-
archiveDirHandle: FileSystemDirectoryHandle
|
|
263
|
-
}) {
|
|
264
|
-
const stateDbBuffer = vfs.readFilePayload(fileName)
|
|
265
|
-
|
|
266
|
-
const archiveFileName = `${Date.now()}-${fileName}`
|
|
267
|
-
|
|
268
|
-
const archiveFileHandle = yield* Effect.tryPromise({
|
|
269
|
-
try: () => archiveDirHandle.getFileHandle(archiveFileName, { create: true }),
|
|
270
|
-
catch: (cause) =>
|
|
271
|
-
new ArchiveStateDbError({
|
|
272
|
-
message: 'Failed to open archive file handle',
|
|
273
|
-
fileName: archiveFileName,
|
|
274
|
-
cause,
|
|
275
|
-
}),
|
|
276
|
-
})
|
|
277
|
-
|
|
278
|
-
const archiveFileAccessHandle = yield* Effect.acquireRelease(
|
|
279
|
-
Effect.tryPromise({
|
|
280
|
-
try: () => archiveFileHandle.createSyncAccessHandle(),
|
|
281
|
-
catch: (cause) =>
|
|
282
|
-
new ArchiveStateDbError({
|
|
283
|
-
message: 'Failed to create sync access handle for archived file',
|
|
284
|
-
fileName: archiveFileName,
|
|
285
|
-
cause,
|
|
286
|
-
}),
|
|
287
|
-
}),
|
|
288
|
-
(handle) => Effect.sync(() => handle.close()).pipe(Effect.ignoreLogged),
|
|
289
|
-
)
|
|
219
|
+
yield* Effect.logDebug(`Deleted old state database file: ${fileName}`)
|
|
220
|
+
}
|
|
290
221
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}),
|
|
302
|
-
})
|
|
303
|
-
|
|
304
|
-
return archiveFileName
|
|
305
|
-
}, Effect.scoped)
|
|
306
|
-
|
|
307
|
-
const pruneArchiveDir = Effect.fn('@livestore/adapter-web:pruneArchiveDir')(function* ({
|
|
308
|
-
archiveDirHandle,
|
|
222
|
+
if (isDev === true) {
|
|
223
|
+
yield* pruneArchiveDirectory({
|
|
224
|
+
archiveDirectory: absoluteArchiveDirName,
|
|
225
|
+
keep: MAX_ARCHIVED_STATE_DBS_IN_DEV,
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
const pruneArchiveDirectory = Effect.fn('@livestore/adapter-web:pruneArchiveDirectory')(function* ({
|
|
231
|
+
archiveDirectory,
|
|
309
232
|
keep,
|
|
310
233
|
}: {
|
|
311
|
-
|
|
234
|
+
archiveDirectory: string
|
|
312
235
|
keep: number
|
|
313
236
|
}) {
|
|
314
|
-
const
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
catch: (cause) => new ArchiveStateDbError({ message: 'Failed to enumerate archived state databases', cause }),
|
|
328
|
-
})
|
|
329
|
-
|
|
330
|
-
const retained = files.slice(0, keep)
|
|
331
|
-
const toDelete = files.slice(keep)
|
|
332
|
-
|
|
333
|
-
yield* Effect.forEach(toDelete, ({ name }) =>
|
|
334
|
-
Effect.tryPromise({
|
|
335
|
-
try: () => archiveDirHandle.removeEntry(name),
|
|
336
|
-
catch: (cause) =>
|
|
337
|
-
new ArchiveStateDbError({
|
|
338
|
-
message: 'Failed to delete archived state database',
|
|
339
|
-
fileName: name,
|
|
340
|
-
cause,
|
|
341
|
-
}),
|
|
342
|
-
}),
|
|
237
|
+
const archiveDirHandle = yield* Opfs.getDirectoryHandleByPath(archiveDirectory)
|
|
238
|
+
const opfs = yield* Opfs.Opfs
|
|
239
|
+
const handlesStream = opfs.values(archiveDirHandle)
|
|
240
|
+
const filesWithMetadata = yield* handlesStream.pipe(
|
|
241
|
+
Stream.filter((handle): handle is FileSystemFileHandle => handle.kind === 'file'),
|
|
242
|
+
Stream.mapEffect((fileHandle) => Opfs.getMetadata(fileHandle)),
|
|
243
|
+
Stream.runCollect,
|
|
244
|
+
)
|
|
245
|
+
const filesToDelete = pipe(
|
|
246
|
+
filesWithMetadata,
|
|
247
|
+
// oxlint-disable-next-line eslint-plugin-unicorn(no-array-sort) -- EffectArray has no toSorted helper in the current Effect version; sort returns a sorted copy.
|
|
248
|
+
EffectArray.sort(Order.mapInput(Order.Number, (entry: { lastModified: number }) => entry.lastModified)),
|
|
249
|
+
EffectArray.drop(keep),
|
|
343
250
|
)
|
|
344
251
|
|
|
345
|
-
return
|
|
346
|
-
retained,
|
|
347
|
-
deleted: toDelete,
|
|
348
|
-
}
|
|
349
|
-
})
|
|
252
|
+
if (filesToDelete.length === 0) return
|
|
350
253
|
|
|
351
|
-
|
|
352
|
-
message: Schema.String,
|
|
353
|
-
fileName: Schema.optional(Schema.String),
|
|
354
|
-
cause: Schema.Defect,
|
|
355
|
-
}) {}
|
|
254
|
+
yield* Effect.forEach(filesToDelete, ({ name }) => opfs.removeEntry(archiveDirHandle, name))
|
|
356
255
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
fileName: Schema.String,
|
|
360
|
-
vfsResultCode: Schema.optional(Schema.Number),
|
|
361
|
-
cause: Schema.optional(Schema.Defect),
|
|
362
|
-
}) {}
|
|
256
|
+
yield* Effect.logDebug(`Pruned ${filesToDelete.length} old database file(s) from archive directory`)
|
|
257
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Effect, RpcClientError, Schema, Stream } from '@livestore/utils/effect'
|
|
2
|
+
import type * as WebmeshWorker from '@livestore/webmesh/worker'
|
|
3
|
+
|
|
4
|
+
export type WebmeshWorkerProxy = Parameters<typeof WebmeshWorker.connectViaWorker>[0]['worker']
|
|
5
|
+
export type WithoutRpcClientError<E> = E extends { readonly _tag: infer Tag }
|
|
6
|
+
? 'RpcClientError' extends Tag
|
|
7
|
+
? never
|
|
8
|
+
: E
|
|
9
|
+
: E
|
|
10
|
+
export type WebmeshWorkerRpcClient = {
|
|
11
|
+
readonly ['WebmeshWorker.CreateConnection']: (
|
|
12
|
+
request: typeof WebmeshWorker.Schema.CreateConnection.Type,
|
|
13
|
+
) => Stream.Stream<{}, RpcClientError.RpcClientError>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const isRpcClientError = (error: unknown): error is RpcClientError.RpcClientError =>
|
|
17
|
+
Schema.is(RpcClientError.RpcClientError)(error)
|
|
18
|
+
|
|
19
|
+
export const dieOnRpcClientError = <A, E, R>(
|
|
20
|
+
effect: Effect.Effect<A, E, R>,
|
|
21
|
+
): Effect.Effect<A, WithoutRpcClientError<E>, R> =>
|
|
22
|
+
(effect as Effect.Effect<A, RpcClientError.RpcClientError | WithoutRpcClientError<E>, R>).pipe(
|
|
23
|
+
Effect.catchIf(isRpcClientError, (error) => Effect.die(error)),
|
|
24
|
+
) as Effect.Effect<A, WithoutRpcClientError<E>, R>
|
|
25
|
+
|
|
26
|
+
export const dieOnRpcClientErrorStream = <A, E, R>(
|
|
27
|
+
stream: Stream.Stream<A, E, R>,
|
|
28
|
+
): Stream.Stream<A, WithoutRpcClientError<E>, R> =>
|
|
29
|
+
(stream as Stream.Stream<A, RpcClientError.RpcClientError | WithoutRpcClientError<E>, R>).pipe(
|
|
30
|
+
Stream.catchIf(
|
|
31
|
+
isRpcClientError,
|
|
32
|
+
(error) => Stream.die(error),
|
|
33
|
+
(error) => Stream.fail(error),
|
|
34
|
+
),
|
|
35
|
+
) as Stream.Stream<A, WithoutRpcClientError<E>, R>
|
|
36
|
+
|
|
37
|
+
export const makeWebmeshWorkerProxy = (client: WebmeshWorkerRpcClient): WebmeshWorkerProxy => ({
|
|
38
|
+
execute: (request) => dieOnRpcClientErrorStream(client['WebmeshWorker.CreateConnection'](request)),
|
|
39
|
+
})
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { ShutdownChannel } from '@livestore/common/leader-thread'
|
|
2
|
-
import { WebChannel } from '@livestore/utils/effect'
|
|
2
|
+
import type { Effect, Scope, WebChannel } from '@livestore/utils/effect'
|
|
3
|
+
import { WebChannelBrowser } from '@livestore/utils/effect/browser'
|
|
3
4
|
|
|
4
|
-
export const makeShutdownChannel = (
|
|
5
|
-
|
|
5
|
+
export const makeShutdownChannel = (
|
|
6
|
+
storeId: string,
|
|
7
|
+
): Effect.Effect<
|
|
8
|
+
WebChannel.WebChannel<typeof ShutdownChannel.All.Type, typeof ShutdownChannel.All.Type>,
|
|
9
|
+
never,
|
|
10
|
+
Scope.Scope
|
|
11
|
+
> =>
|
|
12
|
+
WebChannelBrowser.broadcastChannel({
|
|
6
13
|
channelName: `livestore.shutdown.${storeId}`,
|
|
7
14
|
schema: ShutdownChannel.All,
|
|
8
15
|
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const makeSharedWorkerNodeName = ({ storeId }: { storeId: string }) => `shared-worker-${storeId}`
|