@delali/narsil 0.2.4-next.59 → 0.3.1-next.101

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 (165) hide show
  1. package/README.md +9 -8
  2. package/dist/adapters/filesystem.d.ts +1 -1
  3. package/dist/adapters/filesystem.mjs +1 -1
  4. package/dist/adapters/indexeddb.d.ts +1 -1
  5. package/dist/adapters/indexeddb.mjs +1 -1
  6. package/dist/adapters/memory.d.ts +1 -1
  7. package/dist/{adapters-79OIa4xm.d.ts → adapters-BQFQ4PRz.d.ts} +27 -1
  8. package/dist/browser/chunk-BDOSMEEP.mjs +493 -0
  9. package/dist/browser/chunk-EJDUFWSO.mjs +602 -0
  10. package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
  11. package/dist/browser/chunk-JOVZ6HMT.mjs +4358 -0
  12. package/dist/browser/chunk-JPTQZWJQ.mjs +10 -0
  13. package/dist/browser/chunk-KPJMDPHG.mjs +24558 -0
  14. package/dist/browser/chunk-PGIM3HIM.mjs +2743 -0
  15. package/dist/browser/chunk-RHG4O77A.mjs +206 -0
  16. package/dist/browser/chunk-RIJSLY2I.mjs +360 -0
  17. package/dist/browser/durability-wiring-SRUYT3VV.mjs +3875 -0
  18. package/dist/browser/module-CBB2ETYP.mjs +3111 -0
  19. package/dist/browser/release-76MXRPGV.mjs +33 -0
  20. package/dist/browser/replace-RURXTGVG.mjs +57 -0
  21. package/dist/browser/restore-queue-EEZQO6RF.mjs +35 -0
  22. package/dist/browser/runtime-7ZAHCHWR.mjs +557 -0
  23. package/dist/browser/snapshot-OAAA5UJ7.mjs +1210 -0
  24. package/dist/browser/write-7D6VIK6O.mjs +208 -0
  25. package/dist/{capabilities-DSsOGz6F.d.ts → capabilities-DiGlB6Sr.d.ts} +68 -7
  26. package/dist/chunk-2RIRGOUL.mjs +113 -0
  27. package/dist/chunk-37CD3OKS.mjs +87 -0
  28. package/dist/chunk-44W3CBNG.mjs +7 -0
  29. package/dist/chunk-46D6JQIG.mjs +426 -0
  30. package/dist/{chunk-VMRR3DQU.mjs → chunk-5JNA5SKS.mjs} +546 -35
  31. package/dist/chunk-747OHOUA.mjs +11 -0
  32. package/dist/chunk-75XIEF6H.mjs +126 -0
  33. package/dist/chunk-7X5JST5L.mjs +114 -0
  34. package/dist/chunk-BOFBLJFZ.mjs +209 -0
  35. package/dist/{chunk-USKFU7J5.mjs → chunk-BWJVNZPR.mjs} +8 -1
  36. package/dist/chunk-C7SLA6CM.mjs +19 -0
  37. package/dist/{chunk-POWOTUKJ.mjs → chunk-CQUMNIIG.mjs} +3 -6
  38. package/dist/chunk-CVXLLEQ3.mjs +1011 -0
  39. package/dist/chunk-DJHHF2L5.mjs +17 -0
  40. package/dist/chunk-E74YLAIG.mjs +24 -0
  41. package/dist/{chunk-UXNWQZST.mjs → chunk-EMRMGYRE.mjs} +236 -18
  42. package/dist/chunk-EWZLIYHV.mjs +57 -0
  43. package/dist/chunk-GBEEG5AN.mjs +35 -0
  44. package/dist/chunk-GES7U2PM.mjs +68 -0
  45. package/dist/chunk-HJSN7UYG.mjs +10 -0
  46. package/dist/chunk-HLPRHOSO.mjs +17 -0
  47. package/dist/chunk-HMFNFVP7.mjs +14 -0
  48. package/dist/chunk-J5QP52HA.mjs +1751 -0
  49. package/dist/{chunk-YNVIUGF5.mjs → chunk-JZVARCRZ.mjs} +3 -28
  50. package/dist/chunk-KOKM4PQN.mjs +52 -0
  51. package/dist/{chunk-B2TFROFX.mjs → chunk-KWTW6VTG.mjs} +465 -250
  52. package/dist/chunk-KZ3NX2P7.mjs +21 -0
  53. package/dist/chunk-L5ORED7V.mjs +25 -0
  54. package/dist/{chunk-THYRHHO2.mjs → chunk-LGAMKHA2.mjs} +2009 -2351
  55. package/dist/chunk-LKOB5COA.mjs +117 -0
  56. package/dist/chunk-LPQOEU5M.mjs +96 -0
  57. package/dist/{chunk-3LHGNRYE.mjs → chunk-LXWDQLTW.mjs} +7 -2
  58. package/dist/{chunk-L6TNPLAR.mjs → chunk-OEYI3KRB.mjs} +270 -108
  59. package/dist/chunk-OM362BRO.mjs +948 -0
  60. package/dist/chunk-OZDFA4WC.mjs +67 -0
  61. package/dist/chunk-P36MUFEJ.mjs +10 -0
  62. package/dist/chunk-PC2G76VW.mjs +2540 -0
  63. package/dist/{chunk-GCM3JFVA.mjs → chunk-PNEYIHA4.mjs} +11 -2
  64. package/dist/chunk-PNFKUCG3.mjs +1130 -0
  65. package/dist/chunk-Q6XWVUZX.mjs +44 -0
  66. package/dist/chunk-R27EMH5M.mjs +617 -0
  67. package/dist/{chunk-YMZCMH5M.mjs → chunk-R6A773WK.mjs} +19 -24
  68. package/dist/chunk-RF7PLG43.mjs +106 -0
  69. package/dist/chunk-RFBLQLLZ.mjs +16 -0
  70. package/dist/{chunk-Y3L5MDRV.mjs → chunk-RFVUDM5E.mjs} +17 -2
  71. package/dist/chunk-S7FL6UAH.mjs +521 -0
  72. package/dist/chunk-S7GNDRPS.mjs +651 -0
  73. package/dist/chunk-SP3NM3ZG.mjs +3537 -0
  74. package/dist/chunk-U476IXKQ.mjs +10 -0
  75. package/dist/chunk-UA3AUGNV.mjs +462 -0
  76. package/dist/chunk-UTG5YNXV.mjs +507 -0
  77. package/dist/chunk-VI4R6W25.mjs +8484 -0
  78. package/dist/chunk-WQBCBKY3.mjs +8 -0
  79. package/dist/{chunk-24RXHMEL.mjs → chunk-WVCOCKBU.mjs} +3794 -7570
  80. package/dist/chunk-X5CV2E2B.mjs +1 -0
  81. package/dist/chunk-XADZPPZI.mjs +614 -0
  82. package/dist/chunk-YEEQHT4V.mjs +212 -0
  83. package/dist/chunk-YTJCGKM4.mjs +56 -0
  84. package/dist/chunk-YYQIODMF.mjs +275 -0
  85. package/dist/chunk-ZI34TVOA.mjs +144 -0
  86. package/dist/{chunk-OOSIDHA3.mjs → chunk-ZKWYWW4Y.mjs} +1 -1
  87. package/dist/chunk-ZM7KU4YW.mjs +35 -0
  88. package/dist/{client-BQK6Tf6A.d.ts → client-BOStAkc5.d.ts} +160 -37
  89. package/dist/client.d.ts +11 -10
  90. package/dist/client.mjs +209 -45
  91. package/dist/{config-DmXULPa-.d.ts → config-Cjvzbs_j.d.ts} +51 -3
  92. package/dist/config-DffQlmRq.d.ts +421 -0
  93. package/dist/direct-executor-ZD64PPEZ.mjs +24 -0
  94. package/dist/distribution/coordinator/etcd.d.ts +79 -18
  95. package/dist/distribution/coordinator/etcd.mjs +553 -109
  96. package/dist/distribution/coordinator/in-memory.d.ts +4 -3
  97. package/dist/distribution/coordinator/in-memory.mjs +22 -3
  98. package/dist/distribution/transport/grpc.d.ts +2 -2
  99. package/dist/distribution/transport/grpc.mjs +79 -14
  100. package/dist/distribution/transport/in-memory.d.ts +2 -2
  101. package/dist/distribution/transport/in-memory.mjs +12 -7
  102. package/dist/distribution/transport/tcp.d.ts +2 -2
  103. package/dist/distribution/transport/tcp.mjs +50 -28
  104. package/dist/distribution.d.ts +144 -23
  105. package/dist/distribution.mjs +14479 -8173
  106. package/dist/durability-wiring-FEUBR5BT.mjs +3330 -0
  107. package/dist/embeddings/openai.d.ts +2 -2
  108. package/dist/embeddings/openai.mjs +22 -133
  109. package/dist/{engine-CIOyFozK.d.ts → engine-rC0vjjY-.d.ts} +165 -141
  110. package/dist/{memory-yFaVn2xZ.d.ts → events-DqOIg5xa.d.ts} +645 -136
  111. package/dist/{types-DAK7mnUZ.d.ts → index-Bkdt8nDw.d.ts} +182 -45
  112. package/dist/index.browser.mjs +4 -32180
  113. package/dist/index.d.ts +91 -20
  114. package/dist/index.mjs +33 -16
  115. package/dist/invalidation/broadcast-channel.d.ts +1 -1
  116. package/dist/invalidation/filesystem.d.ts +1 -1
  117. package/dist/invalidation/filesystem.mjs +2 -1
  118. package/dist/invalidation/noop.d.ts +1 -1
  119. package/dist/module-3S65MVCM.mjs +2151 -0
  120. package/dist/partial-words-DTIHO8G4.d.ts +19 -0
  121. package/dist/partial-words.d.ts +22 -0
  122. package/dist/partial-words.mjs +17 -0
  123. package/dist/pattern-Cledccbl.d.ts +18 -0
  124. package/dist/pattern.d.ts +22 -0
  125. package/dist/pattern.mjs +12 -0
  126. package/dist/persistence/durability/checkpoint-worker.d.ts +119 -72
  127. package/dist/persistence/durability/checkpoint-worker.mjs +51 -18
  128. package/dist/react.d.ts +279 -183
  129. package/dist/react.mjs +201 -48
  130. package/dist/release-BH3DKW3T.mjs +33 -0
  131. package/dist/replace-CEO5KJJ3.mjs +57 -0
  132. package/dist/rerank-K7C4WZFG.mjs +60 -0
  133. package/dist/rerankers/http.d.ts +50 -0
  134. package/dist/rerankers/http.mjs +104 -0
  135. package/dist/restore-queue-KQHA7ZV2.mjs +35 -0
  136. package/dist/runtime-6Y4ZYXOG.mjs +177 -0
  137. package/dist/{schema-d5Afu03A.d.ts → schema-4W_7tk1f.d.ts} +81 -11
  138. package/dist/server.d.ts +13 -12
  139. package/dist/server.mjs +611 -80
  140. package/dist/snapshot-QGZPODM5.mjs +799 -0
  141. package/dist/synonyms-CpOAHPa-.d.ts +52 -0
  142. package/dist/synonyms.d.ts +22 -0
  143. package/dist/synonyms.mjs +12 -0
  144. package/dist/{tasks-CH2Dod8w.d.ts → tasks-CwPTz-tP.d.ts} +10 -3
  145. package/dist/thread-N3IUGUS3.mjs +697 -0
  146. package/dist/{types-BQDlIvfS.d.ts → types-2qoitTXg.d.ts} +1 -1
  147. package/dist/{types-D5OwhtsQ.d.ts → types-Cz5okLml.d.ts} +2 -2
  148. package/dist/vector/search-worker.d.ts +46 -4
  149. package/dist/vector/search-worker.mjs +21 -11
  150. package/dist/workers/entry.mjs +9 -6
  151. package/dist/write-U5RI7MAX.mjs +241 -0
  152. package/package.json +55 -9
  153. package/dist/chunk-3L4U6EJQ.mjs +0 -49
  154. package/dist/chunk-IFBKLBGB.mjs +0 -7
  155. package/dist/chunk-ORBLD3EL.mjs +0 -6
  156. package/dist/chunk-U7KYW4BA.mjs +0 -135
  157. package/dist/chunk-ZJ4CSAB2.mjs +0 -13
  158. package/dist/chunk-ZNZBXMP5.mjs +0 -2117
  159. package/dist/chunk-ZZT3EAUC.mjs +0 -11168
  160. package/dist/config-BhldZgaK.d.ts +0 -194
  161. package/dist/direct-executor-ALAVE7FC.mjs +0 -12
  162. package/dist/serialization/crc32-worker.d.ts +0 -20
  163. package/dist/serialization/crc32-worker.mjs +0 -62
  164. package/dist/simd-DW5AtJ6B.d.ts +0 -41
  165. package/dist/thread-DYHEQRVY.mjs +0 -402
