@noy-db/hub 0.1.0-pre.10

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 (203) 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 +496 -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 +51 -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-72UIIX3E.js +1109 -0
  24. package/dist/chunk-72UIIX3E.js.map +1 -0
  25. package/dist/chunk-A4NFZKRW.js +722 -0
  26. package/dist/chunk-A4NFZKRW.js.map +1 -0
  27. package/dist/chunk-AOYCZP2H.js +793 -0
  28. package/dist/chunk-AOYCZP2H.js.map +1 -0
  29. package/dist/chunk-CIMZBAZB.js +72 -0
  30. package/dist/chunk-CIMZBAZB.js.map +1 -0
  31. package/dist/chunk-E3AGCGJ4.js +160 -0
  32. package/dist/chunk-E3AGCGJ4.js.map +1 -0
  33. package/dist/chunk-EKX3YVCI.js +97 -0
  34. package/dist/chunk-EKX3YVCI.js.map +1 -0
  35. package/dist/chunk-EMIGCR7X.js +39 -0
  36. package/dist/chunk-EMIGCR7X.js.map +1 -0
  37. package/dist/chunk-EMMRIE3C.js +72 -0
  38. package/dist/chunk-EMMRIE3C.js.map +1 -0
  39. package/dist/chunk-EUNIORPU.js +680 -0
  40. package/dist/chunk-EUNIORPU.js.map +1 -0
  41. package/dist/chunk-FZU343FL.js +32 -0
  42. package/dist/chunk-FZU343FL.js.map +1 -0
  43. package/dist/chunk-GHGXG53C.js +795 -0
  44. package/dist/chunk-GHGXG53C.js.map +1 -0
  45. package/dist/chunk-GKA4BGJN.js +79 -0
  46. package/dist/chunk-GKA4BGJN.js.map +1 -0
  47. package/dist/chunk-HG2OWBLX.js +430 -0
  48. package/dist/chunk-HG2OWBLX.js.map +1 -0
  49. package/dist/chunk-IGAROPKM.js +34 -0
  50. package/dist/chunk-IGAROPKM.js.map +1 -0
  51. package/dist/chunk-J66GRPNH.js +111 -0
  52. package/dist/chunk-J66GRPNH.js.map +1 -0
  53. package/dist/chunk-LVMMDXFT.js +275 -0
  54. package/dist/chunk-LVMMDXFT.js.map +1 -0
  55. package/dist/chunk-M5INGEFC.js +84 -0
  56. package/dist/chunk-M5INGEFC.js.map +1 -0
  57. package/dist/chunk-NBYQNDXA.js +557 -0
  58. package/dist/chunk-NBYQNDXA.js.map +1 -0
  59. package/dist/chunk-NPC4LFV5.js +132 -0
  60. package/dist/chunk-NPC4LFV5.js.map +1 -0
  61. package/dist/chunk-NSWHB5VQ.js +1285 -0
  62. package/dist/chunk-NSWHB5VQ.js.map +1 -0
  63. package/dist/chunk-OLM4LA6K.js +392 -0
  64. package/dist/chunk-OLM4LA6K.js.map +1 -0
  65. package/dist/chunk-UAFBZWFB.js +155 -0
  66. package/dist/chunk-UAFBZWFB.js.map +1 -0
  67. package/dist/chunk-UF3BUNQZ.js +1 -0
  68. package/dist/chunk-UF3BUNQZ.js.map +1 -0
  69. package/dist/chunk-UMMAVAYW.js +17 -0
  70. package/dist/chunk-UMMAVAYW.js.map +1 -0
  71. package/dist/chunk-UPY7WLBH.js +381 -0
  72. package/dist/chunk-UPY7WLBH.js.map +1 -0
  73. package/dist/chunk-W63BWEJH.js +311 -0
  74. package/dist/chunk-W63BWEJH.js.map +1 -0
  75. package/dist/chunk-WIGI5OJK.js +90 -0
  76. package/dist/chunk-WIGI5OJK.js.map +1 -0
  77. package/dist/chunk-XNL2TKKR.js +490 -0
  78. package/dist/chunk-XNL2TKKR.js.map +1 -0
  79. package/dist/chunk-XWNUJPIS.js +367 -0
  80. package/dist/chunk-XWNUJPIS.js.map +1 -0
  81. package/dist/chunk-YWKJZZGV.js +715 -0
  82. package/dist/chunk-YWKJZZGV.js.map +1 -0
  83. package/dist/consent/index.cjs +204 -0
  84. package/dist/consent/index.cjs.map +1 -0
  85. package/dist/consent/index.d.cts +24 -0
  86. package/dist/consent/index.d.ts +24 -0
  87. package/dist/consent/index.js +23 -0
  88. package/dist/consent/index.js.map +1 -0
  89. package/dist/crdt/index.cjs +152 -0
  90. package/dist/crdt/index.cjs.map +1 -0
  91. package/dist/crdt/index.d.cts +30 -0
  92. package/dist/crdt/index.d.ts +30 -0
  93. package/dist/crdt/index.js +24 -0
  94. package/dist/crdt/index.js.map +1 -0
  95. package/dist/crypto-6PNIHP7W.js +44 -0
  96. package/dist/crypto-6PNIHP7W.js.map +1 -0
  97. package/dist/delegation-WVIVMF73.js +17 -0
  98. package/dist/delegation-WVIVMF73.js.map +1 -0
  99. package/dist/dev-unlock-D4xB0_gs.d.cts +263 -0
  100. package/dist/dev-unlock-Dz8GEbd3.d.ts +263 -0
  101. package/dist/hash--EflSV65.d.cts +63 -0
  102. package/dist/hash-CRdXYnv3.d.ts +63 -0
  103. package/dist/history/index.cjs +1215 -0
  104. package/dist/history/index.cjs.map +1 -0
  105. package/dist/history/index.d.cts +62 -0
  106. package/dist/history/index.d.ts +62 -0
  107. package/dist/history/index.js +79 -0
  108. package/dist/history/index.js.map +1 -0
  109. package/dist/i18n/index.cjs +840 -0
  110. package/dist/i18n/index.cjs.map +1 -0
  111. package/dist/i18n/index.d.cts +38 -0
  112. package/dist/i18n/index.d.ts +38 -0
  113. package/dist/i18n/index.js +68 -0
  114. package/dist/i18n/index.js.map +1 -0
  115. package/dist/index-CD1VnONm.d.cts +415 -0
  116. package/dist/index-CLRxPs-W.d.cts +1960 -0
  117. package/dist/index-CUi9wfss.d.ts +415 -0
  118. package/dist/index-DtV93TMP.d.ts +1960 -0
  119. package/dist/index.cjs +17387 -0
  120. package/dist/index.cjs.map +1 -0
  121. package/dist/index.d.cts +565 -0
  122. package/dist/index.d.ts +565 -0
  123. package/dist/index.js +7525 -0
  124. package/dist/index.js.map +1 -0
  125. package/dist/indexing/index.cjs +736 -0
  126. package/dist/indexing/index.cjs.map +1 -0
  127. package/dist/indexing/index.d.cts +36 -0
  128. package/dist/indexing/index.d.ts +36 -0
  129. package/dist/indexing/index.js +77 -0
  130. package/dist/indexing/index.js.map +1 -0
  131. package/dist/lazy-builder-BwEoBQZ9.d.ts +304 -0
  132. package/dist/lazy-builder-CZVLKh0Z.d.cts +304 -0
  133. package/dist/ledger-HBBH2NPZ.js +33 -0
  134. package/dist/ledger-HBBH2NPZ.js.map +1 -0
  135. package/dist/mime-magic-CBBSOkjm.d.cts +50 -0
  136. package/dist/mime-magic-CBBSOkjm.d.ts +50 -0
  137. package/dist/periods/index.cjs +1035 -0
  138. package/dist/periods/index.cjs.map +1 -0
  139. package/dist/periods/index.d.cts +21 -0
  140. package/dist/periods/index.d.ts +21 -0
  141. package/dist/periods/index.js +25 -0
  142. package/dist/periods/index.js.map +1 -0
  143. package/dist/predicate-SBHmi6D0.d.cts +161 -0
  144. package/dist/predicate-SBHmi6D0.d.ts +161 -0
  145. package/dist/public-envelope-TLQA6REO.js +31 -0
  146. package/dist/public-envelope-TLQA6REO.js.map +1 -0
  147. package/dist/query/index.cjs +1999 -0
  148. package/dist/query/index.cjs.map +1 -0
  149. package/dist/query/index.d.cts +3 -0
  150. package/dist/query/index.d.ts +3 -0
  151. package/dist/query/index.js +73 -0
  152. package/dist/query/index.js.map +1 -0
  153. package/dist/session/index.cjs +495 -0
  154. package/dist/session/index.cjs.map +1 -0
  155. package/dist/session/index.d.cts +45 -0
  156. package/dist/session/index.d.ts +45 -0
  157. package/dist/session/index.js +51 -0
  158. package/dist/session/index.js.map +1 -0
  159. package/dist/shadow/index.cjs +133 -0
  160. package/dist/shadow/index.cjs.map +1 -0
  161. package/dist/shadow/index.d.cts +16 -0
  162. package/dist/shadow/index.d.ts +16 -0
  163. package/dist/shadow/index.js +20 -0
  164. package/dist/shadow/index.js.map +1 -0
  165. package/dist/store/index.cjs +1083 -0
  166. package/dist/store/index.cjs.map +1 -0
  167. package/dist/store/index.d.cts +491 -0
  168. package/dist/store/index.d.ts +491 -0
  169. package/dist/store/index.js +37 -0
  170. package/dist/store/index.js.map +1 -0
  171. package/dist/strategy-BSxFXGzb.d.cts +110 -0
  172. package/dist/strategy-BSxFXGzb.d.ts +110 -0
  173. package/dist/strategy-D-SrOLCl.d.cts +548 -0
  174. package/dist/strategy-D-SrOLCl.d.ts +548 -0
  175. package/dist/sync/index.cjs +1062 -0
  176. package/dist/sync/index.cjs.map +1 -0
  177. package/dist/sync/index.d.cts +42 -0
  178. package/dist/sync/index.d.ts +42 -0
  179. package/dist/sync/index.js +28 -0
  180. package/dist/sync/index.js.map +1 -0
  181. package/dist/team/index.cjs +2606 -0
  182. package/dist/team/index.cjs.map +1 -0
  183. package/dist/team/index.d.cts +117 -0
  184. package/dist/team/index.d.ts +117 -0
  185. package/dist/team/index.js +106 -0
  186. package/dist/team/index.js.map +1 -0
  187. package/dist/tx/index.cjs +212 -0
  188. package/dist/tx/index.cjs.map +1 -0
  189. package/dist/tx/index.d.cts +20 -0
  190. package/dist/tx/index.d.ts +20 -0
  191. package/dist/tx/index.js +20 -0
  192. package/dist/tx/index.js.map +1 -0
  193. package/dist/types-DSFLtbKg.d.ts +9702 -0
  194. package/dist/types-zwwMOqkg.d.cts +9702 -0
  195. package/dist/ulid-COREQ2RQ.js +9 -0
  196. package/dist/ulid-COREQ2RQ.js.map +1 -0
  197. package/dist/util/index.cjs +230 -0
  198. package/dist/util/index.cjs.map +1 -0
  199. package/dist/util/index.d.cts +77 -0
  200. package/dist/util/index.d.ts +77 -0
  201. package/dist/util/index.js +190 -0
  202. package/dist/util/index.js.map +1 -0
  203. package/package.json +244 -0
