@peac/policy-kit 0.9.18
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/README.md +23 -0
- package/dist/compiler.d.ts +111 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +304 -0
- package/dist/compiler.js.map +1 -0
- package/dist/evaluate.d.ts +84 -0
- package/dist/evaluate.d.ts.map +1 -0
- package/dist/evaluate.js +258 -0
- package/dist/evaluate.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +69 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +80 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +245 -0
- package/dist/loader.js.map +1 -0
- package/dist/types.d.ts +276 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +88 -0
- package/dist/types.js.map +1 -0
- package/package.json +41 -0
package/dist/evaluate.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* PEAC Policy Kit Evaluation
|
|
4
|
+
*
|
|
5
|
+
* Deterministic policy evaluation for CAL semantics.
|
|
6
|
+
* First-match-wins rule semantics.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.evaluate = evaluate;
|
|
12
|
+
exports.explainMatches = explainMatches;
|
|
13
|
+
exports.findEffectiveRule = findEffectiveRule;
|
|
14
|
+
exports.isAllowed = isAllowed;
|
|
15
|
+
exports.isDenied = isDenied;
|
|
16
|
+
exports.requiresReview = requiresReview;
|
|
17
|
+
exports.evaluateBatch = evaluateBatch;
|
|
18
|
+
/**
|
|
19
|
+
* Check if a value matches a single-or-array pattern
|
|
20
|
+
*
|
|
21
|
+
* @param value - Value to check
|
|
22
|
+
* @param pattern - Single value or array of values
|
|
23
|
+
* @returns true if value matches pattern
|
|
24
|
+
*/
|
|
25
|
+
function matchesSingleOrArray(value, pattern) {
|
|
26
|
+
// If no pattern specified, match anything
|
|
27
|
+
if (pattern === undefined) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
// If no value and pattern exists, no match
|
|
31
|
+
if (value === undefined) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
// Check against array or single value
|
|
35
|
+
if (Array.isArray(pattern)) {
|
|
36
|
+
return pattern.includes(value);
|
|
37
|
+
}
|
|
38
|
+
return value === pattern;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Check if a subject ID matches a pattern
|
|
42
|
+
*
|
|
43
|
+
* Supports:
|
|
44
|
+
* - Exact match: "user:abc123"
|
|
45
|
+
* - Prefix match with wildcard: "user:*" matches "user:abc123"
|
|
46
|
+
*
|
|
47
|
+
* @param id - Subject ID to check
|
|
48
|
+
* @param pattern - Pattern to match against
|
|
49
|
+
* @returns true if ID matches pattern
|
|
50
|
+
*/
|
|
51
|
+
function matchesIdPattern(id, pattern) {
|
|
52
|
+
// No pattern = match anything
|
|
53
|
+
if (pattern === undefined) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
// No ID but pattern exists = no match
|
|
57
|
+
if (id === undefined) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
// Wildcard prefix match
|
|
61
|
+
if (pattern.endsWith('*')) {
|
|
62
|
+
const prefix = pattern.slice(0, -1);
|
|
63
|
+
return id.startsWith(prefix);
|
|
64
|
+
}
|
|
65
|
+
// Exact match
|
|
66
|
+
return id === pattern;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Check if subject labels contain all required labels
|
|
70
|
+
*
|
|
71
|
+
* @param subjectLabels - Labels on the subject
|
|
72
|
+
* @param requiredLabels - Labels required by the rule
|
|
73
|
+
* @returns true if subject has all required labels
|
|
74
|
+
*/
|
|
75
|
+
function hasAllLabels(subjectLabels, requiredLabels) {
|
|
76
|
+
// No required labels = match
|
|
77
|
+
if (requiredLabels === undefined || requiredLabels.length === 0) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
// Required labels but no subject labels = no match
|
|
81
|
+
if (subjectLabels === undefined || subjectLabels.length === 0) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
// Check all required labels are present
|
|
85
|
+
return requiredLabels.every((label) => subjectLabels.includes(label));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Check if a subject matches a subject matcher
|
|
89
|
+
*
|
|
90
|
+
* @param subject - Subject from evaluation context
|
|
91
|
+
* @param matcher - Subject matcher from rule
|
|
92
|
+
* @returns true if subject matches all criteria
|
|
93
|
+
*/
|
|
94
|
+
function matchesSubject(subject, matcher) {
|
|
95
|
+
// No matcher = match any subject
|
|
96
|
+
if (matcher === undefined) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
// Check type
|
|
100
|
+
if (!matchesSingleOrArray(subject?.type, matcher.type)) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
// Check labels (must have ALL required labels)
|
|
104
|
+
if (!hasAllLabels(subject?.labels, matcher.labels)) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
// Check ID pattern
|
|
108
|
+
if (!matchesIdPattern(subject?.id, matcher.id)) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Check if a rule matches the evaluation context
|
|
115
|
+
*
|
|
116
|
+
* All criteria must match (AND logic).
|
|
117
|
+
*
|
|
118
|
+
* @param rule - Policy rule to check
|
|
119
|
+
* @param context - Evaluation context
|
|
120
|
+
* @returns true if rule matches context
|
|
121
|
+
*/
|
|
122
|
+
function ruleMatches(rule, context) {
|
|
123
|
+
// Check subject
|
|
124
|
+
if (!matchesSubject(context.subject, rule.subject)) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
// Check purpose
|
|
128
|
+
if (!matchesSingleOrArray(context.purpose, rule.purpose)) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
// Check licensing mode
|
|
132
|
+
if (!matchesSingleOrArray(context.licensing_mode, rule.licensing_mode)) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Evaluate a policy against a context
|
|
139
|
+
*
|
|
140
|
+
* Uses first-match-wins semantics:
|
|
141
|
+
* - Rules are evaluated in order
|
|
142
|
+
* - First matching rule determines the decision
|
|
143
|
+
* - If no rule matches, defaults are applied
|
|
144
|
+
*
|
|
145
|
+
* @param policy - Policy document
|
|
146
|
+
* @param context - Evaluation context
|
|
147
|
+
* @returns Evaluation result
|
|
148
|
+
*/
|
|
149
|
+
function evaluate(policy, context) {
|
|
150
|
+
// Find first matching rule
|
|
151
|
+
for (const rule of policy.rules) {
|
|
152
|
+
if (ruleMatches(rule, context)) {
|
|
153
|
+
return {
|
|
154
|
+
decision: rule.decision,
|
|
155
|
+
matched_rule: rule.name,
|
|
156
|
+
reason: rule.reason,
|
|
157
|
+
is_default: false,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// No rule matched, apply defaults
|
|
162
|
+
return {
|
|
163
|
+
decision: policy.defaults.decision,
|
|
164
|
+
reason: policy.defaults.reason,
|
|
165
|
+
is_default: true,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Explain which rules could potentially match a context
|
|
170
|
+
*
|
|
171
|
+
* Useful for debugging and policy analysis.
|
|
172
|
+
* Returns all rules that would match if evaluated, in order.
|
|
173
|
+
*
|
|
174
|
+
* @param policy - Policy document
|
|
175
|
+
* @param context - Evaluation context
|
|
176
|
+
* @returns Array of rule names that match, or 'default' if none
|
|
177
|
+
*/
|
|
178
|
+
function explainMatches(policy, context) {
|
|
179
|
+
const matches = [];
|
|
180
|
+
for (const rule of policy.rules) {
|
|
181
|
+
if (ruleMatches(rule, context)) {
|
|
182
|
+
matches.push(rule.name);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
if (matches.length === 0) {
|
|
186
|
+
matches.push('[default]');
|
|
187
|
+
}
|
|
188
|
+
return matches;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Find the effective rule for a context
|
|
192
|
+
*
|
|
193
|
+
* Same as evaluate() but returns the full rule object.
|
|
194
|
+
*
|
|
195
|
+
* @param policy - Policy document
|
|
196
|
+
* @param context - Evaluation context
|
|
197
|
+
* @returns Matched rule or undefined if default applies
|
|
198
|
+
*/
|
|
199
|
+
function findEffectiveRule(policy, context) {
|
|
200
|
+
for (const rule of policy.rules) {
|
|
201
|
+
if (ruleMatches(rule, context)) {
|
|
202
|
+
return rule;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Check if a policy would allow a given context
|
|
209
|
+
*
|
|
210
|
+
* Convenience helper for common allow/deny checks.
|
|
211
|
+
*
|
|
212
|
+
* @param policy - Policy document
|
|
213
|
+
* @param context - Evaluation context
|
|
214
|
+
* @returns true if decision is 'allow'
|
|
215
|
+
*/
|
|
216
|
+
function isAllowed(policy, context) {
|
|
217
|
+
const result = evaluate(policy, context);
|
|
218
|
+
return result.decision === 'allow';
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Check if a policy would deny a given context
|
|
222
|
+
*
|
|
223
|
+
* Convenience helper for common allow/deny checks.
|
|
224
|
+
*
|
|
225
|
+
* @param policy - Policy document
|
|
226
|
+
* @param context - Evaluation context
|
|
227
|
+
* @returns true if decision is 'deny'
|
|
228
|
+
*/
|
|
229
|
+
function isDenied(policy, context) {
|
|
230
|
+
const result = evaluate(policy, context);
|
|
231
|
+
return result.decision === 'deny';
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Check if a policy requires review for a given context
|
|
235
|
+
*
|
|
236
|
+
* Convenience helper for review checks.
|
|
237
|
+
*
|
|
238
|
+
* @param policy - Policy document
|
|
239
|
+
* @param context - Evaluation context
|
|
240
|
+
* @returns true if decision is 'review'
|
|
241
|
+
*/
|
|
242
|
+
function requiresReview(policy, context) {
|
|
243
|
+
const result = evaluate(policy, context);
|
|
244
|
+
return result.decision === 'review';
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Batch evaluate multiple contexts against a policy
|
|
248
|
+
*
|
|
249
|
+
* Useful for testing or bulk authorization checks.
|
|
250
|
+
*
|
|
251
|
+
* @param policy - Policy document
|
|
252
|
+
* @param contexts - Array of evaluation contexts
|
|
253
|
+
* @returns Array of evaluation results (same order as contexts)
|
|
254
|
+
*/
|
|
255
|
+
function evaluateBatch(policy, contexts) {
|
|
256
|
+
return contexts.map((context) => evaluate(policy, context));
|
|
257
|
+
}
|
|
258
|
+
//# sourceMappingURL=evaluate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evaluate.js","sourceRoot":"","sources":["../src/evaluate.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;AA0KH,4BAmBC;AAYD,wCAcC;AAWD,8CAUC;AAWD,8BAGC;AAWD,4BAGC;AAWD,wCAGC;AAWD,sCAKC;AAzRD;;;;;;GAMG;AACH,SAAS,oBAAoB,CAAI,KAAoB,EAAE,OAA4B;IACjF,0CAA0C;IAC1C,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,2CAA2C;IAC3C,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,sCAAsC;IACtC,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,OAAO,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,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,EAAE,OAA0B;IACzE,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC,QAAQ,KAAK,MAAM,CAAC;AACpC,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,cAAc,CAAC,MAAsB,EAAE,OAA0B;IAC/E,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC,QAAQ,KAAK,QAAQ,CAAC;AACtC,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,aAAa,CAC3B,MAAsB,EACtB,QAA6B;IAE7B,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAC9D,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PEAC Policy Kit
|
|
3
|
+
*
|
|
4
|
+
* Deterministic policy evaluation for Control Abstraction Layer (CAL) semantics.
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - File-based policy format (YAML or JSON)
|
|
8
|
+
* - First-match-wins rule semantics
|
|
9
|
+
* - Subject matching by type, labels, and ID patterns
|
|
10
|
+
* - Purpose and licensing mode matching
|
|
11
|
+
* - No scripting, no dynamic code
|
|
12
|
+
* - Deterministic, auditable, side-effect free
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { loadPolicy, evaluate } from '@peac/policy-kit';
|
|
17
|
+
*
|
|
18
|
+
* const policy = loadPolicy('peac-policy.yaml');
|
|
19
|
+
*
|
|
20
|
+
* const result = evaluate(policy, {
|
|
21
|
+
* subject: { type: 'human', labels: ['subscribed'] },
|
|
22
|
+
* purpose: 'crawl',
|
|
23
|
+
* licensing_mode: 'subscription',
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* console.log(result.decision); // 'allow' | 'deny' | 'review'
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @packageDocumentation
|
|
30
|
+
*/
|
|
31
|
+
export { POLICY_VERSION, type SubjectType, type ControlPurpose, type ControlLicensingMode, type ControlDecision, type SubjectMatcher, type PolicyRule, type PolicyDefaults, type PolicyDocument, type EvaluationContext, type EvaluationResult, SubjectMatcherSchema, PolicyRuleSchema, PolicyDefaultsSchema, PolicyDocumentSchema, } from './types';
|
|
32
|
+
export { loadPolicy, parsePolicy, validatePolicy, policyFileExists, createExamplePolicy, serializePolicyYaml, serializePolicyJson, PolicyLoadError, PolicyValidationError, } from './loader';
|
|
33
|
+
export { evaluate, explainMatches, findEffectiveRule, isAllowed, isDenied, requiresReview, evaluateBatch, } from './evaluate';
|
|
34
|
+
export { PEAC_PROTOCOL_VERSION, compilePeacTxt, compileRobotsSnippet, compileAiprefTemplates, renderPolicyMarkdown, type CompileOptions, type AiprefTemplate, } from './compiler';
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAGH,OAAO,EACL,cAAc,EACd,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EAErB,oBAAoB,EACpB,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,UAAU,EACV,WAAW,EACX,cAAc,EACd,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,EACnB,eAAe,EACf,qBAAqB,GACtB,MAAM,UAAU,CAAC;AAGlB,OAAO,EACL,QAAQ,EACR,cAAc,EACd,iBAAiB,EACjB,SAAS,EACT,QAAQ,EACR,cAAc,EACd,aAAa,GACd,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,qBAAqB,EACrB,cAAc,EACd,oBAAoB,EACpB,sBAAsB,EACtB,oBAAoB,EACpB,KAAK,cAAc,EACnB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* PEAC Policy Kit
|
|
4
|
+
*
|
|
5
|
+
* Deterministic policy evaluation for Control Abstraction Layer (CAL) semantics.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - File-based policy format (YAML or JSON)
|
|
9
|
+
* - First-match-wins rule semantics
|
|
10
|
+
* - Subject matching by type, labels, and ID patterns
|
|
11
|
+
* - Purpose and licensing mode matching
|
|
12
|
+
* - No scripting, no dynamic code
|
|
13
|
+
* - Deterministic, auditable, side-effect free
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { loadPolicy, evaluate } from '@peac/policy-kit';
|
|
18
|
+
*
|
|
19
|
+
* const policy = loadPolicy('peac-policy.yaml');
|
|
20
|
+
*
|
|
21
|
+
* const result = evaluate(policy, {
|
|
22
|
+
* subject: { type: 'human', labels: ['subscribed'] },
|
|
23
|
+
* purpose: 'crawl',
|
|
24
|
+
* licensing_mode: 'subscription',
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* console.log(result.decision); // 'allow' | 'deny' | 'review'
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @packageDocumentation
|
|
31
|
+
*/
|
|
32
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
|
+
exports.renderPolicyMarkdown = exports.compileAiprefTemplates = exports.compileRobotsSnippet = exports.compilePeacTxt = exports.PEAC_PROTOCOL_VERSION = exports.evaluateBatch = exports.requiresReview = exports.isDenied = exports.isAllowed = exports.findEffectiveRule = exports.explainMatches = exports.evaluate = exports.PolicyValidationError = exports.PolicyLoadError = exports.serializePolicyJson = exports.serializePolicyYaml = exports.createExamplePolicy = exports.policyFileExists = exports.validatePolicy = exports.parsePolicy = exports.loadPolicy = exports.PolicyDocumentSchema = exports.PolicyDefaultsSchema = exports.PolicyRuleSchema = exports.SubjectMatcherSchema = exports.POLICY_VERSION = void 0;
|
|
34
|
+
// Types
|
|
35
|
+
var types_1 = require("./types");
|
|
36
|
+
Object.defineProperty(exports, "POLICY_VERSION", { enumerable: true, get: function () { return types_1.POLICY_VERSION; } });
|
|
37
|
+
// Schemas for advanced validation
|
|
38
|
+
Object.defineProperty(exports, "SubjectMatcherSchema", { enumerable: true, get: function () { return types_1.SubjectMatcherSchema; } });
|
|
39
|
+
Object.defineProperty(exports, "PolicyRuleSchema", { enumerable: true, get: function () { return types_1.PolicyRuleSchema; } });
|
|
40
|
+
Object.defineProperty(exports, "PolicyDefaultsSchema", { enumerable: true, get: function () { return types_1.PolicyDefaultsSchema; } });
|
|
41
|
+
Object.defineProperty(exports, "PolicyDocumentSchema", { enumerable: true, get: function () { return types_1.PolicyDocumentSchema; } });
|
|
42
|
+
// Loader
|
|
43
|
+
var loader_1 = require("./loader");
|
|
44
|
+
Object.defineProperty(exports, "loadPolicy", { enumerable: true, get: function () { return loader_1.loadPolicy; } });
|
|
45
|
+
Object.defineProperty(exports, "parsePolicy", { enumerable: true, get: function () { return loader_1.parsePolicy; } });
|
|
46
|
+
Object.defineProperty(exports, "validatePolicy", { enumerable: true, get: function () { return loader_1.validatePolicy; } });
|
|
47
|
+
Object.defineProperty(exports, "policyFileExists", { enumerable: true, get: function () { return loader_1.policyFileExists; } });
|
|
48
|
+
Object.defineProperty(exports, "createExamplePolicy", { enumerable: true, get: function () { return loader_1.createExamplePolicy; } });
|
|
49
|
+
Object.defineProperty(exports, "serializePolicyYaml", { enumerable: true, get: function () { return loader_1.serializePolicyYaml; } });
|
|
50
|
+
Object.defineProperty(exports, "serializePolicyJson", { enumerable: true, get: function () { return loader_1.serializePolicyJson; } });
|
|
51
|
+
Object.defineProperty(exports, "PolicyLoadError", { enumerable: true, get: function () { return loader_1.PolicyLoadError; } });
|
|
52
|
+
Object.defineProperty(exports, "PolicyValidationError", { enumerable: true, get: function () { return loader_1.PolicyValidationError; } });
|
|
53
|
+
// Evaluation
|
|
54
|
+
var evaluate_1 = require("./evaluate");
|
|
55
|
+
Object.defineProperty(exports, "evaluate", { enumerable: true, get: function () { return evaluate_1.evaluate; } });
|
|
56
|
+
Object.defineProperty(exports, "explainMatches", { enumerable: true, get: function () { return evaluate_1.explainMatches; } });
|
|
57
|
+
Object.defineProperty(exports, "findEffectiveRule", { enumerable: true, get: function () { return evaluate_1.findEffectiveRule; } });
|
|
58
|
+
Object.defineProperty(exports, "isAllowed", { enumerable: true, get: function () { return evaluate_1.isAllowed; } });
|
|
59
|
+
Object.defineProperty(exports, "isDenied", { enumerable: true, get: function () { return evaluate_1.isDenied; } });
|
|
60
|
+
Object.defineProperty(exports, "requiresReview", { enumerable: true, get: function () { return evaluate_1.requiresReview; } });
|
|
61
|
+
Object.defineProperty(exports, "evaluateBatch", { enumerable: true, get: function () { return evaluate_1.evaluateBatch; } });
|
|
62
|
+
// Compiler (artifact generation)
|
|
63
|
+
var compiler_1 = require("./compiler");
|
|
64
|
+
Object.defineProperty(exports, "PEAC_PROTOCOL_VERSION", { enumerable: true, get: function () { return compiler_1.PEAC_PROTOCOL_VERSION; } });
|
|
65
|
+
Object.defineProperty(exports, "compilePeacTxt", { enumerable: true, get: function () { return compiler_1.compilePeacTxt; } });
|
|
66
|
+
Object.defineProperty(exports, "compileRobotsSnippet", { enumerable: true, get: function () { return compiler_1.compileRobotsSnippet; } });
|
|
67
|
+
Object.defineProperty(exports, "compileAiprefTemplates", { enumerable: true, get: function () { return compiler_1.compileAiprefTemplates; } });
|
|
68
|
+
Object.defineProperty(exports, "renderPolicyMarkdown", { enumerable: true, get: function () { return compiler_1.renderPolicyMarkdown; } });
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;;;AAEH,QAAQ;AACR,iCAiBiB;AAhBf,uGAAA,cAAc,OAAA;AAWd,kCAAkC;AAClC,6GAAA,oBAAoB,OAAA;AACpB,yGAAA,gBAAgB,OAAA;AAChB,6GAAA,oBAAoB,OAAA;AACpB,6GAAA,oBAAoB,OAAA;AAGtB,SAAS;AACT,mCAUkB;AAThB,oGAAA,UAAU,OAAA;AACV,qGAAA,WAAW,OAAA;AACX,wGAAA,cAAc,OAAA;AACd,0GAAA,gBAAgB,OAAA;AAChB,6GAAA,mBAAmB,OAAA;AACnB,6GAAA,mBAAmB,OAAA;AACnB,6GAAA,mBAAmB,OAAA;AACnB,yGAAA,eAAe,OAAA;AACf,+GAAA,qBAAqB,OAAA;AAGvB,aAAa;AACb,uCAQoB;AAPlB,oGAAA,QAAQ,OAAA;AACR,0GAAA,cAAc,OAAA;AACd,6GAAA,iBAAiB,OAAA;AACjB,qGAAA,SAAS,OAAA;AACT,oGAAA,QAAQ,OAAA;AACR,0GAAA,cAAc,OAAA;AACd,yGAAA,aAAa,OAAA;AAGf,iCAAiC;AACjC,uCAQoB;AAPlB,iHAAA,qBAAqB,OAAA;AACrB,0GAAA,cAAc,OAAA;AACd,gHAAA,oBAAoB,OAAA;AACpB,kHAAA,sBAAsB,OAAA;AACtB,gHAAA,oBAAoB,OAAA"}
|
package/dist/loader.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PEAC Policy Kit Loader
|
|
3
|
+
*
|
|
4
|
+
* Loads and validates policy documents from YAML or JSON.
|
|
5
|
+
* No network calls - file system only.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import { ZodError } from 'zod';
|
|
10
|
+
import { PolicyDocument } from './types';
|
|
11
|
+
/**
|
|
12
|
+
* Policy load error
|
|
13
|
+
*/
|
|
14
|
+
export declare class PolicyLoadError extends Error {
|
|
15
|
+
readonly cause?: (Error | ZodError) | undefined;
|
|
16
|
+
constructor(message: string, cause?: (Error | ZodError) | undefined);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Policy validation error with details
|
|
20
|
+
*/
|
|
21
|
+
export declare class PolicyValidationError extends PolicyLoadError {
|
|
22
|
+
readonly issues: ZodError['issues'];
|
|
23
|
+
constructor(message: string, issues: ZodError['issues']);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Parse policy from string content
|
|
27
|
+
*
|
|
28
|
+
* @param content - YAML or JSON string
|
|
29
|
+
* @param format - Optional format hint ('yaml' | 'json'), auto-detected if not provided
|
|
30
|
+
* @returns Validated policy document
|
|
31
|
+
* @throws PolicyLoadError on parse failure
|
|
32
|
+
* @throws PolicyValidationError on schema validation failure
|
|
33
|
+
*/
|
|
34
|
+
export declare function parsePolicy(content: string, format?: 'yaml' | 'json'): PolicyDocument;
|
|
35
|
+
/**
|
|
36
|
+
* Validate a parsed policy object
|
|
37
|
+
*
|
|
38
|
+
* @param obj - Parsed policy object (from YAML/JSON)
|
|
39
|
+
* @returns Validated policy document
|
|
40
|
+
* @throws PolicyValidationError on schema validation failure
|
|
41
|
+
*/
|
|
42
|
+
export declare function validatePolicy(obj: unknown): PolicyDocument;
|
|
43
|
+
/**
|
|
44
|
+
* Load policy from file
|
|
45
|
+
*
|
|
46
|
+
* @param filePath - Path to policy file (.yaml, .yml, or .json)
|
|
47
|
+
* @returns Validated policy document
|
|
48
|
+
* @throws PolicyLoadError on file read or parse failure
|
|
49
|
+
* @throws PolicyValidationError on schema validation failure
|
|
50
|
+
*/
|
|
51
|
+
export declare function loadPolicy(filePath: string): PolicyDocument;
|
|
52
|
+
/**
|
|
53
|
+
* Check if a policy file exists and is readable
|
|
54
|
+
*
|
|
55
|
+
* @param filePath - Path to policy file
|
|
56
|
+
* @returns true if file exists and is readable
|
|
57
|
+
*/
|
|
58
|
+
export declare function policyFileExists(filePath: string): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Create a minimal example policy document
|
|
61
|
+
*
|
|
62
|
+
* Useful for scaffolding new policy files.
|
|
63
|
+
*/
|
|
64
|
+
export declare function createExamplePolicy(): PolicyDocument;
|
|
65
|
+
/**
|
|
66
|
+
* Serialize policy to YAML string
|
|
67
|
+
*
|
|
68
|
+
* @param policy - Policy document to serialize
|
|
69
|
+
* @returns YAML string
|
|
70
|
+
*/
|
|
71
|
+
export declare function serializePolicyYaml(policy: PolicyDocument): string;
|
|
72
|
+
/**
|
|
73
|
+
* Serialize policy to JSON string
|
|
74
|
+
*
|
|
75
|
+
* @param policy - Policy document to serialize
|
|
76
|
+
* @param pretty - Pretty-print with indentation (default: true)
|
|
77
|
+
* @returns JSON string
|
|
78
|
+
*/
|
|
79
|
+
export declare function serializePolicyJson(policy: PolicyDocument, pretty?: boolean): string;
|
|
80
|
+
//# sourceMappingURL=loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAwC,MAAM,SAAS,CAAC;AAE/E;;GAEG;AACH,qBAAa,eAAgB,SAAQ,KAAK;aAGtB,KAAK,CAAC,GAAE,KAAK,GAAG,QAAQ;gBADxC,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,GAAE,KAAK,GAAG,QAAQ,aAAA;CAK3C;AAED;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,eAAe;aAGtC,MAAM,EAAE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