@@ -1,37 +1,10 @@
1
- import { MAX_M } from './chunk-YNVIUGF5.mjs';
2
- import { NarsilError, ErrorCodes } from './chunk-USKFU7J5.mjs';
3
-
4
- // src/schema/constants.ts
5
- var MAX_NESTING_DEPTH = 4;
6
- var MAX_DOCUMENT_NESTING_DEPTH = 32;
7
-
8
- // src/schema/validator/shared.ts
9
- var SORTABLE_TEXT_FIELD_TYPE = "string:sortable";
10
- var SCALAR_FIELD_TYPES = /* @__PURE__ */ new Set([
11
- "string",
12
- SORTABLE_TEXT_FIELD_TYPE,
13
- "number",
14
- "boolean",
15
- "enum",
16
- "geopoint",
17
- "string[]",
18
- "number[]",
19
- "boolean[]",
20
- "enum[]"
21
- ]);
22
- function isTextFieldType(fieldType) {
23
- return fieldType === "string" || fieldType === SORTABLE_TEXT_FIELD_TYPE;
24
- }
25
- var VECTOR_PATTERN = /^vector\[(\d+)]$/;
26
- function isGeopointOrVectorType(fieldType) {
27
- return fieldType === "geopoint" || fieldType !== void 0 && VECTOR_PATTERN.test(fieldType);
28
- }
29
- var FIELD_NAME_PATTERN = /^[A-Za-z0-9_]+$/;
30
- var RESERVED_ROOT_FIELDS = /* @__PURE__ */ new Set(["id"]);
31
- var PROTOTYPE_POLLUTION_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
32
- function isPlainObject(value) {
33
- return typeof value === "object" && value !== null && !Array.isArray(value);
34
- }
1
+ import { MAX_DOCUMENT_ENCODING_DEPTH, DOCUMENT_ENCODER_RETAINED_BUFFER_BYTES, DOCUMENT_REPLY_ENVELOPE_DEPTH } from './chunk-U476IXKQ.mjs';
2
+ import { EARLIER_RELEASE_MIN_M, MIN_M, MAX_M } from './chunk-HMFNFVP7.mjs';
3
+ import { isWordIndexedBase, parseFieldType } from './chunk-LKOB5COA.mjs';
4
+ import { compareCodePoints, holdsUnpairedSurrogate, exceedsCodePoints } from './chunk-YTJCGKM4.mjs';
5
+ import { DEFAULT_PATTERN_VALUE_LIMIT, isPlainObject, MAX_TEXT_FIELDS, MAX_VECTOR_DIMENSION, MAX_DOCUMENT_BYTES, MAX_NESTING_DEPTH, PROTOTYPE_POLLUTION_KEYS, FIELD_NAME_PATTERN, RESERVED_ROOT_FIELDS, MAX_DOCUMENT_NESTING_DEPTH, VECTOR_PATTERN } from './chunk-L5ORED7V.mjs';
6
+ import { NarsilError, ErrorCodes } from './chunk-BWJVNZPR.mjs';
7
+ import { Encoder, encode } from '@msgpack/msgpack';
35
8
 
