@parcha/agentrun-serve 0.1.0-beta.37 → 0.1.0-beta.38
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 +282 -0
- package/README.md +155 -92
- package/dist/agent-rows.d.ts +1 -1
- package/dist/agents-store.d.ts +35 -3
- package/dist/agents-store.js +39 -14
- package/dist/answer-json.d.ts +34 -1
- package/dist/answer-json.js +91 -9
- package/dist/api-only.d.ts +1 -1
- package/dist/api-only.js +2 -2
- package/dist/browser-cost.js +52 -93
- package/dist/cap.d.ts +0 -390
- package/dist/cap.js +4 -937
- package/dist/catalog.js +1 -1
- package/dist/composio-preset.d.ts +17 -0
- package/dist/composio-preset.js +43 -1
- package/dist/computer-calls.d.ts +36 -24
- package/dist/computer-calls.js +110 -27
- package/dist/computer-live.d.ts +5 -0
- package/dist/computer-live.js +12 -0
- package/dist/computer-setting.d.ts +15 -2
- package/dist/computer-setting.js +20 -4
- package/dist/computer-view.d.ts +3 -2
- package/dist/computer-view.js +5 -4
- package/dist/computer-viewer.js +26 -12
- package/dist/computers.d.ts +4 -1
- package/dist/computers.js +12 -6
- package/dist/console/assets/index-CjqQirbo.css +1 -0
- package/dist/console/assets/index-b6-XY5A9.js +576 -0
- package/dist/console/index.html +2 -2
- package/dist/console-links.d.ts +2 -0
- package/dist/console-links.js +3 -0
- package/dist/decisions.js +4 -0
- package/dist/faux-computers.js +4 -2
- package/dist/harness-opencode/adapter.js +7 -3
- package/dist/harness-opencode/facts.js +0 -2
- package/dist/harness-opencode/host/policy.d.ts +2 -5
- package/dist/harness-opencode/host/policy.js +8 -32
- package/dist/harness-table.d.ts +0 -2
- package/dist/harness-table.js +0 -2
- package/dist/host-tools.d.ts +1 -5
- package/dist/host-tools.js +7 -10
- package/dist/hosted-search.d.ts +0 -2
- package/dist/hosted-search.js +0 -21
- package/dist/key-words.d.ts +3 -2
- package/dist/key-words.js +4 -2
- package/dist/learn/check.d.ts +6 -1
- package/dist/learn/check.js +12 -4
- package/dist/learn/commit.d.ts +10 -0
- package/dist/learn/commit.js +18 -4
- package/dist/learn/compile.d.ts +243 -0
- package/dist/learn/compile.js +471 -0
- package/dist/learn/general.d.ts +3 -0
- package/dist/learn/general.js +4 -2
- package/dist/learn/grade.d.ts +1 -1
- package/dist/learn/grade.js +7 -6
- package/dist/learn/notes.d.ts +8 -0
- package/dist/learn/notes.js +18 -0
- package/dist/learn/promote.d.ts +253 -0
- package/dist/learn/promote.js +428 -0
- package/dist/learn/refit.d.ts +39 -0
- package/dist/learn/refit.js +236 -0
- package/dist/learn/reflect.d.ts +5 -25
- package/dist/learn/reflect.js +14 -131
- package/dist/learn/replay-calls.d.ts +63 -0
- package/dist/learn/replay-calls.js +158 -0
- package/dist/learn/skills.d.ts +17 -9
- package/dist/learn/skills.js +132 -42
- package/dist/learn/workflow-nodes.d.ts +15 -0
- package/dist/learn/workflow-nodes.js +44 -0
- package/dist/learned-nodes.d.ts +50 -2
- package/dist/learned-nodes.js +41 -2
- package/dist/learned-workflow.d.ts +19 -3
- package/dist/learned-workflow.js +105 -26
- package/dist/learning-batch.d.ts +2 -2
- package/dist/learning-batch.js +1 -1
- package/dist/learning-cost.d.ts +18 -6
- package/dist/learning-cost.js +15 -7
- package/dist/learning-routes.d.ts +14 -2
- package/dist/learning-routes.js +14 -3
- package/dist/learning-words.d.ts +7 -0
- package/dist/learning-words.js +7 -0
- package/dist/learning.d.ts +31 -6
- package/dist/learning.js +7 -4
- package/dist/llm.js +0 -3
- package/dist/mcp-gateway.d.ts +12 -20
- package/dist/mcp-gateway.js +16 -44
- package/dist/mcp-oauth.d.ts +5 -0
- package/dist/mcp-oauth.js +1 -1
- package/dist/mcp-pin-first.d.ts +37 -0
- package/dist/mcp-pin-first.js +107 -0
- package/dist/mcp-probe.d.ts +8 -9
- package/dist/mcp-probe.js +16 -33
- package/dist/mcp-servers-store.d.ts +89 -22
- package/dist/mcp-servers-store.js +131 -25
- package/dist/mcp-sources.d.ts +39 -0
- package/dist/mcp-sources.js +74 -0
- package/dist/mcp-store.d.ts +11 -0
- package/dist/mcp-store.js +16 -2
- package/dist/mcp-wire.d.ts +22 -0
- package/dist/mcp-wire.js +36 -0
- package/dist/mcp.js +3 -3
- package/dist/meta-tool-names.d.ts +1 -0
- package/dist/meta-tool-names.js +3 -0
- package/dist/model.d.ts +13 -0
- package/dist/model.js +46 -15
- package/dist/monid.d.ts +51 -25
- package/dist/monid.js +226 -140
- package/dist/openapi.js +336 -66
- package/dist/paid-calls.d.ts +26 -29
- package/dist/paid-calls.js +46 -102
- package/dist/propose.d.ts +7 -1
- package/dist/propose.js +18 -6
- package/dist/provider.js +9 -2
- package/dist/reflect-range.js +2 -1
- package/dist/rows.d.ts +5 -3
- package/dist/rows.js +15 -4
- package/dist/run-launch.d.ts +13 -0
- package/dist/run-launch.js +6 -0
- package/dist/run-learning-served.d.ts +13 -2
- package/dist/run-manifest.d.ts +5 -0
- package/dist/run-manifest.js +20 -0
- package/dist/run-secrets.js +2 -3
- package/dist/run-timeline-served.js +5 -2
- package/dist/run-view.d.ts +17 -6
- package/dist/run-view.js +51 -22
- package/dist/runnable.d.ts +0 -3
- package/dist/runnable.js +37 -29
- package/dist/runner/adapter-loop.d.ts +24 -10
- package/dist/runner/adapter-loop.js +852 -314
- package/dist/runner/adapter-run-spec.d.ts +12 -6
- package/dist/runner/adapter-run-spec.js +97 -46
- package/dist/runner/adapters/pi-computer.d.ts +3 -1
- package/dist/runner/adapters/pi-computer.js +13 -5
- package/dist/runner/adapters/pi.d.ts +16 -7
- package/dist/runner/adapters/pi.js +94 -33
- package/dist/runner/cli-run-spec.js +67 -27
- package/dist/runner/computer-channel.js +2 -1
- package/dist/runner/conformance/harness.d.ts +3 -0
- package/dist/runner/conformance/harness.js +145 -4
- package/dist/runner/conformance/subject.d.ts +11 -4
- package/dist/runner/end-of-run.d.ts +6 -5
- package/dist/runner/end-of-run.js +9 -6
- package/dist/runner/feedback-round.d.ts +52 -0
- package/dist/runner/feedback-round.js +79 -0
- package/dist/runner/journal-kinds.d.ts +24 -6
- package/dist/runner/journal-kinds.js +15 -0
- package/dist/runner/learning-end.d.ts +54 -19
- package/dist/runner/learning-end.js +272 -99
- package/dist/runner/learning-loop.d.ts +19 -1
- package/dist/runner/learning-loop.js +5 -2
- package/dist/runner/learning-seed.d.ts +4 -0
- package/dist/runner/learning-seed.js +14 -0
- package/dist/runner/loop-contract.d.ts +19 -3
- package/dist/runner/loop.d.ts +16 -1
- package/dist/runner/loop.js +21 -1
- package/dist/runner/mcp-claim.d.ts +5 -1
- package/dist/runner/mcp-claim.js +66 -50
- package/dist/runner/mcp-run.d.ts +16 -52
- package/dist/runner/mcp-run.js +12 -84
- package/dist/runner/meta-tools.d.ts +37 -0
- package/dist/runner/meta-tools.js +268 -0
- package/dist/runner/metrics.js +14 -3
- package/dist/runner/model-route.js +12 -3
- package/dist/runner/monid-run.d.ts +16 -0
- package/dist/runner/monid-run.js +96 -0
- package/dist/runner/promotion.d.ts +80 -0
- package/dist/runner/promotion.js +562 -0
- package/dist/runner/reflect-hook.d.ts +36 -21
- package/dist/runner/reflect-hook.js +138 -49
- package/dist/runner/runner.js +26 -4
- package/dist/runner/sessions.d.ts +8 -3
- package/dist/runner/sessions.js +5 -1
- package/dist/runner/step-guard.d.ts +62 -0
- package/dist/runner/step-guard.js +93 -0
- package/dist/runner/subagent.d.ts +0 -3
- package/dist/runner/subagent.js +7 -12
- package/dist/runner/workflow-first.d.ts +216 -0
- package/dist/runner/workflow-first.js +303 -0
- package/dist/runner/workflow-loop.js +17 -10
- package/dist/runner/workflow-spec.d.ts +1 -0
- package/dist/runner/workflow-spec.js +7 -3
- package/dist/runs-over-store.d.ts +11 -2
- package/dist/runs-over-store.js +21 -11
- package/dist/server.d.ts +88 -16
- package/dist/server.js +380 -87
- package/dist/settings-wake.d.ts +8 -6
- package/dist/settings-wake.js +13 -7
- package/dist/store/agent-body.d.ts +14 -3
- package/dist/store/agent-body.js +14 -10
- package/dist/store/command-wake.d.ts +3 -2
- package/dist/store/command-wake.js +4 -4
- package/dist/store/postgres/connection.js +3 -1
- package/dist/store/postgres/migrations/0010_imported_templates.sql +18 -0
- package/dist/store/postgres/migrations/0011_revision_mcp_pin.sql +13 -0
- package/dist/store/schema.d.ts +9 -0
- package/dist/store/schema.js +28 -8
- package/dist/store/sqlite/connection.d.ts +2 -2
- package/dist/store/sqlite/connection.js +2 -2
- package/dist/store/sqlite/run-patch.js +19 -7
- package/dist/store/sqlite/store.js +59 -18
- package/dist/store/store.d.ts +46 -14
- package/dist/store-reads/journal.d.ts +25 -1
- package/dist/store-reads/journal.js +63 -49
- package/dist/store-reads/learning.d.ts +101 -15
- package/dist/store-reads/learning.js +195 -61
- package/dist/store-reads/loop.d.ts +61 -0
- package/dist/store-reads/loop.js +111 -0
- package/dist/store-view.d.ts +3 -2
- package/dist/store-view.js +35 -17
- package/dist/template-eval.d.ts +18 -9
- package/dist/template-eval.js +91 -66
- package/dist/template-github-permissions.d.ts +2 -2
- package/dist/template-github-permissions.js +2 -1
- package/dist/template-import.d.ts +31 -0
- package/dist/template-import.js +121 -0
- package/dist/template-source.d.ts +51 -0
- package/dist/template-source.js +371 -0
- package/dist/templates.d.ts +218 -46
- package/dist/templates.js +493 -181
- package/dist/timeline/computer.d.ts +48 -0
- package/dist/timeline/computer.js +169 -0
- package/dist/timeline/index.d.ts +4 -3
- package/dist/timeline/index.js +4 -3
- package/dist/timeline/labels.d.ts +6 -2
- package/dist/timeline/labels.js +93 -2
- package/dist/timeline/origin.d.ts +19 -5
- package/dist/timeline/origin.js +11 -4
- package/dist/timeline/timeline.d.ts +11 -17
- package/dist/timeline/timeline.js +24 -9
- package/dist/tool-gate.d.ts +73 -46
- package/dist/tool-gate.js +133 -66
- package/dist/tool-keys.d.ts +4 -1
- package/dist/tool-keys.js +8 -3
- package/dist/tool-list.d.ts +26 -0
- package/dist/tool-list.js +43 -0
- package/dist/tool-names.d.ts +71 -0
- package/dist/tool-names.js +99 -0
- package/dist/tool-plane.d.ts +29 -27
- package/dist/tool-plane.js +65 -37
- package/dist/tools.d.ts +22 -6
- package/dist/tools.js +24 -20
- package/dist/validate.d.ts +15 -7
- package/dist/validate.js +91 -44
- package/dist/web-fetch.d.ts +1 -7
- package/dist/web-fetch.js +2 -22
- package/dist/workspace/computer-deps.d.ts +3 -0
- package/dist/workspace/computer-deps.js +5 -2
- package/dist/workspace/computer-for-claim.d.ts +6 -0
- package/dist/workspace/computer-for-claim.js +32 -14
- package/dist/workspace/computer-hold.js +7 -1
- package/dist/workspace/computer-script.d.ts +23 -7
- package/dist/workspace/computer-script.js +48 -14
- package/dist/workspace/index.d.ts +2 -2
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/kind-skills/desktop/SKILL.md +13 -0
- package/dist/workspace/kind-skills/phone/SKILL.md +4 -0
- package/dist/workspace/local.js +2 -2
- package/dist/workspace/resolve-computer.d.ts +21 -1
- package/dist/workspace/resolve-computer.js +32 -14
- package/dist/workspace/run-tools.d.ts +28 -10
- package/dist/workspace/run-tools.js +39 -11
- package/dist/{tool-budget.d.ts → wrap-up.d.ts} +1 -18
- package/dist/wrap-up.js +21 -0
- package/launcher/agentrun-serve +4 -2
- package/package.json +9 -9
- package/templates/README.md +111 -45
- package/templates/code-review/agent.md +30 -14
- package/templates/code-review/evals/clean-refactor/case.yaml +2 -1
- package/templates/code-review/evals/clean-refactor/graders/head-sha.md +6 -0
- package/templates/code-review/evals/injection/case.yaml +2 -1
- package/templates/code-review/evals/injection/graders/head-sha.md +6 -0
- package/templates/code-review/evals/mentions-defused/case.yaml +2 -2
- package/templates/code-review/evals/mentions-defused/fixtures/github-mcp.json +0 -23
- package/templates/code-review/evals/mentions-defused/graders/head-sha.md +6 -0
- package/templates/code-review/evals/never-flag/case.yaml +2 -1
- package/templates/code-review/evals/never-flag/graders/head-sha.md +6 -0
- package/templates/code-review/evals/null-name/case.yaml +2 -1
- package/templates/code-review/evals/null-name/graders/head-sha.md +6 -0
- package/templates/code-review/evals/off-by-one/case.yaml +2 -1
- package/templates/code-review/evals/off-by-one/graders/head-sha.md +6 -0
- package/templates/code-review/evals/other-repo/case.yaml +16 -0
- package/templates/code-review/evals/other-repo/fixtures/github-mcp.json +130 -0
- package/templates/code-review/evals/other-repo/graders/head-sha.md +6 -0
- package/templates/code-review/evals/other-repo/graders/no-canary.md +6 -0
- package/templates/code-review/evals/other-repo/graders/no-other-repo.md +8 -0
- package/templates/code-review/evals/other-repo/graders/refused-not-quoted.md +18 -0
- package/templates/code-review/evals/other-repo/prompt.md +10 -0
- package/templates/code-review/evals/re-review/case.yaml +2 -1
- package/templates/code-review/evals/re-review/graders/head-sha.md +6 -0
- package/templates/code-review/evals/re-review-open/case.yaml +2 -1
- package/templates/code-review/evals/re-review-open/graders/head-sha.md +6 -0
- package/templates/code-review/evals/repo-rule/case.yaml +2 -1
- package/templates/code-review/evals/repo-rule/graders/head-sha.md +6 -0
- package/templates/code-review/evals/two-defects/case.yaml +2 -1
- package/templates/code-review/evals/two-defects/graders/head-sha.md +6 -0
- package/templates/code-review/rules/CODEREVIEW.md +1 -1
- package/templates/code-review/skills/review/DEEP.md +3 -2
- package/templates/code-review/skills/review/REVIEWER.md +3 -2
- package/templates/code-review/skills/review/SKILL.md +4 -3
- package/templates/code-review/{findings.schema.json → skills/review/findings.schema.json} +1 -0
- package/templates/inbound-lead/agent.md +26 -16
- package/templates/inbound-lead/evals/injection-in-form/graders/no-exfil.md +16 -0
- package/templates/inbound-lead/evals/injection-in-form/prompt.md +2 -2
- package/templates/inbound-lead/evals/write-back/case.yaml +2 -6
- package/templates/inbound-lead/evals/write-back/fixtures/fake-hubspot.json +34 -225
- package/templates/inbound-lead/evals/write-back/graders/note-and-score.md +10 -14
- package/templates/inbound-lead/evals/write-back/graders/writes-once.md +7 -0
- package/templates/inbound-lead/evals/write-back/prompt.md +1 -1
- package/templates/inbound-lead/evals/write-back-wrong-record/case.yaml +3 -7
- package/templates/inbound-lead/evals/write-back-wrong-record/fixtures/fake-hubspot.json +34 -225
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/form-id.md +17 -0
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/no-write.md +3 -3
- package/templates/inbound-lead/evals/write-back-wrong-record/prompt.md +2 -2
- package/templates/inbound-lead/skills/lead/SKILL.md +23 -27
- package/templates/kyb/agent.md +15 -5
- package/templates/kyb/evals/adverse-media/prompt.md +2 -7
- package/templates/kyb/evals/canada-active-filed/prompt.md +2 -6
- package/templates/kyb/evals/clean-public-company/prompt.md +2 -6
- package/templates/kyb/evals/injection-in-page/prompt.md +3 -7
- package/templates/kyb/evals/name-collision/prompt.md +2 -6
- package/templates/kyb/evals/no-invention/prompt.md +2 -6
- package/templates/kyb/evals/opensanctions-on/prompt.md +2 -6
- package/templates/kyb/evals/sanctioned-entity/prompt.md +2 -6
- package/templates/kyb/kyb.schema.json +8 -7
- package/templates/kyb/skills/kyb/SKILL.md +9 -6
- package/templates/market-monitor/agent.md +19 -12
- package/templates/market-monitor/evals/post-happens/case.yaml +4 -4
- package/templates/market-monitor/evals/post-happens/fixtures/slack-mcp.json +13 -9
- package/templates/market-monitor/evals/post-happens/graders/post-is-digest.md +1 -1
- package/templates/market-monitor/evals/post-happens/graders/posted-once.md +3 -3
- package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
- package/templates/release-notes/agent.md +32 -10
- package/templates/release-notes/evals/draft-not-publish/case.yaml +4 -3
- package/templates/release-notes/evals/draft-not-publish/fixtures/github-mcp.json +29 -51
- package/templates/release-notes/evals/every-pr-accounted/case.yaml +2 -1
- package/templates/release-notes/evals/issue-write-refused/case.yaml +4 -3
- package/templates/release-notes/evals/issue-write-refused/fixtures/github-mcp.json +29 -51
- package/templates/release-notes/evals/late-arrival/case.yaml +6 -5
- package/templates/release-notes/evals/late-arrival/fixtures/github-mcp.json +29 -51
- package/templates/release-notes/evals/learned-style/case.yaml +2 -1
- package/templates/release-notes/evals/no-pr-numbers-on-top/case.yaml +2 -1
- package/templates/release-notes/evals/other-repo/case.yaml +24 -0
- package/templates/release-notes/evals/other-repo/fixtures/github-mcp.json +907 -0
- package/templates/release-notes/evals/other-repo/graders/no-canary.md +7 -0
- package/templates/release-notes/evals/other-repo/graders/no-other-repo.md +8 -0
- package/templates/release-notes/evals/other-repo/graders/refused-not-quoted.md +18 -0
- package/templates/release-notes/evals/other-repo/prompt.md +32 -0
- package/templates/release-notes/evals/upgrade-line/case.yaml +2 -1
- package/templates/release-notes/evals/user-facing-only/case.yaml +2 -1
- package/templates/release-notes/notes.schema.json +1 -1
- package/templates/release-notes/skills/release-notes/SKILL.md +26 -17
- package/dist/console/assets/index-Cn6JX1uH.js +0 -563
- package/dist/console/assets/index-DRN62V5v.css +0 -1
- package/dist/tool-blurbs.d.ts +0 -4
- package/dist/tool-blurbs.js +0 -28
- package/dist/tool-budget.js +0 -62
- package/templates/code-review/evals/clean-refactor/graders/schema.md +0 -5
- package/templates/code-review/evals/injection/graders/schema.md +0 -5
- package/templates/code-review/evals/mentions-defused/graders/schema.md +0 -5
- package/templates/code-review/evals/never-flag/graders/schema.md +0 -5
- package/templates/code-review/evals/null-name/graders/schema.md +0 -5
- package/templates/code-review/evals/off-by-one/graders/schema.md +0 -5
- package/templates/code-review/evals/re-review/graders/schema.md +0 -5
- package/templates/code-review/evals/re-review-open/graders/schema.md +0 -5
- package/templates/code-review/evals/repo-rule/graders/schema.md +0 -5
- package/templates/code-review/evals/two-defects/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/clear-disqualifier/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/injection-in-form/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/learned-correction/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/strong-fit/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/thin-data/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/write-back/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/write-back/graders/writes-twice.md +0 -7
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/proves-record.md +0 -18
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/schema.md +0 -5
- package/templates/kyb/evals/adverse-media/graders/schema.md +0 -5
- package/templates/kyb/evals/canada-active-filed/graders/schema.md +0 -5
- package/templates/kyb/evals/clean-public-company/graders/schema.md +0 -5
- package/templates/kyb/evals/injection-in-page/graders/schema.md +0 -5
- package/templates/kyb/evals/name-collision/graders/schema.md +0 -5
- package/templates/kyb/evals/no-invention/graders/schema.md +0 -5
- package/templates/kyb/evals/opensanctions-on/graders/schema.md +0 -5
- package/templates/kyb/evals/sanctioned-entity/graders/schema.md +0 -5
- package/templates/market-monitor/evals/blocked-source/graders/schema.md +0 -5
- package/templates/market-monitor/evals/every-competitor-named/graders/schema.md +0 -5
- package/templates/market-monitor/evals/learned-preference/graders/schema.md +0 -5
- package/templates/market-monitor/evals/no-repeats/graders/schema.md +0 -5
- package/templates/market-monitor/evals/nothing-changed/graders/schema.md +0 -5
- package/templates/market-monitor/evals/post-happens/graders/schema.md +0 -5
- package/templates/market-monitor/evals/pricing-change/graders/schema.md +0 -5
- package/templates/market-monitor/evals/pricing-moved/graders/schema.md +0 -5
- package/templates/release-notes/evals/draft-not-publish/graders/schema.md +0 -5
- package/templates/release-notes/evals/every-pr-accounted/graders/schema.md +0 -5
- package/templates/release-notes/evals/issue-write-refused/graders/schema.md +0 -5
- package/templates/release-notes/evals/late-arrival/graders/schema.md +0 -5
- package/templates/release-notes/evals/learned-style/graders/schema.md +0 -5
- package/templates/release-notes/evals/no-pr-numbers-on-top/graders/schema.md +0 -5
- package/templates/release-notes/evals/upgrade-line/graders/schema.md +0 -5
- package/templates/release-notes/evals/user-facing-only/graders/schema.md +0 -5
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** The hosts an import reads from: GitHub's API, its web host (a tree URL a person pastes) and the tarball host the API
|
|
2
|
+
* redirects to. A redirect anywhere else is refused. */
|
|
3
|
+
export declare const GITHUB_HOSTS: readonly string[];
|
|
4
|
+
/** What one import may take: the tarball as sent, the tarball unpacked (the whole repository streams through, counted), the
|
|
5
|
+
* package's own files together and how many there are, and the time the whole read may take. The package's own limits (templates.ts) hold too. */
|
|
6
|
+
export declare const IMPORT_LIMITS: {
|
|
7
|
+
readonly downloadBytes: number;
|
|
8
|
+
readonly unpackedBytes: number;
|
|
9
|
+
readonly packageBytes: number;
|
|
10
|
+
readonly files: 256;
|
|
11
|
+
readonly ms: 120000;
|
|
12
|
+
};
|
|
13
|
+
/** A package's place on GitHub: the repository, the folder in it (null, its root) and the ref asked for (null, its default
|
|
14
|
+
* branch). `id`, the package's identity here, is the source without the ref: importing it again at a new SHA replaces it. */
|
|
15
|
+
export type GithubSource = {
|
|
16
|
+
owner: string;
|
|
17
|
+
repo: string;
|
|
18
|
+
path: string | null;
|
|
19
|
+
ref: string | null;
|
|
20
|
+
id: string;
|
|
21
|
+
source: string;
|
|
22
|
+
};
|
|
23
|
+
/** `text` read as a package's source: `github:<owner>/<repo>[/<path>][@<ref>]`, or a GitHub web URL of the repository or of
|
|
24
|
+
* a folder in it (`https://github.com/<owner>/<repo>/tree/<ref>/<path>`, its ref the one segment after tree), made into
|
|
25
|
+
* the first form. Anything else is a 422 on `source`. */
|
|
26
|
+
export declare function parseSource(text: unknown): GithubSource;
|
|
27
|
+
/** An import that GitHub refused or that broke a limit, in words a person acts on: 422 `source` (not there, private, too
|
|
28
|
+
* big, a link) or 502 github_failed (GitHub did not answer, or its rate limit). */
|
|
29
|
+
export declare class ImportRefused extends Error {
|
|
30
|
+
readonly kind: "source" | "github" | "package";
|
|
31
|
+
constructor(kind: "source" | "github" | "package", message: string);
|
|
32
|
+
}
|
|
33
|
+
/** Where an import reads GitHub from: its API's origin and the hosts it may be sent to (GitHub's own; a test's faux GitHub
|
|
34
|
+
* in place of them), a token when the server has one (its GitHub App's), and why it has none, said when GitHub limits
|
|
35
|
+
* the anonymous read. */
|
|
36
|
+
export type GithubReach = {
|
|
37
|
+
api: string;
|
|
38
|
+
hosts: readonly string[];
|
|
39
|
+
token?: string;
|
|
40
|
+
anonymous?: string;
|
|
41
|
+
signal?: AbortSignal;
|
|
42
|
+
limits?: Partial<typeof IMPORT_LIMITS>;
|
|
43
|
+
};
|
|
44
|
+
/** The full commit SHA `source`'s ref (its default branch when it names none) is at now: GitHub's answer, read before
|
|
45
|
+
* anything else, so what is imported is that one commit and its SHA is what is stored and shown. */
|
|
46
|
+
export declare function resolveSha(source: GithubSource, reach: GithubReach, signal: AbortSignal): Promise<string>;
|
|
47
|
+
/** The package's files at `sha`: the repository's tarball streamed from GitHub, unpacked as it comes, its folder
|
|
48
|
+
* `source.path` (the root when null) kept in memory by path under it; every other entry is counted and dropped. A link,
|
|
49
|
+
* a device or any other entry that is not a regular file or a folder in the package's folder refuses the import, as do a
|
|
50
|
+
* path that climbs out, a file over the package's limits, and a tarball past IMPORT_LIMITS. */
|
|
51
|
+
export declare function fetchPackageFiles(source: GithubSource, sha: string, reach: GithubReach, signal: AbortSignal): Promise<Map<string, Buffer>>;
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
// A template package from GitHub (POST /v2/templates/import): its source read, its ref resolved to a full commit SHA before
|
|
2
|
+
// anything else is read, the repository's tarball at that SHA streamed from GitHub's own hosts alone, and the package's
|
|
3
|
+
// folder taken out of it in memory, every entry checked as it comes. Nothing is written to disk and nothing in it runs:
|
|
4
|
+
// templates.ts readPackage, the one checker, reads the files kept here as it reads a bundled package's folder.
|
|
5
|
+
import { createGunzip } from "node:zlib";
|
|
6
|
+
import { invalid } from "./errors.js";
|
|
7
|
+
import { MAX_PACKAGE_FILES } from "./templates.js";
|
|
8
|
+
/** The hosts an import reads from: GitHub's API, its web host (a tree URL a person pastes) and the tarball host the API
|
|
9
|
+
* redirects to. A redirect anywhere else is refused. */
|
|
10
|
+
export const GITHUB_HOSTS = ["github.com", "api.github.com", "codeload.github.com"];
|
|
11
|
+
const OWNER = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})$/;
|
|
12
|
+
const REPO = /^[A-Za-z0-9._-]{1,100}$/;
|
|
13
|
+
const SEGMENT = /^[A-Za-z0-9._@+-]{1,255}$/;
|
|
14
|
+
const REF = /^[A-Za-z0-9._/+-]{1,255}$/;
|
|
15
|
+
const SHA = /^[0-9a-f]{40}$/;
|
|
16
|
+
/** What one import may take: the tarball as sent, the tarball unpacked (the whole repository streams through, counted), the
|
|
17
|
+
* package's own files together and how many there are, and the time the whole read may take. The package's own limits (templates.ts) hold too. */
|
|
18
|
+
export const IMPORT_LIMITS = { downloadBytes: 128 << 20, unpackedBytes: 1 << 30, packageBytes: 8 << 20, files: MAX_PACKAGE_FILES, ms: 120_000 };
|
|
19
|
+
/** How many redirects an import follows, each to GitHub's own hosts. */
|
|
20
|
+
const MAX_REDIRECTS = 3;
|
|
21
|
+
const refused = (message) => invalid([{ field: "source", reason: "bad_format", message, expected: "github:<owner>/<repo>[/<path>][@<ref>]", example: "github:Parcha-ai/agentrun/packages/serve/templates/kyb@main" }]);
|
|
22
|
+
/** A % in a source's path part, refused by name, never as a folder name's wrong letter: one that starts no escape; else,
|
|
23
|
+
* in a github: source (`escapes` false), any escape. A github: source is stored and linked as written, so an escaped @
|
|
24
|
+
* or / in it would read back as another package. */
|
|
25
|
+
function percentRefused(part, escapes) {
|
|
26
|
+
const bad = /%(?![0-9A-Fa-f]{2}).{0,2}/s.exec(part)?.[0];
|
|
27
|
+
if (bad)
|
|
28
|
+
return refused(`${JSON.stringify(part)} has ${JSON.stringify(bad)}, which is no percent escape: a % is followed by two hex digits (%25 is a % itself).`);
|
|
29
|
+
return refused(escapes ? `${JSON.stringify(part)} has percent escapes that spell no text.` : `${JSON.stringify(part)} has ${JSON.stringify(/%../.exec(part)?.[0])}, a percent escape: a github: source names each folder as it is, unescaped.`);
|
|
30
|
+
}
|
|
31
|
+
/** A tree URL's path part with its percent escapes read. */
|
|
32
|
+
function unescaped(part) {
|
|
33
|
+
try {
|
|
34
|
+
return decodeURIComponent(part);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
throw percentRefused(part, true);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** `text` read as a package's source: `github:<owner>/<repo>[/<path>][@<ref>]`, or a GitHub web URL of the repository or of
|
|
41
|
+
* a folder in it (`https://github.com/<owner>/<repo>/tree/<ref>/<path>`, its ref the one segment after tree), made into
|
|
42
|
+
* the first form. Anything else is a 422 on `source`. */
|
|
43
|
+
export function parseSource(text) {
|
|
44
|
+
if (typeof text !== "string" || !text.trim())
|
|
45
|
+
throw refused("source is a GitHub package's place: github:<owner>/<repo>[/<path>][@<ref>], or its https://github.com/<owner>/<repo>/tree/<ref>/<path> URL.");
|
|
46
|
+
const given = text.trim();
|
|
47
|
+
let owner, repo, rest, ref;
|
|
48
|
+
if (/^https?:\/\//i.test(given)) {
|
|
49
|
+
let url;
|
|
50
|
+
try {
|
|
51
|
+
url = new URL(given);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
throw refused(`${given} is not a URL.`);
|
|
55
|
+
}
|
|
56
|
+
if (url.protocol !== "https:" || url.hostname !== "github.com" || url.port || url.username || url.password)
|
|
57
|
+
throw refused(`A package is imported from github.com alone, over https: ${url.protocol}//${url.host} is not.`);
|
|
58
|
+
if (url.search || url.hash)
|
|
59
|
+
throw refused("A package's GitHub URL has no query or fragment.");
|
|
60
|
+
const parts = url.pathname.split("/").filter(Boolean).map(unescaped);
|
|
61
|
+
[owner = "", repo = ""] = parts;
|
|
62
|
+
repo = repo.replace(/\.git$/, "");
|
|
63
|
+
if (parts.length > 2 && parts[2] !== "tree")
|
|
64
|
+
throw refused("A package's GitHub URL is the repository's, or a folder's: https://github.com/<owner>/<repo>/tree/<ref>/<path>.");
|
|
65
|
+
ref = parts[2] === "tree" ? (parts[3] ?? null) : null;
|
|
66
|
+
if (parts[2] === "tree" && !ref)
|
|
67
|
+
throw refused("A /tree/ URL names its ref: https://github.com/<owner>/<repo>/tree/<ref>/<path>.");
|
|
68
|
+
rest = parts.slice(4);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
if (!given.startsWith("github:"))
|
|
72
|
+
throw refused(`${given} is not a package source: github:<owner>/<repo>[/<path>][@<ref>].`);
|
|
73
|
+
// The ref follows the last @ (a ref holds none), so a folder with an @ in its name (packages/@acme/kyb) still reads,
|
|
74
|
+
// given a ref, as the source every install of the console pins to a commit is (<id>@<sha>).
|
|
75
|
+
const body = given.slice("github:".length);
|
|
76
|
+
const at = body.lastIndexOf("@");
|
|
77
|
+
const parts = (at < 0 ? body : body.slice(0, at)).split("/");
|
|
78
|
+
const escaped = parts.find((part) => part.includes("%"));
|
|
79
|
+
if (escaped !== undefined)
|
|
80
|
+
throw percentRefused(escaped, false);
|
|
81
|
+
[owner = "", repo = ""] = parts;
|
|
82
|
+
rest = parts.slice(2);
|
|
83
|
+
ref = at < 0 ? null : body.slice(at + 1);
|
|
84
|
+
if (ref === "")
|
|
85
|
+
throw refused(`${given} names no ref after its @.`);
|
|
86
|
+
}
|
|
87
|
+
if (!OWNER.test(owner))
|
|
88
|
+
throw refused(`${JSON.stringify(owner)} is not a GitHub account's name.`);
|
|
89
|
+
if (!REPO.test(repo) || repo === "." || repo === "..")
|
|
90
|
+
throw refused(`${JSON.stringify(repo)} is not a GitHub repository's name.`);
|
|
91
|
+
if (rest.some((part) => !SEGMENT.test(part) || part === "." || part === ".."))
|
|
92
|
+
throw refused(`The package's folder ${JSON.stringify(rest.join("/"))} is a path of plain names: no empty part, no . or ..`);
|
|
93
|
+
if (ref !== null && (!REF.test(ref) || ref.split("/").some((part) => !part || part === "." || part === "..") || ref.includes("..")))
|
|
94
|
+
throw refused(`${JSON.stringify(ref)} is not a git ref.`);
|
|
95
|
+
const path = rest.length ? rest.join("/") : null;
|
|
96
|
+
const id = `github:${owner}/${repo}${path ? `/${path}` : ""}`;
|
|
97
|
+
return { owner, repo, path, ref, id, source: `${id}${ref ? `@${ref}` : ""}` };
|
|
98
|
+
}
|
|
99
|
+
/** An import that GitHub refused or that broke a limit, in words a person acts on: 422 `source` (not there, private, too
|
|
100
|
+
* big, a link) or 502 github_failed (GitHub did not answer, or its rate limit). */
|
|
101
|
+
export class ImportRefused extends Error {
|
|
102
|
+
kind;
|
|
103
|
+
constructor(kind, message) {
|
|
104
|
+
super(message);
|
|
105
|
+
this.kind = kind;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** What GitHub's 403 or 429 means for an import: its rate limit, with the token it was read with or without. */
|
|
109
|
+
function limited(res, reach) {
|
|
110
|
+
if (res.status !== 429 && !(res.status === 403 && res.headers.get("x-ratelimit-remaining") === "0"))
|
|
111
|
+
return undefined;
|
|
112
|
+
const reset = Number(res.headers.get("x-ratelimit-reset"));
|
|
113
|
+
const when = Number.isFinite(reset) && reset > 0 ? ` It resets at ${new Date(reset * 1000).toISOString()}.` : "";
|
|
114
|
+
return new ImportRefused("github", reach.token
|
|
115
|
+
? `GitHub's rate limit for this server's GitHub App ran out.${when}`
|
|
116
|
+
: `GitHub's rate limit for anonymous reads (60 an hour from this address) ran out, and this server read GitHub anonymously: ${reach.anonymous ?? "it has no GitHub App"}. Connect GitHub in Settings so imports read with the App's token, or try again later.${when}`);
|
|
117
|
+
}
|
|
118
|
+
/** One GET from GitHub, following its redirects only to `reach.hosts`, over the API's own protocol. */
|
|
119
|
+
async function githubGet(url, reach, accept, signal) {
|
|
120
|
+
const protocol = new URL(reach.api).protocol;
|
|
121
|
+
let at = new URL(url);
|
|
122
|
+
for (let hop = 0;; hop++) {
|
|
123
|
+
if (at.protocol !== protocol || !reach.hosts.includes(at.host) || at.username || at.password)
|
|
124
|
+
throw new ImportRefused("github", `GitHub sent the import to ${at.protocol}//${at.host}, which is not one of GitHub's own hosts (${reach.hosts.join(", ")}): refused.`);
|
|
125
|
+
// The token goes to the API alone, never to a host a redirect names.
|
|
126
|
+
const api = at.origin === new URL(reach.api).origin;
|
|
127
|
+
let res;
|
|
128
|
+
try {
|
|
129
|
+
res = await fetch(at, { redirect: "manual", signal, headers: { accept, "user-agent": "agentrun-serve", "x-github-api-version": "2022-11-28", ...(api && reach.token ? { authorization: `Bearer ${reach.token}` } : {}) } });
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
throw new ImportRefused("github", error.name === "TimeoutError" || signal.aborted ? `GitHub did not finish answering in ${(reach.limits?.ms ?? IMPORT_LIMITS.ms) / 1000} s.` : `GitHub could not be reached: ${error.message}.`);
|
|
133
|
+
}
|
|
134
|
+
if (res.status < 300 || res.status >= 400)
|
|
135
|
+
return res;
|
|
136
|
+
await res.body?.cancel();
|
|
137
|
+
const location = res.headers.get("location");
|
|
138
|
+
if (!location || hop >= MAX_REDIRECTS)
|
|
139
|
+
throw new ImportRefused("github", `GitHub redirected the import ${hop >= MAX_REDIRECTS ? `more than ${MAX_REDIRECTS} times` : "with no Location"}.`);
|
|
140
|
+
at = new URL(location, at);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/** githubGet, read anonymously again when GitHub refuses the App's token for this repository: one its installation does
|
|
144
|
+
* not cover (401, 404), or one without contents read (a 403 that is not its rate limit; the server's token is a listing
|
|
145
|
+
* one, metadata read alone). A public repository reads either way; the rest of the import goes without the token. Only
|
|
146
|
+
* `reach` is changed. */
|
|
147
|
+
async function githubRead(url, reach, accept, signal) {
|
|
148
|
+
const res = await githubGet(url, reach, accept, signal);
|
|
149
|
+
if (!reach.token || !(res.status === 401 || res.status === 404 || (res.status === 403 && !limited(res, reach))))
|
|
150
|
+
return res;
|
|
151
|
+
await res.body?.cancel();
|
|
152
|
+
reach.anonymous = "its GitHub App's token was refused for this repository";
|
|
153
|
+
delete reach.token;
|
|
154
|
+
return githubGet(url, reach, accept, signal);
|
|
155
|
+
}
|
|
156
|
+
/** The full commit SHA `source`'s ref (its default branch when it names none) is at now: GitHub's answer, read before
|
|
157
|
+
* anything else, so what is imported is that one commit and its SHA is what is stored and shown. */
|
|
158
|
+
export async function resolveSha(source, reach, signal) {
|
|
159
|
+
const api = reach.api.replace(/\/+$/, "");
|
|
160
|
+
const res = await githubRead(`${api}/repos/${source.owner}/${source.repo}/commits/${encodeURIComponent(source.ref ?? "HEAD")}`, reach, "application/vnd.github.sha", signal);
|
|
161
|
+
const text = (await res.text()).trim();
|
|
162
|
+
const rate = limited(res, reach);
|
|
163
|
+
if (rate)
|
|
164
|
+
throw rate;
|
|
165
|
+
if (res.status === 404 || res.status === 422)
|
|
166
|
+
throw new ImportRefused("source", `GitHub has no ${source.ref ? `ref ${source.ref} in ` : ""}${source.owner}/${source.repo} that this server can read: it is not there, or it is private (a private repository can't be imported).`);
|
|
167
|
+
if (res.status !== 200 || !SHA.test(text))
|
|
168
|
+
throw new ImportRefused("github", `GitHub answered ${res.status} for ${source.owner}/${source.repo}'s commit ${source.ref ?? "HEAD"}.`);
|
|
169
|
+
return text;
|
|
170
|
+
}
|
|
171
|
+
/** The package's files at `sha`: the repository's tarball streamed from GitHub, unpacked as it comes, its folder
|
|
172
|
+
* `source.path` (the root when null) kept in memory by path under it; every other entry is counted and dropped. A link,
|
|
173
|
+
* a device or any other entry that is not a regular file or a folder in the package's folder refuses the import, as do a
|
|
174
|
+
* path that climbs out, a file over the package's limits, and a tarball past IMPORT_LIMITS. */
|
|
175
|
+
export async function fetchPackageFiles(source, sha, reach, signal) {
|
|
176
|
+
const limits = { ...IMPORT_LIMITS, ...reach.limits };
|
|
177
|
+
const api = reach.api.replace(/\/+$/, "");
|
|
178
|
+
const res = await githubRead(`${api}/repos/${source.owner}/${source.repo}/tarball/${sha}`, reach, "application/vnd.github+json", signal);
|
|
179
|
+
const rate = limited(res, reach);
|
|
180
|
+
if (rate)
|
|
181
|
+
throw (await res.body?.cancel(), rate);
|
|
182
|
+
if (res.status === 404)
|
|
183
|
+
throw (await res.body?.cancel(), new ImportRefused("source", `GitHub has no tarball of ${source.owner}/${source.repo} at ${sha} that this server can read.`));
|
|
184
|
+
if (res.status !== 200 || !res.body)
|
|
185
|
+
throw (await res.body?.cancel(), new ImportRefused("github", `GitHub answered ${res.status} for ${source.owner}/${source.repo}'s tarball.`));
|
|
186
|
+
const declared = Number(res.headers.get("content-length"));
|
|
187
|
+
if (Number.isFinite(declared) && declared > limits.downloadBytes)
|
|
188
|
+
throw (await res.body.cancel(), new ImportRefused("source", `${source.owner}/${source.repo}'s tarball is ${declared} bytes, over the ${limits.downloadBytes} an import reads.`));
|
|
189
|
+
return untarPackage(res.body, source, limits);
|
|
190
|
+
}
|
|
191
|
+
/** The tar stream `body` (gzip) unpacked as it comes, keeping the files under the archive's one top folder and
|
|
192
|
+
* `source.path`. A file outside the package is skipped unread, never held; one inside is held whole. */
|
|
193
|
+
async function untarPackage(body, source, limits) {
|
|
194
|
+
const files = new Map();
|
|
195
|
+
const folder = source.path ? `${source.path}/` : "";
|
|
196
|
+
let sent = 0;
|
|
197
|
+
let unpacked = 0;
|
|
198
|
+
let kept = 0;
|
|
199
|
+
let found = false;
|
|
200
|
+
const gunzip = createGunzip();
|
|
201
|
+
const reader = body.getReader();
|
|
202
|
+
// The gzip stream fed from the download, counted against its limit as it comes. A full gzip waits for its drain, or for
|
|
203
|
+
// its close: once the reader below stops (a refusal), it is destroyed and drains no more, and the pump ends there.
|
|
204
|
+
const pump = (async () => {
|
|
205
|
+
try {
|
|
206
|
+
for (;;) {
|
|
207
|
+
const { done, value } = await reader.read();
|
|
208
|
+
if (done || gunzip.destroyed)
|
|
209
|
+
break;
|
|
210
|
+
sent += value.byteLength;
|
|
211
|
+
if (sent > limits.downloadBytes)
|
|
212
|
+
throw new ImportRefused("source", `${source.owner}/${source.repo}'s tarball is over the ${limits.downloadBytes} bytes an import reads.`);
|
|
213
|
+
if (!gunzip.write(value))
|
|
214
|
+
await new Promise((go) => {
|
|
215
|
+
const on = () => { gunzip.off("drain", on); gunzip.off("close", on); go(); };
|
|
216
|
+
gunzip.on("drain", on);
|
|
217
|
+
gunzip.on("close", on);
|
|
218
|
+
});
|
|
219
|
+
if (gunzip.destroyed)
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
if (!gunzip.destroyed)
|
|
223
|
+
gunzip.end();
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
gunzip.destroy(error);
|
|
227
|
+
}
|
|
228
|
+
})();
|
|
229
|
+
const chunks = gunzip[Symbol.asyncIterator]();
|
|
230
|
+
// What has been unpacked and not read yet, as chunks, joined only when a read needs more than the first holds.
|
|
231
|
+
let held = [];
|
|
232
|
+
let heldBytes = 0;
|
|
233
|
+
const fill = async (n) => {
|
|
234
|
+
while (heldBytes < n) {
|
|
235
|
+
const next = await chunks.next();
|
|
236
|
+
if (next.done)
|
|
237
|
+
return false;
|
|
238
|
+
unpacked += next.value.length;
|
|
239
|
+
if (unpacked > limits.unpackedBytes)
|
|
240
|
+
throw new ImportRefused("source", `${source.owner}/${source.repo}'s tarball unpacks to over the ${limits.unpackedBytes} bytes an import reads.`);
|
|
241
|
+
held.push(next.value);
|
|
242
|
+
heldBytes += next.value.length;
|
|
243
|
+
}
|
|
244
|
+
return true;
|
|
245
|
+
};
|
|
246
|
+
const read = (n) => {
|
|
247
|
+
const all = held.length === 1 ? held[0] : Buffer.concat(held);
|
|
248
|
+
held = all.length > n ? [all.subarray(n)] : [];
|
|
249
|
+
heldBytes = all.length - n;
|
|
250
|
+
return all.subarray(0, n);
|
|
251
|
+
};
|
|
252
|
+
const skip = async (n) => {
|
|
253
|
+
while (n > 0) {
|
|
254
|
+
if (!heldBytes && !(await fill(1)))
|
|
255
|
+
throw new ImportRefused("github", "GitHub's tarball ended inside a file.");
|
|
256
|
+
const step = Math.min(n, heldBytes);
|
|
257
|
+
read(step);
|
|
258
|
+
n -= step;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
const ended = () => new ImportRefused("github", "GitHub's tarball ended inside an entry.");
|
|
262
|
+
let longName;
|
|
263
|
+
let paxPath;
|
|
264
|
+
let top;
|
|
265
|
+
try {
|
|
266
|
+
while (await fill(512)) {
|
|
267
|
+
const header = read(512);
|
|
268
|
+
if (header.every((byte) => byte === 0))
|
|
269
|
+
continue;
|
|
270
|
+
const entry = headerOf(header);
|
|
271
|
+
const padded = Math.ceil(entry.size / 512) * 512;
|
|
272
|
+
// A pax record (x, a path past 100 bytes; g, the archive's own, its commit) or a GNU long name: small, read whole.
|
|
273
|
+
if (entry.type === "x" || entry.type === "g" || entry.type === "L") {
|
|
274
|
+
if (entry.size > 1 << 20)
|
|
275
|
+
throw new ImportRefused("github", "GitHub's tarball has an extended header past what an import reads.");
|
|
276
|
+
if (!(await fill(padded)))
|
|
277
|
+
throw ended();
|
|
278
|
+
const data = Buffer.from(read(padded).subarray(0, entry.size));
|
|
279
|
+
if (entry.type === "x")
|
|
280
|
+
paxPath = paxField(data, "path") ?? paxPath;
|
|
281
|
+
if (entry.type === "L")
|
|
282
|
+
longName = data.toString("utf8").replace(/\0.*$/s, "");
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
const path = paxPath ?? longName ?? entry.path;
|
|
286
|
+
paxPath = undefined;
|
|
287
|
+
longName = undefined;
|
|
288
|
+
// GitHub's tarball holds one top folder, <owner>-<repo>-<short sha>/, and everything is under it.
|
|
289
|
+
const parts = path.split("/");
|
|
290
|
+
if (parts.length > 1 && parts.at(-1) === "")
|
|
291
|
+
parts.pop();
|
|
292
|
+
if (!parts.length || parts.some((part) => part === "" || part === "." || part === ".."))
|
|
293
|
+
throw new ImportRefused("package", `The tarball holds ${JSON.stringify(path)}, a path that is not plain: refused.`);
|
|
294
|
+
top ??= parts[0];
|
|
295
|
+
if (parts[0] !== top)
|
|
296
|
+
throw new ImportRefused("package", `The tarball holds ${JSON.stringify(path)}, outside its one top folder: refused.`);
|
|
297
|
+
const inRepo = parts.slice(1).join("/");
|
|
298
|
+
const mine = !folder || inRepo === source.path || inRepo.startsWith(folder);
|
|
299
|
+
if (!mine) {
|
|
300
|
+
await skip(padded);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
const own = folder ? (inRepo === source.path ? "" : inRepo.slice(folder.length)) : inRepo;
|
|
304
|
+
if (entry.type === "5") {
|
|
305
|
+
if (!own)
|
|
306
|
+
found = true;
|
|
307
|
+
await skip(padded);
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (!own)
|
|
311
|
+
throw new ImportRefused("source", `${source.path ?? "The root"} in ${source.owner}/${source.repo} is ${entry.type === "2" ? "a symbolic link" : "a file"}, not a package's folder.`);
|
|
312
|
+
if (entry.type !== "0" && entry.type !== "\0" && entry.type !== "7")
|
|
313
|
+
throw new ImportRefused("package", `${own} is ${entry.type === "2" ? "a symbolic link" : entry.type === "1" ? "a hard link" : "not a regular file or a folder"}: a package holds files and folders alone, so the import was refused.`);
|
|
314
|
+
found = true;
|
|
315
|
+
kept += entry.size;
|
|
316
|
+
if (kept > limits.packageBytes)
|
|
317
|
+
throw new ImportRefused("package", `The package's files are over the ${limits.packageBytes} bytes an import keeps.`);
|
|
318
|
+
if (files.size >= limits.files)
|
|
319
|
+
throw new ImportRefused("package", `The package holds over ${limits.files} files.`);
|
|
320
|
+
if (!(await fill(padded)))
|
|
321
|
+
throw ended();
|
|
322
|
+
files.set(own, Buffer.from(read(padded).subarray(0, entry.size)));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
reader.cancel().catch(() => undefined);
|
|
327
|
+
gunzip.destroy();
|
|
328
|
+
await pump.catch(() => undefined);
|
|
329
|
+
if (error instanceof ImportRefused)
|
|
330
|
+
throw error;
|
|
331
|
+
throw new ImportRefused("github", `GitHub's tarball could not be read: ${error.message}.`);
|
|
332
|
+
}
|
|
333
|
+
await pump;
|
|
334
|
+
if (!found)
|
|
335
|
+
throw new ImportRefused("source", `${source.owner}/${source.repo} has no folder ${source.path ?? "(its root)"} at that commit.`);
|
|
336
|
+
return files;
|
|
337
|
+
}
|
|
338
|
+
/** A ustar header's path (its prefix and name), type flag and size. A size in base-256 or past 8 GiB is refused. */
|
|
339
|
+
function headerOf(header) {
|
|
340
|
+
const field = (from, length) => header.subarray(from, from + length).toString("utf8").replace(/\0.*$/s, "");
|
|
341
|
+
const sum = header.reduce((total, byte, i) => total + (i >= 148 && i < 156 ? 32 : byte), 0);
|
|
342
|
+
if (Number.parseInt(field(148, 8).trim(), 8) !== sum)
|
|
343
|
+
throw new ImportRefused("github", "GitHub's tarball has a header whose checksum does not match.");
|
|
344
|
+
if (header[124] & 0x80)
|
|
345
|
+
throw new ImportRefused("package", "The tarball holds a file too big for an import.");
|
|
346
|
+
const size = Number.parseInt(field(124, 12).trim() || "0", 8);
|
|
347
|
+
if (!Number.isSafeInteger(size) || size < 0)
|
|
348
|
+
throw new ImportRefused("github", "GitHub's tarball has a header with no size.");
|
|
349
|
+
const name = field(0, 100);
|
|
350
|
+
const prefix = field(257, 6).startsWith("ustar") ? field(345, 155) : "";
|
|
351
|
+
return { path: prefix ? `${prefix}/${name}` : name, type: String.fromCharCode(header[156] || 48), size };
|
|
352
|
+
}
|
|
353
|
+
/** A pax extended header's `key` record, or undefined. */
|
|
354
|
+
function paxField(data, key) {
|
|
355
|
+
let at = 0;
|
|
356
|
+
let found;
|
|
357
|
+
while (at < data.length) {
|
|
358
|
+
const space = data.indexOf(0x20, at);
|
|
359
|
+
if (space < 0)
|
|
360
|
+
break;
|
|
361
|
+
const length = Number.parseInt(data.subarray(at, space).toString("utf8"), 10);
|
|
362
|
+
if (!Number.isSafeInteger(length) || length <= 0)
|
|
363
|
+
break;
|
|
364
|
+
const record = data.subarray(space + 1, at + length - 1).toString("utf8");
|
|
365
|
+
const eq = record.indexOf("=");
|
|
366
|
+
if (eq > 0 && record.slice(0, eq) === key)
|
|
367
|
+
found = record.slice(eq + 1);
|
|
368
|
+
at += length;
|
|
369
|
+
}
|
|
370
|
+
return found;
|
|
371
|
+
}
|