@intutic/shared-types 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. package/dist/anomaly.d.ts +6 -10
  2. package/dist/anomaly.d.ts.map +1 -1
  3. package/dist/anomaly.js +0 -2
  4. package/dist/anomaly.js.map +1 -1
  5. package/dist/api-contracts.d.ts +1 -1
  6. package/dist/api-contracts.js +1 -1
  7. package/dist/attenuation.d.ts +1 -1
  8. package/dist/attenuation.js +1 -1
  9. package/dist/auth.d.ts +116 -9
  10. package/dist/auth.d.ts.map +1 -1
  11. package/dist/auth.js +20 -5
  12. package/dist/auth.js.map +1 -1
  13. package/dist/billing.d.ts +1 -1
  14. package/dist/billing.js +1 -1
  15. package/dist/cloudProbe.d.ts +54 -0
  16. package/dist/cloudProbe.d.ts.map +1 -0
  17. package/dist/cloudProbe.js +254 -0
  18. package/dist/cloudProbe.js.map +1 -0
  19. package/dist/destructiveSql.d.ts +48 -0
  20. package/dist/destructiveSql.d.ts.map +1 -0
  21. package/dist/destructiveSql.js +43 -0
  22. package/dist/destructiveSql.js.map +1 -0
  23. package/dist/devInventory.d.ts +120 -0
  24. package/dist/devInventory.d.ts.map +1 -0
  25. package/dist/devInventory.js +80 -0
  26. package/dist/devInventory.js.map +1 -0
  27. package/dist/enums.d.ts +66 -21
  28. package/dist/enums.d.ts.map +1 -1
  29. package/dist/enums.js +55 -31
  30. package/dist/enums.js.map +1 -1
  31. package/dist/errors.js +3 -3
  32. package/dist/errors.js.map +1 -1
  33. package/dist/finops.d.ts +52 -2
  34. package/dist/finops.d.ts.map +1 -1
  35. package/dist/finops.js +15 -1
  36. package/dist/finops.js.map +1 -1
  37. package/dist/gateDecisions.d.ts +18 -0
  38. package/dist/gateDecisions.d.ts.map +1 -0
  39. package/dist/gateDecisions.js +20 -0
  40. package/dist/gateDecisions.js.map +1 -0
  41. package/dist/gateIdentity.d.ts +23 -0
  42. package/dist/gateIdentity.d.ts.map +1 -0
  43. package/dist/gateIdentity.js +32 -0
  44. package/dist/gateIdentity.js.map +1 -0
  45. package/dist/gateKind.d.ts +172 -0
  46. package/dist/gateKind.d.ts.map +1 -0
  47. package/dist/gateKind.js +160 -0
  48. package/dist/gateKind.js.map +1 -0
  49. package/dist/gateLimits.d.ts +170 -0
  50. package/dist/gateLimits.d.ts.map +1 -0
  51. package/dist/gateLimits.js +96 -0
  52. package/dist/gateLimits.js.map +1 -0
  53. package/dist/gitRemote.d.ts +17 -0
  54. package/dist/gitRemote.d.ts.map +1 -0
  55. package/dist/gitRemote.js +71 -0
  56. package/dist/gitRemote.js.map +1 -0
  57. package/dist/governanceCoverage.d.ts +1 -1
  58. package/dist/governanceCoverage.js +1 -1
  59. package/dist/guardrailIr.d.ts +9 -9
  60. package/dist/guardrailIr.d.ts.map +1 -1
  61. package/dist/guardrailIr.js +3 -3
  62. package/dist/guardrailIr.js.map +1 -1
  63. package/dist/guardrailRender.d.ts +1 -1
  64. package/dist/guardrailRender.js +1 -1
  65. package/dist/harnessRules.d.ts +74 -0
  66. package/dist/harnessRules.d.ts.map +1 -0
  67. package/dist/harnessRules.js +142 -0
  68. package/dist/harnessRules.js.map +1 -0
  69. package/dist/holdMessages.d.ts +25 -0
  70. package/dist/holdMessages.d.ts.map +1 -0
  71. package/dist/holdMessages.js +34 -0
  72. package/dist/holdMessages.js.map +1 -0
  73. package/dist/identity.d.ts +1 -1
  74. package/dist/identity.js +2 -2
  75. package/dist/identity.js.map +1 -1
  76. package/dist/index.d.ts +42 -9
  77. package/dist/index.d.ts.map +1 -1
  78. package/dist/index.js +161 -10
  79. package/dist/index.js.map +1 -1
  80. package/dist/intelligence.d.ts +1 -1
  81. package/dist/mcpBudgets.d.ts +191 -0
  82. package/dist/mcpBudgets.d.ts.map +1 -0
  83. package/dist/mcpBudgets.js +144 -0
  84. package/dist/mcpBudgets.js.map +1 -0
  85. package/dist/mcpRegistryRecord.d.ts +143 -0
  86. package/dist/mcpRegistryRecord.d.ts.map +1 -0
  87. package/dist/mcpRegistryRecord.js +229 -0
  88. package/dist/mcpRegistryRecord.js.map +1 -0
  89. package/dist/mcpToolRisk.d.ts +82 -0
  90. package/dist/mcpToolRisk.d.ts.map +1 -0
  91. package/dist/mcpToolRisk.js +334 -0
  92. package/dist/mcpToolRisk.js.map +1 -0
  93. package/dist/modelCatalog.d.ts +2 -2
  94. package/dist/modelCatalog.generated.js +1 -1
  95. package/dist/modelCatalog.generated.js.map +1 -1
  96. package/dist/modelCatalog.js +2 -2
  97. package/dist/notifications.d.ts +24 -151
  98. package/dist/notifications.d.ts.map +1 -1
  99. package/dist/notifications.js +266 -1
  100. package/dist/notifications.js.map +1 -1
  101. package/dist/phrases.d.ts +87 -0
  102. package/dist/phrases.d.ts.map +1 -0
  103. package/dist/phrases.js +241 -0
  104. package/dist/phrases.js.map +1 -0
  105. package/dist/piiDetectors.d.ts +97 -0
  106. package/dist/piiDetectors.d.ts.map +1 -0
  107. package/dist/piiDetectors.js +96 -0
  108. package/dist/piiDetectors.js.map +1 -0
  109. package/dist/piiDetectors.json +71 -0
  110. package/dist/policy.d.ts +1 -1
  111. package/dist/policy.js +1 -1
  112. package/dist/policyGuardrails.d.ts +127 -10
  113. package/dist/policyGuardrails.d.ts.map +1 -1
  114. package/dist/policyGuardrails.js +59 -6
  115. package/dist/policyGuardrails.js.map +1 -1
  116. package/dist/posturePresets.js +1 -1
  117. package/dist/posturePresets.js.map +1 -1
  118. package/dist/providerVerification.d.ts +36 -10
  119. package/dist/providerVerification.d.ts.map +1 -1
  120. package/dist/providerVerification.js +80 -20
  121. package/dist/providerVerification.js.map +1 -1
  122. package/dist/providers.d.ts +54 -17
  123. package/dist/providers.d.ts.map +1 -1
  124. package/dist/providers.js +135 -26
  125. package/dist/providers.js.map +1 -1
  126. package/dist/regoBuiltins.d.ts +42 -0
  127. package/dist/regoBuiltins.d.ts.map +1 -0
  128. package/dist/regoBuiltins.js +732 -0
  129. package/dist/regoBuiltins.js.map +1 -0
  130. package/dist/regoRules.d.ts +105 -0
  131. package/dist/regoRules.d.ts.map +1 -0
  132. package/dist/regoRules.js +407 -0
  133. package/dist/regoRules.js.map +1 -0
  134. package/dist/ruleLoadFailures.d.ts +15 -0
  135. package/dist/ruleLoadFailures.d.ts.map +1 -0
  136. package/dist/ruleLoadFailures.js +26 -0
  137. package/dist/ruleLoadFailures.js.map +1 -0
  138. package/dist/scriptScan.d.ts +5 -5
  139. package/dist/scriptScan.js +3 -3
  140. package/dist/sequence.d.ts +52 -0
  141. package/dist/sequence.d.ts.map +1 -0
  142. package/dist/sequence.js +127 -0
  143. package/dist/sequence.js.map +1 -0
  144. package/dist/session.d.ts +1 -1
  145. package/dist/session.js +1 -1
  146. package/dist/skillScan.d.ts +3 -3
  147. package/dist/skillScan.d.ts.map +1 -1
  148. package/dist/skillScan.js +26 -4
  149. package/dist/skillScan.js.map +1 -1
  150. package/dist/sop.d.ts +5 -5
  151. package/dist/sop.js +2 -2
  152. package/dist/sourceProviders.d.ts +1 -8
  153. package/dist/sourceProviders.d.ts.map +1 -1
  154. package/dist/sourceProviders.js +2 -9
  155. package/dist/sourceProviders.js.map +1 -1
  156. package/dist/spendBudgets.d.ts +122 -0
  157. package/dist/spendBudgets.d.ts.map +1 -0
  158. package/dist/spendBudgets.js +92 -0
  159. package/dist/spendBudgets.js.map +1 -0
  160. package/dist/ssoGroupClearance.d.ts +18 -2
  161. package/dist/ssoGroupClearance.d.ts.map +1 -1
  162. package/dist/ssoGroupClearance.js +43 -4
  163. package/dist/ssoGroupClearance.js.map +1 -1
  164. package/dist/sync.d.ts +0 -1
  165. package/dist/sync.d.ts.map +1 -1
  166. package/dist/sync.js +0 -1
  167. package/dist/sync.js.map +1 -1
  168. package/dist/taskManagement.d.ts +0 -2
  169. package/dist/taskManagement.d.ts.map +1 -1
  170. package/dist/taskManagement.js +0 -2
  171. package/dist/taskManagement.js.map +1 -1
  172. package/dist/toolPoison.d.ts +33 -0
  173. package/dist/toolPoison.d.ts.map +1 -0
  174. package/dist/toolPoison.js +85 -0
  175. package/dist/toolPoison.js.map +1 -0
  176. package/dist/upstreamRetry.d.ts +121 -0
  177. package/dist/upstreamRetry.d.ts.map +1 -0
  178. package/dist/upstreamRetry.js +90 -0
  179. package/dist/upstreamRetry.js.map +1 -0
  180. package/dist/usage.d.ts +186 -0
  181. package/dist/usage.d.ts.map +1 -1
  182. package/dist/wasmBinary.d.ts +25 -0
  183. package/dist/wasmBinary.d.ts.map +1 -0
  184. package/dist/wasmBinary.js +111 -0
  185. package/dist/wasmBinary.js.map +1 -0
  186. package/dist/wasmFuel.d.ts +52 -0
  187. package/dist/wasmFuel.d.ts.map +1 -0
  188. package/dist/wasmFuel.js +320 -0
  189. package/dist/wasmFuel.js.map +1 -0
  190. package/dist/wasmMemoryCap.d.ts +21 -0
  191. package/dist/wasmMemoryCap.d.ts.map +1 -0
  192. package/dist/wasmMemoryCap.js +58 -0
  193. package/dist/wasmMemoryCap.js.map +1 -0
  194. package/dist/wasmRuleDescriptor.d.ts +43 -0
  195. package/dist/wasmRuleDescriptor.d.ts.map +1 -0
  196. package/dist/wasmRuleDescriptor.js +65 -0
  197. package/dist/wasmRuleDescriptor.js.map +1 -0
  198. package/dist/workspaceSettings.d.ts +95 -17
  199. package/dist/workspaceSettings.d.ts.map +1 -1
  200. package/dist/workspaceSettings.js +2 -1
  201. package/dist/workspaceSettings.js.map +1 -1
  202. package/package.json +2 -2
