@aigne/afs-did-space 1.12.0-beta.5

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.
Files changed (510) hide show
  1. package/LICENSE.md +26 -0
  2. package/README.md +129 -0
  3. package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.cjs +11 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.108.0/helpers/decorate.mjs +10 -0
  5. package/dist/_virtual/rolldown_runtime.cjs +29 -0
  6. package/dist/afs-metadata-context.cjs +20 -0
  7. package/dist/afs-metadata-context.d.cts +21 -0
  8. package/dist/afs-metadata-context.d.cts.map +1 -0
  9. package/dist/afs-metadata-context.d.mts +21 -0
  10. package/dist/afs-metadata-context.d.mts.map +1 -0
  11. package/dist/afs-metadata-context.mjs +18 -0
  12. package/dist/afs-metadata-context.mjs.map +1 -0
  13. package/dist/blocklet-hook.cjs +180 -0
  14. package/dist/blocklet-hook.d.cts +29 -0
  15. package/dist/blocklet-hook.d.cts.map +1 -0
  16. package/dist/blocklet-hook.d.mts +29 -0
  17. package/dist/blocklet-hook.d.mts.map +1 -0
  18. package/dist/blocklet-hook.mjs +178 -0
  19. package/dist/blocklet-hook.mjs.map +1 -0
  20. package/dist/changelog-schema.cjs +603 -0
  21. package/dist/changelog-schema.d.cts +395 -0
  22. package/dist/changelog-schema.d.cts.map +1 -0
  23. package/dist/changelog-schema.d.mts +395 -0
  24. package/dist/changelog-schema.d.mts.map +1 -0
  25. package/dist/changelog-schema.mjs +570 -0
  26. package/dist/changelog-schema.mjs.map +1 -0
  27. package/dist/changelog-write.cjs +80 -0
  28. package/dist/changelog-write.d.cts +70 -0
  29. package/dist/changelog-write.d.cts.map +1 -0
  30. package/dist/changelog-write.d.mts +70 -0
  31. package/dist/changelog-write.d.mts.map +1 -0
  32. package/dist/changelog-write.mjs +77 -0
  33. package/dist/changelog-write.mjs.map +1 -0
  34. package/dist/cid-byte-cache.cjs +126 -0
  35. package/dist/cid-byte-cache.d.cts +46 -0
  36. package/dist/cid-byte-cache.d.cts.map +1 -0
  37. package/dist/cid-byte-cache.d.mts +47 -0
  38. package/dist/cid-byte-cache.d.mts.map +1 -0
  39. package/dist/cid-byte-cache.mjs +125 -0
  40. package/dist/cid-byte-cache.mjs.map +1 -0
  41. package/dist/cid.cjs +68 -0
  42. package/dist/cid.d.cts +22 -0
  43. package/dist/cid.d.cts.map +1 -0
  44. package/dist/cid.d.mts +22 -0
  45. package/dist/cid.d.mts.map +1 -0
  46. package/dist/cid.mjs +65 -0
  47. package/dist/cid.mjs.map +1 -0
  48. package/dist/client.cjs +221 -0
  49. package/dist/client.d.cts +72 -0
  50. package/dist/client.d.cts.map +1 -0
  51. package/dist/client.d.mts +72 -0
  52. package/dist/client.d.mts.map +1 -0
  53. package/dist/client.mjs +220 -0
  54. package/dist/client.mjs.map +1 -0
  55. package/dist/cloudflare.cjs +510 -0
  56. package/dist/cloudflare.d.cts +307 -0
  57. package/dist/cloudflare.d.cts.map +1 -0
  58. package/dist/cloudflare.d.mts +308 -0
  59. package/dist/cloudflare.d.mts.map +1 -0
  60. package/dist/cloudflare.mjs +507 -0
  61. package/dist/cloudflare.mjs.map +1 -0
  62. package/dist/ctime-backfill.cjs +272 -0
  63. package/dist/ctime-backfill.d.cts +169 -0
  64. package/dist/ctime-backfill.d.cts.map +1 -0
  65. package/dist/ctime-backfill.d.mts +169 -0
  66. package/dist/ctime-backfill.d.mts.map +1 -0
  67. package/dist/ctime-backfill.mjs +267 -0
  68. package/dist/ctime-backfill.mjs.map +1 -0
  69. package/dist/d1-gc-queue.cjs +80 -0
  70. package/dist/d1-gc-queue.d.cts +59 -0
  71. package/dist/d1-gc-queue.d.cts.map +1 -0
  72. package/dist/d1-gc-queue.d.mts +59 -0
  73. package/dist/d1-gc-queue.d.mts.map +1 -0
  74. package/dist/d1-gc-queue.mjs +81 -0
  75. package/dist/d1-gc-queue.mjs.map +1 -0
  76. package/dist/d1-resolve-store.cjs +189 -0
  77. package/dist/d1-resolve-store.d.cts +93 -0
  78. package/dist/d1-resolve-store.d.cts.map +1 -0
  79. package/dist/d1-resolve-store.d.mts +93 -0
  80. package/dist/d1-resolve-store.d.mts.map +1 -0
  81. package/dist/d1-resolve-store.mjs +190 -0
  82. package/dist/d1-resolve-store.mjs.map +1 -0
  83. package/dist/d1-schema.cjs +361 -0
  84. package/dist/d1-schema.d.cts +237 -0
  85. package/dist/d1-schema.d.cts.map +1 -0
  86. package/dist/d1-schema.d.mts +237 -0
  87. package/dist/d1-schema.d.mts.map +1 -0
  88. package/dist/d1-schema.mjs +341 -0
  89. package/dist/d1-schema.mjs.map +1 -0
  90. package/dist/d1-share-store.cjs +0 -0
  91. package/dist/d1-share-store.d.cts +53 -0
  92. package/dist/d1-share-store.d.cts.map +1 -0
  93. package/dist/d1-share-store.d.mts +53 -0
  94. package/dist/d1-share-store.d.mts.map +1 -0
  95. package/dist/d1-share-store.mjs +0 -0
  96. package/dist/d1-share-store.mjs.map +1 -0
  97. package/dist/d1-tree-index.cjs +1098 -0
  98. package/dist/d1-tree-index.d.cts +203 -0
  99. package/dist/d1-tree-index.d.cts.map +1 -0
  100. package/dist/d1-tree-index.d.mts +203 -0
  101. package/dist/d1-tree-index.d.mts.map +1 -0
  102. package/dist/d1-tree-index.mjs +1094 -0
  103. package/dist/d1-tree-index.mjs.map +1 -0
  104. package/dist/data-backend.cjs +147 -0
  105. package/dist/data-backend.d.cts +30 -0
  106. package/dist/data-backend.d.cts.map +1 -0
  107. package/dist/data-backend.d.mts +30 -0
  108. package/dist/data-backend.d.mts.map +1 -0
  109. package/dist/data-backend.mjs +148 -0
  110. package/dist/data-backend.mjs.map +1 -0
  111. package/dist/dedup-backfill.cjs +278 -0
  112. package/dist/dedup-backfill.d.cts +165 -0
  113. package/dist/dedup-backfill.d.cts.map +1 -0
  114. package/dist/dedup-backfill.d.mts +165 -0
  115. package/dist/dedup-backfill.d.mts.map +1 -0
  116. package/dist/dedup-backfill.mjs +273 -0
  117. package/dist/dedup-backfill.mjs.map +1 -0
  118. package/dist/did-space-like.d.cts +2 -0
  119. package/dist/did-space-like.d.mts +2 -0
  120. package/dist/did-utils.cjs +44 -0
  121. package/dist/did-utils.d.cts +25 -0
  122. package/dist/did-utils.d.cts.map +1 -0
  123. package/dist/did-utils.d.mts +25 -0
  124. package/dist/did-utils.d.mts.map +1 -0
  125. package/dist/did-utils.mjs +43 -0
  126. package/dist/did-utils.mjs.map +1 -0
  127. package/dist/dirty-path-queue.cjs +31 -0
  128. package/dist/dirty-path-queue.d.cts +104 -0
  129. package/dist/dirty-path-queue.d.cts.map +1 -0
  130. package/dist/dirty-path-queue.d.mts +104 -0
  131. package/dist/dirty-path-queue.d.mts.map +1 -0
  132. package/dist/dirty-path-queue.mjs +31 -0
  133. package/dist/dirty-path-queue.mjs.map +1 -0
  134. package/dist/dirty-path-schema.cjs +142 -0
  135. package/dist/dirty-path-schema.d.cts +90 -0
  136. package/dist/dirty-path-schema.d.cts.map +1 -0
  137. package/dist/dirty-path-schema.d.mts +90 -0
  138. package/dist/dirty-path-schema.d.mts.map +1 -0
  139. package/dist/dirty-path-schema.mjs +136 -0
  140. package/dist/dirty-path-schema.mjs.map +1 -0
  141. package/dist/download-actions.cjs +43 -0
  142. package/dist/download-actions.mjs +40 -0
  143. package/dist/download-actions.mjs.map +1 -0
  144. package/dist/fs-object-store.cjs +153 -0
  145. package/dist/fs-object-store.d.cts +21 -0
  146. package/dist/fs-object-store.d.cts.map +1 -0
  147. package/dist/fs-object-store.d.mts +22 -0
  148. package/dist/fs-object-store.d.mts.map +1 -0
  149. package/dist/fs-object-store.mjs +153 -0
  150. package/dist/fs-object-store.mjs.map +1 -0
  151. package/dist/gc-queue.d.cts +43 -0
  152. package/dist/gc-queue.d.cts.map +1 -0
  153. package/dist/gc-queue.d.mts +43 -0
  154. package/dist/gc-queue.d.mts.map +1 -0
  155. package/dist/index.cjs +2712 -0
  156. package/dist/index.d.cts +504 -0
  157. package/dist/index.d.cts.map +1 -0
  158. package/dist/index.d.mts +505 -0
  159. package/dist/index.d.mts.map +1 -0
  160. package/dist/index.mjs +2537 -0
  161. package/dist/index.mjs.map +1 -0
  162. package/dist/inline-content-migration.cjs +212 -0
  163. package/dist/inline-content-migration.d.cts +110 -0
  164. package/dist/inline-content-migration.d.cts.map +1 -0
  165. package/dist/inline-content-migration.d.mts +111 -0
  166. package/dist/inline-content-migration.d.mts.map +1 -0
  167. package/dist/inline-content-migration.mjs +207 -0
  168. package/dist/inline-content-migration.mjs.map +1 -0
  169. package/dist/inline-content.cjs +145 -0
  170. package/dist/inline-content.d.cts +98 -0
  171. package/dist/inline-content.d.cts.map +1 -0
  172. package/dist/inline-content.d.mts +98 -0
  173. package/dist/inline-content.d.mts.map +1 -0
  174. package/dist/inline-content.mjs +137 -0
  175. package/dist/inline-content.mjs.map +1 -0
  176. package/dist/local-changelog.cjs +241 -0
  177. package/dist/local-changelog.d.cts +119 -0
  178. package/dist/local-changelog.d.cts.map +1 -0
  179. package/dist/local-changelog.d.mts +119 -0
  180. package/dist/local-changelog.d.mts.map +1 -0
  181. package/dist/local-changelog.mjs +240 -0
  182. package/dist/local-changelog.mjs.map +1 -0
  183. package/dist/local.cjs +435 -0
  184. package/dist/local.d.cts +254 -0
  185. package/dist/local.d.cts.map +1 -0
  186. package/dist/local.d.mts +254 -0
  187. package/dist/local.d.mts.map +1 -0
  188. package/dist/local.mjs +427 -0
  189. package/dist/local.mjs.map +1 -0
  190. package/dist/memory-gc-queue.cjs +36 -0
  191. package/dist/memory-gc-queue.d.cts +22 -0
  192. package/dist/memory-gc-queue.d.cts.map +1 -0
  193. package/dist/memory-gc-queue.d.mts +22 -0
  194. package/dist/memory-gc-queue.d.mts.map +1 -0
  195. package/dist/memory-gc-queue.mjs +36 -0
  196. package/dist/memory-gc-queue.mjs.map +1 -0
  197. package/dist/memory-tree-index.cjs +734 -0
  198. package/dist/memory-tree-index.d.cts +159 -0
  199. package/dist/memory-tree-index.d.cts.map +1 -0
  200. package/dist/memory-tree-index.d.mts +159 -0
  201. package/dist/memory-tree-index.d.mts.map +1 -0
  202. package/dist/memory-tree-index.mjs +735 -0
  203. package/dist/memory-tree-index.mjs.map +1 -0
  204. package/dist/metadata-db.cjs +562 -0
  205. package/dist/metadata-db.d.cts +102 -0
  206. package/dist/metadata-db.d.cts.map +1 -0
  207. package/dist/metadata-db.d.mts +102 -0
  208. package/dist/metadata-db.d.mts.map +1 -0
  209. package/dist/metadata-db.mjs +562 -0
  210. package/dist/metadata-db.mjs.map +1 -0
  211. package/dist/mime-utils.cjs +2 -0
  212. package/dist/mime-utils.d.cts +2 -0
  213. package/dist/mime-utils.d.mts +2 -0
  214. package/dist/mime-utils.mjs +3 -0
  215. package/dist/object-key.cjs +41 -0
  216. package/dist/object-key.d.cts +25 -0
  217. package/dist/object-key.d.cts.map +1 -0
  218. package/dist/object-key.d.mts +25 -0
  219. package/dist/object-key.d.mts.map +1 -0
  220. package/dist/object-key.mjs +40 -0
  221. package/dist/object-key.mjs.map +1 -0
  222. package/dist/packages/session/dist/index.cjs +788 -0
  223. package/dist/packages/session/dist/index.mjs +782 -0
  224. package/dist/packages/session/dist/index.mjs.map +1 -0
  225. package/dist/platform/r2/dist/types.d.cts +49 -0
  226. package/dist/platform/r2/dist/types.d.cts.map +1 -0
  227. package/dist/prefix-range.cjs +33 -0
  228. package/dist/prefix-range.d.cts +29 -0
  229. package/dist/prefix-range.d.cts.map +1 -0
  230. package/dist/prefix-range.d.mts +29 -0
  231. package/dist/prefix-range.d.mts.map +1 -0
  232. package/dist/prefix-range.mjs +33 -0
  233. package/dist/prefix-range.mjs.map +1 -0
  234. package/dist/prefixed-tree-index.cjs +238 -0
  235. package/dist/prefixed-tree-index.d.cts +85 -0
  236. package/dist/prefixed-tree-index.d.cts.map +1 -0
  237. package/dist/prefixed-tree-index.d.mts +85 -0
  238. package/dist/prefixed-tree-index.d.mts.map +1 -0
  239. package/dist/prefixed-tree-index.mjs +239 -0
  240. package/dist/prefixed-tree-index.mjs.map +1 -0
  241. package/dist/presign-signer.cjs +7 -0
  242. package/dist/presign-signer.d.cts +99 -0
  243. package/dist/presign-signer.d.cts.map +1 -0
  244. package/dist/presign-signer.d.mts +99 -0
  245. package/dist/presign-signer.d.mts.map +1 -0
  246. package/dist/presign-signer.mjs +7 -0
  247. package/dist/presign-signer.mjs.map +1 -0
  248. package/dist/providers/platform/r2/dist/index.d.mts +3 -0
  249. package/dist/providers/platform/r2/dist/types.d.mts +49 -0
  250. package/dist/providers/platform/r2/dist/types.d.mts.map +1 -0
  251. package/dist/reconcile.cjs +184 -0
  252. package/dist/reconcile.d.cts +26 -0
  253. package/dist/reconcile.d.cts.map +1 -0
  254. package/dist/reconcile.d.mts +26 -0
  255. package/dist/reconcile.d.mts.map +1 -0
  256. package/dist/reconcile.mjs +184 -0
  257. package/dist/reconcile.mjs.map +1 -0
  258. package/dist/reference-check.cjs +49 -0
  259. package/dist/reference-check.d.cts +10 -0
  260. package/dist/reference-check.d.cts.map +1 -0
  261. package/dist/reference-check.d.mts +10 -0
  262. package/dist/reference-check.d.mts.map +1 -0
  263. package/dist/reference-check.mjs +48 -0
  264. package/dist/reference-check.mjs.map +1 -0
  265. package/dist/remote-afs.cjs +681 -0
  266. package/dist/remote-afs.d.cts +236 -0
  267. package/dist/remote-afs.d.cts.map +1 -0
  268. package/dist/remote-afs.d.mts +236 -0
  269. package/dist/remote-afs.d.mts.map +1 -0
  270. package/dist/remote-afs.mjs +676 -0
  271. package/dist/remote-afs.mjs.map +1 -0
  272. package/dist/rename-guard.cjs +18 -0
  273. package/dist/rename-guard.mjs +18 -0
  274. package/dist/rename-guard.mjs.map +1 -0
  275. package/dist/resolve.cjs +260 -0
  276. package/dist/resolve.d.cts +113 -0
  277. package/dist/resolve.d.cts.map +1 -0
  278. package/dist/resolve.d.mts +113 -0
  279. package/dist/resolve.d.mts.map +1 -0
  280. package/dist/resolve.mjs +260 -0
  281. package/dist/resolve.mjs.map +1 -0
  282. package/dist/rsync-content.cjs +33 -0
  283. package/dist/rsync-content.mjs +33 -0
  284. package/dist/rsync-content.mjs.map +1 -0
  285. package/dist/rsync.cjs +400 -0
  286. package/dist/rsync.d.cts +157 -0
  287. package/dist/rsync.d.cts.map +1 -0
  288. package/dist/rsync.d.mts +157 -0
  289. package/dist/rsync.d.mts.map +1 -0
  290. package/dist/rsync.mjs +397 -0
  291. package/dist/rsync.mjs.map +1 -0
  292. package/dist/run-gc.cjs +57 -0
  293. package/dist/run-gc.d.cts +40 -0
  294. package/dist/run-gc.d.cts.map +1 -0
  295. package/dist/run-gc.d.mts +41 -0
  296. package/dist/run-gc.d.mts.map +1 -0
  297. package/dist/run-gc.mjs +57 -0
  298. package/dist/run-gc.mjs.map +1 -0
  299. package/dist/scope-id.cjs +70 -0
  300. package/dist/scope-id.d.cts +35 -0
  301. package/dist/scope-id.d.cts.map +1 -0
  302. package/dist/scope-id.d.mts +35 -0
  303. package/dist/scope-id.d.mts.map +1 -0
  304. package/dist/scope-id.mjs +68 -0
  305. package/dist/scope-id.mjs.map +1 -0
  306. package/dist/scope-migration-node.cjs +172 -0
  307. package/dist/scope-migration-node.d.cts +44 -0
  308. package/dist/scope-migration-node.d.cts.map +1 -0
  309. package/dist/scope-migration-node.d.mts +44 -0
  310. package/dist/scope-migration-node.d.mts.map +1 -0
  311. package/dist/scope-migration-node.mjs +171 -0
  312. package/dist/scope-migration-node.mjs.map +1 -0
  313. package/dist/scope-migration.cjs +155 -0
  314. package/dist/scope-migration.d.cts +65 -0
  315. package/dist/scope-migration.d.cts.map +1 -0
  316. package/dist/scope-migration.d.mts +65 -0
  317. package/dist/scope-migration.d.mts.map +1 -0
  318. package/dist/scope-migration.mjs +154 -0
  319. package/dist/scope-migration.mjs.map +1 -0
  320. package/dist/share-declaration.cjs +225 -0
  321. package/dist/share-declaration.d.cts +30 -0
  322. package/dist/share-declaration.d.cts.map +1 -0
  323. package/dist/share-declaration.d.mts +32 -0
  324. package/dist/share-declaration.d.mts.map +1 -0
  325. package/dist/share-declaration.mjs +221 -0
  326. package/dist/share-declaration.mjs.map +1 -0
  327. package/dist/share-hook.cjs +63 -0
  328. package/dist/share-hook.mjs +62 -0
  329. package/dist/share-hook.mjs.map +1 -0
  330. package/dist/share-resolve.cjs +176 -0
  331. package/dist/share-resolve.d.cts +36 -0
  332. package/dist/share-resolve.d.cts.map +1 -0
  333. package/dist/share-resolve.d.mts +36 -0
  334. package/dist/share-resolve.d.mts.map +1 -0
  335. package/dist/share-resolve.mjs +177 -0
  336. package/dist/share-resolve.mjs.map +1 -0
  337. package/dist/share-serve.cjs +145 -0
  338. package/dist/share-serve.d.cts +30 -0
  339. package/dist/share-serve.d.cts.map +1 -0
  340. package/dist/share-serve.d.mts +30 -0
  341. package/dist/share-serve.d.mts.map +1 -0
  342. package/dist/share-serve.mjs +144 -0
  343. package/dist/share-serve.mjs.map +1 -0
  344. package/dist/share-store.cjs +25 -0
  345. package/dist/share-store.d.cts +65 -0
  346. package/dist/share-store.d.cts.map +1 -0
  347. package/dist/share-store.d.mts +65 -0
  348. package/dist/share-store.d.mts.map +1 -0
  349. package/dist/share-store.mjs +25 -0
  350. package/dist/share-store.mjs.map +1 -0
  351. package/dist/share-virtual-path.cjs +102 -0
  352. package/dist/share-virtual-path.mjs +101 -0
  353. package/dist/share-virtual-path.mjs.map +1 -0
  354. package/dist/space-resolver.cjs +54 -0
  355. package/dist/space-resolver.d.cts +34 -0
  356. package/dist/space-resolver.d.cts.map +1 -0
  357. package/dist/space-resolver.d.mts +34 -0
  358. package/dist/space-resolver.d.mts.map +1 -0
  359. package/dist/space-resolver.mjs +55 -0
  360. package/dist/space-resolver.mjs.map +1 -0
  361. package/dist/sqlite-gc-queue.cjs +51 -0
  362. package/dist/sqlite-gc-queue.d.cts +22 -0
  363. package/dist/sqlite-gc-queue.d.cts.map +1 -0
  364. package/dist/sqlite-gc-queue.d.mts +22 -0
  365. package/dist/sqlite-gc-queue.d.mts.map +1 -0
  366. package/dist/sqlite-gc-queue.mjs +52 -0
  367. package/dist/sqlite-gc-queue.mjs.map +1 -0
  368. package/dist/sqlite-resolve-store.cjs +163 -0
  369. package/dist/sqlite-resolve-store.d.cts +50 -0
  370. package/dist/sqlite-resolve-store.d.cts.map +1 -0
  371. package/dist/sqlite-resolve-store.d.mts +50 -0
  372. package/dist/sqlite-resolve-store.d.mts.map +1 -0
  373. package/dist/sqlite-resolve-store.mjs +164 -0
  374. package/dist/sqlite-resolve-store.mjs.map +1 -0
  375. package/dist/sqlite-share-store.cjs +157 -0
  376. package/dist/sqlite-share-store.d.cts +31 -0
  377. package/dist/sqlite-share-store.d.cts.map +1 -0
  378. package/dist/sqlite-share-store.d.mts +31 -0
  379. package/dist/sqlite-share-store.d.mts.map +1 -0
  380. package/dist/sqlite-share-store.mjs +157 -0
  381. package/dist/sqlite-share-store.mjs.map +1 -0
  382. package/dist/sqlite-tree-index.cjs +901 -0
  383. package/dist/sqlite-tree-index.d.cts +107 -0
  384. package/dist/sqlite-tree-index.d.cts.map +1 -0
  385. package/dist/sqlite-tree-index.d.mts +107 -0
  386. package/dist/sqlite-tree-index.d.mts.map +1 -0
  387. package/dist/sqlite-tree-index.mjs +902 -0
  388. package/dist/sqlite-tree-index.mjs.map +1 -0
  389. package/dist/sync-engine.cjs +1324 -0
  390. package/dist/sync-engine.d.cts +473 -0
  391. package/dist/sync-engine.d.cts.map +1 -0
  392. package/dist/sync-engine.d.mts +473 -0
  393. package/dist/sync-engine.d.mts.map +1 -0
  394. package/dist/sync-engine.mjs +1315 -0
  395. package/dist/sync-engine.mjs.map +1 -0
  396. package/dist/sync-state.cjs +303 -0
  397. package/dist/sync-state.d.cts +224 -0
  398. package/dist/sync-state.d.cts.map +1 -0
  399. package/dist/sync-state.d.mts +224 -0
  400. package/dist/sync-state.d.mts.map +1 -0
  401. package/dist/sync-state.mjs +297 -0
  402. package/dist/sync-state.mjs.map +1 -0
  403. package/dist/timestamp.cjs +92 -0
  404. package/dist/timestamp.mjs +91 -0
  405. package/dist/timestamp.mjs.map +1 -0
  406. package/dist/transports/http.cjs +54 -0
  407. package/dist/transports/http.d.cts +26 -0
  408. package/dist/transports/http.d.cts.map +1 -0
  409. package/dist/transports/http.d.mts +26 -0
  410. package/dist/transports/http.d.mts.map +1 -0
  411. package/dist/transports/http.mjs +54 -0
  412. package/dist/transports/http.mjs.map +1 -0
  413. package/dist/transports/static.cjs +55 -0
  414. package/dist/transports/static.d.cts +28 -0
  415. package/dist/transports/static.d.cts.map +1 -0
  416. package/dist/transports/static.d.mts +28 -0
  417. package/dist/transports/static.d.mts.map +1 -0
  418. package/dist/transports/static.mjs +55 -0
  419. package/dist/transports/static.mjs.map +1 -0
  420. package/dist/transports/types.cjs +13 -0
  421. package/dist/transports/types.d.cts +72 -0
  422. package/dist/transports/types.d.cts.map +1 -0
  423. package/dist/transports/types.d.mts +72 -0
  424. package/dist/transports/types.d.mts.map +1 -0
  425. package/dist/transports/types.mjs +13 -0
  426. package/dist/transports/types.mjs.map +1 -0
  427. package/dist/transports/ws.cjs +207 -0
  428. package/dist/transports/ws.d.cts +47 -0
  429. package/dist/transports/ws.d.cts.map +1 -0
  430. package/dist/transports/ws.d.mts +47 -0
  431. package/dist/transports/ws.d.mts.map +1 -0
  432. package/dist/transports/ws.mjs +206 -0
  433. package/dist/transports/ws.mjs.map +1 -0
  434. package/dist/tree-index.cjs +333 -0
  435. package/dist/tree-index.d.cts +524 -0
  436. package/dist/tree-index.d.cts.map +1 -0
  437. package/dist/tree-index.d.mts +524 -0
  438. package/dist/tree-index.d.mts.map +1 -0
  439. package/dist/tree-index.mjs +322 -0
  440. package/dist/tree-index.mjs.map +1 -0
  441. package/dist/tree-query.cjs +655 -0
  442. package/dist/tree-query.d.cts +70 -0
  443. package/dist/tree-query.d.cts.map +1 -0
  444. package/dist/tree-query.d.mts +70 -0
  445. package/dist/tree-query.d.mts.map +1 -0
  446. package/dist/tree-query.mjs +648 -0
  447. package/dist/tree-query.mjs.map +1 -0
  448. package/dist/upload-actions.cjs +97 -0
  449. package/dist/upload-actions.d.cts +19 -0
  450. package/dist/upload-actions.d.cts.map +1 -0
  451. package/dist/upload-actions.d.mts +21 -0
  452. package/dist/upload-actions.d.mts.map +1 -0
  453. package/dist/upload-actions.mjs +90 -0
  454. package/dist/upload-actions.mjs.map +1 -0
  455. package/dist/upload-session-store.cjs +185 -0
  456. package/dist/upload-session-store.d.cts +99 -0
  457. package/dist/upload-session-store.d.cts.map +1 -0
  458. package/dist/upload-session-store.d.mts +99 -0
  459. package/dist/upload-session-store.d.mts.map +1 -0
  460. package/dist/upload-session-store.mjs +184 -0
  461. package/dist/upload-session-store.mjs.map +1 -0
  462. package/dist/url-normalize.cjs +61 -0
  463. package/dist/url-normalize.d.cts +33 -0
  464. package/dist/url-normalize.d.cts.map +1 -0
  465. package/dist/url-normalize.d.mts +33 -0
  466. package/dist/url-normalize.d.mts.map +1 -0
  467. package/dist/url-normalize.mjs +61 -0
  468. package/dist/url-normalize.mjs.map +1 -0
  469. package/dist/versioned-content-backends.cjs +155 -0
  470. package/dist/versioned-content-backends.d.cts +49 -0
  471. package/dist/versioned-content-backends.d.cts.map +1 -0
  472. package/dist/versioned-content-backends.d.mts +49 -0
  473. package/dist/versioned-content-backends.d.mts.map +1 -0
  474. package/dist/versioned-content-backends.mjs +154 -0
  475. package/dist/versioned-content-backends.mjs.map +1 -0
  476. package/dist/versioned-content-index.cjs +102 -0
  477. package/dist/versioned-content-index.d.cts +46 -0
  478. package/dist/versioned-content-index.d.cts.map +1 -0
  479. package/dist/versioned-content-index.d.mts +46 -0
  480. package/dist/versioned-content-index.d.mts.map +1 -0
  481. package/dist/versioned-content-index.mjs +103 -0
  482. package/dist/versioned-content-index.mjs.map +1 -0
  483. package/dist/versioned-content-store.cjs +121 -0
  484. package/dist/versioned-content-store.d.cts +90 -0
  485. package/dist/versioned-content-store.d.cts.map +1 -0
  486. package/dist/versioned-content-store.d.mts +90 -0
  487. package/dist/versioned-content-store.d.mts.map +1 -0
  488. package/dist/versioned-content-store.mjs +122 -0
  489. package/dist/versioned-content-store.mjs.map +1 -0
  490. package/dist/versioned-scope.cjs +39 -0
  491. package/dist/versioned-scope.d.cts +57 -0
  492. package/dist/versioned-scope.d.cts.map +1 -0
  493. package/dist/versioned-scope.d.mts +57 -0
  494. package/dist/versioned-scope.d.mts.map +1 -0
  495. package/dist/versioned-scope.mjs +37 -0
  496. package/dist/versioned-scope.mjs.map +1 -0
  497. package/migrations/0001_tree_entries.sql +17 -0
  498. package/migrations/0002_resolve_entries.sql +7 -0
  499. package/migrations/0003_entries_v2.sql +38 -0
  500. package/migrations/0004_gc_queue.sql +5 -0
  501. package/migrations/0006_resolve_enhance.sql +14 -0
  502. package/migrations/0007_resolve_created_at.sql +10 -0
  503. package/migrations/0008_changelog.sql +34 -0
  504. package/migrations/0009_device_sync_state.sql +29 -0
  505. package/migrations/0010_scope_meta.sql +33 -0
  506. package/migrations/0011_shares.sql +25 -0
  507. package/migrations/0012_scope_meta_file_count.sql +41 -0
  508. package/migrations/0013_scope_meta_total_bytes.sql +47 -0
  509. package/migrations/0014_rename_tables.sql +62 -0
  510. package/package.json +102 -0