@@ -0,0 +1,840 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/i18n/index.ts
21
+ var i18n_exports = {};
22
+ __export(i18n_exports, {
23
+ DICT_COLLECTION_PREFIX: () => DICT_COLLECTION_PREFIX,
24
+ DictKeyInUseError: () => DictKeyInUseError,
25
+ DictKeyMissingError: () => DictKeyMissingError,
26
+ DictionaryHandle: () => DictionaryHandle,
27
+ LocaleNotSpecifiedError: () => LocaleNotSpecifiedError,
28
+ MissingTranslationError: () => MissingTranslationError,
29
+ ReservedCollectionNameError: () => ReservedCollectionNameError,
30
+ TranslatorNotConfiguredError: () => TranslatorNotConfiguredError,
31
+ applyI18nLocale: () => applyI18nLocale,
32
+ dictCollectionName: () => dictCollectionName,
33
+ dictKey: () => dictKey,
34
+ i18nText: () => i18nText,
35
+ isDictCollectionName: () => isDictCollectionName,
36
+ isDictKeyDescriptor: () => isDictKeyDescriptor,
37
+ isI18nTextDescriptor: () => isI18nTextDescriptor,
38
+ resolveI18nText: () => resolveI18nText,
39
+ validateI18nTextValue: () => validateI18nTextValue,
40
+ withI18n: () => withI18n
41
+ });
42
+ module.exports = __toCommonJS(i18n_exports);
43
+
44
+ // src/errors.ts
45
+ var NoydbError = class extends Error {
46
+ /** Machine-readable error code. Stable across library versions. */
47
+ code;
48
+ constructor(code, message) {
49
+ super(message);
50
+ this.name = "NoydbError";
51
+ this.code = code;
52
+ }
53
+ };
54
+ var DecryptionError = class extends NoydbError {
55
+ constructor(message = "Decryption failed") {
56
+ super("DECRYPTION_FAILED", message);
57
+ this.name = "DecryptionError";
58
+ }
59
+ };
60
+ var TamperedError = class extends NoydbError {
61
+ constructor(message = "Data integrity check failed \u2014 record may have been tampered with") {
62
+ super("TAMPERED", message);
63
+ this.name = "TamperedError";
64
+ }
65
+ };
66
+ var PermissionDeniedError = class extends NoydbError {
67
+ constructor(message = "Permission denied \u2014 insufficient role for this operation") {
68
+ super("PERMISSION_DENIED", message);
69
+ this.name = "PermissionDeniedError";
70
+ }
71
+ };
72
+ var ValidationError = class extends NoydbError {
73
+ constructor(message = "Validation error") {
74
+ super("VALIDATION_ERROR", message);
75
+ this.name = "ValidationError";
76
+ }
77
+ };
78
+ var ReservedCollectionNameError = class extends NoydbError {
79
+ /** The rejected collection name. */
80
+ collectionName;
81
+ constructor(collectionName) {
82
+ super(
83
+ "RESERVED_COLLECTION_NAME",
84
+ `"${collectionName}" is a reserved collection name. Use vault.dictionary("${collectionName.replace(/^_dict_/, "")}") to access dictionary collections.`
85
+ );
86
+ this.name = "ReservedCollectionNameError";
87
+ this.collectionName = collectionName;
88
+ }
89
+ };
90
+ var DictKeyMissingError = class extends NoydbError {
91
+ /** The dictionary name. */
92
+ dictionaryName;
93
+ /** The key that was not found. */
94
+ key;
95
+ constructor(dictionaryName, key) {
96
+ super(
97
+ "DICT_KEY_MISSING",
98
+ `Dictionary "${dictionaryName}" has no entry for key "${key}".`
99
+ );
100
+ this.name = "DictKeyMissingError";
101
+ this.dictionaryName = dictionaryName;
102
+ this.key = key;
103
+ }
104
+ };
105
+ var DictKeyInUseError = class extends NoydbError {
106
+ /** The dictionary name. */
107
+ dictionaryName;
108
+ /** The key that is still referenced. */
109
+ key;
110
+ /** Name of the first collection found to reference this key. */
111
+ usedBy;
112
+ /** Number of records in `usedBy` that reference this key. */
113
+ count;
114
+ constructor(dictionaryName, key, usedBy, count) {
115
+ super(
116
+ "DICT_KEY_IN_USE",
117
+ `Cannot delete key "${key}" from dictionary "${dictionaryName}": ${count} record(s) in "${usedBy}" still reference it. Use dictionary.rename("${key}", newKey) to rewrite references first.`
118
+ );
119
+ this.name = "DictKeyInUseError";
120
+ this.dictionaryName = dictionaryName;
121
+ this.key = key;
122
+ this.usedBy = usedBy;
123
+ this.count = count;
124
+ }
125
+ };
126
+ var MissingTranslationError = class extends NoydbError {
127
+ /** The field name whose translation(s) are missing. */
128
+ field;
129
+ /** Locale codes that were required but absent. */
130
+ missing;
131
+ constructor(field, missing, message) {
132
+ super(
133
+ "MISSING_TRANSLATION",
134
+ message ?? `Field "${field}": missing required translation(s): ${missing.join(", ")}.`
135
+ );
136
+ this.name = "MissingTranslationError";
137
+ this.field = field;
138
+ this.missing = missing;
139
+ }
140
+ };
141
+ var LocaleNotSpecifiedError = class extends NoydbError {
142
+ /** The field name that required a locale. */
143
+ field;
144
+ constructor(field, message) {
145
+ super(
146
+ "LOCALE_NOT_SPECIFIED",
147
+ message ?? `Cannot read i18nText field "${field}" without a locale. Pass { locale } to get()/list()/query() or set a default via openVault(name, { locale }).`
148
+ );
149
+ this.name = "LocaleNotSpecifiedError";
150
+ this.field = field;
151
+ }
152
+ };
153
+ var TranslatorNotConfiguredError = class extends NoydbError {
154
+ /** The field that requested auto-translation. */
155
+ field;
156
+ /** The collection the put was targeting. */
157
+ collection;
158
+ constructor(field, collection) {
159
+ super(
160
+ "TRANSLATOR_NOT_CONFIGURED",
161
+ `Field "${field}" in collection "${collection}" has autoTranslate: true, but no plaintextTranslator was configured on createNoydb(). Either configure a plaintextTranslator or remove autoTranslate from the schema.`
162
+ );
163
+ this.name = "TranslatorNotConfiguredError";
164
+ this.field = field;
165
+ this.collection = collection;
166
+ }
167
+ };
168
+
169
+ // src/i18n/core.ts
170
+ function i18nText(options) {
171
+ return { _noydbI18nText: true, options };
172
+ }
173
+ function isI18nTextDescriptor(x) {
174
+ return typeof x === "object" && x !== null && x._noydbI18nText === true;
175
+ }
176
+ function validateI18nTextValue(value, field, descriptor) {
177
+ const { options } = descriptor;
178
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
179
+ throw new MissingTranslationError(
180
+ field,
181
+ options.languages,
182
+ `Field "${field}" must be a { [locale]: string } map, got ${typeof value}.`
183
+ );
184
+ }
185
+ const map = value;
186
+ for (const [locale, v] of Object.entries(map)) {
187
+ if (typeof v !== "string") {
188
+ throw new MissingTranslationError(
189
+ field,
190
+ [locale],
191
+ `Field "${field}": locale "${locale}" must be a string, got ${typeof v}.`
192
+ );
193
+ }
194
+ }
195
+ const { required } = options;
196
+ if (required === "all") {
197
+ const missing = options.languages.filter(
198
+ (lang) => !(lang in map) || map[lang] === ""
199
+ );
200
+ if (missing.length > 0) {
201
+ throw new MissingTranslationError(
202
+ field,
203
+ missing,
204
+ `Field "${field}" requires all declared languages. Missing: ${missing.join(", ")}.`
205
+ );
206
+ }
207
+ } else if (required === "any") {
208
+ const present = options.languages.some(
209
+ (lang) => lang in map && map[lang] !== ""
210
+ );
211
+ if (!present) {
212
+ throw new MissingTranslationError(
213
+ field,
214
+ options.languages,
215
+ `Field "${field}" requires at least one declared language. None present.`
216
+ );
217
+ }
218
+ } else {
219
+ const requiredList = required;
220
+ const missing = requiredList.filter(
221
+ (lang) => !(lang in map) || map[lang] === ""
222
+ );
223
+ if (missing.length > 0) {
224
+ throw new MissingTranslationError(
225
+ field,
226
+ missing,
227
+ `Field "${field}" requires: ${requiredList.join(", ")}. Missing: ${missing.join(", ")}.`
228
+ );
229
+ }
230
+ }
231
+ }
232
+ function resolveI18nText(value, locale, fallback, field) {
233
+ if (locale === "raw") {
234
+ return value;
235
+ }
236
+ if (!locale) {
237
+ throw new LocaleNotSpecifiedError(field ?? "<unknown>");
238
+ }
239
+ if (value[locale] !== void 0 && value[locale] !== "") {
240
+ return value[locale];
241
+ }
242
+ const chain = Array.isArray(fallback) ? fallback : fallback ? [fallback] : [];
243
+ for (const fb of chain) {
244
+ if (fb === "any") {
245
+ const any = Object.values(value).find((v) => v !== "");
246
+ if (any !== void 0) return any;
247
+ } else if (value[fb] !== void 0 && value[fb] !== "") {
248
+ return value[fb];
249
+ }
250
+ }
251
+ throw new LocaleNotSpecifiedError(
252
+ field ?? "<unknown>",
253
+ `No translation available for locale "${locale}"` + (chain.length > 0 ? ` or fallback chain [${chain.join(", ")}]` : "") + "."
254
+ );
255
+ }
256
+ function applyI18nLocale(record, i18nFields, locale, fallback) {
257
+ const fieldNames = Object.keys(i18nFields);
258
+ if (fieldNames.length === 0) return record;
259
+ const result = { ...record };
260
+ for (const field of fieldNames) {
261
+ const raw = result[field];
262
+ if (raw === void 0 || raw === null) continue;
263
+ if (typeof raw !== "object" || Array.isArray(raw)) continue;
264
+ result[field] = resolveI18nText(
265
+ raw,
266
+ locale,
267
+ fallback,
268
+ field
269
+ );
270
+ }
271
+ return result;
272
+ }
273
+
274
+ // src/types.ts
275
+ var NOYDB_FORMAT_VERSION = 1;
276
+ var NOYDB_KEYRING_VERSION = 1;
277
+
278
+ // src/crypto.ts
279
+ var IV_BYTES = 12;
280
+ var KEY_BITS = 256;
281
+ var subtle = globalThis.crypto.subtle;
282
+ async function generateDEK() {
283
+ return subtle.generateKey(
284
+ { name: "AES-GCM", length: KEY_BITS },
285
+ true,
286
+ // extractable — needed for AES-KW wrapping
287
+ ["encrypt", "decrypt"]
288
+ );
289
+ }
290
+ async function wrapKey(dek, kek) {
291
+ const wrapped = await subtle.wrapKey("raw", dek, kek, "AES-KW");
292
+ return bufferToBase64(wrapped);
293
+ }
294
+ async function encrypt(plaintext, dek) {
295
+ const iv = generateIV();
296
+ const encoded = new TextEncoder().encode(plaintext);
297
+ const ciphertext = await subtle.encrypt(
298
+ { name: "AES-GCM", iv },
299
+ dek,
300
+ encoded
301
+ );
302
+ return {
303
+ iv: bufferToBase64(iv),
304
+ data: bufferToBase64(ciphertext)
305
+ };
306
+ }
307
+ async function decrypt(ivBase64, dataBase64, dek) {
308
+ const iv = base64ToBuffer(ivBase64);
309
+ const ciphertext = base64ToBuffer(dataBase64);
310
+ try {
311
+ const plaintext = await subtle.decrypt(
312
+ { name: "AES-GCM", iv },
313
+ dek,
314
+ ciphertext
315
+ );
316
+ return new TextDecoder().decode(plaintext);
317
+ } catch (err) {
318
+ if (err instanceof Error && err.name === "OperationError") {
319
+ throw new TamperedError();
320
+ }
321
+ throw new DecryptionError(
322
+ err instanceof Error ? err.message : "Decryption failed"
323
+ );
324
+ }
325
+ }
326
+ function generateIV() {
327
+ return globalThis.crypto.getRandomValues(new Uint8Array(IV_BYTES));
328
+ }
329
+ function bufferToBase64(buffer) {
330
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
331
+ let binary = "";
332
+ for (let i = 0; i < bytes.length; i++) {
333
+ binary += String.fromCharCode(bytes[i]);
334
+ }
335
+ return btoa(binary);
336
+ }
337
+ function base64ToBuffer(base64) {
338
+ const binary = atob(base64);
339
+ const bytes = new Uint8Array(binary.length);
340
+ for (let i = 0; i < binary.length; i++) {
341
+ bytes[i] = binary.charCodeAt(i);
342
+ }
343
+ return bytes;
344
+ }
345
+
346
+ // src/team/keyring.ts
347
+ var CANARY_PLAINTEXT_BYTES = new Uint8Array(32);
348
+ var canaryKeyPromise = null;
349
+ function getCanaryKey() {
350
+ if (canaryKeyPromise === null) {
351
+ canaryKeyPromise = globalThis.crypto.subtle.importKey(
352
+ "raw",
353
+ CANARY_PLAINTEXT_BYTES,
354
+ { name: "AES-GCM", length: 256 },
355
+ true,
356
+ // extractable so AES-KW can wrap it
357
+ ["encrypt", "decrypt"]
358
+ );
359
+ }
360
+ return canaryKeyPromise;
361
+ }
362
+ async function mintKeyringCanary(kek) {
363
+ const canaryKey = await getCanaryKey();
364
+ return wrapKey(canaryKey, kek);
365
+ }
366
+ async function ensureCollectionDEK(adapter, vault, keyring) {
367
+ const inFlight = /* @__PURE__ */ new Map();
368
+ return async (collectionName) => {
369
+ const existing = keyring.deks.get(collectionName);
370
+ if (existing) return existing;
371
+ const pending = inFlight.get(collectionName);
372
+ if (pending) return pending;
373
+ const promise = (async () => {
374
+ const dek = await generateDEK();
375
+ keyring.deks.set(collectionName, dek);
376
+ await persistKeyring(adapter, vault, keyring);
377
+ return dek;
378
+ })();
379
+ inFlight.set(collectionName, promise);
380
+ try {
381
+ return await promise;
382
+ } finally {
383
+ inFlight.delete(collectionName);
384
+ }
385
+ };
386
+ }
387
+ async function persistKeyring(adapter, vault, keyring) {
388
+ if (!keyring.kek) {
389
+ throw new ValidationError(
390
+ "persistKeyring: keyring.kek is null \u2014 cannot wrap DEKs without the KEK. This typically means the keyring was opened via tier-3 PIN resume, session restore, or a wrap-DEKs tier-2 unlock. Re-authenticate at tier 1 (passphrase) before persisting."
391
+ );
392
+ }
393
+ const wrappedDeks = {};
394
+ for (const [collName, dek] of keyring.deks) {
395
+ wrappedDeks[collName] = await wrapKey(dek, keyring.kek);
396
+ }
397
+ const canary = await mintKeyringCanary(keyring.kek);
398
+ const keyringFile = {
399
+ _noydb_keyring: NOYDB_KEYRING_VERSION,
400
+ user_id: keyring.userId,
401
+ display_name: keyring.displayName,
402
+ role: keyring.role,
403
+ permissions: keyring.permissions,
404
+ deks: wrappedDeks,
405
+ salt: bufferToBase64(keyring.salt),
406
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
407
+ granted_by: keyring.userId,
408
+ canary,
409
+ ...keyring.exportCapability !== void 0 && { export_capability: keyring.exportCapability },
410
+ ...keyring.importCapability !== void 0 && { import_capability: keyring.importCapability },
411
+ ...keyring.authenticators.length > 0 && { authenticators: keyring.authenticators },
412
+ ...keyring.policy !== void 0 && { policy: keyring.policy }
413
+ };
414
+ await writeKeyringFile(adapter, vault, keyring.userId, keyringFile);
415
+ }
416
+ async function writeKeyringFile(adapter, vault, userId, keyringFile) {
417
+ const envelope = {
418
+ _noydb: 1,
419
+ _v: 1,
420
+ _ts: (/* @__PURE__ */ new Date()).toISOString(),
421
+ _iv: "",
422
+ _data: JSON.stringify(keyringFile)
423
+ };
424
+ await adapter.put(vault, "_keyring", userId, envelope);
425
+ }
426
+
427
+ // src/history/ledger/entry.ts
428
+ async function sha256Hex(input) {
429
+ const bytes = new TextEncoder().encode(input);
430
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
431
+ return bytesToHex(new Uint8Array(digest));
432
+ }
433
+ function bytesToHex(bytes) {
434
+ const hex = new Array(bytes.length);
435
+ for (let i = 0; i < bytes.length; i++) {
436
+ hex[i] = (bytes[i] ?? 0).toString(16).padStart(2, "0");
437
+ }
438
+ return hex.join("");
439
+ }
440
+
441
+ // src/history/ledger/hash.ts
442
+ async function envelopePayloadHash(envelope) {
443
+ if (!envelope) return "";
444
+ return sha256Hex(envelope._data);
445
+ }
446
+
447
+ // src/i18n/dictionary.ts
448
+ var DICT_COLLECTION_PREFIX = "_dict_";
449
+ function dictCollectionName(dictionaryName) {
450
+ return `${DICT_COLLECTION_PREFIX}${dictionaryName}`;
451
+ }
452
+ function isDictCollectionName(name) {
453
+ return name.startsWith(DICT_COLLECTION_PREFIX);
454
+ }
455
+ function dictKey(name, keys) {
456
+ return { _noydbDictKey: true, name, keys };
457
+ }
458
+ function isDictKeyDescriptor(x) {
459
+ return typeof x === "object" && x !== null && x._noydbDictKey === true;
460
+ }
461
+ var DictionaryHandle = class {
462
+ constructor(adapter, compartmentName, dictionaryName, keyring, getDEK, encrypted, ledger, options, findAndUpdateReferences, emitter) {
463
+ this.adapter = adapter;
464
+ this.compartmentName = compartmentName;
465
+ this.dictionaryName = dictionaryName;
466
+ this.keyring = keyring;
467
+ this.getDEK = getDEK;
468
+ this.encrypted = encrypted;
469
+ this.ledger = ledger;
470
+ this.options = options;
471
+ this.findAndUpdateReferences = findAndUpdateReferences;
472
+ this.emitter = emitter;
473
+ this.collName = dictCollectionName(dictionaryName);
474
+ }
475
+ adapter;
476
+ compartmentName;
477
+ dictionaryName;
478
+ keyring;
479
+ getDEK;
480
+ encrypted;
481
+ ledger;
482
+ options;
483
+ findAndUpdateReferences;
484
+ emitter;
485
+ collName;
486
+ /**
487
+ * Synchronous write-through cache for dict-join support.
488
+ * Populated on every `put()`, `delete()`, and `rename()`. The snapshot
489
+ * is built from this cache by `snapshotEntries()` — the query executor
490
+ * calls this synchronously inside `.toArray()`.
491
+ *
492
+ * `null` means "not yet initialized" — callers should use `list()`
493
+ * to warm the cache before using dict joins on pre-existing data.
494
+ */
495
+ _syncCache = /* @__PURE__ */ new Map();
496
+ /**
497
+ * Return all cached entries as `{ key, labels, ...labels }` records —
498
+ * usable synchronously by the join executor's `snapshot()` call.
499
+ * Returns an empty array when the cache has never been populated.
500
+ */
501
+ snapshotEntries() {
502
+ return Array.from(this._syncCache.values()).map((e) => ({
503
+ key: e.key,
504
+ labels: e.labels,
505
+ ...e.labels
506
+ }));
507
+ }
508
+ // ─── Access checks ────────────────────────────────────────────────
509
+ requireWriteAccess() {
510
+ const minRole = this.options.writableBy ?? "admin";
511
+ const roleRank = {
512
+ client: 1,
513
+ viewer: 2,
514
+ operator: 3,
515
+ admin: 4,
516
+ owner: 5
517
+ };
518
+ const callerRank = roleRank[this.keyring.role] ?? 0;
519
+ const requiredRank = roleRank[minRole] ?? 4;
520
+ if (callerRank < requiredRank) {
521
+ throw new PermissionDeniedError(
522
+ `Dictionary "${this.dictionaryName}" writes require "${minRole}" role or above. Current role: "${this.keyring.role}".`
523
+ );
524
+ }
525
+ }
526
+ // ─── Internal helpers ─────────────────────────────────────────────
527
+ async getDekForDict() {
528
+ const resolve = await ensureCollectionDEK(
529
+ this.adapter,
530
+ this.compartmentName,
531
+ this.keyring
532
+ );
533
+ return resolve(this.collName);
534
+ }
535
+ async encryptEntry(entry, version) {
536
+ if (!this.encrypted) {
537
+ return {
538
+ _noydb: NOYDB_FORMAT_VERSION,
539
+ _v: version,
540
+ _ts: (/* @__PURE__ */ new Date()).toISOString(),
541
+ _iv: "",
542
+ _data: JSON.stringify(entry),
543
+ _by: this.keyring.userId
544
+ };
545
+ }
546
+ const dek = await this.getDekForDict();
547
+ const { iv, data } = await encrypt(JSON.stringify(entry), dek);
548
+ return {
549
+ _noydb: NOYDB_FORMAT_VERSION,
550
+ _v: version,
551
+ _ts: (/* @__PURE__ */ new Date()).toISOString(),
552
+ _iv: iv,
553
+ _data: data,
554
+ _by: this.keyring.userId
555
+ };
556
+ }
557
+ async decryptEntry(envelope) {
558
+ if (!this.encrypted) {
559
+ return JSON.parse(envelope._data);
560
+ }
561
+ const dek = await this.getDekForDict();
562
+ const json = await decrypt(envelope._iv, envelope._data, dek);
563
+ return JSON.parse(json);
564
+ }
565
+ // ─── Public API ───────────────────────────────────────────────────
566
+ /**
567
+ * Add or overwrite a single dictionary entry.
568
+ *
569
+ * @param key The stable key to store (e.g. `'paid'`).
570
+ * @param labels Locale → label map (e.g. `{ en: 'Paid', th: 'ชำระแล้ว' }`).
571
+ */
572
+ async put(key, labels) {
573
+ this.requireWriteAccess();
574
+ const entry = { key, labels };
575
+ const existing = await this.adapter.get(
576
+ this.compartmentName,
577
+ this.collName,
578
+ key
579
+ );
580
+ const version = existing ? existing._v + 1 : 1;
581
+ const envelope = await this.encryptEntry(entry, version);
582
+ await this.adapter.put(
583
+ this.compartmentName,
584
+ this.collName,
585
+ key,
586
+ envelope,
587
+ existing ? existing._v : void 0
588
+ );
589
+ this._syncCache.set(key, entry);
590
+ this.emitter.emit("change", {
591
+ vault: this.compartmentName,
592
+ collection: this.collName,
593
+ id: key,
594
+ action: "put"
595
+ });
596
+ if (this.ledger) {
597
+ await this.ledger.append({
598
+ op: "put",
599
+ collection: this.collName,
600
+ id: key,
601
+ version,
602
+ actor: this.keyring.userId,
603
+ // — must be the real envelope hash so
604
+ // vault.verifyBackupIntegrity()'s data-cross-check matches.
605
+ payloadHash: await envelopePayloadHash(envelope)
606
+ });
607
+ }
608
+ }
609
+ /**
610
+ * Batch-add or overwrite multiple dictionary entries in one call.
611
+ *
612
+ * @param entries `{ key: { locale: label } }` map.
613
+ */
614
+ async putAll(entries) {
615
+ this.requireWriteAccess();
616
+ for (const [key, labels] of Object.entries(entries)) {
617
+ await this.put(key, labels);
618
+ }
619
+ }
620
+ /**
621
+ * Load the label map for a single key.
622
+ *
623
+ * @returns The label map, or `null` if the key doesn't exist.
624
+ */
625
+ async get(key) {
626
+ const envelope = await this.adapter.get(
627
+ this.compartmentName,
628
+ this.collName,
629
+ key
630
+ );
631
+ if (!envelope) return null;
632
+ const entry = await this.decryptEntry(envelope);
633
+ return entry.labels;
634
+ }
635
+ /**
636
+ * Delete a dictionary key.
637
+ *
638
+ * Default mode is `'strict'` — throws `DictKeyInUseError` if any
639
+ * registered collection has a record referencing this key. Pass
640
+ * `{ mode: 'warn' }` to skip the check (dev-mode cleanup only).
641
+ */
642
+ async delete(key, opts = {}) {
643
+ this.requireWriteAccess();
644
+ const existing = await this.adapter.get(
645
+ this.compartmentName,
646
+ this.collName,
647
+ key
648
+ );
649
+ if (!existing) {
650
+ throw new DictKeyMissingError(this.dictionaryName, key);
651
+ }
652
+ const mode = opts.mode ?? "strict";
653
+ if (mode === "strict" && this.findAndUpdateReferences) {
654
+ }
655
+ await this.adapter.delete(this.compartmentName, this.collName, key);
656
+ this._syncCache.delete(key);
657
+ this.emitter.emit("change", {
658
+ vault: this.compartmentName,
659
+ collection: this.collName,
660
+ id: key,
661
+ action: "delete"
662
+ });
663
+ if (this.ledger) {
664
+ await this.ledger.append({
665
+ op: "delete",
666
+ collection: this.collName,
667
+ id: key,
668
+ version: existing._v,
669
+ actor: this.keyring.userId,
670
+ // — for delete the prior envelope is what was just
671
+ // removed; we hash it so the chain captures intent. The
672
+ // verifyBackupIntegrity data-cross-check skips delete
673
+ // entries entirely (the live record is gone), but the
674
+ // chain still benefits from a stable non-empty hash.
675
+ payloadHash: await envelopePayloadHash(existing)
676
+ });
677
+ }
678
+ }
679
+ /**
680
+ * Rename a dictionary key — the only sanctioned mass-mutation path.
681
+ *
682
+ * Atomically:
683
+ * 1. Adds the new key with the same labels as the old key.
684
+ * 2. Updates every registered record that stores the old key to
685
+ * store the new key instead.
686
+ * 3. Deletes the old key.
687
+ * 4. Appends a single ledger entry recording the rename.
688
+ *
689
+ * Respects ACL: throws `PermissionDeniedError` before any mutation
690
+ * if the caller can't write. The cascade is best-effort atomic
691
+ * within this call — no two-phase commit across adapter calls.
692
+ *
693
+ * Cascade-on-delete is NOT supported. Use `rename()` when you need
694
+ * to change a key that records reference.
695
+ */
696
+ async rename(oldKey, newKey) {
697
+ this.requireWriteAccess();
698
+ const existing = await this.adapter.get(
699
+ this.compartmentName,
700
+ this.collName,
701
+ oldKey
702
+ );
703
+ if (!existing) {
704
+ throw new DictKeyMissingError(this.dictionaryName, oldKey);
705
+ }
706
+ const oldEntry = await this.decryptEntry(existing);
707
+ const newEntry = { key: newKey, labels: oldEntry.labels };
708
+ const newEnvelope = await this.encryptEntry(newEntry, 1);
709
+ await this.adapter.put(
710
+ this.compartmentName,
711
+ this.collName,
712
+ newKey,
713
+ newEnvelope
714
+ );
715
+ if (this.findAndUpdateReferences) {
716
+ await this.findAndUpdateReferences(this.dictionaryName, oldKey, newKey);
717
+ }
718
+ await this.adapter.delete(this.compartmentName, this.collName, oldKey);
719
+ this._syncCache.delete(oldKey);
720
+ this._syncCache.set(newKey, newEntry);
721
+ this.emitter.emit("change", {
722
+ vault: this.compartmentName,
723
+ collection: this.collName,
724
+ id: oldKey,
725
+ action: "delete"
726
+ });
727
+ this.emitter.emit("change", {
728
+ vault: this.compartmentName,
729
+ collection: this.collName,
730
+ id: newKey,
731
+ action: "put"
732
+ });
733
+ if (this.ledger) {
734
+ await this.ledger.append({
735
+ op: "delete",
736
+ collection: this.collName,
737
+ id: oldKey,
738
+ version: existing._v,
739
+ actor: this.keyring.userId,
740
+ payloadHash: await envelopePayloadHash(existing)
741
+ });
742
+ await this.ledger.append({
743
+ op: "put",
744
+ collection: this.collName,
745
+ id: newKey,
746
+ version: 1,
747
+ actor: this.keyring.userId,
748
+ payloadHash: await envelopePayloadHash(newEnvelope)
749
+ });
750
+ }
751
+ }
752
+ /**
753
+ * List all entries in this dictionary.
754
+ *
755
+ * @returns Array of `{ key, labels }` objects.
756
+ */
757
+ async list() {
758
+ const keys = await this.adapter.list(this.compartmentName, this.collName);
759
+ const entries = [];
760
+ for (const key of keys) {
761
+ const envelope = await this.adapter.get(
762
+ this.compartmentName,
763
+ this.collName,
764
+ key
765
+ );
766
+ if (!envelope) continue;
767
+ const entry = await this.decryptEntry(envelope);
768
+ entries.push(entry);
769
+ this._syncCache.set(key, entry);
770
+ }
771
+ return entries;
772
+ }
773
+ /**
774
+ * Resolve a key to its label for the given locale.
775
+ *
776
+ * Used by the collection's locale-aware read path to populate
777
+ * `<field>Label` virtual fields. Returns `undefined` when the
778
+ * key doesn't exist or has no label for the requested locale
779
+ * (after exhausting the fallback chain).
780
+ */
781
+ async resolveLabel(key, locale, fallback) {
782
+ const labels = await this.get(key);
783
+ if (!labels) return void 0;
784
+ if (labels[locale] !== void 0) return labels[locale];
785
+ const chain = Array.isArray(fallback) ? fallback : fallback ? [fallback] : [];
786
+ for (const fb of chain) {
787
+ if (fb === "any") {
788
+ const any = Object.values(labels)[0];
789
+ if (any !== void 0) return any;
790
+ } else if (labels[fb] !== void 0) {
791
+ return labels[fb];
792
+ }
793
+ }
794
+ return void 0;
795
+ }
796
+ };
797
+
798
+ // src/i18n/active.ts
799
+ function withI18n() {
800
+ return {
801
+ applyI18nLocale,
802
+ validateI18nTextValue,
803
+ buildDictionaryHandle(opts) {
804
+ return new DictionaryHandle(
805
+ opts.adapter,
806
+ opts.compartmentName,
807
+ opts.dictionaryName,
808
+ opts.keyring,
809
+ opts.getDEK,
810
+ opts.encrypted,
811
+ opts.ledger,
812
+ opts.options,
813
+ opts.findAndUpdateReferences,
814
+ opts.emitter
815
+ );
816
+ }
817
+ };
818
+ }
819
+ // Annotate the CommonJS export names for ESM import in node:
820
+ 0 && (module.exports = {
821
+ DICT_COLLECTION_PREFIX,
822
+ DictKeyInUseError,
823
+ DictKeyMissingError,
824
+ DictionaryHandle,
825
+ LocaleNotSpecifiedError,
826
+ MissingTranslationError,
827
+ ReservedCollectionNameError,
828
+ TranslatorNotConfiguredError,
829
+ applyI18nLocale,
830
+ dictCollectionName,
831
+ dictKey,
832
+ i18nText,
833
+ isDictCollectionName,
834
+ isDictKeyDescriptor,
835
+ isI18nTextDescriptor,
836
+ resolveI18nText,
837
+ validateI18nTextValue,
838
+ withI18n
839
+ });
840
+ //# sourceMappingURL=index.cjs.map