@cassiomc1/forgeloop 1.12.0 → 1.14.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/.github/copilot-instructions.md +1 -1
- package/AGENTS.md +1 -1
- package/AGENT_COMPATIBILITY.md +8 -0
- package/CLAUDE.md +1 -1
- package/CONTRIBUTING.md +90 -0
- package/DOCS_INDEX.md +46 -12
- package/ENG/c-development-eng.md +112 -0
- package/ENG/cpp-development-eng.md +109 -0
- package/ENG/dotnet-aspnetcore-development-eng.md +401 -0
- package/ENG/go-development-eng.md +103 -0
- package/ENG/java-development-eng.md +125 -0
- package/ENG/nodejs-backend-development-eng.md +605 -0
- package/ENG/php-development-eng.md +104 -0
- package/ENG/rust-development-eng.md +422 -0
- package/ENG/sec-code-eng.md +7 -7
- package/ENG/sql-development-eng.md +108 -0
- package/ENG/swift-development-eng.md +111 -0
- package/ENG/typescript-development-eng.md +108 -0
- package/EXECUTION_STATE.md +12 -0
- package/GUIDE_ROUTER.md +418 -9
- package/LOOP_ENGINEERING.md +28 -2
- package/ORCHESTRATOR_INTEGRATION.md +9 -5
- package/PROTOCOL_INTEGRATION.md +55 -2
- package/QUALITY_SCORECARD.md +1 -0
- package/README.md +78 -52
- package/TERMINOLOGY.md +2 -0
- package/THIRD_PARTY_NOTICES.md +19 -7
- package/THREAT_MODEL.md +140 -1
- package/completions/_forgeloop +22 -4
- package/completions/forgeloop.bash +40 -4
- package/completions/forgeloop.fish +130 -1
- package/docs/ADVISORY_CONTEXT.md +25 -0
- package/docs/AGENT_BROWSER_ADAPTER.md +81 -0
- package/docs/AGENT_BROWSER_VERIFICATION.md +6 -0
- package/docs/AGENT_PROTOCOL_SUMMARY.md +81 -3
- package/docs/AGENT_SKILL.md +66 -0
- package/docs/ARTIFACT_REFERENCE.md +123 -0
- package/docs/AUDIT_UX.md +46 -0
- package/docs/BROWSER_VERIFICATION.md +136 -0
- package/docs/CLI_REFERENCE.md +392 -10
- package/docs/CODE_ATTESTATION.md +2 -2
- package/docs/DOCUMENTATION_GUIDE.md +34 -12
- package/docs/GETTING_STARTED.md +59 -0
- package/docs/JEV_BENCHMARKS.md +31 -0
- package/docs/MODEL_ROUTING.md +37 -0
- package/docs/OPENSRC_ADAPTER.md +241 -0
- package/docs/PACKAGE_CONTENTS.md +60 -19
- package/docs/PROVIDERS.md +126 -0
- package/docs/PROVIDER_ARCHITECTURE.md +199 -0
- package/docs/RECIPES.md +32 -0
- package/docs/RELEASE_CHECKLIST.md +66 -5
- package/docs/SECURITY_REVIEW.md +71 -0
- package/docs/SEMANTIC_DECISION_PLANE.md +71 -0
- package/docs/TEST_INTELLIGENCE.md +29 -0
- package/docs/TEST_PRUNING.md +14 -0
- package/docs/TROUBLESHOOTING.md +298 -3
- package/docs/UNIVERSAL_INTEGRATION.md +31 -0
- package/docs/assets/diagrams/forgeloop-code-attestation-flow.html +2 -2
- package/docs/assets/diagrams/forgeloop-code-attestation-flow.receipt.json +5 -5
- package/docs/assets/diagrams/forgeloop-code-attestation-flow.svg +1 -1
- package/docs/assets/diagrams/forgeloop-engineering-flow.html +39 -26
- package/docs/assets/diagrams/forgeloop-engineering-flow.receipt.json +6 -6
- package/docs/assets/diagrams/forgeloop-engineering-flow.svg +26 -26
- package/docs/assets/diagrams/forgeloop-verification-trust-flow.html +2 -1
- package/docs/assets/diagrams/forgeloop-verification-trust-flow.receipt.json +5 -5
- package/docs/assets/diagrams/forgeloop-verification-trust-flow.svg +1 -1
- package/docs/diagrams/README.md +13 -9
- package/docs/diagrams/forgeloop-code-attestation-flow.workflow.json +1 -1
- package/docs/diagrams/forgeloop-engineering-flow.workflow.json +24 -19
- package/docs/diagrams/forgeloop-verification-trust-flow.workflow.json +1 -0
- package/docs/diagrams/reviews/forgeloop-code-attestation-flow.review.json +4 -4
- package/docs/diagrams/reviews/forgeloop-engineering-flow.review.json +4 -4
- package/docs/diagrams/reviews/forgeloop-verification-trust-flow.review.json +4 -4
- package/docs/documentation-manifest.json +1397 -0
- package/docs/protocol-requirements.json +101 -0
- package/package.json +46 -4
- package/schemas/config.schema.json +14 -0
- package/schemas/context-plan.schema.json +18 -0
- package/schemas/routing-input.schema.json +1 -1
- package/schemas/semantic-decision.schema.json +46 -0
- package/schemas/test-utility.schema.json +44 -0
- package/scripts/CI_VALIDATORS.md +84 -11
- package/scripts/benchmark-jev.mjs +5 -0
- package/scripts/benchmark-test-intelligence.mjs +4 -0
- package/scripts/generate-agent-protocol-summary.mjs +40 -1
- package/scripts/generate-forgeloop-skill.mjs +133 -0
- package/scripts/jev-smoke.mjs +19 -0
- package/skills/forgeloop/README.md +9 -0
- package/skills/forgeloop/SKILL.md +77 -0
- package/skills/forgeloop/references/lifecycle.md +9 -0
- package/skills/forgeloop/references/recovery.md +7 -0
- package/skills/forgeloop/references/verification.md +7 -0
- package/src/adapters/agent-browser/assertions.js +47 -0
- package/src/adapters/agent-browser/commands.js +54 -0
- package/src/adapters/agent-browser/index.js +3 -0
- package/src/adapters/agent-browser/locator.js +40 -0
- package/src/adapters/agent-browser/process.js +215 -0
- package/src/adapters/agent-browser/provider.js +313 -0
- package/src/adapters/emulated-services/constants.js +24 -0
- package/src/adapters/emulated-services/index.js +7 -0
- package/src/adapters/emulated-services/process.js +162 -0
- package/src/adapters/emulated-services/provider.js +282 -0
- package/src/adapters/opensrc/normalize.js +90 -0
- package/src/adapters/opensrc/process.js +248 -0
- package/src/adapters/opensrc/provider.js +338 -0
- package/src/adapters/opensrc/search.js +264 -0
- package/src/adapters/typesafe/client.js +28 -0
- package/src/adapters/typesafe/engine.js +63 -0
- package/src/adapters/typesafe/normalize.js +41 -0
- package/src/cli.js +108 -0
- package/src/commands/checkpoint-revalidate.js +176 -0
- package/src/commands/context-plan.js +38 -0
- package/src/commands/contract-create.js +264 -0
- package/src/commands/contract-revise.js +236 -0
- package/src/commands/decision-show.js +14 -0
- package/src/commands/decision-status.js +22 -0
- package/src/commands/discover.js +41 -0
- package/src/commands/doctor.js +15 -0
- package/src/commands/gate-record.js +205 -0
- package/src/commands/gate-revalidate.js +137 -0
- package/src/commands/model-route.js +32 -0
- package/src/commands/next.js +19 -7
- package/src/commands/route.js +146 -18
- package/src/commands/semantic-plan.js +17 -0
- package/src/commands/task-abandon.js +224 -0
- package/src/commands/task-create.js +84 -25
- package/src/commands/task-list.js +22 -2
- package/src/commands/task-migrate-contract-bootstrap-repair.js +288 -0
- package/src/commands/task-repair-contract-bootstrap.js +263 -0
- package/src/commands/test-inventory.js +5 -0
- package/src/commands/test-prune-plan.js +5 -0
- package/src/commands/test-prune-probe.js +5 -0
- package/src/commands/test-utility.js +5 -0
- package/src/commands/validate-protocol.js +10 -1
- package/src/config/guides.json +44 -0
- package/src/core/artifact-registry.js +24 -0
- package/src/core/audit-ux.js +514 -0
- package/src/core/browser-verification/constants.js +149 -0
- package/src/core/browser-verification/normalize.js +254 -0
- package/src/core/browser-verification/provider.js +519 -0
- package/src/core/browser-verification/service.js +115 -0
- package/src/core/build-script.js +151 -0
- package/src/core/c-cpp-project.js +143 -0
- package/src/core/checkpoint-revalidation.js +319 -0
- package/src/core/cli-command-definitions.js +249 -1
- package/src/core/command-executors.js +115 -3
- package/src/core/command-input.js +212 -102
- package/src/core/completion-artifacts.js +14 -5
- package/src/core/completion.js +4 -6
- package/src/core/config.js +3 -0
- package/src/core/context-compiler/budget.js +9 -0
- package/src/core/context-compiler/candidates.js +39 -0
- package/src/core/context-compiler/compiler.js +63 -0
- package/src/core/context-compiler/fingerprint.js +11 -0
- package/src/core/context-compiler/policy.js +13 -0
- package/src/core/context-compiler/result.js +23 -0
- package/src/core/contract-bootstrap-recovery.js +655 -0
- package/src/core/contract-presets.js +82 -0
- package/src/core/contract-revision.js +210 -0
- package/src/core/decision/artifact.js +69 -0
- package/src/core/decision/benchmarks.js +103 -0
- package/src/core/decision/cache.js +27 -0
- package/src/core/decision/constants.js +58 -0
- package/src/core/decision/cutover.js +34 -0
- package/src/core/decision/engine.js +22 -0
- package/src/core/decision/errors.js +68 -0
- package/src/core/decision/events.js +101 -0
- package/src/core/decision/freshness.js +19 -0
- package/src/core/decision/normalizers/index.js +115 -0
- package/src/core/decision/policy.js +18 -0
- package/src/core/decision/projection.js +16 -0
- package/src/core/decision/question-registry.js +201 -0
- package/src/core/decision/request.js +26 -0
- package/src/core/decision/resolver.js +130 -0
- package/src/core/decision/result.js +58 -0
- package/src/core/decision/service.js +156 -0
- package/src/core/decision/state-builder.js +65 -0
- package/src/core/decision/task-bindings.js +30 -0
- package/src/core/decision/test-provider.js +32 -0
- package/src/core/decision/thresholds.js +15 -0
- package/src/core/error-codes.js +281 -3
- package/src/core/events.js +226 -57
- package/src/core/evidence-readiness.js +9 -0
- package/src/core/execution-prerequisites.js +14 -0
- package/src/core/execution-profile.js +63 -38
- package/src/core/filesystem.js +1 -10
- package/src/core/gate-provenance.js +124 -0
- package/src/core/go-project.js +206 -0
- package/src/core/integration-invocation-policy.js +27 -4
- package/src/core/integration-resources.js +86 -61
- package/src/core/java-project.js +403 -0
- package/src/core/model-router/constants.js +10 -0
- package/src/core/model-router/policy.js +103 -0
- package/src/core/model-router/router.js +37 -0
- package/src/core/multi-language-project.js +117 -0
- package/src/core/next-action-model.js +58 -0
- package/src/core/next-action-phases.js +130 -42
- package/src/core/next-action-refresh.js +43 -9
- package/src/core/next-action-review-phase.js +7 -2
- package/src/core/next-action.js +35 -7
- package/src/core/next-explanation.js +63 -0
- package/src/core/phase.js +128 -10
- package/src/core/php-project.js +85 -0
- package/src/core/preflight-consistency.js +23 -9
- package/src/core/preflight-loaders.js +37 -5
- package/src/core/project-detection.js +1760 -52
- package/src/core/protocol-info.js +65 -0
- package/src/core/protocol.js +20 -0
- package/src/core/reconcile-closure.js +132 -53
- package/src/core/recovery-history.js +1 -0
- package/src/core/resumability.js +154 -44
- package/src/core/route-artifact.js +15 -1
- package/src/core/router.js +223 -4
- package/src/core/runtime-context.js +118 -61
- package/src/core/rust-project.js +400 -0
- package/src/core/schema-validation.js +3 -0
- package/src/core/security-review/constants.js +64 -0
- package/src/core/security-review/normalize.js +245 -0
- package/src/core/security-review/provider.js +204 -0
- package/src/core/security-review/service.js +134 -0
- package/src/core/semantic-planning/constants.js +19 -0
- package/src/core/semantic-planning/projection.js +94 -0
- package/src/core/semantic-planning/service.js +15 -0
- package/src/core/sources.js +37 -0
- package/src/core/sql-project.js +141 -0
- package/src/core/swift-project.js +200 -0
- package/src/core/task-claim-state.js +201 -1
- package/src/core/task-conflict-inspection.js +31 -5
- package/src/core/task-paths.js +13 -0
- package/src/core/task-recovery.js +1 -0
- package/src/core/templates.js +3 -0
- package/src/core/test-intelligence/benchmarks.js +68 -0
- package/src/core/test-intelligence/inventory.js +73 -0
- package/src/core/test-intelligence/prune.js +90 -0
- package/src/core/test-intelligence/semantic-state.js +15 -0
- package/src/core/test-intelligence/service.js +40 -0
- package/src/core/test-intelligence/utility.js +50 -0
- package/src/core/trace.js +11 -7
- package/src/core/transaction.js +1 -0
- package/src/core/typescript-project.js +349 -0
- package/src/core/xml-structure.js +123 -0
- package/src/integration.d.ts +492 -0
- package/src/integration.js +54 -0
- package/src/providers/README.md +47 -0
- package/src/providers/capabilities.js +46 -0
- package/src/providers/errors.js +15 -0
- package/src/providers/index.js +29 -0
- package/src/providers/json-snapshot.js +105 -0
- package/src/providers/registry.js +152 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { canonicalFingerprint } from "../artifacts.js";
|
|
2
|
+
|
|
3
|
+
function classify(test, judgment = null, confidence = 1) {
|
|
4
|
+
const signal = test.signals ?? {};
|
|
5
|
+
if (signal.requiredBehavior) return { classification: "KEEP_REQUIRED", recommendation: "KEEP", protected: true };
|
|
6
|
+
if (signal.security) return { classification: "KEEP_RISK_GUARD", recommendation: "KEEP", protected: true };
|
|
7
|
+
if (signal.criticalPath) return { classification: "KEEP_RECOVERY_INVARIANT", recommendation: "KEEP", protected: true };
|
|
8
|
+
if (signal.migration) return { classification: "KEEP_MIGRATION_COMPATIBILITY", recommendation: "KEEP", protected: true };
|
|
9
|
+
if (signal.publicApi) return { classification: "KEEP_UNIQUE", recommendation: "KEEP", protected: true };
|
|
10
|
+
if (signal.release) return { classification: "KEEP_RELEASE_SMOKE", recommendation: "KEEP", protected: true };
|
|
11
|
+
if (signal.platformSpecific) return { classification: "KEEP_PLATFORM_BEHAVIOR", recommendation: "KEEP", protected: true };
|
|
12
|
+
if (signal.integration) return { classification: "KEEP_INTEGRATION_GUARD", recommendation: "KEEP", protected: true };
|
|
13
|
+
if (!judgment || confidence < 0.75) return { classification: "UNKNOWN", recommendation: "BLOCKED", protected: false };
|
|
14
|
+
if (judgment.protocol_invariant) return { classification: "KEEP_RECOVERY_INVARIANT", recommendation: "KEEP", protected: true };
|
|
15
|
+
if (judgment.semantic_duplicate) {
|
|
16
|
+
return {
|
|
17
|
+
classification: "REDUNDANT_CANDIDATE",
|
|
18
|
+
recommendation: judgment.requires_system_two_review ? "BLOCKED" : "PROBE_REMOVAL",
|
|
19
|
+
protected: false,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (judgment.likely_obsolete) return { classification: "OBSOLETE_CANDIDATE", recommendation: "BLOCKED", protected: false };
|
|
23
|
+
if (judgment.likely_flaky_low_signal) return { classification: "FLAKY_LOW_SIGNAL", recommendation: "BLOCKED", protected: false };
|
|
24
|
+
if (judgment.expensive_relative_to_signal) return { classification: "EXPENSIVE_LOW_SIGNAL", recommendation: "BLOCKED", protected: false };
|
|
25
|
+
if (judgment.documentation_value) return { classification: "KEEP_DOCUMENTATION_VALUE", recommendation: "KEEP", protected: false };
|
|
26
|
+
if (judgment.integration_value) return { classification: "KEEP_INTEGRATION_GUARD", recommendation: "KEEP", protected: true };
|
|
27
|
+
if (judgment.has_unique_semantic_intent) return { classification: "KEEP_UNIQUE", recommendation: "KEEP", protected: false };
|
|
28
|
+
return { classification: "UNKNOWN", recommendation: "BLOCKED", protected: false };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function buildTestUtilityArtifact({ taskId, inventory, semanticStatus = "NOT_REQUESTED", semanticDecision = null, semanticDecisions = [] } = {}) {
|
|
32
|
+
const decisions = semanticDecisions.length > 0 ? semanticDecisions : (semanticDecision ? [semanticDecision] : []);
|
|
33
|
+
const byTest = new Map(decisions.flatMap((decision) => Object.entries(decision.decision?.tests ?? {}).map(([testId, judgment]) => [testId, {
|
|
34
|
+
judgment, confidence: Math.min(...Object.keys(judgment).map((key) => decision.confidence?.[`test_${(decision.decision.candidateIds ?? []).indexOf(testId)}_${key}`] ?? 0)),
|
|
35
|
+
}])));
|
|
36
|
+
return {
|
|
37
|
+
schemaVersion: 1,
|
|
38
|
+
protocolVersion: 1,
|
|
39
|
+
taskId,
|
|
40
|
+
generatedAt: new Date().toISOString(),
|
|
41
|
+
inventoryFingerprint: canonicalFingerprint(inventory),
|
|
42
|
+
semanticStatus: decisions.length > 0 ? "PROVIDER_REPORTED" : semanticStatus,
|
|
43
|
+
...(decisions.length > 0 ? {
|
|
44
|
+
...(decisions.length === 1 ? { decisionId: decisions[0].decisionId, semanticDecisionFingerprint: canonicalFingerprint(decisions[0]) } : {}),
|
|
45
|
+
decisionIds: decisions.map((decision) => decision.decisionId),
|
|
46
|
+
semanticDecisionFingerprints: decisions.map((decision) => canonicalFingerprint(decision)),
|
|
47
|
+
} : {}),
|
|
48
|
+
tests: (inventory?.tests ?? []).map((test) => ({ ...test, ...classify(test, byTest.get(test.testId)?.judgment, byTest.get(test.testId)?.confidence ?? 0) })),
|
|
49
|
+
};
|
|
50
|
+
}
|
package/src/core/trace.js
CHANGED
|
@@ -51,6 +51,7 @@ const EVENT_CATEGORY_MAP = Object.freeze({
|
|
|
51
51
|
TASK_RECOVERY_RECORDED: "recovery",
|
|
52
52
|
TASK_RECOVERY_RESUMED: "recovery",
|
|
53
53
|
OPERATOR_RECOVERY_RECORDED: "recovery",
|
|
54
|
+
TASK_ABANDONED: "recovery",
|
|
54
55
|
LEGACY_RECOVERY_MIGRATION_RECORDED: "recovery",
|
|
55
56
|
GATE_SATISFIED: "policy",
|
|
56
57
|
COMPLETION_VALIDATED: "completion",
|
|
@@ -82,6 +83,13 @@ const LIFECYCLE_TRANSITIONS = Object.freeze([
|
|
|
82
83
|
"COMPLETION_REJECTED",
|
|
83
84
|
]);
|
|
84
85
|
|
|
86
|
+
const STATIC_EVENT_SUMMARIES = Object.freeze({
|
|
87
|
+
TASK_RECOVERY_RESUMED: "Task resumed after recovery",
|
|
88
|
+
OPERATOR_RECOVERY_RECORDED: "Operator recovery recorded",
|
|
89
|
+
TASK_ABANDONED: "Task explicitly abandoned",
|
|
90
|
+
LEGACY_RECOVERY_MIGRATION_RECORDED: "Legacy recovery migration recorded",
|
|
91
|
+
});
|
|
92
|
+
|
|
85
93
|
export function eventCategory(eventType) {
|
|
86
94
|
return EVENT_CATEGORY_MAP[eventType] ?? "integrity";
|
|
87
95
|
}
|
|
@@ -93,6 +101,8 @@ export function timestampQuality(event) {
|
|
|
93
101
|
|
|
94
102
|
function eventSummary(event) {
|
|
95
103
|
const d = event.details ?? {};
|
|
104
|
+
const staticSummary = STATIC_EVENT_SUMMARIES[event.event];
|
|
105
|
+
if (staticSummary) return staticSummary;
|
|
96
106
|
switch (event.event) {
|
|
97
107
|
case "TASK_RECEIVED":
|
|
98
108
|
return `Task received${d.taskId ? ` (${d.taskId})` : ""}`;
|
|
@@ -132,12 +142,6 @@ function eventSummary(event) {
|
|
|
132
142
|
return "Continuity checkpoint recorded";
|
|
133
143
|
case "TASK_RECOVERY_RECORDED":
|
|
134
144
|
return `Task recovery recorded (${d.classification ?? "unknown"})`;
|
|
135
|
-
case "TASK_RECOVERY_RESUMED":
|
|
136
|
-
return "Task resumed after recovery";
|
|
137
|
-
case "OPERATOR_RECOVERY_RECORDED":
|
|
138
|
-
return "Operator recovery recorded";
|
|
139
|
-
case "LEGACY_RECOVERY_MIGRATION_RECORDED":
|
|
140
|
-
return "Legacy recovery migration recorded";
|
|
141
145
|
case "COMPLETION_VALIDATED":
|
|
142
146
|
return "Completion validated";
|
|
143
147
|
case "COMPLETION_REJECTED":
|
|
@@ -451,7 +455,7 @@ export async function buildTaskTrace({ target, packageRoot, taskId = null, event
|
|
|
451
455
|
const evidence = [...evidenceSources.values()].sort((a, b) => a.ref.localeCompare(b.ref));
|
|
452
456
|
|
|
453
457
|
const recovery = taskEvents
|
|
454
|
-
.filter((event) => ["TASK_RECOVERY_RECORDED", "TASK_RECOVERY_RESUMED", "OPERATOR_RECOVERY_RECORDED", "LEGACY_RECOVERY_MIGRATION_RECORDED"].includes(event.event))
|
|
458
|
+
.filter((event) => ["TASK_RECOVERY_RECORDED", "TASK_RECOVERY_RESUMED", "OPERATOR_RECOVERY_RECORDED", "TASK_ABANDONED", "LEGACY_RECOVERY_MIGRATION_RECORDED"].includes(event.event))
|
|
455
459
|
.map((event) => ({ sequence: event.seq, at: event.at, type: event.event, details: event.details ?? {} }));
|
|
456
460
|
|
|
457
461
|
const completionEvents = taskEvents.filter((event) => ["COMPLETION_VALIDATED", "COMPLETION_REJECTED"].includes(event.event));
|
package/src/core/transaction.js
CHANGED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { createLanguageProject } from "./multi-language-project.js";
|
|
4
|
+
|
|
5
|
+
const TS_CONFIG_NAME = /^tsconfig(?:\.[^/]+)?\.json$/u;
|
|
6
|
+
|
|
7
|
+
function isPlainObject(value) {
|
|
8
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function invalidConfig() {
|
|
12
|
+
return {
|
|
13
|
+
valid: false,
|
|
14
|
+
references: [],
|
|
15
|
+
extends: [],
|
|
16
|
+
compilerOptions: {},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function maskJsonComments(text) {
|
|
21
|
+
let result = "";
|
|
22
|
+
let state = "code";
|
|
23
|
+
let escaped = false;
|
|
24
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
25
|
+
const character = text[index];
|
|
26
|
+
const next = text[index + 1];
|
|
27
|
+
if (state === "string") {
|
|
28
|
+
result += character;
|
|
29
|
+
if (escaped) escaped = false;
|
|
30
|
+
else if (character === "\\") escaped = true;
|
|
31
|
+
else if (character === '"') state = "code";
|
|
32
|
+
} else if (state === "line-comment") {
|
|
33
|
+
if (character === "\n") {
|
|
34
|
+
result += character;
|
|
35
|
+
state = "code";
|
|
36
|
+
} else result += " ";
|
|
37
|
+
} else if (state === "block-comment") {
|
|
38
|
+
if (character === "*" && next === "/") {
|
|
39
|
+
result += " ";
|
|
40
|
+
index += 1;
|
|
41
|
+
state = "code";
|
|
42
|
+
} else result += character === "\n" ? "\n" : " ";
|
|
43
|
+
} else if (character === '"') {
|
|
44
|
+
result += character;
|
|
45
|
+
state = "string";
|
|
46
|
+
} else if (character === "/" && next === "/") {
|
|
47
|
+
result += " ";
|
|
48
|
+
index += 1;
|
|
49
|
+
state = "line-comment";
|
|
50
|
+
} else if (character === "/" && next === "*") {
|
|
51
|
+
result += " ";
|
|
52
|
+
index += 1;
|
|
53
|
+
state = "block-comment";
|
|
54
|
+
} else result += character;
|
|
55
|
+
}
|
|
56
|
+
return state === "block-comment" ? null : result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function removeTrailingCommas(text) {
|
|
60
|
+
let result = "";
|
|
61
|
+
let state = "code";
|
|
62
|
+
let escaped = false;
|
|
63
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
64
|
+
const character = text[index];
|
|
65
|
+
if (state === "string") {
|
|
66
|
+
result += character;
|
|
67
|
+
if (escaped) escaped = false;
|
|
68
|
+
else if (character === "\\") escaped = true;
|
|
69
|
+
else if (character === '"') state = "code";
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (character === '"') {
|
|
73
|
+
result += character;
|
|
74
|
+
state = "string";
|
|
75
|
+
} else if (character === "," && /^[\s]*[}\]]/u.test(text.slice(index + 1))) {
|
|
76
|
+
continue;
|
|
77
|
+
} else result += character;
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function isTypeScriptConfigName(fileName) {
|
|
83
|
+
return typeof fileName === "string" && TS_CONFIG_NAME.test(fileName.toLowerCase());
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function normalizedReferencePath(projectRoot, reference) {
|
|
87
|
+
return path.posix.normalize(path.posix.join(projectRoot, reference.replaceAll("\\", "/")));
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function referenceMatchesConfig(projectRoot, reference, configRelative, allowBareReference = false) {
|
|
91
|
+
if (typeof reference !== "string" || reference.trim() === "") return false;
|
|
92
|
+
const normalizedReference = reference.replaceAll("\\", "/");
|
|
93
|
+
if ((!allowBareReference && !normalizedReference.startsWith("."))
|
|
94
|
+
|| normalizedReference.startsWith("/")
|
|
95
|
+
|| /^[A-Za-z]:\//u.test(normalizedReference)) return false;
|
|
96
|
+
const candidate = normalizedReferencePath(projectRoot, normalizedReference);
|
|
97
|
+
if (candidate === ".." || candidate.startsWith("../")) return false;
|
|
98
|
+
const normalizedConfig = configRelative.toLowerCase();
|
|
99
|
+
const normalizedCandidate = candidate.toLowerCase();
|
|
100
|
+
return normalizedConfig === normalizedCandidate
|
|
101
|
+
|| normalizedConfig === normalizedCandidate + ".json"
|
|
102
|
+
|| normalizedConfig === normalizedCandidate + "/tsconfig.json";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isConfigCandidate(file) {
|
|
106
|
+
return file?.kind === "typescript" || file?.kind === "typescript-config";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function localConfigReferences(configRelative, projectFiles) {
|
|
110
|
+
return projectFiles.filter((file) => {
|
|
111
|
+
if (!isConfigCandidate(file)) return false;
|
|
112
|
+
const parsed = parseTypeScriptConfig(file.text, file.name);
|
|
113
|
+
return parsed.valid && (parsed.references
|
|
114
|
+
.some((reference) => referenceMatchesConfig(path.posix.dirname(file.relative), reference, configRelative, true))
|
|
115
|
+
|| parsed.extends.some((reference) => referenceMatchesConfig(
|
|
116
|
+
path.posix.dirname(file.relative),
|
|
117
|
+
reference,
|
|
118
|
+
configRelative,
|
|
119
|
+
)));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function configRelationships(projectFiles) {
|
|
124
|
+
const entries = projectFiles
|
|
125
|
+
.filter(isConfigCandidate)
|
|
126
|
+
.map((file) => ({ file, parsed: parseTypeScriptConfig(file.text, file.name) }))
|
|
127
|
+
.filter(({ parsed }) => parsed.valid);
|
|
128
|
+
const forward = new Map();
|
|
129
|
+
const reverse = new Map();
|
|
130
|
+
for (const { file, parsed } of entries) {
|
|
131
|
+
const relations = [
|
|
132
|
+
...parsed.references.map((value) => ({ value, kind: "reference", allowBare: true })),
|
|
133
|
+
...parsed.extends.map((value) => ({ value, kind: "extends", allowBare: false })),
|
|
134
|
+
];
|
|
135
|
+
for (const relation of relations) {
|
|
136
|
+
const target = entries.find(({ file: candidate }) => referenceMatchesConfig(
|
|
137
|
+
path.posix.dirname(file.relative), relation.value, candidate.relative, relation.allowBare,
|
|
138
|
+
));
|
|
139
|
+
if (!target) continue;
|
|
140
|
+
const sourceRelative = file.relative.toLowerCase();
|
|
141
|
+
const targetRelative = target.file.relative.toLowerCase();
|
|
142
|
+
if (!forward.has(sourceRelative)) forward.set(sourceRelative, []);
|
|
143
|
+
if (!reverse.has(targetRelative)) reverse.set(targetRelative, []);
|
|
144
|
+
forward.get(sourceRelative).push({ relative: targetRelative, kind: relation.kind });
|
|
145
|
+
reverse.get(targetRelative).push({ relative: sourceRelative, kind: relation.kind });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return { entries, forward, reverse };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function resolveTypeScriptConfigGraph(projectFiles = [], directClaims = []) {
|
|
152
|
+
const { entries, forward, reverse } = configRelationships(projectFiles);
|
|
153
|
+
const byRelative = new Map(entries.map((entry) => [entry.file.relative.toLowerCase(), entry]));
|
|
154
|
+
const active = new Set(entries
|
|
155
|
+
.filter(({ file }) => file.name.toLowerCase() === "tsconfig.json")
|
|
156
|
+
.map(({ file }) => file.relative.toLowerCase()));
|
|
157
|
+
for (const claim of directClaims) {
|
|
158
|
+
if (byRelative.has(claim.toLowerCase())) active.add(claim.toLowerCase());
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const queue = [...active];
|
|
162
|
+
for (let index = 0; index < queue.length; index += 1) {
|
|
163
|
+
const current = byRelative.get(queue[index]);
|
|
164
|
+
const parsed = current?.parsed;
|
|
165
|
+
if (!parsed) continue;
|
|
166
|
+
for (const relation of forward.get(queue[index]) ?? []) {
|
|
167
|
+
if (!active.has(relation.relative)) {
|
|
168
|
+
active.add(relation.relative);
|
|
169
|
+
queue.push(relation.relative);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (directClaims.length > 0) {
|
|
173
|
+
for (const relation of reverse.get(queue[index]) ?? []) {
|
|
174
|
+
if (!active.has(relation.relative)) {
|
|
175
|
+
active.add(relation.relative);
|
|
176
|
+
queue.push(relation.relative);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return active;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function resolveTypeScriptConfigOwnershipRoots(projectFiles = [], activeConfigRelatives = new Set(), directClaims = []) {
|
|
185
|
+
const { entries, reverse } = configRelationships(projectFiles);
|
|
186
|
+
const candidates = entries.filter(({ file }) => activeConfigRelatives.has(file.relative.toLowerCase()));
|
|
187
|
+
const hasIndependentConfigSemantics = (parsed) => (parsed.compilerOptions
|
|
188
|
+
&& Object.keys(parsed.compilerOptions).length > 0)
|
|
189
|
+
|| parsed.references.length > 0
|
|
190
|
+
|| parsed.extends.length > 0;
|
|
191
|
+
const roots = new Set(candidates
|
|
192
|
+
.filter(({ file, parsed }) => file.name.toLowerCase() === "tsconfig.json"
|
|
193
|
+
&& (directClaims.length === 0 || hasIndependentConfigSemantics(parsed)))
|
|
194
|
+
.map(({ file }) => file.relative.toLowerCase()));
|
|
195
|
+
for (const claim of directClaims) {
|
|
196
|
+
const relative = claim.toLowerCase();
|
|
197
|
+
const consumers = (reverse.get(relative) ?? [])
|
|
198
|
+
.filter((relation) => relation.kind === "extends")
|
|
199
|
+
.map((relation) => relation.relative);
|
|
200
|
+
if (consumers.length === 0 && candidates.some(({ file }) => file.relative.toLowerCase() === relative)) {
|
|
201
|
+
roots.add(relative);
|
|
202
|
+
}
|
|
203
|
+
const queue = [...consumers];
|
|
204
|
+
const seen = new Set(queue);
|
|
205
|
+
for (let index = 0; index < queue.length; index += 1) {
|
|
206
|
+
const consumer = candidates.find(({ file }) => file.relative.toLowerCase() === queue[index]);
|
|
207
|
+
if (consumer?.file.name.toLowerCase() === "tsconfig.json") roots.add(queue[index]);
|
|
208
|
+
for (const relation of reverse.get(queue[index]) ?? []) {
|
|
209
|
+
if (relation.kind === "extends" && !seen.has(relation.relative)) {
|
|
210
|
+
seen.add(relation.relative);
|
|
211
|
+
queue.push(relation.relative);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
for (const { file, parsed } of candidates) {
|
|
217
|
+
for (const reference of parsed.references) {
|
|
218
|
+
const target = candidates.find(({ file: candidate }) => referenceMatchesConfig(
|
|
219
|
+
path.posix.dirname(file.relative),
|
|
220
|
+
reference,
|
|
221
|
+
candidate.relative,
|
|
222
|
+
true,
|
|
223
|
+
));
|
|
224
|
+
if (target) roots.add(target.file.relative.toLowerCase());
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return roots;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function shouldActivateTypeScriptConfig({
|
|
231
|
+
manifestName,
|
|
232
|
+
manifestRelative,
|
|
233
|
+
projectFiles = [],
|
|
234
|
+
directClaim = false,
|
|
235
|
+
configKind = "typescript",
|
|
236
|
+
activeConfigRelatives = null,
|
|
237
|
+
ownershipConfigRelatives = null,
|
|
238
|
+
}) {
|
|
239
|
+
if (!isConfigCandidate({ kind: configKind }) || manifestName.toLowerCase() === "jsconfig.json") return false;
|
|
240
|
+
if (activeConfigRelatives) {
|
|
241
|
+
return activeConfigRelatives.has(manifestRelative.toLowerCase())
|
|
242
|
+
&& (!ownershipConfigRelatives || ownershipConfigRelatives.has(manifestRelative.toLowerCase()));
|
|
243
|
+
}
|
|
244
|
+
if (manifestName.toLowerCase() === "tsconfig.json" || directClaim) return true;
|
|
245
|
+
return localConfigReferences(manifestRelative, projectFiles).length > 0;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function parseJsonc(text) {
|
|
249
|
+
if (typeof text !== "string" || text.length > 1024 * 1024 || /\r(?!\n)/u.test(text)) return null;
|
|
250
|
+
try {
|
|
251
|
+
return JSON.parse(removeTrailingCommas(maskJsonComments(text)));
|
|
252
|
+
} catch {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function parseTypeScriptConfig(text, fileName = "tsconfig.json") {
|
|
258
|
+
const value = parseJsonc(text);
|
|
259
|
+
if (!isPlainObject(value)) return invalidConfig();
|
|
260
|
+
const references = value.references === undefined
|
|
261
|
+
? []
|
|
262
|
+
: Array.isArray(value.references) && value.references.every((reference) => isPlainObject(reference) && typeof reference.path === "string" && reference.path.trim() !== "")
|
|
263
|
+
? value.references.map((reference) => reference.path)
|
|
264
|
+
: null;
|
|
265
|
+
const extendsValue = value.extends === undefined
|
|
266
|
+
? []
|
|
267
|
+
: typeof value.extends === "string" && value.extends.trim() !== ""
|
|
268
|
+
? [value.extends]
|
|
269
|
+
: Array.isArray(value.extends) && value.extends.every((reference) => typeof reference === "string" && reference.trim() !== "")
|
|
270
|
+
? [...value.extends]
|
|
271
|
+
: undefined;
|
|
272
|
+
const compilerOptions = value.compilerOptions === undefined
|
|
273
|
+
? {}
|
|
274
|
+
: isPlainObject(value.compilerOptions) ? value.compilerOptions : null;
|
|
275
|
+
if (references === null || extendsValue === undefined || compilerOptions === null) return invalidConfig();
|
|
276
|
+
for (const key of ["files", "include", "exclude"]) {
|
|
277
|
+
if (value[key] !== undefined && (!Array.isArray(value[key])
|
|
278
|
+
|| value[key].some((entry) => typeof entry !== "string" || entry.trim() === ""))) return invalidConfig();
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
valid: true,
|
|
282
|
+
references,
|
|
283
|
+
extends: extendsValue,
|
|
284
|
+
compilerOptions,
|
|
285
|
+
fileName,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function inspectTypeScriptProject({
|
|
290
|
+
projectRoot,
|
|
291
|
+
manifestRelative,
|
|
292
|
+
manifestText,
|
|
293
|
+
manifestName,
|
|
294
|
+
targetRoot,
|
|
295
|
+
projectFiles,
|
|
296
|
+
directClaim = false,
|
|
297
|
+
configKind = "typescript",
|
|
298
|
+
activeConfigRelatives = null,
|
|
299
|
+
ownershipConfigRelatives = null,
|
|
300
|
+
}) {
|
|
301
|
+
const parsed = parseTypeScriptConfig(manifestText, manifestName);
|
|
302
|
+
const isConfig = shouldActivateTypeScriptConfig({
|
|
303
|
+
manifestName,
|
|
304
|
+
manifestRelative,
|
|
305
|
+
projectFiles,
|
|
306
|
+
directClaim,
|
|
307
|
+
configKind,
|
|
308
|
+
activeConfigRelatives,
|
|
309
|
+
ownershipConfigRelatives,
|
|
310
|
+
});
|
|
311
|
+
const primary = parsed.valid && isConfig;
|
|
312
|
+
const primarySignals = primary ? [`${manifestRelative}:tsconfig`] : [];
|
|
313
|
+
const supportingSignals = [];
|
|
314
|
+
for (const reference of parsed.references) supportingSignals.push(`${manifestRelative}:reference=${reference}`);
|
|
315
|
+
for (const reference of parsed.extends) supportingSignals.push(`${manifestRelative}:extends=${reference}`);
|
|
316
|
+
const localReferences = parsed.references.filter((reference) => {
|
|
317
|
+
return projectFiles.some((file) => referenceMatchesConfig(
|
|
318
|
+
projectRoot,
|
|
319
|
+
reference,
|
|
320
|
+
path.posix.normalize(file.relative ?? ""),
|
|
321
|
+
true,
|
|
322
|
+
));
|
|
323
|
+
});
|
|
324
|
+
const localExtends = parsed.extends.flatMap((reference) => projectFiles
|
|
325
|
+
.filter((file) => referenceMatchesConfig(path.posix.dirname(manifestRelative), reference, file.relative))
|
|
326
|
+
.map((file) => file.relative.toLowerCase()));
|
|
327
|
+
for (const reference of localReferences) supportingSignals.push(`${manifestRelative}:local-reference=${reference}`);
|
|
328
|
+
for (const reference of parsed.extends) {
|
|
329
|
+
if (projectFiles.some((file) => referenceMatchesConfig(
|
|
330
|
+
path.posix.dirname(manifestRelative),
|
|
331
|
+
reference,
|
|
332
|
+
file.relative,
|
|
333
|
+
))) supportingSignals.push(`${manifestRelative}:local-extends=${reference}`);
|
|
334
|
+
}
|
|
335
|
+
return createLanguageProject({
|
|
336
|
+
kind: "typescript",
|
|
337
|
+
root: projectRoot,
|
|
338
|
+
manifest: manifestRelative,
|
|
339
|
+
frameworks: primary ? ["typescript"] : [],
|
|
340
|
+
primary,
|
|
341
|
+
primarySignals,
|
|
342
|
+
supportingSignals,
|
|
343
|
+
internal: { ...parsed, valid: parsed.valid, localReferences, localExtends },
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export function isDeclarationFile(fileName) {
|
|
348
|
+
return typeof fileName === "string" && /\.d\.(?:ts|mts|cts)$/iu.test(fileName);
|
|
349
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
const DEFAULT_XML_MAX_BYTES = 1024 * 1024;
|
|
2
|
+
|
|
3
|
+
function findTagEnd(text, start) {
|
|
4
|
+
let quote = null;
|
|
5
|
+
for (let index = start; index < text.length; index += 1) {
|
|
6
|
+
const character = text[index];
|
|
7
|
+
if (quote) {
|
|
8
|
+
if (character === quote) quote = null;
|
|
9
|
+
} else if (character === "\"" || character === "'") {
|
|
10
|
+
quote = character;
|
|
11
|
+
} else if (character === ">") {
|
|
12
|
+
return index;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return -1;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function parseAttributes(text) {
|
|
19
|
+
const attributes = {};
|
|
20
|
+
let index = 0;
|
|
21
|
+
while (index < text.length) {
|
|
22
|
+
while (index < text.length && /\s/u.test(text[index])) index += 1;
|
|
23
|
+
if (index >= text.length) break;
|
|
24
|
+
const name = text.slice(index).match(/^[A-Za-z_][A-Za-z0-9_.:-]*/u)?.[0];
|
|
25
|
+
if (!name) return null;
|
|
26
|
+
index += name.length;
|
|
27
|
+
while (index < text.length && /\s/u.test(text[index])) index += 1;
|
|
28
|
+
if (text[index] !== "=") return null;
|
|
29
|
+
index += 1;
|
|
30
|
+
while (index < text.length && /\s/u.test(text[index])) index += 1;
|
|
31
|
+
const quote = text[index];
|
|
32
|
+
if (quote !== "\"" && quote !== "'") return null;
|
|
33
|
+
index += 1;
|
|
34
|
+
const end = text.indexOf(quote, index);
|
|
35
|
+
if (end < 0 || Object.prototype.hasOwnProperty.call(attributes, name)) return null;
|
|
36
|
+
attributes[name] = text.slice(index, end);
|
|
37
|
+
index = end + 1;
|
|
38
|
+
}
|
|
39
|
+
return attributes;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function appendText(stack, value) {
|
|
43
|
+
if (value === "") return true;
|
|
44
|
+
const current = stack[stack.length - 1];
|
|
45
|
+
if (!current) return value.trim() === "";
|
|
46
|
+
current.textParts.push(value);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function specialToken(text, start, stack) {
|
|
51
|
+
if (text.startsWith("<!--", start)) {
|
|
52
|
+
const end = text.indexOf("-->", start + 4);
|
|
53
|
+
return end < 0 ? { valid: false } : { valid: true, next: end + 3 };
|
|
54
|
+
}
|
|
55
|
+
if (text.startsWith("<![CDATA[", start)) {
|
|
56
|
+
const end = text.indexOf("]]>", start + 9);
|
|
57
|
+
if (end < 0 || stack.length === 0) return { valid: false };
|
|
58
|
+
stack[stack.length - 1].textParts.push(text.slice(start + 9, end));
|
|
59
|
+
return { valid: true, next: end + 3 };
|
|
60
|
+
}
|
|
61
|
+
if (text.startsWith("<?", start)) {
|
|
62
|
+
const end = text.indexOf("?>", start + 2);
|
|
63
|
+
return end < 0 ? { valid: false } : { valid: true, next: end + 2 };
|
|
64
|
+
}
|
|
65
|
+
if (text.startsWith("<!", start)) return { valid: false };
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function closeNode(raw, state) {
|
|
70
|
+
const closing = raw.slice(1).trim().match(/^([A-Za-z_][A-Za-z0-9_.:-]*)\s*$/u)?.[1];
|
|
71
|
+
const open = state.stack.pop();
|
|
72
|
+
if (!closing || !open || open.name !== closing) return false;
|
|
73
|
+
open.text = open.textParts.join("");
|
|
74
|
+
if (state.stack.length === 0) {
|
|
75
|
+
if (state.rootClosed || open !== state.root) return false;
|
|
76
|
+
state.rootClosed = true;
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function openNode(raw, state) {
|
|
82
|
+
const selfClosing = /\/\s*$/u.test(raw);
|
|
83
|
+
const body = selfClosing ? raw.replace(/\/\s*$/u, "").trimEnd() : raw;
|
|
84
|
+
const name = body.match(/^([A-Za-z_][A-Za-z0-9_.:-]*)/u)?.[1];
|
|
85
|
+
if (!name || (state.stack.length === 0 && (state.root || state.rootClosed))) return false;
|
|
86
|
+
if (!state.root && state.expectedRoot && name !== state.expectedRoot) return false;
|
|
87
|
+
const attributes = parseAttributes(body.slice(name.length).trim());
|
|
88
|
+
if (!attributes) return false;
|
|
89
|
+
const node = { name, attributes, textParts: [], children: [], text: "" };
|
|
90
|
+
state.nodes.push(node);
|
|
91
|
+
if (state.stack.length > 0) state.stack[state.stack.length - 1].children.push(node);
|
|
92
|
+
if (!state.root) state.root = node;
|
|
93
|
+
if (!selfClosing) state.stack.push(node);
|
|
94
|
+
else if (node === state.root) state.rootClosed = true;
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function parseXmlStructure(text, expectedRoot = null, maxBytes = DEFAULT_XML_MAX_BYTES) {
|
|
99
|
+
if (typeof text !== "string" || text.length > maxBytes || /\r(?!\n)/u.test(text)) return null;
|
|
100
|
+
const state = { nodes: [], stack: [], root: null, rootClosed: false, expectedRoot };
|
|
101
|
+
let index = 0;
|
|
102
|
+
while (index < text.length) {
|
|
103
|
+
const tagStart = text.indexOf("<", index);
|
|
104
|
+
if (tagStart < 0) break;
|
|
105
|
+
if (!appendText(state.stack, text.slice(index, tagStart))) return null;
|
|
106
|
+
const special = specialToken(text, tagStart, state.stack);
|
|
107
|
+
if (special) {
|
|
108
|
+
if (!special.valid) return null;
|
|
109
|
+
index = special.next;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const tagEnd = findTagEnd(text, tagStart + 1);
|
|
113
|
+
if (tagEnd < 0) return null;
|
|
114
|
+
const raw = text.slice(tagStart + 1, tagEnd).trim();
|
|
115
|
+
if (raw === "") return null;
|
|
116
|
+
const valid = raw.startsWith("/") ? closeNode(raw, state) : openNode(raw, state);
|
|
117
|
+
if (!valid) return null;
|
|
118
|
+
index = tagEnd + 1;
|
|
119
|
+
}
|
|
120
|
+
if (!appendText(state.stack, text.slice(index))) return null;
|
|
121
|
+
if (!state.root || state.stack.length > 0 || !state.rootClosed) return null;
|
|
122
|
+
return { root: state.root, nodes: state.nodes };
|
|
123
|
+
}
|