36
9
  // src/schema/validator/schema.ts
37
10
  function validateSchemaFields(schema, depth, prefix) {
@@ -42,6 +15,11 @@ function validateSchemaFields(schema, depth, prefix) {
42
15
  { path: prefix, maxDepth: MAX_NESTING_DEPTH }
43
16
  );
44
17
  }
18
+ let canonical = null;
19
+ const replace = (field, value) => {
20
+ if (canonical === null) canonical = { ...schema };
21
+ canonical[field] = value;
22
+ };
45
23
  for (const [field, type] of Object.entries(schema)) {
46
24
  const path = prefix ? `${prefix}.${field}` : field;
47
25
  if (PROTOTYPE_POLLUTION_KEYS.has(field)) {
@@ -65,7 +43,8 @@ function validateSchemaFields(schema, depth, prefix) {
65
43
  );
66
44
  }
67
45
  if (isPlainObject(type)) {
68
- validateSchemaFields(type, depth + 1, path);
46
+ const nested = validateSchemaFields(type, depth + 1, path);
47
+ if (nested !== type) replace(field, nested);
69
48
  continue;
70
49
  }
71
50
  if (typeof type !== "string") {
@@ -78,24 +57,10 @@ function validateSchemaFields(schema, depth, prefix) {
78
57
  }
79
58
  );
80
59
  }