@@ -0,0 +1,102 @@
1
+ import { PutOpts } from "./tree-index.mjs";
2
+ import * as _libsql_client0 from "@libsql/client";
3
+
4
+ //#region src/metadata-db.d.ts
5
+ interface EntryRow {
6
+ scope: string;
7
+ path: string;
8
+ parent: string;
9
+ name: string;
10
+ kind: "file" | "dir";
11
+ cid: string | null;
12
+ size: number;
13
+ mime_type: string | null;
14
+ ctime: number;
15
+ mtime: number;
16
+ }
17
+ interface CidCacheRow {
18
+ path: string;
19
+ cid: string;
20
+ size: number;
21
+ mtime_ms: number;
22
+ computed_at: number;
23
+ }
24
+ interface CheckpointRow {
25
+ session_id: string;
26
+ direction: string;
27
+ remote_url: string;
28
+ base_path: string;
29
+ status: string;
30
+ diff_json: string;
31
+ /** JSON-encoded string[] of completed file paths */
32
+ completed: string;
33
+ created_at: number;
34
+ updated_at: number;
35
+ }
36
+ declare class MetadataDB {
37
+ private client;
38
+ readonly dbPath: string;
39
+ private constructor();
40
+ /**
41
+ * Internal: the raw libsql client, for `SQLiteTreeIndex` to run the same
42
+ * atomic batches / queries D1TreeIndex runs on D1 (shared `entries` +
43
+ * `changelog` tables). Kept package-internal — application code must go
44
+ * through AFS / the TreeIndex interface, never this client directly.
45
+ */
46
+ get rawClient(): _libsql_client0.Client;
47
+ /**
48
+ * Open (or create) _metadata.db in the given directory.
49
+ * Creates the directory if it does not exist.
50
+ *
51
+ * Executes the core schema (entries / cid_cache / sync_checkpoint) AND the
52
+ * Phase 2 changelog table DDL. Changelog DDL is imported from
53
+ * `changelog-schema.ts` so that D1, desktop SQLite, and the Swift/Kotlin
54
+ * runtimes share byte-identical statements (verified by the CI parity
55
+ * check).
56
+ */
57
+ static open(userDir: string): Promise<MetadataDB>;
58
+ /** Return all file entries (kind='file') for the given scope. */
59
+ getFiles(scope: string, basePath?: string): Promise<EntryRow[]>;
60
+ /**
61
+ * Read the current cid for (scope, path). Internal helper for atomic
62
+ * changelog writes — callers need the old_cid to populate the changelog
63
+ * row. Returns null if the row does not exist OR if its cid is null
64
+ * (directory row).
65
+ */
66
+ private getCurrentCid;
67
+ /**
68
+ * Upsert a single entry. Writes `entries` + `changelog` atomically via libsql batch.
69
+ *
70
+ * Directory rows (kind='dir') and rows with cid=null never produce a
71
+ * changelog entry — the changelog tracks file content mutations only.
72
+ */
73
+ upsertEntry(row: EntryRow, opts: PutOpts): Promise<void>;
74
+ /** Upsert multiple entries in a single atomic transaction. */
75
+ upsertEntries(rows: EntryRow[], opts: PutOpts): Promise<void>;
76
+ /** Delete an entry by scope + path. Writes changelog delete row atomically. */
77
+ deleteEntry(scope: string, path: string, opts: PutOpts): Promise<void>;
78
+ /** Delete all file entries for a scope whose paths are NOT in the given set. */
79
+ deleteEntriesNotIn(scope: string, keepPaths: Set<string>, opts: PutOpts): Promise<void>;
80
+ /** Get CID cache entry for a filesystem path. Returns null on cache miss. */
81
+ getCidCache(path: string): Promise<CidCacheRow | null>;
82
+ /** Set (insert or replace) a CID cache entry. */
83
+ setCidCache(row: CidCacheRow): Promise<void>;
84
+ /**
85
+ * Get the most recent active checkpoint for the given (direction, remoteUrl, basePath).
86
+ * Returns null when no active session exists.
87
+ */
88
+ getActiveCheckpoint(direction: string, remoteUrl: string, basePath: string): Promise<CheckpointRow | null>;
89
+ /** Insert a new checkpoint record. */
90
+ createCheckpoint(row: CheckpointRow): Promise<void>;
91
+ /** Update the completed paths list for an active checkpoint. */
92
+ updateCheckpointCompleted(sessionId: string, completedPaths: string[]): Promise<void>;
93
+ /** Mark a checkpoint as done. */
94
+ completeCheckpoint(sessionId: string): Promise<void>;
95
+ /** Get a checkpoint by session_id (for test assertions). */
96
+ getCheckpoint(sessionId: string): Promise<CheckpointRow | null>;
97
+ /** Close the database connection. */
98
+ close(): void;
99
+ }
100
+ //#endregion
101
+ export { CidCacheRow, EntryRow, MetadataDB };
102
+ //# sourceMappingURL=metadata-db.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metadata-db.d.mts","names":[],"sources":["../src/metadata-db.ts"],"mappings":";;;;UA6CiB,QAAA;EACf,KAAA;EACA,IAAA;EACA,MAAA;EACA,IAAA;EACA,IAAA;EACA,GAAA;EACA,IAAA;EACA,SAAA;EACA,KAAA;EACA,KAAA;AAAA;AAAA,UAGe,WAAA;EACf,IAAA;EACA,GAAA;EACA,IAAA;EACA,QAAA;EACA,WAAA;AAAA;AAAA,UAGe,aAAA;EACf,UAAA;EACA,SAAA;EACA,UAAA;EACA,SAAA;EACA,MAAA;EACA,SAAA;EADA;EAGA,SAAA;EACA,UAAA;EACA,UAAA;AAAA;AAAA,cA0GW,UAAA;EAAA,QACH,MAAA;EAAA,SACC,MAAA;EAAA,QAEF,WAAA,CAAA;;;;;;;MAWH,SAAA,CAAA,GAfiB,eAAA,CAeqB,MAAA;EAmOH;;;;;;;;;;EAAA,OArN1B,IAAA,CAAK,OAAA,WAAkB,OAAA,CAAQ,UAAA;EAmaX;EAvP3B,QAAA,CAAS,KAAA,UAAe,QAAA,YAAoB,OAAA,CAAQ,QAAA;EAiQrB;;;;;;EAAA,QAxOvB,aAAA;EAsSkC;;;;;;EAtR1C,WAAA,CAAY,GAAA,EAAK,QAAA,EAAU,IAAA,EAAM,OAAA,GAAU,OAAA;EAnO7C;EA+QE,aAAA,CAAc,IAAA,EAAM,QAAA,IAAY,IAAA,EAAM,OAAA,GAAU,OAAA;EAjQzC;EAqUP,WAAA,CAAY,KAAA,UAAe,IAAA,UAAc,IAAA,EAAM,OAAA,GAAU,OAAA;EArU3B;EA+V9B,kBAAA,CAAmB,KAAA,UAAe,SAAA,EAAW,GAAA,UAAa,IAAA,EAAM,OAAA,GAAU,OAAA;EAnL1E;EAuPA,WAAA,CAAY,IAAA,WAAe,OAAA,CAAQ,WAAA;EAvPX;EAiQxB,WAAA,CAAY,GAAA,EAAK,WAAA,GAAc,OAAA;EAjQqB;;;;EA+QpD,mBAAA,CACJ,SAAA,UACA,SAAA,UACA,QAAA,WACC,OAAA,CAAQ,aAAA;EA1O4B;EAoPjC,gBAAA,CAAiB,GAAA,EAAK,aAAA,GAAgB,OAAA;EApPK;EAsQ3C,yBAAA,CAA0B,SAAA,UAAmB,cAAA,aAA2B,OAAA;EA1NpD;EAkOpB,kBAAA,CAAmB,SAAA,WAAoB,OAAA;EAlOD;EA0OtC,aAAA,CAAc,SAAA,WAAoB,OAAA,CAAQ,aAAA;EA1OM;EAsPtD,KAAA,CAAA;AAAA"}
@@ -0,0 +1,562 @@
1
+ import { CHANGELOG_INDEX_SQL, CHANGELOG_TABLE_SQL, PREFIX_WATERMARK_TABLE_SQL, SYNC_STATE_INDEX_SQL, SYNC_STATE_TABLE_SQL } from "./changelog-schema.mjs";
2
+ import { ENTRIES_CONTENT_TYPE_COLUMN_SQL, ENTRIES_CONTENT_TYPE_INDEX_DROP_SQL, ENTRIES_DEDUP_INDEX_SQL, ENTRIES_DEDUP_KEY_COLUMN_SQL, ENTRIES_DEDUP_SCOPE_COLUMN_SQL, ENTRIES_INLINE_CONTENT_COLUMN_SQL, ENTRIES_METADATA_COLUMN_SQL, UPLOAD_SESSIONS_INDEX_SQL, UPLOAD_SESSIONS_TABLE_SQL, UPLOAD_SESSION_ENTRIES_TABLE_SQL } from "./d1-schema.mjs";
3
+ import { CHANGELOG_INSERT_SQL, validateDeviceId } from "./changelog-write.mjs";
4
+ import { DIRTY_PATH_INDEX_SQL, DIRTY_PATH_TABLE_SQL, DIRTY_PATH_TRIGGER_SQL } from "./dirty-path-schema.mjs";
5
+ import { renameLegacyTable } from "./rename-guard.mjs";
6
+ import { mkdirSync, rmSync } from "node:fs";
7
+ import { dirname, join } from "node:path";
8
+
9
+ //#region src/metadata-db.ts
10
+ /**
11
+ * MetadataDB — local SQLite index for DID Space files.
12
+ *
13
+ * Opened at: ~/.afs/spaces/{userDid}/_metadata.db
14
+ *
15
+ * Tables:
16
+ * entries — same schema as D1 (entries table in scalability-design §4.1)
17
+ * cid_cache — path → cid + mtime_ms for avoiding repeated hash computation
18
+ * sync_checkpoint — persistent sync state for resume-on-failure (Phase 3)
19
+ *
20
+ * SQLite tuning: WAL, cache_size 256 MB, mmap 1 GB, busy_timeout 30 s.
21
+ */
22
+ const DB_FILENAME = "_metadata.db";
23
+ const SCHEMA_SQL = `
24
+ PRAGMA journal_mode = WAL;
25
+ PRAGMA synchronous = NORMAL;
26
+ PRAGMA cache_size = -262144;
27
+ PRAGMA mmap_size = 1073741824;
28
+ PRAGMA busy_timeout = 30000;
29
+ PRAGMA wal_autocheckpoint = 10000;
30
+
31
+ CREATE TABLE IF NOT EXISTS ds_entries (
32
+ scope TEXT NOT NULL,
33
+ path TEXT NOT NULL,
34
+ parent TEXT NOT NULL,
35
+ name TEXT NOT NULL,
36
+ kind TEXT NOT NULL DEFAULT 'file',
37
+ cid TEXT,
38
+ size INTEGER NOT NULL DEFAULT 0,
39
+ mime_type TEXT,
40
+ ctime INTEGER NOT NULL,
41
+ mtime INTEGER NOT NULL,
42
+ PRIMARY KEY (scope, path),
43
+ CHECK (LENGTH(path) <= 4096),
44
+ CHECK (LENGTH(name) <= 255),
45
+ CHECK (kind IN ('file', 'dir')),
46
+ CHECK (LENGTH(scope) <= 128)
47
+ ) WITHOUT ROWID;
48
+
49
+ CREATE INDEX IF NOT EXISTS idx_entries_children
50
+ ON ds_entries(scope, parent, name);
51
+
52
+ CREATE INDEX IF NOT EXISTS idx_entries_mtime
53
+ ON ds_entries(scope, mtime DESC);
54
+
55
+ CREATE INDEX IF NOT EXISTS idx_entries_kind
56
+ ON ds_entries(scope, kind);
57
+
58
+ CREATE INDEX IF NOT EXISTS idx_entries_cid
59
+ ON ds_entries(cid) WHERE cid IS NOT NULL;
60
+
61
+ -- Ordered listChildren (did-space-sorting Phase 2): scope+parent equality,
62
+ -- mtime/ctime range order, name tiebreak — index-only ordered scans.
63
+ CREATE INDEX IF NOT EXISTS idx_entries_parent_mtime
64
+ ON ds_entries(scope, parent, mtime, name);
65
+
66
+ CREATE INDEX IF NOT EXISTS idx_entries_parent_ctime
67
+ ON ds_entries(scope, parent, ctime, name);
68
+
69
+ CREATE TABLE IF NOT EXISTS cid_cache (
70
+ path TEXT PRIMARY KEY,
71
+ cid TEXT NOT NULL,
72
+ size INTEGER NOT NULL,
73
+ mtime_ms INTEGER NOT NULL,
74
+ computed_at INTEGER NOT NULL
75
+ );
76
+
77
+ CREATE TABLE IF NOT EXISTS sync_checkpoint (
78
+ session_id TEXT PRIMARY KEY,
79
+ direction TEXT NOT NULL,
80
+ remote_url TEXT NOT NULL,
81
+ base_path TEXT NOT NULL,
82
+ status TEXT NOT NULL DEFAULT 'active',
83
+ diff_json TEXT NOT NULL,
84
+ completed TEXT NOT NULL DEFAULT '[]',
85
+ created_at INTEGER NOT NULL,
86
+ updated_at INTEGER NOT NULL
87
+ );
88
+ `;
89
+ var MetadataDB = class MetadataDB {
90
+ client;
91
+ dbPath;
92
+ constructor(client, dbPath) {
93
+ this.client = client;
94
+ this.dbPath = dbPath;
95
+ }
96
+ /**
97
+ * Internal: the raw libsql client, for `SQLiteTreeIndex` to run the same
98
+ * atomic batches / queries D1TreeIndex runs on D1 (shared `entries` +
99
+ * `changelog` tables). Kept package-internal — application code must go
100
+ * through AFS / the TreeIndex interface, never this client directly.
101
+ */
102
+ get rawClient() {
103
+ return this.client;
104
+ }
105
+ /**
106
+ * Open (or create) _metadata.db in the given directory.
107
+ * Creates the directory if it does not exist.
108
+ *
109
+ * Executes the core schema (entries / cid_cache / sync_checkpoint) AND the
110
+ * Phase 2 changelog table DDL. Changelog DDL is imported from
111
+ * `changelog-schema.ts` so that D1, desktop SQLite, and the Swift/Kotlin
112
+ * runtimes share byte-identical statements (verified by the CI parity
113
+ * check).
114
+ */
115
+ static async open(userDir) {
116
+ const dbPath = join(userDir, DB_FILENAME);
117
+ mkdirSync(dirname(dbPath), { recursive: true });
118
+ const { createClient } = await import("@libsql/client");
119
+ const statements = [
120
+ ...SCHEMA_SQL.split(";").map((s) => s.trim()).filter((s) => s.length > 0),
121
+ CHANGELOG_TABLE_SQL,
122
+ ...CHANGELOG_INDEX_SQL,
123
+ SYNC_STATE_TABLE_SQL,
124
+ ...SYNC_STATE_INDEX_SQL,
125
+ PREFIX_WATERMARK_TABLE_SQL,
126
+ DIRTY_PATH_TABLE_SQL,
127
+ ...DIRTY_PATH_INDEX_SQL,
128
+ ...DIRTY_PATH_TRIGGER_SQL,
129
+ UPLOAD_SESSIONS_TABLE_SQL,
130
+ UPLOAD_SESSION_ENTRIES_TABLE_SQL,
131
+ ...UPLOAD_SESSIONS_INDEX_SQL
132
+ ];
133
+ async function tryOpen() {
134
+ const client$1 = createClient({ url: `file:${dbPath}` });
135
+ for (const [oldName, newName] of [
136
+ ["entries", "ds_entries"],
137
+ ["changelog", "ds_changelog"],
138
+ ["gc_queue", "ds_gc_queue"],
139
+ ["sync_state", "ds_sync_state"],
140
+ ["prefix_watermark", "ds_prefix_watermark"],
141
+ ["upload_sessions", "ds_upload_sessions"],
142
+ ["upload_session_entries", "ds_upload_session_entries"],
143
+ ["versioned_scopes", "ds_versioned_scopes"],
144
+ ["scope_meta", "ds_scope_meta"],
145
+ ["device_sync_state", "ds_device_sync_state"]
146
+ ]) await renameLegacyTable(client$1, oldName, newName);
147
+ for (const stmt of statements) await client$1.execute(stmt);
148
+ const cols = await client$1.execute("SELECT name FROM pragma_table_info('ds_entries')");
149
+ const colNames = new Set(cols.rows.map((r) => String(r.name)));
150
+ if (!colNames.has("content")) try {
151
+ await client$1.execute(ENTRIES_INLINE_CONTENT_COLUMN_SQL);
152
+ } catch (err) {
153
+ if (!(err instanceof Error ? err.message : String(err)).includes("duplicate column name")) throw err;
154
+ }
155
+ if (!colNames.has("metadata")) try {
156
+ await client$1.execute(ENTRIES_METADATA_COLUMN_SQL);
157
+ } catch (err) {
158
+ if (!(err instanceof Error ? err.message : String(err)).includes("duplicate column name")) throw err;
159
+ }
160
+ if (!colNames.has("dedup_scope")) try {
161
+ await client$1.execute(ENTRIES_DEDUP_SCOPE_COLUMN_SQL);
162
+ } catch (err) {
163
+ if (!(err instanceof Error ? err.message : String(err)).includes("duplicate column name")) throw err;
164
+ }
165
+ if (!colNames.has("dedup_key")) try {
166
+ await client$1.execute(ENTRIES_DEDUP_KEY_COLUMN_SQL);
167
+ } catch (err) {
168
+ if (!(err instanceof Error ? err.message : String(err)).includes("duplicate column name")) throw err;
169
+ }
170
+ await client$1.execute(ENTRIES_DEDUP_INDEX_SQL);
171
+ if (!colNames.has("content_type")) try {
172
+ await client$1.execute(ENTRIES_CONTENT_TYPE_COLUMN_SQL);
173
+ } catch (err) {
174
+ if (!(err instanceof Error ? err.message : String(err)).includes("duplicate column name")) throw err;
175
+ }
176
+ await client$1.execute(ENTRIES_CONTENT_TYPE_INDEX_DROP_SQL);
177
+ await client$1.execute("SELECT COUNT(*) FROM ds_entries");
178
+ await client$1.execute("SELECT COUNT(*) FROM ds_changelog");
179
+ await client$1.execute("SELECT COUNT(*) FROM ds_sync_state");
180
+ await client$1.execute("SELECT COUNT(*) FROM ds_dirty_path");
181
+ return client$1;
182
+ }
183
+ let client;
184
+ try {
185
+ client = await tryOpen();
186
+ } catch (err) {
187
+ const msg = err instanceof Error ? err.message : String(err);
188
+ if (msg.includes("SQLITE_CORRUPT") || msg.includes("SQLITE_NOTADB") || msg.includes("database disk image is malformed") || msg.includes("file is not a database")) {
189
+ for (const suffix of [
190
+ "",
191
+ "-wal",
192
+ "-shm"
193
+ ]) rmSync(dbPath + suffix, { force: true });
194
+ client = await tryOpen();
195
+ } else throw err;
196
+ }
197
+ return new MetadataDB(client, dbPath);
198
+ }
199
+ /** Return all file entries (kind='file') for the given scope. */
200
+ async getFiles(scope, basePath) {
201
+ let sql;
202
+ let args;
203
+ if (basePath) {
204
+ const end = basePath.slice(0, -1) + String.fromCharCode(basePath.charCodeAt(basePath.length - 1) + 1);
205
+ sql = `SELECT * FROM ds_entries WHERE scope = ? AND kind = 'file' AND path >= ? AND path < ? ORDER BY path`;
206
+ args = [
207
+ scope,
208
+ basePath,
209
+ end
210
+ ];
211
+ } else {
212
+ sql = `SELECT * FROM ds_entries WHERE scope = ? AND kind = 'file' ORDER BY path`;
213
+ args = [scope];
214
+ }
215
+ return (await this.client.execute({
216
+ sql,
217
+ args
218
+ })).rows.map(toEntryRow);
219
+ }
220
+ /**
221
+ * Read the current cid for (scope, path). Internal helper for atomic
222
+ * changelog writes — callers need the old_cid to populate the changelog
223
+ * row. Returns null if the row does not exist OR if its cid is null
224
+ * (directory row).
225
+ */
226
+ async getCurrentCid(scope, path) {
227
+ const result = await this.client.execute({
228
+ sql: `SELECT cid FROM ds_entries WHERE scope = ? AND path = ?`,
229
+ args: [scope, path]
230
+ });
231
+ if (result.rows.length === 0) return null;
232
+ return result.rows[0][0] ?? null;
233
+ }
234
+ /**
235
+ * Upsert a single entry. Writes `entries` + `changelog` atomically via libsql batch.
236
+ *
237
+ * Directory rows (kind='dir') and rows with cid=null never produce a
238
+ * changelog entry — the changelog tracks file content mutations only.
239
+ */
240
+ async upsertEntry(row, opts) {
241
+ validateDeviceId(opts.deviceId);
242
+ const oldCid = !opts.skipChangelog && row.kind === "file" ? await this.getCurrentCid(row.scope, row.path) : null;
243
+ const stmts = [{
244
+ sql: `INSERT OR REPLACE INTO ds_entries
245
+ (scope, path, parent, name, kind, cid, size, mime_type, ctime, mtime)
246
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
247
+ args: [
248
+ row.scope,
249
+ row.path,
250
+ row.parent,
251
+ row.name,
252
+ row.kind,
253
+ row.cid ?? null,
254
+ row.size,
255
+ row.mime_type ?? null,
256
+ row.ctime,
257
+ row.mtime
258
+ ]
259
+ }];
260
+ if (!opts.skipChangelog && row.kind === "file" && row.cid) stmts.push({
261
+ sql: CHANGELOG_INSERT_SQL,
262
+ args: [
263
+ row.scope,
264
+ row.path,
265
+ "put",
266
+ row.cid,
267
+ oldCid,
268
+ row.size,
269
+ row.mtime,
270
+ opts.deviceId
271
+ ]
272
+ });
273
+ await this.client.batch(stmts, "write");
274
+ }
275
+ /** Upsert multiple entries in a single atomic transaction. */
276
+ async upsertEntries(rows, opts) {
277
+ if (rows.length === 0) return;
278
+ validateDeviceId(opts.deviceId);
279
+ const oldCidMap = /* @__PURE__ */ new Map();
280
+ if (!opts.skipChangelog) {
281
+ const byScope = /* @__PURE__ */ new Map();
282
+ for (const r of rows) {
283
+ if (r.kind !== "file") continue;
284
+ const list = byScope.get(r.scope) ?? [];
285
+ list.push(r.path);
286
+ byScope.set(r.scope, list);
287
+ }
288
+ for (const [scope, paths] of byScope) {
289
+ if (paths.length === 0) continue;
290
+ const placeholders = paths.map(() => "?").join(",");
291
+ const result = await this.client.execute({
292
+ sql: `SELECT path, cid FROM ds_entries WHERE scope = ? AND path IN (${placeholders}) AND cid IS NOT NULL`,
293
+ args: [scope, ...paths]
294
+ });
295
+ for (const r of result.rows) {
296
+ const path = r[0];
297
+ const cid = r[1];
298
+ if (cid) oldCidMap.set(`${scope}\x00${path}`, cid);
299
+ }
300
+ }
301
+ }
302
+ const stmts = [];
303
+ for (const row of rows) {
304
+ stmts.push({
305
+ sql: `INSERT OR REPLACE INTO ds_entries
306
+ (scope, path, parent, name, kind, cid, size, mime_type, ctime, mtime)
307
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
308
+ args: [
309
+ row.scope,
310
+ row.path,
311
+ row.parent,
312
+ row.name,
313
+ row.kind,
314
+ row.cid ?? null,
315
+ row.size,
316
+ row.mime_type ?? null,
317
+ row.ctime,
318
+ row.mtime
319
+ ]
320
+ });
321
+ if (!opts.skipChangelog && row.kind === "file" && row.cid) {
322
+ const oldCid = oldCidMap.get(`${row.scope}\x00${row.path}`) ?? null;
323
+ stmts.push({
324
+ sql: CHANGELOG_INSERT_SQL,
325
+ args: [
326
+ row.scope,
327
+ row.path,
328
+ "put",
329
+ row.cid,
330
+ oldCid,
331
+ row.size,
332
+ row.mtime,
333
+ opts.deviceId
334
+ ]
335
+ });
336
+ }
337
+ }
338
+ await this.client.batch(stmts, "write");
339
+ }
340
+ /** Delete an entry by scope + path. Writes changelog delete row atomically. */
341
+ async deleteEntry(scope, path, opts) {
342
+ validateDeviceId(opts.deviceId);
343
+ const oldCid = opts.skipChangelog ? null : await this.getCurrentCid(scope, path);
344
+ const stmts = [{
345
+ sql: `DELETE FROM ds_entries WHERE scope = ? AND path = ?`,
346
+ args: [scope, path]
347
+ }];
348
+ if (!opts.skipChangelog && oldCid) stmts.push({
349
+ sql: CHANGELOG_INSERT_SQL,
350
+ args: [
351
+ scope,
352
+ path,
353
+ "delete",
354
+ null,
355
+ oldCid,
356
+ null,
357
+ Date.now(),
358
+ opts.deviceId
359
+ ]
360
+ });
361
+ await this.client.batch(stmts, "write");
362
+ }
363
+ /** Delete all file entries for a scope whose paths are NOT in the given set. */
364
+ async deleteEntriesNotIn(scope, keepPaths, opts) {
365
+ validateDeviceId(opts.deviceId);
366
+ if (keepPaths.size === 0) {
367
+ let filesToLog = [];
368
+ if (!opts.skipChangelog) filesToLog = (await this.client.execute({
369
+ sql: `SELECT path, cid FROM ds_entries WHERE scope = ? AND kind = 'file' AND cid IS NOT NULL`,
370
+ args: [scope]
371
+ })).rows.map((r) => ({
372
+ path: r[0],
373
+ cid: r[1]
374
+ }));
375
+ const stmts$1 = [{
376
+ sql: `DELETE FROM ds_entries WHERE scope = ?`,
377
+ args: [scope]
378
+ }];
379
+ const now$1 = Date.now();
380
+ for (const row of filesToLog) stmts$1.push({
381
+ sql: CHANGELOG_INSERT_SQL,
382
+ args: [
383
+ scope,
384
+ row.path,
385
+ "delete",
386
+ null,
387
+ row.cid,
388
+ null,
389
+ now$1,
390
+ opts.deviceId
391
+ ]
392
+ });
393
+ await this.client.batch(stmts$1, "write");
394
+ return;
395
+ }
396
+ const result = await this.client.execute({
397
+ sql: `SELECT path, cid FROM ds_entries WHERE scope = ? AND kind = 'file'`,
398
+ args: [scope]
399
+ });
400
+ const toDelete = [];
401
+ for (const r of result.rows) {
402
+ const path = r[0];
403
+ const cid = r[1];
404
+ if (!keepPaths.has(path)) toDelete.push({
405
+ path,
406
+ cid
407
+ });
408
+ }
409
+ if (toDelete.length === 0) return;
410
+ const stmts = [];
411
+ const now = Date.now();
412
+ for (const row of toDelete) {
413
+ stmts.push({
414
+ sql: `DELETE FROM ds_entries WHERE scope = ? AND path = ?`,
415
+ args: [scope, row.path]
416
+ });
417
+ if (!opts.skipChangelog && row.cid) stmts.push({
418
+ sql: CHANGELOG_INSERT_SQL,
419
+ args: [
420
+ scope,
421
+ row.path,
422
+ "delete",
423
+ null,
424
+ row.cid,
425
+ null,
426
+ now,
427
+ opts.deviceId
428
+ ]
429
+ });
430
+ }
431
+ await this.client.batch(stmts, "write");
432
+ }
433
+ /** Get CID cache entry for a filesystem path. Returns null on cache miss. */
434
+ async getCidCache(path) {
435
+ const result = await this.client.execute({
436
+ sql: `SELECT path, cid, size, mtime_ms, computed_at FROM cid_cache WHERE path = ?`,
437
+ args: [path]
438
+ });
439
+ if (result.rows.length === 0) return null;
440
+ return toCidCacheRow(result.rows[0]);
441
+ }
442
+ /** Set (insert or replace) a CID cache entry. */
443
+ async setCidCache(row) {
444
+ await this.client.execute({
445
+ sql: `INSERT OR REPLACE INTO cid_cache (path, cid, size, mtime_ms, computed_at)
446
+ VALUES (?, ?, ?, ?, ?)`,
447
+ args: [
448
+ row.path,
449
+ row.cid,
450
+ row.size,
451
+ row.mtime_ms,
452
+ row.computed_at
453
+ ]
454
+ });
455
+ }
456
+ /**
457
+ * Get the most recent active checkpoint for the given (direction, remoteUrl, basePath).
458
+ * Returns null when no active session exists.
459
+ */
460
+ async getActiveCheckpoint(direction, remoteUrl, basePath) {
461
+ const result = await this.client.execute({
462
+ sql: `SELECT * FROM sync_checkpoint WHERE direction = ? AND remote_url = ? AND base_path = ? AND status = 'active' ORDER BY created_at DESC LIMIT 1`,
463
+ args: [
464
+ direction,
465
+ remoteUrl,
466
+ basePath
467
+ ]
468
+ });
469
+ if (result.rows.length === 0) return null;
470
+ return toCheckpointRow(result.rows[0]);
471
+ }
472
+ /** Insert a new checkpoint record. */
473
+ async createCheckpoint(row) {
474
+ await this.client.execute({
475
+ sql: `INSERT INTO sync_checkpoint (session_id, direction, remote_url, base_path, status, diff_json, completed, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
476
+ args: [
477
+ row.session_id,
478
+ row.direction,
479
+ row.remote_url,
480
+ row.base_path,
481
+ row.status,
482
+ row.diff_json,
483
+ row.completed,
484
+ row.created_at,
485
+ row.updated_at
486
+ ]
487
+ });
488
+ }
489
+ /** Update the completed paths list for an active checkpoint. */
490
+ async updateCheckpointCompleted(sessionId, completedPaths) {
491
+ await this.client.execute({
492
+ sql: `UPDATE sync_checkpoint SET completed = ?, updated_at = ? WHERE session_id = ?`,
493
+ args: [
494
+ JSON.stringify(completedPaths),
495
+ Date.now(),
496
+ sessionId
497
+ ]
498
+ });
499
+ }
500
+ /** Mark a checkpoint as done. */
501
+ async completeCheckpoint(sessionId) {
502
+ await this.client.execute({
503
+ sql: `UPDATE sync_checkpoint SET status = 'done', updated_at = ? WHERE session_id = ?`,
504
+ args: [Date.now(), sessionId]
505
+ });
506
+ }
507
+ /** Get a checkpoint by session_id (for test assertions). */
508
+ async getCheckpoint(sessionId) {
509
+ const result = await this.client.execute({
510
+ sql: `SELECT * FROM sync_checkpoint WHERE session_id = ?`,
511
+ args: [sessionId]
512
+ });
513
+ if (result.rows.length === 0) return null;
514
+ return toCheckpointRow(result.rows[0]);
515
+ }
516
+ /** Close the database connection. */
517
+ close() {
518
+ this.client.close();
519
+ }
520
+ };
521
+ function toEntryRow(row) {
522
+ const r = row;
523
+ return {
524
+ scope: r.scope,
525
+ path: r.path,
526
+ parent: r.parent,
527
+ name: r.name,
528
+ kind: r.kind,
529
+ cid: r.cid,
530
+ size: r.size,
531
+ mime_type: r.mime_type,
532
+ ctime: r.ctime,
533
+ mtime: r.mtime
534
+ };
535
+ }
536
+ function toCidCacheRow(row) {
537
+ return {
538
+ path: row.path,
539
+ cid: row.cid,
540
+ size: row.size,
541
+ mtime_ms: row.mtime_ms,
542
+ computed_at: row.computed_at
543
+ };
544
+ }
545
+ function toCheckpointRow(row) {
546
+ const r = row;
547
+ return {
548
+ session_id: r.session_id,
549
+ direction: r.direction,
550
+ remote_url: r.remote_url,
551
+ base_path: r.base_path,
552
+ status: r.status,
553
+ diff_json: r.diff_json,
554
+ completed: r.completed,
555
+ created_at: r.created_at,
556
+ updated_at: r.updated_at
557
+ };
558
+ }
559
+
560
+ //#endregion
561
+ export { MetadataDB };
562
+ //# sourceMappingURL=metadata-db.mjs.map