@@ -0,0 +1,732 @@
1
+ "use strict";
2
+ /**
3
+ * OPA builtins a compiled Rego policy asks the host to run — the TypeScript
4
+ * twin of `packages/proxy/src/wasm/opa_builtins.rs`.
5
+ *
6
+ * `opa build -t wasm` compiles most builtins into the module; the ones it
7
+ * imports through `opa_builtinN` are listed by name in the module's `builtins`
8
+ * export. These follow OPA's documented semantics (Go's `fmt`, `regexp`,
9
+ * `strings` and RFC 6902) and are checked against `opa eval` on the same
10
+ * inputs as the Rust host (`packages/proxy/tests/fixtures/rego/conformance.*`),
11
+ * so a policy decides the same way in the proxy, the MCP proxy and
12
+ * `intutic rules test`.
13
+ *
14
+ * Arguments arrive and results leave as JSON, so a Rego set reaches a builtin
15
+ * as an array. A thrown error makes the result undefined, as `opa eval` does
16
+ * outside strict mode.
17
+ *
18
+ * Differences from OPA, all also true of the Rust host unless noted:
19
+ * - a number written in exponent notation is formatted by `sprintf` as a float
20
+ * (OPA passes its source text);
21
+ * - `sprintf` refuses a field width or precision over 4096;
22
+ * - regular expressions run on JavaScript's engine here, with Go's `(?P<name>)`
23
+ * groups and leading `(?i)`/`(?s)`/`(?m)` flags translated; other RE2-only
24
+ * syntax is refused, and the MCP proxy's per-rule deadline bounds a pattern
25
+ * that backtracks;
26
+ * - integers beyond 2^53 lose precision here, where JSON numbers are doubles.
27
+ *
28
+ * @module
29
+ */
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.REGO_HOST_BUILTIN_NAMES = exports.REGO_HOST_BUILTINS = void 0;
32
+ const fail = (message) => {
33
+ throw new Error(message);
34
+ };
35
+ function str(args, i) {
36
+ const v = args[i];
37
+ return typeof v === 'string' ? v : fail(`operand ${i + 1} must be a string`);
38
+ }
39
+ function int(args, i) {
40
+ const v = args[i];
41
+ return typeof v === 'number' && Number.isInteger(v) ? v : fail(`operand ${i + 1} must be an integer`);
42
+ }
43
+ /** A string, or an array of strings (how a set of strings arrives). */
44
+ function strs(args, i) {
45
+ const v = args[i];
46
+ if (typeof v === 'string')
47
+ return [v];
48
+ if (Array.isArray(v) && v.every((s) => typeof s === 'string'))
49
+ return v;
50
+ return fail(`operand ${i + 1} must be a string, array or set of strings`);
51
+ }
52
+ /** `v` with every object's keys sorted, as Go writes a map. */
53
+ function sorted(v) {
54
+ if (Array.isArray(v))
55
+ return v.map(sorted);
56
+ if (v !== null && typeof v === 'object') {
57
+ return Object.fromEntries(Object.keys(v)
58
+ .sort()
59
+ .map((k) => [k, sorted(v[k])]));
60
+ }
61
+ return v;
62
+ }
63
+ // ── regex ──────────────────────────────────────────────────────────────
64
+ /** Translate RE2 syntax JavaScript spells differently, refusing what it lacks. */
65
+ function compile(pattern) {
66
+ let source = pattern;
67
+ let flags = 'gu';
68
+ const leading = /^\(\?([ims]+)\)/.exec(source);
69
+ if (leading) {
70
+ flags += leading[1];
71
+ source = source.slice(leading[0].length);
72
+ }
73
+ if (/\(\?[a-zA-Z-]+[:)]/.test(source) || /\[\[:/.test(source) || /\\[zAQE]/.test(source)) {
74
+ fail(`regular expression uses RE2 syntax this host does not support: ${pattern}`);
75
+ }
76
+ source = source.replace(/\(\?P</g, '(?<');
77
+ try {
78
+ return new RegExp(source, flags);
79
+ }
80
+ catch (err) {
81
+ return fail(`invalid regular expression: ${err.message}`);
82
+ }
83
+ }
84
+ /** Go's `FindAllString(s, n)`: an empty match abutting the previous match is skipped. */
85
+ function findAll(re, s, n) {
86
+ const out = [];
87
+ re.lastIndex = 0;
88
+ let prevEnd = -1;
89
+ for (;;) {
90
+ if (n >= 0 && out.length >= n)
91
+ break;
92
+ const m = re.exec(s);
93
+ if (!m)
94
+ break;
95
+ const end = m.index + m[0].length;
96
+ if (m[0].length === 0) {
97
+ // Step past the empty match by one code point, as `u` mode requires.
98
+ const cp = s.codePointAt(m.index);
99
+ re.lastIndex = m.index + (cp !== undefined && cp > 0xffff ? 2 : 1);
100
+ if (m.index === prevEnd)
101
+ continue;
102
+ if (m.index > s.length)
103
+ break;
104
+ }
105
+ out.push(m);
106
+ prevEnd = end;
107
+ }
108
+ return out;
109
+ }
110
+ /** Go's `Regexp.Expand` template: `$1`, `${1}`, `$name`, `${name}`, `$$`. */
111
+ function expand(template, m) {
112
+ let out = '';
113
+ for (let i = 0; i < template.length; i++) {
114
+ const c = template[i];
115
+ if (c !== '$' || i + 1 >= template.length) {
116
+ out += c;
117
+ continue;
118
+ }
119
+ if (template[i + 1] === '$') {
120
+ out += '$';
121
+ i += 1;
122
+ continue;
123
+ }
124
+ let name;
125
+ if (template[i + 1] === '{') {
126
+ const close = template.indexOf('}', i + 2);
127
+ if (close < 0) {
128
+ out += c;
129
+ continue;
130
+ }
131
+ name = template.slice(i + 2, close);
132
+ i = close;
133
+ }
134
+ else {
135
+ const word = /^[A-Za-z0-9_]+/.exec(template.slice(i + 1));
136
+ if (!word) {
137
+ out += c;
138
+ continue;
139
+ }
140
+ name = word[0];
141
+ i += word[0].length;
142
+ }
143
+ const group = /^\d+$/.test(name) ? m[Number(name)] : m.groups?.[name];
144
+ out += group ?? '';
145
+ }
146
+ return out;
147
+ }
148
+ const MAX_REPLACED_BYTES = 1024 * 1024;
149
+ function pointer(op, field) {
150
+ const p = op[field];
151
+ if (p === '')
152
+ return [];
153
+ if (typeof p === 'string') {
154
+ if (!p.startsWith('/'))
155
+ fail(`${field} must start with /`);
156
+ return p
157
+ .slice(1)
158
+ .split('/')
159
+ .map((s) => s.replace(/~1/g, '/').replace(/~0/g, '~'));
160
+ }
161
+ if (Array.isArray(p)) {
162
+ return p.map((s) => typeof s === 'string' || typeof s === 'number' ? String(s) : fail(`${field} segments must be strings or numbers`));
163
+ }
164
+ return fail(`patch is missing \`${field}\``);
165
+ }
166
+ function arrayIndex(segment, len, allowEnd) {
167
+ if (allowEnd && segment === '-')
168
+ return len;
169
+ if (!/^\d+$/.test(segment))
170
+ fail(`invalid array index ${segment}`);
171
+ const i = Number(segment);
172
+ if (i > len || (!allowEnd && i === len))
173
+ fail(`array index ${i} out of range`);
174
+ return i;
175
+ }
176
+ function walk(doc, path) {
177
+ let cur = doc;
178
+ for (const seg of path) {
179
+ if (Array.isArray(cur))
180
+ cur = cur[arrayIndex(seg, cur.length, false)];
181
+ else if (cur !== null && typeof cur === 'object') {
182
+ if (!Object.prototype.hasOwnProperty.call(cur, seg))
183
+ fail(`path segment ${seg} not found`);
184
+ cur = cur[seg];
185
+ }
186
+ else
187
+ fail(`cannot descend into a scalar at ${seg}`);
188
+ }
189
+ return cur;
190
+ }
191
+ /**
192
+ * Sets an object member as an own data property. A plain assignment to
193
+ * `__proto__` would replace the object's prototype instead of storing the key,
194
+ * which a JSON document (and OPA's json.patch) treats as an ordinary member.
195
+ */
196
+ function setMember(obj, key, value) {
197
+ Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });
198
+ }
199
+ /** Returns the new document: `add` at the root replaces it. */
200
+ function add(doc, path, value) {
201
+ if (path.length === 0)
202
+ return value;
203
+ const parent = walk(doc, path.slice(0, -1));
204
+ const last = path[path.length - 1];
205
+ if (Array.isArray(parent))
206
+ parent.splice(arrayIndex(last, parent.length, true), 0, value);
207
+ else if (parent !== null && typeof parent === 'object')
208
+ setMember(parent, last, value);
209
+ else
210
+ fail('cannot add to a scalar');
211
+ return doc;
212
+ }
213
+ function remove(doc, path) {
214
+ if (path.length === 0)
215
+ fail('cannot remove the root');
216
+ const parent = walk(doc, path.slice(0, -1));
217
+ const last = path[path.length - 1];
218
+ if (Array.isArray(parent))
219
+ return parent.splice(arrayIndex(last, parent.length, false), 1)[0];
220
+ if (parent !== null && typeof parent === 'object') {
221
+ if (!Object.prototype.hasOwnProperty.call(parent, last))
222
+ fail(`path segment ${last} not found`);
223
+ const v = parent[last];
224
+ Reflect.deleteProperty(parent, last);
225
+ return v;
226
+ }
227
+ return fail('cannot remove from a scalar');
228
+ }
229
+ function equalJson(a, b) {
230
+ return JSON.stringify(sorted(a)) === JSON.stringify(sorted(b));
231
+ }
232
+ function applyPatch(doc, op) {
233
+ const path = pointer(op, 'path');
234
+ const value = () => (op['value'] === undefined ? fail('patch is missing `value`') : structuredClone(op['value']));
235
+ switch (op['op']) {
236
+ case 'add':
237
+ return add(doc, path, value());
238
+ case 'remove':
239
+ remove(doc, path);
240
+ return doc;
241
+ case 'replace': {
242
+ const v = value();
243
+ if (path.length === 0)
244
+ return v;
245
+ walk(doc, path);
246
+ return replaceAt(doc, path, v);
247
+ }
248
+ case 'move': {
249
+ const moved = remove(doc, pointer(op, 'from'));
250
+ return add(doc, path, moved);
251
+ }
252
+ case 'copy':
253
+ return add(doc, path, structuredClone(walk(doc, pointer(op, 'from'))));
254
+ case 'test':
255
+ if (!equalJson(walk(doc, path), op['value']))
256
+ fail('test operation failed');
257
+ return doc;
258
+ default:
259
+ return fail(`unsupported patch op ${String(op['op'])}`);
260
+ }
261
+ }
262
+ /** `replace` sets an existing member in place, where `add` would insert into an array. */
263
+ function replaceAt(doc, path, value) {
264
+ const parent = walk(doc, path.slice(0, -1));
265
+ const last = path[path.length - 1];
266
+ if (Array.isArray(parent))
267
+ parent[arrayIndex(last, parent.length, false)] = value;
268
+ else if (parent !== null && typeof parent === 'object')
269
+ setMember(parent, last, value);
270
+ return doc;
271
+ }
272
+ const escapeGoHtml = (text) => text
273
+ .replace(/</g, '\\u003c')
274
+ .replace(/>/g, '\\u003e')
275
+ .replace(/&/g, '\\u0026')
276
+ .replace(/\u2028/g, '\\u2028')
277
+ .replace(/\u2029/g, '\\u2029');
278
+ /** Go's `json.MarshalIndent` layout (JSON.stringify caps indent at 10 characters). */
279
+ function indentJson(v, indent, depth) {
280
+ const pad = indent.repeat(depth + 1);
281
+ const close = indent.repeat(depth);
282
+ if (Array.isArray(v)) {
283
+ if (v.length === 0)
284
+ return '[]';
285
+ return `[\n${v.map((x) => pad + indentJson(x, indent, depth + 1)).join(',\n')}\n${close}]`;
286
+ }
287
+ if (v !== null && typeof v === 'object') {
288
+ const entries = Object.entries(v);
289
+ if (entries.length === 0)
290
+ return '{}';
291
+ return `{\n${entries.map(([k, x]) => `${pad}${JSON.stringify(k)}: ${indentJson(x, indent, depth + 1)}`).join(',\n')}\n${close}}`;
292
+ }
293
+ return JSON.stringify(v);
294
+ }
295
+ const MAX_FIELD = 4096;
296
+ /** Go's `strconv.Quote`. */
297
+ function goQuoteChar(ch, quote) {
298
+ const cp = ch.codePointAt(0) ?? 0;
299
+ switch (ch) {
300
+ case '\x07': return '\\a';
301
+ case '\b': return '\\b';
302
+ case '\f': return '\\f';
303
+ case '\n': return '\\n';
304
+ case '\r': return '\\r';
305
+ case '\t': return '\\t';
306
+ case '\v': return '\\v';
307
+ case '\\': return '\\\\';
308
+ }
309
+ if (ch === quote)
310
+ return `\\${ch}`;
311
+ if (cp < 0x20 || cp === 0x7f)
312
+ return `\\x${cp.toString(16).padStart(2, '0')}`;
313
+ if (/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u.test(ch) || (/\s/u.test(ch) && ch !== ' ')) {
314
+ return cp <= 0xffff ? `\\u${cp.toString(16).padStart(4, '0')}` : `\\U${cp.toString(16).padStart(8, '0')}`;
315
+ }
316
+ return ch;
317
+ }
318
+ function goQuote(s) {
319
+ let out = '"';
320
+ for (const ch of s)
321
+ out += goQuoteChar(ch, '"');
322
+ return `${out}"`;
323
+ }
324
+ /** A value's Rego text, as `ast.Term.String()` writes it. */
325
+ function regoText(v) {
326
+ if (v === null)
327
+ return 'null';
328
+ if (typeof v === 'string')
329
+ return goQuote(v);
330
+ if (typeof v === 'number' || typeof v === 'boolean')
331
+ return String(v);
332
+ if (Array.isArray(v))
333
+ return `[${v.map(regoText).join(', ')}]`;
334
+ const obj = sorted(v);
335
+ return `{${Object.entries(obj).map(([k, x]) => `${goQuote(k)}: ${regoText(x)}`).join(', ')}}`;
336
+ }
337
+ function toArg(v) {
338
+ if (typeof v === 'number')
339
+ return Number.isInteger(v) ? { kind: 'int', value: v } : { kind: 'float', value: v };
340
+ if (typeof v === 'string')
341
+ return { kind: 'str', value: v };
342
+ return { kind: 'str', value: regoText(v) };
343
+ }
344
+ const typeName = (a) => (a.kind === 'int' ? 'int64' : a.kind === 'float' ? 'float64' : 'string');
345
+ const plain = (a) => a.kind === 'str' ? a.value : a.kind === 'int' ? String(a.value) : goFloat(Math.abs(a.value), 'g', undefined, a.value < 0);
346
+ /**
347
+ * The exact decimal expansion of a finite double: `digits` with the decimal
348
+ * point `point` places from the left. Doubles are dyadic rationals, so
349
+ * multiplying by 5^k turns m / 2^k into a whole number of 10^-k.
350
+ */
351
+ function exactDecimal(x) {
352
+ if (x === 0)
353
+ return { digits: '0', point: 1 };
354
+ const view = new DataView(new ArrayBuffer(8));
355
+ view.setFloat64(0, x);
356
+ const hi = view.getUint32(0);
357
+ const lo = view.getUint32(4);
358
+ const exponent = (hi >>> 20) & 0x7ff;
359
+ let mantissa = (BigInt(hi & 0xfffff) << 32n) | BigInt(lo);
360
+ let e = exponent - 1075;
361
+ if (exponent === 0)
362
+ e = -1074;
363
+ else
364
+ mantissa |= 1n << 52n;
365
+ if (e >= 0) {
366
+ const digits = (mantissa << BigInt(e)).toString();
367
+ return { digits, point: digits.length };
368
+ }
369
+ const k = -e;
370
+ const digits = (mantissa * 5n ** BigInt(k)).toString();
371
+ return { digits, point: digits.length - k };
372
+ }
373
+ /** Round a digit string to `keep` digits, half to even. Returns the digits and a carry. */
374
+ function roundDigits(digits, keep) {
375
+ if (keep >= digits.length)
376
+ return { digits: digits.padEnd(keep, '0'), carry: false };
377
+ if (keep < 0)
378
+ return { digits: '', carry: false };
379
+ const head = digits.slice(0, keep);
380
+ const rest = digits.slice(keep);
381
+ const first = rest.charCodeAt(0) - 48;
382
+ const tail = /[1-9]/.test(rest.slice(1));
383
+ const lastOdd = keep > 0 && (head.charCodeAt(keep - 1) - 48) % 2 === 1;
384
+ const up = first > 5 || (first === 5 && (tail || lastOdd));
385
+ if (!up)
386
+ return { digits: head, carry: false };
387
+ const n = (BigInt(head || '0') + 1n).toString().padStart(keep, '0');
388
+ return n.length > keep ? { digits: n.slice(0, keep), carry: true } : { digits: n, carry: false };
389
+ }
390
+ /** `%f` with `precision` fraction digits, magnitude only. */
391
+ function fixed(x, precision) {
392
+ const { digits, point } = exactDecimal(x);
393
+ const normalized = point <= 0 ? '0'.repeat(1 - point) + digits : digits;
394
+ const intLen = Math.max(point, 1);
395
+ const r = roundDigits(normalized, intLen + precision);
396
+ const all = r.carry ? `1${r.digits}` : r.digits;
397
+ const intPart = all.slice(0, all.length - precision) || '0';
398
+ return precision > 0 ? `${intPart}.${all.slice(all.length - precision)}` : intPart;
399
+ }
400
+ /** Significant digits and decimal exponent: `precision` digits, or the shortest. */
401
+ function scientific(x, precision) {
402
+ if (x === 0)
403
+ return { digits: '0'.repeat(precision ?? 1), exp: 0 };
404
+ if (precision === undefined) {
405
+ const [m, e] = x.toExponential().split('e');
406
+ return { digits: m.replace('.', ''), exp: Number(e) };
407
+ }
408
+ const { digits, point } = exactDecimal(x);
409
+ const lead = digits.search(/[1-9]/);
410
+ const significant = digits.slice(lead);
411
+ let exp = point - lead - 1;
412
+ const r = roundDigits(significant, precision);
413
+ if (r.carry) {
414
+ exp += 1;
415
+ return { digits: `1${r.digits.slice(0, precision - 1)}`, exp };
416
+ }
417
+ return { digits: r.digits, exp };
418
+ }
419
+ const goExp = (exp) => `e${exp < 0 ? '-' : '+'}${String(Math.abs(exp)).padStart(2, '0')}`;
420
+ /** `strconv.FormatFloat` for `e E f F g G`, magnitude only (sign added by the caller). */
421
+ function goFloat(x, verb, precision, negative = false) {
422
+ const sign = negative ? '-' : '';
423
+ if (Number.isNaN(x))
424
+ return 'NaN';
425
+ if (!Number.isFinite(x))
426
+ return `${negative ? '-' : '+'}Inf`;
427
+ if (verb === 'f' || verb === 'F')
428
+ return sign + fixed(x, precision ?? 6);
429
+ if (verb === 'e' || verb === 'E') {
430
+ const p = precision ?? 6;
431
+ const { digits, exp } = scientific(x, p + 1);
432
+ const text = `${digits[0]}${p > 0 ? `.${digits.slice(1)}` : ''}${goExp(exp)}`;
433
+ return sign + (verb === 'E' ? text.toUpperCase() : text);
434
+ }
435
+ // `%g`: shortest (or `precision` significant digits), in exponent form when
436
+ // the exponent is below -4 or at least the precision (6 when shortest).
437
+ if (x === 0)
438
+ return `${sign}0`;
439
+ const p = precision === undefined ? undefined : Math.max(precision, 1);
440
+ const sci = scientific(x, p);
441
+ let digits = p === undefined ? sci.digits : sci.digits.replace(/0+$/, '') || '0';
442
+ let text;
443
+ if (sci.exp < -4 || sci.exp >= (p ?? 6)) {
444
+ text = `${digits[0]}${digits.length > 1 ? `.${digits.slice(1)}` : ''}${goExp(sci.exp)}`;
445
+ }
446
+ else if (sci.exp < 0) {
447
+ text = `0.${'0'.repeat(-sci.exp - 1)}${digits}`;
448
+ }
449
+ else {
450
+ const point = sci.exp + 1;
451
+ digits = digits.padEnd(point, '0');
452
+ text = digits.length > point ? `${digits.slice(0, point)}.${digits.slice(point)}` : digits;
453
+ }
454
+ return sign + (verb === 'G' ? text.toUpperCase() : text);
455
+ }
456
+ function signed(spec, body, negative) {
457
+ if (negative)
458
+ return `-${body}`;
459
+ if (spec.plus)
460
+ return `+${body}`;
461
+ if (spec.space)
462
+ return ` ${body}`;
463
+ return body;
464
+ }
465
+ function pad(spec, body, zero) {
466
+ const len = [...body].length;
467
+ if (spec.width === undefined || len >= spec.width)
468
+ return body;
469
+ const fill = spec.width - len;
470
+ if (spec.minus)
471
+ return body + ' '.repeat(fill);
472
+ if (zero) {
473
+ const signLen = /^[-+ ]/.test(body) ? 1 : 0;
474
+ return body.slice(0, signLen) + '0'.repeat(fill) + body.slice(signLen);
475
+ }
476
+ return ' '.repeat(fill) + body;
477
+ }
478
+ function intText(spec, value, radix, upper) {
479
+ let digits = Math.abs(value).toString(radix);
480
+ if (upper)
481
+ digits = digits.toUpperCase();
482
+ if (spec.precision !== undefined) {
483
+ if (spec.precision === 0 && value === 0)
484
+ digits = '';
485
+ digits = digits.padStart(spec.precision, '0');
486
+ }
487
+ if (spec.sharp) {
488
+ if (radix === 16)
489
+ digits = (upper ? '0X' : '0x') + digits;
490
+ else if (radix === 8 && !digits.startsWith('0'))
491
+ digits = `0${digits}`;
492
+ else if (radix === 2)
493
+ digits = `0b${digits}`;
494
+ }
495
+ return signed(spec, digits, value < 0);
496
+ }
497
+ function truncate(s, precision) {
498
+ return precision === undefined ? s : [...s].slice(0, precision).join('');
499
+ }
500
+ function hexOf(s) {
501
+ return Array.from(new TextEncoder().encode(s), (b) => b.toString(16).padStart(2, '0')).join('');
502
+ }
503
+ function formatOne(verb, spec, a) {
504
+ const bad = () => `%!${verb}(${typeName(a)}=${plain(a)})`;
505
+ let body;
506
+ if (verb === 'T')
507
+ return pad(spec, typeName(a), spec.zero);
508
+ if (a.kind === 'int') {
509
+ switch (verb) {
510
+ case 'v':
511
+ case 'd':
512
+ body = intText(spec, a.value, 10, false);
513
+ break;
514
+ case 'x':
515
+ body = intText(spec, a.value, 16, false);
516
+ break;
517
+ case 'X':
518
+ body = intText(spec, a.value, 16, true);
519
+ break;
520
+ case 'o':
521
+ body = intText(spec, a.value, 8, false);
522
+ break;
523
+ case 'b':
524
+ body = intText(spec, a.value, 2, false);
525
+ break;
526
+ case 'c':
527
+ body = String.fromCodePoint(a.value);
528
+ break;
529
+ case 'q':
530
+ body = `'${goQuoteChar(String.fromCodePoint(a.value), "'")}'`;
531
+ break;
532
+ default: return bad();
533
+ }
534
+ }
535
+ else if (a.kind === 'float') {
536
+ if (!'vgGeEfF'.includes(verb))
537
+ return bad();
538
+ body = signed(spec, goFloat(Math.abs(a.value), verb === 'v' ? 'g' : verb, spec.precision), a.value < 0);
539
+ }
540
+ else {
541
+ switch (verb) {
542
+ case 'v':
543
+ case 's':
544
+ body = truncate(a.value, spec.precision);
545
+ break;
546
+ case 'q':
547
+ body = goQuote(truncate(a.value, spec.precision));
548
+ break;
549
+ case 'x':
550
+ body = hexOf(truncate(a.value, spec.precision));
551
+ break;
552
+ case 'X':
553
+ body = hexOf(truncate(a.value, spec.precision)).toUpperCase();
554
+ break;
555
+ default: return bad();
556
+ }
557
+ }
558
+ const zero = spec.zero && !(a.kind === 'int' && spec.precision !== undefined);
559
+ return pad(spec, body, zero);
560
+ }
561
+ function goSprintf(format, values) {
562
+ const chars = [...format];
563
+ let out = '';
564
+ let next = 0;
565
+ let i = 0;
566
+ const digits = () => {
567
+ let n;
568
+ while (i < chars.length && /[0-9]/.test(chars[i])) {
569
+ n = (n ?? 0) * 10 + Number(chars[i]);
570
+ if (n > MAX_FIELD)
571
+ fail(`field width or precision over ${MAX_FIELD}`);
572
+ i += 1;
573
+ }
574
+ return n;
575
+ };
576
+ while (i < chars.length) {
577
+ const c = chars[i++];
578
+ if (c !== '%') {
579
+ out += c;
580
+ continue;
581
+ }
582
+ const spec = { plus: false, minus: false, sharp: false, zero: false, space: false };
583
+ for (; i < chars.length; i++) {
584
+ const f = chars[i];
585
+ if (f === '+')
586
+ spec.plus = true;
587
+ else if (f === '-')
588
+ spec.minus = true;
589
+ else if (f === '#')
590
+ spec.sharp = true;
591
+ else if (f === '0')
592
+ spec.zero = true;
593
+ else if (f === ' ')
594
+ spec.space = true;
595
+ else
596
+ break;
597
+ }
598
+ spec.width = digits();
599
+ if (chars[i] === '.') {
600
+ i += 1;
601
+ spec.precision = digits() ?? 0;
602
+ }
603
+ if (i >= chars.length) {
604
+ out += '%!(NOVERB)';
605
+ break;
606
+ }
607
+ const verb = chars[i++];
608
+ if (verb === '%') {
609
+ out += '%';
610
+ continue;
611
+ }
612
+ const value = values[next];
613
+ if (!value) {
614
+ out += `%!${verb}(MISSING)`;
615
+ continue;
616
+ }
617
+ next += 1;
618
+ out += formatOne(verb, spec, value);
619
+ }
620
+ if (next < values.length) {
621
+ out += `%!(EXTRA ${values.slice(next).map((v) => `${typeName(v)}=${plain(v)}`).join(', ')})`;
622
+ }
623
+ return out;
624
+ }
625
+ // ── the table ──────────────────────────────────────────────────────────
626
+ /** Every builtin the host provides, by OPA name. */
627
+ exports.REGO_HOST_BUILTINS = {
628
+ sprintf: (_, args) => {
629
+ const values = args[1];
630
+ if (!Array.isArray(values))
631
+ fail('operand 2 must be an array');
632
+ return goSprintf(str(args, 0), values.map(toArg));
633
+ },
634
+ 'time.now_ns': (ctx) => ctx.nowNs,
635
+ 'crypto.sha1': (ctx, args) => (ctx.digest ?? fail('no digest provided'))('sha1', str(args, 0)),
636
+ 'crypto.sha256': (ctx, args) => (ctx.digest ?? fail('no digest provided'))('sha256', str(args, 0)),
637
+ 'regex.find_n': (_, args) => findAll(compile(str(args, 0)), str(args, 1), int(args, 2)).map((m) => m[0]),
638
+ 'regex.replace': (_, args) => {
639
+ const s = str(args, 0);
640
+ const re = compile(str(args, 1));
641
+ const template = str(args, 2);
642
+ const matches = findAll(re, s, -1);
643
+ if (s.length + matches.length * template.length > MAX_REPLACED_BYTES)
644
+ fail('the replaced string would be over 1 MB');
645
+ let out = '';
646
+ let last = 0;
647
+ for (const m of matches) {
648
+ out += s.slice(last, m.index) + expand(template, m);
649
+ last = m.index + m[0].length;
650
+ }
651
+ return out + s.slice(last);
652
+ },
653
+ 'regex.split': (_, args) => {
654
+ const pattern = str(args, 0);
655
+ const s = str(args, 1);
656
+ const re = compile(pattern);
657
+ if (pattern !== '' && s === '')
658
+ return [''];
659
+ const parts = [];
660
+ let beg = 0;
661
+ let end = 0;
662
+ for (const m of findAll(re, s, -1)) {
663
+ end = m.index;
664
+ if (m.index + m[0].length !== 0)
665
+ parts.push(s.slice(beg, end));
666
+ beg = m.index + m[0].length;
667
+ }
668
+ if (end !== s.length)
669
+ parts.push(s.slice(beg));
670
+ return parts;
671
+ },
672
+ 'strings.any_prefix_match': (_, args) => {
673
+ const base = strs(args, 1);
674
+ return strs(args, 0).some((s) => base.some((b) => s.startsWith(b)));
675
+ },
676
+ 'strings.any_suffix_match': (_, args) => {
677
+ const base = strs(args, 1);
678
+ return strs(args, 0).some((s) => base.some((b) => s.endsWith(b)));
679
+ },
680
+ 'strings.count': (_, args) => {
681
+ const s = str(args, 0);
682
+ const sub = str(args, 1);
683
+ return sub === '' ? [...s].length + 1 : s.split(sub).length - 1;
684
+ },
685
+ indexof_n: (_, args) => {
686
+ const hay = [...str(args, 0)];
687
+ const pat = [...str(args, 1)];
688
+ if (pat.length === 0)
689
+ fail('empty search character');
690
+ const found = [];
691
+ for (let i = 0; i + pat.length <= hay.length; i++) {
692
+ if (pat.every((c, j) => hay[i + j] === c))
693
+ found.push(i);
694
+ }
695
+ return found;
696
+ },
697
+ 'json.patch': (_, args) => {
698
+ const ops = args[1];
699
+ if (!Array.isArray(ops))
700
+ fail('patches must be an array');
701
+ let doc = structuredClone(args[0]);
702
+ for (const op of ops) {
703
+ if (op === null || typeof op !== 'object')
704
+ fail('a patch must be an object');
705
+ doc = applyPatch(doc, op);
706
+ }
707
+ return doc;
708
+ },
709
+ 'json.marshal_with_options': (_, args) => {
710
+ const opts = args[1];
711
+ if (opts === null || typeof opts !== 'object' || Array.isArray(opts))
712
+ fail('options must be an object');
713
+ let pretty = false;
714
+ let indent = '\t';
715
+ let prefix = '';
716
+ for (const [key, v] of Object.entries(opts)) {
717
+ if (key === 'pretty' && typeof v === 'boolean')
718
+ pretty = v;
719
+ else if (key === 'indent' && typeof v === 'string')
720
+ [pretty, indent] = [true, v];
721
+ else if (key === 'prefix' && typeof v === 'string')
722
+ [pretty, prefix] = [true, v];
723
+ else
724
+ fail(`invalid option ${key}`);
725
+ }
726
+ const value = sorted(args[0]);
727
+ const text = escapeGoHtml(pretty ? indentJson(value, indent, 0) : JSON.stringify(value));
728
+ return pretty ? prefix + text.replace(/\n/g, `\n${prefix}`) : text;
729
+ },
730
+ };
731
+ exports.REGO_HOST_BUILTIN_NAMES = Object.keys(exports.REGO_HOST_BUILTINS).sort();
732
+ //# sourceMappingURL=regoBuiltins.js.map