@bendyline/docblocks 2.0.0 → 2.1.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/dist/{chunk-J32GU3F3.js → chunk-6IYMTYMQ.js} +249 -221
- package/dist/chunk-6IYMTYMQ.js.map +1 -0
- package/dist/{chunk-75UUF6C4.js → chunk-B56KWCSE.js} +2 -2
- package/dist/{chunk-RVQHY3XU.js → chunk-BNKV7MTO.js} +1 -1
- package/dist/{chunk-RVQHY3XU.js.map → chunk-BNKV7MTO.js.map} +1 -1
- package/dist/chunk-BXMF5YQJ.js +149 -0
- package/dist/chunk-BXMF5YQJ.js.map +1 -0
- package/dist/{chunk-PGR7LK6D.js → chunk-COYQXQK3.js} +14 -5
- package/dist/chunk-COYQXQK3.js.map +1 -0
- package/dist/{chunk-RQJRQF46.js → chunk-DN7A33LL.js} +42 -49
- package/dist/chunk-DN7A33LL.js.map +1 -0
- package/dist/chunk-FA7UNOXK.js +20 -0
- package/dist/chunk-FA7UNOXK.js.map +1 -0
- package/dist/chunk-GM5YXU4L.js +29 -0
- package/dist/chunk-GM5YXU4L.js.map +1 -0
- package/dist/{chunk-SI2LU2RP.js → chunk-JS6XFWQK.js} +2 -1
- package/dist/chunk-JS6XFWQK.js.map +1 -0
- package/dist/{chunk-EGNDADXB.js → chunk-MNSZ7EZN.js} +2 -2
- package/dist/chunk-P3DVZQ6S.js +228 -0
- package/dist/chunk-P3DVZQ6S.js.map +1 -0
- package/dist/chunk-P4WATND4.js +52 -0
- package/dist/chunk-P4WATND4.js.map +1 -0
- package/dist/chunk-RVPGQBWR.js +169 -0
- package/dist/chunk-RVPGQBWR.js.map +1 -0
- package/dist/chunk-U722UUFN.js +9 -0
- package/dist/chunk-U722UUFN.js.map +1 -0
- package/dist/{chunk-GCPBEMMH.js → chunk-VTCSF7VS.js} +11 -21
- package/dist/chunk-VTCSF7VS.js.map +1 -0
- package/dist/{chunk-2D7Y4IR3.js → chunk-WJKYY5QR.js} +123 -66
- package/dist/chunk-WJKYY5QR.js.map +1 -0
- package/dist/{chunk-5MYAICRE.js → chunk-XUML3MKV.js} +44 -56
- package/dist/chunk-XUML3MKV.js.map +1 -0
- package/dist/document/index.d.ts +358 -6
- package/dist/document/index.js +24 -3
- package/dist/document/index.js.map +1 -1
- package/dist/filesystem/electron.d.ts +73 -4
- package/dist/filesystem/electron.js +16 -3
- package/dist/filesystem/electron.js.map +1 -1
- package/dist/filesystem/index.d.ts +228 -22
- package/dist/filesystem/index.js +133 -19
- package/dist/filesystem/index.js.map +1 -1
- package/dist/filesystem/indexeddb.d.ts +284 -5
- package/dist/filesystem/indexeddb.js +22 -4
- package/dist/filesystem/indexeddb.js.map +1 -1
- package/dist/filesystem/memory.d.ts +152 -4
- package/dist/filesystem/memory.js +12 -3
- package/dist/filesystem/memory.js.map +1 -1
- package/dist/filesystem/native.d.ts +136 -4
- package/dist/filesystem/native.js +27 -3
- package/dist/filesystem/native.js.map +1 -1
- package/dist/host/index.d.ts +32 -9
- package/dist/host/index.js +26 -29
- package/dist/host/index.js.map +1 -1
- package/dist/index.d.ts +14 -10
- package/dist/index.js +204 -9
- package/dist/index.js.map +1 -1
- package/dist/mcp/index.d.ts +33 -4
- package/dist/mcp/index.js +31 -3
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/zod.d.ts +637 -635
- package/dist/mcp/zod.js +700 -884
- package/dist/mcp/zod.js.map +1 -1
- package/dist/share/index.d.ts +43 -0
- package/dist/share/index.js +13 -0
- package/dist/share/index.js.map +1 -0
- package/dist/{types-CsX6EN2t.d.ts → types-BEg5SQ1_.d.ts} +1 -1
- package/dist/{types-eOlt9J7n.d.ts → types-CueK0tvU.d.ts} +34 -11
- package/dist/vscode/index.d.ts +220 -3
- package/dist/vscode/index.js +382 -2
- package/dist/vscode/index.js.map +1 -1
- package/dist/workspace/index.d.ts +141 -4
- package/dist/workspace/index.js +28 -2
- package/dist/workspace/index.js.map +1 -1
- package/package.json +9 -5
- package/dist/chunk-2D7Y4IR3.js.map +0 -1
- package/dist/chunk-5MYAICRE.js.map +0 -1
- package/dist/chunk-AYZ4HN5M.js +0 -20
- package/dist/chunk-AYZ4HN5M.js.map +0 -1
- package/dist/chunk-GCPBEMMH.js.map +0 -1
- package/dist/chunk-J32GU3F3.js.map +0 -1
- package/dist/chunk-PGR7LK6D.js.map +0 -1
- package/dist/chunk-RQJRQF46.js.map +0 -1
- package/dist/chunk-SI2LU2RP.js.map +0 -1
- package/dist/chunk-W5UUUII5.js +0 -142
- package/dist/chunk-W5UUUII5.js.map +0 -1
- package/dist/document/document-session.d.ts +0 -127
- package/dist/document/document-session.d.ts.map +0 -1
- package/dist/document/document-session.js +0 -749
- package/dist/document/document-session.js.map +0 -1
- package/dist/document/filesystem-target.d.ts +0 -17
- package/dist/document/filesystem-target.d.ts.map +0 -1
- package/dist/document/filesystem-target.js +0 -70
- package/dist/document/filesystem-target.js.map +0 -1
- package/dist/document/index.d.ts.map +0 -1
- package/dist/document/recovery-journal.d.ts +0 -125
- package/dist/document/recovery-journal.d.ts.map +0 -1
- package/dist/document/recovery-journal.js +0 -385
- package/dist/document/recovery-journal.js.map +0 -1
- package/dist/document/types.d.ts +0 -89
- package/dist/document/types.d.ts.map +0 -1
- package/dist/document/types.js +0 -9
- package/dist/document/types.js.map +0 -1
- package/dist/filesystem/commit-lock.d.ts +0 -3
- package/dist/filesystem/commit-lock.d.ts.map +0 -1
- package/dist/filesystem/commit-lock.js +0 -20
- package/dist/filesystem/commit-lock.js.map +0 -1
- package/dist/filesystem/dbk-workspace.d.ts +0 -34
- package/dist/filesystem/dbk-workspace.d.ts.map +0 -1
- package/dist/filesystem/dbk-workspace.js +0 -85
- package/dist/filesystem/dbk-workspace.js.map +0 -1
- package/dist/filesystem/electron-provider-v2.d.ts +0 -36
- package/dist/filesystem/electron-provider-v2.d.ts.map +0 -1
- package/dist/filesystem/electron-provider-v2.js +0 -503
- package/dist/filesystem/electron-provider-v2.js.map +0 -1
- package/dist/filesystem/electron-provider.d.ts +0 -35
- package/dist/filesystem/electron-provider.d.ts.map +0 -1
- package/dist/filesystem/electron-provider.js +0 -69
- package/dist/filesystem/electron-provider.js.map +0 -1
- package/dist/filesystem/electron.d.ts.map +0 -1
- package/dist/filesystem/file-media-provider.d.ts +0 -23
- package/dist/filesystem/file-media-provider.d.ts.map +0 -1
- package/dist/filesystem/file-media-provider.js +0 -85
- package/dist/filesystem/file-media-provider.js.map +0 -1
- package/dist/filesystem/filesystem-content-container.d.ts +0 -24
- package/dist/filesystem/filesystem-content-container.d.ts.map +0 -1
- package/dist/filesystem/filesystem-content-container.js +0 -153
- package/dist/filesystem/filesystem-content-container.js.map +0 -1
- package/dist/filesystem/fs-error.d.ts +0 -46
- package/dist/filesystem/fs-error.d.ts.map +0 -1
- package/dist/filesystem/fs-error.js +0 -170
- package/dist/filesystem/fs-error.js.map +0 -1
- package/dist/filesystem/index.d.ts.map +0 -1
- package/dist/filesystem/indexeddb-content-container.d.ts +0 -22
- package/dist/filesystem/indexeddb-content-container.d.ts.map +0 -1
- package/dist/filesystem/indexeddb-content-container.js +0 -100
- package/dist/filesystem/indexeddb-content-container.js.map +0 -1
- package/dist/filesystem/indexeddb-provider-v2.d.ts +0 -96
- package/dist/filesystem/indexeddb-provider-v2.d.ts.map +0 -1
- package/dist/filesystem/indexeddb-provider-v2.js +0 -1241
- package/dist/filesystem/indexeddb-provider-v2.js.map +0 -1
- package/dist/filesystem/indexeddb-provider.d.ts +0 -39
- package/dist/filesystem/indexeddb-provider.d.ts.map +0 -1
- package/dist/filesystem/indexeddb-provider.js +0 -145
- package/dist/filesystem/indexeddb-provider.js.map +0 -1
- package/dist/filesystem/indexeddb-store.d.ts +0 -34
- package/dist/filesystem/indexeddb-store.d.ts.map +0 -1
- package/dist/filesystem/indexeddb-store.js +0 -165
- package/dist/filesystem/indexeddb-store.js.map +0 -1
- package/dist/filesystem/indexeddb.d.ts.map +0 -1
- package/dist/filesystem/legacy-v2-adapter.d.ts +0 -62
- package/dist/filesystem/legacy-v2-adapter.d.ts.map +0 -1
- package/dist/filesystem/legacy-v2-adapter.js +0 -499
- package/dist/filesystem/legacy-v2-adapter.js.map +0 -1
- package/dist/filesystem/memory-provider-v2.d.ts +0 -92
- package/dist/filesystem/memory-provider-v2.d.ts.map +0 -1
- package/dist/filesystem/memory-provider-v2.js +0 -615
- package/dist/filesystem/memory-provider-v2.js.map +0 -1
- package/dist/filesystem/memory-provider.d.ts +0 -54
- package/dist/filesystem/memory-provider.d.ts.map +0 -1
- package/dist/filesystem/memory-provider.js +0 -132
- package/dist/filesystem/memory-provider.js.map +0 -1
- package/dist/filesystem/memory.d.ts.map +0 -1
- package/dist/filesystem/move-entry.d.ts +0 -9
- package/dist/filesystem/move-entry.d.ts.map +0 -1
- package/dist/filesystem/move-entry.js +0 -103
- package/dist/filesystem/move-entry.js.map +0 -1
- package/dist/filesystem/move-error.d.ts +0 -53
- package/dist/filesystem/move-error.d.ts.map +0 -1
- package/dist/filesystem/move-error.js +0 -99
- package/dist/filesystem/move-error.js.map +0 -1
- package/dist/filesystem/native-provider-v2.d.ts +0 -75
- package/dist/filesystem/native-provider-v2.d.ts.map +0 -1
- package/dist/filesystem/native-provider-v2.js +0 -849
- package/dist/filesystem/native-provider-v2.js.map +0 -1
- package/dist/filesystem/native-provider.d.ts +0 -53
- package/dist/filesystem/native-provider.d.ts.map +0 -1
- package/dist/filesystem/native-provider.js +0 -471
- package/dist/filesystem/native-provider.js.map +0 -1
- package/dist/filesystem/native.d.ts.map +0 -1
- package/dist/filesystem/types.d.ts +0 -83
- package/dist/filesystem/types.d.ts.map +0 -1
- package/dist/filesystem/types.js +0 -16
- package/dist/filesystem/types.js.map +0 -1
- package/dist/filesystem/v2.d.ts +0 -172
- package/dist/filesystem/v2.d.ts.map +0 -1
- package/dist/filesystem/v2.js +0 -16
- package/dist/filesystem/v2.js.map +0 -1
- package/dist/filesystem/workspace-path.d.ts +0 -21
- package/dist/filesystem/workspace-path.d.ts.map +0 -1
- package/dist/filesystem/workspace-path.js +0 -80
- package/dist/filesystem/workspace-path.js.map +0 -1
- package/dist/host/filesystem-v2.d.ts +0 -45
- package/dist/host/filesystem-v2.d.ts.map +0 -1
- package/dist/host/filesystem-v2.js +0 -13
- package/dist/host/filesystem-v2.js.map +0 -1
- package/dist/host/git.d.ts +0 -200
- package/dist/host/git.d.ts.map +0 -1
- package/dist/host/git.js +0 -15
- package/dist/host/git.js.map +0 -1
- package/dist/host/index.d.ts.map +0 -1
- package/dist/host/types.d.ts +0 -232
- package/dist/host/types.d.ts.map +0 -1
- package/dist/host/types.js +0 -10
- package/dist/host/types.js.map +0 -1
- package/dist/host/wire-policy.d.ts +0 -22
- package/dist/host/wire-policy.d.ts.map +0 -1
- package/dist/host/wire-policy.js +0 -92
- package/dist/host/wire-policy.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/mcp/index.d.ts.map +0 -1
- package/dist/mcp/types.d.ts +0 -466
- package/dist/mcp/types.d.ts.map +0 -1
- package/dist/mcp/types.js +0 -25
- package/dist/mcp/types.js.map +0 -1
- package/dist/mcp/wire.d.ts +0 -29
- package/dist/mcp/wire.d.ts.map +0 -1
- package/dist/mcp/wire.js +0 -1046
- package/dist/mcp/wire.js.map +0 -1
- package/dist/mcp/zod.d.ts.map +0 -1
- package/dist/vscode/index.d.ts.map +0 -1
- package/dist/vscode/messages.d.ts +0 -204
- package/dist/vscode/messages.d.ts.map +0 -1
- package/dist/vscode/messages.js +0 -494
- package/dist/vscode/messages.js.map +0 -1
- package/dist/workspace/index.d.ts.map +0 -1
- package/dist/workspace/reconcile-electron.d.ts +0 -19
- package/dist/workspace/reconcile-electron.d.ts.map +0 -1
- package/dist/workspace/reconcile-electron.js +0 -42
- package/dist/workspace/reconcile-electron.js.map +0 -1
- package/dist/workspace/types.d.ts +0 -69
- package/dist/workspace/types.d.ts.map +0 -1
- package/dist/workspace/types.js +0 -2
- package/dist/workspace/types.js.map +0 -1
- package/dist/workspace/workspace-manager.d.ts +0 -47
- package/dist/workspace/workspace-manager.d.ts.map +0 -1
- package/dist/workspace/workspace-manager.js +0 -126
- package/dist/workspace/workspace-manager.js.map +0 -1
- package/src/document/document-session.ts +0 -834
- package/src/document/filesystem-target.ts +0 -109
- package/src/document/index.ts +0 -41
- package/src/document/recovery-journal.ts +0 -536
- package/src/document/types.ts +0 -116
- package/src/filesystem/commit-lock.ts +0 -29
- package/src/filesystem/dbk-workspace.ts +0 -135
- package/src/filesystem/electron-provider-v2.ts +0 -630
- package/src/filesystem/electron-provider.ts +0 -99
- package/src/filesystem/electron.ts +0 -3
- package/src/filesystem/file-media-provider.ts +0 -103
- package/src/filesystem/filesystem-content-container.ts +0 -164
- package/src/filesystem/fs-error.ts +0 -234
- package/src/filesystem/index.ts +0 -143
- package/src/filesystem/indexeddb-content-container.ts +0 -112
- package/src/filesystem/indexeddb-provider-v2.ts +0 -1740
- package/src/filesystem/indexeddb-provider.ts +0 -171
- package/src/filesystem/indexeddb-store.ts +0 -207
- package/src/filesystem/indexeddb.ts +0 -12
- package/src/filesystem/legacy-v2-adapter.ts +0 -779
- package/src/filesystem/memory-provider-v2.ts +0 -896
- package/src/filesystem/memory-provider.ts +0 -180
- package/src/filesystem/memory.ts +0 -15
- package/src/filesystem/move-entry.ts +0 -142
- package/src/filesystem/move-error.ts +0 -159
- package/src/filesystem/native-provider-v2.ts +0 -1165
- package/src/filesystem/native-provider.ts +0 -537
- package/src/filesystem/native.ts +0 -16
- package/src/filesystem/types.ts +0 -114
- package/src/filesystem/v2.ts +0 -231
- package/src/filesystem/workspace-path.ts +0 -92
- package/src/host/filesystem-v2.ts +0 -104
- package/src/host/git.ts +0 -246
- package/src/host/index.ts +0 -93
- package/src/host/types.ts +0 -274
- package/src/host/wire-policy.ts +0 -103
- package/src/index.ts +0 -10
- package/src/mcp/index.ts +0 -3
- package/src/mcp/types.ts +0 -594
- package/src/mcp/wire.ts +0 -1280
- package/src/mcp/zod.ts +0 -1000
- package/src/vscode/index.ts +0 -2
- package/src/vscode/messages.ts +0 -704
- package/src/workspace/index.ts +0 -17
- package/src/workspace/reconcile-electron.ts +0 -65
- package/src/workspace/types.ts +0 -67
- package/src/workspace/workspace-manager.ts +0 -151
- /package/dist/{chunk-75UUF6C4.js.map → chunk-B56KWCSE.js.map} +0 -0
- /package/dist/{chunk-EGNDADXB.js.map → chunk-MNSZ7EZN.js.map} +0 -0
|
@@ -1,1740 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FsError,
|
|
3
|
-
fsErrorFromUnknown,
|
|
4
|
-
type FsErrorCode,
|
|
5
|
-
type FsErrorContext,
|
|
6
|
-
type FsOperation,
|
|
7
|
-
} from './fs-error.js';
|
|
8
|
-
import { withFileCommitLock } from './commit-lock.js';
|
|
9
|
-
import {
|
|
10
|
-
RawIndexedDBFileSystemStore,
|
|
11
|
-
type IndexedDBFileSystemStore,
|
|
12
|
-
type IndexedDBFileSystemTransaction,
|
|
13
|
-
type IndexedDBFileSystemTransactionMode,
|
|
14
|
-
} from './indexeddb-store.js';
|
|
15
|
-
import {
|
|
16
|
-
parseFileSystemVersion,
|
|
17
|
-
type FileSystemCreateDirectoryOptions,
|
|
18
|
-
type FileSystemDirectorySnapshot,
|
|
19
|
-
type FileSystemEntrySnapshot,
|
|
20
|
-
type FileSystemFileRead,
|
|
21
|
-
type FileSystemFileSnapshot,
|
|
22
|
-
type FileSystemMoveOptions,
|
|
23
|
-
type FileSystemProviderCapabilities,
|
|
24
|
-
type FileSystemProviderV2,
|
|
25
|
-
type FileSystemRemoveOptions,
|
|
26
|
-
type FileSystemRemoveResult,
|
|
27
|
-
type FileSystemSnapshot,
|
|
28
|
-
type FileSystemSnapshotEntry,
|
|
29
|
-
type FileSystemVersion,
|
|
30
|
-
type FileSystemWatchListener,
|
|
31
|
-
type FileSystemWatchOptions,
|
|
32
|
-
type FileSystemWatchSubscription,
|
|
33
|
-
type FileSystemWriteOptions,
|
|
34
|
-
} from './v2.js';
|
|
35
|
-
import {
|
|
36
|
-
WORKSPACE_ROOT,
|
|
37
|
-
parseWorkspacePath,
|
|
38
|
-
workspacePathBasename,
|
|
39
|
-
workspacePathContains,
|
|
40
|
-
workspacePathDirname,
|
|
41
|
-
type WorkspacePath,
|
|
42
|
-
} from './workspace-path.js';
|
|
43
|
-
import type { FileMeta } from './types.js';
|
|
44
|
-
|
|
45
|
-
const ENTRY_SCHEMA_VERSION = 1;
|
|
46
|
-
const STATE_SCHEMA_VERSION = 1;
|
|
47
|
-
const LEGACY_CONFLICT_SCHEMA_VERSION = 2;
|
|
48
|
-
const LEGACY_CONFLICT_SCHEMA_VERSION_V1 = 1;
|
|
49
|
-
const ENTRY_PREFIX = 'v2:entry:';
|
|
50
|
-
const STATE_KEY = 'v2:state';
|
|
51
|
-
const LEGACY_CONFLICT_PREFIX = 'v2:legacy-conflict:';
|
|
52
|
-
const LEGACY_IGNORED_PREFIX = 'v2:legacy-ignored:';
|
|
53
|
-
const LEGACY_OPAQUE_PREFIX = 'v2:legacy-opaque:';
|
|
54
|
-
const LEGACY_DIRS_KEY = 'fs:dirs';
|
|
55
|
-
const LEGACY_PREFIX = 'fs:';
|
|
56
|
-
const LEGACY_CONTENT_SUFFIX = ':content';
|
|
57
|
-
const LEGACY_BINARY_SUFFIX = ':binary';
|
|
58
|
-
const LEGACY_META_SUFFIX = ':meta';
|
|
59
|
-
|
|
60
|
-
class LegacyNamespaceDetectedError extends Error {}
|
|
61
|
-
|
|
62
|
-
const INDEXEDDB_V2_CAPABILITIES: FileSystemProviderCapabilities = Object.freeze({
|
|
63
|
-
writeAtomicity: 'cross-context',
|
|
64
|
-
moveAtomicity: 'cross-context',
|
|
65
|
-
snapshotAtomicity: 'cross-context',
|
|
66
|
-
conditionalWrite: 'storage-atomic',
|
|
67
|
-
recursiveRemove: true,
|
|
68
|
-
watch: false,
|
|
69
|
-
caseSensitivity: 'sensitive',
|
|
70
|
-
symlinkPolicy: 'unsupported',
|
|
71
|
-
durability: 'best-effort',
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
interface PersistedEntryBase {
|
|
75
|
-
readonly schemaVersion: typeof ENTRY_SCHEMA_VERSION;
|
|
76
|
-
readonly path: WorkspacePath;
|
|
77
|
-
readonly version: string;
|
|
78
|
-
readonly lastModified: string;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
interface PersistedFileEntry extends PersistedEntryBase {
|
|
82
|
-
readonly kind: 'file';
|
|
83
|
-
readonly data: ArrayBuffer;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
interface PersistedDirectoryEntry extends PersistedEntryBase {
|
|
87
|
-
readonly kind: 'directory';
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
type PersistedEntry = PersistedFileEntry | PersistedDirectoryEntry;
|
|
91
|
-
|
|
92
|
-
interface PersistedState {
|
|
93
|
-
readonly schemaVersion: typeof STATE_SCHEMA_VERSION;
|
|
94
|
-
readonly migrationVersion: 1 | 2;
|
|
95
|
-
readonly mutationCounter: number;
|
|
96
|
-
readonly treeVersion: string;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export type IndexedDBLegacyConflictReason =
|
|
100
|
-
| 'dual-payload'
|
|
101
|
-
| 'kind-collision'
|
|
102
|
-
| 'authority-collision'
|
|
103
|
-
| 'stale-client-write';
|
|
104
|
-
|
|
105
|
-
export type IndexedDBLegacyRecoveryKind = 'file' | 'directory';
|
|
106
|
-
|
|
107
|
-
interface PersistedLegacyRecoveryCandidate {
|
|
108
|
-
readonly kind: IndexedDBLegacyRecoveryKind;
|
|
109
|
-
readonly reason: IndexedDBLegacyConflictReason;
|
|
110
|
-
readonly text: string | null;
|
|
111
|
-
readonly binary: ArrayBuffer | null;
|
|
112
|
-
readonly metadata: FileMeta | null;
|
|
113
|
-
readonly detectedAt: string;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
interface PersistedLegacyConflict {
|
|
117
|
-
readonly schemaVersion: typeof LEGACY_CONFLICT_SCHEMA_VERSION;
|
|
118
|
-
readonly path: WorkspacePath;
|
|
119
|
-
readonly candidates: readonly PersistedLegacyRecoveryCandidate[];
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export interface IndexedDBLegacyRecoveryCandidate {
|
|
123
|
-
readonly kind: IndexedDBLegacyRecoveryKind;
|
|
124
|
-
readonly reason: IndexedDBLegacyConflictReason;
|
|
125
|
-
readonly text: string | null;
|
|
126
|
-
readonly binary: ArrayBuffer | null;
|
|
127
|
-
readonly metadata: FileMeta | null;
|
|
128
|
-
readonly detectedAt: string;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export interface IndexedDBLegacyMigrationConflict {
|
|
132
|
-
readonly path: WorkspacePath;
|
|
133
|
-
/** Backward-compatible projection of the newest quarantined candidate. */
|
|
134
|
-
readonly reason: IndexedDBLegacyConflictReason;
|
|
135
|
-
readonly kind: IndexedDBLegacyRecoveryKind;
|
|
136
|
-
readonly text: string | null;
|
|
137
|
-
readonly binary: ArrayBuffer | null;
|
|
138
|
-
readonly metadata: FileMeta | null;
|
|
139
|
-
readonly detectedAt: string;
|
|
140
|
-
readonly candidates: readonly IndexedDBLegacyRecoveryCandidate[];
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export type IndexedDBLegacyConflictResolution =
|
|
144
|
-
| 'v2'
|
|
145
|
-
| 'text'
|
|
146
|
-
| 'binary'
|
|
147
|
-
| {
|
|
148
|
-
readonly candidate: number;
|
|
149
|
-
readonly payload: 'text' | 'binary' | 'directory';
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
export interface IndexedDBFileSystemProviderV2Options {
|
|
153
|
-
/** Injectable store for fault, migration, and concurrency tests. */
|
|
154
|
-
readonly store?: IndexedDBFileSystemStore;
|
|
155
|
-
readonly now?: () => Date;
|
|
156
|
-
/** Shared-store owners can retain the connection when this facade is disposed. */
|
|
157
|
-
readonly closeStoreOnDispose?: boolean;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
interface MutationVersion {
|
|
161
|
-
readonly state: PersistedState;
|
|
162
|
-
readonly version: FileSystemVersion;
|
|
163
|
-
readonly lastModified: string;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* IndexedDB implementation of the correctness-first filesystem contract.
|
|
168
|
-
*
|
|
169
|
-
* Each canonical path has one authoritative structured-clone record containing
|
|
170
|
-
* its kind, owned bytes (for files), version, and metadata. Every mutation and
|
|
171
|
-
* snapshot is one IndexedDB transaction. Legacy v1 keys are imported once;
|
|
172
|
-
* ambiguous or stale branches move into durable recovery records before the
|
|
173
|
-
* legacy namespace is deleted, leaving one live authority.
|
|
174
|
-
*/
|
|
175
|
-
export class IndexedDBFileSystemProviderV2 implements FileSystemProviderV2 {
|
|
176
|
-
public readonly id: string;
|
|
177
|
-
public readonly label: string;
|
|
178
|
-
public readonly capabilities = INDEXEDDB_V2_CAPABILITIES;
|
|
179
|
-
|
|
180
|
-
private readonly store: IndexedDBFileSystemStore;
|
|
181
|
-
private readonly now: () => Date;
|
|
182
|
-
private readonly closeStoreOnDispose: boolean;
|
|
183
|
-
private readonly versionPrefix: string;
|
|
184
|
-
private readonly subscriptions = new Set<FileSystemWatchSubscription>();
|
|
185
|
-
private readonly inFlight = new Set<Promise<unknown>>();
|
|
186
|
-
private initialization: Promise<void> | null = null;
|
|
187
|
-
private disposePromise: Promise<void> | null = null;
|
|
188
|
-
private storeClosed = false;
|
|
189
|
-
private disposed = false;
|
|
190
|
-
|
|
191
|
-
public constructor(
|
|
192
|
-
id: string,
|
|
193
|
-
label: string,
|
|
194
|
-
options: IndexedDBFileSystemProviderV2Options = {},
|
|
195
|
-
) {
|
|
196
|
-
this.id = id;
|
|
197
|
-
this.label = label;
|
|
198
|
-
this.store = options.store ?? new RawIndexedDBFileSystemStore(`docblocks-fs-${id}`, 'files');
|
|
199
|
-
this.now = options.now ?? (() => new Date());
|
|
200
|
-
this.closeStoreOnDispose = options.closeStoreOnDispose ?? true;
|
|
201
|
-
this.versionPrefix = `idb-${encodeURIComponent(id).slice(0, 900) || 'workspace'}`;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/** Track accepted storage work so disposal can join it before closing. */
|
|
205
|
-
private runSharedTransaction<T>(
|
|
206
|
-
mode: IndexedDBFileSystemTransactionMode,
|
|
207
|
-
operation: FsOperation,
|
|
208
|
-
work: (transaction: IndexedDBFileSystemTransaction) => Promise<T>,
|
|
209
|
-
): Promise<T> {
|
|
210
|
-
this.assertOpen(operation);
|
|
211
|
-
const transaction = this.store.transaction(mode, work);
|
|
212
|
-
this.inFlight.add(transaction);
|
|
213
|
-
void transaction.then(
|
|
214
|
-
() => this.inFlight.delete(transaction),
|
|
215
|
-
() => this.inFlight.delete(transaction),
|
|
216
|
-
);
|
|
217
|
-
return transaction;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
public async stat(path: WorkspacePath): Promise<FileSystemEntrySnapshot | null> {
|
|
221
|
-
this.assertOpen('stat');
|
|
222
|
-
const canonical = parseWorkspacePath(path);
|
|
223
|
-
await this.ensureInitialized('stat');
|
|
224
|
-
return this.transaction('readonly', { operation: 'stat', path: canonical }, async (tx) => {
|
|
225
|
-
const entry = await readEntry(tx, canonical);
|
|
226
|
-
return entry ? toEntrySnapshot(entry) : null;
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
public async readFile(path: WorkspacePath): Promise<FileSystemFileRead | null> {
|
|
231
|
-
this.assertOpen('read');
|
|
232
|
-
const canonical = parseWorkspacePath(path);
|
|
233
|
-
await this.ensureInitialized('read');
|
|
234
|
-
return this.transaction('readonly', { operation: 'read', path: canonical }, async (tx) => {
|
|
235
|
-
const entry = await readEntry(tx, canonical);
|
|
236
|
-
if (!entry) return null;
|
|
237
|
-
if (entry.kind !== 'file') {
|
|
238
|
-
throw this.error(
|
|
239
|
-
'type-mismatch',
|
|
240
|
-
'read',
|
|
241
|
-
canonical,
|
|
242
|
-
'Expected a file but found a directory.',
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
return Object.freeze({ entry: toFileSnapshot(entry), data: copyBytes(entry.data) });
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
public async readDirectory(path: WorkspacePath): Promise<readonly FileSystemEntrySnapshot[]> {
|
|
250
|
-
this.assertOpen('list');
|
|
251
|
-
const canonical = parseWorkspacePath(path);
|
|
252
|
-
await this.ensureInitialized('list');
|
|
253
|
-
return this.transaction('readonly', { operation: 'list', path: canonical }, async (tx) => {
|
|
254
|
-
const directory = await readEntry(tx, canonical);
|
|
255
|
-
if (!directory) {
|
|
256
|
-
throw this.error('not-found', 'list', canonical, 'Directory does not exist.');
|
|
257
|
-
}
|
|
258
|
-
if (directory.kind !== 'directory') {
|
|
259
|
-
throw this.error(
|
|
260
|
-
'type-mismatch',
|
|
261
|
-
'list',
|
|
262
|
-
canonical,
|
|
263
|
-
'Expected a directory but found a file.',
|
|
264
|
-
);
|
|
265
|
-
}
|
|
266
|
-
const paths = await listEntryPaths(tx);
|
|
267
|
-
const children: FileSystemEntrySnapshot[] = [];
|
|
268
|
-
for (const candidate of paths) {
|
|
269
|
-
if (candidate && workspacePathDirname(candidate) === canonical) {
|
|
270
|
-
const entry = await readRequiredEntry(tx, candidate);
|
|
271
|
-
children.push(toEntrySnapshot(entry));
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
children.sort(compareSnapshots);
|
|
275
|
-
return Object.freeze(children);
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
public async writeFile(
|
|
280
|
-
path: WorkspacePath,
|
|
281
|
-
data: ArrayBuffer | Uint8Array,
|
|
282
|
-
options: FileSystemWriteOptions = {},
|
|
283
|
-
): Promise<FileSystemFileSnapshot> {
|
|
284
|
-
this.assertOpen('write');
|
|
285
|
-
const canonical = this.canonicalNonRoot(path, 'write');
|
|
286
|
-
const ownedData = copyBytes(data);
|
|
287
|
-
await this.ensureInitialized('write');
|
|
288
|
-
return this.withMutationLock(() =>
|
|
289
|
-
this.transaction('readwrite', { operation: 'write', path: canonical }, async (tx) => {
|
|
290
|
-
await assertNoLegacyConflict(tx, canonical, 'write');
|
|
291
|
-
const mutation = new EntryMutation(tx);
|
|
292
|
-
const existing = await mutation.get(canonical);
|
|
293
|
-
if (existing?.kind === 'directory') {
|
|
294
|
-
throw this.error(
|
|
295
|
-
'type-mismatch',
|
|
296
|
-
'write',
|
|
297
|
-
canonical,
|
|
298
|
-
'Cannot replace a directory with a file.',
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
const mode = options.mode ?? 'upsert';
|
|
302
|
-
if (mode === 'create' && existing) {
|
|
303
|
-
throw this.error('already-exists', 'write', canonical, 'File already exists.');
|
|
304
|
-
}
|
|
305
|
-
if (mode === 'replace' && !existing) {
|
|
306
|
-
throw this.error('not-found', 'write', canonical, 'File does not exist.');
|
|
307
|
-
}
|
|
308
|
-
this.assertExpectedVersion(existing, options.expectedVersion, 'write', canonical);
|
|
309
|
-
const missingParents = await this.planParents(
|
|
310
|
-
mutation,
|
|
311
|
-
canonical,
|
|
312
|
-
options.createParents ?? false,
|
|
313
|
-
'write',
|
|
314
|
-
);
|
|
315
|
-
const next = await this.nextMutationVersion(tx);
|
|
316
|
-
this.addDirectories(mutation, missingParents, next);
|
|
317
|
-
const file: PersistedFileEntry = {
|
|
318
|
-
schemaVersion: ENTRY_SCHEMA_VERSION,
|
|
319
|
-
kind: 'file',
|
|
320
|
-
path: canonical,
|
|
321
|
-
data: ownedData,
|
|
322
|
-
version: next.version,
|
|
323
|
-
lastModified: next.lastModified,
|
|
324
|
-
};
|
|
325
|
-
mutation.set(canonical, file);
|
|
326
|
-
await this.touchParents(mutation, canonical, next);
|
|
327
|
-
await mutation.commit(next.state);
|
|
328
|
-
return toFileSnapshot(file);
|
|
329
|
-
}),
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
public async createDirectory(
|
|
334
|
-
path: WorkspacePath,
|
|
335
|
-
options: FileSystemCreateDirectoryOptions = {},
|
|
336
|
-
): Promise<FileSystemDirectorySnapshot> {
|
|
337
|
-
this.assertOpen('create-directory');
|
|
338
|
-
const canonical = parseWorkspacePath(path);
|
|
339
|
-
await this.ensureInitialized('create-directory');
|
|
340
|
-
return this.withMutationLock(() =>
|
|
341
|
-
this.transaction(
|
|
342
|
-
'readwrite',
|
|
343
|
-
{ operation: 'create-directory', path: canonical },
|
|
344
|
-
async (tx) => {
|
|
345
|
-
await assertNoLegacyConflict(tx, canonical, 'create-directory');
|
|
346
|
-
const mutation = new EntryMutation(tx);
|
|
347
|
-
const existing = await mutation.get(canonical);
|
|
348
|
-
if (existing) {
|
|
349
|
-
if (existing.kind !== 'directory') {
|
|
350
|
-
throw this.error(
|
|
351
|
-
'type-mismatch',
|
|
352
|
-
'create-directory',
|
|
353
|
-
canonical,
|
|
354
|
-
'Cannot replace a file with a directory.',
|
|
355
|
-
);
|
|
356
|
-
}
|
|
357
|
-
if ((options.mode ?? 'ensure') === 'create') {
|
|
358
|
-
throw this.error(
|
|
359
|
-
'already-exists',
|
|
360
|
-
'create-directory',
|
|
361
|
-
canonical,
|
|
362
|
-
'Directory already exists.',
|
|
363
|
-
);
|
|
364
|
-
}
|
|
365
|
-
return toDirectorySnapshot(existing);
|
|
366
|
-
}
|
|
367
|
-
const missingParents = await this.planParents(
|
|
368
|
-
mutation,
|
|
369
|
-
canonical,
|
|
370
|
-
options.createParents ?? false,
|
|
371
|
-
'create-directory',
|
|
372
|
-
);
|
|
373
|
-
const next = await this.nextMutationVersion(tx);
|
|
374
|
-
this.addDirectories(mutation, [...missingParents, canonical], next);
|
|
375
|
-
await this.touchParents(mutation, canonical, next);
|
|
376
|
-
await mutation.commit(next.state);
|
|
377
|
-
const created = await mutation.get(canonical);
|
|
378
|
-
if (!created || created.kind !== 'directory') {
|
|
379
|
-
throw corrupt('Created directory was not staged.', canonical);
|
|
380
|
-
}
|
|
381
|
-
return toDirectorySnapshot(created);
|
|
382
|
-
},
|
|
383
|
-
),
|
|
384
|
-
);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
public async remove(
|
|
388
|
-
path: WorkspacePath,
|
|
389
|
-
options: FileSystemRemoveOptions = {},
|
|
390
|
-
): Promise<FileSystemRemoveResult> {
|
|
391
|
-
this.assertOpen('remove');
|
|
392
|
-
const canonical = this.canonicalNonRoot(path, 'remove');
|
|
393
|
-
await this.ensureInitialized('remove');
|
|
394
|
-
return this.withMutationLock(() =>
|
|
395
|
-
this.transaction('readwrite', { operation: 'remove', path: canonical }, async (tx) => {
|
|
396
|
-
const mutation = new EntryMutation(tx);
|
|
397
|
-
const existing = await mutation.get(canonical);
|
|
398
|
-
const state = await readRequiredState(tx);
|
|
399
|
-
if (!existing) {
|
|
400
|
-
if ((options.missing ?? 'error') === 'ignore') {
|
|
401
|
-
return Object.freeze({
|
|
402
|
-
removed: false,
|
|
403
|
-
version: parseFileSystemVersion(state.treeVersion),
|
|
404
|
-
});
|
|
405
|
-
}
|
|
406
|
-
throw this.error('not-found', 'remove', canonical, 'Entry does not exist.');
|
|
407
|
-
}
|
|
408
|
-
this.assertExpectedVersion(existing, options.expectedVersion, 'remove', canonical);
|
|
409
|
-
const paths = await mutation.paths();
|
|
410
|
-
const descendants = paths.filter((candidate) =>
|
|
411
|
-
workspacePathContains(canonical, candidate, false),
|
|
412
|
-
);
|
|
413
|
-
if (existing.kind === 'directory' && descendants.length > 0 && !options.recursive) {
|
|
414
|
-
throw this.error('not-empty', 'remove', canonical, 'Directory is not empty.');
|
|
415
|
-
}
|
|
416
|
-
const next = this.advanceState(state);
|
|
417
|
-
mutation.delete(canonical);
|
|
418
|
-
if (existing.kind === 'directory') {
|
|
419
|
-
for (const descendant of descendants) mutation.delete(descendant);
|
|
420
|
-
}
|
|
421
|
-
await this.touchParents(mutation, canonical, next);
|
|
422
|
-
await mutation.commit(next.state);
|
|
423
|
-
return Object.freeze({ removed: true, version: next.version });
|
|
424
|
-
}),
|
|
425
|
-
);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
public async move(
|
|
429
|
-
oldPath: WorkspacePath,
|
|
430
|
-
newPath: WorkspacePath,
|
|
431
|
-
options: FileSystemMoveOptions = {},
|
|
432
|
-
): Promise<FileSystemEntrySnapshot> {
|
|
433
|
-
this.assertOpen('move');
|
|
434
|
-
const sourcePath = this.canonicalNonRoot(oldPath, 'move');
|
|
435
|
-
const destinationPath = this.canonicalNonRoot(newPath, 'move');
|
|
436
|
-
await this.ensureInitialized('move');
|
|
437
|
-
return this.withMutationLock(() =>
|
|
438
|
-
this.transaction(
|
|
439
|
-
'readwrite',
|
|
440
|
-
{ operation: 'move', path: sourcePath, destinationPath },
|
|
441
|
-
async (tx) => {
|
|
442
|
-
await assertNoLegacyConflict(tx, sourcePath, 'move');
|
|
443
|
-
await assertNoLegacyConflict(tx, destinationPath, 'move');
|
|
444
|
-
const mutation = new EntryMutation(tx);
|
|
445
|
-
const source = await mutation.get(sourcePath);
|
|
446
|
-
if (!source) {
|
|
447
|
-
throw this.error('not-found', 'move', sourcePath, 'Source does not exist.');
|
|
448
|
-
}
|
|
449
|
-
this.assertExpectedVersion(source, options.expectedVersion, 'move', sourcePath);
|
|
450
|
-
if (sourcePath === destinationPath) return toEntrySnapshot(source);
|
|
451
|
-
if (await mutation.get(destinationPath)) {
|
|
452
|
-
throw new FsError('already-exists', 'Destination already exists.', {
|
|
453
|
-
operation: 'move',
|
|
454
|
-
path: sourcePath,
|
|
455
|
-
destinationPath,
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
if (
|
|
459
|
-
source.kind === 'directory' &&
|
|
460
|
-
workspacePathContains(sourcePath, destinationPath, false)
|
|
461
|
-
) {
|
|
462
|
-
throw new FsError('invalid-path', 'Cannot move a directory into itself.', {
|
|
463
|
-
operation: 'move',
|
|
464
|
-
path: sourcePath,
|
|
465
|
-
destinationPath,
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
const missingParents = await this.planParents(
|
|
469
|
-
mutation,
|
|
470
|
-
destinationPath,
|
|
471
|
-
options.createParents ?? false,
|
|
472
|
-
'move',
|
|
473
|
-
);
|
|
474
|
-
const paths = await mutation.paths();
|
|
475
|
-
const sourcePaths = paths.filter((candidate) =>
|
|
476
|
-
workspacePathContains(sourcePath, candidate),
|
|
477
|
-
);
|
|
478
|
-
const sourceSet = new Set(sourcePaths);
|
|
479
|
-
const remapped: Array<readonly [WorkspacePath, WorkspacePath, PersistedEntry]> = [];
|
|
480
|
-
for (const currentPath of sourcePaths) {
|
|
481
|
-
const suffix = currentPath.slice(sourcePath.length);
|
|
482
|
-
const targetPath = parseWorkspacePath(`${destinationPath}${suffix}`);
|
|
483
|
-
const collision = await mutation.get(targetPath);
|
|
484
|
-
if (collision && !sourceSet.has(targetPath)) {
|
|
485
|
-
throw new FsError('already-exists', 'A destination descendant already exists.', {
|
|
486
|
-
operation: 'move',
|
|
487
|
-
path: currentPath,
|
|
488
|
-
destinationPath: targetPath,
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
remapped.push([currentPath, targetPath, await mutation.require(currentPath)]);
|
|
492
|
-
}
|
|
493
|
-
const state = await readRequiredState(tx);
|
|
494
|
-
const next = this.advanceState(state);
|
|
495
|
-
for (const currentPath of sourcePaths) mutation.delete(currentPath);
|
|
496
|
-
this.addDirectories(mutation, missingParents, next);
|
|
497
|
-
for (const [, targetPath, current] of remapped) {
|
|
498
|
-
mutation.set(targetPath, withPathAndVersion(current, targetPath, next));
|
|
499
|
-
}
|
|
500
|
-
await this.touchParents(mutation, sourcePath, next);
|
|
501
|
-
await this.touchParents(mutation, destinationPath, next);
|
|
502
|
-
await mutation.commit(next.state);
|
|
503
|
-
return toEntrySnapshot(await mutation.require(destinationPath));
|
|
504
|
-
},
|
|
505
|
-
),
|
|
506
|
-
);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
public async snapshot(): Promise<FileSystemSnapshot> {
|
|
510
|
-
this.assertOpen('snapshot');
|
|
511
|
-
await this.ensureInitialized('snapshot');
|
|
512
|
-
return this.transaction('readonly', { operation: 'snapshot' }, async (tx) => {
|
|
513
|
-
const [state, paths] = await Promise.all([readRequiredState(tx), listEntryPaths(tx)]);
|
|
514
|
-
const entries: FileSystemSnapshotEntry[] = [];
|
|
515
|
-
for (const path of paths.sort(compareText)) {
|
|
516
|
-
const entry = await readRequiredEntry(tx, path);
|
|
517
|
-
entries.push(
|
|
518
|
-
entry.kind === 'file'
|
|
519
|
-
? Object.freeze({ ...toFileSnapshot(entry), data: copyBytes(entry.data) })
|
|
520
|
-
: toDirectorySnapshot(entry),
|
|
521
|
-
);
|
|
522
|
-
}
|
|
523
|
-
return Object.freeze({
|
|
524
|
-
version: parseFileSystemVersion(state.treeVersion),
|
|
525
|
-
entries: Object.freeze(entries),
|
|
526
|
-
});
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
/** IndexedDB has no reliable cross-browser external-change observer. */
|
|
531
|
-
public watch(
|
|
532
|
-
_listener: FileSystemWatchListener,
|
|
533
|
-
options: FileSystemWatchOptions = {},
|
|
534
|
-
): FileSystemWatchSubscription {
|
|
535
|
-
this.assertOpen('watch');
|
|
536
|
-
const error = new FsError('not-supported', 'IndexedDB filesystem watching is unavailable.', {
|
|
537
|
-
operation: 'watch',
|
|
538
|
-
});
|
|
539
|
-
let closed = false;
|
|
540
|
-
let disposePromise: Promise<void> | null = null;
|
|
541
|
-
const ready = Promise.reject(error);
|
|
542
|
-
void ready.catch((caught: FsError) => {
|
|
543
|
-
try {
|
|
544
|
-
options.onError?.(caught);
|
|
545
|
-
} catch {
|
|
546
|
-
// An observational error callback cannot change subscription state.
|
|
547
|
-
}
|
|
548
|
-
});
|
|
549
|
-
const subscription: FileSystemWatchSubscription = {
|
|
550
|
-
get closed() {
|
|
551
|
-
return closed;
|
|
552
|
-
},
|
|
553
|
-
ready,
|
|
554
|
-
dispose: () => {
|
|
555
|
-
if (disposePromise) return disposePromise;
|
|
556
|
-
closed = true;
|
|
557
|
-
this.subscriptions.delete(subscription);
|
|
558
|
-
disposePromise = Promise.resolve();
|
|
559
|
-
return disposePromise;
|
|
560
|
-
},
|
|
561
|
-
};
|
|
562
|
-
this.subscriptions.add(subscription);
|
|
563
|
-
return subscription;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
public dispose(): Promise<void> {
|
|
567
|
-
if (this.disposePromise) return this.disposePromise;
|
|
568
|
-
this.disposed = true;
|
|
569
|
-
this.disposePromise = (async () => {
|
|
570
|
-
try {
|
|
571
|
-
await Promise.allSettled([...this.inFlight]);
|
|
572
|
-
await Promise.all([...this.subscriptions].map((subscription) => subscription.dispose()));
|
|
573
|
-
} finally {
|
|
574
|
-
if (this.closeStoreOnDispose && !this.storeClosed) {
|
|
575
|
-
this.storeClosed = true;
|
|
576
|
-
this.store.close?.();
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
})();
|
|
580
|
-
return this.disposePromise;
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
/** List unresolved, byte-preserving conflicts found during v1 migration. */
|
|
584
|
-
public async listLegacyMigrationConflicts(): Promise<
|
|
585
|
-
readonly IndexedDBLegacyMigrationConflict[]
|
|
586
|
-
> {
|
|
587
|
-
this.assertOpen('read');
|
|
588
|
-
await this.ensureInitialized('read');
|
|
589
|
-
return this.transaction('readonly', { operation: 'read' }, async (tx) => {
|
|
590
|
-
const keys = (await tx.keys()).filter((key) => key.startsWith(LEGACY_CONFLICT_PREFIX));
|
|
591
|
-
const conflicts: IndexedDBLegacyMigrationConflict[] = [];
|
|
592
|
-
for (const key of keys) {
|
|
593
|
-
const raw = await tx.get<unknown>(key);
|
|
594
|
-
conflicts.push(toPublicLegacyConflict(validateLegacyConflict(raw)));
|
|
595
|
-
}
|
|
596
|
-
conflicts.sort((left, right) => compareText(left.path, right.path));
|
|
597
|
-
return Object.freeze(conflicts);
|
|
598
|
-
});
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
/** Resolve quarantined legacy data by choosing v2 or one legacy byte payload. */
|
|
602
|
-
public async resolveLegacyMigrationConflict(
|
|
603
|
-
path: WorkspacePath,
|
|
604
|
-
resolution: IndexedDBLegacyConflictResolution,
|
|
605
|
-
): Promise<FileSystemEntrySnapshot> {
|
|
606
|
-
this.assertOpen('write');
|
|
607
|
-
const canonical = this.canonicalNonRoot(path, 'write');
|
|
608
|
-
await this.ensureInitialized('write');
|
|
609
|
-
return this.withMutationLock(() =>
|
|
610
|
-
this.transaction('readwrite', { operation: 'write', path: canonical }, async (tx) => {
|
|
611
|
-
const key = legacyConflictKey(canonical);
|
|
612
|
-
const conflict = validateLegacyConflict(await tx.get<unknown>(key));
|
|
613
|
-
if (conflict.path !== canonical) throw corrupt('Legacy conflict key mismatch.', canonical);
|
|
614
|
-
const mutation = new EntryMutation(tx);
|
|
615
|
-
const existing = await mutation.get(canonical);
|
|
616
|
-
if (resolution === 'v2') {
|
|
617
|
-
if (!existing) {
|
|
618
|
-
throw this.error(
|
|
619
|
-
'type-mismatch',
|
|
620
|
-
'write',
|
|
621
|
-
canonical,
|
|
622
|
-
'There is no v2 authority to keep at this path.',
|
|
623
|
-
);
|
|
624
|
-
}
|
|
625
|
-
await tx.delete(key);
|
|
626
|
-
return toEntrySnapshot(existing);
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
const selected = selectLegacyPayload(conflict, resolution, canonical);
|
|
630
|
-
if (selected.kind === 'directory') {
|
|
631
|
-
if (existing?.kind === 'file') {
|
|
632
|
-
throw this.error(
|
|
633
|
-
'type-mismatch',
|
|
634
|
-
'write',
|
|
635
|
-
canonical,
|
|
636
|
-
'Move or remove the v2 file before restoring a legacy directory at this path.',
|
|
637
|
-
);
|
|
638
|
-
}
|
|
639
|
-
if (existing) {
|
|
640
|
-
await tx.delete(key);
|
|
641
|
-
return toDirectorySnapshot(existing);
|
|
642
|
-
}
|
|
643
|
-
const missingParents = await this.planParents(mutation, canonical, true, 'write');
|
|
644
|
-
const next = await this.nextMutationVersion(tx);
|
|
645
|
-
this.addDirectories(mutation, [...missingParents, canonical], next);
|
|
646
|
-
await this.touchParents(mutation, canonical, next);
|
|
647
|
-
await mutation.commit(next.state);
|
|
648
|
-
await tx.delete(key);
|
|
649
|
-
const restored = await mutation.require(canonical);
|
|
650
|
-
if (restored.kind !== 'directory') {
|
|
651
|
-
throw corrupt('Restored legacy directory was not staged as a directory.', canonical);
|
|
652
|
-
}
|
|
653
|
-
return toDirectorySnapshot(restored);
|
|
654
|
-
}
|
|
655
|
-
if (existing?.kind === 'directory') {
|
|
656
|
-
throw this.error(
|
|
657
|
-
'type-mismatch',
|
|
658
|
-
'write',
|
|
659
|
-
canonical,
|
|
660
|
-
'Move or remove the v2 directory before restoring a legacy file at this path.',
|
|
661
|
-
);
|
|
662
|
-
}
|
|
663
|
-
const missingParents = await this.planParents(mutation, canonical, true, 'write');
|
|
664
|
-
const next = await this.nextMutationVersion(tx);
|
|
665
|
-
this.addDirectories(mutation, missingParents, next);
|
|
666
|
-
const file: PersistedFileEntry = {
|
|
667
|
-
schemaVersion: ENTRY_SCHEMA_VERSION,
|
|
668
|
-
kind: 'file',
|
|
669
|
-
path: canonical,
|
|
670
|
-
data: copyBytes(selected.data),
|
|
671
|
-
version: next.version,
|
|
672
|
-
lastModified: next.lastModified,
|
|
673
|
-
};
|
|
674
|
-
mutation.set(canonical, file);
|
|
675
|
-
await this.touchParents(mutation, canonical, next);
|
|
676
|
-
await mutation.commit(next.state);
|
|
677
|
-
await tx.delete(key);
|
|
678
|
-
return toFileSnapshot(file);
|
|
679
|
-
}),
|
|
680
|
-
);
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
/** Explicitly discard every quarantined legacy candidate at a path. */
|
|
684
|
-
public async discardLegacyMigrationConflict(path: WorkspacePath): Promise<boolean> {
|
|
685
|
-
this.assertOpen('remove');
|
|
686
|
-
const canonical = this.canonicalNonRoot(path, 'remove');
|
|
687
|
-
await this.ensureInitialized('remove');
|
|
688
|
-
return this.withMutationLock(() =>
|
|
689
|
-
this.transaction('readwrite', { operation: 'remove', path: canonical }, async (tx) => {
|
|
690
|
-
const key = legacyConflictKey(canonical);
|
|
691
|
-
if ((await tx.get<unknown>(key)) === null) return false;
|
|
692
|
-
await tx.delete(key);
|
|
693
|
-
return true;
|
|
694
|
-
}),
|
|
695
|
-
);
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
private async ensureInitialized(operation: FsOperation): Promise<void> {
|
|
699
|
-
this.assertOpen(operation);
|
|
700
|
-
if (this.initialization) return this.initialization;
|
|
701
|
-
const attempt = this.withMutationLock(() =>
|
|
702
|
-
this.runSharedTransaction('readwrite', operation, (tx) => this.migrateLegacy(tx)),
|
|
703
|
-
);
|
|
704
|
-
this.initialization = attempt.catch((error: unknown) => {
|
|
705
|
-
this.initialization = null;
|
|
706
|
-
throw fsErrorFromUnknown(error, { operation });
|
|
707
|
-
});
|
|
708
|
-
return this.initialization;
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
private async migrateLegacy(tx: IndexedDBFileSystemTransaction): Promise<void> {
|
|
712
|
-
const existingState = await readState(tx);
|
|
713
|
-
const keys = await tx.keys();
|
|
714
|
-
if (existingState) {
|
|
715
|
-
const root = await readEntry(tx, WORKSPACE_ROOT);
|
|
716
|
-
if (!root || root.kind !== 'directory') {
|
|
717
|
-
throw corrupt('Initialized v2 filesystem has no root directory.', WORKSPACE_ROOT);
|
|
718
|
-
}
|
|
719
|
-
if (
|
|
720
|
-
existingState.migrationVersion === 1 ||
|
|
721
|
-
keys.some((key) => key.startsWith(LEGACY_PREFIX))
|
|
722
|
-
) {
|
|
723
|
-
await this.quarantineExistingLegacy(
|
|
724
|
-
tx,
|
|
725
|
-
keys,
|
|
726
|
-
existingState,
|
|
727
|
-
existingState.migrationVersion === 1 ? 'authority-collision' : 'stale-client-write',
|
|
728
|
-
);
|
|
729
|
-
}
|
|
730
|
-
return;
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
const now = this.now().toISOString();
|
|
734
|
-
const version = this.makeVersion(1);
|
|
735
|
-
const entries = new Map<WorkspacePath, PersistedEntry>();
|
|
736
|
-
|
|
737
|
-
// Preserve any authoritative v2 records left by a pre-release build.
|
|
738
|
-
for (const path of entryPathsFromKeys(keys)) {
|
|
739
|
-
entries.set(path, await readRequiredEntry(tx, path));
|
|
740
|
-
}
|
|
741
|
-
if (entries.get(WORKSPACE_ROOT)?.kind === 'file') {
|
|
742
|
-
throw corrupt(
|
|
743
|
-
'Pre-release v2 authority stored a file at the workspace root.',
|
|
744
|
-
WORKSPACE_ROOT,
|
|
745
|
-
);
|
|
746
|
-
}
|
|
747
|
-
entries.set(
|
|
748
|
-
WORKSPACE_ROOT,
|
|
749
|
-
entries.get(WORKSPACE_ROOT) ?? directoryRecord(WORKSPACE_ROOT, version, now),
|
|
750
|
-
);
|
|
751
|
-
|
|
752
|
-
const legacyDirectories = await readLegacyDirectories(tx);
|
|
753
|
-
const legacyFilePaths = legacyPathsFromKeys(keys);
|
|
754
|
-
for (const directory of legacyDirectories) {
|
|
755
|
-
const existing = entries.get(directory);
|
|
756
|
-
if (existing?.kind === 'file' || hasFileAncestor(entries, directory)) {
|
|
757
|
-
await appendLegacyConflict(
|
|
758
|
-
tx,
|
|
759
|
-
directory,
|
|
760
|
-
directoryRecoveryCandidate('authority-collision', now),
|
|
761
|
-
);
|
|
762
|
-
continue;
|
|
763
|
-
}
|
|
764
|
-
addParentDirectories(entries, directory, version, now);
|
|
765
|
-
if (!existing) entries.set(directory, directoryRecord(directory, version, now));
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
for (const path of legacyFilePaths) {
|
|
769
|
-
const legacy = await readLegacyFile(tx, path);
|
|
770
|
-
const existing = entries.get(path);
|
|
771
|
-
if (existing?.kind === 'file') {
|
|
772
|
-
if (!legacyPayloadsMatchEntry(legacy, existing)) {
|
|
773
|
-
await appendLegacyConflict(
|
|
774
|
-
tx,
|
|
775
|
-
path,
|
|
776
|
-
fileRecoveryCandidate(legacy, 'authority-collision', now),
|
|
777
|
-
);
|
|
778
|
-
}
|
|
779
|
-
continue;
|
|
780
|
-
}
|
|
781
|
-
const kindCollision = existing?.kind === 'directory' || hasFileAncestor(entries, path);
|
|
782
|
-
const payloadsDisagree =
|
|
783
|
-
legacy.content !== null &&
|
|
784
|
-
legacy.binary !== null &&
|
|
785
|
-
!bytesEqual(new TextEncoder().encode(legacy.content), legacy.binary);
|
|
786
|
-
if (
|
|
787
|
-
payloadsDisagree ||
|
|
788
|
-
kindCollision ||
|
|
789
|
-
(legacy.content === null && legacy.binary === null)
|
|
790
|
-
) {
|
|
791
|
-
await appendLegacyConflict(
|
|
792
|
-
tx,
|
|
793
|
-
path,
|
|
794
|
-
fileRecoveryCandidate(
|
|
795
|
-
legacy,
|
|
796
|
-
kindCollision
|
|
797
|
-
? 'kind-collision'
|
|
798
|
-
: payloadsDisagree
|
|
799
|
-
? 'dual-payload'
|
|
800
|
-
: 'authority-collision',
|
|
801
|
-
now,
|
|
802
|
-
),
|
|
803
|
-
);
|
|
804
|
-
continue;
|
|
805
|
-
}
|
|
806
|
-
addParentDirectories(entries, path, version, now);
|
|
807
|
-
const data =
|
|
808
|
-
legacy.content !== null
|
|
809
|
-
? new TextEncoder().encode(legacy.content).buffer
|
|
810
|
-
: copyBytes(legacy.binary!);
|
|
811
|
-
entries.set(path, {
|
|
812
|
-
schemaVersion: ENTRY_SCHEMA_VERSION,
|
|
813
|
-
kind: 'file',
|
|
814
|
-
path,
|
|
815
|
-
data: copyBytes(data),
|
|
816
|
-
version,
|
|
817
|
-
lastModified: legacy.metadata?.lastModified ?? now,
|
|
818
|
-
});
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
for (const entry of entries.values()) {
|
|
822
|
-
await tx.put(entryKey(entry.path), entry);
|
|
823
|
-
}
|
|
824
|
-
await deleteLegacyNamespace(tx, keys);
|
|
825
|
-
const state: PersistedState = {
|
|
826
|
-
schemaVersion: STATE_SCHEMA_VERSION,
|
|
827
|
-
migrationVersion: 2,
|
|
828
|
-
mutationCounter: 1,
|
|
829
|
-
treeVersion: version,
|
|
830
|
-
};
|
|
831
|
-
await tx.put(STATE_KEY, state);
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
private async quarantineExistingLegacy(
|
|
835
|
-
tx: IndexedDBFileSystemTransaction,
|
|
836
|
-
keys: readonly string[],
|
|
837
|
-
state: PersistedState,
|
|
838
|
-
reason: 'authority-collision' | 'stale-client-write',
|
|
839
|
-
): Promise<void> {
|
|
840
|
-
const now = this.now().toISOString();
|
|
841
|
-
const ignored = await readMarkerPaths(
|
|
842
|
-
tx,
|
|
843
|
-
keys,
|
|
844
|
-
LEGACY_IGNORED_PREFIX,
|
|
845
|
-
validateIgnoredLegacyPath,
|
|
846
|
-
);
|
|
847
|
-
const legacyDirectories = await readLegacyDirectories(tx);
|
|
848
|
-
for (const path of legacyDirectories) {
|
|
849
|
-
if (ignored.has(path)) continue;
|
|
850
|
-
const entry = await readEntry(tx, path);
|
|
851
|
-
if (entry?.kind === 'directory') continue;
|
|
852
|
-
await appendLegacyConflict(
|
|
853
|
-
tx,
|
|
854
|
-
path,
|
|
855
|
-
directoryRecoveryCandidate(entry ? 'kind-collision' : reason, now),
|
|
856
|
-
);
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
for (const path of legacyPathsFromKeys(keys)) {
|
|
860
|
-
if (ignored.has(path)) continue;
|
|
861
|
-
const legacy = await readLegacyFile(tx, path);
|
|
862
|
-
const entry = await readEntry(tx, path);
|
|
863
|
-
if (entry?.kind === 'file' && legacyPayloadsMatchEntry(legacy, entry)) continue;
|
|
864
|
-
await appendLegacyConflict(
|
|
865
|
-
tx,
|
|
866
|
-
path,
|
|
867
|
-
fileRecoveryCandidate(legacy, entry?.kind === 'directory' ? 'kind-collision' : reason, now),
|
|
868
|
-
);
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
await deleteLegacyNamespace(tx, keys);
|
|
872
|
-
for (const key of keys) {
|
|
873
|
-
if (key.startsWith(LEGACY_IGNORED_PREFIX)) await tx.delete(key);
|
|
874
|
-
}
|
|
875
|
-
if (state.migrationVersion === 1) {
|
|
876
|
-
await tx.put(STATE_KEY, { ...state, migrationVersion: 2 } satisfies PersistedState);
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
private async transaction<T>(
|
|
881
|
-
mode: IndexedDBFileSystemTransactionMode,
|
|
882
|
-
context: FsErrorContext,
|
|
883
|
-
operation: (transaction: IndexedDBFileSystemTransaction) => Promise<T>,
|
|
884
|
-
): Promise<T> {
|
|
885
|
-
this.assertOpen(context.operation ?? 'read');
|
|
886
|
-
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
887
|
-
try {
|
|
888
|
-
const outcome = await this.runSharedTransaction(
|
|
889
|
-
mode,
|
|
890
|
-
context.operation ?? 'read',
|
|
891
|
-
async (transaction) => {
|
|
892
|
-
if (await transaction.hasKeyWithPrefix(LEGACY_PREFIX)) {
|
|
893
|
-
if (mode === 'readonly') throw new LegacyNamespaceDetectedError();
|
|
894
|
-
const state = await readRequiredState(transaction);
|
|
895
|
-
await this.quarantineExistingLegacy(
|
|
896
|
-
transaction,
|
|
897
|
-
await transaction.keys(),
|
|
898
|
-
state,
|
|
899
|
-
'stale-client-write',
|
|
900
|
-
);
|
|
901
|
-
return { retry: true as const };
|
|
902
|
-
}
|
|
903
|
-
return { retry: false as const, value: await operation(transaction) };
|
|
904
|
-
},
|
|
905
|
-
);
|
|
906
|
-
if (outcome.retry) continue;
|
|
907
|
-
return outcome.value;
|
|
908
|
-
} catch (error: unknown) {
|
|
909
|
-
if (!(error instanceof LegacyNamespaceDetectedError)) {
|
|
910
|
-
throw fsErrorFromUnknown(error, context);
|
|
911
|
-
}
|
|
912
|
-
try {
|
|
913
|
-
await this.withMutationLock(() =>
|
|
914
|
-
this.runSharedTransaction(
|
|
915
|
-
'readwrite',
|
|
916
|
-
context.operation ?? 'read',
|
|
917
|
-
async (transaction) => {
|
|
918
|
-
if (!(await transaction.hasKeyWithPrefix(LEGACY_PREFIX))) return;
|
|
919
|
-
const state = await readRequiredState(transaction);
|
|
920
|
-
await this.quarantineExistingLegacy(
|
|
921
|
-
transaction,
|
|
922
|
-
await transaction.keys(),
|
|
923
|
-
state,
|
|
924
|
-
'stale-client-write',
|
|
925
|
-
);
|
|
926
|
-
},
|
|
927
|
-
),
|
|
928
|
-
);
|
|
929
|
-
} catch (quarantineError: unknown) {
|
|
930
|
-
throw fsErrorFromUnknown(quarantineError, context);
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
throw new FsError('busy', 'A stale IndexedDB client is continuously writing legacy records.', {
|
|
935
|
-
...context,
|
|
936
|
-
retryable: true,
|
|
937
|
-
});
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
private withMutationLock<T>(operation: () => Promise<T>): Promise<T> {
|
|
941
|
-
return withFileCommitLock(`indexeddb:${this.id}:workspace`, operation);
|
|
942
|
-
}
|
|
943
|
-
|
|
944
|
-
private async nextMutationVersion(tx: IndexedDBFileSystemTransaction): Promise<MutationVersion> {
|
|
945
|
-
return this.advanceState(await readRequiredState(tx));
|
|
946
|
-
}
|
|
947
|
-
|
|
948
|
-
private advanceState(state: PersistedState): MutationVersion {
|
|
949
|
-
if (state.mutationCounter >= Number.MAX_SAFE_INTEGER) {
|
|
950
|
-
throw corrupt('IndexedDB filesystem mutation counter overflowed.');
|
|
951
|
-
}
|
|
952
|
-
const mutationCounter = state.mutationCounter + 1;
|
|
953
|
-
const version = this.makeVersion(mutationCounter);
|
|
954
|
-
const nextState: PersistedState = {
|
|
955
|
-
...state,
|
|
956
|
-
mutationCounter,
|
|
957
|
-
treeVersion: version,
|
|
958
|
-
};
|
|
959
|
-
return { state: nextState, version, lastModified: this.now().toISOString() };
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
private makeVersion(counter: number): FileSystemVersion {
|
|
963
|
-
return parseFileSystemVersion(`${this.versionPrefix}:${counter}`);
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
private async planParents(
|
|
967
|
-
mutation: EntryMutation,
|
|
968
|
-
path: WorkspacePath,
|
|
969
|
-
createParents: boolean,
|
|
970
|
-
operation: FsOperation,
|
|
971
|
-
): Promise<WorkspacePath[]> {
|
|
972
|
-
const missing: WorkspacePath[] = [];
|
|
973
|
-
for (const parent of parentChain(path)) {
|
|
974
|
-
const entry = await mutation.get(parent);
|
|
975
|
-
if (entry?.kind === 'file') {
|
|
976
|
-
throw this.error(
|
|
977
|
-
'type-mismatch',
|
|
978
|
-
operation,
|
|
979
|
-
parent,
|
|
980
|
-
'A parent path is a file, not a directory.',
|
|
981
|
-
);
|
|
982
|
-
}
|
|
983
|
-
if (!entry) {
|
|
984
|
-
if (!createParents) {
|
|
985
|
-
throw this.error('not-found', operation, parent, 'Parent directory does not exist.');
|
|
986
|
-
}
|
|
987
|
-
missing.push(parent);
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
return missing;
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
private addDirectories(
|
|
994
|
-
mutation: EntryMutation,
|
|
995
|
-
paths: readonly WorkspacePath[],
|
|
996
|
-
next: MutationVersion,
|
|
997
|
-
): void {
|
|
998
|
-
for (const path of paths) {
|
|
999
|
-
mutation.set(path, directoryRecord(path, next.version, next.lastModified));
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
|
|
1003
|
-
private async touchParents(
|
|
1004
|
-
mutation: EntryMutation,
|
|
1005
|
-
path: WorkspacePath,
|
|
1006
|
-
next: MutationVersion,
|
|
1007
|
-
): Promise<void> {
|
|
1008
|
-
for (const parent of parentChain(path)) {
|
|
1009
|
-
const entry = await mutation.get(parent);
|
|
1010
|
-
if (entry?.kind === 'directory') {
|
|
1011
|
-
mutation.set(parent, directoryRecord(parent, next.version, next.lastModified));
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
private canonicalNonRoot(path: WorkspacePath, operation: FsOperation): WorkspacePath {
|
|
1017
|
-
const canonical = parseWorkspacePath(path);
|
|
1018
|
-
if (!canonical) {
|
|
1019
|
-
throw this.error(
|
|
1020
|
-
'invalid-path',
|
|
1021
|
-
operation,
|
|
1022
|
-
canonical,
|
|
1023
|
-
'The workspace root is not a file entry.',
|
|
1024
|
-
);
|
|
1025
|
-
}
|
|
1026
|
-
return canonical;
|
|
1027
|
-
}
|
|
1028
|
-
|
|
1029
|
-
private assertExpectedVersion(
|
|
1030
|
-
entry: PersistedEntry | null,
|
|
1031
|
-
expected: FileSystemVersion | null | undefined,
|
|
1032
|
-
operation: FsOperation,
|
|
1033
|
-
path: WorkspacePath,
|
|
1034
|
-
): void {
|
|
1035
|
-
if (expected === undefined) return;
|
|
1036
|
-
if (expected === null ? !entry : entry?.version === expected) return;
|
|
1037
|
-
throw this.error(
|
|
1038
|
-
'conflict',
|
|
1039
|
-
operation,
|
|
1040
|
-
path,
|
|
1041
|
-
'Entry version does not match the expected version.',
|
|
1042
|
-
);
|
|
1043
|
-
}
|
|
1044
|
-
|
|
1045
|
-
private assertOpen(operation: FsOperation): void {
|
|
1046
|
-
if (!this.disposed) return;
|
|
1047
|
-
throw new FsError('disposed', 'Filesystem provider is disposed.', { operation });
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
private error(
|
|
1051
|
-
code: FsErrorCode,
|
|
1052
|
-
operation: FsOperation,
|
|
1053
|
-
path: WorkspacePath,
|
|
1054
|
-
message: string,
|
|
1055
|
-
): FsError {
|
|
1056
|
-
return new FsError(code, message, { operation, path });
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
|
|
1060
|
-
class EntryMutation {
|
|
1061
|
-
private readonly cache = new Map<WorkspacePath, PersistedEntry | null>();
|
|
1062
|
-
private readonly changed = new Map<WorkspacePath, PersistedEntry>();
|
|
1063
|
-
private readonly deleted = new Set<WorkspacePath>();
|
|
1064
|
-
|
|
1065
|
-
public constructor(private readonly transaction: IndexedDBFileSystemTransaction) {}
|
|
1066
|
-
|
|
1067
|
-
public async get(path: WorkspacePath): Promise<PersistedEntry | null> {
|
|
1068
|
-
if (this.deleted.has(path)) return null;
|
|
1069
|
-
const cached = this.cache.get(path);
|
|
1070
|
-
if (cached !== undefined) return cached;
|
|
1071
|
-
const entry = await readEntry(this.transaction, path);
|
|
1072
|
-
this.cache.set(path, entry);
|
|
1073
|
-
return entry;
|
|
1074
|
-
}
|
|
1075
|
-
|
|
1076
|
-
public async require(path: WorkspacePath): Promise<PersistedEntry> {
|
|
1077
|
-
const entry = await this.get(path);
|
|
1078
|
-
if (!entry) throw corrupt(`Missing staged entry: ${path}.`, path);
|
|
1079
|
-
return entry;
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
|
-
public set(path: WorkspacePath, entry: PersistedEntry): void {
|
|
1083
|
-
const owned = copyEntry(entry);
|
|
1084
|
-
this.cache.set(path, owned);
|
|
1085
|
-
this.changed.set(path, owned);
|
|
1086
|
-
this.deleted.delete(path);
|
|
1087
|
-
}
|
|
1088
|
-
|
|
1089
|
-
public delete(path: WorkspacePath): void {
|
|
1090
|
-
this.cache.set(path, null);
|
|
1091
|
-
this.changed.delete(path);
|
|
1092
|
-
this.deleted.add(path);
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
public async paths(): Promise<WorkspacePath[]> {
|
|
1096
|
-
const paths = new Set(await listEntryPaths(this.transaction));
|
|
1097
|
-
for (const path of this.deleted) paths.delete(path);
|
|
1098
|
-
for (const path of this.changed.keys()) paths.add(path);
|
|
1099
|
-
return [...paths];
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
|
-
public async commit(state: PersistedState): Promise<void> {
|
|
1103
|
-
for (const path of this.deleted) await this.transaction.delete(entryKey(path));
|
|
1104
|
-
for (const [path, entry] of this.changed) await this.transaction.put(entryKey(path), entry);
|
|
1105
|
-
await this.transaction.put(STATE_KEY, state);
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
function entryKey(path: WorkspacePath): string {
|
|
1110
|
-
return `${ENTRY_PREFIX}${path}`;
|
|
1111
|
-
}
|
|
1112
|
-
|
|
1113
|
-
function legacyConflictKey(path: WorkspacePath): string {
|
|
1114
|
-
return `${LEGACY_CONFLICT_PREFIX}${path}`;
|
|
1115
|
-
}
|
|
1116
|
-
|
|
1117
|
-
function entryPathsFromKeys(keys: readonly string[]): WorkspacePath[] {
|
|
1118
|
-
return keys
|
|
1119
|
-
.filter((key) => key.startsWith(ENTRY_PREFIX))
|
|
1120
|
-
.map((key) => {
|
|
1121
|
-
const rawPath = key.slice(ENTRY_PREFIX.length);
|
|
1122
|
-
const path = parseWorkspacePath(rawPath);
|
|
1123
|
-
if (path !== rawPath) throw corrupt(`Non-canonical v2 entry key: ${key}.`);
|
|
1124
|
-
return path;
|
|
1125
|
-
});
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
async function listEntryPaths(tx: IndexedDBFileSystemTransaction): Promise<WorkspacePath[]> {
|
|
1129
|
-
return entryPathsFromKeys(await tx.keys());
|
|
1130
|
-
}
|
|
1131
|
-
|
|
1132
|
-
async function readEntry(
|
|
1133
|
-
tx: IndexedDBFileSystemTransaction,
|
|
1134
|
-
path: WorkspacePath,
|
|
1135
|
-
): Promise<PersistedEntry | null> {
|
|
1136
|
-
const raw = await tx.get<unknown>(entryKey(path));
|
|
1137
|
-
return raw === null ? null : validateEntry(raw, path);
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1140
|
-
async function readRequiredEntry(
|
|
1141
|
-
tx: IndexedDBFileSystemTransaction,
|
|
1142
|
-
path: WorkspacePath,
|
|
1143
|
-
): Promise<PersistedEntry> {
|
|
1144
|
-
const entry = await readEntry(tx, path);
|
|
1145
|
-
if (!entry) throw corrupt(`Missing v2 entry record: ${path}.`, path);
|
|
1146
|
-
return entry;
|
|
1147
|
-
}
|
|
1148
|
-
|
|
1149
|
-
function validateEntry(value: unknown, expectedPath: WorkspacePath): PersistedEntry {
|
|
1150
|
-
if (!isRecord(value)) throw corrupt(`Corrupt v2 entry record: ${expectedPath}.`, expectedPath);
|
|
1151
|
-
const path = parseWorkspacePath(readString(value.path, 'entry path'));
|
|
1152
|
-
if (path !== expectedPath) throw corrupt(`V2 entry path does not match key: ${expectedPath}.`);
|
|
1153
|
-
if (value.schemaVersion !== ENTRY_SCHEMA_VERSION) {
|
|
1154
|
-
throw corrupt(`Unsupported v2 entry schema at ${expectedPath}.`, expectedPath);
|
|
1155
|
-
}
|
|
1156
|
-
const version = parseFileSystemVersion(value.version);
|
|
1157
|
-
const lastModified = readString(value.lastModified, 'lastModified');
|
|
1158
|
-
if (value.kind === 'directory') {
|
|
1159
|
-
return { schemaVersion: ENTRY_SCHEMA_VERSION, kind: 'directory', path, version, lastModified };
|
|
1160
|
-
}
|
|
1161
|
-
if (value.kind !== 'file') throw corrupt(`Unknown entry kind at ${expectedPath}.`, expectedPath);
|
|
1162
|
-
return {
|
|
1163
|
-
schemaVersion: ENTRY_SCHEMA_VERSION,
|
|
1164
|
-
kind: 'file',
|
|
1165
|
-
path,
|
|
1166
|
-
version,
|
|
1167
|
-
lastModified,
|
|
1168
|
-
data: validateBytes(value.data, expectedPath),
|
|
1169
|
-
};
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
async function readState(tx: IndexedDBFileSystemTransaction): Promise<PersistedState | null> {
|
|
1173
|
-
const raw = await tx.get<unknown>(STATE_KEY);
|
|
1174
|
-
return raw === null ? null : validateState(raw);
|
|
1175
|
-
}
|
|
1176
|
-
|
|
1177
|
-
async function readRequiredState(tx: IndexedDBFileSystemTransaction): Promise<PersistedState> {
|
|
1178
|
-
const state = await readState(tx);
|
|
1179
|
-
if (!state) throw corrupt('IndexedDB v2 state record is missing.');
|
|
1180
|
-
return state;
|
|
1181
|
-
}
|
|
1182
|
-
|
|
1183
|
-
function validateState(value: unknown): PersistedState {
|
|
1184
|
-
if (
|
|
1185
|
-
!isRecord(value) ||
|
|
1186
|
-
value.schemaVersion !== STATE_SCHEMA_VERSION ||
|
|
1187
|
-
(value.migrationVersion !== 1 && value.migrationVersion !== 2) ||
|
|
1188
|
-
typeof value.mutationCounter !== 'number' ||
|
|
1189
|
-
!Number.isSafeInteger(value.mutationCounter) ||
|
|
1190
|
-
value.mutationCounter < 0
|
|
1191
|
-
) {
|
|
1192
|
-
throw corrupt('IndexedDB v2 state record is corrupt.');
|
|
1193
|
-
}
|
|
1194
|
-
return {
|
|
1195
|
-
schemaVersion: STATE_SCHEMA_VERSION,
|
|
1196
|
-
migrationVersion: value.migrationVersion,
|
|
1197
|
-
mutationCounter: value.mutationCounter,
|
|
1198
|
-
treeVersion: parseFileSystemVersion(value.treeVersion),
|
|
1199
|
-
};
|
|
1200
|
-
}
|
|
1201
|
-
|
|
1202
|
-
async function readLegacyDirectories(tx: IndexedDBFileSystemTransaction): Promise<WorkspacePath[]> {
|
|
1203
|
-
const raw = await tx.get<unknown>(LEGACY_DIRS_KEY);
|
|
1204
|
-
if (raw === null) return [];
|
|
1205
|
-
if (!Array.isArray(raw)) throw corrupt('Legacy directory index is corrupt.');
|
|
1206
|
-
const directories: WorkspacePath[] = [];
|
|
1207
|
-
for (const value of raw) {
|
|
1208
|
-
if (typeof value !== 'string') throw corrupt('Legacy directory index is corrupt.');
|
|
1209
|
-
const path = parseWorkspacePath(value);
|
|
1210
|
-
if (path) directories.push(path);
|
|
1211
|
-
}
|
|
1212
|
-
return directories;
|
|
1213
|
-
}
|
|
1214
|
-
|
|
1215
|
-
function legacyPathsFromKeys(keys: readonly string[]): WorkspacePath[] {
|
|
1216
|
-
const paths = new Set<WorkspacePath>();
|
|
1217
|
-
for (const key of keys) {
|
|
1218
|
-
if (!key.startsWith(LEGACY_PREFIX) || key === LEGACY_DIRS_KEY) continue;
|
|
1219
|
-
const suffix = [LEGACY_CONTENT_SUFFIX, LEGACY_BINARY_SUFFIX, LEGACY_META_SUFFIX].find(
|
|
1220
|
-
(candidate) => key.endsWith(candidate),
|
|
1221
|
-
);
|
|
1222
|
-
if (!suffix) continue;
|
|
1223
|
-
const rawPath = key.slice(LEGACY_PREFIX.length, -suffix.length);
|
|
1224
|
-
try {
|
|
1225
|
-
const path = parseWorkspacePath(rawPath);
|
|
1226
|
-
if (path) paths.add(path);
|
|
1227
|
-
} catch {
|
|
1228
|
-
// Invalid legacy keys remain untouched. They are not valid v2 paths.
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
return [...paths];
|
|
1232
|
-
}
|
|
1233
|
-
|
|
1234
|
-
async function readLegacyFile(
|
|
1235
|
-
tx: IndexedDBFileSystemTransaction,
|
|
1236
|
-
path: WorkspacePath,
|
|
1237
|
-
): Promise<{ content: string | null; binary: ArrayBuffer | null; metadata: FileMeta | null }> {
|
|
1238
|
-
const [rawContent, rawBinary, rawMetadata] = await Promise.all([
|
|
1239
|
-
tx.get<unknown>(`${LEGACY_PREFIX}${path}${LEGACY_CONTENT_SUFFIX}`),
|
|
1240
|
-
tx.get<unknown>(`${LEGACY_PREFIX}${path}${LEGACY_BINARY_SUFFIX}`),
|
|
1241
|
-
tx.get<unknown>(`${LEGACY_PREFIX}${path}${LEGACY_META_SUFFIX}`),
|
|
1242
|
-
]);
|
|
1243
|
-
if (rawContent !== null && typeof rawContent !== 'string') {
|
|
1244
|
-
throw corrupt(`Legacy text payload is corrupt: ${path}.`, path);
|
|
1245
|
-
}
|
|
1246
|
-
return {
|
|
1247
|
-
content: rawContent,
|
|
1248
|
-
binary: rawBinary === null ? null : validateBytes(rawBinary, path),
|
|
1249
|
-
metadata: validateLegacyMetadata(rawMetadata, path),
|
|
1250
|
-
};
|
|
1251
|
-
}
|
|
1252
|
-
|
|
1253
|
-
async function readMarkerPaths(
|
|
1254
|
-
transaction: IndexedDBFileSystemTransaction,
|
|
1255
|
-
keys: readonly string[],
|
|
1256
|
-
prefix: string,
|
|
1257
|
-
validate: (value: unknown) => WorkspacePath,
|
|
1258
|
-
): Promise<Set<WorkspacePath>> {
|
|
1259
|
-
const paths = new Set<WorkspacePath>();
|
|
1260
|
-
for (const key of keys) {
|
|
1261
|
-
if (!key.startsWith(prefix)) continue;
|
|
1262
|
-
const expectedPath = parseWorkspacePath(key.slice(prefix.length));
|
|
1263
|
-
if (!expectedPath) throw corrupt(`Recovery marker cannot target the workspace root: ${key}.`);
|
|
1264
|
-
const value = await transaction.get<unknown>(key);
|
|
1265
|
-
const path = validate(value);
|
|
1266
|
-
if (path !== expectedPath)
|
|
1267
|
-
throw corrupt(`Recovery marker path does not match its key: ${key}.`);
|
|
1268
|
-
paths.add(path);
|
|
1269
|
-
}
|
|
1270
|
-
return paths;
|
|
1271
|
-
}
|
|
1272
|
-
|
|
1273
|
-
function validateIgnoredLegacyPath(value: unknown): WorkspacePath {
|
|
1274
|
-
if (!isRecord(value) || value.schemaVersion !== 1 || typeof value.ignoredAt !== 'string') {
|
|
1275
|
-
throw corrupt('Ignored legacy recovery marker is corrupt.');
|
|
1276
|
-
}
|
|
1277
|
-
const path = parseWorkspacePath(readString(value.path, 'ignored legacy path'));
|
|
1278
|
-
if (!path) throw corrupt('Ignored legacy recovery marker cannot target the workspace root.');
|
|
1279
|
-
return path;
|
|
1280
|
-
}
|
|
1281
|
-
|
|
1282
|
-
function bytesEqual(left: ArrayBuffer | Uint8Array, right: ArrayBuffer | Uint8Array): boolean {
|
|
1283
|
-
const leftBytes = ArrayBuffer.isView(left)
|
|
1284
|
-
? new Uint8Array(left.buffer, left.byteOffset, left.byteLength)
|
|
1285
|
-
: new Uint8Array(left);
|
|
1286
|
-
const rightBytes = ArrayBuffer.isView(right)
|
|
1287
|
-
? new Uint8Array(right.buffer, right.byteOffset, right.byteLength)
|
|
1288
|
-
: new Uint8Array(right);
|
|
1289
|
-
if (leftBytes.byteLength !== rightBytes.byteLength) return false;
|
|
1290
|
-
return leftBytes.every((byte, index) => byte === rightBytes[index]);
|
|
1291
|
-
}
|
|
1292
|
-
|
|
1293
|
-
async function assertNoLegacyConflict(
|
|
1294
|
-
transaction: IndexedDBFileSystemTransaction,
|
|
1295
|
-
path: WorkspacePath,
|
|
1296
|
-
operation: FsOperation,
|
|
1297
|
-
): Promise<void> {
|
|
1298
|
-
if ((await transaction.get<unknown>(legacyConflictKey(path))) === null) return;
|
|
1299
|
-
throw new FsError(
|
|
1300
|
-
'conflict',
|
|
1301
|
-
'Resolve the legacy migration conflict before mutating this path.',
|
|
1302
|
-
{
|
|
1303
|
-
operation,
|
|
1304
|
-
path,
|
|
1305
|
-
retryable: false,
|
|
1306
|
-
},
|
|
1307
|
-
);
|
|
1308
|
-
}
|
|
1309
|
-
|
|
1310
|
-
function validateLegacyMetadata(value: unknown, path: WorkspacePath): FileMeta | null {
|
|
1311
|
-
if (value === null) return null;
|
|
1312
|
-
if (
|
|
1313
|
-
!isRecord(value) ||
|
|
1314
|
-
typeof value.name !== 'string' ||
|
|
1315
|
-
typeof value.path !== 'string' ||
|
|
1316
|
-
typeof value.size !== 'number' ||
|
|
1317
|
-
!Number.isFinite(value.size) ||
|
|
1318
|
-
typeof value.lastModified !== 'string'
|
|
1319
|
-
) {
|
|
1320
|
-
throw corrupt(`Legacy metadata is corrupt: ${path}.`, path);
|
|
1321
|
-
}
|
|
1322
|
-
return {
|
|
1323
|
-
name: value.name,
|
|
1324
|
-
path: value.path,
|
|
1325
|
-
size: value.size,
|
|
1326
|
-
lastModified: value.lastModified,
|
|
1327
|
-
};
|
|
1328
|
-
}
|
|
1329
|
-
|
|
1330
|
-
function validateLegacyConflict(value: unknown): PersistedLegacyConflict {
|
|
1331
|
-
if (!isRecord(value)) {
|
|
1332
|
-
throw corrupt('Legacy migration conflict record is corrupt.');
|
|
1333
|
-
}
|
|
1334
|
-
const path = parseWorkspacePath(readString(value.path, 'legacy conflict path'));
|
|
1335
|
-
if (!path) throw corrupt('Legacy migration conflict cannot target the workspace root.');
|
|
1336
|
-
|
|
1337
|
-
if (value.schemaVersion === LEGACY_CONFLICT_SCHEMA_VERSION_V1) {
|
|
1338
|
-
const reason = validateLegacyConflictReason(value.reason);
|
|
1339
|
-
const candidate: PersistedLegacyRecoveryCandidate = {
|
|
1340
|
-
kind: 'file',
|
|
1341
|
-
reason,
|
|
1342
|
-
text: value.text === null ? null : readString(value.text, 'legacy conflict text'),
|
|
1343
|
-
binary: value.binary === null ? null : validateBytes(value.binary, path),
|
|
1344
|
-
metadata: validateLegacyMetadata(value.metadata, path),
|
|
1345
|
-
detectedAt: readString(value.detectedAt, 'legacy conflict detectedAt'),
|
|
1346
|
-
};
|
|
1347
|
-
return {
|
|
1348
|
-
schemaVersion: LEGACY_CONFLICT_SCHEMA_VERSION,
|
|
1349
|
-
path,
|
|
1350
|
-
candidates: Object.freeze([candidate]),
|
|
1351
|
-
};
|
|
1352
|
-
}
|
|
1353
|
-
if (value.schemaVersion !== LEGACY_CONFLICT_SCHEMA_VERSION || !Array.isArray(value.candidates)) {
|
|
1354
|
-
throw corrupt('Legacy migration conflict record is corrupt.');
|
|
1355
|
-
}
|
|
1356
|
-
const candidates = value.candidates.map((candidate) =>
|
|
1357
|
-
validateLegacyRecoveryCandidate(candidate, path),
|
|
1358
|
-
);
|
|
1359
|
-
if (candidates.length === 0) throw corrupt('Legacy migration conflict has no candidates.', path);
|
|
1360
|
-
return {
|
|
1361
|
-
schemaVersion: LEGACY_CONFLICT_SCHEMA_VERSION,
|
|
1362
|
-
path,
|
|
1363
|
-
candidates: Object.freeze(candidates),
|
|
1364
|
-
};
|
|
1365
|
-
}
|
|
1366
|
-
|
|
1367
|
-
function toPublicLegacyConflict(
|
|
1368
|
-
conflict: PersistedLegacyConflict,
|
|
1369
|
-
): IndexedDBLegacyMigrationConflict {
|
|
1370
|
-
const candidates = conflict.candidates.map(toPublicLegacyCandidate);
|
|
1371
|
-
const latest = candidates[candidates.length - 1]!;
|
|
1372
|
-
return Object.freeze({
|
|
1373
|
-
path: parseWorkspacePath(conflict.path),
|
|
1374
|
-
reason: latest.reason,
|
|
1375
|
-
kind: latest.kind,
|
|
1376
|
-
text: latest.text,
|
|
1377
|
-
binary: latest.binary,
|
|
1378
|
-
metadata: latest.metadata,
|
|
1379
|
-
detectedAt: latest.detectedAt,
|
|
1380
|
-
candidates: Object.freeze(candidates),
|
|
1381
|
-
});
|
|
1382
|
-
}
|
|
1383
|
-
|
|
1384
|
-
function validateLegacyRecoveryCandidate(
|
|
1385
|
-
value: unknown,
|
|
1386
|
-
path: WorkspacePath,
|
|
1387
|
-
): PersistedLegacyRecoveryCandidate {
|
|
1388
|
-
if (!isRecord(value) || (value.kind !== 'file' && value.kind !== 'directory')) {
|
|
1389
|
-
throw corrupt('Legacy recovery candidate is corrupt.', path);
|
|
1390
|
-
}
|
|
1391
|
-
const kind = value.kind;
|
|
1392
|
-
const text = value.text === null ? null : readString(value.text, 'legacy recovery text');
|
|
1393
|
-
const binary = value.binary === null ? null : validateBytes(value.binary, path);
|
|
1394
|
-
const metadata = validateLegacyMetadata(value.metadata, path);
|
|
1395
|
-
if (kind === 'directory' && (text !== null || binary !== null || metadata !== null)) {
|
|
1396
|
-
throw corrupt('A legacy directory recovery candidate contains file data.', path);
|
|
1397
|
-
}
|
|
1398
|
-
return {
|
|
1399
|
-
kind,
|
|
1400
|
-
reason: validateLegacyConflictReason(value.reason),
|
|
1401
|
-
text,
|
|
1402
|
-
binary,
|
|
1403
|
-
metadata,
|
|
1404
|
-
detectedAt: readString(value.detectedAt, 'legacy recovery detectedAt'),
|
|
1405
|
-
};
|
|
1406
|
-
}
|
|
1407
|
-
|
|
1408
|
-
function validateLegacyConflictReason(value: unknown): IndexedDBLegacyConflictReason {
|
|
1409
|
-
if (
|
|
1410
|
-
value === 'dual-payload' ||
|
|
1411
|
-
value === 'kind-collision' ||
|
|
1412
|
-
value === 'authority-collision' ||
|
|
1413
|
-
value === 'stale-client-write'
|
|
1414
|
-
) {
|
|
1415
|
-
return value;
|
|
1416
|
-
}
|
|
1417
|
-
throw corrupt('Legacy recovery reason is invalid.');
|
|
1418
|
-
}
|
|
1419
|
-
|
|
1420
|
-
function toPublicLegacyCandidate(
|
|
1421
|
-
candidate: PersistedLegacyRecoveryCandidate,
|
|
1422
|
-
): IndexedDBLegacyRecoveryCandidate {
|
|
1423
|
-
return Object.freeze({
|
|
1424
|
-
kind: candidate.kind,
|
|
1425
|
-
reason: candidate.reason,
|
|
1426
|
-
text: candidate.text,
|
|
1427
|
-
binary: candidate.binary ? copyBytes(candidate.binary) : null,
|
|
1428
|
-
metadata: candidate.metadata ? Object.freeze({ ...candidate.metadata }) : null,
|
|
1429
|
-
detectedAt: candidate.detectedAt,
|
|
1430
|
-
});
|
|
1431
|
-
}
|
|
1432
|
-
|
|
1433
|
-
async function appendLegacyConflict(
|
|
1434
|
-
transaction: IndexedDBFileSystemTransaction,
|
|
1435
|
-
path: WorkspacePath,
|
|
1436
|
-
candidate: PersistedLegacyRecoveryCandidate,
|
|
1437
|
-
): Promise<void> {
|
|
1438
|
-
const key = legacyConflictKey(path);
|
|
1439
|
-
const raw = await transaction.get<unknown>(key);
|
|
1440
|
-
const existing = raw === null ? null : validateLegacyConflict(raw);
|
|
1441
|
-
if (existing?.candidates.some((item) => recoveryCandidatesEqual(item, candidate))) return;
|
|
1442
|
-
const candidates = [...(existing?.candidates ?? []), copyRecoveryCandidate(candidate)];
|
|
1443
|
-
await transaction.put(key, {
|
|
1444
|
-
schemaVersion: LEGACY_CONFLICT_SCHEMA_VERSION,
|
|
1445
|
-
path,
|
|
1446
|
-
candidates,
|
|
1447
|
-
} satisfies PersistedLegacyConflict);
|
|
1448
|
-
}
|
|
1449
|
-
|
|
1450
|
-
function fileRecoveryCandidate(
|
|
1451
|
-
legacy: { content: string | null; binary: ArrayBuffer | null; metadata: FileMeta | null },
|
|
1452
|
-
reason: IndexedDBLegacyConflictReason,
|
|
1453
|
-
detectedAt: string,
|
|
1454
|
-
): PersistedLegacyRecoveryCandidate {
|
|
1455
|
-
return {
|
|
1456
|
-
kind: 'file',
|
|
1457
|
-
reason,
|
|
1458
|
-
text: legacy.content,
|
|
1459
|
-
binary: legacy.binary ? copyBytes(legacy.binary) : null,
|
|
1460
|
-
metadata: legacy.metadata ? { ...legacy.metadata } : null,
|
|
1461
|
-
detectedAt,
|
|
1462
|
-
};
|
|
1463
|
-
}
|
|
1464
|
-
|
|
1465
|
-
function directoryRecoveryCandidate(
|
|
1466
|
-
reason: IndexedDBLegacyConflictReason,
|
|
1467
|
-
detectedAt: string,
|
|
1468
|
-
): PersistedLegacyRecoveryCandidate {
|
|
1469
|
-
return { kind: 'directory', reason, text: null, binary: null, metadata: null, detectedAt };
|
|
1470
|
-
}
|
|
1471
|
-
|
|
1472
|
-
function copyRecoveryCandidate(
|
|
1473
|
-
candidate: PersistedLegacyRecoveryCandidate,
|
|
1474
|
-
): PersistedLegacyRecoveryCandidate {
|
|
1475
|
-
return {
|
|
1476
|
-
...candidate,
|
|
1477
|
-
binary: candidate.binary ? copyBytes(candidate.binary) : null,
|
|
1478
|
-
metadata: candidate.metadata ? { ...candidate.metadata } : null,
|
|
1479
|
-
};
|
|
1480
|
-
}
|
|
1481
|
-
|
|
1482
|
-
function recoveryCandidatesEqual(
|
|
1483
|
-
left: PersistedLegacyRecoveryCandidate,
|
|
1484
|
-
right: PersistedLegacyRecoveryCandidate,
|
|
1485
|
-
): boolean {
|
|
1486
|
-
return (
|
|
1487
|
-
left.kind === right.kind &&
|
|
1488
|
-
left.text === right.text &&
|
|
1489
|
-
nullableBytesEqual(left.binary, right.binary) &&
|
|
1490
|
-
nullableMetadataEqual(left.metadata, right.metadata)
|
|
1491
|
-
);
|
|
1492
|
-
}
|
|
1493
|
-
|
|
1494
|
-
function nullableBytesEqual(left: ArrayBuffer | null, right: ArrayBuffer | null): boolean {
|
|
1495
|
-
if (left === null || right === null) return left === right;
|
|
1496
|
-
return bytesEqual(left, right);
|
|
1497
|
-
}
|
|
1498
|
-
|
|
1499
|
-
function nullableMetadataEqual(left: FileMeta | null, right: FileMeta | null): boolean {
|
|
1500
|
-
if (left === null || right === null) return left === right;
|
|
1501
|
-
return (
|
|
1502
|
-
left.name === right.name &&
|
|
1503
|
-
left.path === right.path &&
|
|
1504
|
-
left.size === right.size &&
|
|
1505
|
-
left.lastModified === right.lastModified
|
|
1506
|
-
);
|
|
1507
|
-
}
|
|
1508
|
-
|
|
1509
|
-
function legacyPayloadsMatchEntry(
|
|
1510
|
-
legacy: { content: string | null; binary: ArrayBuffer | null; metadata: FileMeta | null },
|
|
1511
|
-
entry: PersistedFileEntry,
|
|
1512
|
-
): boolean {
|
|
1513
|
-
if (legacy.content === null && legacy.binary === null) return false;
|
|
1514
|
-
return (
|
|
1515
|
-
(legacy.content === null || bytesEqual(new TextEncoder().encode(legacy.content), entry.data)) &&
|
|
1516
|
-
(legacy.binary === null || bytesEqual(legacy.binary, entry.data))
|
|
1517
|
-
);
|
|
1518
|
-
}
|
|
1519
|
-
|
|
1520
|
-
function hasFileAncestor(
|
|
1521
|
-
entries: ReadonlyMap<WorkspacePath, PersistedEntry>,
|
|
1522
|
-
path: WorkspacePath,
|
|
1523
|
-
): boolean {
|
|
1524
|
-
return parentChain(path).some((parent) => entries.get(parent)?.kind === 'file');
|
|
1525
|
-
}
|
|
1526
|
-
|
|
1527
|
-
async function deleteLegacyNamespace(
|
|
1528
|
-
transaction: IndexedDBFileSystemTransaction,
|
|
1529
|
-
keys: readonly string[],
|
|
1530
|
-
): Promise<void> {
|
|
1531
|
-
for (const key of keys) {
|
|
1532
|
-
if (key.startsWith(LEGACY_PREFIX) && !isRecognizedLegacyKey(key)) {
|
|
1533
|
-
const archiveKey = `${LEGACY_OPAQUE_PREFIX}${encodeURIComponent(key)}`;
|
|
1534
|
-
const existing = await transaction.get<unknown>(archiveKey);
|
|
1535
|
-
const values =
|
|
1536
|
-
isRecord(existing) &&
|
|
1537
|
-
existing.schemaVersion === 1 &&
|
|
1538
|
-
existing.key === key &&
|
|
1539
|
-
Array.isArray(existing.values)
|
|
1540
|
-
? existing.values
|
|
1541
|
-
: [];
|
|
1542
|
-
await transaction.put(archiveKey, {
|
|
1543
|
-
schemaVersion: 1,
|
|
1544
|
-
key,
|
|
1545
|
-
values: [...values, await transaction.get<unknown>(key)],
|
|
1546
|
-
});
|
|
1547
|
-
}
|
|
1548
|
-
if (key.startsWith(LEGACY_PREFIX) || key.startsWith(LEGACY_IGNORED_PREFIX))
|
|
1549
|
-
await transaction.delete(key);
|
|
1550
|
-
}
|
|
1551
|
-
}
|
|
1552
|
-
|
|
1553
|
-
function isRecognizedLegacyKey(key: string): boolean {
|
|
1554
|
-
if (key === LEGACY_DIRS_KEY) return true;
|
|
1555
|
-
const suffix = [LEGACY_CONTENT_SUFFIX, LEGACY_BINARY_SUFFIX, LEGACY_META_SUFFIX].find((item) =>
|
|
1556
|
-
key.endsWith(item),
|
|
1557
|
-
);
|
|
1558
|
-
if (!suffix) return false;
|
|
1559
|
-
const rawPath = key.slice(LEGACY_PREFIX.length, -suffix.length);
|
|
1560
|
-
try {
|
|
1561
|
-
const path = parseWorkspacePath(rawPath);
|
|
1562
|
-
return path !== WORKSPACE_ROOT && path === rawPath;
|
|
1563
|
-
} catch {
|
|
1564
|
-
return false;
|
|
1565
|
-
}
|
|
1566
|
-
}
|
|
1567
|
-
|
|
1568
|
-
function selectLegacyPayload(
|
|
1569
|
-
conflict: PersistedLegacyConflict,
|
|
1570
|
-
resolution: Exclude<IndexedDBLegacyConflictResolution, 'v2'>,
|
|
1571
|
-
path: WorkspacePath,
|
|
1572
|
-
): { readonly kind: 'file'; readonly data: ArrayBuffer } | { readonly kind: 'directory' } {
|
|
1573
|
-
const payload = typeof resolution === 'string' ? resolution : resolution.payload;
|
|
1574
|
-
const eligible =
|
|
1575
|
-
typeof resolution === 'string'
|
|
1576
|
-
? conflict.candidates.filter(
|
|
1577
|
-
(candidate) =>
|
|
1578
|
-
candidate.kind === 'file' &&
|
|
1579
|
-
(payload === 'text' ? candidate.text !== null : candidate.binary !== null),
|
|
1580
|
-
)
|
|
1581
|
-
: [conflict.candidates[resolution.candidate]].filter(
|
|
1582
|
-
(candidate): candidate is PersistedLegacyRecoveryCandidate => candidate !== undefined,
|
|
1583
|
-
);
|
|
1584
|
-
if (eligible.length !== 1) {
|
|
1585
|
-
throw new FsError(
|
|
1586
|
-
'conflict',
|
|
1587
|
-
'Choose an explicit recovery candidate because multiple legacy versions are quarantined.',
|
|
1588
|
-
{ operation: 'write', path, retryable: false },
|
|
1589
|
-
);
|
|
1590
|
-
}
|
|
1591
|
-
const candidate = eligible[0]!;
|
|
1592
|
-
if (payload === 'directory') {
|
|
1593
|
-
if (candidate.kind !== 'directory') {
|
|
1594
|
-
throw new FsError('type-mismatch', 'The selected recovery candidate is not a directory.', {
|
|
1595
|
-
operation: 'write',
|
|
1596
|
-
path,
|
|
1597
|
-
});
|
|
1598
|
-
}
|
|
1599
|
-
return { kind: 'directory' };
|
|
1600
|
-
}
|
|
1601
|
-
if (candidate.kind !== 'file') {
|
|
1602
|
-
throw new FsError('type-mismatch', 'The selected recovery candidate is a directory.', {
|
|
1603
|
-
operation: 'write',
|
|
1604
|
-
path,
|
|
1605
|
-
});
|
|
1606
|
-
}
|
|
1607
|
-
const data =
|
|
1608
|
-
payload === 'text'
|
|
1609
|
-
? candidate.text === null
|
|
1610
|
-
? null
|
|
1611
|
-
: new TextEncoder().encode(candidate.text).buffer
|
|
1612
|
-
: candidate.binary;
|
|
1613
|
-
if (data === null) {
|
|
1614
|
-
throw new FsError('type-mismatch', `The selected candidate has no ${payload} payload.`, {
|
|
1615
|
-
operation: 'write',
|
|
1616
|
-
path,
|
|
1617
|
-
});
|
|
1618
|
-
}
|
|
1619
|
-
return { kind: 'file', data: copyBytes(data) };
|
|
1620
|
-
}
|
|
1621
|
-
|
|
1622
|
-
function addParentDirectories(
|
|
1623
|
-
entries: Map<WorkspacePath, PersistedEntry>,
|
|
1624
|
-
path: WorkspacePath,
|
|
1625
|
-
version: FileSystemVersion,
|
|
1626
|
-
lastModified: string,
|
|
1627
|
-
): void {
|
|
1628
|
-
for (const parent of parentChain(path)) {
|
|
1629
|
-
if (!entries.has(parent)) entries.set(parent, directoryRecord(parent, version, lastModified));
|
|
1630
|
-
}
|
|
1631
|
-
}
|
|
1632
|
-
|
|
1633
|
-
function parentChain(path: WorkspacePath): WorkspacePath[] {
|
|
1634
|
-
const parents: WorkspacePath[] = [];
|
|
1635
|
-
let current = workspacePathDirname(path);
|
|
1636
|
-
while (true) {
|
|
1637
|
-
parents.unshift(current);
|
|
1638
|
-
if (!current) return parents;
|
|
1639
|
-
current = workspacePathDirname(current);
|
|
1640
|
-
}
|
|
1641
|
-
}
|
|
1642
|
-
|
|
1643
|
-
function directoryRecord(
|
|
1644
|
-
path: WorkspacePath,
|
|
1645
|
-
version: FileSystemVersion,
|
|
1646
|
-
lastModified: string,
|
|
1647
|
-
): PersistedDirectoryEntry {
|
|
1648
|
-
return {
|
|
1649
|
-
schemaVersion: ENTRY_SCHEMA_VERSION,
|
|
1650
|
-
kind: 'directory',
|
|
1651
|
-
path,
|
|
1652
|
-
version,
|
|
1653
|
-
lastModified,
|
|
1654
|
-
};
|
|
1655
|
-
}
|
|
1656
|
-
|
|
1657
|
-
function withPathAndVersion(
|
|
1658
|
-
entry: PersistedEntry,
|
|
1659
|
-
path: WorkspacePath,
|
|
1660
|
-
next: MutationVersion,
|
|
1661
|
-
): PersistedEntry {
|
|
1662
|
-
return entry.kind === 'file'
|
|
1663
|
-
? {
|
|
1664
|
-
schemaVersion: ENTRY_SCHEMA_VERSION,
|
|
1665
|
-
kind: 'file',
|
|
1666
|
-
path,
|
|
1667
|
-
data: copyBytes(entry.data),
|
|
1668
|
-
version: next.version,
|
|
1669
|
-
lastModified: next.lastModified,
|
|
1670
|
-
}
|
|
1671
|
-
: directoryRecord(path, next.version, next.lastModified);
|
|
1672
|
-
}
|
|
1673
|
-
|
|
1674
|
-
function copyEntry(entry: PersistedEntry): PersistedEntry {
|
|
1675
|
-
return entry.kind === 'file' ? { ...entry, data: copyBytes(entry.data) } : { ...entry };
|
|
1676
|
-
}
|
|
1677
|
-
|
|
1678
|
-
function toEntrySnapshot(entry: PersistedEntry): FileSystemEntrySnapshot {
|
|
1679
|
-
return entry.kind === 'file' ? toFileSnapshot(entry) : toDirectorySnapshot(entry);
|
|
1680
|
-
}
|
|
1681
|
-
|
|
1682
|
-
function toFileSnapshot(entry: PersistedFileEntry): FileSystemFileSnapshot {
|
|
1683
|
-
return Object.freeze({
|
|
1684
|
-
kind: 'file',
|
|
1685
|
-
path: parseWorkspacePath(entry.path),
|
|
1686
|
-
name: workspacePathBasename(parseWorkspacePath(entry.path)),
|
|
1687
|
-
size: entry.data.byteLength,
|
|
1688
|
-
version: parseFileSystemVersion(entry.version),
|
|
1689
|
-
lastModified: entry.lastModified,
|
|
1690
|
-
});
|
|
1691
|
-
}
|
|
1692
|
-
|
|
1693
|
-
function toDirectorySnapshot(entry: PersistedDirectoryEntry): FileSystemDirectorySnapshot {
|
|
1694
|
-
const path = parseWorkspacePath(entry.path);
|
|
1695
|
-
return Object.freeze({
|
|
1696
|
-
kind: 'directory',
|
|
1697
|
-
path,
|
|
1698
|
-
name: workspacePathBasename(path),
|
|
1699
|
-
size: null,
|
|
1700
|
-
version: parseFileSystemVersion(entry.version),
|
|
1701
|
-
lastModified: entry.lastModified,
|
|
1702
|
-
});
|
|
1703
|
-
}
|
|
1704
|
-
|
|
1705
|
-
function compareSnapshots(left: FileSystemEntrySnapshot, right: FileSystemEntrySnapshot): number {
|
|
1706
|
-
if (left.kind !== right.kind) return left.kind === 'directory' ? -1 : 1;
|
|
1707
|
-
return compareText(left.name, right.name);
|
|
1708
|
-
}
|
|
1709
|
-
|
|
1710
|
-
function compareText(left: string, right: string): number {
|
|
1711
|
-
return left < right ? -1 : left > right ? 1 : 0;
|
|
1712
|
-
}
|
|
1713
|
-
|
|
1714
|
-
function copyBytes(data: ArrayBuffer | Uint8Array): ArrayBuffer {
|
|
1715
|
-
const source = ArrayBuffer.isView(data)
|
|
1716
|
-
? new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
|
|
1717
|
-
: new Uint8Array(data);
|
|
1718
|
-
const copy = new Uint8Array(source.byteLength);
|
|
1719
|
-
copy.set(source);
|
|
1720
|
-
return copy.buffer as ArrayBuffer;
|
|
1721
|
-
}
|
|
1722
|
-
|
|
1723
|
-
function validateBytes(value: unknown, path: WorkspacePath): ArrayBuffer {
|
|
1724
|
-
if (value instanceof ArrayBuffer) return copyBytes(value);
|
|
1725
|
-
if (value instanceof Uint8Array) return copyBytes(value);
|
|
1726
|
-
throw corrupt(`Stored file bytes are corrupt: ${path}.`, path);
|
|
1727
|
-
}
|
|
1728
|
-
|
|
1729
|
-
function readString(value: unknown, label: string): string {
|
|
1730
|
-
if (typeof value !== 'string') throw corrupt(`Invalid ${label}.`);
|
|
1731
|
-
return value;
|
|
1732
|
-
}
|
|
1733
|
-
|
|
1734
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
1735
|
-
return typeof value === 'object' && value !== null;
|
|
1736
|
-
}
|
|
1737
|
-
|
|
1738
|
-
function corrupt(message: string, path?: WorkspacePath): FsError {
|
|
1739
|
-
return new FsError('io', message, { operation: 'read', path, retryable: false });
|
|
1740
|
-
}
|