@cclabsnz/sf-core 0.3.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/CHANGELOG.md +149 -0
- package/README.md +32 -8
- package/lib/events/rteCatalog.d.ts +17 -1
- package/lib/events/rteCatalog.js +28 -4
- package/lib/graph/coverage.d.ts +3 -0
- package/lib/graph/coverage.js +10 -0
- package/lib/graph/index.d.ts +9 -0
- package/lib/graph/index.js +10 -0
- package/lib/graph/kinds.d.ts +109 -0
- package/lib/graph/kinds.js +39 -0
- package/lib/graph/merge.d.ts +24 -0
- package/lib/graph/merge.js +175 -0
- package/lib/graph/roleOf.d.ts +17 -0
- package/lib/graph/roleOf.js +56 -0
- package/lib/graph/rules.d.ts +34 -0
- package/lib/graph/rules.js +30 -0
- package/lib/graph/schema.d.ts +170 -0
- package/lib/graph/schema.js +91 -0
- package/lib/graph/types.d.ts +84 -0
- package/lib/graph/types.js +10 -0
- package/lib/graph/validate.d.ts +8 -0
- package/lib/graph/validate.js +200 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +4 -0
- package/lib/lib/order.d.ts +2 -0
- package/lib/lib/order.js +10 -0
- package/lib/report/branding.js +1 -1
- package/package.json +12 -7
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type GraphDiagnostic } from './rules.js';
|
|
2
|
+
/** Edge identity used in findings, so a reader can locate the offending edge in the file. */
|
|
3
|
+
export declare function graphEdgeId(e: {
|
|
4
|
+
from: string;
|
|
5
|
+
to: string;
|
|
6
|
+
}): string;
|
|
7
|
+
export declare function validateGraph(doc: unknown): GraphDiagnostic[];
|
|
8
|
+
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// src/graph/validate.ts
|
|
2
|
+
// Runs before any renderer touches a document. Returns findings; never throws, never renders
|
|
3
|
+
// partially. Spec section 5.
|
|
4
|
+
import { Ajv } from 'ajv';
|
|
5
|
+
import { GRAPH_SCHEMA } from './schema.js';
|
|
6
|
+
import { GRAPH_RULES } from './rules.js';
|
|
7
|
+
import { SUPPORTED_GRAPH_SCHEMA_VERSION } from './types.js';
|
|
8
|
+
import { isKnownGraphKind, levelOfKind, layerOfKind } from './kinds.js';
|
|
9
|
+
import { codepointCompare } from '../lib/order.js';
|
|
10
|
+
// ajv exports the class as a named export alongside the default, and the named one is what
|
|
11
|
+
// survives the CommonJS-to-NodeNext boundary as a constructable value.
|
|
12
|
+
// `allErrors: true` is deliberate, and the risk Semgrep's ajv-allerrors-true rule guards against
|
|
13
|
+
// does not reach it. That rule is about unbounded error allocation: a schema containing `anyOf`,
|
|
14
|
+
// `oneOf`, `allOf`, `not` or `patternProperties` can make an attacker's input produce errors
|
|
15
|
+
// combinatorially. GRAPH_SCHEMA contains none of those keywords, its six `$ref`s point at flat
|
|
16
|
+
// non-recursive `$defs`, and the schema is a constant in this package — only the document varies,
|
|
17
|
+
// so error count is bounded linearly by the document's own size.
|
|
18
|
+
//
|
|
19
|
+
// It is also load-bearing. This validator's contract is that one run reports every problem with
|
|
20
|
+
// its rule code, the offending id and a fix, so an operator repairs a graph in one pass. Stopping
|
|
21
|
+
// at the first error would report one problem per run against documents that routinely carry
|
|
22
|
+
// thousands of nodes.
|
|
23
|
+
// nosemgrep: javascript.ajv.security.audit.ajv-allerrors-true
|
|
24
|
+
const ajv = new Ajv({ allErrors: true, strict: false });
|
|
25
|
+
const validateShape = ajv.compile(GRAPH_SCHEMA);
|
|
26
|
+
/** Edge identity used in findings, so a reader can locate the offending edge in the file. */
|
|
27
|
+
export function graphEdgeId(e) {
|
|
28
|
+
return `${e.from}->${e.to}`;
|
|
29
|
+
}
|
|
30
|
+
export function validateGraph(doc) {
|
|
31
|
+
if (typeof doc !== 'object' || doc === null || Array.isArray(doc)) {
|
|
32
|
+
return [
|
|
33
|
+
{
|
|
34
|
+
code: GRAPH_RULES.NOT_AN_OBJECT,
|
|
35
|
+
id: 'document',
|
|
36
|
+
message: 'The graph document is not a JSON object.',
|
|
37
|
+
fix: 'Pass the path to a canonical graph document written by this tool.',
|
|
38
|
+
},
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
const candidate = doc;
|
|
42
|
+
if (candidate.schemaVersion !== SUPPORTED_GRAPH_SCHEMA_VERSION) {
|
|
43
|
+
// Loud, not partial: a document from another schema version is not partially loadable,
|
|
44
|
+
// because the fields that changed are exactly the ones a reader would misread.
|
|
45
|
+
return [
|
|
46
|
+
{
|
|
47
|
+
code: GRAPH_RULES.SCHEMA_VERSION_UNSUPPORTED,
|
|
48
|
+
id: 'document',
|
|
49
|
+
message: `schemaVersion is ${String(candidate.schemaVersion)}; this build supports ` +
|
|
50
|
+
`${SUPPORTED_GRAPH_SCHEMA_VERSION}.`,
|
|
51
|
+
fix: `Re-extract the graph with this version of the tool, or run the migration to ${SUPPORTED_GRAPH_SCHEMA_VERSION}.`,
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
if (!validateShape(doc)) {
|
|
56
|
+
// A missing edge provenance is reported under its own code rather than as a generic shape
|
|
57
|
+
// error, because it is the one shape failure that has a specific remedy and because
|
|
58
|
+
// consumers key off the code.
|
|
59
|
+
const g = doc;
|
|
60
|
+
const provenanceFindings = (g.edges ?? [])
|
|
61
|
+
.filter((e) => e !== null && typeof e === 'object' && e.provenance === undefined)
|
|
62
|
+
.map((e) => ({
|
|
63
|
+
code: GRAPH_RULES.EDGE_MISSING_PROVENANCE,
|
|
64
|
+
id: graphEdgeId({ from: String(e.from), to: String(e.to) }),
|
|
65
|
+
message: 'Edge carries no provenance.',
|
|
66
|
+
fix: "Add provenance with source 'metadata', 'runtime' or 'derived'.",
|
|
67
|
+
}));
|
|
68
|
+
if (provenanceFindings.length > 0)
|
|
69
|
+
return provenanceFindings;
|
|
70
|
+
return (validateShape.errors ?? []).map((e) => ({
|
|
71
|
+
code: GRAPH_RULES.SCHEMA_SHAPE,
|
|
72
|
+
id: e.instancePath || 'document',
|
|
73
|
+
message: `${e.instancePath || 'document'} ${e.message ?? 'failed schema validation'}.`,
|
|
74
|
+
fix: 'Correct the field named in the path so it matches the documented shape.',
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
// ajv's compiled function is a type guard, so `doc` is narrowed to its own inferred shape
|
|
78
|
+
// here rather than to ours. The double assertion is deliberate: the schema is what makes the
|
|
79
|
+
// claim true, and it has just run.
|
|
80
|
+
const graph = doc;
|
|
81
|
+
return semanticFindings(graph);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Cross-referencing rules JSON Schema cannot express. Findings are accumulated rather than
|
|
85
|
+
* short-circuited: a reader fixing a document wants the whole list, not one error per run.
|
|
86
|
+
* Sorted by id then code so two runs over the same document report identically.
|
|
87
|
+
*/
|
|
88
|
+
function semanticFindings(graph) {
|
|
89
|
+
const findings = [];
|
|
90
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
91
|
+
const seen = new Set();
|
|
92
|
+
for (const n of graph.nodes) {
|
|
93
|
+
if (seen.has(n.id)) {
|
|
94
|
+
findings.push({
|
|
95
|
+
code: GRAPH_RULES.ID_DUPLICATE, id: n.id,
|
|
96
|
+
message: `Duplicate node id ${n.id}.`,
|
|
97
|
+
fix: 'Node ids are content-derived and unique. Two nodes derived the same id — namespace them by kind.',
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
seen.add(n.id);
|
|
101
|
+
if (!n.id.includes('.')) {
|
|
102
|
+
findings.push({
|
|
103
|
+
code: GRAPH_RULES.ID_NOT_NAMESPACED, id: n.id,
|
|
104
|
+
message: `Node id ${n.id} is not namespaced by kind.`,
|
|
105
|
+
fix: 'Prefix the id with its kind namespace, for example obj.Account or permset.Sales_Ops.',
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (!isKnownGraphKind(n.kind)) {
|
|
109
|
+
findings.push({
|
|
110
|
+
code: GRAPH_RULES.UNKNOWN_KIND, id: n.id,
|
|
111
|
+
message: `Unknown kind ${n.kind}.`,
|
|
112
|
+
fix: 'Add the kind to GRAPH_KIND_TABLE in src/graph/kinds.ts with its layer and level, or correct the node.',
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
if (n.level !== levelOfKind(n.kind)) {
|
|
117
|
+
findings.push({
|
|
118
|
+
code: GRAPH_RULES.LEVEL_KIND_MISMATCH, id: n.id,
|
|
119
|
+
message: `Node ${n.id} stores level ${n.level}; kind ${n.kind} is level ${levelOfKind(n.kind)}.`,
|
|
120
|
+
fix: `Set level to ${levelOfKind(n.kind)}. Level is a pure function of kind; the stored value is a convenience copy.`,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (n.layer !== layerOfKind(n.kind)) {
|
|
124
|
+
findings.push({
|
|
125
|
+
code: GRAPH_RULES.LAYER_KIND_MISMATCH, id: n.id,
|
|
126
|
+
message: `Node ${n.id} stores layer ${n.layer}; kind ${n.kind} is layer ${layerOfKind(n.kind)}.`,
|
|
127
|
+
fix: `Set layer to ${layerOfKind(n.kind)}.`,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (n.level === 0 && n.parent !== null) {
|
|
132
|
+
findings.push({
|
|
133
|
+
code: GRAPH_RULES.ROOT_HAS_PARENT, id: n.id,
|
|
134
|
+
message: `Level 0 node ${n.id} carries a parent.`,
|
|
135
|
+
fix: 'Set parent to null. Level 0 is the root of the containment tree.',
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
// A null parent above level 0 is deliberately not a finding. It means extracted but not
|
|
139
|
+
// grouped, and the selector reports it as unattributed. Spec section 2.3.
|
|
140
|
+
if (n.parent !== null) {
|
|
141
|
+
const parent = byId.get(n.parent);
|
|
142
|
+
if (!parent) {
|
|
143
|
+
findings.push({
|
|
144
|
+
code: GRAPH_RULES.PARENT_UNRESOLVED, id: n.id,
|
|
145
|
+
message: `Parent ${n.parent} of ${n.id} is not in the document.`,
|
|
146
|
+
fix: 'Add the parent node, or set parent to null to report this node as unattributed.',
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
else if (parent.level >= n.level) {
|
|
150
|
+
findings.push({
|
|
151
|
+
code: GRAPH_RULES.LEVEL_PARENT_ORDER, id: n.id,
|
|
152
|
+
message: `Parent ${parent.id} is level ${parent.level}; child ${n.id} is level ${n.level}.`,
|
|
153
|
+
fix: 'A parent must sit at a strictly lower level than its child.',
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
for (const n of graph.nodes) {
|
|
159
|
+
if (inCycle(n.id, byId)) {
|
|
160
|
+
findings.push({
|
|
161
|
+
code: GRAPH_RULES.PARENT_CYCLE, id: n.id,
|
|
162
|
+
message: `Node ${n.id} is part of a parent cycle.`,
|
|
163
|
+
fix: 'Break the cycle: containment must be a tree rooted at a level 0 node.',
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (const e of graph.edges) {
|
|
168
|
+
const id = graphEdgeId(e);
|
|
169
|
+
if (e.provenance.source === 'derived' && !e.provenance.rule) {
|
|
170
|
+
findings.push({
|
|
171
|
+
code: GRAPH_RULES.DERIVED_MISSING_RULE, id,
|
|
172
|
+
message: 'Derived edge does not name the rule that produced it.',
|
|
173
|
+
fix: 'Set provenance.rule to the identifier of the aggregation rule.',
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
for (const endpoint of [e.from, e.to]) {
|
|
177
|
+
if (!byId.has(endpoint)) {
|
|
178
|
+
findings.push({
|
|
179
|
+
code: GRAPH_RULES.EDGE_ENDPOINT_UNRESOLVED, id,
|
|
180
|
+
message: `Edge endpoint ${endpoint} is not in the document.`,
|
|
181
|
+
fix: 'Add the missing node, or remove the edge.',
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return findings.sort((a, b) => codepointCompare(a.id, b.id) || codepointCompare(a.code, b.code));
|
|
187
|
+
}
|
|
188
|
+
/** Walks the parent chain with a visited set, so a cycle terminates instead of hanging. */
|
|
189
|
+
function inCycle(start, byId) {
|
|
190
|
+
const visited = new Set();
|
|
191
|
+
let current = start;
|
|
192
|
+
while (current !== null) {
|
|
193
|
+
if (visited.has(current))
|
|
194
|
+
return true;
|
|
195
|
+
visited.add(current);
|
|
196
|
+
current = byId.get(current)?.parent ?? null;
|
|
197
|
+
}
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=validate.js.map
|
package/lib/index.d.ts
CHANGED
|
@@ -26,4 +26,5 @@ export type { RiskLevel } from './findings/RiskLevel.js';
|
|
|
26
26
|
export { RISK_LEVELS } from './findings/RiskLevel.js';
|
|
27
27
|
export * from './platform/index.js';
|
|
28
28
|
export * from './schemas/index.js';
|
|
29
|
+
export * from './graph/index.js';
|
|
29
30
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
CHANGED
|
@@ -19,4 +19,8 @@ export { RISK_LEVELS } from './findings/RiskLevel.js';
|
|
|
19
19
|
export * from './platform/index.js';
|
|
20
20
|
// --- IR schemas (typed contracts + JSON schema access) ---
|
|
21
21
|
export * from './schemas/index.js';
|
|
22
|
+
// The canonical org graph. Owned here rather than in sf-orgviz so that sf-orgviz and
|
|
23
|
+
// sf-orgintel can both produce fragments of one schema without either depending on the other.
|
|
24
|
+
// See sf-orgviz/docs/CONVERGENCE_SPEC.md section 1.2.
|
|
25
|
+
export * from './graph/index.js';
|
|
22
26
|
//# sourceMappingURL=index.js.map
|
package/lib/lib/order.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// src/lib/order.ts
|
|
2
|
+
// The one comparator every path that produces output bytes must use. Spec section 12 requires
|
|
3
|
+
// same input -> identical output, byte for byte, across runs and machines. `String.prototype
|
|
4
|
+
// .localeCompare()` with no locale argument resolves against the runtime's ICU and the `LANG`
|
|
5
|
+
// environment variable, so it can reorder output between two machines -- or two Node builds --
|
|
6
|
+
// that never disagree about anything else. Plain codepoint order has no such dependency.
|
|
7
|
+
export function codepointCompare(a, b) {
|
|
8
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=order.js.map
|
package/lib/report/branding.js
CHANGED
|
@@ -12,7 +12,7 @@ export const DEFAULT_BRANDING = {
|
|
|
12
12
|
website: 'cloudcounsel.co.nz',
|
|
13
13
|
};
|
|
14
14
|
export function resolveBranding(overrides, preparedFor) {
|
|
15
|
-
const merged = { ...DEFAULT_BRANDING, ...
|
|
15
|
+
const merged = { ...DEFAULT_BRANDING, ...overrides };
|
|
16
16
|
if (preparedFor)
|
|
17
17
|
merged.preparedFor = preparedFor;
|
|
18
18
|
return merged;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cclabsnz/sf-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Shared platform layer for CloudCounsel Salesforce sf plugins — API clients, org context, event log pull, report shell, and versioned IR schemas.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"./schemas/*": "./schemas/*"
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
|
+
"lint": "oxlint src test",
|
|
21
22
|
"build": "tsc -p tsconfig.json",
|
|
22
23
|
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --passWithNoTests",
|
|
23
24
|
"test:unit": "node --experimental-vm-modules node_modules/jest/bin/jest.js test/unit --passWithNoTests",
|
|
@@ -27,14 +28,14 @@
|
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"@jest/globals": "^29.7.0",
|
|
31
|
+
"@salesforce/core": "^9",
|
|
30
32
|
"@types/jest": "^29",
|
|
31
33
|
"@types/node": "^22",
|
|
32
|
-
"ajv": "^8",
|
|
33
34
|
"jest": "^29",
|
|
35
|
+
"oxlint": "^1.79.0",
|
|
34
36
|
"ts-jest": "^29",
|
|
35
37
|
"ts-node": "^10.9.2",
|
|
36
|
-
"typescript": "^5"
|
|
37
|
-
"@salesforce/core": "^9"
|
|
38
|
+
"typescript": "^5"
|
|
38
39
|
},
|
|
39
40
|
"engines": {
|
|
40
41
|
"node": ">=18.0.0"
|
|
@@ -45,6 +46,7 @@
|
|
|
45
46
|
"/src/assets",
|
|
46
47
|
"LICENSE",
|
|
47
48
|
"README.md",
|
|
49
|
+
"CHANGELOG.md",
|
|
48
50
|
"!/lib/**/*.map"
|
|
49
51
|
],
|
|
50
52
|
"repository": {
|
|
@@ -71,13 +73,16 @@
|
|
|
71
73
|
"pnpm": {
|
|
72
74
|
"overrides": {
|
|
73
75
|
"form-data": "^4.0.6",
|
|
74
|
-
"js-yaml@3": "^3.15.
|
|
76
|
+
"js-yaml@3": "^3.15.1",
|
|
75
77
|
"handlebars": "^4.7.9",
|
|
76
|
-
"brace-expansion": "^5.0.
|
|
77
|
-
"fast-uri@3": "^3.1.
|
|
78
|
+
"brace-expansion": "^5.0.9",
|
|
79
|
+
"fast-uri@3": "^3.1.6"
|
|
78
80
|
}
|
|
79
81
|
},
|
|
80
82
|
"peerDependencies": {
|
|
81
83
|
"@salesforce/core": "^8 || ^9"
|
|
84
|
+
},
|
|
85
|
+
"dependencies": {
|
|
86
|
+
"ajv": "^8.20.0"
|
|
82
87
|
}
|
|
83
88
|
}
|