@noy-db/hub 0.1.0-pre.3

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 (195) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +197 -0
  3. package/dist/aggregate/index.cjs +476 -0
  4. package/dist/aggregate/index.cjs.map +1 -0
  5. package/dist/aggregate/index.d.cts +38 -0
  6. package/dist/aggregate/index.d.ts +38 -0
  7. package/dist/aggregate/index.js +53 -0
  8. package/dist/aggregate/index.js.map +1 -0
  9. package/dist/blobs/index.cjs +1480 -0
  10. package/dist/blobs/index.cjs.map +1 -0
  11. package/dist/blobs/index.d.cts +45 -0
  12. package/dist/blobs/index.d.ts +45 -0
  13. package/dist/blobs/index.js +48 -0
  14. package/dist/blobs/index.js.map +1 -0
  15. package/dist/bundle/index.cjs +436 -0
  16. package/dist/bundle/index.cjs.map +1 -0
  17. package/dist/bundle/index.d.cts +7 -0
  18. package/dist/bundle/index.d.ts +7 -0
  19. package/dist/bundle/index.js +40 -0
  20. package/dist/bundle/index.js.map +1 -0
  21. package/dist/chunk-2QR2PQTT.js +217 -0
  22. package/dist/chunk-2QR2PQTT.js.map +1 -0
  23. package/dist/chunk-4OWFYIDQ.js +79 -0
  24. package/dist/chunk-4OWFYIDQ.js.map +1 -0
  25. package/dist/chunk-5AATM2M2.js +90 -0
  26. package/dist/chunk-5AATM2M2.js.map +1 -0
  27. package/dist/chunk-ACLDOTNQ.js +543 -0
  28. package/dist/chunk-ACLDOTNQ.js.map +1 -0
  29. package/dist/chunk-BTDCBVJW.js +160 -0
  30. package/dist/chunk-BTDCBVJW.js.map +1 -0
  31. package/dist/chunk-CIMZBAZB.js +72 -0
  32. package/dist/chunk-CIMZBAZB.js.map +1 -0
  33. package/dist/chunk-E445ICYI.js +365 -0
  34. package/dist/chunk-E445ICYI.js.map +1 -0
  35. package/dist/chunk-EXQRC2L4.js +722 -0
  36. package/dist/chunk-EXQRC2L4.js.map +1 -0
  37. package/dist/chunk-FZU343FL.js +32 -0
  38. package/dist/chunk-FZU343FL.js.map +1 -0
  39. package/dist/chunk-GJILMRPO.js +354 -0
  40. package/dist/chunk-GJILMRPO.js.map +1 -0
  41. package/dist/chunk-GOUT6DND.js +1285 -0
  42. package/dist/chunk-GOUT6DND.js.map +1 -0
  43. package/dist/chunk-J66GRPNH.js +111 -0
  44. package/dist/chunk-J66GRPNH.js.map +1 -0
  45. package/dist/chunk-M2F2JAWB.js +464 -0
  46. package/dist/chunk-M2F2JAWB.js.map +1 -0
  47. package/dist/chunk-M5INGEFC.js +84 -0
  48. package/dist/chunk-M5INGEFC.js.map +1 -0
  49. package/dist/chunk-M62XNWRA.js +72 -0
  50. package/dist/chunk-M62XNWRA.js.map +1 -0
  51. package/dist/chunk-MR4424N3.js +275 -0
  52. package/dist/chunk-MR4424N3.js.map +1 -0
  53. package/dist/chunk-NPC4LFV5.js +132 -0
  54. package/dist/chunk-NPC4LFV5.js.map +1 -0
  55. package/dist/chunk-NXFEYLVG.js +311 -0
  56. package/dist/chunk-NXFEYLVG.js.map +1 -0
  57. package/dist/chunk-R36SIKES.js +79 -0
  58. package/dist/chunk-R36SIKES.js.map +1 -0
  59. package/dist/chunk-TDR6T5CJ.js +381 -0
  60. package/dist/chunk-TDR6T5CJ.js.map +1 -0
  61. package/dist/chunk-UF3BUNQZ.js +1 -0
  62. package/dist/chunk-UF3BUNQZ.js.map +1 -0
  63. package/dist/chunk-UQFSPSWG.js +1109 -0
  64. package/dist/chunk-UQFSPSWG.js.map +1 -0
  65. package/dist/chunk-USKYUS74.js +793 -0
  66. package/dist/chunk-USKYUS74.js.map +1 -0
  67. package/dist/chunk-XCL3WP6J.js +121 -0
  68. package/dist/chunk-XCL3WP6J.js.map +1 -0
  69. package/dist/chunk-XHFOENR2.js +680 -0
  70. package/dist/chunk-XHFOENR2.js.map +1 -0
  71. package/dist/chunk-ZFKD4QMV.js +430 -0
  72. package/dist/chunk-ZFKD4QMV.js.map +1 -0
  73. package/dist/chunk-ZLMV3TUA.js +490 -0
  74. package/dist/chunk-ZLMV3TUA.js.map +1 -0
  75. package/dist/chunk-ZRG4V3F5.js +17 -0
  76. package/dist/chunk-ZRG4V3F5.js.map +1 -0
  77. package/dist/consent/index.cjs +204 -0
  78. package/dist/consent/index.cjs.map +1 -0
  79. package/dist/consent/index.d.cts +24 -0
  80. package/dist/consent/index.d.ts +24 -0
  81. package/dist/consent/index.js +23 -0
  82. package/dist/consent/index.js.map +1 -0
  83. package/dist/crdt/index.cjs +152 -0
  84. package/dist/crdt/index.cjs.map +1 -0
  85. package/dist/crdt/index.d.cts +30 -0
  86. package/dist/crdt/index.d.ts +30 -0
  87. package/dist/crdt/index.js +24 -0
  88. package/dist/crdt/index.js.map +1 -0
  89. package/dist/crypto-IVKU7YTT.js +44 -0
  90. package/dist/crypto-IVKU7YTT.js.map +1 -0
  91. package/dist/delegation-XDJCBTI2.js +16 -0
  92. package/dist/delegation-XDJCBTI2.js.map +1 -0
  93. package/dist/dev-unlock-CeXic1xC.d.cts +263 -0
  94. package/dist/dev-unlock-KrKkcqD3.d.ts +263 -0
  95. package/dist/hash-9KO1BGxh.d.cts +63 -0
  96. package/dist/hash-ChfJjRjQ.d.ts +63 -0
  97. package/dist/history/index.cjs +1215 -0
  98. package/dist/history/index.cjs.map +1 -0
  99. package/dist/history/index.d.cts +62 -0
  100. package/dist/history/index.d.ts +62 -0
  101. package/dist/history/index.js +79 -0
  102. package/dist/history/index.js.map +1 -0
  103. package/dist/i18n/index.cjs +746 -0
  104. package/dist/i18n/index.cjs.map +1 -0
  105. package/dist/i18n/index.d.cts +38 -0
  106. package/dist/i18n/index.d.ts +38 -0
  107. package/dist/i18n/index.js +55 -0
  108. package/dist/i18n/index.js.map +1 -0
  109. package/dist/index-BRHBCmLt.d.ts +1940 -0
  110. package/dist/index-C8kQtmOk.d.ts +380 -0
  111. package/dist/index-DN-J-5wT.d.cts +1940 -0
  112. package/dist/index-DhjMjz7L.d.cts +380 -0
  113. package/dist/index.cjs +14756 -0
  114. package/dist/index.cjs.map +1 -0
  115. package/dist/index.d.cts +269 -0
  116. package/dist/index.d.ts +269 -0
  117. package/dist/index.js +6085 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/indexing/index.cjs +736 -0
  120. package/dist/indexing/index.cjs.map +1 -0
  121. package/dist/indexing/index.d.cts +36 -0
  122. package/dist/indexing/index.d.ts +36 -0
  123. package/dist/indexing/index.js +77 -0
  124. package/dist/indexing/index.js.map +1 -0
  125. package/dist/lazy-builder-BwEoBQZ9.d.ts +304 -0
  126. package/dist/lazy-builder-CZVLKh0Z.d.cts +304 -0
  127. package/dist/ledger-2NX4L7PN.js +33 -0
  128. package/dist/ledger-2NX4L7PN.js.map +1 -0
  129. package/dist/mime-magic-CBBSOkjm.d.cts +50 -0
  130. package/dist/mime-magic-CBBSOkjm.d.ts +50 -0
  131. package/dist/periods/index.cjs +1035 -0
  132. package/dist/periods/index.cjs.map +1 -0
  133. package/dist/periods/index.d.cts +21 -0
  134. package/dist/periods/index.d.ts +21 -0
  135. package/dist/periods/index.js +25 -0
  136. package/dist/periods/index.js.map +1 -0
  137. package/dist/predicate-SBHmi6D0.d.cts +161 -0
  138. package/dist/predicate-SBHmi6D0.d.ts +161 -0
  139. package/dist/query/index.cjs +1957 -0
  140. package/dist/query/index.cjs.map +1 -0
  141. package/dist/query/index.d.cts +3 -0
  142. package/dist/query/index.d.ts +3 -0
  143. package/dist/query/index.js +62 -0
  144. package/dist/query/index.js.map +1 -0
  145. package/dist/session/index.cjs +487 -0
  146. package/dist/session/index.cjs.map +1 -0
  147. package/dist/session/index.d.cts +45 -0
  148. package/dist/session/index.d.ts +45 -0
  149. package/dist/session/index.js +44 -0
  150. package/dist/session/index.js.map +1 -0
  151. package/dist/shadow/index.cjs +133 -0
  152. package/dist/shadow/index.cjs.map +1 -0
  153. package/dist/shadow/index.d.cts +16 -0
  154. package/dist/shadow/index.d.ts +16 -0
  155. package/dist/shadow/index.js +20 -0
  156. package/dist/shadow/index.js.map +1 -0
  157. package/dist/store/index.cjs +1069 -0
  158. package/dist/store/index.cjs.map +1 -0
  159. package/dist/store/index.d.cts +491 -0
  160. package/dist/store/index.d.ts +491 -0
  161. package/dist/store/index.js +34 -0
  162. package/dist/store/index.js.map +1 -0
  163. package/dist/strategy-BSxFXGzb.d.cts +110 -0
  164. package/dist/strategy-BSxFXGzb.d.ts +110 -0
  165. package/dist/strategy-D-SrOLCl.d.cts +548 -0
  166. package/dist/strategy-D-SrOLCl.d.ts +548 -0
  167. package/dist/sync/index.cjs +1062 -0
  168. package/dist/sync/index.cjs.map +1 -0
  169. package/dist/sync/index.d.cts +42 -0
  170. package/dist/sync/index.d.ts +42 -0
  171. package/dist/sync/index.js +28 -0
  172. package/dist/sync/index.js.map +1 -0
  173. package/dist/team/index.cjs +1233 -0
  174. package/dist/team/index.cjs.map +1 -0
  175. package/dist/team/index.d.cts +117 -0
  176. package/dist/team/index.d.ts +117 -0
  177. package/dist/team/index.js +39 -0
  178. package/dist/team/index.js.map +1 -0
  179. package/dist/tx/index.cjs +212 -0
  180. package/dist/tx/index.cjs.map +1 -0
  181. package/dist/tx/index.d.cts +20 -0
  182. package/dist/tx/index.d.ts +20 -0
  183. package/dist/tx/index.js +20 -0
  184. package/dist/tx/index.js.map +1 -0
  185. package/dist/types-BZpCZB8N.d.ts +7526 -0
  186. package/dist/types-Bfs0qr5F.d.cts +7526 -0
  187. package/dist/ulid-COREQ2RQ.js +9 -0
  188. package/dist/ulid-COREQ2RQ.js.map +1 -0
  189. package/dist/util/index.cjs +230 -0
  190. package/dist/util/index.cjs.map +1 -0
  191. package/dist/util/index.d.cts +77 -0
  192. package/dist/util/index.d.ts +77 -0
  193. package/dist/util/index.js +190 -0
  194. package/dist/util/index.js.map +1 -0
  195. package/package.json +244 -0
