stitchkit 0.132.3 → 0.133.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +172 -1
- package/dist/agent-executor/acp/v1-connect.d.ts +12 -1
- package/dist/agent-executor/acp/v1-connect.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-driver.d.ts +3 -1
- package/dist/agent-executor/acp/v1-driver.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-handlers.d.ts +9 -1
- package/dist/agent-executor/acp/v1-handlers.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-permission.d.ts +3 -2
- package/dist/agent-executor/acp/v1-permission.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-probe.d.ts +10 -1
- package/dist/agent-executor/acp/v1-probe.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-session.d.ts +11 -2
- package/dist/agent-executor/acp/v1-session.d.ts.map +1 -1
- package/dist/agent-executor/acp/v1-turn.d.ts +13 -10
- package/dist/agent-executor/acp/v1-turn.d.ts.map +1 -1
- package/dist/agent-executor/claude/cli-driver.d.ts +6 -0
- package/dist/agent-executor/claude/cli-driver.d.ts.map +1 -1
- package/dist/agent-executor/claude/record-router.d.ts +44 -0
- package/dist/agent-executor/claude/record-router.d.ts.map +1 -0
- package/dist/agent-executor/claude/session.d.ts +2 -0
- package/dist/agent-executor/claude/session.d.ts.map +1 -1
- package/dist/agent-executor/claude-sdk/driver.d.ts +20 -2
- package/dist/agent-executor/claude-sdk/driver.d.ts.map +1 -1
- package/dist/agent-executor/claude-sdk/engine-spawn.d.ts +18 -0
- package/dist/agent-executor/claude-sdk/engine-spawn.d.ts.map +1 -0
- package/dist/agent-executor/claude-sdk/query-loop.d.ts +8 -11
- package/dist/agent-executor/claude-sdk/query-loop.d.ts.map +1 -1
- package/dist/agent-executor/claude-sdk/query-manager.d.ts +4 -2
- package/dist/agent-executor/claude-sdk/query-manager.d.ts.map +1 -1
- package/dist/agent-executor/claude-sdk/query-options.d.ts +1 -0
- package/dist/agent-executor/claude-sdk/query-options.d.ts.map +1 -1
- package/dist/agent-executor/claude-sdk/session.d.ts.map +1 -1
- package/dist/agent-executor/codex/capabilities.d.ts +8 -0
- package/dist/agent-executor/codex/capabilities.d.ts.map +1 -0
- package/dist/agent-executor/codex/connect.d.ts +6 -1
- package/dist/agent-executor/codex/connect.d.ts.map +1 -1
- package/dist/agent-executor/codex/driver.d.ts +10 -0
- package/dist/agent-executor/codex/driver.d.ts.map +1 -1
- package/dist/agent-executor/codex/event-routing.d.ts +23 -1
- package/dist/agent-executor/codex/event-routing.d.ts.map +1 -1
- package/dist/agent-executor/codex/handlers.d.ts +23 -3
- package/dist/agent-executor/codex/handlers.d.ts.map +1 -1
- package/dist/agent-executor/codex/limits.d.ts +17 -0
- package/dist/agent-executor/codex/limits.d.ts.map +1 -1
- package/dist/agent-executor/codex/request-answers.d.ts +23 -3
- package/dist/agent-executor/codex/request-answers.d.ts.map +1 -1
- package/dist/agent-executor/codex/session-bind.d.ts +6 -1
- package/dist/agent-executor/codex/session-bind.d.ts.map +1 -1
- package/dist/agent-executor/codex/session-types.d.ts +5 -1
- package/dist/agent-executor/codex/session-types.d.ts.map +1 -1
- package/dist/agent-executor/codex/session.d.ts.map +1 -1
- package/dist/agent-executor/codex/shared-server.d.ts.map +1 -1
- package/dist/agent-executor/codex/shared-thread.d.ts +6 -8
- package/dist/agent-executor/codex/shared-thread.d.ts.map +1 -1
- package/dist/agent-executor/codex/thread-open.d.ts +12 -3
- package/dist/agent-executor/codex/thread-open.d.ts.map +1 -1
- package/dist/agent-executor/engine-requests.d.ts +42 -0
- package/dist/agent-executor/engine-requests.d.ts.map +1 -1
- package/dist/agent-executor/handoff-schemas.d.ts.map +1 -1
- package/dist/agent-executor/handoff.d.ts.map +1 -1
- package/dist/agent-executor/image-input.d.ts +6 -5
- package/dist/agent-executor/image-input.d.ts.map +1 -1
- package/dist/agent-executor/profile.d.ts +3 -5
- package/dist/agent-executor/profile.d.ts.map +1 -1
- package/dist/agent-executor/session-primitives.d.ts +64 -3
- package/dist/agent-executor/session-primitives.d.ts.map +1 -1
- package/dist/agent-executor/testing/claude-sdk-stubs.d.ts +5 -0
- package/dist/agent-executor/testing/claude-sdk-stubs.d.ts.map +1 -0
- package/dist/agent-executor/testing/conformance-lifecycle.d.ts +11 -0
- package/dist/agent-executor/testing/conformance-lifecycle.d.ts.map +1 -0
- package/dist/agent-executor/testing/conformance-support.d.ts +23 -0
- package/dist/agent-executor/testing/conformance-support.d.ts.map +1 -0
- package/dist/agent-executor/testing/conformance.d.ts +7 -2
- package/dist/agent-executor/testing/conformance.d.ts.map +1 -1
- package/dist/agent-executor/testing/consumer-stubs.d.ts +5 -3
- package/dist/agent-executor/testing/consumer-stubs.d.ts.map +1 -1
- package/dist/agent-executor/testing/fake-codex-app-server.d.ts +5 -1
- package/dist/agent-executor/testing/fake-codex-app-server.d.ts.map +1 -1
- package/dist/agent-executor/testing/replay-driver.d.ts +6 -0
- package/dist/agent-executor/testing/replay-driver.d.ts.map +1 -1
- package/dist/agent-executor/testing/replay-gate.d.ts +4 -1
- package/dist/agent-executor/testing/replay-gate.d.ts.map +1 -1
- package/dist/agent-runtime/deferred-tool-types.d.ts +2 -0
- package/dist/agent-runtime/deferred-tool-types.d.ts.map +1 -1
- package/dist/agent-runtime/deferred-tools.d.ts.map +1 -1
- package/dist/browser/client.d.ts.map +1 -1
- package/dist/browser/operation-refusal.d.ts +10 -0
- package/dist/browser/operation-refusal.d.ts.map +1 -0
- package/dist/contract/client-types.d.ts +13 -4
- package/dist/contract/client-types.d.ts.map +1 -1
- package/dist/contract/define-validation.d.ts +6 -0
- package/dist/contract/define-validation.d.ts.map +1 -1
- package/dist/contract/define.d.ts +3 -0
- package/dist/contract/define.d.ts.map +1 -1
- package/dist/contract/errors-factory.d.ts +8 -2
- package/dist/contract/errors-factory.d.ts.map +1 -1
- package/dist/contract/errors.d.ts +9 -0
- package/dist/contract/errors.d.ts.map +1 -1
- package/dist/contract/fingerprint.d.ts +4 -1
- package/dist/contract/fingerprint.d.ts.map +1 -1
- package/dist/contract/operation-refusals.d.ts +51 -0
- package/dist/contract/operation-refusals.d.ts.map +1 -0
- package/dist/contract/tool-options.d.ts +4 -0
- package/dist/contract/tool-options.d.ts.map +1 -1
- package/dist/entrypoints/agent-executor/acp.js +253 -210
- package/dist/entrypoints/agent-executor/claude-sdk.js +248 -173
- package/dist/entrypoints/agent-executor/claude.js +57 -109
- package/dist/entrypoints/agent-executor/codex.js +412 -335
- package/dist/entrypoints/agent-executor/testing/claude-sdk.d.ts +3 -0
- package/dist/entrypoints/agent-executor/testing/claude-sdk.d.ts.map +1 -0
- package/dist/entrypoints/agent-executor/testing/claude-sdk.js +329 -0
- package/dist/entrypoints/agent-executor/testing.d.ts +1 -2
- package/dist/entrypoints/agent-executor/testing.d.ts.map +1 -1
- package/dist/entrypoints/agent-executor/testing.js +413 -667
- package/dist/entrypoints/agent-executor.d.ts +1 -1
- package/dist/entrypoints/agent-executor.d.ts.map +1 -1
- package/dist/entrypoints/agent-executor.js +12 -12
- package/dist/entrypoints/agent-runtime/browser.js +2 -2
- package/dist/entrypoints/agent-runtime/coding-tools.js +3 -3
- package/dist/entrypoints/agent-runtime/harness.js +8 -8
- package/dist/entrypoints/agent-runtime/postgres.js +5 -5
- package/dist/entrypoints/agent-runtime/realtime.js +2 -2
- package/dist/entrypoints/agent-runtime/sandbox.js +3 -3
- package/dist/entrypoints/agent-runtime/sqlite/bun.js +6 -6
- package/dist/entrypoints/agent-runtime/sqlite/node.js +6 -6
- package/dist/entrypoints/agent-runtime/testing.js +5 -5
- package/dist/entrypoints/agent-runtime.js +26 -18
- package/dist/entrypoints/application/grammy.js +1 -1
- package/dist/entrypoints/application.js +2 -2
- package/dist/entrypoints/cli.js +8 -8
- package/dist/entrypoints/contract.d.ts +3 -2
- package/dist/entrypoints/contract.d.ts.map +1 -1
- package/dist/entrypoints/contract.js +6 -4
- package/dist/entrypoints/files/packaging.d.ts +4 -1
- package/dist/entrypoints/files/packaging.d.ts.map +1 -1
- package/dist/entrypoints/files/packaging.js +868 -167
- package/dist/entrypoints/files.js +1 -1
- package/dist/entrypoints/handler.js +7 -7
- package/dist/entrypoints/http.js +2 -2
- package/dist/entrypoints/index.js +10 -8
- package/dist/entrypoints/node.js +11 -11
- package/dist/entrypoints/observability.js +1 -1
- package/dist/entrypoints/process/bootstrap.d.ts +3 -0
- package/dist/entrypoints/process/bootstrap.d.ts.map +1 -0
- package/dist/entrypoints/process/bootstrap.js +16 -0
- package/dist/entrypoints/process.js +7 -7
- package/dist/entrypoints/realtime.js +3 -3
- package/dist/entrypoints/remote.js +8 -7
- package/dist/entrypoints/server.js +30 -14
- package/dist/entrypoints/service-host/macos.js +72 -206
- package/dist/entrypoints/service-host/work.d.ts +15 -0
- package/dist/entrypoints/service-host/work.d.ts.map +1 -0
- package/dist/entrypoints/service-host/work.js +676 -0
- package/dist/entrypoints/service-host.js +146 -536
- package/dist/entrypoints/telegram/screens.js +4 -4
- package/dist/entrypoints/telegram.js +4 -4
- package/dist/entrypoints/testing.d.ts +2 -1
- package/dist/entrypoints/testing.d.ts.map +1 -1
- package/dist/entrypoints/testing.js +27 -8
- package/dist/entrypoints/tools/agent.js +6 -6
- package/dist/entrypoints/tools/connections.js +2 -2
- package/dist/entrypoints/tools/invoker.js +5 -5
- package/dist/entrypoints/tools/mcp.js +8 -6
- package/dist/entrypoints/tools/toolkit.js +11 -9
- package/dist/entrypoints/tools.js +19 -17
- package/dist/entrypoints/tracking/contract.js +1 -1
- package/dist/entrypoints/tracking/performance.js +3 -3
- package/dist/files/client-artifact.d.ts +14 -42
- package/dist/files/client-artifact.d.ts.map +1 -1
- package/dist/files/client-build.d.ts +57 -0
- package/dist/files/client-build.d.ts.map +1 -0
- package/dist/files/client-declarations.d.ts +35 -0
- package/dist/files/client-declarations.d.ts.map +1 -0
- package/dist/files/client-package-contract.d.ts +162 -0
- package/dist/files/client-package-contract.d.ts.map +1 -0
- package/dist/files/client-package-proof.d.ts +38 -0
- package/dist/files/client-package-proof.d.ts.map +1 -0
- package/dist/files/client-package.d.ts +17 -0
- package/dist/files/client-package.d.ts.map +1 -0
- package/dist/files/declaration-specifiers.d.ts +30 -0
- package/dist/files/declaration-specifiers.d.ts.map +1 -0
- package/dist/{index-v759mzed.js → index-02r5sq9p.js} +36 -23
- package/dist/{index-0r15pkjq.js → index-0k1ba0ss.js} +1 -1
- package/dist/{index-znd1b57f.js → index-1dn2wjq6.js} +5 -4
- package/dist/{index-p06z7g7w.js → index-1p8q0yrt.js} +6 -5
- package/dist/{index-wa6h4tz6.js → index-1qwd54n3.js} +6 -5
- package/dist/index-2dzc7mv9.js +174 -0
- package/dist/index-2mz8hj58.js +155 -0
- package/dist/{index-17qgm6xe.js → index-34k82crg.js} +2 -2
- package/dist/index-3mcbr8sx.js +349 -0
- package/dist/{index-ybdq8kk2.js → index-3y2tzhr8.js} +3 -3
- package/dist/{index-fgahdc7s.js → index-5n3a8hd8.js} +5 -3
- package/dist/{index-jb4m241t.js → index-6918kxbf.js} +45 -1
- package/dist/{index-n3bm7zqq.js → index-6kmjcm81.js} +4 -4
- package/dist/{index-akf0xkrm.js → index-78da4g74.js} +1 -1
- package/dist/{index-9pv126eh.js → index-7fjbdga8.js} +7 -1
- package/dist/{index-zhshhxew.js → index-8jf4bt7v.js} +1 -1
- package/dist/{index-p16q492v.js → index-8t9dr41h.js} +18 -5
- package/dist/{index-xq4s9mka.js → index-8yt3rwe7.js} +1 -1
- package/dist/{index-39c2hj6h.js → index-9c76vfxf.js} +7 -3
- package/dist/index-9ft4n9j1.js +432 -0
- package/dist/{index-sd4e5x5j.js → index-9wdrwx3b.js} +8 -7
- package/dist/{index-z114ax5e.js → index-an7vcmwf.js} +49 -2
- package/dist/{index-pzhqydxb.js → index-d04w81fz.js} +1 -1
- package/dist/index-d2drhb9c.js +219 -0
- package/dist/{index-8gmmdz7k.js → index-dacxr7nf.js} +1 -1
- package/dist/{index-dnzxxf8a.js → index-dm6nxkn1.js} +1 -1
- package/dist/index-dxft2mnm.js +42 -0
- package/dist/{index-rxh3qy4x.js → index-fzd477xk.js} +1 -1
- package/dist/{index-xgae0bn7.js → index-g4gstmp1.js} +2 -2
- package/dist/{index-athc412x.js → index-g5fgeb6v.js} +1 -1
- package/dist/{index-z9d74jmb.js → index-gbhj370g.js} +3 -3
- package/dist/{index-3fym4j8n.js → index-gtaaxe9d.js} +1 -1
- package/dist/{index-v59pxmf1.js → index-jpecjm30.js} +2 -2
- package/dist/index-k9g061ma.js +164 -0
- package/dist/{index-669374qc.js → index-ma26mfnt.js} +84 -315
- package/dist/{index-ryxb427v.js → index-msxf2r8x.js} +16 -3
- package/dist/{index-rbd2rq3z.js → index-nr4fhfn4.js} +1 -1
- package/dist/{index-qa2jjb65.js → index-pbekeqzm.js} +1 -1
- package/dist/{index-mkfhpk11.js → index-pq6g1k9t.js} +2 -2
- package/dist/{index-gya4hwh3.js → index-pws5c3x6.js} +21 -1
- package/dist/{index-x2fb37t0.js → index-q427q23r.js} +3 -2
- package/dist/{index-jcrj0b8z.js → index-qggqqja2.js} +39 -2
- package/dist/{index-jxtg307y.js → index-rkjfzs0a.js} +1 -1
- package/dist/{index-5tdc0q43.js → index-rw8ze745.js} +5 -3
- package/dist/index-s43tr4my.js +129 -0
- package/dist/{index-f9p62ean.js → index-stpy56r3.js} +175 -249
- package/dist/index-t5g5rm9n.js +213 -0
- package/dist/{index-z20pdxeb.js → index-tmv3ez1h.js} +3 -3
- package/dist/{index-dcsxj68j.js → index-tqjamnax.js} +1 -1
- package/dist/{index-j15dhkda.js → index-wddcfbng.js} +1 -325
- package/dist/{index-9sfhje6y.js → index-wh70tmpd.js} +24 -234
- package/dist/index-wxjv4sqx.js +82 -0
- package/dist/{index-qhjmmsm2.js → index-x46y9z26.js} +2 -2
- package/dist/{index-nk0cy9g1.js → index-y53cqwke.js} +1 -1
- package/dist/{index-sv5svtq7.js → index-y68gavb5.js} +1 -1
- package/dist/{index-5y7j383h.js → index-yrhw10ef.js} +1 -1
- package/dist/process/bootstrap.d.ts +12 -0
- package/dist/process/bootstrap.d.ts.map +1 -0
- package/dist/process/owner-loss-guard.d.ts +2 -9
- package/dist/process/owner-loss-guard.d.ts.map +1 -1
- package/dist/process/work-root.d.ts +20 -0
- package/dist/process/work-root.d.ts.map +1 -0
- package/dist/server/contract-identity.d.ts.map +1 -1
- package/dist/server/contract-method.d.ts +12 -0
- package/dist/server/contract-method.d.ts.map +1 -1
- package/dist/server/implement.d.ts.map +1 -1
- package/dist/server/openapi.d.ts.map +1 -1
- package/dist/server/types.d.ts +10 -9
- package/dist/server/types.d.ts.map +1 -1
- package/dist/service-host/host-adapter.d.ts +2 -0
- package/dist/service-host/host-adapter.d.ts.map +1 -1
- package/dist/service-host/work/command.d.ts +72 -0
- package/dist/service-host/work/command.d.ts.map +1 -0
- package/dist/service-host/work/errors.d.ts +29 -0
- package/dist/service-host/work/errors.d.ts.map +1 -0
- package/dist/service-host/work/host.d.ts +38 -0
- package/dist/service-host/work/host.d.ts.map +1 -0
- package/dist/service-host/work/launch.d.ts +51 -0
- package/dist/service-host/work/launch.d.ts.map +1 -0
- package/dist/service-host/work/membership.d.ts +51 -0
- package/dist/service-host/work/membership.d.ts.map +1 -0
- package/dist/service-host/work/origin.d.ts +79 -0
- package/dist/service-host/work/origin.d.ts.map +1 -0
- package/dist/service-host/work/owner.d.ts +45 -0
- package/dist/service-host/work/owner.d.ts.map +1 -0
- package/dist/service-host/work/placement.d.ts +55 -0
- package/dist/service-host/work/placement.d.ts.map +1 -0
- package/dist/service-host/work/probe.d.ts +24 -0
- package/dist/service-host/work/probe.d.ts.map +1 -0
- package/dist/service-host/work/scope-command.d.ts +68 -0
- package/dist/service-host/work/scope-command.d.ts.map +1 -0
- package/dist/sync/session.d.ts +4 -1
- package/dist/sync/session.d.ts.map +1 -1
- package/dist/testing/surface-manifest-digest.d.ts +2 -0
- package/dist/testing/surface-manifest-digest.d.ts.map +1 -1
- package/dist/testing/surface-manifest.d.ts +32 -3
- package/dist/testing/surface-manifest.d.ts.map +1 -1
- package/dist/tools/agent.d.ts +2 -0
- package/dist/tools/agent.d.ts.map +1 -1
- package/dist/tools/cli/stream-surface.d.ts.map +1 -1
- package/dist/tools/internal/mcp-projection.d.ts +58 -0
- package/dist/tools/internal/mcp-projection.d.ts.map +1 -0
- package/dist/tools/internal/surface-projector.d.ts +3 -47
- package/dist/tools/internal/surface-projector.d.ts.map +1 -1
- package/dist/tools/invoker.d.ts +2 -0
- package/dist/tools/invoker.d.ts.map +1 -1
- package/dist/tools/manifest.d.ts +2 -0
- package/dist/tools/manifest.d.ts.map +1 -1
- package/dist/tools/mcp/handler.d.ts.map +1 -1
- package/dist/tools/mcp/lazy-discovery.d.ts.map +1 -1
- package/dist/tools/mcp/mount.d.ts +4 -0
- package/dist/tools/mcp/mount.d.ts.map +1 -1
- package/dist/tools/mcp/prepare.d.ts +5 -2
- package/dist/tools/mcp/prepare.d.ts.map +1 -1
- package/dist/tools/mount.d.ts +2 -0
- package/dist/tools/mount.d.ts.map +1 -1
- package/dist/tools/remote.d.ts.map +1 -1
- package/dist/tools/schema/nesting-limit.d.ts +8 -0
- package/dist/tools/schema/nesting-limit.d.ts.map +1 -0
- package/dist/tools/schema/presentation.d.ts +7 -0
- package/dist/tools/schema/presentation.d.ts.map +1 -1
- package/dist/tools/surface.d.ts.map +1 -1
- package/llms/agent-executor-acp.txt +3 -3
- package/llms/agent-executor-claude-sdk.txt +1 -1
- package/llms/agent-executor-claude.txt +1 -1
- package/llms/agent-executor-codex.txt +1 -1
- package/llms/agent-executor-testing-claude-sdk.txt +28 -0
- package/llms/agent-executor-testing.txt +7 -8
- package/llms/agent-executor.txt +27 -4
- package/llms/agent-runtime.txt +1 -1
- package/llms/contract.txt +8 -2
- package/llms/files-packaging.txt +11 -6
- package/llms/process-bootstrap.txt +18 -0
- package/llms/process.2.txt +215 -18
- package/llms/process.txt +130 -92
- package/llms/server.4.txt +49 -1
- package/llms/service-host-work.txt +34 -0
- package/llms/stitchkit.2.txt +31 -0
- package/llms/stitchkit.txt +78 -75
- package/llms/sync.txt +5 -2
- package/llms/testing.txt +1 -0
- package/llms/tools.2.txt +747 -775
- package/llms/tools.3.txt +846 -210
- package/llms/tools.4.txt +226 -0
- package/llms/tools.txt +3 -790
- package/llms/{upgrading-0.128.0-0.132.0.txt → upgrading-0.128.0-0.133.0.txt} +194 -6
- package/llms/upgrading.txt +1 -1
- package/llms-full.txt +746 -135
- package/llms.txt +18 -16
- package/package.json +12 -4
- package/dist/entrypoints/process/owner-loss.d.ts +0 -3
- package/dist/entrypoints/process/owner-loss.d.ts.map +0 -1
- package/dist/entrypoints/process/owner-loss.js +0 -7
- package/llms/process-owner-loss.txt +0 -18
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import"../../index-
|
|
2
|
-
import"../../index-
|
|
1
|
+
import"../../index-jpecjm30.js";
|
|
2
|
+
import"../../index-pbekeqzm.js";
|
|
3
3
|
import {
|
|
4
4
|
NATIVE_NOT_PACKAGED
|
|
5
5
|
} from "../../index-6kec3b14.js";
|
|
6
6
|
import {
|
|
7
7
|
runNativeCommand2
|
|
8
|
-
} from "../../index-
|
|
8
|
+
} from "../../index-d04w81fz.js";
|
|
9
9
|
import {
|
|
10
10
|
safeJsonParse
|
|
11
11
|
} from "../../index-zsarb5xp.js";
|
|
@@ -15,9 +15,13 @@ import {
|
|
|
15
15
|
|
|
16
16
|
// src/files/client-artifact.ts
|
|
17
17
|
import { createHash } from "node:crypto";
|
|
18
|
-
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
18
|
+
import { mkdir, mkdtemp, readFile as readFile2, rm, writeFile } from "node:fs/promises";
|
|
19
19
|
import { tmpdir } from "node:os";
|
|
20
|
-
import { basename, dirname, join, resolve } from "node:path";
|
|
20
|
+
import { basename, dirname, join, resolve as resolve2 } from "node:path";
|
|
21
|
+
|
|
22
|
+
// src/files/client-build.ts
|
|
23
|
+
import { readFile } from "node:fs/promises";
|
|
24
|
+
import { resolve } from "node:path";
|
|
21
25
|
import { z } from "zod";
|
|
22
26
|
class ForbiddenModuleError extends Error {
|
|
23
27
|
module;
|
|
@@ -29,12 +33,59 @@ class ForbiddenModuleError extends Error {
|
|
|
29
33
|
this.name = "ForbiddenModuleError";
|
|
30
34
|
}
|
|
31
35
|
}
|
|
36
|
+
async function runClientTool(step, executable, args, cwd) {
|
|
37
|
+
const result = await runNativeCommand2({
|
|
38
|
+
executable,
|
|
39
|
+
args: [...args],
|
|
40
|
+
cwd,
|
|
41
|
+
envPolicy: "ambient",
|
|
42
|
+
capture: true,
|
|
43
|
+
maxOutputBytes: 4 * 1024 * 1024,
|
|
44
|
+
timeoutMs: step.timeoutMs,
|
|
45
|
+
signal: step.signal
|
|
46
|
+
});
|
|
47
|
+
const decoder = new TextDecoder;
|
|
48
|
+
const stdout = decoder.decode(result.stdout);
|
|
49
|
+
return {
|
|
50
|
+
ok: result.exitCode === 0,
|
|
51
|
+
exit: String(result.exitCode ?? result.signal),
|
|
52
|
+
stdout,
|
|
53
|
+
output: `${decoder.decode(result.stderr)}${stdout}`.trim()
|
|
54
|
+
};
|
|
55
|
+
}
|
|
32
56
|
var MetafileSchema = z.object({
|
|
33
57
|
inputs: z.record(z.string(), z.object({
|
|
34
|
-
imports: z.array(z.object({
|
|
35
|
-
|
|
58
|
+
imports: z.array(z.object({
|
|
59
|
+
path: z.string(),
|
|
60
|
+
original: z.string().optional(),
|
|
61
|
+
external: z.boolean().default(false)
|
|
62
|
+
})).default([])
|
|
63
|
+
})),
|
|
64
|
+
outputs: z.record(z.string(), z.object({
|
|
65
|
+
entryPoint: z.string().optional(),
|
|
66
|
+
exports: z.array(z.string()).default([])
|
|
67
|
+
})).default({})
|
|
36
68
|
});
|
|
37
|
-
function
|
|
69
|
+
async function readClientBuildGraph(metafilePath, root) {
|
|
70
|
+
const metafile = MetafileSchema.parse(safeJsonParse(await readFile(metafilePath, "utf8")));
|
|
71
|
+
const absolute = (path) => resolve(root, path).replaceAll("\\", "/");
|
|
72
|
+
const modules = new Map;
|
|
73
|
+
for (const [path, input] of Object.entries(metafile.inputs)) {
|
|
74
|
+
modules.set(absolute(path), input.imports.map((imported) => ({
|
|
75
|
+
path: imported.external ? imported.path : absolute(imported.path),
|
|
76
|
+
original: imported.original,
|
|
77
|
+
external: imported.external
|
|
78
|
+
})));
|
|
79
|
+
}
|
|
80
|
+
const entryOutputs = new Map;
|
|
81
|
+
for (const [path, output] of Object.entries(metafile.outputs)) {
|
|
82
|
+
if (output.entryPoint === undefined)
|
|
83
|
+
continue;
|
|
84
|
+
entryOutputs.set(absolute(output.entryPoint), path.replace(/^\.\//, ""));
|
|
85
|
+
}
|
|
86
|
+
return { modules, entryOutputs };
|
|
87
|
+
}
|
|
88
|
+
function matchesPath(rule, modulePath) {
|
|
38
89
|
if (rule instanceof RegExp)
|
|
39
90
|
return rule.test(modulePath);
|
|
40
91
|
if (rule.startsWith("@") || !rule.includes("/")) {
|
|
@@ -42,161 +93,809 @@ function matches(rule, modulePath) {
|
|
|
42
93
|
}
|
|
43
94
|
return modulePath.includes(rule.startsWith("/") ? rule : `/${rule}`);
|
|
44
95
|
}
|
|
45
|
-
function absoluteModules(metafile, root) {
|
|
46
|
-
const absolute = (path) => resolve(root, path).replaceAll("\\", "/");
|
|
47
|
-
return {
|
|
48
|
-
inputs: Object.fromEntries(Object.entries(metafile.inputs).map(([path, input]) => [
|
|
49
|
-
absolute(path),
|
|
50
|
-
{
|
|
51
|
-
imports: input.imports.map((imported) => ({
|
|
52
|
-
path: absolute(imported.path),
|
|
53
|
-
original: imported.original
|
|
54
|
-
}))
|
|
55
|
-
}
|
|
56
|
-
]))
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
96
|
function namesPackage(specifier, rule) {
|
|
60
97
|
return typeof rule === "string" && specifier !== undefined && (specifier === rule || specifier.startsWith(`${rule}/`));
|
|
61
98
|
}
|
|
62
|
-
function
|
|
63
|
-
for (const [importer,
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
99
|
+
function refuseForbiddenModules(graph, forbidden) {
|
|
100
|
+
for (const [importer, imports] of graph.modules) {
|
|
101
|
+
for (const imported of imports) {
|
|
102
|
+
const specifier = imported.external ? imported.path : imported.original;
|
|
103
|
+
if (forbidden.some((rule) => namesPackage(specifier, rule))) {
|
|
104
|
+
throw new ForbiddenModuleError(imported.path, importer);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
67
107
|
}
|
|
68
|
-
for (const modulePath of
|
|
69
|
-
if (!forbidden.some((rule) =>
|
|
108
|
+
for (const modulePath of graph.modules.keys()) {
|
|
109
|
+
if (!forbidden.some((rule) => matchesPath(rule, modulePath)))
|
|
70
110
|
continue;
|
|
71
|
-
const importer =
|
|
111
|
+
const importer = [...graph.modules].find(([, imports]) => imports.some((imported) => !imported.external && imported.path === modulePath));
|
|
72
112
|
throw new ForbiddenModuleError(modulePath, importer?.[0]);
|
|
73
113
|
}
|
|
74
114
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
executable: step.bun,
|
|
78
|
-
args,
|
|
79
|
-
cwd,
|
|
80
|
-
envPolicy: "ambient",
|
|
81
|
-
capture: true,
|
|
82
|
-
maxOutputBytes: 4 * 1024 * 1024,
|
|
83
|
-
timeoutMs: step.timeoutMs,
|
|
84
|
-
signal: step.signal
|
|
85
|
-
});
|
|
86
|
-
const decoder = new TextDecoder;
|
|
87
|
-
if (result.exitCode !== 0) {
|
|
88
|
-
throw new Error(`Client artifact ${what} failed (exit ${result.exitCode ?? result.signal}): ` + `${decoder.decode(result.stderr)}${decoder.decode(result.stdout)}`.trim());
|
|
89
|
-
}
|
|
90
|
-
return decoder.decode(result.stdout);
|
|
91
|
-
}
|
|
115
|
+
|
|
116
|
+
// src/files/client-artifact.ts
|
|
92
117
|
async function buildClientArtifact(options) {
|
|
93
|
-
const
|
|
94
|
-
bun: options.executable ?? "bun",
|
|
95
|
-
timeoutMs: options.timeoutMs ?? 120000,
|
|
96
|
-
signal: options.signal
|
|
97
|
-
};
|
|
98
|
-
const file = resolve(options.outFile);
|
|
118
|
+
const file = resolve2(options.outFile);
|
|
99
119
|
const sha256File = `${file}.sha256`;
|
|
100
120
|
const work = await mkdtemp(join(tmpdir(), "stitchkit-client-artifact-"));
|
|
101
121
|
try {
|
|
102
122
|
await rm(file, { force: true });
|
|
103
123
|
await rm(sha256File, { force: true });
|
|
104
|
-
const root = dirname(
|
|
124
|
+
const root = dirname(resolve2(options.entry));
|
|
105
125
|
const built = join(work, "client.js");
|
|
106
126
|
const metafilePath = join(work, "meta.json");
|
|
107
|
-
await
|
|
127
|
+
const build = await runClientTool({ timeoutMs: options.timeoutMs ?? 120000, signal: options.signal }, options.executables?.bun ?? "bun", [
|
|
108
128
|
"build",
|
|
109
|
-
|
|
129
|
+
resolve2(options.entry),
|
|
110
130
|
`--target=${options.runtime ?? "node"}`,
|
|
111
131
|
"--format=esm",
|
|
112
132
|
`--outfile=${built}`,
|
|
113
133
|
`--metafile=${metafilePath}`
|
|
114
|
-
], root
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
134
|
+
], root);
|
|
135
|
+
if (!build.ok) {
|
|
136
|
+
throw new Error(`Client artifact build failed (exit ${build.exit}): ${build.output}`);
|
|
137
|
+
}
|
|
138
|
+
const graph = await readClientBuildGraph(metafilePath, root);
|
|
139
|
+
refuseForbiddenModules(graph, options.forbiddenModules ?? []);
|
|
140
|
+
const bytes = await readFile2(built);
|
|
118
141
|
const digest = createHash("sha256").update(bytes).digest("hex");
|
|
119
142
|
await mkdir(dirname(file), { recursive: true });
|
|
120
143
|
await writeFile(file, bytes);
|
|
121
144
|
await writeFile(sha256File, `${digest} ${basename(file)}
|
|
122
145
|
`);
|
|
123
|
-
|
|
146
|
+
return {
|
|
124
147
|
file,
|
|
125
148
|
sha256: digest,
|
|
126
149
|
bytes: bytes.byteLength,
|
|
127
150
|
sha256File,
|
|
128
|
-
modules:
|
|
151
|
+
modules: [...graph.modules.keys()].sort()
|
|
129
152
|
};
|
|
130
|
-
if (options.package === undefined)
|
|
131
|
-
return artifact;
|
|
132
|
-
try {
|
|
133
|
-
return { ...artifact, package: await packAndVerify(step, work, bytes, options.package) };
|
|
134
|
-
} catch (error) {
|
|
135
|
-
await rm(file, { force: true });
|
|
136
|
-
await rm(sha256File, { force: true });
|
|
137
|
-
throw error;
|
|
138
|
-
}
|
|
139
153
|
} finally {
|
|
140
154
|
await rm(work, { recursive: true, force: true });
|
|
141
155
|
}
|
|
142
156
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
157
|
+
// src/files/client-package.ts
|
|
158
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
159
|
+
import { existsSync as existsSync3 } from "node:fs";
|
|
160
|
+
import { copyFile as copyFile2, mkdir as mkdir3, mkdtemp as mkdtemp2, readFile as readFile4, realpath as realpath2, rm as rm3, writeFile as writeFile4 } from "node:fs/promises";
|
|
161
|
+
import { tmpdir as tmpdir2 } from "node:os";
|
|
162
|
+
import { dirname as dirname5, isAbsolute, join as join5, normalize, relative as relative3, resolve as resolve4 } from "node:path";
|
|
163
|
+
|
|
164
|
+
// src/files/client-declarations.ts
|
|
165
|
+
import { randomUUID } from "node:crypto";
|
|
166
|
+
import { rm as rm2, writeFile as writeFile2 } from "node:fs/promises";
|
|
167
|
+
import { builtinModules } from "node:module";
|
|
168
|
+
import { dirname as dirname3, join as join3, relative } from "node:path";
|
|
169
|
+
|
|
170
|
+
// src/files/client-package-contract.ts
|
|
171
|
+
import { z as z2 } from "zod";
|
|
172
|
+
var ClientPackageManifestSchema = z2.looseObject({
|
|
173
|
+
description: z2.string().optional(),
|
|
174
|
+
license: z2.string().optional(),
|
|
175
|
+
repository: z2.union([
|
|
176
|
+
z2.string(),
|
|
177
|
+
z2.object({ type: z2.string(), url: z2.string(), directory: z2.string().optional() })
|
|
178
|
+
]).optional(),
|
|
179
|
+
homepage: z2.string().optional(),
|
|
180
|
+
keywords: z2.array(z2.string()).readonly().optional(),
|
|
181
|
+
author: z2.union([
|
|
182
|
+
z2.string(),
|
|
183
|
+
z2.object({ name: z2.string(), email: z2.string().optional(), url: z2.string().optional() })
|
|
184
|
+
]).optional()
|
|
185
|
+
});
|
|
186
|
+
var CLIENT_PACKAGE_OWNED_FIELDS = [
|
|
187
|
+
"name",
|
|
188
|
+
"version",
|
|
189
|
+
"type",
|
|
190
|
+
"main",
|
|
191
|
+
"module",
|
|
192
|
+
"types",
|
|
193
|
+
"typings",
|
|
194
|
+
"exports",
|
|
195
|
+
"files",
|
|
196
|
+
"dependencies",
|
|
197
|
+
"peerDependencies",
|
|
198
|
+
"bundleDependencies",
|
|
199
|
+
"bundledDependencies"
|
|
200
|
+
];
|
|
201
|
+
var ClientPackageRefusalSchema = z2.enum([
|
|
202
|
+
"build",
|
|
203
|
+
"declarations",
|
|
204
|
+
"declaration-unresolved",
|
|
205
|
+
"declaration-dependency",
|
|
206
|
+
"bundled-peer",
|
|
207
|
+
"manifest-owned-field",
|
|
208
|
+
"install",
|
|
209
|
+
"peer-range",
|
|
210
|
+
"missing-export",
|
|
211
|
+
"shared-export-split",
|
|
212
|
+
"typecheck"
|
|
213
|
+
]);
|
|
214
|
+
|
|
215
|
+
class ClientPackageError extends Error {
|
|
216
|
+
reason;
|
|
217
|
+
details;
|
|
218
|
+
constructor(reason, message, details = {}, options) {
|
|
219
|
+
super(message, options);
|
|
220
|
+
this.reason = reason;
|
|
221
|
+
this.details = details;
|
|
222
|
+
this.name = "ClientPackageError";
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// src/files/declaration-specifiers.ts
|
|
227
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
228
|
+
import { dirname as dirname2, join as join2, resolve as resolve3 } from "node:path";
|
|
229
|
+
var REFERENCE = /^\/\/\/\s*<reference\s+(path|types)\s*=\s*(["'])([^"']*)\2/;
|
|
230
|
+
function tokenize(source, references) {
|
|
231
|
+
const tokens = [];
|
|
232
|
+
const substitutions = [];
|
|
233
|
+
let braces = 0;
|
|
234
|
+
let index = 0;
|
|
235
|
+
const readTemplate = () => {
|
|
236
|
+
while (index < source.length) {
|
|
237
|
+
const char = source[index];
|
|
238
|
+
if (char === "\\") {
|
|
239
|
+
index += 2;
|
|
240
|
+
} else if (char === "`") {
|
|
241
|
+
index += 1;
|
|
242
|
+
return;
|
|
243
|
+
} else if (char === "$" && source[index + 1] === "{") {
|
|
244
|
+
index += 2;
|
|
245
|
+
substitutions.push(braces);
|
|
246
|
+
braces += 1;
|
|
247
|
+
return;
|
|
248
|
+
} else {
|
|
249
|
+
index += 1;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
const readLineComment = () => {
|
|
254
|
+
const lineEnd = source.indexOf(`
|
|
255
|
+
`, index);
|
|
256
|
+
const end = lineEnd === -1 ? source.length : lineEnd;
|
|
257
|
+
const match = REFERENCE.exec(source.slice(index, end));
|
|
258
|
+
if (match) {
|
|
259
|
+
const text = match[3] ?? "";
|
|
260
|
+
const start = index + match[0].length - 1 - text.length;
|
|
261
|
+
references.push({
|
|
262
|
+
kind: match[1] === "path" ? "reference-path" : "reference-types",
|
|
263
|
+
text,
|
|
264
|
+
start,
|
|
265
|
+
end: start + text.length
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
index = end;
|
|
269
|
+
};
|
|
270
|
+
const readString = (quote) => {
|
|
271
|
+
const start = index;
|
|
272
|
+
index += 1;
|
|
273
|
+
let text = "";
|
|
274
|
+
while (index < source.length && source[index] !== quote) {
|
|
275
|
+
if (source[index] === "\\") {
|
|
276
|
+
text += source[index + 1] ?? "";
|
|
277
|
+
index += 2;
|
|
278
|
+
} else {
|
|
279
|
+
text += source[index];
|
|
280
|
+
index += 1;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
index += 1;
|
|
284
|
+
tokens.push({ kind: "string", text, start, end: index });
|
|
285
|
+
};
|
|
286
|
+
while (index < source.length) {
|
|
287
|
+
const char = source[index] ?? "";
|
|
288
|
+
if (char === "/" && source[index + 1] === "/") {
|
|
289
|
+
readLineComment();
|
|
290
|
+
} else if (char === "/" && source[index + 1] === "*") {
|
|
291
|
+
const close = source.indexOf("*/", index + 2);
|
|
292
|
+
index = close === -1 ? source.length : close + 2;
|
|
293
|
+
} else if (char === '"' || char === "'") {
|
|
294
|
+
readString(char);
|
|
295
|
+
} else if (char === "`") {
|
|
296
|
+
index += 1;
|
|
297
|
+
readTemplate();
|
|
298
|
+
tokens.push({ kind: "punctuation", text: "`", start: index, end: index });
|
|
299
|
+
} else if (char === "{") {
|
|
300
|
+
braces += 1;
|
|
301
|
+
index += 1;
|
|
302
|
+
tokens.push({ kind: "punctuation", text: char, start: index - 1, end: index });
|
|
303
|
+
} else if (char === "}") {
|
|
304
|
+
braces -= 1;
|
|
305
|
+
index += 1;
|
|
306
|
+
if (substitutions.at(-1) === braces) {
|
|
307
|
+
substitutions.pop();
|
|
308
|
+
readTemplate();
|
|
309
|
+
}
|
|
310
|
+
tokens.push({ kind: "punctuation", text: char, start: index - 1, end: index });
|
|
311
|
+
} else if (/[A-Za-z_$]/.test(char)) {
|
|
312
|
+
const start = index;
|
|
313
|
+
while (index < source.length && /[\w$]/.test(source[index] ?? ""))
|
|
314
|
+
index += 1;
|
|
315
|
+
tokens.push({ kind: "word", text: source.slice(start, index), start, end: index });
|
|
316
|
+
} else if (/\s/.test(char)) {
|
|
317
|
+
index += 1;
|
|
318
|
+
} else {
|
|
319
|
+
tokens.push({ kind: "punctuation", text: char, start: index, end: index + 1 });
|
|
320
|
+
index += 1;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return tokens;
|
|
324
|
+
}
|
|
325
|
+
function isModuleSpecifier(tokens, position) {
|
|
326
|
+
const before = tokens[position - 1];
|
|
327
|
+
const earlier = tokens[position - 2];
|
|
328
|
+
if (before?.kind === "word") {
|
|
329
|
+
if (before.text === "from" || before.text === "import")
|
|
330
|
+
return true;
|
|
331
|
+
return before.text === "module" && earlier?.kind === "word" && earlier.text === "declare";
|
|
332
|
+
}
|
|
333
|
+
return before?.text === "(" && earlier?.kind === "word" && (earlier.text === "import" || earlier.text === "require");
|
|
334
|
+
}
|
|
335
|
+
function declarationSpecifiers(source) {
|
|
336
|
+
const found = [];
|
|
337
|
+
const tokens = tokenize(source, found);
|
|
338
|
+
tokens.forEach((token, position) => {
|
|
339
|
+
if (token.kind !== "string" || !isModuleSpecifier(tokens, position))
|
|
340
|
+
return;
|
|
341
|
+
found.push({
|
|
342
|
+
kind: "module",
|
|
343
|
+
text: token.text,
|
|
344
|
+
start: token.start + 1,
|
|
345
|
+
end: token.end - 1
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
return found.sort((left, right) => left.start - right.start);
|
|
349
|
+
}
|
|
350
|
+
function isRelativeSpecifier(specifier) {
|
|
351
|
+
return /^\.\.?(?:\/|$)/.test(specifier);
|
|
352
|
+
}
|
|
353
|
+
var EXTENSIONS = [
|
|
354
|
+
[".ts", ".js", ".d.ts"],
|
|
355
|
+
[".tsx", ".js", ".d.ts"],
|
|
356
|
+
[".mts", ".mjs", ".d.mts"],
|
|
357
|
+
[".cts", ".cjs", ".d.cts"],
|
|
358
|
+
[".js", ".js", ".d.ts"],
|
|
359
|
+
[".jsx", ".js", ".d.ts"],
|
|
360
|
+
[".mjs", ".mjs", ".d.mts"],
|
|
361
|
+
[".cjs", ".cjs", ".d.cts"]
|
|
362
|
+
];
|
|
363
|
+
function declarationOutputSpecifier(file, specifier) {
|
|
364
|
+
const target = resolve3(dirname2(file), specifier);
|
|
365
|
+
for (const [extension, output, declaration] of EXTENSIONS) {
|
|
366
|
+
if (!specifier.endsWith(extension))
|
|
367
|
+
continue;
|
|
368
|
+
const stem = target.slice(0, -extension.length);
|
|
369
|
+
if (existsSync(`${stem}${declaration}`)) {
|
|
370
|
+
return `${specifier.slice(0, -extension.length)}${output}`;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (existsSync(`${target}.d.ts`))
|
|
374
|
+
return `${specifier}.js`;
|
|
375
|
+
if (existsSync(join2(target, "index.d.ts")))
|
|
376
|
+
return `${specifier.replace(/\/$/, "")}/index.js`;
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
function declarationFiles(directory) {
|
|
380
|
+
const files = [];
|
|
381
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
382
|
+
const path = join2(directory, entry.name);
|
|
383
|
+
if (entry.isDirectory())
|
|
384
|
+
files.push(...declarationFiles(path));
|
|
385
|
+
else if (/\.d\.[cm]?ts$/.test(entry.name))
|
|
386
|
+
files.push(path);
|
|
387
|
+
}
|
|
388
|
+
return files.sort();
|
|
389
|
+
}
|
|
390
|
+
function rewriteDeclarationTree(directory, rewrite) {
|
|
391
|
+
let files = 0;
|
|
392
|
+
let specifiers = 0;
|
|
393
|
+
for (const file of declarationFiles(directory)) {
|
|
394
|
+
const source = readFileSync(file, "utf8");
|
|
395
|
+
let output = source;
|
|
396
|
+
let changed = 0;
|
|
397
|
+
for (const specifier of declarationSpecifiers(source).reverse()) {
|
|
398
|
+
const text = rewrite(specifier, file);
|
|
399
|
+
if (text === undefined || text === specifier.text)
|
|
400
|
+
continue;
|
|
401
|
+
output = `${output.slice(0, specifier.start)}${text}${output.slice(specifier.end)}`;
|
|
402
|
+
changed += 1;
|
|
403
|
+
}
|
|
404
|
+
if (changed === 0)
|
|
405
|
+
continue;
|
|
406
|
+
writeFileSync(file, output);
|
|
407
|
+
files += 1;
|
|
408
|
+
specifiers += changed;
|
|
409
|
+
}
|
|
410
|
+
return { files, specifiers };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// src/files/client-declarations.ts
|
|
414
|
+
async function emitClientDeclarations(input) {
|
|
415
|
+
const root = dirname3(input.tsconfig);
|
|
416
|
+
const generated = join3(root, `.client-package-${randomUUID()}.tsconfig.json`);
|
|
417
|
+
await writeFile2(generated, `${JSON.stringify({
|
|
418
|
+
extends: input.tsconfig,
|
|
419
|
+
compilerOptions: {
|
|
420
|
+
noEmit: false,
|
|
421
|
+
declaration: true,
|
|
422
|
+
emitDeclarationOnly: true,
|
|
423
|
+
declarationMap: false,
|
|
424
|
+
composite: false,
|
|
425
|
+
incremental: false,
|
|
426
|
+
rootDir: root,
|
|
427
|
+
outDir: input.outDir,
|
|
428
|
+
declarationDir: input.outDir
|
|
429
|
+
},
|
|
430
|
+
files: input.entries,
|
|
431
|
+
include: []
|
|
154
432
|
}, null, 2)}
|
|
155
433
|
`);
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
434
|
+
try {
|
|
435
|
+
const [compiler, ...leading] = input.tsc;
|
|
436
|
+
const emitted = await runClientTool(input.step, compiler, [...leading, "-p", generated], root);
|
|
437
|
+
if (!emitted.ok) {
|
|
438
|
+
throw new ClientPackageError("declarations", `Client package declarations failed (exit ${emitted.exit}): ${emitted.output}`, { output: emitted.output });
|
|
439
|
+
}
|
|
440
|
+
} finally {
|
|
441
|
+
await rm2(generated, { force: true });
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
function packageOf(specifier) {
|
|
445
|
+
const [first = "", second] = specifier.split("/");
|
|
446
|
+
return first.startsWith("@") && second !== undefined ? `${first}/${second}` : first;
|
|
447
|
+
}
|
|
448
|
+
function isPlatformModule(specifier, closure) {
|
|
449
|
+
if (specifier.startsWith("node:") || builtinModules.includes(specifier))
|
|
450
|
+
return true;
|
|
451
|
+
return closure.runtime === "bun" && (specifier === "bun" || specifier.startsWith("bun:"));
|
|
452
|
+
}
|
|
453
|
+
function normalizeClientDeclarations(directory, closure) {
|
|
454
|
+
rewriteDeclarationTree(directory, (specifier, file) => {
|
|
455
|
+
const shown = relative(directory, file);
|
|
456
|
+
if (specifier.kind === "reference-types") {
|
|
457
|
+
const declared = closure.packages.has(specifier.text) || closure.packages.has(`@types/${specifier.text}`) || specifier.text === "node" || isPlatformModule(specifier.text, closure);
|
|
458
|
+
if (!declared)
|
|
459
|
+
throw undeclared(shown, specifier.text, specifier.text);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
if (specifier.kind === "reference-path" || isRelativeSpecifier(specifier.text)) {
|
|
463
|
+
const output = specifier.kind === "module" ? declarationOutputSpecifier(file, specifier.text) : undefined;
|
|
464
|
+
if (output === undefined) {
|
|
465
|
+
throw new ClientPackageError("declaration-unresolved", `Client package declaration ${shown} names ${JSON.stringify(specifier.text)}, ` + "which has no declaration in the package", { file: shown, specifier: specifier.text });
|
|
466
|
+
}
|
|
467
|
+
return output;
|
|
468
|
+
}
|
|
469
|
+
if (isPlatformModule(specifier.text, closure))
|
|
470
|
+
return;
|
|
471
|
+
const name = packageOf(specifier.text);
|
|
472
|
+
if (!closure.packages.has(name))
|
|
473
|
+
throw undeclared(shown, specifier.text, name);
|
|
474
|
+
return;
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
function undeclared(file, specifier, name) {
|
|
478
|
+
return new ClientPackageError("declaration-dependency", `Client package declaration ${file} names ${JSON.stringify(specifier)}, but "${name}" ` + "is neither a peer dependency nor a dependency of the package", { file, specifier, package: name });
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// src/files/client-package-proof.ts
|
|
482
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
483
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
484
|
+
import { copyFile, mkdir as mkdir2, readdir, readFile as readFile3, realpath, writeFile as writeFile3 } from "node:fs/promises";
|
|
485
|
+
import { basename as basename2, dirname as dirname4, join as join4, relative as relative2 } from "node:path";
|
|
486
|
+
import { z as z3 } from "zod";
|
|
487
|
+
var InstalledManifestSchema = z3.object({ version: z3.string() });
|
|
488
|
+
async function resolveExternalPackage(from, name) {
|
|
489
|
+
for (let directory = from;; directory = dirname4(directory)) {
|
|
490
|
+
const candidate = join4(directory, "node_modules", name);
|
|
491
|
+
if (existsSync2(join4(candidate, "package.json"))) {
|
|
492
|
+
const real = await realpath(candidate);
|
|
493
|
+
const manifest = InstalledManifestSchema.parse(safeJsonParse(await readFile3(join4(real, "package.json"), "utf8")));
|
|
494
|
+
return { directory: real, version: manifest.version };
|
|
495
|
+
}
|
|
496
|
+
if (dirname4(directory) === directory) {
|
|
497
|
+
throw new ClientPackageError("install", `Client package needs "${name}" installed where it is built, and ${from} does not resolve it`, { package: name });
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
async function filesUnder(directory, base = directory) {
|
|
502
|
+
const files = [];
|
|
503
|
+
for (const entry of await readdir(directory, { withFileTypes: true })) {
|
|
504
|
+
const path = join4(directory, entry.name);
|
|
505
|
+
if (entry.isDirectory()) {
|
|
506
|
+
if (entry.name !== "node_modules")
|
|
507
|
+
files.push(...await filesUnder(path, base));
|
|
508
|
+
} else {
|
|
509
|
+
files.push(relative2(base, path).replaceAll("\\", "/"));
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return files.sort();
|
|
513
|
+
}
|
|
514
|
+
var IdentityReportSchema = z3.object({
|
|
515
|
+
exports: z3.record(z3.string(), z3.array(z3.string())),
|
|
516
|
+
shared: z3.array(z3.string()),
|
|
517
|
+
split: z3.array(z3.string())
|
|
518
|
+
});
|
|
519
|
+
function identityScript(name, subpaths) {
|
|
520
|
+
return `const name = ${JSON.stringify(name)};
|
|
521
|
+
const subpaths = ${JSON.stringify(subpaths)};
|
|
522
|
+
const namespaces = {};
|
|
523
|
+
for (const subpath of subpaths) {
|
|
524
|
+
namespaces[subpath] = await import(subpath === '.' ? name : name + subpath.slice(1));
|
|
525
|
+
}
|
|
526
|
+
const exported = {};
|
|
527
|
+
const owners = new Map();
|
|
528
|
+
for (const subpath of subpaths) {
|
|
529
|
+
exported[subpath] = Object.keys(namespaces[subpath]).sort();
|
|
530
|
+
for (const key of exported[subpath]) owners.set(key, [...(owners.get(key) ?? []), subpath]);
|
|
531
|
+
}
|
|
532
|
+
const shared = [];
|
|
533
|
+
const split = [];
|
|
534
|
+
for (const [key, list] of owners) {
|
|
535
|
+
if (list.length < 2) continue;
|
|
536
|
+
const first = namespaces[list[0]][key];
|
|
537
|
+
(list.every((subpath) => Object.is(namespaces[subpath][key], first)) ? shared : split).push(key);
|
|
538
|
+
}
|
|
539
|
+
process.stdout.write(JSON.stringify({ exports: exported, shared: shared.sort(), split: split.sort() }));
|
|
540
|
+
`;
|
|
541
|
+
}
|
|
542
|
+
var RangeReportSchema = z3.array(z3.object({ package: z3.string(), range: z3.string(), installed: z3.string() }));
|
|
543
|
+
var RANGE_SCRIPT = `const declared = JSON.parse(await Bun.file('ranges.json').text());
|
|
544
|
+
const outside = declared.filter((entry) => !Bun.semver.satisfies(entry.installed, entry.range));
|
|
545
|
+
process.stdout.write(JSON.stringify(outside));
|
|
546
|
+
`;
|
|
547
|
+
function ranOrRefuse(result, reason, what, details = {}) {
|
|
548
|
+
if (result.ok)
|
|
549
|
+
return result;
|
|
550
|
+
throw new ClientPackageError(reason, `Client package ${what} failed (exit ${result.exit}): ${result.output}`, { ...details, output: result.output });
|
|
551
|
+
}
|
|
552
|
+
async function install(input) {
|
|
553
|
+
const { options, consumer } = input;
|
|
554
|
+
const peers = Object.keys(options.peerDependencies ?? {});
|
|
555
|
+
const fromBuild = (names) => Object.fromEntries(names.map((name) => [name, `file:${input.externals.get(name)?.directory ?? ""}`]));
|
|
556
|
+
const types = {};
|
|
557
|
+
for (const name of options.proof?.types ?? []) {
|
|
558
|
+
types[name] = `file:${(await resolveExternalPackage(input.root, name)).directory}`;
|
|
559
|
+
}
|
|
560
|
+
await writeFile3(join4(consumer, "package.json"), `${JSON.stringify({
|
|
561
|
+
name: "client-package-consumer",
|
|
562
|
+
private: true,
|
|
563
|
+
type: "module",
|
|
564
|
+
dependencies: {
|
|
565
|
+
...types,
|
|
566
|
+
...fromBuild(peers),
|
|
567
|
+
[options.name]: `file:${input.tarball}`
|
|
568
|
+
},
|
|
569
|
+
overrides: fromBuild(Object.keys(options.dependencies ?? {}))
|
|
570
|
+
}, null, 2)}
|
|
168
571
|
`);
|
|
169
|
-
await
|
|
572
|
+
ranOrRefuse(await runClientTool(input.step, input.tools.bun, ["install", "--ignore-scripts"], consumer), "install", "install");
|
|
573
|
+
const installed = join4(consumer, "node_modules", options.name);
|
|
574
|
+
for (const peer of peers) {
|
|
575
|
+
if (!existsSync2(join4(installed, "node_modules", peer)))
|
|
576
|
+
continue;
|
|
577
|
+
throw new ClientPackageError("bundled-peer", `Client package installs its own copy of peer "${peer}" instead of sharing the consumer's`, { package: peer });
|
|
578
|
+
}
|
|
579
|
+
const files = [];
|
|
580
|
+
for (const path of await filesUnder(input.staged)) {
|
|
581
|
+
const packed = await readFile3(join4(input.staged, path));
|
|
582
|
+
const digest = createHash2("sha256").update(packed).digest("hex");
|
|
583
|
+
const unpacked = existsSync2(join4(installed, path)) ? createHash2("sha256").update(await readFile3(join4(installed, path))).digest("hex") : undefined;
|
|
584
|
+
if (unpacked !== digest) {
|
|
585
|
+
throw new ClientPackageError("install", `Client package file ${path} is not in the installed archive as it was built`, { file: path });
|
|
586
|
+
}
|
|
587
|
+
files.push({ path, sha256: digest, bytes: packed.byteLength });
|
|
588
|
+
}
|
|
589
|
+
return files;
|
|
590
|
+
}
|
|
591
|
+
async function proveRanges(input) {
|
|
592
|
+
const { options, consumer } = input;
|
|
593
|
+
const declared = { ...options.dependencies, ...options.peerDependencies };
|
|
594
|
+
const installedVersions = {};
|
|
595
|
+
for (const name of Object.keys(declared)) {
|
|
596
|
+
const manifest = [join4(options.name, "node_modules", name), name].map((path) => join4(consumer, "node_modules", path, "package.json")).find((path) => existsSync2(path));
|
|
597
|
+
if (manifest === undefined) {
|
|
598
|
+
throw new ClientPackageError("install", `Client package was installed without "${name}"`, { package: name });
|
|
599
|
+
}
|
|
600
|
+
installedVersions[name] = InstalledManifestSchema.parse(safeJsonParse(await readFile3(manifest, "utf8"))).version;
|
|
601
|
+
}
|
|
602
|
+
await writeFile3(join4(consumer, "ranges.json"), JSON.stringify(Object.entries(declared).map(([name, range]) => ({
|
|
603
|
+
package: name,
|
|
604
|
+
range,
|
|
605
|
+
installed: installedVersions[name] ?? ""
|
|
606
|
+
}))));
|
|
607
|
+
await writeFile3(join4(consumer, "ranges.mjs"), RANGE_SCRIPT);
|
|
608
|
+
const ran = ranOrRefuse(await runClientTool(input.step, input.tools.bun, ["ranges.mjs"], consumer), "install", "range check");
|
|
609
|
+
const [outside] = RangeReportSchema.parse(safeJsonParse(ran.stdout));
|
|
610
|
+
if (outside !== undefined) {
|
|
611
|
+
throw new ClientPackageError("peer-range", `Client package declares "${outside.package}" as ${outside.range}, ` + `but it is built and proven against ${outside.installed}`, outside);
|
|
612
|
+
}
|
|
613
|
+
return installedVersions;
|
|
614
|
+
}
|
|
615
|
+
async function proveIdentity(input) {
|
|
616
|
+
const { options, consumer } = input;
|
|
617
|
+
const subpaths = Object.keys(options.entries);
|
|
618
|
+
await writeFile3(join4(consumer, "identity.mjs"), identityScript(options.name, subpaths));
|
|
619
|
+
const runtimes = (options.runtime ?? "node") === "node" ? ["node", "bun"] : ["bun"];
|
|
620
|
+
const distinct = new Set(options.proof?.distinctExports ?? []);
|
|
621
|
+
let first;
|
|
622
|
+
for (const runtime of runtimes) {
|
|
623
|
+
const ran = ranOrRefuse(await runClientTool(input.step, input.tools[runtime], ["identity.mjs"], consumer), "install", `import in ${runtime}`, { runtime });
|
|
624
|
+
const report = IdentityReportSchema.parse(safeJsonParse(ran.stdout));
|
|
625
|
+
for (const [subpath, required] of Object.entries(options.proof?.exports ?? {})) {
|
|
626
|
+
const names = required.filter((name) => !report.exports[subpath]?.includes(name));
|
|
627
|
+
if (names.length === 0)
|
|
628
|
+
continue;
|
|
629
|
+
throw new ClientPackageError("missing-export", `Client package subpath "${subpath}" does not export ${names.join(", ")} in ${runtime}`, { subpath, names, runtime });
|
|
630
|
+
}
|
|
631
|
+
const names = report.split.filter((name) => !distinct.has(name));
|
|
632
|
+
if (names.length > 0) {
|
|
633
|
+
throw new ClientPackageError("shared-export-split", `Client package exports ${names.join(", ")} from several subpaths as different values in ${runtime}`, { names, runtime });
|
|
634
|
+
}
|
|
635
|
+
first ??= report;
|
|
636
|
+
}
|
|
637
|
+
return { exports: first?.exports ?? {}, sharedExports: first?.shared ?? [], runtimes };
|
|
638
|
+
}
|
|
639
|
+
var RESOLUTIONS = [
|
|
640
|
+
{ resolution: "bundler", module: "Preserve" },
|
|
641
|
+
{ resolution: "NodeNext", module: "NodeNext" }
|
|
642
|
+
];
|
|
643
|
+
async function proveTypes(input) {
|
|
644
|
+
const { options, consumer } = input;
|
|
645
|
+
const sources = ["exports.ts"];
|
|
646
|
+
await writeFile3(join4(consumer, "exports.ts"), Object.keys(options.entries).map((subpath, index) => {
|
|
647
|
+
const specifier = subpath === "." ? options.name : options.name + subpath.slice(1);
|
|
648
|
+
return `export * as E${index} from ${JSON.stringify(specifier)};
|
|
649
|
+
`;
|
|
650
|
+
}).join(""));
|
|
651
|
+
await mkdir2(join4(consumer, "typecheck"));
|
|
652
|
+
for (const [index, source] of (options.proof?.typecheck ?? []).entries()) {
|
|
653
|
+
const copy = `typecheck/${index}-${basename2(source)}`;
|
|
654
|
+
await copyFile(source, join4(consumer, copy));
|
|
655
|
+
sources.push(copy);
|
|
656
|
+
}
|
|
657
|
+
const [compiler, ...leading] = input.tools.tsc;
|
|
658
|
+
const version = ranOrRefuse(await runClientTool(input.step, compiler, [...leading, "--version"], consumer), "typecheck", "compiler").stdout.replace(/^Version\s+/, "");
|
|
659
|
+
for (const { resolution, module } of RESOLUTIONS) {
|
|
660
|
+
const config = `tsconfig.${resolution.toLowerCase()}.json`;
|
|
661
|
+
await writeFile3(join4(consumer, config), JSON.stringify({
|
|
662
|
+
compilerOptions: {
|
|
663
|
+
strict: true,
|
|
664
|
+
noEmit: true,
|
|
665
|
+
skipLibCheck: false,
|
|
666
|
+
target: "ES2022",
|
|
667
|
+
module,
|
|
668
|
+
moduleResolution: resolution,
|
|
669
|
+
lib: ["ES2023", "DOM"],
|
|
670
|
+
types: (options.proof?.types ?? []).map((name) => name.replace(/^@types\//, ""))
|
|
671
|
+
},
|
|
672
|
+
files: sources
|
|
673
|
+
}));
|
|
674
|
+
ranOrRefuse(await runClientTool(input.step, compiler, [...leading, "-p", config], consumer), "typecheck", `consumer typecheck under ${resolution}`, { resolution });
|
|
675
|
+
}
|
|
676
|
+
return version.trim();
|
|
677
|
+
}
|
|
678
|
+
async function proveClientPackage(input) {
|
|
679
|
+
const files = await install(input);
|
|
680
|
+
const externals = await proveRanges(input);
|
|
681
|
+
const identity = await proveIdentity(input);
|
|
682
|
+
const typescript = await proveTypes(input);
|
|
170
683
|
return {
|
|
171
|
-
|
|
172
|
-
|
|
684
|
+
files,
|
|
685
|
+
exports: identity.exports,
|
|
686
|
+
sharedExports: identity.sharedExports,
|
|
687
|
+
proof: {
|
|
688
|
+
runtimes: identity.runtimes,
|
|
689
|
+
resolutions: RESOLUTIONS.map(({ resolution }) => resolution),
|
|
690
|
+
typescript,
|
|
691
|
+
externals
|
|
692
|
+
}
|
|
173
693
|
};
|
|
174
694
|
}
|
|
695
|
+
|
|
696
|
+
// src/files/client-package.ts
|
|
697
|
+
var PACKAGE_NAME = /^(?:@[a-z0-9~-][a-z0-9._~-]*\/)?[a-z0-9~-][a-z0-9._~-]*$/;
|
|
698
|
+
function plan(options) {
|
|
699
|
+
if (!PACKAGE_NAME.test(options.name)) {
|
|
700
|
+
throw new TypeError(`Client package name ${JSON.stringify(options.name)} is not a package name`);
|
|
701
|
+
}
|
|
702
|
+
const tsconfig = resolve4(options.tsconfig);
|
|
703
|
+
const root = dirname5(tsconfig);
|
|
704
|
+
const entries = new Map;
|
|
705
|
+
for (const [subpath, source] of Object.entries(options.entries)) {
|
|
706
|
+
const entry = resolve4(source);
|
|
707
|
+
const inside = relative3(root, entry);
|
|
708
|
+
if (subpath !== "." && !/^\.\/[^/]/.test(subpath)) {
|
|
709
|
+
throw new TypeError(`Client package entry ${JSON.stringify(subpath)} is not "." or "./name"`);
|
|
710
|
+
}
|
|
711
|
+
if (inside.startsWith("..") || isAbsolute(inside) || !/\.tsx?$/.test(entry)) {
|
|
712
|
+
throw new TypeError(`Client package entry ${source} must be a .ts or .tsx module under ${root}, the tsconfig's directory`);
|
|
713
|
+
}
|
|
714
|
+
if ([...entries.values()].includes(entry)) {
|
|
715
|
+
throw new TypeError(`Client package entry ${source} is listed under two subpaths`);
|
|
716
|
+
}
|
|
717
|
+
entries.set(subpath, entry);
|
|
718
|
+
}
|
|
719
|
+
if (entries.size === 0)
|
|
720
|
+
throw new TypeError("Client package has no entries");
|
|
721
|
+
const peers = Object.keys(options.peerDependencies ?? {});
|
|
722
|
+
const dependencies = Object.keys(options.dependencies ?? {});
|
|
723
|
+
const both = peers.find((name) => dependencies.includes(name));
|
|
724
|
+
if (both !== undefined) {
|
|
725
|
+
throw new TypeError(`Client package lists "${both}" as both a peer and a dependency`);
|
|
726
|
+
}
|
|
727
|
+
const manifest = ClientPackageManifestSchema.parse(options.manifest ?? {});
|
|
728
|
+
const owned = Object.keys(manifest).find((field) => CLIENT_PACKAGE_OWNED_FIELDS.includes(field));
|
|
729
|
+
if (owned !== undefined) {
|
|
730
|
+
throw new ClientPackageError("manifest-owned-field", `Client package manifest sets "${owned}", which the build writes itself`, { field: owned });
|
|
731
|
+
}
|
|
732
|
+
const files = new Map;
|
|
733
|
+
for (const [target, source] of Object.entries(options.files ?? {})) {
|
|
734
|
+
const path = normalize(target).replaceAll("\\", "/");
|
|
735
|
+
if (isAbsolute(path) || path.startsWith("..") || path === "package.json" || /^dist(\/|$)/.test(path)) {
|
|
736
|
+
throw new TypeError(`Client package file ${JSON.stringify(target)} must be a path inside the package, outside dist/`);
|
|
737
|
+
}
|
|
738
|
+
files.set(path, resolve4(source));
|
|
739
|
+
}
|
|
740
|
+
return {
|
|
741
|
+
root,
|
|
742
|
+
tsconfig,
|
|
743
|
+
entries,
|
|
744
|
+
external: [...peers, ...dependencies],
|
|
745
|
+
manifest,
|
|
746
|
+
files,
|
|
747
|
+
tarball: join5(resolve4(options.outDir), `${options.name.replace(/^@/, "").replaceAll("/", "-")}-${options.version}.tgz`)
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
async function bundle(options, planned, step, bun, work) {
|
|
751
|
+
const metafile = join5(work, "meta.json");
|
|
752
|
+
const built = await runClientTool(step, bun, [
|
|
753
|
+
"build",
|
|
754
|
+
...planned.entries.values(),
|
|
755
|
+
`--root=${planned.root}`,
|
|
756
|
+
`--outdir=${join5(work, "package", "dist")}`,
|
|
757
|
+
`--target=${options.runtime ?? "node"}`,
|
|
758
|
+
"--format=esm",
|
|
759
|
+
"--splitting",
|
|
760
|
+
...planned.external.map((name) => `--external=${name}`),
|
|
761
|
+
"--entry-naming=[dir]/[name].[ext]",
|
|
762
|
+
"--chunk-naming=chunks/[name]-[hash].[ext]",
|
|
763
|
+
`--tsconfig-override=${planned.tsconfig}`,
|
|
764
|
+
`--metafile=${metafile}`
|
|
765
|
+
], planned.root);
|
|
766
|
+
if (!built.ok) {
|
|
767
|
+
throw new ClientPackageError("build", `Client package build failed (exit ${built.exit}): ${built.output}`, { output: built.output });
|
|
768
|
+
}
|
|
769
|
+
return readClientBuildGraph(metafile, planned.root);
|
|
770
|
+
}
|
|
771
|
+
async function refuseBundledExternals(graph, externals) {
|
|
772
|
+
for (const module of graph.modules.keys()) {
|
|
773
|
+
const real = await realpath2(module);
|
|
774
|
+
for (const [name, external] of externals) {
|
|
775
|
+
if (!real.startsWith(`${external.directory}/`))
|
|
776
|
+
continue;
|
|
777
|
+
throw new ClientPackageError("bundled-peer", `Client package bundles ${module}, which is code of "${name}"; import it by the package name`, { package: name, module });
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
async function stage(options, planned, graph, staged) {
|
|
782
|
+
const exported = {};
|
|
783
|
+
for (const [subpath, entry] of planned.entries) {
|
|
784
|
+
const output = graph.entryOutputs.get(entry);
|
|
785
|
+
const types = output?.replace(/\.js$/, ".d.ts");
|
|
786
|
+
if (output === undefined || types === undefined || !existsSync3(join5(staged, "dist", types))) {
|
|
787
|
+
throw new ClientPackageError("declarations", `Client package entry ${entry} has no built module with a declaration beside it`, { entry, subpath });
|
|
788
|
+
}
|
|
789
|
+
exported[subpath] = { types: `./dist/${types}`, import: `./dist/${output}` };
|
|
790
|
+
}
|
|
791
|
+
for (const [target, source] of planned.files) {
|
|
792
|
+
await mkdir3(dirname5(join5(staged, target)), { recursive: true });
|
|
793
|
+
await copyFile2(source, join5(staged, target));
|
|
794
|
+
}
|
|
795
|
+
const present = (record) => record !== undefined && Object.keys(record).length > 0 ? record : undefined;
|
|
796
|
+
await writeFile4(join5(staged, "package.json"), `${JSON.stringify({
|
|
797
|
+
name: options.name,
|
|
798
|
+
version: options.version,
|
|
799
|
+
...planned.manifest,
|
|
800
|
+
type: "module",
|
|
801
|
+
exports: exported,
|
|
802
|
+
files: ["dist", ...planned.files.keys()],
|
|
803
|
+
peerDependencies: present(options.peerDependencies),
|
|
804
|
+
dependencies: present(options.dependencies)
|
|
805
|
+
}, null, 2)}
|
|
806
|
+
`);
|
|
807
|
+
}
|
|
808
|
+
async function buildClientPackage(options) {
|
|
809
|
+
const planned = plan(options);
|
|
810
|
+
const step = {
|
|
811
|
+
timeoutMs: options.timeoutMs ?? 120000,
|
|
812
|
+
signal: options.signal
|
|
813
|
+
};
|
|
814
|
+
const bun = options.executables?.bun ?? "bun";
|
|
815
|
+
const work = await mkdtemp2(join5(tmpdir2(), "stitchkit-client-package-"));
|
|
816
|
+
await rm3(planned.tarball, { force: true });
|
|
817
|
+
try {
|
|
818
|
+
const externals = new Map;
|
|
819
|
+
for (const name of planned.external) {
|
|
820
|
+
externals.set(name, await resolveExternalPackage(planned.root, name));
|
|
821
|
+
}
|
|
822
|
+
const tsc = options.executables?.tsc !== undefined ? [options.executables.tsc] : [
|
|
823
|
+
process.execPath,
|
|
824
|
+
join5((await resolveExternalPackage(planned.root, "typescript")).directory, "bin", "tsc")
|
|
825
|
+
];
|
|
826
|
+
const staged = join5(work, "package");
|
|
827
|
+
const graph = await bundle(options, planned, step, bun, work);
|
|
828
|
+
refuseForbiddenModules(graph, options.forbiddenModules ?? []);
|
|
829
|
+
await refuseBundledExternals(graph, externals);
|
|
830
|
+
await emitClientDeclarations({
|
|
831
|
+
step,
|
|
832
|
+
tsc,
|
|
833
|
+
tsconfig: planned.tsconfig,
|
|
834
|
+
entries: [...planned.entries.values()],
|
|
835
|
+
outDir: join5(staged, "dist")
|
|
836
|
+
});
|
|
837
|
+
normalizeClientDeclarations(join5(staged, "dist"), {
|
|
838
|
+
packages: new Set(planned.external),
|
|
839
|
+
runtime: options.runtime ?? "node"
|
|
840
|
+
});
|
|
841
|
+
await stage(options, planned, graph, staged);
|
|
842
|
+
await mkdir3(dirname5(planned.tarball), { recursive: true });
|
|
843
|
+
const packed = await runClientTool(step, bun, ["pm", "pack", "--filename", planned.tarball, "--ignore-scripts", "--quiet"], staged);
|
|
844
|
+
if (!packed.ok) {
|
|
845
|
+
throw new ClientPackageError("install", `Client package pack failed (exit ${packed.exit}): ${packed.output}`, { output: packed.output });
|
|
846
|
+
}
|
|
847
|
+
const consumer = join5(work, "consumer");
|
|
848
|
+
await mkdir3(consumer);
|
|
849
|
+
const proven = await proveClientPackage({
|
|
850
|
+
options,
|
|
851
|
+
step,
|
|
852
|
+
tools: { bun, node: options.executables?.node ?? "node", tsc },
|
|
853
|
+
consumer,
|
|
854
|
+
staged,
|
|
855
|
+
tarball: planned.tarball,
|
|
856
|
+
externals,
|
|
857
|
+
root: planned.root
|
|
858
|
+
});
|
|
859
|
+
const archive = await readFile4(planned.tarball);
|
|
860
|
+
return {
|
|
861
|
+
tarball: planned.tarball,
|
|
862
|
+
sha256: createHash3("sha256").update(archive).digest("hex"),
|
|
863
|
+
bytes: archive.byteLength,
|
|
864
|
+
modules: [...graph.modules.keys()].sort(),
|
|
865
|
+
...proven
|
|
866
|
+
};
|
|
867
|
+
} catch (error) {
|
|
868
|
+
await rm3(planned.tarball, { force: true });
|
|
869
|
+
throw error;
|
|
870
|
+
} finally {
|
|
871
|
+
await rm3(work, { recursive: true, force: true });
|
|
872
|
+
}
|
|
873
|
+
}
|
|
175
874
|
// src/files/native-packaging-layout.ts
|
|
176
|
-
import { createHash as
|
|
177
|
-
import { z as
|
|
178
|
-
var RelativePathSchema =
|
|
179
|
-
var NativeLayoutSchema =
|
|
875
|
+
import { createHash as createHash4 } from "node:crypto";
|
|
876
|
+
import { z as z4 } from "zod";
|
|
877
|
+
var RelativePathSchema = z4.string().min(1).refine((value) => !value.startsWith("/") && !/^[a-z]:/i.test(value) && !value.includes("\\") && value.split("/").every((part) => part !== ".." && part !== "." && part.length > 0), "Expected a relative path without traversal");
|
|
878
|
+
var NativeLayoutSchema = z4.strictObject({
|
|
180
879
|
loader: RelativePathSchema,
|
|
181
|
-
assets:
|
|
880
|
+
assets: z4.strictObject({ arm64: RelativePathSchema, x64: RelativePathSchema })
|
|
182
881
|
});
|
|
183
|
-
var NativeAssetEntrySchema =
|
|
882
|
+
var NativeAssetEntrySchema = z4.strictObject({
|
|
184
883
|
path: RelativePathSchema,
|
|
185
|
-
size:
|
|
186
|
-
sha256:
|
|
884
|
+
size: z4.int().nonnegative(),
|
|
885
|
+
sha256: z4.string().regex(/^[a-f0-9]{64}$/)
|
|
187
886
|
});
|
|
188
|
-
var NativeAssetManifestSchema =
|
|
189
|
-
formatVersion:
|
|
887
|
+
var NativeAssetManifestSchema = z4.strictObject({
|
|
888
|
+
formatVersion: z4.literal(2, {
|
|
190
889
|
error: "Native asset manifest formatVersion must be 2 for this Stitchkit"
|
|
191
890
|
}),
|
|
192
891
|
loader: RelativePathSchema,
|
|
193
|
-
assets:
|
|
892
|
+
assets: z4.strictObject({
|
|
194
893
|
arm64: NativeAssetEntrySchema.optional(),
|
|
195
894
|
x64: NativeAssetEntrySchema.optional()
|
|
196
895
|
})
|
|
197
896
|
});
|
|
198
897
|
function nativeAssetDigest(bytes) {
|
|
199
|
-
return { size: bytes.byteLength, sha256:
|
|
898
|
+
return { size: bytes.byteLength, sha256: createHash4("sha256").update(bytes).digest("hex") };
|
|
200
899
|
}
|
|
201
900
|
var LOADER_MARK = ["stitchkit", "native", "loader"].join("-");
|
|
202
901
|
function nativeLoaderMarker(state) {
|
|
@@ -255,15 +954,15 @@ function inspectNativeArtifact(bytes) {
|
|
|
255
954
|
return "no-loader";
|
|
256
955
|
}
|
|
257
956
|
// src/files/native-packaging.ts
|
|
258
|
-
import { existsSync as
|
|
259
|
-
import { dirname as
|
|
957
|
+
import { existsSync as existsSync5, readFileSync as readFileSync3 } from "node:fs";
|
|
958
|
+
import { dirname as dirname6, relative as relative4, resolve as resolve5 } from "node:path";
|
|
260
959
|
import { fileURLToPath } from "node:url";
|
|
261
|
-
import { z as
|
|
960
|
+
import { z as z5 } from "zod";
|
|
262
961
|
|
|
263
962
|
// src/files/native-packaging-plugin.ts
|
|
264
|
-
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
265
|
-
import { basename as
|
|
266
|
-
var
|
|
963
|
+
import { existsSync as existsSync4, readFileSync as readFileSync2, realpathSync } from "node:fs";
|
|
964
|
+
import { basename as basename3, join as join6 } from "node:path";
|
|
965
|
+
var realpath3 = (path) => {
|
|
267
966
|
try {
|
|
268
967
|
return realpathSync(path);
|
|
269
968
|
} catch {
|
|
@@ -275,11 +974,11 @@ function loaderInstallation(path, loaderName) {
|
|
|
275
974
|
if (!path.endsWith(suffix))
|
|
276
975
|
return;
|
|
277
976
|
const root = path.slice(0, -suffix.length);
|
|
278
|
-
const manifest =
|
|
279
|
-
if (!
|
|
977
|
+
const manifest = join6(root, "package.json");
|
|
978
|
+
if (!existsSync4(manifest))
|
|
280
979
|
return;
|
|
281
980
|
try {
|
|
282
|
-
const parsed = JSON.parse(
|
|
981
|
+
const parsed = JSON.parse(readFileSync2(manifest, "utf8"));
|
|
283
982
|
if (typeof parsed !== "object" || parsed === null)
|
|
284
983
|
return;
|
|
285
984
|
const name = Reflect.get(parsed, "name");
|
|
@@ -294,8 +993,8 @@ function loaderInstallation(path, loaderName) {
|
|
|
294
993
|
function nativePlugin(owner, specifiers, embedded, companionEntry) {
|
|
295
994
|
const contents = nativeLoaderSource(specifiers, "static");
|
|
296
995
|
const external = new Set(Object.values(specifiers));
|
|
297
|
-
const ownLoader =
|
|
298
|
-
const loaderFile =
|
|
996
|
+
const ownLoader = realpath3(owner.loaderPath);
|
|
997
|
+
const loaderFile = basename3(owner.loaderName);
|
|
299
998
|
return {
|
|
300
999
|
name: "stitchkit-native-packaging",
|
|
301
1000
|
setup(build) {
|
|
@@ -312,9 +1011,9 @@ function nativePlugin(owner, specifiers, embedded, companionEntry) {
|
|
|
312
1011
|
build.onLoad({ filter: /\.[cm]?js$/ }, (args) => {
|
|
313
1012
|
if (args.path === owner.loaderPath)
|
|
314
1013
|
return { contents, loader: "js" };
|
|
315
|
-
if (
|
|
1014
|
+
if (basename3(args.path) !== loaderFile)
|
|
316
1015
|
return;
|
|
317
|
-
if (
|
|
1016
|
+
if (realpath3(args.path) === ownLoader)
|
|
318
1017
|
return { contents, loader: "js" };
|
|
319
1018
|
const foreign = loaderInstallation(args.path, owner.loaderName);
|
|
320
1019
|
if (!foreign)
|
|
@@ -327,18 +1026,18 @@ function nativePlugin(owner, specifiers, embedded, companionEntry) {
|
|
|
327
1026
|
return bytes ? { contents: bytes, loader: "napi" } : undefined;
|
|
328
1027
|
});
|
|
329
1028
|
if (companionEntry !== undefined)
|
|
330
|
-
build.onResolve({ filter: /./ }, (args) => (args.importer === owner.loaderPath ||
|
|
1029
|
+
build.onResolve({ filter: /./ }, (args) => (args.importer === owner.loaderPath || realpath3(args.importer) === ownLoader) && external.has(args.path) ? { path: args.path, external: true } : undefined);
|
|
331
1030
|
}
|
|
332
1031
|
};
|
|
333
1032
|
}
|
|
334
1033
|
|
|
335
1034
|
// src/files/native-packaging.ts
|
|
336
|
-
var PackageManifestSchema =
|
|
337
|
-
name:
|
|
338
|
-
version:
|
|
1035
|
+
var PackageManifestSchema = z5.object({
|
|
1036
|
+
name: z5.literal("stitchkit"),
|
|
1037
|
+
version: z5.string().min(1)
|
|
339
1038
|
});
|
|
340
|
-
var ArchitectureSchema =
|
|
341
|
-
var PlatformSchema =
|
|
1039
|
+
var ArchitectureSchema = z5.enum(["arm64", "x64"]);
|
|
1040
|
+
var PlatformSchema = z5.enum(["darwin"]);
|
|
342
1041
|
var CommonOptions = {
|
|
343
1042
|
platform: PlatformSchema,
|
|
344
1043
|
entryPath: RelativePathSchema.refine((value) => !value.includes("[") && !value.includes("]"), "Expected a fixed entry path without Bun naming templates")
|
|
@@ -346,24 +1045,24 @@ var CommonOptions = {
|
|
|
346
1045
|
var ASSET_HASH_TOKEN = "[hash]";
|
|
347
1046
|
var ASSET_HASH_LENGTH = 16;
|
|
348
1047
|
var AssetPathSchema = RelativePathSchema.refine((value) => !/[[\]]/.test(value.replaceAll(ASSET_HASH_TOKEN, "")), "Expected an addon path whose only template is [hash]");
|
|
349
|
-
var SingleOptionsSchema =
|
|
1048
|
+
var SingleOptionsSchema = z5.object({
|
|
350
1049
|
...CommonOptions,
|
|
351
|
-
architecture:
|
|
352
|
-
delivery:
|
|
1050
|
+
architecture: z5.string().min(1),
|
|
1051
|
+
delivery: z5.literal("companion"),
|
|
353
1052
|
assetPath: AssetPathSchema
|
|
354
1053
|
});
|
|
355
|
-
var EmbeddedOptionsSchema =
|
|
1054
|
+
var EmbeddedOptionsSchema = z5.strictObject({
|
|
356
1055
|
platform: PlatformSchema,
|
|
357
|
-
architecture:
|
|
358
|
-
delivery:
|
|
1056
|
+
architecture: z5.string().min(1),
|
|
1057
|
+
delivery: z5.literal("embedded")
|
|
359
1058
|
});
|
|
360
|
-
var MultipleOptionsSchema =
|
|
1059
|
+
var MultipleOptionsSchema = z5.object({
|
|
361
1060
|
...CommonOptions,
|
|
362
|
-
architecture:
|
|
363
|
-
delivery:
|
|
364
|
-
assetPath:
|
|
1061
|
+
architecture: z5.array(z5.string().min(1)).refine((targets) => targets.length > 0),
|
|
1062
|
+
delivery: z5.literal("companion"),
|
|
1063
|
+
assetPath: z5.record(z5.string().min(1), AssetPathSchema)
|
|
365
1064
|
});
|
|
366
|
-
var CompanionOptionsSchema =
|
|
1065
|
+
var CompanionOptionsSchema = z5.union([SingleOptionsSchema, MultipleOptionsSchema]).check((ctx) => {
|
|
367
1066
|
const input = ctx.value;
|
|
368
1067
|
const targets = typeof input.architecture === "string" ? [input.architecture] : input.architecture;
|
|
369
1068
|
if (new Set(targets).size !== targets.length)
|
|
@@ -395,35 +1094,35 @@ var CompanionOptionsSchema = z3.union([SingleOptionsSchema, MultipleOptionsSchem
|
|
|
395
1094
|
}
|
|
396
1095
|
}
|
|
397
1096
|
});
|
|
398
|
-
var EmbeddedAssetSchema =
|
|
399
|
-
size:
|
|
400
|
-
sha256:
|
|
401
|
-
bytes:
|
|
1097
|
+
var EmbeddedAssetSchema = z5.object({
|
|
1098
|
+
size: z5.int().nonnegative(),
|
|
1099
|
+
sha256: z5.string().regex(/^[a-f0-9]{64}$/),
|
|
1100
|
+
bytes: z5.custom((value) => value instanceof Uint8Array)
|
|
402
1101
|
});
|
|
403
|
-
var AssetSchema = EmbeddedAssetSchema.extend({ outputPath:
|
|
1102
|
+
var AssetSchema = EmbeddedAssetSchema.extend({ outputPath: z5.string() });
|
|
404
1103
|
var TargetAssetSchema = AssetSchema.extend({ architecture: ArchitectureSchema });
|
|
405
|
-
var DigestSchema =
|
|
406
|
-
size:
|
|
407
|
-
sha256:
|
|
1104
|
+
var DigestSchema = z5.object({
|
|
1105
|
+
size: z5.int().nonnegative(),
|
|
1106
|
+
sha256: z5.string().regex(/^[a-f0-9]{64}$/)
|
|
408
1107
|
});
|
|
409
|
-
var RefusalSchema =
|
|
410
|
-
|
|
411
|
-
state:
|
|
1108
|
+
var RefusalSchema = z5.discriminatedUnion("state", [
|
|
1109
|
+
z5.object({
|
|
1110
|
+
state: z5.literal("unsupported"),
|
|
412
1111
|
platform: PlatformSchema,
|
|
413
|
-
architecture:
|
|
414
|
-
code:
|
|
1112
|
+
architecture: z5.string(),
|
|
1113
|
+
code: z5.literal("NATIVE_TARGET_UNSUPPORTED")
|
|
415
1114
|
}),
|
|
416
|
-
|
|
417
|
-
state:
|
|
1115
|
+
z5.object({
|
|
1116
|
+
state: z5.literal("missing"),
|
|
418
1117
|
platform: PlatformSchema,
|
|
419
1118
|
architecture: ArchitectureSchema,
|
|
420
|
-
code:
|
|
1119
|
+
code: z5.literal("NATIVE_ASSET_MISSING")
|
|
421
1120
|
}),
|
|
422
|
-
|
|
423
|
-
state:
|
|
1121
|
+
z5.object({
|
|
1122
|
+
state: z5.literal("mismatch"),
|
|
424
1123
|
platform: PlatformSchema,
|
|
425
1124
|
architecture: ArchitectureSchema,
|
|
426
|
-
code:
|
|
1125
|
+
code: z5.literal("NATIVE_ASSET_DIGEST_MISMATCH"),
|
|
427
1126
|
expected: DigestSchema,
|
|
428
1127
|
actual: DigestSchema
|
|
429
1128
|
})
|
|
@@ -451,11 +1150,11 @@ function resolveTargets(platform, declared) {
|
|
|
451
1150
|
return { targets };
|
|
452
1151
|
}
|
|
453
1152
|
function installedPackageRoot() {
|
|
454
|
-
let directory =
|
|
1153
|
+
let directory = dirname6(fileURLToPath(import.meta.url));
|
|
455
1154
|
for (let depth = 0;depth < 64; depth++) {
|
|
456
|
-
if (
|
|
1155
|
+
if (existsSync5(resolve5(directory, "native-assets.json")))
|
|
457
1156
|
return directory;
|
|
458
|
-
const parent =
|
|
1157
|
+
const parent = dirname6(directory);
|
|
459
1158
|
if (parent === directory)
|
|
460
1159
|
break;
|
|
461
1160
|
directory = parent;
|
|
@@ -468,10 +1167,10 @@ function readVerifiedAsset(root, published, platform, architecture) {
|
|
|
468
1167
|
};
|
|
469
1168
|
if (!published)
|
|
470
1169
|
return missing;
|
|
471
|
-
const sourcePath =
|
|
1170
|
+
const sourcePath = resolve5(root, published.path);
|
|
472
1171
|
let bytes;
|
|
473
1172
|
try {
|
|
474
|
-
bytes =
|
|
1173
|
+
bytes = readFileSync3(sourcePath);
|
|
475
1174
|
} catch (cause) {
|
|
476
1175
|
if (errnoOf(cause) === "ENOENT")
|
|
477
1176
|
return missing;
|
|
@@ -493,13 +1192,13 @@ function readVerifiedAsset(root, published, platform, architecture) {
|
|
|
493
1192
|
}
|
|
494
1193
|
function installedPackage() {
|
|
495
1194
|
const root = installedPackageRoot();
|
|
496
|
-
const published = NativeAssetManifestSchema.parse(JSON.parse(
|
|
497
|
-
const manifest = PackageManifestSchema.parse(JSON.parse(
|
|
1195
|
+
const published = NativeAssetManifestSchema.parse(JSON.parse(readFileSync3(resolve5(root, "native-assets.json"), "utf8")));
|
|
1196
|
+
const manifest = PackageManifestSchema.parse(JSON.parse(readFileSync3(resolve5(root, "package.json"), "utf8")));
|
|
498
1197
|
return {
|
|
499
1198
|
root,
|
|
500
1199
|
published,
|
|
501
1200
|
version: manifest.version,
|
|
502
|
-
loaderPath:
|
|
1201
|
+
loaderPath: resolve5(root, published.loader),
|
|
503
1202
|
loaderName: published.loader
|
|
504
1203
|
};
|
|
505
1204
|
}
|
|
@@ -542,7 +1241,7 @@ function packageCompanion(input) {
|
|
|
542
1241
|
const outputPath = template.replaceAll(ASSET_HASH_TOKEN, sha256.slice(0, ASSET_HASH_LENGTH));
|
|
543
1242
|
if (assets.some((asset) => asset.outputPath === outputPath))
|
|
544
1243
|
throw new Error("Addons must have distinct output paths");
|
|
545
|
-
const specifier =
|
|
1244
|
+
const specifier = relative4(dirname6(input.entryPath), outputPath).split("\\").join("/");
|
|
546
1245
|
specifiers[architecture] = specifier.startsWith(".") ? specifier : `./${specifier}`;
|
|
547
1246
|
const asset = { outputPath, size, sha256, bytes };
|
|
548
1247
|
assets.push(typeof input.architecture === "string" ? asset : { ...asset, architecture });
|
|
@@ -560,8 +1259,10 @@ function createNativePackaging(options) {
|
|
|
560
1259
|
return options.delivery === "embedded" ? embedNative(EmbeddedOptionsSchema.parse(options)) : packageCompanion(CompanionOptionsSchema.parse(options));
|
|
561
1260
|
}
|
|
562
1261
|
export {
|
|
1262
|
+
ClientPackageError,
|
|
563
1263
|
ForbiddenModuleError,
|
|
564
1264
|
buildClientArtifact,
|
|
1265
|
+
buildClientPackage,
|
|
565
1266
|
createNativePackaging,
|
|
566
1267
|
inspectNativeArtifact
|
|
567
1268
|
};
|