81
- if (SCALAR_FIELD_TYPES.has(type)) continue;
82
- const vectorMatch = VECTOR_PATTERN.exec(type);
83
- if (vectorMatch) {
84
- const dimension = Number.parseInt(vectorMatch[1], 10);
85
- if (dimension <= 0) {
86
- throw new NarsilError(
87
- ErrorCodes.SCHEMA_INVALID_VECTOR_DIMENSION,
88
- `Vector field "${path}" must have a positive dimension, got ${dimension}`,
89
- { field: path, dimension }
90
- );
91
- }
92
- continue;
93
- }
94
- throw new NarsilError(ErrorCodes.SCHEMA_INVALID_TYPE, `Field "${path}" has an unsupported type: "${type}"`, {
95
- field: path,
96
- type
97
- });
60
+ const parsed = parseFieldType(type, path);
61
+ if (parsed.type !== type) replace(field, parsed.type);
98
62
  }
63
+ return canonical ?? schema;
99
64
  }
100
65
  function validateSchema(schema) {
101
66
  if (!isPlainObject(schema)) {
@@ -106,7 +71,41 @@ function validateSchema(schema) {
106
71
  if (Object.keys(schema).length === 0) {
107
72
  throw new NarsilError(ErrorCodes.SCHEMA_INVALID_TYPE, "Schema must define at least one field");
108
73
  }
109
- validateSchemaFields(schema, 1, "");
74
+ const validated = validateSchemaFields(schema, 1, "");
75
+ requireTextFieldCount(schemaFieldsOf(validated).filter((field) => isWordIndexedBase(field.base)).length);
76
+ return validated;
77
+ }
78
+ function requireTextFieldCount(count) {
79
+ if (count <= MAX_TEXT_FIELDS) return;
80
+ throw new NarsilError(
81
+ ErrorCodes.SCHEMA_INVALID_TYPE,
82
+ `The schema declares ${count} string and string[] fields, but a schema holds at most ${MAX_TEXT_FIELDS}`,
83
+ { textFields: count, maximum: MAX_TEXT_FIELDS }
84
+ );
85
+ }
86
+ function requireCreatableSchema(schema) {
87
+ for (const field of schemaFieldsOf(schema)) {
88
+ if (field.base !== "vector" || field.dimension <= MAX_VECTOR_DIMENSION) continue;
89
+ throw new NarsilError(
90
+ ErrorCodes.SCHEMA_INVALID_VECTOR_DIMENSION,
91
+ `Vector field "${field.path}" has ${field.dimension} dimensions, but a new index takes at most ${MAX_VECTOR_DIMENSION}`,
92
+ { field: field.path, dimension: field.dimension, maximum: MAX_VECTOR_DIMENSION }
93
+ );
94
+ }
95
+ }
96
+ function requireValidFieldTypes(flatSchema) {
97
+ let textFields = 0;
98
+ for (const [path, type] of Object.entries(flatSchema)) {
99
+ if (typeof type !== "string") {
100
+ throw new NarsilError(
101
+ ErrorCodes.SCHEMA_INVALID_TYPE,
102
+ `Field "${path}" has an invalid type definition: ${String(type)}`,
103
+ { field: path, type: String(type) }
104
+ );
105
+ }
106
+ if (isWordIndexedBase(parseFieldType(type, path).base)) textFields += 1;
107
+ }
108
+ requireTextFieldCount(textFields);
110
109
  }
111
110
  function flattenRecursive(schema, prefix, result) {
112
111
  for (const [field, type] of Object.entries(schema)) {
@@ -127,18 +126,61 @@ function flattenSchema(schema) {
127
126
  flattenedSchemas.set(schema, Object.freeze(result));
128
127
  return result;
129
128
  }
129
+ var parsedSchemas = /* @__PURE__ */ new WeakMap();
130
+ function schemaFieldsOf(schema) {
131
+ const cached = parsedSchemas.get(schema);
132
+ if (cached !== void 0) return cached;
133
+ const fields = [];
134
+ for (const [path, type] of Object.entries(flattenSchema(schema))) {
135
+ fields.push({ ...parseFieldType(type, path), path });
136
+ }
137
+ const frozen = Object.freeze(fields);
138
+ parsedSchemas.set(schema, frozen);
139
+ return frozen;
140
+ }
141
+ var partialFieldsBySchema = /* @__PURE__ */ new WeakMap();
142
+ function partialFieldsOf(schema) {
143
+ const cached = partialFieldsBySchema.get(schema);
144
+ if (cached !== void 0) return cached;
145
+ const paths = [];
146
+ for (const field of schemaFieldsOf(schema)) {
147
+ if (field.partial) paths.push(field.path);
148
+ }
149
+ const frozen = Object.freeze(paths.sort(compareCodePoints));
150
+ partialFieldsBySchema.set(schema, frozen);
151
+ return frozen;
152
+ }
130
153
  function extractVectorFieldsFromSchema(schema) {
131
- const flat = flattenSchema(schema);
132
154
  const result = /* @__PURE__ */ new Map();
133
- for (const [fieldPath, fieldType] of Object.entries(flat)) {
134
- const match = VECTOR_PATTERN.exec(fieldType);
135
- if (match) {
136
- result.set(fieldPath, Number.parseInt(match[1], 10));
137
- }
155
+ for (const field of schemaFieldsOf(schema)) {
156
+ if (field.base === "vector") result.set(field.path, field.dimension);
138
157
  }
139
158
  return result;
140
159
  }
141
160
 
161
+ // src/geo/constants.ts
162
+ var DEG_TO_RAD = Math.PI / 180;
163
+ var MAX_LATITUDE_DEGREES = 90;
164
+ var MAX_LONGITUDE_DEGREES = 180;
165
+ var MIN_POLYGON_POINTS = 3;
166
+ var ANTIMERIDIAN_SPAN_DEGREES = 180;
167
+ var FULL_TURN_DEGREES = 360;
168
+ var EARTH_RADIUS_METERS = 63710088e-1;
169
+ var LOCATION_CODE_BITS_PER_AXIS = 26;
170
+ var MAX_COVERING_CELLS = 16;
171
+ var RADIUS_BOX_MARGIN_RATIO = 1.02;
172
+ var RADIUS_BOX_MARGIN_RADIANS = 1e-9;
173
+ var VINCENTY_MAX_ITERATIONS = 200;
174
+ var VINCENTY_CONVERGENCE_THRESHOLD = 1e-12;
175
+
176
+ // src/geo/coordinates.ts
177
+ function isLatitude(value) {
178
+ return value >= -MAX_LATITUDE_DEGREES && value <= MAX_LATITUDE_DEGREES;
179
+ }
180
+ function isLongitude(value) {
181
+ return value >= -MAX_LONGITUDE_DEGREES && value <= MAX_LONGITUDE_DEGREES;
182
+ }
183
+
142
184
  // src/schema/validator/field-values.ts
143
185
  function validateGeopoint(path, value) {
144
186
  if (!isPlainObject(value)) {
@@ -158,7 +200,7 @@ function validateGeopoint(path, value) {
158
200
  lon: Number.isNaN(value.lon) ? "NaN" : typeof value.lon
159
201
  });
160
202
  }
161
- if (value.lat < -90 || value.lat > 90) {
203
+ if (!isLatitude(value.lat)) {
162
204
  throw new NarsilError(
163
205
  ErrorCodes.SCHEMA_INVALID_GEOPOINT,
164
206
  `Field "${path}" geopoint lat must be between -90 and 90`,
@@ -168,7 +210,7 @@ function validateGeopoint(path, value) {
168
210
  }
169
211
  );
170
212
  }
171
- if (value.lon < -180 || value.lon > 180) {
213
+ if (!isLongitude(value.lon)) {
172
214
  throw new NarsilError(
173
215
  ErrorCodes.SCHEMA_INVALID_GEOPOINT,
174
216
  `Field "${path}" geopoint lon must be between -180 and 180`,
@@ -198,7 +240,7 @@ function validateVector(path, value, expectedDimension) {
198
240
  );
199
241
  }
200
242
  for (let i = 0; i < arr.length; i++) {
201
- if (typeof arr[i] !== "number" || Number.isNaN(arr[i])) {
243
+ if (typeof arr[i] !== "number" || !Number.isFinite(arr[i])) {
202
244
  throw new NarsilError(
203
245
  ErrorCodes.DOC_VALIDATION_FAILED,
204
246
  `Field "${path}" vector element at index ${i} is not a valid number`,
@@ -223,18 +265,28 @@ function validateTypedArray(path, value, elementType) {
223
265
  { field: path, index: i, expected: elementType, received: typeof el }
224
266
  );
225
267
  }
226
- if (elementType === "number" && Number.isNaN(el)) {
227
- throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Field "${path}" array element at index ${i} is NaN`, {
228
- field: path,
229
- index: i
230
- });
268
+ if (elementType === "number" && !Number.isFinite(el)) {
269
+ throw new NarsilError(
270
+ ErrorCodes.DOC_VALIDATION_FAILED,
271
+ `Field "${path}" array element at index ${i} is ${String(el)}, while a number field holds finite numbers alone`,
272
+ { field: path, index: i }
273
+ );
231
274
  }
232
275
  }
233
276
  }
234
- function validateFieldValue(path, value, type) {
235
- switch (type) {
277
+ function validatePatternValue(path, value, patternValueLimit, index) {
278
+ if (!exceedsCodePoints(value, patternValueLimit)) return;
279
+ const subject = index === void 0 ? `The value of field "${path}"` : `Element ${index} of field "${path}"`;
280
+ throw new NarsilError(
281
+ ErrorCodes.DOC_VALIDATION_FAILED,
282
+ `${subject} is longer than ${patternValueLimit} code points, which is the pattern value limit of this index`,
283
+ { field: path, limit: patternValueLimit, ...index === void 0 ? {} : { index } }
284
+ );
285
+ }
286
+ function validateFieldValue(path, value, type, patternValueLimit) {
287
+ switch (type.base) {
236
288
  case "string":
237
- case SORTABLE_TEXT_FIELD_TYPE:
289
+ case "verbatim":
238
290
  if (typeof value !== "string") {
239
291
  throw new NarsilError(
240
292
  ErrorCodes.DOC_VALIDATION_FAILED,
@@ -246,13 +298,14 @@ function validateFieldValue(path, value, type) {
246
298
  }
247
299
  );
248
300
  }
301
+ if (type.pattern) validatePatternValue(path, value, patternValueLimit);
249
302
  return;
250
303
  case "number":
251
- if (typeof value !== "number" || Number.isNaN(value)) {
252
- throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Field "${path}" expected a valid number`, {
304
+ if (typeof value !== "number" || !Number.isFinite(value)) {
305
+ throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Field "${path}" expected a finite number`, {
253
306
  field: path,
254
307
  expected: "number",
255
- received: Number.isNaN(value) ? "NaN" : typeof value
308
+ received: typeof value === "number" ? String(value) : typeof value
256
309
  });
257
310
  }
258
311
  return;
@@ -286,7 +339,12 @@ function validateFieldValue(path, value, type) {
286
339
  validateGeopoint(path, value);
287
340
  return;
288
341
  case "string[]":
342
+ case "verbatim[]":
289
343
  validateTypedArray(path, value, "string");
344
+ if (type.pattern) {
345
+ const elements = value;
346
+ for (let i = 0; i < elements.length; i++) validatePatternValue(path, elements[i], patternValueLimit, i);
347
+ }
290
348
  return;
291
349
  case "number[]":
292
350
  validateTypedArray(path, value, "number");
@@ -297,17 +355,15 @@ function validateFieldValue(path, value, type) {
297
355
  case "enum[]":
298
356
  validateTypedArray(path, value, "string");
299
357
  return;
300
- default: {
301
- const vectorMatch = VECTOR_PATTERN.exec(type);
302
- if (vectorMatch) {
303
- validateVector(path, value, Number.parseInt(vectorMatch[1], 10));
304
- }
305
- }
358
+ case "vector":
359
+ validateVector(path, value, type.dimension);
360
+ return;
306
361
  }
307
362
  }
308
363
 
309
364
  // src/schema/validator/storable.ts
310
365
  var RESERVED_OBJECT_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
366
+ var UNPAIRED_SURROGATE_REASON = "holds an unpaired surrogate, the half of a character such as an emoji whose other half is missing, so the engine cannot store it as UTF-8";
311
367
  function reject(path, reason) {
312
368
  throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Field "${path === "" ? "(document)" : path}" ${reason}`, {
313
369
  field: path
@@ -320,13 +376,20 @@ function isPlainObject2(value) {
320
376
  function walkStorable(value, path, depth, ancestors, skip) {
321
377
  if (value === null || value === void 0) return;
322
378
  const kind = typeof value;
323
- if (kind === "string" || kind === "boolean") return;
324
- if (kind === "number") return;
379
+ if (kind === "string") {
380
+ if (holdsUnpairedSurrogate(value)) reject(path, UNPAIRED_SURROGATE_REASON);
381
+ return;
382
+ }
383
+ if (kind === "boolean" || kind === "number") return;
325
384
  if (kind === "bigint" || kind === "symbol" || kind === "function") {
326
385
  reject(path, `holds a ${kind}, which cannot be stored or recovered`);
327
386
  }
328
387
  const container = value;
329
- if (container instanceof Date || container instanceof Uint8Array) return;
388
+ if (container instanceof Date) {
389
+ if (Number.isNaN(container.getTime())) reject(path, "holds an invalid date, which the engine cannot store");
390
+ return;
391
+ }
392
+ if (container instanceof Uint8Array) return;
330
393
  const isArray = Array.isArray(container);
331
394
  if (!isArray && !isPlainObject2(container)) {
332
395
  reject(
@@ -353,6 +416,7 @@ function walkStorable(value, path, depth, ancestors, skip) {
353
416
  if (RESERVED_OBJECT_KEYS.has(key)) {
354
417
  reject(keyPath, "uses a reserved object key");
355
418
  }
419
+ if (holdsUnpairedSurrogate(key)) reject(keyPath, `has a name that ${UNPAIRED_SURROGATE_REASON}`);
356
420
  if (skip.has(keyPath)) continue;
357
421
  walkStorable(record[key], keyPath, depth + 1, ancestors, skip);
358
422
  }
@@ -383,32 +447,55 @@ function vectorFieldPaths(schema) {
383
447
  vectorPathCache.set(schema, paths);
384
448
  return paths;
385
449
  }
386
- function validateDocumentFields(doc, schema, prefix) {
387
- for (const [field, type] of Object.entries(schema)) {
450
+ var parsedLevelCache = /* @__PURE__ */ new WeakMap();
451
+ function parsedLevel(schema, prefix) {
452
+ let entries = parsedLevelCache.get(schema);
453
+ if (entries !== void 0) return entries;
454
+ entries = Object.entries(schema).map(([field, type]) => {
455
+ if (isPlainObject(type)) return { field, nested: type, type: null };
388
456
  const path = prefix ? `${prefix}.${field}` : field;
457
+ return { field, nested: null, type: parseFieldType(type, path) };
458
+ });
459
+ parsedLevelCache.set(schema, entries);
460
+ return entries;
461
+ }
462
+ function validateDocumentFields(doc, schema, prefix, patternValueLimit) {
463
+ for (const { field, nested, type } of parsedLevel(schema, prefix)) {
389
464
  const value = doc[field];
390
465
  if (value === void 0 || value === null) continue;
391
- if (isPlainObject(type)) {
466
+ const path = prefix ? `${prefix}.${field}` : field;
467
+ if (nested !== null) {
392
468
  if (!isPlainObject(value)) {
393
469
  throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Field "${path}" expected an object`, {
394
470
  field: path,
395
471
  received: Array.isArray(value) ? "array" : typeof value
396
472
  });
397
473
  }
398
- validateDocumentFields(value, type, path);
474
+ validateDocumentFields(value, nested, path, patternValueLimit);
399
475
  continue;
400
476
  }
401
- validateFieldValue(path, value, type);
477
+ if (type !== null) validateFieldValue(path, value, type, patternValueLimit);
402
478
  }
403
479
  }
404
- function validateDocument(document, schema) {
480
+ function validateDocument(document, schema, patternValueLimit = DEFAULT_PATTERN_VALUE_LIMIT) {
405
481
  if (!isPlainObject(document)) {
406
482
  throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, "Document must be a plain object", {
407
483
  received: typeof document
408
484
  });
409
485
  }
486
+ requireIdFieldKey(document);
410
487
  assertStorableDocument(document, vectorFieldPaths(schema));
411
- validateDocumentFields(document, schema, "");
488
+ validateDocumentFields(document, schema, "", patternValueLimit);
489
+ }
490
+ function requireIdFieldKey(document) {
491
+ if (!Object.hasOwn(document, "id")) return;
492
+ const id = document.id;
493
+ if (typeof id === "string" && id.length > 0) return;
494
+ throw new NarsilError(
495
+ ErrorCodes.DOC_VALIDATION_FAILED,
496
+ `Field "id" holds ${typeof id === "string" ? "an empty string" : id === null ? "null" : `a ${typeof id}`}, while the engine stores a document under its id field, so that field must hold a non-empty string`,
497
+ { field: "id", received: id === null ? "null" : typeof id }
498
+ );
412
499
  }
413
500
  function collectExtraFields(doc, schema, prefix, extras) {
414
501
  for (const key of Object.keys(doc)) {
@@ -456,6 +543,90 @@ function validateRequiredFields(document, required) {
456
543
  }
457
544
  }
458
545
  }
546
+ var INTEGER_ENCODING = { maxDepth: MAX_DOCUMENT_ENCODING_DEPTH };
547
+ var FLOAT_ENCODING = { maxDepth: MAX_DOCUMENT_ENCODING_DEPTH, forceIntegerToFloat: true };
548
+ var integerEncoder = new Encoder(INTEGER_ENCODING);
549
+ var floatEncoder = new Encoder(FLOAT_ENCODING);
550
+ function encodeExactly(document, forceIntegerToFloat) {
551
+ const bytes = (forceIntegerToFloat ? floatEncoder : integerEncoder).encode(document);
552
+ if (bytes.byteLength > DOCUMENT_ENCODER_RETAINED_BUFFER_BYTES) {
553
+ if (forceIntegerToFloat) floatEncoder = new Encoder(FLOAT_ENCODING);
554
+ else integerEncoder = new Encoder(INTEGER_ENCODING);
555
+ }
556
+ return bytes;
557
+ }
558
+ function holdsNegativeZero(value, depth) {
559
+ if (typeof value === "number") return Object.is(value, -0);
560
+ if (value === null || typeof value !== "object" || ArrayBuffer.isView(value) || depth > MAX_DOCUMENT_ENCODING_DEPTH) {
561
+ return false;
562
+ }
563
+ for (const child of Array.isArray(value) ? value : Object.values(value)) {
564
+ if (holdsNegativeZero(child, depth + 1)) return true;
565
+ }
566
+ return false;
567
+ }
568
+ function encodeDocumentReply(payload) {
569
+ return encode(payload, { forceIntegerToFloat: holdsNegativeZero(payload, 1 - DOCUMENT_REPLY_ENVELOPE_DEPTH) });
570
+ }
571
+ function encodeDocument(document) {
572
+ try {
573
+ return encodeExactly(document, holdsNegativeZero(document, 1));
574
+ } catch (error) {
575
+ throw new NarsilError(
576
+ ErrorCodes.DOC_VALIDATION_FAILED,
577
+ `The document cannot be encoded as MessagePack: ${error instanceof Error ? error.message : String(error)}`
578
+ );
579
+ }
580
+ }
581
+
582
+ // src/schema/validator/document-size.ts
583
+ var LARGEST_HEADER_BYTES = 5;
584
+ var LARGEST_NUMBER_BYTES = 9;
585
+ var LARGEST_TIMESTAMP_BYTES = 15;
586
+ var LARGEST_UTF8_BYTES_PER_UNIT = 3;
587
+ function upperBoundBytes(value, budget, depth) {
588
+ if (value === null || value === void 0 || typeof value === "boolean") return 1;
589
+ if (typeof value === "number") return LARGEST_NUMBER_BYTES;
590
+ if (typeof value === "string") return LARGEST_HEADER_BYTES + value.length * LARGEST_UTF8_BYTES_PER_UNIT;
591
+ if (value instanceof Date) return LARGEST_TIMESTAMP_BYTES;
592
+ if (ArrayBuffer.isView(value)) return LARGEST_HEADER_BYTES + value.byteLength;
593
+ if (typeof value !== "object" || depth > MAX_DOCUMENT_NESTING_DEPTH) return LARGEST_NUMBER_BYTES;
594
+ let total = LARGEST_HEADER_BYTES;
595
+ if (Array.isArray(value)) {
596
+ for (const item of value) {
597
+ total += upperBoundBytes(item, budget - total, depth + 1);
598
+ if (total > budget) return total;
599
+ }
600
+ return total;
601
+ }
602
+ const record = value;
603
+ for (const key of Object.keys(record)) {
604
+ total += LARGEST_HEADER_BYTES + key.length * LARGEST_UTF8_BYTES_PER_UNIT;
605
+ total += upperBoundBytes(record[key], budget - total, depth + 1);
606
+ if (total > budget) return total;
607
+ }
608
+ return total;
609
+ }
610
+ function messagePackUpperBoundBytes(value) {
611
+ return upperBoundBytes(value, Number.POSITIVE_INFINITY, 0);
612
+ }
613
+ function encodedBytesOf(document) {
614
+ try {
615
+ return encodeDocument(document).byteLength;
616
+ } catch {
617
+ return null;
618
+ }
619
+ }
620
+ function requireDocumentWithinSizeLimit(document) {
621
+ if (upperBoundBytes(document, MAX_DOCUMENT_BYTES, 0) <= MAX_DOCUMENT_BYTES) return;
622
+ const encodedBytes = encodedBytesOf(document);
623
+ if (encodedBytes === null || encodedBytes <= MAX_DOCUMENT_BYTES) return;
624
+ throw new NarsilError(
625
+ ErrorCodes.DOC_VALIDATION_FAILED,
626
+ `The document takes ${encodedBytes} bytes as MessagePack, vector values included, but a document may take at most ${MAX_DOCUMENT_BYTES}`,
627
+ { bytes: encodedBytes, maximum: MAX_DOCUMENT_BYTES }
628
+ );
629
+ }
459
630
 
460
631
  // src/schema/validator/vector-promotion.ts
461
632
  var QUANTIZATION_MODES = /* @__PURE__ */ new Set(["osq8", "osq4", "osq2", "osq1", "none"]);
@@ -479,6 +650,13 @@ function validateVectorStorage(config, filesystemDurability) {
479
650
  });
480
651
  }
481
652
  }
653
+ function raiseEarlierReleaseGraphSetting(config) {
654
+ if (typeof config !== "object" || config === null) return config;
655
+ const hnsw = config.hnswConfig;
656
+ if (typeof hnsw !== "object" || hnsw === null) return config;
657
+ if (hnsw.m !== EARLIER_RELEASE_MIN_M) return config;
658
+ return { ...config, hnswConfig: { ...hnsw, m: MIN_M } };
659
+ }
482
660
  function validateVectorPromotion(config) {
483
661
  if (config === void 0) return;
484
662
  if (config.threshold !== void 0 && !isPositiveInteger(config.threshold)) {
@@ -494,8 +672,8 @@ function validateVectorPromotion(config) {
494
672
  }
495
673
  const hnsw = config.hnswConfig;
496
674
  if (hnsw === void 0) return;
497
- if (hnsw.m !== void 0 && (!isPositiveInteger(hnsw.m) || hnsw.m > MAX_M)) {
498
- fail(`vectorPromotion.hnswConfig.m must be a positive integer no greater than ${MAX_M}`, { m: hnsw.m });
675
+ if (hnsw.m !== void 0 && (!Number.isInteger(hnsw.m) || hnsw.m < MIN_M || hnsw.m > MAX_M)) {
676
+ fail(`vectorPromotion.hnswConfig.m must be a whole number from ${MIN_M} to ${MAX_M}`, { m: hnsw.m });
499
677
  }
500
678
  if (hnsw.efConstruction !== void 0 && !isPositiveInteger(hnsw.efConstruction)) {
501
679
  fail("vectorPromotion.hnswConfig.efConstruction must be a positive integer", {
@@ -508,20 +686,4 @@ function validateVectorPromotion(config) {
508
686
  }
509
687
  }
510
688
 
511
- // src/search/constants.ts
512
- var RESULT_WINDOW = 1e4;
513
- var DEFAULT_PAGE_SIZE = 10;
514
- var MAX_CURSOR_LENGTH = 40960;
515
- var MAX_SORT_FIELDS = 8;
516
- var MAX_SORT_FIELD_NAME_LENGTH = 255;
517
- var MAX_CURSOR_ANCHOR_CODE_POINTS = 512;
518
- var PREFIX_MAX_EXPANSIONS = 50;
519
- var DEFAULT_FUZZY_PREFIX_LENGTH = 2;
520
- var DEFAULT_MAX_PER_GROUP = 1;
521
- var DEFAULT_RANK_CONSTANT = 60;
522
- var MIN_RANK_CONSTANT = 1;
523
- var DEFAULT_LINEAR_ALPHA = 0.5;
524
- var MIN_LINEAR_ALPHA = 0;
525
- var MAX_LINEAR_ALPHA = 1;
526
-
527
- export { DEFAULT_FUZZY_PREFIX_LENGTH, DEFAULT_LINEAR_ALPHA, DEFAULT_MAX_PER_GROUP, DEFAULT_PAGE_SIZE, DEFAULT_RANK_CONSTANT, MAX_CURSOR_ANCHOR_CODE_POINTS, MAX_CURSOR_LENGTH, MAX_LINEAR_ALPHA, MAX_NESTING_DEPTH, MAX_SORT_FIELDS, MAX_SORT_FIELD_NAME_LENGTH, MIN_LINEAR_ALPHA, MIN_RANK_CONSTANT, PREFIX_MAX_EXPANSIONS, PROTOTYPE_POLLUTION_KEYS, RESULT_WINDOW, SORTABLE_TEXT_FIELD_TYPE, VECTOR_PATTERN, extractVectorFieldsFromSchema, flattenSchema, isGeopointOrVectorType, isQuantizationMode, isStorageMode, isTextFieldType, validateDocument, validateDocumentStrict, validateRequiredFields, validateSchema, validateVectorPromotion, validateVectorStorage };
689
+ export { ANTIMERIDIAN_SPAN_DEGREES, DEG_TO_RAD, EARTH_RADIUS_METERS, FULL_TURN_DEGREES, LOCATION_CODE_BITS_PER_AXIS, MAX_COVERING_CELLS, MAX_LATITUDE_DEGREES, MAX_LONGITUDE_DEGREES, MIN_POLYGON_POINTS, RADIUS_BOX_MARGIN_RADIANS, RADIUS_BOX_MARGIN_RATIO, VINCENTY_CONVERGENCE_THRESHOLD, VINCENTY_MAX_ITERATIONS, encodeDocument, encodeDocumentReply, extractVectorFieldsFromSchema, flattenSchema, isLatitude, isLongitude, isQuantizationMode, isStorageMode, messagePackUpperBoundBytes, partialFieldsOf, raiseEarlierReleaseGraphSetting, requireCreatableSchema, requireDocumentWithinSizeLimit, requireValidFieldTypes, schemaFieldsOf, validateDocument, validateDocumentStrict, validateRequiredFields, validateSchema, validateVectorPromotion, validateVectorStorage };