@@ -0,0 +1,1109 @@
1
+ import {
2
+ NOYDB_FORMAT_VERSION
3
+ } from "./chunk-ZRG4V3F5.js";
4
+ import {
5
+ base64ToBuffer,
6
+ bufferToBase64,
7
+ decrypt,
8
+ decryptBytesWithAAD,
9
+ encrypt,
10
+ encryptBytesWithAAD,
11
+ hmacSha256Hex,
12
+ sha256Hex
13
+ } from "./chunk-MR4424N3.js";
14
+ import {
15
+ ConflictError,
16
+ NotFoundError
17
+ } from "./chunk-ACLDOTNQ.js";
18
+
19
+ // src/blobs/mime-magic.ts
20
+ function hex(s) {
21
+ return new Uint8Array(s.split(" ").map((b) => parseInt(b, 16)));
22
+ }
23
+ var MAGIC_RULES = [
24
+ // ── Images ───────────────────────────────────────────────────────────
25
+ // #2 PNG — full 8-byte signature (RFC 2083)
26
+ { mime: "image/png", format: "PNG", bytes: hex("89 50 4E 47 0D 0A 1A 0A"), preCompressed: true },
27
+ // #1 JPEG — FF D8 FF (third byte is start of APP marker, always FF)
28
+ { mime: "image/jpeg", format: "JPEG", bytes: hex("FF D8 FF"), preCompressed: true },
29
+ // #7 WebP — RIFF compound: bytes 0-3 = RIFF, bytes 8-11 = WEBP
30
+ {
31
+ mime: "image/webp",
32
+ format: "WebP",
33
+ bytes: hex("52 49 46 46"),
34
+ secondaryBytes: hex("57 45 42 50"),
35
+ secondaryOffset: 8,
36
+ preCompressed: true
37
+ },
38
+ // #5 TIFF (little-endian) — II + version 42
39
+ { mime: "image/tiff", format: "TIFF", bytes: hex("49 49 2A 00") },
40
+ // #6 TIFF (big-endian) — MM + version 42
41
+ { mime: "image/tiff", format: "TIFF", bytes: hex("4D 4D 00 2A") },
42
+ // #3 GIF — GIF8 (covers GIF87a and GIF89a)
43
+ { mime: "image/gif", format: "GIF", bytes: hex("47 49 46 38"), preCompressed: true },
44
+ // #4 BMP — BM
45
+ { mime: "image/bmp", format: "BMP", bytes: hex("42 4D") },
46
+ // PSD — 8BPS
47
+ { mime: "image/vnd.adobe.photoshop", format: "PSD", bytes: hex("38 42 50 53") },
48
+ // #8 ICO — 00 00 01 00 (note: 00 00 02 00 is CUR cursor format)
49
+ { mime: "image/x-icon", format: "ICO", bytes: hex("00 00 01 00") },
50
+ // #9 HEIC — ISOBMFF: ftyp at offset 4, brand "heic" at offset 8
51
+ {
52
+ mime: "image/heic",
53
+ format: "HEIC",
54
+ bytes: hex("66 74 79 70"),
55
+ offset: 4,
56
+ secondaryBytes: hex("68 65 69 63"),
57
+ secondaryOffset: 8,
58
+ preCompressed: true
59
+ },
60
+ // ── Documents ────────────────────────────────────────────────────────
61
+ // PDF — %PDF
62
+ { mime: "application/pdf", format: "PDF", bytes: hex("25 50 44 46") },
63
+ // RTF — {\rtf
64
+ { mime: "application/rtf", format: "RTF", bytes: hex("7B 5C 72 74 66") },
65
+ // ── Archives & compression ───────────────────────────────────────────
66
+ // RAR v5 — 8-byte signature (test before RAR v4)
67
+ { mime: "application/vnd.rar", format: "RAR v5", bytes: hex("52 61 72 21 1A 07 01 00"), preCompressed: true },
68
+ // RAR v4 — 7-byte signature
69
+ { mime: "application/vnd.rar", format: "RAR v4", bytes: hex("52 61 72 21 1A 07 00"), preCompressed: true },
70
+ // 7-Zip — 6-byte signature
71
+ { mime: "application/x-7z-compressed", format: "7Z", bytes: hex("37 7A BC AF 27 1C"), preCompressed: true },
72
+ // XZ — 6-byte stream header
73
+ { mime: "application/x-xz", format: "XZ", bytes: hex("FD 37 7A 58 5A 00"), preCompressed: true },
74
+ // ZIP — PK\x03\x04 (local file header)
75
+ { mime: "application/zip", format: "ZIP", bytes: hex("50 4B 03 04"), preCompressed: true },
76
+ // GZIP — 1F 8B
77
+ { mime: "application/gzip", format: "GZIP", bytes: hex("1F 8B"), preCompressed: true },
78
+ // BZIP2 — BZh
79
+ { mime: "application/x-bzip2", format: "BZIP2", bytes: hex("42 5A 68"), preCompressed: true },
80
+ // LZIP — LZIP
81
+ { mime: "application/x-lzip", format: "LZIP", bytes: hex("4C 5A 49 50"), preCompressed: true },
82
+ // ── Audio ────────────────────────────────────────────────────────────
83
+ // WAV — RIFF compound: bytes 0-3 = RIFF, bytes 8-11 = WAVE
84
+ {
85
+ mime: "audio/wav",
86
+ format: "WAV",
87
+ bytes: hex("52 49 46 46"),
88
+ secondaryBytes: hex("57 41 56 45"),
89
+ secondaryOffset: 8
90
+ },
91
+ // AIFF — FORM compound: bytes 0-3 = FORM, bytes 8-11 = AIFF
92
+ {
93
+ mime: "audio/aiff",
94
+ format: "AIFF",
95
+ bytes: hex("46 4F 52 4D"),
96
+ secondaryBytes: hex("41 49 46 46"),
97
+ secondaryOffset: 8
98
+ },
99
+ // FLAC — fLaC
100
+ { mime: "audio/flac", format: "FLAC", bytes: hex("66 4C 61 43") },
101
+ // OGG — OggS (container — may hold Vorbis, Opus, Theora, etc.)
102
+ { mime: "application/ogg", format: "OGG", bytes: hex("4F 67 67 53") },
103
+ // MIDI — MThd
104
+ { mime: "audio/midi", format: "MIDI", bytes: hex("4D 54 68 64") },
105
+ // MP3 (ID3-tagged) — ID3
106
+ { mime: "audio/mpeg", format: "MP3", bytes: hex("49 44 33"), preCompressed: true },
107
+ // ── Video ────────────────────────────────────────────────────────────
108
+ // AVI — RIFF compound: bytes 0-3 = RIFF, bytes 8-11 = AVI\x20
109
+ {
110
+ mime: "video/x-msvideo",
111
+ format: "AVI",
112
+ bytes: hex("52 49 46 46"),
113
+ secondaryBytes: hex("41 56 49 20"),
114
+ secondaryOffset: 8,
115
+ preCompressed: true
116
+ },
117
+ // WMV/ASF — 8-byte ASF header GUID prefix
118
+ { mime: "video/x-ms-wmv", format: "WMV", bytes: hex("30 26 B2 75 8E 66 CF 11"), preCompressed: true },
119
+ // MKV/WebM — EBML header (Matroska container)
120
+ { mime: "video/x-matroska", format: "MKV", bytes: hex("1A 45 DF A3"), preCompressed: true },
121
+ // FLV — FLV
122
+ { mime: "video/x-flv", format: "FLV", bytes: hex("46 4C 56"), preCompressed: true },
123
+ // MOV — ISOBMFF: ftyp at offset 4, brand "qt " at offset 8
124
+ {
125
+ mime: "video/quicktime",
126
+ format: "MOV",
127
+ bytes: hex("66 74 79 70"),
128
+ offset: 4,
129
+ secondaryBytes: hex("71 74 20 20"),
130
+ secondaryOffset: 8,
131
+ preCompressed: true
132
+ },
133
+ // MP4 — ISOBMFF: ftyp at offset 4 (brands vary: isom, mp41, mp42, etc.)
134
+ // Tested AFTER MOV and HEIC so their specific brands match first.
135
+ { mime: "video/mp4", format: "MP4", bytes: hex("66 74 79 70"), offset: 4, preCompressed: true },
136
+ // ── Executables & binaries ───────────────────────────────────────────
137
+ // SQLite — "SQLite 3" (first 8 bytes of the 16-byte header)
138
+ { mime: "application/vnd.sqlite3", format: "SQLite", bytes: hex("53 51 4C 69 74 65 20 33") },
139
+ // WASM — \0asm
140
+ { mime: "application/wasm", format: "WASM", bytes: hex("00 61 73 6D") },
141
+ // ELF — \x7FELF
142
+ { mime: "application/x-elf", format: "ELF", bytes: hex("7F 45 4C 46") },
143
+ // PE (EXE/DLL) — MZ
144
+ { mime: "application/vnd.microsoft.portable-executable", format: "PE", bytes: hex("4D 5A") },
145
+ // Mach-O — all four single-arch variants
146
+ { mime: "application/x-mach-binary", format: "Mach-O 64 LE", bytes: hex("CF FA ED FE") },
147
+ { mime: "application/x-mach-binary", format: "Mach-O 64 BE", bytes: hex("FE ED FA CF") },
148
+ { mime: "application/x-mach-binary", format: "Mach-O 32 LE", bytes: hex("CE FA ED FE") },
149
+ { mime: "application/x-mach-binary", format: "Mach-O 32 BE", bytes: hex("FE ED FA CE") },
150
+ // Java Class — CA FE BA BE
151
+ // Note: collides with Mach-O Universal Binary. Disambiguated by checking
152
+ // bytes 4-7: Java class version is >= 0x002D (45), while fat binary
153
+ // arch count is a small number (typically 0x00000002).
154
+ // We place Java after Mach-O single-arch entries so the more common
155
+ // Mach-O variants match first. The CA FE BA BE collision between Java
156
+ // and Mach-O fat binary is resolved by the caller if needed.
157
+ { mime: "application/java-vm", format: "Java Class", bytes: hex("CA FE BA BE") },
158
+ // DEX — dex\n (Android Dalvik Executable)
159
+ { mime: "application/vnd.android.dex", format: "DEX", bytes: hex("64 65 78 0A") },
160
+ // ── Package formats ──────────────────────────────────────────────────
161
+ // DEB — !<arch> (ar archive; DEB-specific member follows)
162
+ { mime: "application/vnd.debian.binary-package", format: "DEB", bytes: hex("21 3C 61 72 63 68 3E") },
163
+ // RPM — ED AB EE DB
164
+ { mime: "application/x-rpm", format: "RPM", bytes: hex("ED AB EE DB") },
165
+ // CAB — MSCF
166
+ { mime: "application/vnd.ms-cab-compressed", format: "CAB", bytes: hex("4D 53 43 46"), preCompressed: true },
167
+ // ── Capture & Flash ──────────────────────────────────────────────────
168
+ // PCAP (little-endian) — D4 C3 B2 A1
169
+ { mime: "application/vnd.tcpdump.pcap", format: "PCAP", bytes: hex("D4 C3 B2 A1") },
170
+ // PCAP (big-endian) — A1 B2 C3 D4
171
+ { mime: "application/vnd.tcpdump.pcap", format: "PCAP BE", bytes: hex("A1 B2 C3 D4") },
172
+ // PCAPNG — Section Header Block
173
+ { mime: "application/x-pcapng", format: "PCAPNG", bytes: hex("0A 0D 0D 0A") },
174
+ // SWF — all three variants (uncompressed, zlib, LZMA)
175
+ { mime: "application/x-shockwave-flash", format: "SWF", bytes: hex("46 57 53") },
176
+ { mime: "application/x-shockwave-flash", format: "SWF zlib", bytes: hex("43 57 53"), preCompressed: true },
177
+ { mime: "application/x-shockwave-flash", format: "SWF LZMA", bytes: hex("5A 57 53"), preCompressed: true },
178
+ // ── Data formats ─────────────────────────────────────────────────────
179
+ // Parquet — PAR1 (no registered IANA MIME; using Apache's informal type)
180
+ { mime: "application/vnd.apache.parquet", format: "Parquet", bytes: hex("50 41 52 31") },
181
+ // Avro Object Container — Obj\x01
182
+ { mime: "application/avro", format: "Avro", bytes: hex("4F 62 6A 01") },
183
+ // NES ROM — NES\x1A (iNES header)
184
+ { mime: "application/x-nintendo-nes-rom", format: "NES ROM", bytes: hex("4E 45 53 1A") }
185
+ ];
186
+ function isMp3SyncWord(byte0, byte1) {
187
+ return byte0 === 255 && (byte1 & 224) === 224;
188
+ }
189
+ function detectMimeType(header) {
190
+ const result = detectMagic(header);
191
+ return result?.mime ?? "application/octet-stream";
192
+ }
193
+ function detectMagic(header) {
194
+ for (const rule of MAGIC_RULES) {
195
+ if (matchRule(header, rule)) {
196
+ return {
197
+ mime: rule.mime,
198
+ format: rule.format,
199
+ preCompressed: rule.preCompressed ?? false
200
+ };
201
+ }
202
+ }
203
+ if (header.length >= 2 && isMp3SyncWord(header[0], header[1])) {
204
+ return { mime: "audio/mpeg", format: "MP3", preCompressed: true };
205
+ }
206
+ return null;
207
+ }
208
+ function isPreCompressed(mimeType) {
209
+ return PRE_COMPRESSED_MIMES.has(mimeType);
210
+ }
211
+ function matchRule(header, rule) {
212
+ const offset = rule.offset ?? 0;
213
+ const end = offset + rule.bytes.length;
214
+ if (header.length < end) return false;
215
+ for (let i = 0; i < rule.bytes.length; i++) {
216
+ if (header[offset + i] !== rule.bytes[i]) return false;
217
+ }
218
+ if (rule.secondaryBytes && rule.secondaryOffset !== void 0) {
219
+ const sEnd = rule.secondaryOffset + rule.secondaryBytes.length;
220
+ if (header.length < sEnd) return false;
221
+ for (let i = 0; i < rule.secondaryBytes.length; i++) {
222
+ if (header[rule.secondaryOffset + i] !== rule.secondaryBytes[i]) return false;
223
+ }
224
+ }
225
+ return true;
226
+ }
227
+ var PRE_COMPRESSED_MIMES = new Set(
228
+ MAGIC_RULES.filter((r) => r.preCompressed).map((r) => r.mime)
229
+ );
230
+
231
+ // src/blobs/blob-set.ts
232
+ var BLOB_COLLECTION = "_blob";
233
+ var BLOB_INDEX_COLLECTION = "_blob_index";
234
+ var BLOB_CHUNKS_COLLECTION = "_blob_chunks";
235
+ var BLOB_SLOTS_PREFIX = "_blob_slots_";
236
+ var BLOB_VERSIONS_PREFIX = "_blob_versions_";
237
+ var DEFAULT_CHUNK_SIZE = 256 * 1024;
238
+ var MAX_CAS_RETRIES = 5;
239
+ async function compressBytes(data) {
240
+ if (typeof CompressionStream === "undefined") {
241
+ return { bytes: data, algorithm: "none" };
242
+ }
243
+ const cs = new CompressionStream("gzip");
244
+ const writer = cs.writable.getWriter();
245
+ await writer.write(data);
246
+ await writer.close();
247
+ const buf = await new Response(cs.readable).arrayBuffer();
248
+ return { bytes: new Uint8Array(buf), algorithm: "gzip" };
249
+ }
250
+ async function decompressBytes(data) {
251
+ if (typeof DecompressionStream === "undefined") {
252
+ throw new Error(
253
+ "[noy-db] DecompressionStream not available \u2014 cannot decompress blob chunk"
254
+ );
255
+ }
256
+ const ds = new DecompressionStream("gzip");
257
+ const writer = ds.writable.getWriter();
258
+ await writer.write(data);
259
+ await writer.close();
260
+ const buf = await new Response(ds.readable).arrayBuffer();
261
+ return new Uint8Array(buf);
262
+ }
263
+ function concatChunks(chunks) {
264
+ const total = chunks.reduce((s, c) => s + c.byteLength, 0);
265
+ const out = new Uint8Array(total);
266
+ let offset = 0;
267
+ for (const c of chunks) {
268
+ out.set(c, offset);
269
+ offset += c.byteLength;
270
+ }
271
+ return out;
272
+ }
273
+ function chunkAAD(eTag, chunkIndex, chunkCount) {
274
+ return new TextEncoder().encode(`${eTag}:${chunkIndex}:${chunkCount}`);
275
+ }
276
+ var BlobSet = class {
277
+ store;
278
+ vault;
279
+ collection;
280
+ recordId;
281
+ getDEK;
282
+ encrypted;
283
+ userId;
284
+ maxBlobBytes;
285
+ constructor(opts) {
286
+ this.store = opts.store;
287
+ this.vault = opts.vault;
288
+ this.collection = opts.collection;
289
+ this.recordId = opts.recordId;
290
+ this.getDEK = opts.getDEK;
291
+ this.encrypted = opts.encrypted;
292
+ this.userId = opts.userId;
293
+ this.maxBlobBytes = opts.maxBlobBytes;
294
+ }
295
+ /** The internal collection that holds slot metadata for this collection's blobs. */
296
+ get slotsCollection() {
297
+ return `${BLOB_SLOTS_PREFIX}${this.collection}`;
298
+ }
299
+ /** The internal collection that holds published versions for this collection's blobs. */
300
+ get versionsCollection() {
301
+ return `${BLOB_VERSIONS_PREFIX}${this.collection}`;
302
+ }
303
+ // ─── Slot Metadata I/O (CAS-protected) ─────────────────────────────
304
+ async loadSlots() {
305
+ const envelope = await this.store.get(this.vault, this.slotsCollection, this.recordId);
306
+ if (!envelope) return { slots: {}, version: 0 };
307
+ if (!this.encrypted) {
308
+ return {
309
+ slots: JSON.parse(envelope._data),
310
+ version: envelope._v
311
+ };
312
+ }
313
+ const dek = await this.getDEK(this.collection);
314
+ const json = await decrypt(envelope._iv, envelope._data, dek);
315
+ return {
316
+ slots: JSON.parse(json),
317
+ version: envelope._v
318
+ };
319
+ }
320
+ async saveSlots(slots, currentVersion) {
321
+ const json = JSON.stringify(slots);
322
+ const now = (/* @__PURE__ */ new Date()).toISOString();
323
+ let envelope;
324
+ if (this.encrypted) {
325
+ const dek = await this.getDEK(this.collection);
326
+ const { iv, data } = await encrypt(json, dek);
327
+ envelope = {
328
+ _noydb: NOYDB_FORMAT_VERSION,
329
+ _v: currentVersion + 1,
330
+ _ts: now,
331
+ _iv: iv,
332
+ _data: data
333
+ };
334
+ } else {
335
+ envelope = {
336
+ _noydb: NOYDB_FORMAT_VERSION,
337
+ _v: currentVersion + 1,
338
+ _ts: now,
339
+ _iv: "",
340
+ _data: json
341
+ };
342
+ }
343
+ await this.store.put(
344
+ this.vault,
345
+ this.slotsCollection,
346
+ this.recordId,
347
+ envelope,
348
+ currentVersion > 0 ? currentVersion : void 0
349
+ );
350
+ }
351
+ /**
352
+ * CAS retry loop for slot metadata updates. Re-reads slots on conflict
353
+ * and re-applies the mutation function.
354
+ */
355
+ async casUpdateSlots(mutate) {
356
+ for (let attempt = 0; attempt < MAX_CAS_RETRIES; attempt++) {
357
+ const { slots, version } = await this.loadSlots();
358
+ const updated = mutate(slots);
359
+ if (updated === null) return;
360
+ try {
361
+ await this.saveSlots(updated, version);
362
+ return;
363
+ } catch (err) {
364
+ if (err instanceof ConflictError && attempt < MAX_CAS_RETRIES - 1) continue;
365
+ throw err;
366
+ }
367
+ }
368
+ }
369
+ // ─── Blob Index I/O (versioned for CAS refCount) ──────────────────
370
+ async loadBlobObject(eTag) {
371
+ const envelope = await this.store.get(this.vault, BLOB_INDEX_COLLECTION, eTag);
372
+ if (!envelope) return null;
373
+ if (!this.encrypted) {
374
+ return { blob: JSON.parse(envelope._data), version: envelope._v };
375
+ }
376
+ const dek = await this.getDEK(BLOB_COLLECTION);
377
+ const json = await decrypt(envelope._iv, envelope._data, dek);
378
+ return { blob: JSON.parse(json), version: envelope._v };
379
+ }
380
+ async writeBlobObject(blob, expectedVersion) {
381
+ const json = JSON.stringify(blob);
382
+ const now = (/* @__PURE__ */ new Date()).toISOString();
383
+ const newVersion = (expectedVersion ?? 0) + 1;
384
+ let envelope;
385
+ if (this.encrypted) {
386
+ const dek = await this.getDEK(BLOB_COLLECTION);
387
+ const { iv, data } = await encrypt(json, dek);
388
+ envelope = { _noydb: NOYDB_FORMAT_VERSION, _v: newVersion, _ts: now, _iv: iv, _data: data };
389
+ } else {
390
+ envelope = { _noydb: NOYDB_FORMAT_VERSION, _v: newVersion, _ts: now, _iv: "", _data: json };
391
+ }
392
+ await this.store.put(
393
+ this.vault,
394
+ BLOB_INDEX_COLLECTION,
395
+ blob.eTag,
396
+ envelope,
397
+ expectedVersion
398
+ );
399
+ }
400
+ /**
401
+ * CAS retry loop for refCount changes on a BlobObject.
402
+ */
403
+ async casUpdateRefCount(eTag, delta) {
404
+ for (let attempt = 0; attempt < MAX_CAS_RETRIES; attempt++) {
405
+ const result = await this.loadBlobObject(eTag);
406
+ if (!result) throw new NotFoundError(`BlobObject ${eTag} not found`);
407
+ const { blob, version } = result;
408
+ const updated = { ...blob, refCount: blob.refCount + delta };
409
+ try {
410
+ await this.writeBlobObject(updated, version);
411
+ return;
412
+ } catch (err) {
413
+ if (err instanceof ConflictError && attempt < MAX_CAS_RETRIES - 1) continue;
414
+ throw err;
415
+ }
416
+ }
417
+ }
418
+ // ─── Chunk I/O (with AAD binding) ─────────────────────────────────
419
+ async writeChunk(eTag, index, chunkCount, chunk, dek) {
420
+ const id = `${eTag}_${index}`;
421
+ const now = (/* @__PURE__ */ new Date()).toISOString();
422
+ let envelope;
423
+ if (dek) {
424
+ const aad = chunkAAD(eTag, index, chunkCount);
425
+ const { iv, data } = await encryptBytesWithAAD(chunk, dek, aad);
426
+ envelope = { _noydb: NOYDB_FORMAT_VERSION, _v: 1, _ts: now, _iv: iv, _data: data };
427
+ } else {
428
+ envelope = {
429
+ _noydb: NOYDB_FORMAT_VERSION,
430
+ _v: 1,
431
+ _ts: now,
432
+ _iv: "",
433
+ _data: bufferToBase64(chunk)
434
+ };
435
+ }
436
+ await this.store.put(this.vault, BLOB_CHUNKS_COLLECTION, id, envelope);
437
+ }
438
+ async readChunk(eTag, index, chunkCount, dek) {
439
+ const envelope = await this.store.get(this.vault, BLOB_CHUNKS_COLLECTION, `${eTag}_${index}`);
440
+ if (!envelope) return null;
441
+ if (dek) {
442
+ const aad = chunkAAD(eTag, index, chunkCount);
443
+ return await decryptBytesWithAAD(envelope._iv, envelope._data, dek, aad);
444
+ }
445
+ return base64ToBuffer(envelope._data);
446
+ }
447
+ // ─── Version record I/O ───────────────────────────────────────────
448
+ versionKey(slotName, label) {
449
+ return `${this.recordId}::${slotName}::${label}`;
450
+ }
451
+ async loadVersionRecord(slotName, label) {
452
+ const key = this.versionKey(slotName, label);
453
+ const envelope = await this.store.get(this.vault, this.versionsCollection, key);
454
+ if (!envelope) return null;
455
+ if (!this.encrypted) {
456
+ return JSON.parse(envelope._data);
457
+ }
458
+ const dek = await this.getDEK(this.collection);
459
+ const json = await decrypt(envelope._iv, envelope._data, dek);
460
+ return JSON.parse(json);
461
+ }
462
+ async writeVersionRecord(slotName, record) {
463
+ const key = this.versionKey(slotName, record.label);
464
+ const json = JSON.stringify(record);
465
+ const now = (/* @__PURE__ */ new Date()).toISOString();
466
+ let envelope;
467
+ if (this.encrypted) {
468
+ const dek = await this.getDEK(this.collection);
469
+ const { iv, data } = await encrypt(json, dek);
470
+ envelope = { _noydb: NOYDB_FORMAT_VERSION, _v: 1, _ts: now, _iv: iv, _data: data };
471
+ } else {
472
+ envelope = { _noydb: NOYDB_FORMAT_VERSION, _v: 1, _ts: now, _iv: "", _data: json };
473
+ }
474
+ await this.store.put(this.vault, this.versionsCollection, key, envelope);
475
+ }
476
+ async deleteVersionRecord(slotName, label) {
477
+ const key = this.versionKey(slotName, label);
478
+ await this.store.delete(this.vault, this.versionsCollection, key);
479
+ }
480
+ // ─── Effective chunk size ─────────────────────────────────────────
481
+ effectiveChunkSize(opts) {
482
+ if (opts?.chunkSize) return opts.chunkSize;
483
+ if (this.maxBlobBytes) return this.maxBlobBytes;
484
+ return DEFAULT_CHUNK_SIZE;
485
+ }
486
+ // ─── Fetch all chunks for a blob ──────────────────────────────────
487
+ async fetchAllChunks(blob) {
488
+ const blobDEK = this.encrypted ? await this.getDEK(BLOB_COLLECTION) : null;
489
+ const chunks = [];
490
+ for (let i = 0; i < blob.chunkCount; i++) {
491
+ const chunk = await this.readChunk(blob.eTag, i, blob.chunkCount, blobDEK);
492
+ if (!chunk) {
493
+ throw new NotFoundError(
494
+ `Blob chunk ${i}/${blob.chunkCount} missing for eTag "${blob.eTag}" on record "${this.recordId}"`
495
+ );
496
+ }
497
+ chunks.push(chunk);
498
+ }
499
+ const assembled = concatChunks(chunks);
500
+ return blob.compression === "gzip" ? await decompressBytes(assembled) : assembled;
501
+ }
502
+ // ─── Public API: Slot management ──────────────────────────────────
503
+ /**
504
+ * Upload bytes and attach them to this record under `slotName`.
505
+ *
506
+ * 1. Computes `eTag = HMAC-SHA-256(blobDEK, plaintext)` for keyed content-addressing.
507
+ * 2. Auto-detects MIME type from magic bytes if not provided.
508
+ * 3. If a blob with this eTag already exists, skips chunk upload (deduplication)
509
+ * and CAS-increments refCount.
510
+ * 4. Otherwise: compresses → splits into chunks → encrypts each chunk with
511
+ * AAD binding → writes `_blob_chunks` → writes `BlobObject` to `_blob_index`.
512
+ * 5. CAS-updates the slot metadata in `_blob_slots_{collection}`.
513
+ * If overwriting an existing slot, decrements the old eTag's refCount.
514
+ */
515
+ async put(slotName, data, opts) {
516
+ const blobDEK = this.encrypted ? await this.getDEK(BLOB_COLLECTION) : null;
517
+ const eTag = blobDEK ? await hmacSha256Hex(blobDEK, data) : await plainSha256Hex(data);
518
+ let mimeType = opts?.mimeType;
519
+ if (!mimeType) {
520
+ const detected = detectMagic(data.subarray(0, 16));
521
+ if (detected) mimeType = detected.mime;
522
+ }
523
+ let shouldCompress;
524
+ if (opts?.compress !== void 0) {
525
+ shouldCompress = opts.compress;
526
+ } else if (mimeType && isPreCompressed(mimeType)) {
527
+ shouldCompress = false;
528
+ } else {
529
+ shouldCompress = true;
530
+ }
531
+ const existingBlob = await this.loadBlobObject(eTag);
532
+ if (existingBlob) {
533
+ await this.casUpdateRefCount(eTag, 1);
534
+ } else {
535
+ const { bytes: compressed, algorithm } = shouldCompress ? await compressBytes(data) : { bytes: data, algorithm: "none" };
536
+ const chunkSize = this.effectiveChunkSize(opts);
537
+ const chunkCount = Math.max(1, Math.ceil(compressed.byteLength / chunkSize));
538
+ for (let i = 0; i < chunkCount; i++) {
539
+ const start = i * chunkSize;
540
+ await this.writeChunk(
541
+ eTag,
542
+ i,
543
+ chunkCount,
544
+ compressed.subarray(start, start + chunkSize),
545
+ blobDEK
546
+ );
547
+ }
548
+ await this.writeBlobObject({
549
+ eTag,
550
+ size: data.byteLength,
551
+ compressedSize: compressed.byteLength,
552
+ compression: algorithm,
553
+ chunkSize,
554
+ chunkCount,
555
+ ...mimeType !== void 0 ? { mimeType } : {},
556
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
557
+ refCount: 1
558
+ });
559
+ }
560
+ const uploaderUserId = opts?.uploadedBy ?? this.userId;
561
+ await this.casUpdateSlots((slots) => {
562
+ const oldETag = slots[slotName]?.eTag;
563
+ slots[slotName] = {
564
+ eTag,
565
+ filename: slotName,
566
+ size: data.byteLength,
567
+ ...mimeType !== void 0 ? { mimeType } : {},
568
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
569
+ ...uploaderUserId !== void 0 ? { uploadedBy: uploaderUserId } : {}
570
+ };
571
+ if (oldETag && oldETag !== eTag) {
572
+ this._deferredRefDecrement = oldETag;
573
+ }
574
+ return slots;
575
+ });
576
+ if (this._deferredRefDecrement) {
577
+ const oldETag = this._deferredRefDecrement;
578
+ this._deferredRefDecrement = void 0;
579
+ await this.casUpdateRefCount(oldETag, -1).catch(() => {
580
+ });
581
+ }
582
+ }
583
+ _deferredRefDecrement;
584
+ /**
585
+ * Fetch all bytes for the named slot.
586
+ * Returns `null` if the slot does not exist.
587
+ * Throws `NotFoundError` if the index entry exists but a chunk is missing.
588
+ */
589
+ async get(slotName) {
590
+ const { slots } = await this.loadSlots();
591
+ const slot = slots[slotName];
592
+ if (!slot) return null;
593
+ const result = await this.loadBlobObject(slot.eTag);
594
+ if (!result) return null;
595
+ return this.fetchAllChunks(result.blob);
596
+ }
597
+ /**
598
+ * List all slot entries for this record.
599
+ * Returns metadata only — no chunk data is loaded.
600
+ */
601
+ async list() {
602
+ const { slots } = await this.loadSlots();
603
+ return Object.entries(slots).map(([name, slot]) => ({ name, ...slot }));
604
+ }
605
+ /**
606
+ * Delete the named slot from this record.
607
+ * Decrements refCount on the blob. Chunks are GC'd by `vault.blobGC()`.
608
+ */
609
+ async delete(slotName) {
610
+ let eTagToDecrement;
611
+ await this.casUpdateSlots((slots) => {
612
+ if (!(slotName in slots)) return null;
613
+ eTagToDecrement = slots[slotName].eTag;
614
+ delete slots[slotName];
615
+ return slots;
616
+ });
617
+ if (eTagToDecrement) {
618
+ await this.casUpdateRefCount(eTagToDecrement, -1).catch(() => {
619
+ });
620
+ }
621
+ }
622
+ /**
623
+ * Return a native `Response` whose body streams the decrypted,
624
+ * decompressed blob bytes with full HTTP metadata headers.
625
+ *
626
+ * Note: implementation is buffered — all chunks are loaded into
627
+ * memory before being enqueued. True streaming deferred to.
628
+ *
629
+ * Returns `null` if the slot does not exist.
630
+ */
631
+ async response(slotName, opts) {
632
+ const { slots } = await this.loadSlots();
633
+ const slot = slots[slotName];
634
+ if (!slot) return null;
635
+ const result = await this.loadBlobObject(slot.eTag);
636
+ if (!result) return null;
637
+ return this.buildResponse(slot, result.blob, opts);
638
+ }
639
+ /**
640
+ * Decrypt the slot and wrap the bytes in a browser ObjectURL ready
641
+ * to feed into `<img src>`, `<a href>`, etc. The caller MUST call
642
+ * `revoke()` when the URL is no longer needed — otherwise the URL
643
+ * (and the underlying decrypted Blob) are pinned for the lifetime
644
+ * of the document, which leaks memory in long-lived pages.
645
+ *
646
+ * Returns `null` when the slot does not exist.
647
+ *
648
+ * Throws when `URL.createObjectURL` is unavailable in the host
649
+ * environment (Node without DOM, restricted workers). Framework
650
+ * adapters — `useBlobURL` in `@noy-db/in-vue`, etc. — guard against
651
+ * this for SSR contexts and stay at `null` instead of propagating.
652
+ */
653
+ async objectURL(slotName, opts) {
654
+ if (typeof URL === "undefined" || typeof URL.createObjectURL !== "function") {
655
+ throw new Error(
656
+ "BlobSet.objectURL: URL.createObjectURL is unavailable in this environment. Call this from the browser, or use BlobSet.get() and create the URL yourself."
657
+ );
658
+ }
659
+ const bytes = await this.get(slotName);
660
+ if (!bytes) return null;
661
+ const { slots } = await this.loadSlots();
662
+ const slot = slots[slotName];
663
+ const type = opts?.mimeType ?? slot?.mimeType ?? "application/octet-stream";
664
+ const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
665
+ const blob = new Blob([buffer], { type });
666
+ const url = URL.createObjectURL(blob);
667
+ let revoked = false;
668
+ const revoke = () => {
669
+ if (revoked) return;
670
+ revoked = true;
671
+ URL.revokeObjectURL(url);
672
+ };
673
+ return { url, revoke };
674
+ }
675
+ // ─── Public API: Published versions (UC-3 amendment versioning) ───
676
+ /**
677
+ * Publish the current slot content as a named version snapshot.
678
+ *
679
+ * The published version holds an independent refCount reference to
680
+ * the blob. Even if the slot is later overwritten or deleted, the
681
+ * published version keeps the blob data alive.
682
+ *
683
+ * Publishing with an existing label overwrites it — if the eTags differ,
684
+ * refCounts are adjusted accordingly.
685
+ */
686
+ async publish(slotName, label) {
687
+ const { slots } = await this.loadSlots();
688
+ const slot = slots[slotName];
689
+ if (!slot) throw new NotFoundError(`Slot "${slotName}" not found on record "${this.recordId}"`);
690
+ const existing = await this.loadVersionRecord(slotName, label);
691
+ if (existing && existing.eTag === slot.eTag) return;
692
+ const record = {
693
+ label,
694
+ eTag: slot.eTag,
695
+ publishedAt: (/* @__PURE__ */ new Date()).toISOString(),
696
+ ...this.userId !== void 0 ? { publishedBy: this.userId } : {}
697
+ };
698
+ await this.writeVersionRecord(slotName, record);
699
+ await this.casUpdateRefCount(slot.eTag, 1);
700
+ if (existing && existing.eTag !== slot.eTag) {
701
+ await this.casUpdateRefCount(existing.eTag, -1).catch(() => {
702
+ });
703
+ }
704
+ }
705
+ /**
706
+ * Fetch bytes for a published version.
707
+ * Returns `null` if the version does not exist.
708
+ */
709
+ async getVersion(slotName, label) {
710
+ const record = await this.loadVersionRecord(slotName, label);
711
+ if (!record) return null;
712
+ const result = await this.loadBlobObject(record.eTag);
713
+ if (!result) return null;
714
+ return this.fetchAllChunks(result.blob);
715
+ }
716
+ /**
717
+ * List all published versions for a slot.
718
+ */
719
+ async listVersions(slotName) {
720
+ const prefix = `${this.recordId}::${slotName}::`;
721
+ const allKeys = await this.store.list(this.vault, this.versionsCollection);
722
+ const matchingKeys = allKeys.filter((k) => k.startsWith(prefix));
723
+ const versions = [];
724
+ for (const key of matchingKeys) {
725
+ const envelope = await this.store.get(this.vault, this.versionsCollection, key);
726
+ if (!envelope) continue;
727
+ if (!this.encrypted) {
728
+ versions.push(JSON.parse(envelope._data));
729
+ } else {
730
+ const dek = await this.getDEK(this.collection);
731
+ const json = await decrypt(envelope._iv, envelope._data, dek);
732
+ versions.push(JSON.parse(json));
733
+ }
734
+ }
735
+ return versions;
736
+ }
737
+ /**
738
+ * Delete a published version. Decrements refCount on its blob.
739
+ */
740
+ async deleteVersion(slotName, label) {
741
+ const record = await this.loadVersionRecord(slotName, label);
742
+ if (!record) return;
743
+ await this.deleteVersionRecord(slotName, label);
744
+ await this.casUpdateRefCount(record.eTag, -1).catch(() => {
745
+ });
746
+ }
747
+ /**
748
+ * Return a `Response` for a published version — same as `response()`
749
+ * but reads from the version record's eTag instead of the current slot.
750
+ */
751
+ async responseVersion(slotName, label, opts) {
752
+ const record = await this.loadVersionRecord(slotName, label);
753
+ if (!record) return null;
754
+ const result = await this.loadBlobObject(record.eTag);
755
+ if (!result) return null;
756
+ const slotLike = {
757
+ eTag: record.eTag,
758
+ filename: opts?.filename ?? `${slotName}-${label}`,
759
+ size: result.blob.size,
760
+ ...result.blob.mimeType !== void 0 ? { mimeType: result.blob.mimeType } : {},
761
+ uploadedAt: record.publishedAt,
762
+ ...record.publishedBy !== void 0 ? { uploadedBy: record.publishedBy } : {}
763
+ };
764
+ return this.buildResponse(slotLike, result.blob, opts);
765
+ }
766
+ // ─── Diagnostics ──────────────────────────────────────────────────
767
+ /**
768
+ * Return the `BlobObject` metadata for the named slot.
769
+ * Returns `null` if the slot or blob does not exist.
770
+ */
771
+ async blobInfo(slotName) {
772
+ const { slots } = await this.loadSlots();
773
+ const slot = slots[slotName];
774
+ if (!slot) return null;
775
+ const result = await this.loadBlobObject(slot.eTag);
776
+ return result?.blob ?? null;
777
+ }
778
+ // ─── Presigned URL (E5) ────────────────────────────────────────────
779
+ /**
780
+ * Generate a presigned URL for direct client download of the blob's
781
+ * ciphertext. Only works when the blob store supports `presignUrl`.
782
+ *
783
+ * **Important:** The URL returns encrypted data. The caller must
784
+ * decrypt client-side using `decryptResponse()` or a service worker.
785
+ *
786
+ * Returns `null` if the slot doesn't exist or the store doesn't support presigning.
787
+ */
788
+ async presignedUrl(slotName, expiresInSeconds = 3600) {
789
+ const { slots } = await this.loadSlots();
790
+ const slot = slots[slotName];
791
+ if (!slot) return null;
792
+ const result = await this.loadBlobObject(slot.eTag);
793
+ if (!result) return null;
794
+ if (result.blob.chunkCount !== 1) return null;
795
+ if (!this.store.presignUrl) return null;
796
+ const chunkId = `${slot.eTag}_0`;
797
+ return this.store.presignUrl(this.vault, "_blob_chunks", chunkId, expiresInSeconds);
798
+ }
799
+ /**
800
+ * Decrypt a ciphertext Response (e.g. from a presigned URL fetch)
801
+ * back into a plaintext Response with correct headers.
802
+ *
803
+ * Usage with service worker or client-side fetch:
804
+ * ```ts
805
+ * const url = await blobs.presignedUrl('invoice.pdf')
806
+ * const cipherResponse = await fetch(url)
807
+ * const plainResponse = await blobs.decryptResponse('invoice.pdf', cipherResponse)
808
+ * ```
809
+ */
810
+ async decryptResponse(slotName, cipherResponse) {
811
+ const { slots } = await this.loadSlots();
812
+ const slot = slots[slotName];
813
+ if (!slot) return null;
814
+ const result = await this.loadBlobObject(slot.eTag);
815
+ if (!result) return null;
816
+ const text = await cipherResponse.text();
817
+ const envelope = JSON.parse(text);
818
+ const blobDEK = this.encrypted ? await this.getDEK("_blob") : null;
819
+ if (!blobDEK) {
820
+ return this.buildResponse(slot, result.blob, { inline: true });
821
+ }
822
+ const aad = chunkAAD(slot.eTag, 0, result.blob.chunkCount);
823
+ const { decryptBytesWithAAD: decryptAAD } = await import("./crypto-IVKU7YTT.js");
824
+ const decrypted = await decryptAAD(envelope._iv, envelope._data, blobDEK, aad);
825
+ const plaintext = result.blob.compression === "gzip" ? await decompressBytes(decrypted) : decrypted;
826
+ const body = new ReadableStream({
827
+ start(controller) {
828
+ controller.enqueue(plaintext);
829
+ controller.close();
830
+ }
831
+ });
832
+ const filename = slot.filename;
833
+ return new Response(body, {
834
+ headers: {
835
+ "Content-Type": slot.mimeType ?? "application/octet-stream",
836
+ "Content-Length": String(slot.size),
837
+ "ETag": `"${slot.eTag}"`,
838
+ "Content-Disposition": `inline; filename="${filename}"`,
839
+ "Last-Modified": new Date(slot.uploadedAt).toUTCString()
840
+ }
841
+ });
842
+ }
843
+ // ─── Internal: build Response from slot + blob ────────────────────
844
+ async buildResponse(slot, blob, opts) {
845
+ const fetchAllChunks = this.fetchAllChunks.bind(this);
846
+ const body = new ReadableStream({
847
+ async start(controller) {
848
+ try {
849
+ const output = await fetchAllChunks(blob);
850
+ controller.enqueue(output);
851
+ controller.close();
852
+ } catch (err) {
853
+ controller.error(err);
854
+ }
855
+ }
856
+ });
857
+ const filename = opts?.filename ?? slot.filename;
858
+ const disposition = opts?.inline ? `inline; filename="${filename}"` : `attachment; filename="${filename}"`;
859
+ return new Response(body, {
860
+ headers: {
861
+ "Content-Type": slot.mimeType ?? "application/octet-stream",
862
+ "Content-Length": String(slot.size),
863
+ "ETag": `"${slot.eTag}"`,
864
+ "Content-Disposition": disposition,
865
+ "Last-Modified": new Date(slot.uploadedAt).toUTCString()
866
+ }
867
+ });
868
+ }
869
+ };
870
+ async function plainSha256Hex(data) {
871
+ return sha256Hex(data);
872
+ }
873
+
874
+ // src/blobs/export-blobs.ts
875
+ var ExportBlobsAbortedError = class extends Error {
876
+ constructor(reason) {
877
+ super(`exportBlobs aborted: ${reason}`);
878
+ this.name = "ExportBlobsAbortedError";
879
+ }
880
+ };
881
+ var EXPORT_AUDIT_COLLECTION = "_export_audit";
882
+ function createExportBlobsHandle(actor, listAccessibleCollections, getCollection, writeAudit, options) {
883
+ let aborted = false;
884
+ const abort = () => {
885
+ aborted = true;
886
+ };
887
+ if (options.signal) {
888
+ if (options.signal.aborted) aborted = true;
889
+ options.signal.addEventListener("abort", () => {
890
+ aborted = true;
891
+ });
892
+ }
893
+ function assertLive() {
894
+ if (aborted) throw new ExportBlobsAbortedError("aborted by caller");
895
+ }
896
+ const allowlist = options.collections ? new Set(options.collections) : null;
897
+ let auditPromise = null;
898
+ function writeAuditOnce() {
899
+ if (!auditPromise) {
900
+ auditPromise = writeAudit({
901
+ id: generateBatchId(),
902
+ mechanism: "exportBlobs",
903
+ actor,
904
+ startedAt: (/* @__PURE__ */ new Date()).toISOString(),
905
+ collections: options.collections ?? null,
906
+ predicate: Boolean(options.where),
907
+ afterBlobId: options.afterBlobId ?? null
908
+ });
909
+ }
910
+ return auditPromise;
911
+ }
912
+ async function* generate() {
913
+ await writeAuditOnce();
914
+ assertLive();
915
+ const allCollections = await listAccessibleCollections();
916
+ const targets = allCollections.filter((name) => {
917
+ if (name.startsWith("_")) return false;
918
+ if (allowlist && !allowlist.has(name)) return false;
919
+ return true;
920
+ });
921
+ let resumeCursorHit = options.afterBlobId === void 0;
922
+ for (const collectionName of targets) {
923
+ if (aborted) return;
924
+ const coll = getCollection(collectionName);
925
+ const records = await coll.list().catch(() => []);
926
+ for (const record of records) {
927
+ if (aborted) return;
928
+ assertLive();
929
+ const idField = record.id;
930
+ if (typeof idField !== "string") continue;
931
+ if (options.where && !options.where(record, { collection: collectionName, id: idField })) continue;
932
+ const blobSet = coll.blob(idField);
933
+ const slots = await blobSet.list().catch(() => []);
934
+ for (const slot of slots) {
935
+ if (aborted) return;
936
+ if (!resumeCursorHit) {
937
+ if (slot.eTag === options.afterBlobId) {
938
+ resumeCursorHit = true;
939
+ }
940
+ continue;
941
+ }
942
+ const bytes = await blobSet.get(slot.name);
943
+ if (!bytes) continue;
944
+ const item = {
945
+ blobId: slot.eTag,
946
+ recordRef: { collection: collectionName, id: idField, slot: slot.name },
947
+ bytes,
948
+ meta: {
949
+ size: slot.size,
950
+ filename: slot.filename,
951
+ ...slot.mimeType !== void 0 && { mimeType: slot.mimeType },
952
+ ...slot.uploadedAt !== void 0 && { createdAt: slot.uploadedAt }
953
+ }
954
+ };
955
+ yield item;
956
+ }
957
+ }
958
+ }
959
+ }
960
+ const handle = {
961
+ abort,
962
+ get aborted() {
963
+ return aborted;
964
+ },
965
+ [Symbol.asyncIterator]: () => generate()
966
+ };
967
+ return handle;
968
+ }
969
+ function generateBatchId() {
970
+ const raw = globalThis.crypto.getRandomValues(new Uint8Array(16));
971
+ let s = "";
972
+ for (const b of raw) s += b.toString(16).padStart(2, "0");
973
+ return `batch-${Date.now().toString(36)}-${s.slice(0, 12)}`;
974
+ }
975
+
976
+ // src/blobs/blob-compaction.ts
977
+ var BLOB_EVICTION_AUDIT_COLLECTION = "_blob_eviction_audit";
978
+ async function runCompaction(ctx, options = {}) {
979
+ const now = options.now ?? /* @__PURE__ */ new Date();
980
+ const maxEvictions = options.maxEvictions ?? Infinity;
981
+ const dryRun = options.dryRun === true;
982
+ const allCollections = await ctx.listCollections();
983
+ const byCollection = {};
984
+ let evicted = 0;
985
+ let records = 0;
986
+ let auditEntries = 0;
987
+ let collectionsWithPolicy = 0;
988
+ outer: for (const collectionName of allCollections) {
989
+ if (collectionName.startsWith("_")) continue;
990
+ const config = ctx.getBlobFields(collectionName);
991
+ if (!config) continue;
992
+ const configuredSlots = Object.keys(config);
993
+ if (configuredSlots.length === 0) continue;
994
+ collectionsWithPolicy += 1;
995
+ byCollection[collectionName] = { records: 0, evicted: 0 };
996
+ const ids = await ctx.listRecords(collectionName);
997
+ for (const recordId of ids) {
998
+ if (evicted >= maxEvictions) break outer;
999
+ const record = await ctx.getRecord(collectionName, recordId).catch(() => null);
1000
+ if (record === null) continue;
1001
+ records += 1;
1002
+ byCollection[collectionName].records += 1;
1003
+ const slots = await ctx.listSlots(collectionName, recordId).catch(() => []);
1004
+ for (const slot of slots) {
1005
+ if (evicted >= maxEvictions) break outer;
1006
+ const policy = config[slot.name];
1007
+ if (!policy) continue;
1008
+ const reason = evaluatePolicy(policy, record, slot, now);
1009
+ if (!reason) continue;
1010
+ if (!dryRun) {
1011
+ await ctx.deleteSlot(collectionName, recordId, slot.name);
1012
+ await writeAuditEntry(ctx, {
1013
+ id: generateEvictionId(collectionName, recordId, slot.name),
1014
+ collection: collectionName,
1015
+ recordId,
1016
+ slotName: slot.name,
1017
+ blobHash: slot.eTag,
1018
+ reason,
1019
+ evictedAt: now.toISOString(),
1020
+ actor: ctx.actor
1021
+ });
1022
+ auditEntries += 1;
1023
+ }
1024
+ evicted += 1;
1025
+ byCollection[collectionName].evicted += 1;
1026
+ }
1027
+ }
1028
+ }
1029
+ return {
1030
+ evicted,
1031
+ records,
1032
+ collections: collectionsWithPolicy,
1033
+ auditEntries,
1034
+ byCollection
1035
+ };
1036
+ }
1037
+ function evaluatePolicy(policy, record, slot, now) {
1038
+ let ttlTriggered = false;
1039
+ let predicateTriggered = false;
1040
+ if (policy.retainDays !== void 0 && policy.retainDays > 0) {
1041
+ const uploadedAt = Date.parse(slot.uploadedAt);
1042
+ if (Number.isFinite(uploadedAt)) {
1043
+ const ageMs = now.getTime() - uploadedAt;
1044
+ const limitMs = policy.retainDays * 864e5;
1045
+ if (ageMs > limitMs) ttlTriggered = true;
1046
+ }
1047
+ }
1048
+ if (policy.evictWhen) {
1049
+ try {
1050
+ if (policy.evictWhen(record)) predicateTriggered = true;
1051
+ } catch {
1052
+ }
1053
+ }
1054
+ if (ttlTriggered && predicateTriggered) return "both";
1055
+ if (ttlTriggered) return "ttl";
1056
+ if (predicateTriggered) return "predicate";
1057
+ return null;
1058
+ }
1059
+ function generateEvictionId(collection, recordId, slotName) {
1060
+ const rand = globalThis.crypto.getRandomValues(new Uint8Array(8));
1061
+ let suffix = "";
1062
+ for (const b of rand) suffix += b.toString(16).padStart(2, "0");
1063
+ return `${collection}__${recordId}__${slotName}__${suffix}`;
1064
+ }
1065
+ async function writeAuditEntry(ctx, entry) {
1066
+ const json = JSON.stringify(entry);
1067
+ let envelope;
1068
+ if (ctx.encrypted) {
1069
+ const dek = await ctx.getDEK(BLOB_EVICTION_AUDIT_COLLECTION);
1070
+ const { iv, data } = await encrypt(json, dek);
1071
+ envelope = {
1072
+ _noydb: NOYDB_FORMAT_VERSION,
1073
+ _v: 1,
1074
+ _ts: entry.evictedAt,
1075
+ _iv: iv,
1076
+ _data: data,
1077
+ _by: entry.actor
1078
+ };
1079
+ } else {
1080
+ envelope = {
1081
+ _noydb: NOYDB_FORMAT_VERSION,
1082
+ _v: 1,
1083
+ _ts: entry.evictedAt,
1084
+ _iv: "",
1085
+ _data: json,
1086
+ _by: entry.actor
1087
+ };
1088
+ }
1089
+ await ctx.adapter.put(ctx.vault, BLOB_EVICTION_AUDIT_COLLECTION, entry.id, envelope);
1090
+ }
1091
+
1092
+ export {
1093
+ detectMimeType,
1094
+ detectMagic,
1095
+ isPreCompressed,
1096
+ BLOB_COLLECTION,
1097
+ BLOB_INDEX_COLLECTION,
1098
+ BLOB_CHUNKS_COLLECTION,
1099
+ BLOB_SLOTS_PREFIX,
1100
+ BLOB_VERSIONS_PREFIX,
1101
+ DEFAULT_CHUNK_SIZE,
1102
+ BlobSet,
1103
+ ExportBlobsAbortedError,
1104
+ EXPORT_AUDIT_COLLECTION,
1105
+ createExportBlobsHandle,
1106
+ BLOB_EVICTION_AUDIT_COLLECTION,
1107
+ runCompaction
1108
+ };
1109
+ //# sourceMappingURL=chunk-UQFSPSWG.js.map