chainlesschain 0.163.2 → 0.163.4
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/package.json +3 -2
- package/scripts/command-lifecycle-report.mjs +330 -38
- package/src/assets/web-panel/assets/{AIOps-DnGsmsqJ.js → AIOps-DB5X7qqV.js} +1 -1
- package/src/assets/web-panel/assets/{ActionButton-CrO6fcQ7.js → ActionButton-DxHRtKLk.js} +1 -1
- package/src/assets/web-panel/assets/{Analytics-DsBdOaP2.js → Analytics-DIH5QrxT.js} +3 -3
- package/src/assets/web-panel/assets/{AppLayout-DmQslKUB.js → AppLayout-uu0oxVz7.js} +5 -5
- package/src/assets/web-panel/assets/{Artifacts-BvyrT_QQ.js → Artifacts-BNSMiW9Y.js} +1 -1
- package/src/assets/web-panel/assets/{Audit-prndoLiC.js → Audit-DcAzB4_x.js} +1 -1
- package/src/assets/web-panel/assets/{BackgroundAgents-DOTFyA55.js → BackgroundAgents-ClSsvjIc.js} +1 -1
- package/src/assets/web-panel/assets/{Backup-9zOnPp_8.js → Backup-BMciY0js.js} +1 -1
- package/src/assets/web-panel/assets/{BaseInput-C4PC6sAG.js → BaseInput-Hn3b57Sh.js} +1 -1
- package/src/assets/web-panel/assets/{Chat-BBAF-W9v.js → Chat-CoSlC53V.js} +6 -6
- package/src/assets/web-panel/assets/{ChatBubbleRenderer-CWhvvloO.js → ChatBubbleRenderer-CFevJ3So.js} +1 -1
- package/src/assets/web-panel/assets/{Checkbox-BI9iculd.js → Checkbox-B4L4Eu4w.js} +1 -1
- package/src/assets/web-panel/assets/{Codegen-3DP9BCKb.js → Codegen-C77FgRxF.js} +1 -1
- package/src/assets/web-panel/assets/{Col-df-2TnJX.js → Col-BrLtJLH3.js} +1 -1
- package/src/assets/web-panel/assets/{Community-CHs8gG_A.js → Community-Be7pXXhA.js} +1 -1
- package/src/assets/web-panel/assets/{Compact-CDfF6q7V.js → Compact-DuGPecjo.js} +1 -1
- package/src/assets/web-panel/assets/{Compliance-CthrDnO7.js → Compliance-CzS66mjP.js} +1 -1
- package/src/assets/web-panel/assets/{Cowork-HjIqsWJw.js → Cowork-CEhjCoyK.js} +2 -2
- package/src/assets/web-panel/assets/{Cron-BKc196fb.js → Cron-D7ir8Vja.js} +2 -2
- package/src/assets/web-panel/assets/{Crosschain-CJnjdc8W.js → Crosschain-BcXmuLxf.js} +1 -1
- package/src/assets/web-panel/assets/{DID-BHUy-8Q2.js → DID-Cto1dtBy.js} +2 -2
- package/src/assets/web-panel/assets/{Dashboard-QDB7sKeY.js → Dashboard-B7KjgwOX.js} +2 -2
- package/src/assets/web-panel/assets/{Dropdown-DStcVK11.js → Dropdown-COMqxijg.js} +1 -1
- package/src/assets/web-panel/assets/{EmailListRenderer-BwSUskMJ.js → EmailListRenderer-BZrya-nf.js} +1 -1
- package/src/assets/web-panel/assets/{FamilyGuardDashboard-DgF_kurT.js → FamilyGuardDashboard-AmVeMq0J.js} +1 -1
- package/src/assets/web-panel/assets/{Federation-k8wvv-wN.js → Federation-kZU8c-27.js} +1 -1
- package/src/assets/web-panel/assets/{FormItemContext-C11qoq2r.js → FormItemContext-sOV22v2-.js} +1 -1
- package/src/assets/web-panel/assets/{GenericCardRenderer-BIIpr9kg.js → GenericCardRenderer-BUuLtGCz.js} +1 -1
- package/src/assets/web-panel/assets/{Git-BlA1exTe.js → Git-K-ZxsLsH.js} +2 -2
- package/src/assets/web-panel/assets/{Governance-CYa1ZYM4.js → Governance-D3OEnawI.js} +1 -1
- package/src/assets/web-panel/assets/{Inference-6_kKVIt6.js → Inference-Bll0gRmP.js} +1 -1
- package/src/assets/web-panel/assets/{KnowledgeGraph-CB4AwxYy.js → KnowledgeGraph-DBXwjr53.js} +1 -1
- package/src/assets/web-panel/assets/{Logs-xf7nHFAe.js → Logs-CEzPwEfP.js} +2 -2
- package/src/assets/web-panel/assets/{MarkdownRenderer-Bf3DEqP6.js → MarkdownRenderer-CLmCbHx6.js} +1 -1
- package/src/assets/web-panel/assets/{Marketplace-aPg8e4Jt.js → Marketplace-BYyRyOPz.js} +1 -1
- package/src/assets/web-panel/assets/{McpTools-C-TNlVUf.js → McpTools-DVu8w_rM.js} +5 -5
- package/src/assets/web-panel/assets/{Memory-DMTPIpGa.js → Memory-tenuZ_hO.js} +2 -2
- package/src/assets/web-panel/assets/{MobileBridge-BxyvveRq.js → MobileBridge-Bxgy4ozS.js} +1 -1
- package/src/assets/web-panel/assets/{MobileProjects-DyPCrAyQ.js → MobileProjects-Dgx1D6sc.js} +1 -1
- package/src/assets/web-panel/assets/{Mtc-BdhqNXp6.js → Mtc-CRfJ8r8j.js} +5 -5
- package/src/assets/web-panel/assets/{MtcAudit-Cz-HFxzW.js → MtcAudit-BBJq4Aiz.js} +5 -5
- package/src/assets/web-panel/assets/{Multisig-CIUaFNsx.js → Multisig-B3ii9lai.js} +3 -3
- package/src/assets/web-panel/assets/{NLProgramming-CWe6-J17.js → NLProgramming-FIi-Cubv.js} +1 -1
- package/src/assets/web-panel/assets/{Notes-vYAKeviU.js → Notes-DIq8MPf1.js} +3 -3
- package/src/assets/web-panel/assets/{NotificationSettings-DQL9-2d7.js → NotificationSettings-BFOD9eGd.js} +1 -1
- package/src/assets/web-panel/assets/{OrderTableRenderer-DnXj1jBn.js → OrderTableRenderer--41u7vre.js} +1 -1
- package/src/assets/web-panel/assets/{Organization-B3spU8AN.js → Organization-B_0AWztW.js} +2 -2
- package/src/assets/web-panel/assets/{Overflow-DgEwo7vm.js → Overflow-B6I7TuXv.js} +1 -1
- package/src/assets/web-panel/assets/{P2P-hujMRH8X.js → P2P-Cdf6qens.js} +2 -2
- package/src/assets/web-panel/assets/{PdhVaultBrowser-C_qhdg9h.js → PdhVaultBrowser-B_0FcQJY.js} +3 -3
- package/src/assets/web-panel/assets/{Permissions-vp7LMcSf.js → Permissions-6nFBjsQp.js} +4 -4
- package/src/assets/web-panel/assets/{PersonalDataHub-Dxr5Hgnq.js → PersonalDataHub-nQYV7xpn.js} +3 -3
- package/src/assets/web-panel/assets/{Pipeline-DEPZqqH4.js → Pipeline-CBVod5jm.js} +1 -1
- package/src/assets/web-panel/assets/{Privacy-DTpDiKPe.js → Privacy-D52jW1Nt.js} +1 -1
- package/src/assets/web-panel/assets/{ProjectInit-jDJsRWmF.js → ProjectInit-Bdx3BYhf.js} +2 -2
- package/src/assets/web-panel/assets/{ProjectSettings-Dhor5RWH.js → ProjectSettings-b7xr4DUa.js} +2 -2
- package/src/assets/web-panel/assets/Projects-DhfifCKx.js +1 -0
- package/src/assets/web-panel/assets/Providers-Dy_PgDMr.js +1 -0
- package/src/assets/web-panel/assets/{QrScannerModal-CjIw-wIb.js → QrScannerModal-FQKSmc3e.js} +1 -1
- package/src/assets/web-panel/assets/{QuickAsk-DX6jcqoi.js → QuickAsk-CSTrhzcF.js} +1 -1
- package/src/assets/web-panel/assets/{Recommend-DopXkhar.js → Recommend-CxMABOUh.js} +1 -1
- package/src/assets/web-panel/assets/{RemoteSession-BLvqJE9c.js → RemoteSession-Cv2T8yRa.js} +2 -2
- package/src/assets/web-panel/assets/{Reputation-DhRj2Me7.js → Reputation-CE1k4Vvu.js} +1 -1
- package/src/assets/web-panel/assets/{Row-DQ53vuPq.js → Row-BNgNcAV_.js} +1 -1
- package/src/assets/web-panel/assets/{RssFeed-BslvwiBD.js → RssFeed-5ZiBZ3ph.js} +3 -3
- package/src/assets/web-panel/assets/{Search-DJKBDNKZ.js → Search-mDOIF1UV.js} +1 -1
- package/src/assets/web-panel/assets/{Security-COhpxHER.js → Security-CtuCTC-e.js} +3 -3
- package/src/assets/web-panel/assets/{Services-dbulD6cI.js → Services-DxFO2Zg7.js} +2 -2
- package/src/assets/web-panel/assets/{Skeleton-ciIVNfhC.js → Skeleton-CKmKkH9y.js} +1 -1
- package/src/assets/web-panel/assets/{Skills-AiRTkTCk.js → Skills-Bmzl9qkk.js} +1 -1
- package/src/assets/web-panel/assets/{Sla-DDMcWYBc.js → Sla-9RqaWHOC.js} +1 -1
- package/src/assets/web-panel/assets/{SpeechSettings-BPxWbpXO.js → SpeechSettings-CGdzFIYw.js} +1 -1
- package/src/assets/web-panel/assets/{SyncSettings-BoEOXCyg.js → SyncSettings-CvNuCeBT.js} +2 -2
- package/src/assets/web-panel/assets/Tasks-B_sLdcnw.js +1 -0
- package/src/assets/web-panel/assets/{Templates-UowY8Ixe.js → Templates-D9_YijdK.js} +1 -1
- package/src/assets/web-panel/assets/{Tenant-DauSLQXN.js → Tenant-Cuc7BUSP.js} +1 -1
- package/src/assets/web-panel/assets/{Terminal-DBj7Jneq.js → Terminal-6VYRnJgd.js} +2 -2
- package/src/assets/web-panel/assets/{TimelineRenderer-UMrPwoZC.js → TimelineRenderer-GtK8xd8Y.js} +1 -1
- package/src/assets/web-panel/assets/{Tokens-4ea96-bn.js → Tokens-BZ94Lt5W.js} +1 -1
- package/src/assets/web-panel/assets/{Trigger--ISCqt4b.js → Trigger-DEcZgDaX.js} +1 -1
- package/src/assets/web-panel/assets/{Trust-D4GVimRJ.js → Trust-CC3z9THe.js} +1 -1
- package/src/assets/web-panel/assets/{UkeySign-Ci-nXRQc.js → UkeySign-Cvtw8cV3.js} +1 -1
- package/src/assets/web-panel/assets/{VideoEditing-Cjk7mKPw.js → VideoEditing-CL4e6Bn3.js} +1 -1
- package/src/assets/web-panel/assets/{Wallet-BgOLOO3Z.js → Wallet-ycEemP27.js} +2 -2
- package/src/assets/web-panel/assets/{WebAuthn-CCX0K29i.js → WebAuthn-DgZ4X_aw.js} +5 -5
- package/src/assets/web-panel/assets/{WorkflowEditor-Ch3zgIsE.js → WorkflowEditor-D0qMhSv7.js} +1 -1
- package/src/assets/web-panel/assets/{chat-DKOU_2Vb.js → chat-DsAg01Zn.js} +1 -1
- package/src/assets/web-panel/assets/{colors-6OVo4u3C.js → colors-B0oqAhrQ.js} +1 -1
- package/src/assets/web-panel/assets/{compact-item-BH78Hlsq.js → compact-item-CmX0Rabz.js} +1 -1
- package/src/assets/web-panel/assets/{createContext-CFRpSbbC.js → createContext-Be4cn1Ub.js} +1 -1
- package/src/assets/web-panel/assets/devWarning-CP2qzG6D.js +1 -0
- package/src/assets/web-panel/assets/{hasIn-DNQhgcVi.js → hasIn-D5v12kVq.js} +1 -1
- package/src/assets/web-panel/assets/{index-BkN3iTTp.js → index-0eWLkQKT.js} +1 -1
- package/src/assets/web-panel/assets/{index-CkNEMANR.js → index-301ogenv.js} +1 -1
- package/src/assets/web-panel/assets/{index-D3RtTT4K.js → index-5LGY0JbG.js} +1 -1
- package/src/assets/web-panel/assets/{index-dDkoL4Pe.js → index-7VKDn-t1.js} +1 -1
- package/src/assets/web-panel/assets/{index-BHa4WyTZ.js → index-8WvYGnON.js} +1 -1
- package/src/assets/web-panel/assets/{index-CaYqY35Z.js → index-B1YGjOWC.js} +1 -1
- package/src/assets/web-panel/assets/{index-bKm3TUDp.js → index-B3mUHuVc.js} +1 -1
- package/src/assets/web-panel/assets/{index--rq90rL9.js → index-BA4oulaP.js} +1 -1
- package/src/assets/web-panel/assets/{index-DaDgdJYj.js → index-BBREnn1V.js} +1 -1
- package/src/assets/web-panel/assets/{index-B_Z-YoxX.js → index-BBvyHguy.js} +1 -1
- package/src/assets/web-panel/assets/{index-CkqHgG-b.js → index-BKmsS2jj.js} +1 -1
- package/src/assets/web-panel/assets/{index-CrcLHN2F.js → index-BRIqLy1m.js} +1 -1
- package/src/assets/web-panel/assets/{index-BaVE6E8y.js → index-BXGSbelg.js} +3 -3
- package/src/assets/web-panel/assets/{index-BYrnm5Ob.js → index-BaE0Ui07.js} +1 -1
- package/src/assets/web-panel/assets/{index-BDSR7ecX.js → index-BoAIAZ5C.js} +1 -1
- package/src/assets/web-panel/assets/{index-CNuCHHQT.js → index-BqltWojQ.js} +1 -1
- package/src/assets/web-panel/assets/{index-Eag8e_mA.js → index-CGiIyTZw.js} +1 -1
- package/src/assets/web-panel/assets/index-CKB0vbJU.js +1 -0
- package/src/assets/web-panel/assets/{index-BepBTpa8.js → index-CZ2wUEgr.js} +1 -1
- package/src/assets/web-panel/assets/{index-HNGGVF6o.js → index-CZpfzcuy.js} +1 -1
- package/src/assets/web-panel/assets/{index-AuBhLqOm.js → index-Ckrk3pIV.js} +1 -1
- package/src/assets/web-panel/assets/{index-7TC3S8SN.js → index-CqoCZ5vR.js} +1 -1
- package/src/assets/web-panel/assets/{index-CFPJ8yzF.js → index-CyWBir7N.js} +1 -1
- package/src/assets/web-panel/assets/{index-Bb6kDrUx.js → index-CyyGTMKd.js} +1 -1
- package/src/assets/web-panel/assets/{index-DKN2noIF.js → index-D0BSkxYQ.js} +1 -1
- package/src/assets/web-panel/assets/{index-Du0rnksC.js → index-D5YVopyr.js} +1 -1
- package/src/assets/web-panel/assets/{index-BxRy5LPR.js → index-DKUBj36_.js} +1 -1
- package/src/assets/web-panel/assets/{index-sm7W8AXy.js → index-DM724NgU.js} +1 -1
- package/src/assets/web-panel/assets/{index-BujsOZfc.js → index-DQRsQen0.js} +1 -1
- package/src/assets/web-panel/assets/index-DgoXmwqB.js +1 -0
- package/src/assets/web-panel/assets/{index-Y9MJv28e.js → index-DikJ0Qqe.js} +1 -1
- package/src/assets/web-panel/assets/{index-caye7AOE.js → index-Dn2dSdXx.js} +1 -1
- package/src/assets/web-panel/assets/{index-cdf7fd7d.js → index-DyUaFWEk.js} +1 -1
- package/src/assets/web-panel/assets/{index-BnnXQot_.js → index-Hfe_dEGx.js} +1 -1
- package/src/assets/web-panel/assets/{index-DvHGtNnF.js → index-_JjRLSD0.js} +1 -1
- package/src/assets/web-panel/assets/{index-nbfHl1oV.js → index-frSojpHj.js} +1 -1
- package/src/assets/web-panel/assets/{index-Cu3L4r5l.js → index-jK3ax6Z6.js} +1 -1
- package/src/assets/web-panel/assets/{index-D84id2Y4.js → index-qMuZ-NRR.js} +1 -1
- package/src/assets/web-panel/assets/{index-hPY0fFrd.js → index-q_fQ94SE.js} +1 -1
- package/src/assets/web-panel/assets/{initDefaultProps-BZ9w6jPL.js → initDefaultProps-D4VdhJ9_.js} +1 -1
- package/src/assets/web-panel/assets/{motion-BnwUiOho.js → motion-CZJA0MQW.js} +1 -1
- package/src/assets/web-panel/assets/{move-ByUKJnNm.js → move-BqEs7Sb1.js} +1 -1
- package/src/assets/web-panel/assets/{mtc-parser-V7yK1KEl.js → mtc-parser-BpHEtsu1.js} +1 -1
- package/src/assets/web-panel/assets/{omit-BEXBBt4j.js → omit-BW94tY9W.js} +1 -1
- package/src/assets/web-panel/assets/{pickAttrs-qDm_46Gi.js → pickAttrs-BnBjPWP9.js} +1 -1
- package/src/assets/web-panel/assets/{placementArrow-BIbhEo_0.js → placementArrow-uUkjQ_hu.js} +1 -1
- package/src/assets/web-panel/assets/{responsiveObserve-NbPbDgA_.js → responsiveObserve-B-klUqXK.js} +1 -1
- package/src/assets/web-panel/assets/{slide-BnUWglZ0.js → slide-CvY1cNAs.js} +1 -1
- package/src/assets/web-panel/assets/{statusUtils-BX8_wZI5.js → statusUtils-Ix5sEDT2.js} +1 -1
- package/src/assets/web-panel/assets/{styleChecker-BV4OycQX.js → styleChecker-C8qcXOne.js} +1 -1
- package/src/assets/web-panel/assets/{useFlexGapSupport-Od_H5llY.js → useFlexGapSupport-KNF6Kj8f.js} +1 -1
- package/src/assets/web-panel/assets/{useFs-BWI-PyiX.js → useFs-DGJr345n.js} +1 -1
- package/src/assets/web-panel/assets/{usePersonalDataHub-BqMuhG3g.js → usePersonalDataHub-BboN99rI.js} +1 -1
- package/src/assets/web-panel/assets/{vnode-DNd23z6f.js → vnode-Bgt8Vgte.js} +1 -1
- package/src/assets/web-panel/assets/{zoom-Cjaxnm7l.js → zoom-BhJtErgt.js} +1 -1
- package/src/assets/web-panel/index.html +1 -1
- package/src/command-help-index.json +2 -2
- package/src/commands/agenda.js +6 -5
- package/src/commands/agent.js +237 -75
- package/src/commands/agents.js +6 -0
- package/src/commands/artifacts.js +98 -0
- package/src/commands/background-session.js +11 -1
- package/src/commands/mcp.js +218 -31
- package/src/commands/review.js +221 -22
- package/src/commands/session-mcp-recovery.js +5 -5
- package/src/data/changelog.json +27 -1
- package/src/gateways/ws/ws-agent-handler.js +492 -14
- package/src/harness/jsonl-session-store.js +39 -1
- package/src/harness/mcp-client.js +414 -71
- package/src/harness/prompt-compressor.js +24 -0
- package/src/harness/provider-backed-compaction.js +205 -0
- package/src/harness/worktree-isolator.js +26 -3
- package/src/lib/agent-worktree.js +41 -2
- package/src/lib/app-preview.js +3 -0
- package/src/lib/background-agent-supervisor.js +790 -186
- package/src/lib/background-command-argv.js +318 -0
- package/src/lib/background-launch-profile.js +1 -0
- package/src/lib/background-phase-reporter.js +22 -2
- package/src/lib/background-worktree-policy.js +109 -0
- package/src/lib/command-lifecycle-report.js +1340 -145
- package/src/lib/cowork-mcp-tools.js +51 -6
- package/src/lib/cowork-task-runner.js +1 -0
- package/src/lib/delivery-coordinator.js +30 -5
- package/src/lib/delivery-production-adapter.js +1777 -0
- package/src/lib/delivery-production-runner.js +240 -0
- package/src/lib/durable-security-store.js +29 -17
- package/src/lib/execution-trace/trace-context.cjs +179 -0
- package/src/lib/execution-trace/trace-context.js +8 -175
- package/src/lib/mcp-call-ledger-store.js +58 -6
- package/src/lib/mcp-headers-helper.js +76 -8
- package/src/lib/mcp-host-recovery-runtime.js +2 -0
- package/src/lib/mcp-ledger-recovery-admission.js +39 -3
- package/src/lib/mcp-recovery-adjudication.js +256 -19
- package/src/lib/mcp-sandbox-policy.js +184 -0
- package/src/lib/mcp-stdio-capsule-builder-worker.cjs +495 -0
- package/src/lib/mcp-stdio-executable-identity.js +33 -2
- package/src/lib/mcp-stdio-execution-authority.js +163 -0
- package/src/lib/mcp-stdio-immutable-vfs-resolver.cjs +830 -0
- package/src/lib/mcp-stdio-package-materialization.js +721 -333
- package/src/lib/mcp-stdio-workspace-authority.js +252 -0
- package/src/lib/observability/index.js +2 -0
- package/src/lib/observability/otlp-exporter.js +2 -2
- package/src/lib/packer/pack-update-applier.js +8 -1
- package/src/lib/packer/pkg-config-generator.js +34 -0
- package/src/lib/plugin-runtime/sandbox-policy.js +42 -2
- package/src/lib/process-execution-broker/index.js +213 -109
- package/src/lib/process-execution-broker/platform-sandbox.js +1024 -169
- package/src/lib/process-tree-termination.js +17 -1
- package/src/lib/scheduler-kernel/contract.js +315 -0
- package/src/lib/scheduler-kernel/store.js +1345 -0
- package/src/lib/session-host-lease.js +403 -30
- package/src/lib/session-persistence-failure.js +4 -1
- package/src/lib/skill-mcp.js +65 -4
- package/src/lib/sub-agent-context.js +8 -0
- package/src/lib/web-fetch.js +4 -4
- package/src/repl/agent-repl.js +285 -38
- package/src/runtime/agent-core.js +1044 -90
- package/src/runtime/agent-runtime.js +61 -8
- package/src/runtime/headless-runner.js +260 -166
- package/src/runtime/headless-stream.js +254 -25
- package/src/runtime/mcp-config.js +235 -28
- package/src/workers/background-agent-worker.js +492 -71
- package/src/assets/web-panel/assets/Projects-08xUDZGt.js +0 -1
- package/src/assets/web-panel/assets/Providers-BGs47s1a.js +0 -1
- package/src/assets/web-panel/assets/Tasks-CEYFJwmr.js +0 -1
- package/src/assets/web-panel/assets/devWarning-DEMblAIm.js +0 -1
- package/src/assets/web-panel/assets/index-BTvq-36D.js +0 -1
- package/src/assets/web-panel/assets/index-hUpvk6Jl.js +0 -1
|
@@ -0,0 +1,830 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const { isBuiltin } = require("node:module");
|
|
5
|
+
|
|
6
|
+
const posix = path.posix;
|
|
7
|
+
const OWN = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
|
|
8
|
+
const NO_MATCH = Symbol("no-match");
|
|
9
|
+
const BLOCKED = Symbol("blocked");
|
|
10
|
+
const DEFAULT_EXTENSIONS = Object.freeze([".js", ".json", ".mjs", ".cjs"]);
|
|
11
|
+
const SAFE_LOADERS = Object.freeze(
|
|
12
|
+
new Map([
|
|
13
|
+
["", "js"],
|
|
14
|
+
[".js", "js"],
|
|
15
|
+
[".cjs", "js"],
|
|
16
|
+
[".mjs", "js"],
|
|
17
|
+
[".json", "json"],
|
|
18
|
+
]),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
class VfsResolutionError extends Error {
|
|
22
|
+
constructor(code, message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "VfsResolutionError";
|
|
25
|
+
this.code = code;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class InvalidPackageTargetError extends VfsResolutionError {
|
|
30
|
+
constructor(message) {
|
|
31
|
+
super("ERR_INVALID_PACKAGE_TARGET", message);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function fail(code, message) {
|
|
36
|
+
throw new VfsResolutionError(code, message);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function assertPortableAbsolute(value, label) {
|
|
40
|
+
if (
|
|
41
|
+
typeof value !== "string" ||
|
|
42
|
+
!value.startsWith("/") ||
|
|
43
|
+
value.includes("\\") ||
|
|
44
|
+
value.includes("\0") ||
|
|
45
|
+
value.includes("?") ||
|
|
46
|
+
value.includes("#") ||
|
|
47
|
+
(value !== "/" && value.endsWith("/")) ||
|
|
48
|
+
posix.normalize(value) !== value
|
|
49
|
+
) {
|
|
50
|
+
fail(
|
|
51
|
+
"ERR_INVALID_VFS_PATH",
|
|
52
|
+
`${label} is not a canonical POSIX absolute path: ${String(value)}`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isWithin(root, candidate) {
|
|
59
|
+
return (
|
|
60
|
+
candidate === root ||
|
|
61
|
+
(root === "/"
|
|
62
|
+
? candidate.startsWith("/")
|
|
63
|
+
: candidate.startsWith(`${root}/`))
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function assertWithin(root, candidate, label) {
|
|
68
|
+
if (!isWithin(root, candidate)) {
|
|
69
|
+
fail(
|
|
70
|
+
"ERR_VFS_ESCAPE",
|
|
71
|
+
`${label} escapes immutable VFS root ${root}: ${candidate}`,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return candidate;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function deepFreeze(value) {
|
|
78
|
+
if (!value || typeof value !== "object" || Object.isFrozen(value))
|
|
79
|
+
return value;
|
|
80
|
+
for (const child of Object.values(value)) deepFreeze(child);
|
|
81
|
+
return Object.freeze(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function validatePackageJson(json, packageJsonPath) {
|
|
85
|
+
if (!json || typeof json !== "object" || Array.isArray(json)) {
|
|
86
|
+
fail(
|
|
87
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
88
|
+
`${packageJsonPath} must contain a JSON object`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (OWN(json, "name") && typeof json.name !== "string") {
|
|
92
|
+
fail(
|
|
93
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
94
|
+
`${packageJsonPath} has a non-string name`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (OWN(json, "main") && typeof json.main !== "string") {
|
|
98
|
+
fail(
|
|
99
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
100
|
+
`${packageJsonPath} has a non-string main`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
if (OWN(json, "type") && json.type !== "module" && json.type !== "commonjs") {
|
|
104
|
+
fail(
|
|
105
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
106
|
+
`${packageJsonPath} has an unsupported type`,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return deepFreeze(json);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function validateSpecifier(specifier) {
|
|
113
|
+
if (
|
|
114
|
+
typeof specifier !== "string" ||
|
|
115
|
+
specifier.length === 0 ||
|
|
116
|
+
specifier.includes("\0") ||
|
|
117
|
+
specifier.includes("\\") ||
|
|
118
|
+
specifier.includes("?") ||
|
|
119
|
+
specifier.indexOf("#", 1) !== -1 ||
|
|
120
|
+
/%2f|%5c/i.test(specifier)
|
|
121
|
+
) {
|
|
122
|
+
fail(
|
|
123
|
+
"ERR_INVALID_MODULE_SPECIFIER",
|
|
124
|
+
`invalid module specifier: ${String(specifier)}`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
if (
|
|
128
|
+
/^[A-Za-z][A-Za-z+.-]*:/.test(specifier) &&
|
|
129
|
+
!specifier.startsWith("node:")
|
|
130
|
+
) {
|
|
131
|
+
fail("ERR_UNSUPPORTED_SCHEME", `unsupported module scheme: ${specifier}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function parsePackageSpecifier(specifier) {
|
|
136
|
+
const parts = specifier.split("/");
|
|
137
|
+
let packageName;
|
|
138
|
+
let remainder;
|
|
139
|
+
if (specifier.startsWith("@")) {
|
|
140
|
+
if (parts.length < 2 || !parts[0] || !parts[1]) {
|
|
141
|
+
fail(
|
|
142
|
+
"ERR_INVALID_MODULE_SPECIFIER",
|
|
143
|
+
`invalid scoped package specifier: ${specifier}`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
packageName = `${parts[0]}/${parts[1]}`;
|
|
147
|
+
remainder = parts.slice(2);
|
|
148
|
+
} else {
|
|
149
|
+
packageName = parts[0];
|
|
150
|
+
remainder = parts.slice(1);
|
|
151
|
+
}
|
|
152
|
+
if (
|
|
153
|
+
!packageName ||
|
|
154
|
+
packageName === "." ||
|
|
155
|
+
packageName === ".." ||
|
|
156
|
+
packageName.includes("%") ||
|
|
157
|
+
remainder.some((part) => !part || part === "." || part === "..")
|
|
158
|
+
) {
|
|
159
|
+
fail(
|
|
160
|
+
"ERR_INVALID_MODULE_SPECIFIER",
|
|
161
|
+
`invalid package specifier: ${specifier}`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
return Object.freeze({
|
|
165
|
+
packageName,
|
|
166
|
+
subpath: remainder.length ? `./${remainder.join("/")}` : ".",
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function conditionForKind(kind) {
|
|
171
|
+
if (kind === "require-call" || kind === "require-resolve") return "require";
|
|
172
|
+
return "import";
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function patternCompare(left, right) {
|
|
176
|
+
const leftBase = left.indexOf("*");
|
|
177
|
+
const rightBase = right.indexOf("*");
|
|
178
|
+
if (leftBase !== rightBase) return rightBase - leftBase;
|
|
179
|
+
return right.length - left.length;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
class ImmutableVfsResolver {
|
|
183
|
+
#root;
|
|
184
|
+
#files;
|
|
185
|
+
#directories;
|
|
186
|
+
#packageCache = new Map();
|
|
187
|
+
#extensions;
|
|
188
|
+
#customConditions;
|
|
189
|
+
#loaded = new Set();
|
|
190
|
+
#resolved = [];
|
|
191
|
+
|
|
192
|
+
constructor(input, options = {}) {
|
|
193
|
+
this.#root = assertPortableAbsolute(options.root || "/tree", "VFS root");
|
|
194
|
+
this.#extensions = Object.freeze([
|
|
195
|
+
...(options.extensions || DEFAULT_EXTENSIONS),
|
|
196
|
+
]);
|
|
197
|
+
if (
|
|
198
|
+
this.#extensions.some(
|
|
199
|
+
(extension) =>
|
|
200
|
+
typeof extension !== "string" ||
|
|
201
|
+
!extension.startsWith(".") ||
|
|
202
|
+
extension === ".node" ||
|
|
203
|
+
!SAFE_LOADERS.has(extension),
|
|
204
|
+
)
|
|
205
|
+
) {
|
|
206
|
+
fail(
|
|
207
|
+
"ERR_INVALID_VFS_OPTIONS",
|
|
208
|
+
"extensions must contain only safe JavaScript/JSON extensions",
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
this.#customConditions = Object.freeze([...(options.conditions || [])]);
|
|
212
|
+
if (
|
|
213
|
+
this.#customConditions.some(
|
|
214
|
+
(condition) =>
|
|
215
|
+
typeof condition !== "string" ||
|
|
216
|
+
!condition ||
|
|
217
|
+
condition === "import" ||
|
|
218
|
+
condition === "require" ||
|
|
219
|
+
condition === "default",
|
|
220
|
+
)
|
|
221
|
+
) {
|
|
222
|
+
fail(
|
|
223
|
+
"ERR_INVALID_VFS_OPTIONS",
|
|
224
|
+
"custom conditions contain a reserved or invalid condition",
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const entries =
|
|
229
|
+
input instanceof Map ? input.entries() : Object.entries(input || {});
|
|
230
|
+
const files = new Map();
|
|
231
|
+
for (const [rawPath, rawContents] of entries) {
|
|
232
|
+
const filePath = assertPortableAbsolute(rawPath, "VFS file");
|
|
233
|
+
assertWithin(this.#root, filePath, "VFS file");
|
|
234
|
+
if (filePath === this.#root || files.has(filePath)) {
|
|
235
|
+
fail("ERR_INVALID_VFS_PATH", `duplicate or root VFS file: ${filePath}`);
|
|
236
|
+
}
|
|
237
|
+
if (
|
|
238
|
+
typeof rawContents !== "string" &&
|
|
239
|
+
!Buffer.isBuffer(rawContents) &&
|
|
240
|
+
!(rawContents instanceof Uint8Array)
|
|
241
|
+
) {
|
|
242
|
+
fail(
|
|
243
|
+
"ERR_INVALID_VFS_CONTENT",
|
|
244
|
+
`VFS contents must be bytes or string: ${filePath}`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
files.set(filePath, Buffer.from(rawContents));
|
|
248
|
+
}
|
|
249
|
+
if (files.size === 0)
|
|
250
|
+
fail("ERR_EMPTY_VFS", "immutable VFS cannot be empty");
|
|
251
|
+
this.#files = files;
|
|
252
|
+
this.#directories = new Set([this.#root]);
|
|
253
|
+
for (const filePath of files.keys()) {
|
|
254
|
+
let directory = posix.dirname(filePath);
|
|
255
|
+
while (isWithin(this.#root, directory)) {
|
|
256
|
+
this.#directories.add(directory);
|
|
257
|
+
if (directory === this.#root) break;
|
|
258
|
+
directory = posix.dirname(directory);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
resolveEntry(entryPoint) {
|
|
264
|
+
const candidate = assertPortableAbsolute(entryPoint, "entry point");
|
|
265
|
+
assertWithin(this.#root, candidate, "entry point");
|
|
266
|
+
const result = this.#resolveFileOrDirectory(candidate, new Set());
|
|
267
|
+
if (!result)
|
|
268
|
+
fail(
|
|
269
|
+
"ERR_MODULE_NOT_FOUND",
|
|
270
|
+
`entry point is absent from immutable VFS: ${candidate}`,
|
|
271
|
+
);
|
|
272
|
+
this.#record("entry-point", "", entryPoint, result);
|
|
273
|
+
return result;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
resolve(specifier, importer, kind = "import-statement") {
|
|
277
|
+
validateSpecifier(specifier);
|
|
278
|
+
const importerPath = assertPortableAbsolute(importer, "importer");
|
|
279
|
+
assertWithin(this.#root, importerPath, "importer");
|
|
280
|
+
if (!this.#files.has(importerPath)) {
|
|
281
|
+
fail(
|
|
282
|
+
"ERR_UNKNOWN_IMPORTER",
|
|
283
|
+
`importer is absent from immutable VFS: ${importerPath}`,
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
let result;
|
|
288
|
+
if (isBuiltin(specifier)) {
|
|
289
|
+
result = Object.freeze({
|
|
290
|
+
external: true,
|
|
291
|
+
path: specifier,
|
|
292
|
+
builtin: true,
|
|
293
|
+
});
|
|
294
|
+
} else if (specifier.startsWith("node:")) {
|
|
295
|
+
fail("ERR_UNKNOWN_BUILTIN_MODULE", `unknown Node builtin: ${specifier}`);
|
|
296
|
+
} else if (specifier.startsWith("#")) {
|
|
297
|
+
result = this.#resolveImports(specifier, importerPath, kind, new Set());
|
|
298
|
+
} else if (specifier.startsWith("./") || specifier.startsWith("../")) {
|
|
299
|
+
const candidate = assertWithin(
|
|
300
|
+
this.#root,
|
|
301
|
+
posix.resolve(posix.dirname(importerPath), specifier),
|
|
302
|
+
`relative import ${specifier}`,
|
|
303
|
+
);
|
|
304
|
+
result = this.#resolveFileOrDirectory(candidate, new Set());
|
|
305
|
+
} else if (specifier.startsWith("/")) {
|
|
306
|
+
const candidate = assertPortableAbsolute(
|
|
307
|
+
posix.normalize(specifier),
|
|
308
|
+
"absolute import",
|
|
309
|
+
);
|
|
310
|
+
assertWithin(this.#root, candidate, `absolute import ${specifier}`);
|
|
311
|
+
result = this.#resolveFileOrDirectory(candidate, new Set());
|
|
312
|
+
} else {
|
|
313
|
+
result = this.#resolveBare(specifier, importerPath, kind, new Set());
|
|
314
|
+
}
|
|
315
|
+
if (!result) {
|
|
316
|
+
fail(
|
|
317
|
+
"ERR_MODULE_NOT_FOUND",
|
|
318
|
+
`cannot resolve ${specifier} from immutable VFS importer ${importerPath}`,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
this.#record(kind, importerPath, specifier, result);
|
|
322
|
+
return result;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
load(resolvedPath) {
|
|
326
|
+
const candidate = assertPortableAbsolute(resolvedPath, "load path");
|
|
327
|
+
assertWithin(this.#root, candidate, "load path");
|
|
328
|
+
const contents = this.#files.get(candidate);
|
|
329
|
+
if (!contents)
|
|
330
|
+
fail(
|
|
331
|
+
"ERR_MODULE_NOT_FOUND",
|
|
332
|
+
`load path is absent from immutable VFS: ${candidate}`,
|
|
333
|
+
);
|
|
334
|
+
const loader = this.#loaderFor(candidate);
|
|
335
|
+
this.#loaded.add(candidate);
|
|
336
|
+
return Object.freeze({
|
|
337
|
+
contents: Buffer.from(contents),
|
|
338
|
+
loader,
|
|
339
|
+
resolveDir: posix.dirname(candidate),
|
|
340
|
+
packageType: this.#packageType(candidate),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
audit() {
|
|
345
|
+
return Object.freeze({
|
|
346
|
+
root: this.#root,
|
|
347
|
+
fileCount: this.#files.size,
|
|
348
|
+
loaded: Object.freeze([...this.#loaded].sort()),
|
|
349
|
+
resolutions: Object.freeze(
|
|
350
|
+
this.#resolved.map((item) => Object.freeze({ ...item })),
|
|
351
|
+
),
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
createEsbuildPlugin(entryPoint) {
|
|
356
|
+
const resolver = this;
|
|
357
|
+
return Object.freeze({
|
|
358
|
+
name: "chainlesschain-immutable-vfs",
|
|
359
|
+
setup(build) {
|
|
360
|
+
build.onResolve({ filter: /.*/ }, (args) => {
|
|
361
|
+
try {
|
|
362
|
+
const result =
|
|
363
|
+
args.kind === "entry-point"
|
|
364
|
+
? resolver.resolveEntry(entryPoint || args.path)
|
|
365
|
+
: resolver.resolve(args.path, args.importer, args.kind);
|
|
366
|
+
if (result.external) return { path: result.path, external: true };
|
|
367
|
+
return { path: result.path, namespace: "cc-immutable-vfs" };
|
|
368
|
+
} catch (error) {
|
|
369
|
+
return {
|
|
370
|
+
errors: [
|
|
371
|
+
{
|
|
372
|
+
text: `${error.code || "ERR_VFS_RESOLUTION"}: ${error.message}`,
|
|
373
|
+
},
|
|
374
|
+
],
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
build.onLoad(
|
|
379
|
+
{ filter: /.*/, namespace: "cc-immutable-vfs" },
|
|
380
|
+
(args) => {
|
|
381
|
+
try {
|
|
382
|
+
const loaded = resolver.load(args.path);
|
|
383
|
+
return {
|
|
384
|
+
contents: loaded.contents,
|
|
385
|
+
loader: loaded.loader,
|
|
386
|
+
resolveDir: loaded.resolveDir,
|
|
387
|
+
pluginData: { packageType: loaded.packageType },
|
|
388
|
+
};
|
|
389
|
+
} catch (error) {
|
|
390
|
+
return {
|
|
391
|
+
errors: [
|
|
392
|
+
{ text: `${error.code || "ERR_VFS_LOAD"}: ${error.message}` },
|
|
393
|
+
],
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
},
|
|
397
|
+
);
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
#record(kind, importer, specifier, result) {
|
|
403
|
+
this.#resolved.push({
|
|
404
|
+
kind,
|
|
405
|
+
importer,
|
|
406
|
+
specifier,
|
|
407
|
+
path: result.path,
|
|
408
|
+
external: result.external === true,
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
#loaderFor(filePath) {
|
|
413
|
+
const extension = posix.extname(filePath).toLowerCase();
|
|
414
|
+
if (extension === ".node") {
|
|
415
|
+
fail(
|
|
416
|
+
"ERR_NATIVE_ADDON_BLOCKED",
|
|
417
|
+
`native addon is forbidden in a capsule: ${filePath}`,
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
const loader = SAFE_LOADERS.get(extension);
|
|
421
|
+
if (!loader)
|
|
422
|
+
fail(
|
|
423
|
+
"ERR_UNSUPPORTED_MODULE_TYPE",
|
|
424
|
+
`unsupported module type in immutable VFS: ${filePath}`,
|
|
425
|
+
);
|
|
426
|
+
return loader;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
#file(filePath) {
|
|
430
|
+
if (!this.#files.has(filePath)) return undefined;
|
|
431
|
+
this.#loaderFor(filePath);
|
|
432
|
+
return Object.freeze({ path: filePath, external: false });
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
#resolveFile(base) {
|
|
436
|
+
let result = this.#file(base);
|
|
437
|
+
if (result) return result;
|
|
438
|
+
for (const extension of this.#extensions) {
|
|
439
|
+
result = this.#file(`${base}${extension}`);
|
|
440
|
+
if (result) return result;
|
|
441
|
+
}
|
|
442
|
+
if (this.#files.has(`${base}.node`)) {
|
|
443
|
+
fail(
|
|
444
|
+
"ERR_NATIVE_ADDON_BLOCKED",
|
|
445
|
+
`native addon is forbidden in a capsule: ${base}.node`,
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
return undefined;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
#resolveFileOrDirectory(candidate, seen) {
|
|
452
|
+
assertWithin(this.#root, candidate, "module path");
|
|
453
|
+
const file = this.#resolveFile(candidate);
|
|
454
|
+
if (file) return file;
|
|
455
|
+
if (!this.#directories.has(candidate) || seen.has(candidate))
|
|
456
|
+
return undefined;
|
|
457
|
+
seen.add(candidate);
|
|
458
|
+
const packageRecord = this.#readPackage(candidate);
|
|
459
|
+
if (packageRecord && packageRecord.json.main) {
|
|
460
|
+
const mainTarget = this.#packageRelative(
|
|
461
|
+
candidate,
|
|
462
|
+
packageRecord.json.main,
|
|
463
|
+
"package main",
|
|
464
|
+
);
|
|
465
|
+
const mainResult =
|
|
466
|
+
this.#resolveFile(mainTarget) ||
|
|
467
|
+
this.#resolveFileOrDirectory(mainTarget, seen);
|
|
468
|
+
if (mainResult) return mainResult;
|
|
469
|
+
}
|
|
470
|
+
return this.#resolveFile(posix.join(candidate, "index"));
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
#readPackage(packageRoot) {
|
|
474
|
+
if (this.#packageCache.has(packageRoot))
|
|
475
|
+
return this.#packageCache.get(packageRoot);
|
|
476
|
+
const packageJsonPath = posix.join(packageRoot, "package.json");
|
|
477
|
+
const bytes = this.#files.get(packageJsonPath);
|
|
478
|
+
if (!bytes) {
|
|
479
|
+
this.#packageCache.set(packageRoot, undefined);
|
|
480
|
+
return undefined;
|
|
481
|
+
}
|
|
482
|
+
let json;
|
|
483
|
+
try {
|
|
484
|
+
json = JSON.parse(bytes.toString("utf8"));
|
|
485
|
+
} catch (error) {
|
|
486
|
+
fail(
|
|
487
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
488
|
+
`cannot parse ${packageJsonPath}: ${error.message}`,
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
const record = Object.freeze({
|
|
492
|
+
root: packageRoot,
|
|
493
|
+
path: packageJsonPath,
|
|
494
|
+
json: validatePackageJson(json, packageJsonPath),
|
|
495
|
+
});
|
|
496
|
+
this.#packageCache.set(packageRoot, record);
|
|
497
|
+
return record;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
#findPackageScope(startDirectory) {
|
|
501
|
+
let directory = startDirectory;
|
|
502
|
+
while (isWithin(this.#root, directory)) {
|
|
503
|
+
const record = this.#readPackage(directory);
|
|
504
|
+
if (record) return record;
|
|
505
|
+
if (directory === this.#root) break;
|
|
506
|
+
directory = posix.dirname(directory);
|
|
507
|
+
}
|
|
508
|
+
return undefined;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
#packageType(filePath) {
|
|
512
|
+
if (posix.extname(filePath).toLowerCase() !== ".js") return undefined;
|
|
513
|
+
return (
|
|
514
|
+
this.#findPackageScope(posix.dirname(filePath))?.json.type || "commonjs"
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
#packageRelative(packageRoot, target, label) {
|
|
519
|
+
if (
|
|
520
|
+
typeof target !== "string" ||
|
|
521
|
+
!target ||
|
|
522
|
+
target.includes("\\") ||
|
|
523
|
+
target.includes("\0") ||
|
|
524
|
+
target.includes("?") ||
|
|
525
|
+
target.includes("#") ||
|
|
526
|
+
target.includes("%") ||
|
|
527
|
+
posix.isAbsolute(target)
|
|
528
|
+
) {
|
|
529
|
+
fail(
|
|
530
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
531
|
+
`${label} is not a safe relative path: ${String(target)}`,
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
const resolved = posix.resolve(packageRoot, target);
|
|
535
|
+
if (resolved !== packageRoot && !isWithin(packageRoot, resolved)) {
|
|
536
|
+
fail(
|
|
537
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
538
|
+
`${label} escapes package root: ${target}`,
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
return resolved;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
#resolveBare(specifier, importerPath, kind, seen) {
|
|
545
|
+
const cycleKey = `${kind}:${posix.dirname(importerPath)}:${specifier}`;
|
|
546
|
+
if (seen.has(cycleKey))
|
|
547
|
+
fail("ERR_PACKAGE_IMPORT_CYCLE", `package map cycle: ${specifier}`);
|
|
548
|
+
seen.add(cycleKey);
|
|
549
|
+
const parsed = parsePackageSpecifier(specifier);
|
|
550
|
+
const scope = this.#findPackageScope(posix.dirname(importerPath));
|
|
551
|
+
if (scope?.json.name === parsed.packageName && OWN(scope.json, "exports")) {
|
|
552
|
+
return this.#resolvePackageAt(scope.root, parsed, kind, seen);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
let directory = posix.dirname(importerPath);
|
|
556
|
+
while (isWithin(this.#root, directory)) {
|
|
557
|
+
if (posix.basename(directory) !== "node_modules") {
|
|
558
|
+
const packageRoot = posix.join(
|
|
559
|
+
directory,
|
|
560
|
+
"node_modules",
|
|
561
|
+
...parsed.packageName.split("/"),
|
|
562
|
+
);
|
|
563
|
+
if (this.#directories.has(packageRoot)) {
|
|
564
|
+
return this.#resolvePackageAt(packageRoot, parsed, kind, seen);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (directory === this.#root) break;
|
|
568
|
+
directory = posix.dirname(directory);
|
|
569
|
+
}
|
|
570
|
+
return undefined;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
#resolvePackageAt(packageRoot, parsed, kind, seen) {
|
|
574
|
+
const record = this.#readPackage(packageRoot);
|
|
575
|
+
if (record && OWN(record.json, "exports")) {
|
|
576
|
+
const mapped = this.#resolvePackageMap({
|
|
577
|
+
field: record.json.exports,
|
|
578
|
+
request: parsed.subpath,
|
|
579
|
+
packageRecord: record,
|
|
580
|
+
kind,
|
|
581
|
+
imports: false,
|
|
582
|
+
});
|
|
583
|
+
if (mapped === BLOCKED || mapped === NO_MATCH) {
|
|
584
|
+
fail(
|
|
585
|
+
"ERR_PACKAGE_PATH_NOT_EXPORTED",
|
|
586
|
+
`${parsed.subpath} is not exported by ${parsed.packageName}`,
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
const exact = this.#file(mapped);
|
|
590
|
+
if (!exact)
|
|
591
|
+
fail(
|
|
592
|
+
"ERR_MODULE_NOT_FOUND",
|
|
593
|
+
`export target is absent from immutable VFS: ${mapped}`,
|
|
594
|
+
);
|
|
595
|
+
return exact;
|
|
596
|
+
}
|
|
597
|
+
if (parsed.subpath === ".")
|
|
598
|
+
return this.#resolveFileOrDirectory(packageRoot, new Set());
|
|
599
|
+
const target = this.#packageRelative(
|
|
600
|
+
packageRoot,
|
|
601
|
+
parsed.subpath.slice(2),
|
|
602
|
+
"legacy package subpath",
|
|
603
|
+
);
|
|
604
|
+
return this.#resolveFileOrDirectory(target, new Set());
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
#resolveImports(specifier, importerPath, kind, seen) {
|
|
608
|
+
if (specifier === "#" || specifier.startsWith("#/")) {
|
|
609
|
+
fail(
|
|
610
|
+
"ERR_INVALID_MODULE_SPECIFIER",
|
|
611
|
+
`invalid package import specifier: ${specifier}`,
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
const scope = this.#findPackageScope(posix.dirname(importerPath));
|
|
615
|
+
if (!scope || !OWN(scope.json, "imports")) {
|
|
616
|
+
fail(
|
|
617
|
+
"ERR_PACKAGE_IMPORT_NOT_DEFINED",
|
|
618
|
+
`package import is not defined: ${specifier}`,
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
const mapped = this.#resolvePackageMap({
|
|
622
|
+
field: scope.json.imports,
|
|
623
|
+
request: specifier,
|
|
624
|
+
packageRecord: scope,
|
|
625
|
+
kind,
|
|
626
|
+
imports: true,
|
|
627
|
+
});
|
|
628
|
+
if (mapped === BLOCKED || mapped === NO_MATCH) {
|
|
629
|
+
fail(
|
|
630
|
+
"ERR_PACKAGE_IMPORT_NOT_DEFINED",
|
|
631
|
+
`package import is not defined: ${specifier}`,
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
if (typeof mapped === "object" && mapped.externalSpecifier) {
|
|
635
|
+
if (isBuiltin(mapped.externalSpecifier)) {
|
|
636
|
+
return Object.freeze({
|
|
637
|
+
external: true,
|
|
638
|
+
path: mapped.externalSpecifier,
|
|
639
|
+
builtin: true,
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
return this.#resolveBare(
|
|
643
|
+
mapped.externalSpecifier,
|
|
644
|
+
scope.path,
|
|
645
|
+
kind,
|
|
646
|
+
seen,
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
const exact = this.#file(mapped);
|
|
650
|
+
if (!exact)
|
|
651
|
+
fail(
|
|
652
|
+
"ERR_MODULE_NOT_FOUND",
|
|
653
|
+
`imports target is absent from immutable VFS: ${mapped}`,
|
|
654
|
+
);
|
|
655
|
+
return exact;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
#resolvePackageMap({ field, request, packageRecord, kind, imports }) {
|
|
659
|
+
let map = field;
|
|
660
|
+
if (
|
|
661
|
+
!imports &&
|
|
662
|
+
(typeof map === "string" || map === null || Array.isArray(map))
|
|
663
|
+
) {
|
|
664
|
+
map = { ".": map };
|
|
665
|
+
} else if (
|
|
666
|
+
!imports &&
|
|
667
|
+
map &&
|
|
668
|
+
typeof map === "object" &&
|
|
669
|
+
!Array.isArray(map)
|
|
670
|
+
) {
|
|
671
|
+
const keys = Object.keys(map);
|
|
672
|
+
const subpathKeys = keys.filter((key) => key.startsWith("."));
|
|
673
|
+
if (subpathKeys.length && subpathKeys.length !== keys.length) {
|
|
674
|
+
fail(
|
|
675
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
676
|
+
`${packageRecord.path} mixes exports conditions and subpaths`,
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
if (!subpathKeys.length) {
|
|
680
|
+
if (request !== ".") return NO_MATCH;
|
|
681
|
+
return this.#resolveTarget(
|
|
682
|
+
map,
|
|
683
|
+
undefined,
|
|
684
|
+
packageRecord,
|
|
685
|
+
kind,
|
|
686
|
+
imports,
|
|
687
|
+
);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
if (!map || typeof map !== "object" || Array.isArray(map)) {
|
|
691
|
+
fail(
|
|
692
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
693
|
+
`${packageRecord.path} has an invalid ${imports ? "imports" : "exports"} map`,
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
const keys = Object.keys(map);
|
|
697
|
+
for (const key of keys) {
|
|
698
|
+
if (imports ? !key.startsWith("#") : !key.startsWith(".")) {
|
|
699
|
+
fail(
|
|
700
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
701
|
+
`${packageRecord.path} has an invalid package map key: ${key}`,
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
if ((key.match(/\*/g) || []).length > 1 || key.endsWith("/")) {
|
|
705
|
+
fail(
|
|
706
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
707
|
+
`${packageRecord.path} has an unsupported package map pattern: ${key}`,
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
let key = OWN(map, request) ? request : undefined;
|
|
712
|
+
let match;
|
|
713
|
+
if (!key) {
|
|
714
|
+
for (const candidate of keys
|
|
715
|
+
.filter((item) => item.includes("*"))
|
|
716
|
+
.sort(patternCompare)) {
|
|
717
|
+
const star = candidate.indexOf("*");
|
|
718
|
+
const prefix = candidate.slice(0, star);
|
|
719
|
+
const suffix = candidate.slice(star + 1);
|
|
720
|
+
if (
|
|
721
|
+
request.startsWith(prefix) &&
|
|
722
|
+
request.endsWith(suffix) &&
|
|
723
|
+
request.length >= prefix.length + suffix.length
|
|
724
|
+
) {
|
|
725
|
+
key = candidate;
|
|
726
|
+
match = request.slice(prefix.length, request.length - suffix.length);
|
|
727
|
+
break;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
if (!key) return NO_MATCH;
|
|
732
|
+
return this.#resolveTarget(map[key], match, packageRecord, kind, imports);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
#resolveTarget(target, match, packageRecord, kind, imports) {
|
|
736
|
+
if (target === null) return BLOCKED;
|
|
737
|
+
if (typeof target === "string") {
|
|
738
|
+
const substituted =
|
|
739
|
+
match === undefined ? target : target.replaceAll("*", match);
|
|
740
|
+
if (substituted.startsWith("./")) {
|
|
741
|
+
const tail = substituted.slice(2);
|
|
742
|
+
if (
|
|
743
|
+
!tail ||
|
|
744
|
+
tail.includes("\\") ||
|
|
745
|
+
tail.includes("%") ||
|
|
746
|
+
tail
|
|
747
|
+
.split("/")
|
|
748
|
+
.some(
|
|
749
|
+
(part) =>
|
|
750
|
+
!part ||
|
|
751
|
+
part === "." ||
|
|
752
|
+
part === ".." ||
|
|
753
|
+
part === "node_modules",
|
|
754
|
+
)
|
|
755
|
+
) {
|
|
756
|
+
throw new InvalidPackageTargetError(
|
|
757
|
+
`invalid local package target in ${packageRecord.path}: ${target}`,
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
return posix.join(packageRecord.root, tail);
|
|
761
|
+
}
|
|
762
|
+
if (
|
|
763
|
+
imports &&
|
|
764
|
+
!substituted.startsWith("../") &&
|
|
765
|
+
!substituted.startsWith("/")
|
|
766
|
+
) {
|
|
767
|
+
validateSpecifier(substituted);
|
|
768
|
+
return Object.freeze({ externalSpecifier: substituted });
|
|
769
|
+
}
|
|
770
|
+
throw new InvalidPackageTargetError(
|
|
771
|
+
`invalid package target in ${packageRecord.path}: ${target}`,
|
|
772
|
+
);
|
|
773
|
+
}
|
|
774
|
+
if (Array.isArray(target)) {
|
|
775
|
+
if (target.length === 0) return BLOCKED;
|
|
776
|
+
let lastInvalid;
|
|
777
|
+
for (const alternative of target) {
|
|
778
|
+
try {
|
|
779
|
+
const result = this.#resolveTarget(
|
|
780
|
+
alternative,
|
|
781
|
+
match,
|
|
782
|
+
packageRecord,
|
|
783
|
+
kind,
|
|
784
|
+
imports,
|
|
785
|
+
);
|
|
786
|
+
if (result !== NO_MATCH) return result;
|
|
787
|
+
} catch (error) {
|
|
788
|
+
if (!(error instanceof InvalidPackageTargetError)) throw error;
|
|
789
|
+
lastInvalid = error;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
if (lastInvalid) throw lastInvalid;
|
|
793
|
+
return NO_MATCH;
|
|
794
|
+
}
|
|
795
|
+
if (target && typeof target === "object") {
|
|
796
|
+
for (const condition of Object.keys(target)) {
|
|
797
|
+
if (/^(0|[1-9][0-9]*)$/.test(condition)) {
|
|
798
|
+
fail(
|
|
799
|
+
"ERR_INVALID_PACKAGE_CONFIG",
|
|
800
|
+
`${packageRecord.path} uses an integer condition key`,
|
|
801
|
+
);
|
|
802
|
+
}
|
|
803
|
+
const active = new Set([
|
|
804
|
+
"node",
|
|
805
|
+
conditionForKind(kind),
|
|
806
|
+
...this.#customConditions,
|
|
807
|
+
]);
|
|
808
|
+
if (condition === "default" || active.has(condition)) {
|
|
809
|
+
const result = this.#resolveTarget(
|
|
810
|
+
target[condition],
|
|
811
|
+
match,
|
|
812
|
+
packageRecord,
|
|
813
|
+
kind,
|
|
814
|
+
imports,
|
|
815
|
+
);
|
|
816
|
+
if (result !== NO_MATCH) return result;
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
return NO_MATCH;
|
|
820
|
+
}
|
|
821
|
+
throw new InvalidPackageTargetError(
|
|
822
|
+
`invalid package target type in ${packageRecord.path}`,
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
module.exports = {
|
|
828
|
+
ImmutableVfsResolver,
|
|
829
|
+
VfsResolutionError,
|
|
830
|
+
};
|