@burojs/config 0.2.0 → 0.5.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.
package/dist/index.cjs CHANGED
@@ -1,563 +1,519 @@
1
- 'use strict';
2
-
3
- var yaml = require('yaml');
4
-
5
- // src/parse.ts
6
- var RESERVED_KEYS = /* @__PURE__ */ new Set(["__proto__", "prototype", "constructor"]);
7
- var FORBIDDEN_VALUE_CATEGORIES = /* @__PURE__ */ new Map([
8
- ["action", "action"],
9
- ["actions", "action"],
10
- ["provider", "provider"],
11
- ["providers", "provider"],
12
- ["mapper", "mapper"],
13
- ["mappers", "mapper"],
14
- ["service", "service"],
15
- ["services", "service"],
16
- ["credential", "credential"],
17
- ["credentials", "credential"],
18
- ["schema", "schema"],
19
- ["schemas", "schema"],
20
- ["component", "component"],
21
- ["components", "component"],
22
- ["attachment", "attachment"],
23
- ["attachments", "attachment"],
24
- ["validator", "validator"],
25
- ["validators", "validator"]
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _burojs_core = require("@burojs/core");
3
+ let _burojs_core_internal = require("@burojs/core/internal");
4
+ let yaml = require("yaml");
5
+ //#region src/source-policy.ts
6
+ const MAX_SOURCE_DEPTH = _burojs_core.DEFAULT_COMPILER_LIMITS.maxPortableDepth;
7
+ const SCREEN_SOURCE_LIMITS = Object.freeze({
8
+ maxBytes: 256e3,
9
+ maxDeclarations: 64,
10
+ maxDepth: MAX_SOURCE_DEPTH,
11
+ maxNodes: 5e3,
12
+ maxStringLength: 4096,
13
+ maxDiagnostics: 32
14
+ });
15
+ //#endregion
16
+ //#region src/parse.ts
17
+ const LAYER_KEYS = new Set([
18
+ "id",
19
+ "priority",
20
+ "defaultNamespace",
21
+ "revision",
22
+ "declarations",
23
+ "transforms"
26
24
  ]);
27
- var LAYER_KEYS = /* @__PURE__ */ new Set([
28
- "id",
29
- "priority",
30
- "defaultNamespace",
31
- "revision",
32
- "declarations",
33
- "transforms"
25
+ const DECLARATION_KEYS = new Set([
26
+ "kind",
27
+ "ref",
28
+ "schemaVersion",
29
+ "value"
34
30
  ]);
35
- var DECLARATION_KEYS = /* @__PURE__ */ new Set(["kind", "ref", "schemaVersion", "value"]);
36
- var TRANSFORM_KEYS = /* @__PURE__ */ new Set(["id", "scope", "select", "operation"]);
37
- var SELECTOR_KEYS = /* @__PURE__ */ new Set(["kinds", "namespaces", "layers", "refs", "excludeRefs", "tags"]);
38
- var OPERATION_KEYS = {
39
- merge: /* @__PURE__ */ new Set(["op", "value"]),
40
- replace: /* @__PURE__ */ new Set(["op", "value", "ifMissing"]),
41
- disable: /* @__PURE__ */ new Set(["op", "reason"]),
42
- delete: /* @__PURE__ */ new Set(["op"])
31
+ const TRANSFORM_KEYS = new Set([
32
+ "id",
33
+ "scope",
34
+ "select",
35
+ "operation"
36
+ ]);
37
+ const SELECTOR_KEYS = new Set([
38
+ "kinds",
39
+ "namespaces",
40
+ "layers",
41
+ "refs",
42
+ "excludeRefs",
43
+ "tags"
44
+ ]);
45
+ const OPERATION_KEYS = {
46
+ merge: new Set(["op", "value"]),
47
+ replace: new Set([
48
+ "op",
49
+ "value",
50
+ "ifMissing"
51
+ ]),
52
+ disable: new Set(["op", "reason"]),
53
+ delete: new Set(["op"])
43
54
  };
44
- var ATTACHMENT_TAG = "@burojs/core/trusted-attachment";
55
+ const ATTACHMENT_TAG = "@burojs/core/trusted-attachment";
45
56
  var Diagnostics = class {
46
- constructor(limit) {
47
- this.limit = limit;
48
- }
49
- limit;
50
- values = [];
51
- limited = false;
52
- add(code) {
53
- if (this.limited) return;
54
- if (this.values.length + 1 < this.limit) {
55
- this.values.push(Object.freeze({ code, message: messageFor(code) }));
56
- return;
57
- }
58
- this.limited = true;
59
- this.values.push(
60
- Object.freeze({
61
- code: "source.diagnostic-limit",
62
- message: messageFor("source.diagnostic-limit")
63
- })
64
- );
65
- }
57
+ limit;
58
+ values = [];
59
+ limited = false;
60
+ constructor(limit) {
61
+ this.limit = limit;
62
+ }
63
+ add(code) {
64
+ if (this.limited) return;
65
+ if (this.values.length + 1 < this.limit) {
66
+ this.values.push(Object.freeze({
67
+ code,
68
+ message: messageFor(code)
69
+ }));
70
+ return;
71
+ }
72
+ this.limited = true;
73
+ this.values.push(Object.freeze({
74
+ code: "source.diagnostic-limit",
75
+ message: messageFor("source.diagnostic-limit")
76
+ }));
77
+ }
66
78
  };
67
79
  function messageFor(code) {
68
- switch (code) {
69
- case "source.invalid-json":
70
- return "The source is not valid JSON.";
71
- case "source.invalid-yaml":
72
- return "The source is not valid YAML.";
73
- case "source.invalid-policy":
74
- return "The source policy is invalid.";
75
- case "source.byte-limit":
76
- return "The source exceeds its byte limit.";
77
- case "source.declaration-limit":
78
- return "The source exceeds its declaration limit.";
79
- case "source.depth-limit":
80
- return "The source exceeds its nesting-depth limit.";
81
- case "source.string-limit":
82
- return "The source contains an overlong string.";
83
- case "source.diagnostic-limit":
84
- return "Additional source diagnostics were omitted.";
85
- case "source.unknown-key":
86
- return "The source contains an unknown key.";
87
- case "source.reserved-key":
88
- return "The source contains a reserved key.";
89
- case "source.accessor":
90
- return "The source must use data properties.";
91
- case "source.cyclic-value":
92
- return "The source must not contain cyclic values.";
93
- case "source.invalid-layer":
94
- return "The source layer is invalid.";
95
- case "source.invalid-declaration":
96
- return "The source declaration is invalid.";
97
- case "source.invalid-transform":
98
- return "The source transform is invalid.";
99
- case "source.forbidden-kind":
100
- return "The source requests a forbidden declaration kind.";
101
- case "source.forbidden-namespace":
102
- return "The source requests a forbidden namespace.";
103
- case "source.forbidden-transform-op":
104
- return "The source requests a forbidden transform operation.";
105
- case "source.forbidden-transform-scope":
106
- return "The source requests a forbidden transform scope.";
107
- case "source.forbidden-capability":
108
- return "The source requests a forbidden capability.";
109
- case "source.forbidden-action":
110
- return "The source requests a forbidden action binding.";
111
- case "source.forbidden-provider":
112
- return "The source requests a forbidden provider binding.";
113
- case "source.forbidden-mapper":
114
- return "The source requests a forbidden mapper binding.";
115
- case "source.forbidden-service":
116
- return "The source requests a forbidden service value.";
117
- case "source.forbidden-credential":
118
- return "The source requests a forbidden credential value.";
119
- case "source.forbidden-schema":
120
- return "The source requests a forbidden schema value.";
121
- case "source.forbidden-component":
122
- return "The source requests a forbidden component value.";
123
- case "source.forbidden-attachment":
124
- return "The source requests a forbidden attachment value.";
125
- case "source.forbidden-validator":
126
- return "The source requests a forbidden validator value.";
127
- default:
128
- return "The source is invalid.";
129
- }
80
+ switch (code) {
81
+ case "source.invalid-json": return "The source is not valid JSON.";
82
+ case "source.invalid-yaml": return "The source is not valid YAML.";
83
+ case "source.invalid-policy": return "The source policy is invalid.";
84
+ case "source.byte-limit": return "The source exceeds its byte limit.";
85
+ case "source.declaration-limit": return "The source exceeds its declaration limit.";
86
+ case "source.depth-limit": return "The source exceeds its nesting-depth limit.";
87
+ case "source.node-limit": return "The source exceeds its value-count limit.";
88
+ case "source.string-limit": return "The source contains an overlong string.";
89
+ case "source.diagnostic-limit": return "Additional source diagnostics were omitted.";
90
+ case "source.unknown-key": return "The source contains an unknown key.";
91
+ case "source.reserved-key": return "The source contains a reserved key.";
92
+ case "source.accessor": return "The source must use data properties.";
93
+ case "source.cyclic-value": return "The source must not contain cyclic values.";
94
+ case "source.invalid-layer": return "The source layer is invalid.";
95
+ case "source.invalid-declaration": return "The source declaration is invalid.";
96
+ case "source.invalid-transform": return "The source transform is invalid.";
97
+ case "source.forbidden-kind": return "The source requests a forbidden declaration kind.";
98
+ case "source.forbidden-namespace": return "The source requests a forbidden namespace.";
99
+ case "source.forbidden-transform-op": return "The source requests a forbidden transform operation.";
100
+ case "source.forbidden-transform-scope": return "The source requests a forbidden transform scope.";
101
+ case "source.forbidden-capability": return "The source requests a forbidden capability.";
102
+ case "source.forbidden-action": return "The source requests a forbidden action binding; a resource's flows are written under toolbar.";
103
+ case "source.forbidden-provider": return "The source requests a forbidden provider binding.";
104
+ case "source.forbidden-mapper": return "The source requests a forbidden mapper binding.";
105
+ case "source.forbidden-service": return "The source requests a forbidden service value.";
106
+ case "source.forbidden-credential": return "The source requests a forbidden credential value.";
107
+ case "source.forbidden-schema": return "The source requests a forbidden schema value.";
108
+ case "source.forbidden-component": return "The source requests a forbidden component value.";
109
+ case "source.forbidden-attachment": return "The source requests a forbidden attachment value.";
110
+ case "source.forbidden-validator": return "The source requests a forbidden validator value.";
111
+ default: return "The source is invalid.";
112
+ }
130
113
  }
114
+ const LIMIT_KEYS = [
115
+ "maxBytes",
116
+ "maxDeclarations",
117
+ "maxDepth",
118
+ "maxNodes",
119
+ "maxStringLength",
120
+ "maxDiagnostics"
121
+ ];
122
+ const LIMIT_KEY_SET = new Set(LIMIT_KEYS);
131
123
  function validLimits(limits) {
132
- return Object.values(limits).every((value) => Number.isSafeInteger(value) && value > 0);
124
+ return plainObject(limits) && Object.keys(limits).every((key) => LIMIT_KEY_SET.has(key)) && LIMIT_KEYS.every((key) => Number.isSafeInteger(limits[key]) && limits[key] > 0);
133
125
  }
134
126
  function validCapabilities(value) {
135
- return value !== null && typeof value === "object" && !Array.isArray(value) && Object.values(value).every(
136
- (capabilities) => Array.isArray(capabilities) && capabilities.every((capability) => typeof capability === "string")
137
- );
127
+ return value !== null && typeof value === "object" && !Array.isArray(value) && Object.values(value).every((capabilities) => Array.isArray(capabilities) && capabilities.every((capability) => typeof capability === "string"));
128
+ }
129
+ function grantsError(policy) {
130
+ try {
131
+ (0, _burojs_core.normalizeLayerGrants)(policy.grants);
132
+ return;
133
+ } catch (error) {
134
+ return error instanceof TypeError ? error.message : "";
135
+ }
138
136
  }
139
- function validPolicy(policy) {
140
- return policy !== null && typeof policy === "object" && Array.isArray(policy.allowedKinds) && Array.isArray(policy.allowedNamespaces) && Array.isArray(policy.allowedTransformOps) && (policy.maxTransformScope === "layer" || policy.maxTransformScope === "namespace" || policy.maxTransformScope === "application") && validCapabilities(policy.allowedCapabilities) && validLimits(policy.limits);
137
+ function policyDiagnostic(policy) {
138
+ const generic = messageFor("source.invalid-policy");
139
+ const stray = strayPolicyKey(policy);
140
+ if (stray !== void 0) return Object.freeze({
141
+ code: "source.invalid-policy",
142
+ message: `The source policy is invalid: it carries ${stray} at the top level; a policy is { grants, allowedCapabilities, limits }, with every grant field inside grants.`
143
+ });
144
+ if (!validPolicyShape(policy)) return Object.freeze({
145
+ code: "source.invalid-policy",
146
+ message: generic
147
+ });
148
+ const reason = grantsError(policy);
149
+ if (reason === void 0) return void 0;
150
+ return Object.freeze({
151
+ code: "source.invalid-policy",
152
+ message: reason === "" ? generic : `The source policy is invalid: ${reason}`
153
+ });
154
+ }
155
+ const POLICY_KEYS = new Set([
156
+ "grants",
157
+ "allowedCapabilities",
158
+ "limits"
159
+ ]);
160
+ function strayPolicyKey(policy) {
161
+ return plainObject(policy) ? Object.keys(policy).find((key) => !POLICY_KEYS.has(key)) : void 0;
162
+ }
163
+ function validPolicyShape(policy) {
164
+ return plainObject(policy) && plainObject(policy.grants) && validCapabilities(policy.allowedCapabilities) && validLimits(policy.limits);
165
+ }
166
+ function ownedPolicy(policy) {
167
+ const invalid = policyDiagnostic(policy);
168
+ if (invalid !== void 0) throw new TypeError(invalid.message);
169
+ const capabilities = Object.create(null);
170
+ for (const [kind, values] of Object.entries(policy.allowedCapabilities)) capabilities[kind] = Object.freeze([...values]);
171
+ return Object.freeze({
172
+ grants: (0, _burojs_core.normalizeLayerGrants)(policy.grants),
173
+ allowedCapabilities: Object.freeze(capabilities),
174
+ limits: Object.freeze({ ...policy.limits })
175
+ });
141
176
  }
142
177
  function failure(diagnostics) {
143
- return { ok: false, diagnostics: Object.freeze(diagnostics.values) };
178
+ return {
179
+ ok: false,
180
+ diagnostics: Object.freeze(diagnostics.values)
181
+ };
144
182
  }
145
183
  function plainObject(value) {
146
- if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
147
- const prototype = Object.getPrototypeOf(value);
148
- return prototype === Object.prototype || prototype === null;
184
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
185
+ const prototype = Object.getPrototypeOf(value);
186
+ return prototype === Object.prototype || prototype === null;
149
187
  }
150
188
  function ownData(value, key) {
151
- const descriptor = Object.getOwnPropertyDescriptor(value, key);
152
- return descriptor !== void 0 && "value" in descriptor ? descriptor.value : void 0;
189
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
190
+ return descriptor !== void 0 && "value" in descriptor ? descriptor.value : void 0;
153
191
  }
154
192
  function hasData(value, key) {
155
- const descriptor = Object.getOwnPropertyDescriptor(value, key);
156
- return descriptor !== void 0 && "value" in descriptor;
193
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
194
+ return descriptor !== void 0 && "value" in descriptor;
195
+ }
196
+ const MAX_ENVELOPE_DEPTH = 8;
197
+ function childRole(role, key) {
198
+ switch (role) {
199
+ case "root": return key === "declarations" ? "declarations" : key === "transforms" ? "transforms" : "envelope";
200
+ case "declarations": return "declaration";
201
+ case "transforms": return "transform";
202
+ case "declaration": return key === "value" ? "value" : "envelope";
203
+ case "transform": return key === "operation" ? "operation" : "envelope";
204
+ case "operation": return key === "value" ? "value" : "envelope";
205
+ default: return role;
206
+ }
157
207
  }
158
208
  function clonePortable(value, limits, diagnostics) {
159
- const ancestors = /* @__PURE__ */ new WeakSet();
160
- function copy(current, depth) {
161
- if (depth > limits.maxDepth) {
162
- diagnostics.add("source.depth-limit");
163
- return null;
164
- }
165
- if (current === null || typeof current === "boolean" || typeof current === "number")
166
- return current;
167
- if (typeof current === "string") {
168
- if ([...current].length > limits.maxStringLength) diagnostics.add("source.string-limit");
169
- return current;
170
- }
171
- if (typeof current !== "object") {
172
- diagnostics.add("source.invalid-layer");
173
- return null;
174
- }
175
- if (ancestors.has(current)) {
176
- diagnostics.add("source.cyclic-value");
177
- return null;
178
- }
179
- ancestors.add(current);
180
- if (Array.isArray(current)) {
181
- const output2 = [];
182
- for (const key of Reflect.ownKeys(current)) {
183
- if (typeof key !== "string" || key !== "length" && !/^(0|[1-9]\d*)$/u.test(key)) {
184
- diagnostics.add("source.invalid-layer");
185
- continue;
186
- }
187
- if (key === "length") continue;
188
- if (!hasData(current, key)) diagnostics.add("source.accessor");
189
- }
190
- const length = ownData(current, "length");
191
- const arrayLength = typeof length === "number" && Number.isSafeInteger(length) && length >= 0 ? length : void 0;
192
- if (arrayLength === void 0) diagnostics.add("source.invalid-layer");
193
- else
194
- for (let index = 0; index < arrayLength; index += 1) {
195
- if (!hasData(current, String(index))) diagnostics.add("source.accessor");
196
- output2.push(copy(ownData(current, String(index)), depth + 1));
197
- }
198
- ancestors.delete(current);
199
- return Object.freeze(output2);
200
- }
201
- if (!plainObject(current)) diagnostics.add("source.invalid-layer");
202
- const output = /* @__PURE__ */ Object.create(null);
203
- for (const key of Reflect.ownKeys(current)) {
204
- if (typeof key !== "string") {
205
- diagnostics.add("source.invalid-layer");
206
- continue;
207
- }
208
- if (RESERVED_KEYS.has(key)) diagnostics.add("source.reserved-key");
209
- if ([...key].length > limits.maxStringLength) diagnostics.add("source.string-limit");
210
- if (!hasData(current, key)) {
211
- diagnostics.add("source.accessor");
212
- continue;
213
- }
214
- output[key] = copy(ownData(current, key), depth + 1);
215
- }
216
- ancestors.delete(current);
217
- return Object.freeze(output);
218
- }
219
- return copy(value, 0);
209
+ const ancestors = new WeakSet();
210
+ const maxDepth = Math.min(limits.maxDepth, MAX_SOURCE_DEPTH);
211
+ let nodes = 0;
212
+ let nodesExceeded = false;
213
+ function overNodeBudget(children) {
214
+ if (children <= limits.maxNodes - nodes) return false;
215
+ if (!nodesExceeded) diagnostics.add("source.node-limit");
216
+ nodesExceeded = true;
217
+ return true;
218
+ }
219
+ function copy(current, depth, role) {
220
+ nodes += 1;
221
+ if (nodes > limits.maxNodes) {
222
+ if (!nodesExceeded) diagnostics.add("source.node-limit");
223
+ nodesExceeded = true;
224
+ return null;
225
+ }
226
+ if (depth > (role === "value" ? maxDepth : MAX_ENVELOPE_DEPTH)) {
227
+ diagnostics.add("source.depth-limit");
228
+ return null;
229
+ }
230
+ if (current === null || typeof current === "boolean" || typeof current === "number") return current;
231
+ if (typeof current === "string") {
232
+ if ([...current].length > limits.maxStringLength) diagnostics.add("source.string-limit");
233
+ return current;
234
+ }
235
+ if (typeof current !== "object") {
236
+ diagnostics.add("source.invalid-layer");
237
+ return null;
238
+ }
239
+ if (ancestors.has(current)) {
240
+ diagnostics.add("source.cyclic-value");
241
+ return null;
242
+ }
243
+ ancestors.add(current);
244
+ if (Array.isArray(current)) {
245
+ const output = [];
246
+ const length = ownData(current, "length");
247
+ const arrayLength = typeof length === "number" && Number.isSafeInteger(length) && length >= 0 ? length : void 0;
248
+ if (arrayLength !== void 0 && overNodeBudget(arrayLength)) {
249
+ ancestors.delete(current);
250
+ return null;
251
+ }
252
+ for (const key of Reflect.ownKeys(current)) {
253
+ if (typeof key !== "string" || key !== "length" && !/^(0|[1-9]\d*)$/u.test(key)) {
254
+ diagnostics.add("source.invalid-layer");
255
+ continue;
256
+ }
257
+ if (key === "length") continue;
258
+ if (!hasData(current, key)) diagnostics.add("source.accessor");
259
+ }
260
+ if (arrayLength === void 0) diagnostics.add("source.invalid-layer");
261
+ else for (let index = 0; index < arrayLength && !nodesExceeded; index += 1) {
262
+ if (!hasData(current, String(index))) diagnostics.add("source.accessor");
263
+ output.push(copy(ownData(current, String(index)), depth + 1, childRole(role, void 0)));
264
+ }
265
+ ancestors.delete(current);
266
+ return Object.freeze(output);
267
+ }
268
+ if (!plainObject(current)) diagnostics.add("source.invalid-layer");
269
+ const output = Object.create(null);
270
+ const keys = Reflect.ownKeys(current);
271
+ if (overNodeBudget(keys.length)) {
272
+ ancestors.delete(current);
273
+ return null;
274
+ }
275
+ for (const key of keys) {
276
+ if (nodesExceeded) break;
277
+ if (typeof key !== "string") {
278
+ diagnostics.add("source.invalid-layer");
279
+ continue;
280
+ }
281
+ if ((0, _burojs_core_internal.isReservedSegment)(key)) diagnostics.add("source.reserved-key");
282
+ if ([...key].length > limits.maxStringLength) diagnostics.add("source.string-limit");
283
+ if (!hasData(current, key)) {
284
+ diagnostics.add("source.accessor");
285
+ continue;
286
+ }
287
+ const next = childRole(role, key);
288
+ const nextDepth = next === "value" && role !== "value" ? 0 : depth + 1;
289
+ output[key] = copy(ownData(current, key), nextDepth, next);
290
+ }
291
+ ancestors.delete(current);
292
+ return Object.freeze(output);
293
+ }
294
+ return copy(value, 0, "root");
220
295
  }
221
296
  function unknownKeys(value, allowed, diagnostics) {
222
- for (const key of Object.keys(value))
223
- if (!allowed.has(key)) diagnostics.add("source.unknown-key");
297
+ for (const key of Object.keys(value)) if (!allowed.has(key)) diagnostics.add("source.unknown-key");
224
298
  }
225
299
  function allowedNamespace(value, policy, diagnostics) {
226
- if (typeof value !== "string" || !policy.allowedNamespaces.includes(value)) {
227
- diagnostics.add("source.forbidden-namespace");
228
- return false;
229
- }
230
- return true;
300
+ if (typeof value !== "string" || !policy.grants.allowedNamespaces.includes(value)) {
301
+ diagnostics.add("source.forbidden-namespace");
302
+ return false;
303
+ }
304
+ return true;
231
305
  }
232
306
  function namespaceFor(ref, defaultNamespace, diagnostics) {
233
- if (typeof ref === "string") {
234
- const separator = ref.indexOf(":");
235
- return separator === -1 ? defaultNamespace : ref.slice(0, separator);
236
- }
237
- if (!plainObject(ref) || !hasData(ref, "id")) {
238
- diagnostics.add("source.invalid-declaration");
239
- return void 0;
240
- }
241
- for (const key of Object.keys(ref))
242
- if (key !== "id" && key !== "namespace") diagnostics.add("source.unknown-key");
243
- const namespace = hasData(ref, "namespace") ? ownData(ref, "namespace") : defaultNamespace;
244
- if (typeof namespace !== "string") diagnostics.add("source.invalid-declaration");
245
- return typeof namespace === "string" ? namespace : void 0;
307
+ if (typeof ref === "string") {
308
+ const separator = ref.indexOf(":");
309
+ return separator === -1 ? defaultNamespace : ref.slice(0, separator);
310
+ }
311
+ if (!plainObject(ref) || !hasData(ref, "id")) {
312
+ diagnostics.add("source.invalid-declaration");
313
+ return;
314
+ }
315
+ for (const key of Object.keys(ref)) if (key !== "id" && key !== "namespace") diagnostics.add("source.unknown-key");
316
+ const namespace = hasData(ref, "namespace") ? ownData(ref, "namespace") : defaultNamespace;
317
+ if (typeof namespace !== "string") diagnostics.add("source.invalid-declaration");
318
+ return typeof namespace === "string" ? namespace : void 0;
246
319
  }
247
320
  function validateValue(value, kind, policy, diagnostics) {
248
- if (value === null || typeof value !== "object") return;
249
- if (Array.isArray(value)) {
250
- for (const item of value) validateValue(item, kind, policy, diagnostics);
251
- return;
252
- }
253
- if (!plainObject(value)) return;
254
- if (value.$buro === ATTACHMENT_TAG) {
255
- const capability = ownData(value, "capability");
256
- if (typeof capability !== "string" || !policy.allowedCapabilities[kind]?.includes(capability)) {
257
- diagnostics.add("source.forbidden-capability");
258
- }
259
- }
260
- for (const key of Object.keys(value)) {
261
- const category = FORBIDDEN_VALUE_CATEGORIES.get(key);
262
- if (category !== void 0) diagnostics.add(`source.forbidden-${category}`);
263
- validateValue(ownData(value, key), kind, policy, diagnostics);
264
- }
265
- }
266
- function scopeRank(scope) {
267
- return scope === "layer" ? 0 : scope === "namespace" ? 1 : 2;
321
+ if (value === null || typeof value !== "object") return;
322
+ if (Array.isArray(value)) {
323
+ for (const item of value) validateValue(item, kind, policy, diagnostics);
324
+ return;
325
+ }
326
+ if (!plainObject(value)) return;
327
+ if (value.$buro === ATTACHMENT_TAG) {
328
+ const capability = ownData(value, "capability");
329
+ const allowed = Object.hasOwn(policy.allowedCapabilities, kind) ? policy.allowedCapabilities[kind] : void 0;
330
+ if (typeof capability !== "string" || !allowed?.includes(capability)) diagnostics.add("source.forbidden-capability");
331
+ }
332
+ for (const key of Object.keys(value)) {
333
+ const category = (0, _burojs_core_internal.blacklistedKeyCategory)(key);
334
+ if (category !== void 0) diagnostics.add(`source.forbidden-${category}`);
335
+ validateValue(ownData(value, key), kind, policy, diagnostics);
336
+ }
268
337
  }
269
338
  function transformOperation(value) {
270
- if (!plainObject(value)) return void 0;
271
- const operation = ownData(value, "op");
272
- return operation === "merge" || operation === "replace" || operation === "disable" || operation === "delete" ? operation : void 0;
339
+ if (!plainObject(value)) return void 0;
340
+ const operation = ownData(value, "op");
341
+ return operation === "merge" || operation === "replace" || operation === "disable" || operation === "delete" ? operation : void 0;
273
342
  }
274
343
  function validateSelectorRefs(value, defaultNamespace, policy, diagnostics) {
275
- if (value === void 0) return;
276
- if (!Array.isArray(value)) {
277
- diagnostics.add("source.invalid-transform");
278
- return;
279
- }
280
- for (const ref of value) {
281
- const namespace = namespaceFor(ref, defaultNamespace, diagnostics);
282
- if (namespace !== void 0) allowedNamespace(namespace, policy, diagnostics);
283
- }
344
+ if (value === void 0) return;
345
+ if (!Array.isArray(value)) {
346
+ diagnostics.add("source.invalid-transform");
347
+ return;
348
+ }
349
+ for (const ref of value) {
350
+ const namespace = namespaceFor(ref, defaultNamespace, diagnostics);
351
+ if (namespace !== void 0) allowedNamespace(namespace, policy, diagnostics);
352
+ }
284
353
  }
285
354
  function validateTransforms(value, defaultNamespace, policy, diagnostics) {
286
- if (value === void 0) return;
287
- if (!Array.isArray(value)) {
288
- diagnostics.add("source.invalid-transform");
289
- return;
290
- }
291
- for (const transform of value) {
292
- if (!plainObject(transform)) {
293
- diagnostics.add("source.invalid-transform");
294
- continue;
295
- }
296
- unknownKeys(transform, TRANSFORM_KEYS, diagnostics);
297
- const scope = ownData(transform, "scope") ?? "layer";
298
- if (scope !== "layer" && scope !== "application") diagnostics.add("source.invalid-transform");
299
- else if (scopeRank(scope) > scopeRank(policy.maxTransformScope))
300
- diagnostics.add("source.forbidden-transform-scope");
301
- const select = ownData(transform, "select");
302
- if (!plainObject(select)) diagnostics.add("source.invalid-transform");
303
- else {
304
- unknownKeys(select, SELECTOR_KEYS, diagnostics);
305
- const selectedKinds = ownData(select, "kinds");
306
- if (selectedKinds !== void 0) {
307
- if (!Array.isArray(selectedKinds)) diagnostics.add("source.invalid-transform");
308
- else
309
- for (const kind of selectedKinds) {
310
- if (typeof kind !== "string") diagnostics.add("source.invalid-transform");
311
- else if (!policy.allowedKinds.includes(kind)) diagnostics.add("source.forbidden-kind");
312
- }
313
- }
314
- const selectedNamespaces = ownData(select, "namespaces");
315
- if (selectedNamespaces !== void 0) {
316
- if (!Array.isArray(selectedNamespaces)) diagnostics.add("source.invalid-transform");
317
- else
318
- for (const namespace of selectedNamespaces)
319
- allowedNamespace(namespace, policy, diagnostics);
320
- }
321
- validateSelectorRefs(ownData(select, "refs"), defaultNamespace, policy, diagnostics);
322
- validateSelectorRefs(ownData(select, "excludeRefs"), defaultNamespace, policy, diagnostics);
323
- }
324
- const operation = ownData(transform, "operation");
325
- const op = transformOperation(operation);
326
- const rawOp = plainObject(operation) ? ownData(operation, "op") : void 0;
327
- if (typeof rawOp === "string" && op === void 0)
328
- diagnostics.add("source.forbidden-transform-op");
329
- else if (op === void 0) diagnostics.add("source.invalid-transform");
330
- else {
331
- if (!policy.allowedTransformOps.includes(op))
332
- diagnostics.add("source.forbidden-transform-op");
333
- if (plainObject(operation)) {
334
- unknownKeys(operation, OPERATION_KEYS[op], diagnostics);
335
- if (hasData(operation, "value"))
336
- validateValue(ownData(operation, "value"), "", policy, diagnostics);
337
- }
338
- }
339
- }
355
+ if (value === void 0) return;
356
+ if (!Array.isArray(value)) {
357
+ diagnostics.add("source.invalid-transform");
358
+ return;
359
+ }
360
+ for (const transform of value) {
361
+ if (!plainObject(transform)) {
362
+ diagnostics.add("source.invalid-transform");
363
+ continue;
364
+ }
365
+ unknownKeys(transform, TRANSFORM_KEYS, diagnostics);
366
+ const scope = ownData(transform, "scope") ?? "layer";
367
+ if (scope !== "layer" && scope !== "application") diagnostics.add("source.invalid-transform");
368
+ else if ((0, _burojs_core_internal.scopeRank)(scope) > (0, _burojs_core_internal.scopeRank)(policy.grants.maxTransformScope)) diagnostics.add("source.forbidden-transform-scope");
369
+ const select = ownData(transform, "select");
370
+ if (!plainObject(select)) diagnostics.add("source.invalid-transform");
371
+ else {
372
+ unknownKeys(select, SELECTOR_KEYS, diagnostics);
373
+ const selectedKinds = ownData(select, "kinds");
374
+ if (selectedKinds !== void 0) {
375
+ if (!Array.isArray(selectedKinds)) diagnostics.add("source.invalid-transform");
376
+ else for (const kind of selectedKinds) if (typeof kind !== "string") diagnostics.add("source.invalid-transform");
377
+ else if (!policy.grants.allowedKinds.includes(kind)) diagnostics.add("source.forbidden-kind");
378
+ }
379
+ const selectedNamespaces = ownData(select, "namespaces");
380
+ if (selectedNamespaces !== void 0) {
381
+ if (!Array.isArray(selectedNamespaces)) diagnostics.add("source.invalid-transform");
382
+ else for (const namespace of selectedNamespaces) allowedNamespace(namespace, policy, diagnostics);
383
+ }
384
+ validateSelectorRefs(ownData(select, "refs"), defaultNamespace, policy, diagnostics);
385
+ validateSelectorRefs(ownData(select, "excludeRefs"), defaultNamespace, policy, diagnostics);
386
+ }
387
+ const operation = ownData(transform, "operation");
388
+ const op = transformOperation(operation);
389
+ if (typeof (plainObject(operation) ? ownData(operation, "op") : void 0) === "string" && op === void 0) diagnostics.add("source.forbidden-transform-op");
390
+ else if (op === void 0) diagnostics.add("source.invalid-transform");
391
+ else {
392
+ if (!policy.grants.allowedTransformOps.includes(op)) diagnostics.add("source.forbidden-transform-op");
393
+ if (plainObject(operation)) {
394
+ unknownKeys(operation, OPERATION_KEYS[op], diagnostics);
395
+ if (hasData(operation, "value")) validateValue(ownData(operation, "value"), "", policy, diagnostics);
396
+ }
397
+ }
398
+ }
340
399
  }
341
400
  function validateLayerValue(value, policy) {
342
- if (!validPolicy(policy)) {
343
- return {
344
- ok: false,
345
- diagnostics: Object.freeze([
346
- Object.freeze({
347
- code: "source.invalid-policy",
348
- message: messageFor("source.invalid-policy")
349
- })
350
- ])
351
- };
352
- }
353
- const diagnostics = new Diagnostics(policy.limits.maxDiagnostics);
354
- const cloned = clonePortable(value, policy.limits, diagnostics);
355
- if (diagnostics.values.length > 0) return failure(diagnostics);
356
- if (!plainObject(cloned)) {
357
- diagnostics.add("source.invalid-layer");
358
- return failure(diagnostics);
359
- }
360
- unknownKeys(cloned, LAYER_KEYS, diagnostics);
361
- const id = ownData(cloned, "id");
362
- const priority = ownData(cloned, "priority");
363
- const declarations = ownData(cloned, "declarations");
364
- if (typeof id !== "string" || !Number.isSafeInteger(priority) || !Array.isArray(declarations)) {
365
- diagnostics.add("source.invalid-layer");
366
- return failure(diagnostics);
367
- }
368
- if (declarations.length > policy.limits.maxDeclarations)
369
- diagnostics.add("source.declaration-limit");
370
- const rawDefaultNamespace = ownData(cloned, "defaultNamespace");
371
- const defaultNamespace = rawDefaultNamespace === void 0 ? "default" : rawDefaultNamespace;
372
- if (typeof defaultNamespace !== "string") diagnostics.add("source.invalid-layer");
373
- else allowedNamespace(defaultNamespace, policy, diagnostics);
374
- for (const declaration of declarations) {
375
- if (!plainObject(declaration)) {
376
- diagnostics.add("source.invalid-declaration");
377
- continue;
378
- }
379
- unknownKeys(declaration, DECLARATION_KEYS, diagnostics);
380
- const kind = ownData(declaration, "kind");
381
- if (typeof kind !== "string") diagnostics.add("source.invalid-declaration");
382
- else if (!policy.allowedKinds.includes(kind)) diagnostics.add("source.forbidden-kind");
383
- const namespace = namespaceFor(
384
- ownData(declaration, "ref"),
385
- typeof defaultNamespace === "string" ? defaultNamespace : "default",
386
- diagnostics
387
- );
388
- if (namespace !== void 0) allowedNamespace(namespace, policy, diagnostics);
389
- if (!Number.isSafeInteger(ownData(declaration, "schemaVersion")) || !hasData(declaration, "value")) {
390
- diagnostics.add("source.invalid-declaration");
391
- }
392
- if (typeof kind === "string" && hasData(declaration, "value")) {
393
- validateValue(ownData(declaration, "value"), kind, policy, diagnostics);
394
- }
395
- }
396
- validateTransforms(
397
- ownData(cloned, "transforms"),
398
- typeof defaultNamespace === "string" ? defaultNamespace : "default",
399
- policy,
400
- diagnostics
401
- );
402
- if (diagnostics.values.length > 0) return failure(diagnostics);
403
- return { ok: true, value: cloned };
401
+ const invalidPolicy = policyDiagnostic(policy);
402
+ if (invalidPolicy !== void 0) return {
403
+ ok: false,
404
+ diagnostics: Object.freeze([invalidPolicy])
405
+ };
406
+ const diagnostics = new Diagnostics(policy.limits.maxDiagnostics);
407
+ const cloned = clonePortable(value, policy.limits, diagnostics);
408
+ if (diagnostics.values.length > 0) return failure(diagnostics);
409
+ if (!plainObject(cloned)) {
410
+ diagnostics.add("source.invalid-layer");
411
+ return failure(diagnostics);
412
+ }
413
+ unknownKeys(cloned, LAYER_KEYS, diagnostics);
414
+ const id = ownData(cloned, "id");
415
+ const priority = ownData(cloned, "priority");
416
+ const declarations = ownData(cloned, "declarations");
417
+ if (typeof id !== "string" || !Number.isSafeInteger(priority) || !Array.isArray(declarations)) {
418
+ diagnostics.add("source.invalid-layer");
419
+ return failure(diagnostics);
420
+ }
421
+ if (declarations.length > policy.limits.maxDeclarations) diagnostics.add("source.declaration-limit");
422
+ const rawDefaultNamespace = ownData(cloned, "defaultNamespace");
423
+ const defaultNamespace = rawDefaultNamespace === void 0 ? "default" : rawDefaultNamespace;
424
+ if (typeof defaultNamespace !== "string") diagnostics.add("source.invalid-layer");
425
+ else allowedNamespace(defaultNamespace, policy, diagnostics);
426
+ for (const declaration of declarations) {
427
+ if (!plainObject(declaration)) {
428
+ diagnostics.add("source.invalid-declaration");
429
+ continue;
430
+ }
431
+ unknownKeys(declaration, DECLARATION_KEYS, diagnostics);
432
+ const kind = ownData(declaration, "kind");
433
+ if (typeof kind !== "string") diagnostics.add("source.invalid-declaration");
434
+ else if (!policy.grants.allowedKinds.includes(kind)) diagnostics.add("source.forbidden-kind");
435
+ const namespace = namespaceFor(ownData(declaration, "ref"), typeof defaultNamespace === "string" ? defaultNamespace : "default", diagnostics);
436
+ if (namespace !== void 0) allowedNamespace(namespace, policy, diagnostics);
437
+ if (!Number.isSafeInteger(ownData(declaration, "schemaVersion")) || !hasData(declaration, "value")) diagnostics.add("source.invalid-declaration");
438
+ if (typeof kind === "string" && hasData(declaration, "value")) validateValue(ownData(declaration, "value"), kind, policy, diagnostics);
439
+ }
440
+ validateTransforms(ownData(cloned, "transforms"), typeof defaultNamespace === "string" ? defaultNamespace : "default", policy, diagnostics);
441
+ if (diagnostics.values.length > 0) return failure(diagnostics);
442
+ return {
443
+ ok: true,
444
+ value: (0, _burojs_core_internal.createParsedLayer)(cloned, (0, _burojs_core.normalizeLayerGrants)(policy.grants))
445
+ };
404
446
  }
405
447
  function byteLength(value) {
406
- return new TextEncoder().encode(value).byteLength;
448
+ return new TextEncoder().encode(value).byteLength;
407
449
  }
408
450
  function parseSource(source, policy, format) {
409
- if (!validPolicy(policy)) return validateLayerValue(null, policy);
410
- const diagnostics = new Diagnostics(policy.limits.maxDiagnostics);
411
- if (byteLength(source) > policy.limits.maxBytes) {
412
- diagnostics.add("source.byte-limit");
413
- return failure(diagnostics);
414
- }
415
- try {
416
- if (format === "json") return validateLayerValue(JSON.parse(source), policy);
417
- const document = yaml.parseDocument(source, { prettyErrors: false, strict: true, uniqueKeys: true });
418
- if (document.errors.length > 0) {
419
- diagnostics.add("source.invalid-yaml");
420
- return failure(diagnostics);
421
- }
422
- return validateLayerValue(document.toJS({ maxAliasCount: 0 }), policy);
423
- } catch {
424
- diagnostics.add(format === "json" ? "source.invalid-json" : "source.invalid-yaml");
425
- return failure(diagnostics);
426
- }
451
+ if (policyDiagnostic(policy) !== void 0) return validateLayerValue(null, policy);
452
+ const diagnostics = new Diagnostics(policy.limits.maxDiagnostics);
453
+ if (byteLength(source) > policy.limits.maxBytes) {
454
+ diagnostics.add("source.byte-limit");
455
+ return failure(diagnostics);
456
+ }
457
+ try {
458
+ if (format === "json") return validateLayerValue(JSON.parse(source), policy);
459
+ const document = (0, yaml.parseDocument)(source, {
460
+ prettyErrors: false,
461
+ strict: true,
462
+ uniqueKeys: true
463
+ });
464
+ if (document.errors.length > 0) {
465
+ diagnostics.add("source.invalid-yaml");
466
+ return failure(diagnostics);
467
+ }
468
+ return validateLayerValue(document.toJS({ maxAliasCount: 0 }), policy);
469
+ } catch {
470
+ diagnostics.add(format === "json" ? "source.invalid-json" : "source.invalid-yaml");
471
+ return failure(diagnostics);
472
+ }
427
473
  }
428
474
  function parseJsonLayer(source, policy) {
429
- return parseSource(source, policy, "json");
475
+ return parseSource(source, policy, "json");
430
476
  }
431
477
  function parseYamlLayer(source, policy) {
432
- return parseSource(source, policy, "yaml");
433
- }
434
-
435
- // src/adapter.ts
436
- function diagnostic(code, message) {
437
- return Object.freeze({ code, message });
438
- }
439
- function diagnosticLimit(policy) {
440
- const candidate = policy?.limits?.maxDiagnostics;
441
- return typeof candidate === "number" && Number.isSafeInteger(candidate) && candidate > 0 ? candidate : 1;
442
- }
443
- function frozenPolicy(policy) {
444
- const capabilities = /* @__PURE__ */ Object.create(null);
445
- for (const [kind, values] of Object.entries(policy.allowedCapabilities)) {
446
- capabilities[kind] = Object.freeze([...values]);
447
- }
448
- const limits = Object.freeze({ ...policy.limits });
449
- return Object.freeze({
450
- allowedKinds: Object.freeze([...policy.allowedKinds]),
451
- allowedNamespaces: Object.freeze([...policy.allowedNamespaces]),
452
- allowedTransformOps: Object.freeze([...policy.allowedTransformOps]),
453
- maxTransformScope: policy.maxTransformScope,
454
- allowedCapabilities: Object.freeze(capabilities),
455
- limits
456
- });
478
+ return parseSource(source, policy, "yaml");
457
479
  }
458
- function copyDiagnostic(value) {
459
- if (value !== null && typeof value === "object" && typeof value.code === "string" && typeof value.message === "string") {
460
- return diagnostic(
461
- value.code,
462
- value.message
463
- );
464
- }
465
- return diagnostic(
466
- "source.invalid-adapter-diagnostic",
467
- "The source adapter returned an invalid diagnostic."
468
- );
469
- }
470
- function adaptRawConfig(input, adapter, policy) {
471
- const policyValidation = validateLayerValue(null, policy);
472
- if (!policyValidation.ok && policyValidation.diagnostics.some((entry) => entry.code === "source.invalid-policy")) {
473
- return policyValidation;
474
- }
475
- const ownedPolicy = frozenPolicy(policy);
476
- const diagnostics = [];
477
- const maxDiagnostics = diagnosticLimit(ownedPolicy);
478
- let limited = false;
479
- const add = (value) => {
480
- if (limited) return;
481
- if (diagnostics.length + 1 < maxDiagnostics) {
482
- diagnostics.push(value);
483
- return;
484
- }
485
- limited = true;
486
- diagnostics.push(
487
- diagnostic("source.diagnostic-limit", "Additional source diagnostics were omitted.")
488
- );
489
- };
490
- const context = Object.freeze({
491
- policy: ownedPolicy,
492
- limits: ownedPolicy.limits,
493
- diagnose(code, message) {
494
- add(copyDiagnostic({ code, message }));
495
- }
496
- });
497
- const result = adapter.adapt(input, context);
498
- if (result === null || typeof result !== "object") {
499
- add(
500
- diagnostic("source.invalid-adapter-result", "The source adapter returned an invalid result.")
501
- );
502
- } else if (result.diagnostics !== void 0) {
503
- if (!Array.isArray(result.diagnostics)) {
504
- add(
505
- diagnostic(
506
- "source.invalid-adapter-result",
507
- "The source adapter returned an invalid result."
508
- )
509
- );
510
- } else {
511
- for (const entry of result.diagnostics) add(copyDiagnostic(entry));
512
- }
513
- }
514
- if (diagnostics.length > 0) {
515
- return { ok: false, diagnostics: Object.freeze(diagnostics) };
516
- }
517
- if (result === null || typeof result !== "object" || result.layer === void 0) {
518
- return {
519
- ok: false,
520
- diagnostics: Object.freeze([
521
- diagnostic(
522
- "source.invalid-adapter-result",
523
- "The source adapter did not return a declaration layer."
524
- )
525
- ])
526
- };
527
- }
528
- return validateLayerValue(result.layer, ownedPolicy);
529
- }
530
- var parseResultAdapter = Object.freeze({
531
- adapt(input) {
532
- return input.ok ? { layer: input.value } : { diagnostics: input.diagnostics };
533
- }
480
+ //#endregion
481
+ //#region src/runtime-source.ts
482
+ const INVALID_FORMAT = Object.freeze({
483
+ code: "source.invalid-format",
484
+ message: "The source format must be json or yaml."
534
485
  });
535
-
536
- // src/runtime-source.ts
537
- function adaptationFailure(diagnostics) {
538
- return Object.freeze({
539
- status: "rejected",
540
- diagnostics: Object.freeze(
541
- diagnostics.map((entry) => Object.freeze({ code: entry.code, message: entry.message }))
542
- )
543
- });
486
+ function rejection(diagnostics) {
487
+ return Object.freeze({
488
+ status: "rejected",
489
+ diagnostics: Object.freeze(diagnostics.map((entry) => Object.freeze({
490
+ code: entry.code,
491
+ message: entry.message
492
+ })))
493
+ });
544
494
  }
545
- function createConfigRuntimeSource(id, fetchRaw, adapter, policy) {
546
- return Object.freeze({
547
- id,
548
- async load(context) {
549
- const raw = await fetchRaw(context);
550
- const adapted = adaptRawConfig(raw, adapter, policy);
551
- if (!adapted.ok) return adaptationFailure(adapted.diagnostics);
552
- return adapted.value;
553
- }
554
- });
495
+ function createConfigRuntimeSource(id, options) {
496
+ const policy = ownedPolicy(options.policy);
497
+ const { load } = options;
498
+ const source = Object.freeze({
499
+ id,
500
+ async load(context) {
501
+ const raw = await load(context);
502
+ const parsed = raw.format === "json" ? parseJsonLayer(raw.text, policy) : raw.format === "yaml" ? parseYamlLayer(raw.text, policy) : void 0;
503
+ if (parsed === void 0) return rejection([INVALID_FORMAT]);
504
+ return parsed.ok ? parsed.value : rejection(parsed.diagnostics);
505
+ }
506
+ });
507
+ return Object.freeze({
508
+ source,
509
+ trust: policy.grants
510
+ });
555
511
  }
556
-
557
- exports.adaptRawConfig = adaptRawConfig;
512
+ //#endregion
513
+ exports.MAX_SOURCE_DEPTH = MAX_SOURCE_DEPTH;
514
+ exports.SCREEN_SOURCE_LIMITS = SCREEN_SOURCE_LIMITS;
558
515
  exports.createConfigRuntimeSource = createConfigRuntimeSource;
559
516
  exports.parseJsonLayer = parseJsonLayer;
560
- exports.parseResultAdapter = parseResultAdapter;
561
517
  exports.parseYamlLayer = parseYamlLayer;
562
- //# sourceMappingURL=index.cjs.map
518
+
563
519
  //# sourceMappingURL=index.cjs.map