@johnhenry/mallory-cli 0.0.0-stage → 0.1.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/README.md +60 -2
- package/bin/mallory.mjs +51 -0
- package/package.json +51 -4
- package/src/a2a/client.js +511 -0
- package/src/a2a/demo-agent.js +396 -0
- package/src/a2a/hub.js +578 -0
- package/src/a2a/settings.js +88 -0
- package/src/a2a/task.js +358 -0
- package/src/acp/client.js +573 -0
- package/src/acp/demo-agent.js +332 -0
- package/src/acp/hub.js +632 -0
- package/src/acp/settings.js +78 -0
- package/src/actions/map.js +524 -0
- package/src/agent/cloud.js +77 -0
- package/src/agent/compact.js +84 -0
- package/src/agent/constants.js +39 -0
- package/src/agent/diff.js +101 -0
- package/src/agent/fork.js +159 -0
- package/src/agent/grounding.js +73 -0
- package/src/agent/guards.js +129 -0
- package/src/agent/local-transport.js +152 -0
- package/src/agent/loop.js +479 -0
- package/src/agent/park-store.js +77 -0
- package/src/agent/park.js +76 -0
- package/src/agent/ports.js +28 -0
- package/src/agent/providers.js +591 -0
- package/src/agent/restart.js +51 -0
- package/src/agent/router.js +380 -0
- package/src/agent/run.js +861 -0
- package/src/agent/tools.js +247 -0
- package/src/agent-library/engine.js +43 -0
- package/src/ai/bridge.js +155 -0
- package/src/ai/policy.js +131 -0
- package/src/aimatey/vendor.js +18 -0
- package/src/audio/container-duration.js +35 -0
- package/src/audio/instruments.js +776 -0
- package/src/audio/lanes.js +230 -0
- package/src/audio/patch.js +1410 -0
- package/src/audio/plan.js +276 -0
- package/src/audio/rack.js +1006 -0
- package/src/audio/sample-trim.js +293 -0
- package/src/audio/soundfont.js +243 -0
- package/src/audio/take-store.js +87 -0
- package/src/audio/takes.js +129 -0
- package/src/audio/wav.js +170 -0
- package/src/audio/zzfx.js +333 -0
- package/src/autonomy/bundle.js +207 -0
- package/src/autonomy/confine.js +61 -0
- package/src/autonomy/enforce.js +155 -0
- package/src/autonomy/file-kv.js +88 -0
- package/src/autonomy/ledger.js +203 -0
- package/src/autonomy/pins.js +50 -0
- package/src/autonomy/psl/index.js +32 -0
- package/src/autonomy/psl/suffixes.json +1 -0
- package/src/autonomy/record.js +28 -0
- package/src/autonomy/routines.js +107 -0
- package/src/autonomy/server.js +175 -0
- package/src/autonomy/signer-node.js +167 -0
- package/src/autonomy/store.js +43 -0
- package/src/autonomy/unattended.js +13 -0
- package/src/bytes/bytes.js +378 -0
- package/src/calc/syntax.js +125 -0
- package/src/calc/tex.js +28 -0
- package/src/calendar/vendor.js +2 -0
- package/src/clipboard.js +130 -0
- package/src/clsh/clsh.js +3417 -0
- package/src/codec/capnp.js +1804 -0
- package/src/codec/core.js +2043 -0
- package/src/codec/vendor/avro.js +136 -0
- package/src/codec/vendor/cbor.js +25 -0
- package/src/codec/vendor/flatbuffers.js +206 -0
- package/src/codec/vendor/msgpack.js +9 -0
- package/src/codec/vendor/protobuf.js +53 -0
- package/src/delegation/caps.js +60 -0
- package/src/delegation/config.js +32 -0
- package/src/device/model.js +246 -0
- package/src/device/recording.js +198 -0
- package/src/device/stream-model.js +151 -0
- package/src/documents/documents.js +1224 -0
- package/src/effects/catalog.js +63 -0
- package/src/effects/extras.js +133 -0
- package/src/effects/parse.js +339 -0
- package/src/embed/protocol.js +131 -0
- package/src/env/env.js +159 -0
- package/src/exact/vendor.js +45 -0
- package/src/example-meta.js +164 -0
- package/src/examples.js +94 -0
- package/src/export/canvas-image.js +217 -0
- package/src/export/expr-latex-lazy.js +8 -0
- package/src/export/formats.js +322 -0
- package/src/export/latex.js +183 -0
- package/src/export/pane.js +184 -0
- package/src/export/value.js +452 -0
- package/src/export/zip.js +214 -0
- package/src/ext/access.js +58 -0
- package/src/ext/btasks.js +225 -0
- package/src/ext/client.js +209 -0
- package/src/ext/tools.js +69 -0
- package/src/ext/view.js +105 -0
- package/src/ext/webmcp-tabs.js +299 -0
- package/src/files/archive.js +232 -0
- package/src/files/files.js +317 -0
- package/src/formats/json.js +24 -0
- package/src/formats/jupyter.js +188 -0
- package/src/formats/markdown.js +254 -0
- package/src/formats/n8n.js +276 -0
- package/src/formats/node-red.js +363 -0
- package/src/formats/registry.js +177 -0
- package/src/formats/rivet.js +259 -0
- package/src/formats/shared.js +98 -0
- package/src/formats/structure.js +182 -0
- package/src/formats/yaml.js +127 -0
- package/src/frame/frame.js +962 -0
- package/src/frame/vendor.js +1484 -0
- package/src/gate/audit.js +116 -0
- package/src/gate/gate.js +365 -0
- package/src/gate/policy.js +295 -0
- package/src/gate/scripted.js +110 -0
- package/src/gate/seams.js +32 -0
- package/src/gate/store.js +66 -0
- package/src/geometry/model.js +533 -0
- package/src/grant-key.js +149 -0
- package/src/graph/model.js +381 -0
- package/src/headless/a2a-push.js +123 -0
- package/src/headless/channels/config.js +142 -0
- package/src/headless/channels/connectors.js +283 -0
- package/src/headless/channels/index.js +257 -0
- package/src/headless/channels/mail.js +197 -0
- package/src/headless/channels/mailnet.js +223 -0
- package/src/headless/channels/platforms.js +194 -0
- package/src/headless/channels/replay.js +49 -0
- package/src/headless/cli.js +1397 -0
- package/src/headless/companion-client.js +182 -0
- package/src/headless/cost-cli.js +177 -0
- package/src/headless/deprecations-cli.js +102 -0
- package/src/headless/examples.js +48 -0
- package/src/headless/ffmpeg-exec.js +217 -0
- package/src/headless/gateways/config.js +213 -0
- package/src/headless/gateways/homeassistant.js +159 -0
- package/src/headless/gateways/index.js +121 -0
- package/src/headless/gateways/matter.js +150 -0
- package/src/headless/gateways/mqtt-client.js +177 -0
- package/src/headless/gateways/mqtt.js +70 -0
- package/src/headless/gateways/netway.js +226 -0
- package/src/headless/gateways/serverpod-worker.mjs +16 -0
- package/src/headless/gateways/serverpod.js +71 -0
- package/src/headless/gateways/util.js +27 -0
- package/src/headless/grant-cli.js +208 -0
- package/src/headless/guard.js +125 -0
- package/src/headless/guards.js +124 -0
- package/src/headless/machine-cli.js +853 -0
- package/src/headless/mcp-endpoint.js +287 -0
- package/src/headless/mcp-roots.js +38 -0
- package/src/headless/model-endpoint.js +88 -0
- package/src/headless/net.js +151 -0
- package/src/headless/node.js +263 -0
- package/src/headless/npm-tarball.js +29 -0
- package/src/headless/operator-page.js +112 -0
- package/src/headless/operator.js +242 -0
- package/src/headless/people.js +34 -0
- package/src/headless/pkg-cli.js +1103 -0
- package/src/headless/pkg-serve.js +138 -0
- package/src/headless/process-worker.js +88 -0
- package/src/headless/runner-remote.js +477 -0
- package/src/headless/sandbox-child.mjs +21 -0
- package/src/headless/scheduled.js +557 -0
- package/src/headless/serve-stream.js +97 -0
- package/src/headless/serve.js +2238 -0
- package/src/headless/short-links.js +158 -0
- package/src/headless/static-dirs.js +207 -0
- package/src/headless/trust-file.js +51 -0
- package/src/headless/watch.js +291 -0
- package/src/headless/ws-connect.js +65 -0
- package/src/headless/ws-connections.js +44 -0
- package/src/headless/ws-echo.js +150 -0
- package/src/headless/ws-proxy.js +354 -0
- package/src/headless/ws-ticket.js +48 -0
- package/src/hooks/config.js +50 -0
- package/src/hooks/engine.js +189 -0
- package/src/input/log.js +613 -0
- package/src/input/player.js +217 -0
- package/src/input/replay.js +209 -0
- package/src/jth/vendor.js +28 -0
- package/src/ledger/tx.js +94 -0
- package/src/machine/apply.js +193 -0
- package/src/machine/diff.js +168 -0
- package/src/machine/drive.js +296 -0
- package/src/machine/engine.js +478 -0
- package/src/machine/files-tool.js +76 -0
- package/src/machine/lint.js +139 -0
- package/src/machine/llmtm-trace.js +117 -0
- package/src/machine/llmtm.js +475 -0
- package/src/machine/otel.js +80 -0
- package/src/machine/pane-provider.js +51 -0
- package/src/machine/propose.js +159 -0
- package/src/machine/random.js +52 -0
- package/src/machine/risk.js +107 -0
- package/src/machine/session.js +1017 -0
- package/src/machine/shell-tool.js +45 -0
- package/src/machine/tape.js +349 -0
- package/src/machine/templates.js +104 -0
- package/src/machine/trace.js +664 -0
- package/src/machine/view.js +73 -0
- package/src/machine/vm-tool.js +47 -0
- package/src/machine/watchdog.js +46 -0
- package/src/mcp/app-args.js +51 -0
- package/src/mcp/app-run.js +56 -0
- package/src/mcp/apps.js +468 -0
- package/src/mcp/cache.js +232 -0
- package/src/mcp/client.js +507 -0
- package/src/mcp/demo-app.js +183 -0
- package/src/mcp/demo-server.js +602 -0
- package/src/mcp/elicitation.js +116 -0
- package/src/mcp/hub.js +1326 -0
- package/src/mcp/roots.js +139 -0
- package/src/mcp/sampling.js +105 -0
- package/src/mcp/settings.js +125 -0
- package/src/mcp/tape.js +292 -0
- package/src/mcp/uri-template.js +156 -0
- package/src/mcp/wsh-client.js +164 -0
- package/src/media/animated-codec.js +7 -0
- package/src/media/animated.js +623 -0
- package/src/media/compare-agent.js +70 -0
- package/src/media/compare-job.js +63 -0
- package/src/media/compare-page.js +296 -0
- package/src/media/compare-worker.js +14 -0
- package/src/media/compare.js +600 -0
- package/src/media/contract.js +62 -0
- package/src/media/decode-frame.js +57 -0
- package/src/media/decode-pool.js +188 -0
- package/src/media/ffmpeg-edit.js +344 -0
- package/src/media/ffmpeg-wasm-client.js +121 -0
- package/src/media/ffmpeg-wasm-session.js +79 -0
- package/src/media/ffmpeg-wasm.js +139 -0
- package/src/media/ffmpeg-worker.js +23 -0
- package/src/media/ffmpeg.js +903 -0
- package/src/media/helpers.js +419 -0
- package/src/media/keep.js +108 -0
- package/src/media/metadata-worker.js +16 -0
- package/src/media/metadata.js +2518 -0
- package/src/media/mime.js +102 -0
- package/src/media/page-decode.js +74 -0
- package/src/media/persist.js +114 -0
- package/src/media/png.js +226 -0
- package/src/media/sample-input.js +67 -0
- package/src/media/store.js +86 -0
- package/src/media/wav.js +109 -0
- package/src/midi/mappings.js +250 -0
- package/src/midi/message.js +125 -0
- package/src/model/action-hub.js +143 -0
- package/src/model/adapters.js +284 -0
- package/src/model/agent-runs.js +12 -0
- package/src/model/arrange.js +308 -0
- package/src/model/automation-clock.js +494 -0
- package/src/model/automation.js +653 -0
- package/src/model/batches.js +78 -0
- package/src/model/binary-view.js +195 -0
- package/src/model/breakpoints.js +392 -0
- package/src/model/calc-settings.js +65 -0
- package/src/model/calendar.js +254 -0
- package/src/model/canvas-id.js +5 -0
- package/src/model/canvas.js +5048 -0
- package/src/model/channel-names.js +29 -0
- package/src/model/channels.js +692 -0
- package/src/model/chart-clock.js +631 -0
- package/src/model/chart-code.js +69 -0
- package/src/model/codecs.js +60 -0
- package/src/model/companion-access.js +271 -0
- package/src/model/companion-pane.js +50 -0
- package/src/model/composite.js +324 -0
- package/src/model/context.js +380 -0
- package/src/model/controls.js +231 -0
- package/src/model/converter-presets.js +368 -0
- package/src/model/cost.js +580 -0
- package/src/model/curve-transform.js +122 -0
- package/src/model/cycles.js +176 -0
- package/src/model/debug-log.js +168 -0
- package/src/model/decision-clock.js +100 -0
- package/src/model/decisions.js +265 -0
- package/src/model/desk.js +87 -0
- package/src/model/device-hub.js +400 -0
- package/src/model/diff.js +445 -0
- package/src/model/emit-events.js +179 -0
- package/src/model/errors.js +238 -0
- package/src/model/event-presets.js +297 -0
- package/src/model/events.js +1303 -0
- package/src/model/explicit-setters.js +139 -0
- package/src/model/field-mapping.js +623 -0
- package/src/model/files-pane.js +263 -0
- package/src/model/flow-idle.js +89 -0
- package/src/model/foreach.js +549 -0
- package/src/model/frame-loop.js +71 -0
- package/src/model/frame-pressure.js +180 -0
- package/src/model/function-scaffold.js +1000 -0
- package/src/model/geometry-settings.js +54 -0
- package/src/model/goals.js +176 -0
- package/src/model/graph-settings.js +76 -0
- package/src/model/grid-widget.js +175 -0
- package/src/model/groups.js +332 -0
- package/src/model/hash.js +21 -0
- package/src/model/history.js +259 -0
- package/src/model/http-copy.js +88 -0
- package/src/model/http-send-companion.js +110 -0
- package/src/model/http-send.js +163 -0
- package/src/model/http-tap.js +521 -0
- package/src/model/http-tools.js +1138 -0
- package/src/model/import-lock.js +78 -0
- package/src/model/imports.js +258 -0
- package/src/model/infer-outputs.js +822 -0
- package/src/model/ink-connect.js +150 -0
- package/src/model/ink-diff.js +75 -0
- package/src/model/ink-gone.js +71 -0
- package/src/model/ink-route.js +120 -0
- package/src/model/ink-svg.js +206 -0
- package/src/model/ink.js +739 -0
- package/src/model/input-log-hub.js +456 -0
- package/src/model/input-split.js +69 -0
- package/src/model/json-schema-host.js +9 -0
- package/src/model/json-schema.js +368 -0
- package/src/model/jth.js +78 -0
- package/src/model/latex-expr.js +528 -0
- package/src/model/layout-history.js +120 -0
- package/src/model/lazy-map.js +88 -0
- package/src/model/ledger-pane.js +101 -0
- package/src/model/legacy-viaframe.js +90 -0
- package/src/model/lessons.js +448 -0
- package/src/model/links.js +981 -0
- package/src/model/live.js +411 -0
- package/src/model/math-widget.js +53 -0
- package/src/model/memory-settings.js +33 -0
- package/src/model/midi-controls.js +350 -0
- package/src/model/nl-time.js +408 -0
- package/src/model/notebook.js +137 -0
- package/src/model/pane-view-store.js +15 -0
- package/src/model/partial.js +115 -0
- package/src/model/pause-clock.js +53 -0
- package/src/model/peer-pane.js +32 -0
- package/src/model/persist-state.js +290 -0
- package/src/model/person-only.js +122 -0
- package/src/model/pin.js +209 -0
- package/src/model/plot-3d.js +213 -0
- package/src/model/plot-series.js +157 -0
- package/src/model/plot-settings.js +531 -0
- package/src/model/ports.js +312 -0
- package/src/model/presentation.js +222 -0
- package/src/model/preset-apply.js +316 -0
- package/src/model/preset-banks.js +335 -0
- package/src/model/preset-morph.js +263 -0
- package/src/model/preset-ops.js +205 -0
- package/src/model/preset-player.js +283 -0
- package/src/model/preset-states.js +172 -0
- package/src/model/profile-settings.js +109 -0
- package/src/model/promote.js +103 -0
- package/src/model/protected.js +10 -0
- package/src/model/proxy-log.js +230 -0
- package/src/model/proxy.js +367 -0
- package/src/model/quantity-widget.js +61 -0
- package/src/model/queue-book.js +359 -0
- package/src/model/quick-add.js +224 -0
- package/src/model/rate-limit.js +313 -0
- package/src/model/rate.js +95 -0
- package/src/model/recovery.js +216 -0
- package/src/model/rename-follow.js +113 -0
- package/src/model/repeater.js +1126 -0
- package/src/model/retention.js +664 -0
- package/src/model/route-edit.js +132 -0
- package/src/model/router.js +155 -0
- package/src/model/runaway.js +232 -0
- package/src/model/runlog.js +1033 -0
- package/src/model/runners/a2a.js +2 -0
- package/src/model/runners/acp.js +2 -0
- package/src/model/runners/actions.js +4 -0
- package/src/model/runners/agent.js +4 -0
- package/src/model/runners/audio.js +21 -0
- package/src/model/runners/automation.js +2 -0
- package/src/model/runners/calc.js +23 -0
- package/src/model/runners/catch.js +10 -0
- package/src/model/runners/codec.js +24 -0
- package/src/model/runners/companion-run.js +2 -0
- package/src/model/runners/device.js +7 -0
- package/src/model/runners/dom.js +13 -0
- package/src/model/runners/effects.js +2 -0
- package/src/model/runners/embed.js +11 -0
- package/src/model/runners/eval.js +2 -0
- package/src/model/runners/files.js +19 -0
- package/src/model/runners/geometry.js +23 -0
- package/src/model/runners/goals.js +18 -0
- package/src/model/runners/graph.js +23 -0
- package/src/model/runners/html.js +2 -0
- package/src/model/runners/http-proxy.js +185 -0
- package/src/model/runners/http-send.js +61 -0
- package/src/model/runners/http-static.js +83 -0
- package/src/model/runners/http-tap.js +26 -0
- package/src/model/runners/http.js +40 -0
- package/src/model/runners/import.js +2 -0
- package/src/model/runners/index.js +74 -0
- package/src/model/runners/input.js +14 -0
- package/src/model/runners/inputlog.js +7 -0
- package/src/model/runners/ledger.js +15 -0
- package/src/model/runners/machine.js +4 -0
- package/src/model/runners/mcp.js +2 -0
- package/src/model/runners/mcpapp.js +2 -0
- package/src/model/runners/media.js +18 -0
- package/src/model/runners/memory.js +28 -0
- package/src/model/runners/mesh-inspect.js +13 -0
- package/src/model/runners/peer.js +14 -0
- package/src/model/runners/plot.js +22 -0
- package/src/model/runners/render.js +14 -0
- package/src/model/runners/respond.js +2 -0
- package/src/model/runners/sample-soundfont.js +85 -0
- package/src/model/runners/sample.js +159 -0
- package/src/model/runners/scene.js +8 -0
- package/src/model/runners/score.js +24 -0
- package/src/model/runners/secret.js +10 -0
- package/src/model/runners/select.js +2 -0
- package/src/model/runners/shared.js +4 -0
- package/src/model/runners/shares.js +15 -0
- package/src/model/runners/shell.js +7 -0
- package/src/model/runners/skill.js +26 -0
- package/src/model/runners/state.js +2 -0
- package/src/model/runners/statechart.js +4 -0
- package/src/model/runners/subgraph.js +9 -0
- package/src/model/runners/tabs.js +25 -0
- package/src/model/runners/terminal.js +5 -0
- package/src/model/runners/text.js +4 -0
- package/src/model/runners/timer.js +7 -0
- package/src/model/runners/traffic.js +65 -0
- package/src/model/runners/trigger.js +2 -0
- package/src/model/runners/verdict.js +29 -0
- package/src/model/runners/vm.js +7 -0
- package/src/model/runners/wait.js +4 -0
- package/src/model/runners/ws-client.js +57 -0
- package/src/model/safe-regex.js +719 -0
- package/src/model/sample-settings.js +43 -0
- package/src/model/sample-source.js +47 -0
- package/src/model/saved-mapping.js +153 -0
- package/src/model/score-controllers.js +87 -0
- package/src/model/score-edit.js +399 -0
- package/src/model/score-instruments.js +300 -0
- package/src/model/score-library.js +407 -0
- package/src/model/score-markup.js +235 -0
- package/src/model/score-mix.js +248 -0
- package/src/model/score-patterns.js +276 -0
- package/src/model/score-selection.js +34 -0
- package/src/model/score-time.js +139 -0
- package/src/model/score-transforms.js +336 -0
- package/src/model/score-transport.js +192 -0
- package/src/model/selection.js +351 -0
- package/src/model/serial-controls.js +328 -0
- package/src/model/shares-pane.js +17 -0
- package/src/model/shell-pane.js +106 -0
- package/src/model/signature.js +1122 -0
- package/src/model/size-hints.js +106 -0
- package/src/model/size-ratio.js +12 -0
- package/src/model/skill-settings.js +23 -0
- package/src/model/snapping.js +466 -0
- package/src/model/split.js +375 -0
- package/src/model/spring-player.js +222 -0
- package/src/model/statecharts.js +1369 -0
- package/src/model/states-preset.js +208 -0
- package/src/model/static-route.js +100 -0
- package/src/model/step-words.js +45 -0
- package/src/model/subgraph.js +1042 -0
- package/src/model/switch.js +98 -0
- package/src/model/tabs-store.js +10 -0
- package/src/model/tensor-view.js +146 -0
- package/src/model/timer-clock.js +361 -0
- package/src/model/timers.js +422 -0
- package/src/model/timing.js +212 -0
- package/src/model/traffic-agent.js +111 -0
- package/src/model/traffic-store.js +20 -0
- package/src/model/traffic.js +665 -0
- package/src/model/transport.js +364 -0
- package/src/model/turns.js +86 -0
- package/src/model/types.js +425 -0
- package/src/model/value-path.js +138 -0
- package/src/model/versions.js +535 -0
- package/src/model/view-outputs.js +144 -0
- package/src/model/viewport-bookmarks.js +194 -0
- package/src/model/virtual-clock.js +85 -0
- package/src/model/vm-pane.js +258 -0
- package/src/model/wait-clock.js +715 -0
- package/src/model/wait-persist.js +248 -0
- package/src/model/waits.js +431 -0
- package/src/model/watch.js +80 -0
- package/src/model/webhook.js +496 -0
- package/src/model/when.js +48 -0
- package/src/model/widget-values.js +392 -0
- package/src/model/window-commands.js +278 -0
- package/src/model/windows.js +348 -0
- package/src/model/wire-inspect.js +343 -0
- package/src/model/wire-ops.js +324 -0
- package/src/model/wire-pipeline.js +378 -0
- package/src/model/wire-route.js +637 -0
- package/src/model/workspaces.js +122 -0
- package/src/model/ws-client.js +155 -0
- package/src/model/ws-demo.js +31 -0
- package/src/model/ws-frames.js +141 -0
- package/src/model/ws-hub.js +219 -0
- package/src/model/ws-served.js +13 -0
- package/src/model/xstate.js +374 -0
- package/src/narrative/narrative.js +1557 -0
- package/src/network/auth.js +224 -0
- package/src/network/csp-frame.js +197 -0
- package/src/network/declarative-shadow.js +27 -0
- package/src/network/frame-net-shim.js +962 -0
- package/src/network/hosts.js +157 -0
- package/src/network/policy.js +520 -0
- package/src/network/rtc-shim.js +418 -0
- package/src/network/secret-gate.js +78 -0
- package/src/network/secrets.js +261 -0
- package/src/network/sockets.js +287 -0
- package/src/network/store.js +186 -0
- package/src/network/streams.js +99 -0
- package/src/packages/asset-values.js +50 -0
- package/src/packages/assets.js +99 -0
- package/src/packages/deprecation.js +203 -0
- package/src/packages/fetch.js +152 -0
- package/src/packages/function-canvas.js +73 -0
- package/src/packages/module.js +120 -0
- package/src/packages/package.js +474 -0
- package/src/packages/published.js +27 -0
- package/src/packages/requires.js +83 -0
- package/src/packages/signing.js +145 -0
- package/src/packages/trust.js +107 -0
- package/src/panes/a2a.js +51 -0
- package/src/panes/acp.js +60 -0
- package/src/panes/actions.js +54 -0
- package/src/panes/agent.js +83 -0
- package/src/panes/audio.js +103 -0
- package/src/panes/automation.js +112 -0
- package/src/panes/calc.js +67 -0
- package/src/panes/catch.js +52 -0
- package/src/panes/codec.js +59 -0
- package/src/panes/colors.js +12 -0
- package/src/panes/companion.js +54 -0
- package/src/panes/decision.js +65 -0
- package/src/panes/device.js +56 -0
- package/src/panes/dom.js +37 -0
- package/src/panes/effects.js +118 -0
- package/src/panes/embed.js +30 -0
- package/src/panes/eval.js +186 -0
- package/src/panes/fields.js +299 -0
- package/src/panes/files.js +45 -0
- package/src/panes/gate.js +70 -0
- package/src/panes/geometry.js +64 -0
- package/src/panes/goals.js +75 -0
- package/src/panes/graph.js +66 -0
- package/src/panes/html.js +54 -0
- package/src/panes/http.js +82 -0
- package/src/panes/import.js +38 -0
- package/src/panes/input.js +124 -0
- package/src/panes/inputlog.js +66 -0
- package/src/panes/ledger.js +56 -0
- package/src/panes/machine.js +138 -0
- package/src/panes/mcp.js +85 -0
- package/src/panes/mcpapp.js +64 -0
- package/src/panes/media.js +38 -0
- package/src/panes/memory.js +46 -0
- package/src/panes/mesh-inspect.js +49 -0
- package/src/panes/note.js +38 -0
- package/src/panes/peer.js +44 -0
- package/src/panes/plot.js +196 -0
- package/src/panes/registry.js +475 -0
- package/src/panes/render.js +87 -0
- package/src/panes/respond.js +44 -0
- package/src/panes/router.js +84 -0
- package/src/panes/sample.js +75 -0
- package/src/panes/scene.js +55 -0
- package/src/panes/score.js +87 -0
- package/src/panes/secret.js +38 -0
- package/src/panes/select.js +67 -0
- package/src/panes/shader.js +166 -0
- package/src/panes/shared.js +33 -0
- package/src/panes/shares.js +43 -0
- package/src/panes/shell.js +47 -0
- package/src/panes/skill.js +44 -0
- package/src/panes/state.js +91 -0
- package/src/panes/statechart.js +63 -0
- package/src/panes/story.js +33 -0
- package/src/panes/subgraph.js +94 -0
- package/src/panes/tabs.js +52 -0
- package/src/panes/terminal.js +57 -0
- package/src/panes/text.js +36 -0
- package/src/panes/timer.js +55 -0
- package/src/panes/traffic.js +59 -0
- package/src/panes/verdict.js +62 -0
- package/src/panes/vm.js +46 -0
- package/src/panes/wait.js +61 -0
- package/src/panes/watch.js +57 -0
- package/src/panes/webhook.js +67 -0
- package/src/panes/ws-client.js +67 -0
- package/src/persist.js +281 -0
- package/src/plot/tex.js +19 -0
- package/src/plot/vendor.js +59 -0
- package/src/plot/view-vendor.js +5 -0
- package/src/privacy/levels.js +108 -0
- package/src/protocol/devtools.js +116 -0
- package/src/protocol/inspector.js +368 -0
- package/src/render/time.js +381 -0
- package/src/rooms/mesh-metrics.js +162 -0
- package/src/run/companion.js +355 -0
- package/src/run/describe.js +114 -0
- package/src/run/local-model.js +150 -0
- package/src/run/monitor.js +208 -0
- package/src/run/publish.js +360 -0
- package/src/run/respond-outcome.js +12 -0
- package/src/run/run.js +820 -0
- package/src/run/runner-pairing.js +243 -0
- package/src/run/values.js +122 -0
- package/src/runtime/compile.js +122 -0
- package/src/runtime/console-tag.js +64 -0
- package/src/runtime/frame-signals.js +126 -0
- package/src/runtime/iteration-push.js +19 -0
- package/src/runtime/keys.js +474 -0
- package/src/runtime/midi-input.js +170 -0
- package/src/runtime/person-waits.js +140 -0
- package/src/runtime/resource-timing.js +56 -0
- package/src/runtime/runtime.js +2150 -0
- package/src/runtime/sandbox-modules.js +163 -0
- package/src/runtime/spring.js +246 -0
- package/src/runtime/strict.js +215 -0
- package/src/runtime/transfer.js +75 -0
- package/src/runtime/tween.js +158 -0
- package/src/runtime/wasm-fuel.js +596 -0
- package/src/runtime/worker/00-prelude.js +88 -0
- package/src/runtime/worker/10-helpers.js +295 -0
- package/src/runtime/worker/20-io.js +645 -0
- package/src/runtime/worker/30-previews.js +210 -0
- package/src/runtime/worker/40-profile-lineage.js +278 -0
- package/src/runtime/worker/50-own-worker.js +233 -0
- package/src/runtime/worker/60-timers.js +172 -0
- package/src/runtime/worker/70-state-keyed.js +193 -0
- package/src/runtime/worker/80-each-compile-generators.js +619 -0
- package/src/runtime/worker/90-events-operators.js +655 -0
- package/src/runtime/worker/95-agents-batches.js +1820 -0
- package/src/runtime/worker/96-plot.js +47 -0
- package/src/runtime/worker/97-calc.js +28 -0
- package/src/runtime/worker/98-geometry.js +24 -0
- package/src/runtime/worker/99-graph.js +30 -0
- package/src/runtime/worker/99-shell.js +30 -0
- package/src/safety/config.js +57 -0
- package/src/safety/engine.js +324 -0
- package/src/scene/pixi-scene.js +806 -0
- package/src/scene/scene-assets.js +241 -0
- package/src/scene/scene-edit.js +319 -0
- package/src/secret/settings.js +41 -0
- package/src/security/multipart.js +257 -0
- package/src/security/redact.js +261 -0
- package/src/serial/broker.js +473 -0
- package/src/serial/config.js +185 -0
- package/src/serial/framers.js +389 -0
- package/src/serial/packets.js +64 -0
- package/src/shader/audio-texture.js +87 -0
- package/src/shader/channels.js +496 -0
- package/src/shader/common.js +90 -0
- package/src/shader/model.js +867 -0
- package/src/shader/passes.js +150 -0
- package/src/shader/shadertoy.js +198 -0
- package/src/shell/hub.js +287 -0
- package/src/sim/checkpoints.js +850 -0
- package/src/sim/compile.js +37 -0
- package/src/sim/fixed.js +377 -0
- package/src/sim/headless.js +205 -0
- package/src/sim/physics.js +399 -0
- package/src/smf-helper/vendor.js +2 -0
- package/src/sources/config.js +435 -0
- package/src/storage.js +153 -0
- package/src/table/table-geo.js +2344 -0
- package/src/table/table-lineage.js +808 -0
- package/src/table/table-quality.js +355 -0
- package/src/table/table-schema.js +392 -0
- package/src/table/table-sql.js +1650 -0
- package/src/table/table-stats.js +480 -0
- package/src/table/table.js +2102 -0
- package/src/tensor/tensor.js +1153 -0
- package/src/terminal/hub.js +715 -0
- package/src/terminal/screen.js +516 -0
- package/src/tests/capture.js +88 -0
- package/src/tests/compare.js +480 -0
- package/src/tests/dataset.js +170 -0
- package/src/tests/evaluate.js +286 -0
- package/src/tests/from-runs.js +99 -0
- package/src/tests/judge.js +134 -0
- package/src/tests/matchers.js +263 -0
- package/src/tests/matrix-spec.js +87 -0
- package/src/tests/matrix.js +397 -0
- package/src/tests/metrics.js +369 -0
- package/src/tests/no-value.js +42 -0
- package/src/tests/pane-check.js +45 -0
- package/src/tests/report.js +124 -0
- package/src/tests/reporters.js +140 -0
- package/src/tests/run.js +155 -0
- package/src/tilemap/tilemap.js +1122 -0
- package/src/units/units.js +3 -0
- package/src/util/bytes-text.js +21 -0
- package/src/util/num.js +10 -0
- package/src/util/regexp.js +2 -0
- package/src/util/text.js +38 -0
- package/src/verdict/engine.js +154 -0
- package/src/vm/guest-wsh.js +108 -0
- package/src/vm/hub.js +457 -0
- package/src/vm/image-files.js +4 -0
- package/src/vm/image.js +33 -0
- package/src/vm/image.lock.json +13 -0
- package/src/vm/link.js +266 -0
- package/src/vm/mount.js +212 -0
- package/src/vm/net.js +85 -0
- package/src/vm/pipe-ws.js +187 -0
- package/src/vm/v86-loader.js +11 -0
- package/src/webmcp/args.js +212 -0
- package/src/webmcp/hidden.js +218 -0
- package/src/webmcp/text.js +250 -0
- package/src/webmcp/tools/bookmark-tools.js +106 -0
- package/src/webmcp/tools/breakpoint-tools.js +48 -0
- package/src/webmcp/tools/canvases.js +208 -0
- package/src/webmcp/tools/context-tools.js +84 -0
- package/src/webmcp/tools/core.js +170 -0
- package/src/webmcp/tools/devices.js +41 -0
- package/src/webmcp/tools/edit.js +357 -0
- package/src/webmcp/tools/event-tools.js +145 -0
- package/src/webmcp/tools/image-tools.js +173 -0
- package/src/webmcp/tools/ink-tools.js +247 -0
- package/src/webmcp/tools/input-log-tools.js +163 -0
- package/src/webmcp/tools/live.js +166 -0
- package/src/webmcp/tools/machine-tools.js +74 -0
- package/src/webmcp/tools/media.js +161 -0
- package/src/webmcp/tools/midi-tools.js +32 -0
- package/src/webmcp/tools/preset-tools.js +184 -0
- package/src/webmcp/tools/quick-add-tools.js +95 -0
- package/src/webmcp/tools/reshape.js +152 -0
- package/src/webmcp/tools/run-tools.js +309 -0
- package/src/webmcp/tools/runs.js +101 -0
- package/src/webmcp/tools/scaffold-tools.js +161 -0
- package/src/webmcp/tools/serial-tools.js +36 -0
- package/src/webmcp/tools/shared.js +338 -0
- package/src/webmcp/tools/subgraphs.js +360 -0
- package/src/webmcp/tools/tests.js +170 -0
- package/src/webmcp/tools/traffic-tools.js +70 -0
- package/src/webmcp/tools/transport-tools.js +65 -0
- package/src/webmcp/tools/version-tools.js +126 -0
- package/src/webmcp/tools/versions.js +11 -0
- package/src/webmcp/tools/wire-tools.js +90 -0
- package/src/webmcp/tools/workspace-tools.js +93 -0
- package/src/webmcp/tools.js +1323 -0
- package/src/wsh/connect.js +347 -0
- package/src/wsh/demo-host.js +361 -0
- package/src/wsh/e2e.js +27 -0
- package/src/wsh/keys.js +154 -0
- package/src/wsh/known-hosts.js +39 -0
- package/src/wsh/private-key.js +95 -0
- package/src/wsh/reverse.js +34 -0
- package/src/wsh/settings.js +118 -0
- package/src/wsh/transfer.js +62 -0
- package/src/wsh/vendor.js +5 -0
- package/src/x402/challenge.js +171 -0
- package/src/x402/demo-server.js +127 -0
- package/src/x402/pay.js +125 -0
- package/src/x402/retry.js +95 -0
- package/src/x402/store.js +76 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// Workspaces: named sets of panes on one canvas. One is on screen at a time;
|
|
2
|
+
// the others keep running, and wires stay global (a wire to a pane in another
|
|
3
|
+
// workspace carries its value as ever, it just isn't drawn). Pure data and
|
|
4
|
+
// functions (no DOM), shared by the canvas model, the tab strip on the page
|
|
5
|
+
// (src/ui/workspaces.js), the agent tool mallory_workspaces and the headless
|
|
6
|
+
// runner.
|
|
7
|
+
//
|
|
8
|
+
// Saved at the top level of a canvas, only when it has more than one workspace
|
|
9
|
+
// or a renamed one (a canvas with the one default workspace is saved as it
|
|
10
|
+
// always was):
|
|
11
|
+
//
|
|
12
|
+
// workspaces: [{ name, panes: [paneId, …], viewport? }, …] in tab order
|
|
13
|
+
// activeWorkspace: name
|
|
14
|
+
//
|
|
15
|
+
// Optional and additive: the format version stays 2, and a mallory that
|
|
16
|
+
// doesn't know workspaces shows every pane on one canvas. A pane the list
|
|
17
|
+
// doesn't name is in the active workspace. In the canvas, workspaces live in
|
|
18
|
+
// window-algebra's state (workspace/create, activate, rename, reorder, remove
|
|
19
|
+
// and window/move-to-workspace), so making one, switching, renaming, moving
|
|
20
|
+
// panes and removing one are undo steps like moves (src/model/canvas.js).
|
|
21
|
+
// Workspaces belong to the top-level canvas: a subgraph's inner canvas keeps the one default workspace.
|
|
22
|
+
|
|
23
|
+
import { loadGroups } from "./groups.js";
|
|
24
|
+
|
|
25
|
+
/** The workspace every canvas starts with. */
|
|
26
|
+
export const DEFAULT_WORKSPACE = "main";
|
|
27
|
+
|
|
28
|
+
const NAME_MAX = 60;
|
|
29
|
+
|
|
30
|
+
/** A name as it is kept: trimmed, inner whitespace collapsed, at most 60 characters. "" for anything that isn't a name. */
|
|
31
|
+
export function cleanName(name) {
|
|
32
|
+
if (typeof name !== "string") return "";
|
|
33
|
+
return name.replace(/\s+/g, " ").trim().slice(0, NAME_MAX).trim();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** `base`, or `base 2`, `base 3`… when `taken` (a Set or a list of names) already has it. */
|
|
37
|
+
export function freeName(base, taken) {
|
|
38
|
+
const used = new Set(taken);
|
|
39
|
+
const stem = cleanName(base) || "workspace";
|
|
40
|
+
if (!used.has(stem)) return stem;
|
|
41
|
+
for (let n = 2; ; n++) {
|
|
42
|
+
const name = `${stem.slice(0, NAME_MAX - String(n).length - 1)} ${n}`;
|
|
43
|
+
if (!used.has(name)) return name;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const finite = (n) => typeof n === "number" && Number.isFinite(n);
|
|
48
|
+
|
|
49
|
+
/** A saved view of a workspace ({ x, y, zoom }), or undefined when it isn't one. */
|
|
50
|
+
export function cleanViewport(v) {
|
|
51
|
+
return v && typeof v === "object" && finite(v.x) && finite(v.y) && finite(v.zoom) && v.zoom > 0 ? { x: v.x, y: v.y, zoom: v.zoom } : undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Throws a readable error unless `doc`'s workspaces (optional) are a list of well-formed, uniquely named ones. */
|
|
55
|
+
export function validateWorkspaces(doc) {
|
|
56
|
+
const { workspaces, activeWorkspace } = doc ?? {};
|
|
57
|
+
if (workspaces === undefined && activeWorkspace === undefined) return;
|
|
58
|
+
if (workspaces === undefined) throw new Error("That canvas names an active workspace but has no workspaces.");
|
|
59
|
+
if (!Array.isArray(workspaces) || !workspaces.length) throw new Error("The workspaces in that canvas aren't a list with at least one workspace.");
|
|
60
|
+
const seen = new Set();
|
|
61
|
+
for (const w of workspaces) {
|
|
62
|
+
const ok = w && typeof w === "object" && typeof w.name === "string" && cleanName(w.name) === w.name && w.name && (w.panes === undefined || (Array.isArray(w.panes) && w.panes.every((id) => typeof id === "string"))) && (w.viewport === undefined || Boolean(cleanViewport(w.viewport)));
|
|
63
|
+
if (!ok) throw new Error("A workspace in that canvas is malformed.");
|
|
64
|
+
if (seen.has(w.name)) throw new Error(`That canvas has two workspaces called "${w.name}".`);
|
|
65
|
+
seen.add(w.name);
|
|
66
|
+
}
|
|
67
|
+
if (activeWorkspace !== undefined && !seen.has(activeWorkspace)) throw new Error("That canvas's active workspace isn't one of its workspaces.");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The workspaces a canvas opens with, from its doc and the ids of its panes:
|
|
72
|
+
* `{ list: [{ name, panes, viewport? }], active }`. Always at least one. A pane
|
|
73
|
+
* belongs to the first workspace that names it, a pane none names to the active
|
|
74
|
+
* one, and an id that is no pane is dropped. A hand-edited file's duplicate or
|
|
75
|
+
* unusable names are repaired, not refused.
|
|
76
|
+
*/
|
|
77
|
+
export function loadWorkspaces(doc, ids) {
|
|
78
|
+
const order = [...ids];
|
|
79
|
+
const present = new Set(order);
|
|
80
|
+
const list = [];
|
|
81
|
+
const names = new Set();
|
|
82
|
+
for (const raw of Array.isArray(doc?.workspaces) ? doc.workspaces : []) {
|
|
83
|
+
const name = cleanName(raw?.name);
|
|
84
|
+
if (!name || names.has(name)) continue;
|
|
85
|
+
names.add(name);
|
|
86
|
+
const viewport = cleanViewport(raw.viewport);
|
|
87
|
+
list.push({ name, panes: [], ...(viewport ? { viewport } : {}), listed: Array.isArray(raw.panes) ? raw.panes : [] });
|
|
88
|
+
}
|
|
89
|
+
if (!list.length) list.push({ name: DEFAULT_WORKSPACE, panes: [], listed: [] });
|
|
90
|
+
const wanted = cleanName(doc?.activeWorkspace);
|
|
91
|
+
const active = names.has(wanted) ? wanted : list[0].name;
|
|
92
|
+
const owner = new Map();
|
|
93
|
+
for (const w of list) for (const id of w.listed) if (present.has(id) && !owner.has(id)) owner.set(id, w.name);
|
|
94
|
+
for (const id of order) {
|
|
95
|
+
const w = list.find((x) => x.name === (owner.get(id) ?? active));
|
|
96
|
+
w.panes.push(id);
|
|
97
|
+
}
|
|
98
|
+
return { list: list.map(({ listed: _listed, ...w }) => w), active };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Is it worth writing `workspaces` into a saved canvas? Not for the one default workspace. */
|
|
102
|
+
export const worthSaving = (list) => list.length > 1 || list[0]?.name !== DEFAULT_WORKSPACE;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The group lists to put in each workspace's layout: a group lives in the
|
|
106
|
+
* workspace of its first pane, holding only the panes that are there (a group
|
|
107
|
+
* never spans workspaces). `groups` are loaded ones; `where` maps a pane id to its workspace name.
|
|
108
|
+
* Returns a Map name -> groups.
|
|
109
|
+
*/
|
|
110
|
+
export function groupsByWorkspace(groups, where) {
|
|
111
|
+
const out = new Map();
|
|
112
|
+
for (const g of groups) {
|
|
113
|
+
const home = g.panes.map((id) => where.get(id)).find(Boolean);
|
|
114
|
+
if (!home) continue;
|
|
115
|
+
const panes = g.panes.filter((id) => where.get(id) === home);
|
|
116
|
+
if (!panes.length) continue;
|
|
117
|
+
out.set(home, [...(out.get(home) ?? []), panes.length === g.panes.length ? g : { ...g, panes }]);
|
|
118
|
+
}
|
|
119
|
+
// Cutting a group down can make it equal to another in its workspace: the nesting rules decide again, per workspace.
|
|
120
|
+
for (const [name, list] of out) out.set(name, loadGroups(list));
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// The ws-client pane's settings (prism parity wave P-F; src/panes/ws-client.js, docs/design/traffic.md "Wave P-F"). Pure: no DOM, no network.
|
|
2
|
+
//
|
|
3
|
+
// Two modes:
|
|
4
|
+
// - "client": the page (or `mallory run`) opens ONE WebSocket to `ws.url`, through the canvas's network policy (src/network/sockets.js), and the pane
|
|
5
|
+
// shows its frames and sends the ones you type. It connects on ▶ (or on every first run with `ws.auto`, which only the person sets), never because a
|
|
6
|
+
// canvas opened.
|
|
7
|
+
// - "proxy": under `mallory serve`, a client of the served canvas connects to `/<route>/<ws.proxy.path>` and the server dials ONE fixed upstream for it
|
|
8
|
+
// and relays the frames both ways, logging them (src/headless/ws-proxy.js). Anywhere else it is a declaration: nothing listens.
|
|
9
|
+
//
|
|
10
|
+
// Person-only (src/panes/ws-client.js `personOnly`): where a socket goes (`url`, `protocols`, every `proxy` setting), whether it connects by itself
|
|
11
|
+
// (`auto`) and whether the served log keeps frame text (`proxy.bodies`). An agent can read the pane and its frames; it cannot point it anywhere.
|
|
12
|
+
// A saved pane holds no credential: the upstream URL has no user:password and no query, and a header that would carry a key must be a secret placeholder.
|
|
13
|
+
import { parseUpstream, proxyHeaderProblem, validPrefix, keyShaped, isPlaceholderValue } from "./proxy.js";
|
|
14
|
+
import { FRAME_DEFAULTS, FRAME_LIMITS } from "./ws-frames.js";
|
|
15
|
+
import { SOCKET_LIMITS, cleanProtocols, socketTarget } from "../network/sockets.js";
|
|
16
|
+
import { DEMO_URL, isDemoUrl } from "./ws-demo.js";
|
|
17
|
+
|
|
18
|
+
export { DEMO_URL };
|
|
19
|
+
export const WS_MODES = ["client", "proxy"];
|
|
20
|
+
export const PROXY_LIMITS = Object.freeze({ connections: [1, 8], perAddress: [1, 8], frameBytes: [1024, 1048576], framesPerSecond: [1, 200], idleMs: [1000, 3600000], keep: [1, 500], headers: 20 });
|
|
21
|
+
export const PROXY_DEFAULTS = Object.freeze({ path: "", upstream: "", headers: [], connections: 4, perAddress: 2, frameBytes: 262144, framesPerSecond: 50, idleMs: 300000, keep: 100, bodies: false });
|
|
22
|
+
export const WS_DEFAULTS = Object.freeze({ mode: "client", url: "", protocols: [], keep: FRAME_DEFAULTS.keep, bytes: FRAME_DEFAULTS.bytes, auto: false });
|
|
23
|
+
|
|
24
|
+
const isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
25
|
+
const int = (v, [low, high], d) => (Number.isInteger(v) && v >= low && v <= high ? v : d);
|
|
26
|
+
const pairs = (list) => (Array.isArray(list) ? list.filter(isObject).map((x) => ({ name: String(x.name ?? ""), value: String(x.value ?? "") })) : []);
|
|
27
|
+
/** Headers the handshake sets itself, which a proxy's settings may not. */
|
|
28
|
+
const HANDSHAKE = ["sec-websocket-key", "sec-websocket-version", "sec-websocket-extensions", "sec-websocket-protocol", "sec-websocket-accept", "origin", "upgrade", "connection"];
|
|
29
|
+
|
|
30
|
+
/** The proxy settings as used: unknown keys dropped, defaults filled in, a number out of range falls back. */
|
|
31
|
+
export function cleanProxySocket(raw) {
|
|
32
|
+
const r = isObject(raw) ? raw : {};
|
|
33
|
+
return {
|
|
34
|
+
path: typeof r.path === "string" ? r.path.trim().replace(/^\/+|\/+$/g, "") : "",
|
|
35
|
+
upstream: typeof r.upstream === "string" ? r.upstream.trim() : "",
|
|
36
|
+
headers: pairs(r.headers),
|
|
37
|
+
connections: int(r.connections, PROXY_LIMITS.connections, PROXY_DEFAULTS.connections),
|
|
38
|
+
perAddress: int(r.perAddress, PROXY_LIMITS.perAddress, PROXY_DEFAULTS.perAddress),
|
|
39
|
+
frameBytes: int(r.frameBytes, PROXY_LIMITS.frameBytes, PROXY_DEFAULTS.frameBytes),
|
|
40
|
+
framesPerSecond: int(r.framesPerSecond, PROXY_LIMITS.framesPerSecond, PROXY_DEFAULTS.framesPerSecond),
|
|
41
|
+
idleMs: int(r.idleMs, PROXY_LIMITS.idleMs, PROXY_DEFAULTS.idleMs),
|
|
42
|
+
keep: int(r.keep, PROXY_LIMITS.keep, PROXY_DEFAULTS.keep),
|
|
43
|
+
bodies: r.bodies === true,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The settings as they are used (the whole `ws` object). */
|
|
48
|
+
export function cleanWs(raw) {
|
|
49
|
+
const r = isObject(raw) ? raw : {};
|
|
50
|
+
let protocols = [];
|
|
51
|
+
try {
|
|
52
|
+
protocols = cleanProtocols(r.protocols);
|
|
53
|
+
} catch {
|
|
54
|
+
protocols = [];
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
mode: WS_MODES.includes(r.mode) ? r.mode : WS_DEFAULTS.mode,
|
|
58
|
+
url: typeof r.url === "string" ? r.url.trim() : "",
|
|
59
|
+
protocols,
|
|
60
|
+
keep: int(r.keep, FRAME_LIMITS.keep, WS_DEFAULTS.keep),
|
|
61
|
+
bytes: int(r.bytes, FRAME_LIMITS.bytes, WS_DEFAULTS.bytes),
|
|
62
|
+
auto: r.auto === true,
|
|
63
|
+
proxy: cleanProxySocket(r.proxy),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const defaultWs = () => ({ mode: "client", url: "", protocols: [], keep: WS_DEFAULTS.keep, bytes: WS_DEFAULTS.bytes, auto: false });
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The fixed upstream of a proxy as `{ ok: true, base, href }` (`href`: the ws: or wss: URL the server dials; `base`: parseUpstream's shape, on the
|
|
71
|
+
* http(s) twin) or `{ ok: false, why }`. Same rules as an http proxy's upstream: no user:password, no query or fragment, no dot segment or encoded
|
|
72
|
+
* slash, nothing credential-shaped; only ws: and wss:.
|
|
73
|
+
*/
|
|
74
|
+
export function parseSocketUpstream(text) {
|
|
75
|
+
const raw = String(text ?? "").trim();
|
|
76
|
+
if (!raw) return { ok: false, why: "an upstream is a base URL like wss://stream.example.com/feed" };
|
|
77
|
+
if (!/^wss?:\/\//i.test(raw)) return { ok: false, why: "a WebSocket upstream is a ws: or wss: URL" };
|
|
78
|
+
if (/%(?:2e|2f|5c)/i.test(raw.replace(/^wss?:\/\/[^/]*/i, ""))) return { ok: false, why: "an upstream path holds an encoded dot or slash" };
|
|
79
|
+
const twin = parseUpstream(raw.replace(/^ws/i, "http"));
|
|
80
|
+
if (!twin.ok) return { ok: false, why: twin.why.replace(/http\(s\)|http or https/g, "ws or wss") };
|
|
81
|
+
const { base } = twin;
|
|
82
|
+
return { ok: true, base, href: `${base.protocol === "https:" ? "wss:" : "ws:"}//${base.host}${base.path}` || raw };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** What is wrong with these settings, or "". They are saved in the canvas, so a credential in them is refused. */
|
|
86
|
+
export function wsProblem(raw) {
|
|
87
|
+
if (raw === undefined || raw === null) return "";
|
|
88
|
+
if (!isObject(raw)) return "ws must be an object";
|
|
89
|
+
const allowed = new Set(["mode", "url", "protocols", "keep", "bytes", "auto", "proxy"]);
|
|
90
|
+
for (const key of Object.keys(raw)) if (!allowed.has(key)) return `ws has no setting "${key}"`;
|
|
91
|
+
if (raw.mode !== undefined && !WS_MODES.includes(raw.mode)) return `ws.mode must be ${WS_MODES.map((m) => `"${m}"`).join(" or ")}`;
|
|
92
|
+
for (const key of ["keep", "bytes"]) {
|
|
93
|
+
if (raw[key] !== undefined && !(Number.isInteger(raw[key]) && raw[key] >= FRAME_LIMITS[key][0] && raw[key] <= FRAME_LIMITS[key][1])) return `ws.${key} must be a whole number from ${FRAME_LIMITS[key][0]} to ${FRAME_LIMITS[key][1]}`;
|
|
94
|
+
}
|
|
95
|
+
if (raw.auto !== undefined && typeof raw.auto !== "boolean") return "ws.auto must be true or false";
|
|
96
|
+
if (raw.url !== undefined && typeof raw.url !== "string") return "ws.url must be text";
|
|
97
|
+
if (typeof raw.url === "string" && raw.url.trim() && !isDemoUrl(raw.url.trim())) {
|
|
98
|
+
const t = socketTarget(raw.url.trim());
|
|
99
|
+
if (!t.ok) return `ws.url: ${t.why}`;
|
|
100
|
+
// A credential is not saved in the canvas: no user:password (socketTarget), no key-shaped query value.
|
|
101
|
+
const q = new URL(t.href).searchParams;
|
|
102
|
+
for (const [name, value] of q) if (value && !isPlaceholderValue(value) && (/(token|key|secret|password|auth|sig)/i.test(name) || keyShaped(value))) return `ws.url: the query value of "${name}" looks like a credential (by its name or its shape): use a secret placeholder (mallory-secret:NAME)`;
|
|
103
|
+
}
|
|
104
|
+
if (raw.protocols !== undefined) {
|
|
105
|
+
try {
|
|
106
|
+
cleanProtocols(raw.protocols);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
return `ws.protocols: ${String(error.message).replace(/^websocket: /, "")}`;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (raw.proxy !== undefined) {
|
|
112
|
+
if (!isObject(raw.proxy)) return "ws.proxy must be an object";
|
|
113
|
+
const proxyAllowed = new Set(["path", "upstream", "headers", "connections", "perAddress", "frameBytes", "framesPerSecond", "idleMs", "keep", "bodies"]);
|
|
114
|
+
for (const key of Object.keys(raw.proxy)) if (!proxyAllowed.has(key)) return `ws.proxy has no setting "${key}"`;
|
|
115
|
+
for (const key of ["connections", "perAddress", "frameBytes", "framesPerSecond", "idleMs", "keep"]) {
|
|
116
|
+
const v = raw.proxy[key];
|
|
117
|
+
if (v !== undefined && !(Number.isInteger(v) && v >= PROXY_LIMITS[key][0] && v <= PROXY_LIMITS[key][1])) return `ws.proxy.${key} must be a whole number from ${PROXY_LIMITS[key][0]} to ${PROXY_LIMITS[key][1]}`;
|
|
118
|
+
}
|
|
119
|
+
if (raw.proxy.bodies !== undefined && typeof raw.proxy.bodies !== "boolean") return "ws.proxy.bodies must be true or false";
|
|
120
|
+
if (raw.proxy.path !== undefined && typeof raw.proxy.path !== "string") return "ws.proxy.path must be text";
|
|
121
|
+
if (raw.proxy.upstream !== undefined && typeof raw.proxy.upstream !== "string") return "ws.proxy.upstream must be text";
|
|
122
|
+
if (raw.proxy.headers !== undefined && !Array.isArray(raw.proxy.headers)) return "ws.proxy.headers must be a list";
|
|
123
|
+
const p = cleanProxySocket(raw.proxy);
|
|
124
|
+
if (p.path && !validPrefix(p.path)) return "ws.proxy.path is path segments of letters, digits and . _ ~ - (like live/feed)";
|
|
125
|
+
if (p.upstream) {
|
|
126
|
+
const up = parseSocketUpstream(p.upstream);
|
|
127
|
+
if (!up.ok) return `ws.proxy.upstream: ${up.why}`;
|
|
128
|
+
}
|
|
129
|
+
if (p.headers.length > PROXY_LIMITS.headers) return `ws.proxy.headers has at most ${PROXY_LIMITS.headers} entries`;
|
|
130
|
+
for (const { name, value } of p.headers) {
|
|
131
|
+
const bad = proxyHeaderProblem(name, value, HANDSHAKE);
|
|
132
|
+
if (bad) return bad.replace(/^proxy header/, "ws.proxy header");
|
|
133
|
+
}
|
|
134
|
+
if (p.frameBytes > SOCKET_LIMITS.frameBytes) return "ws.proxy.frameBytes is too large";
|
|
135
|
+
}
|
|
136
|
+
return "";
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The pane's one-line description (the Markdown format and the agent's status). */
|
|
140
|
+
export function describeWs(raw) {
|
|
141
|
+
const s = cleanWs(raw);
|
|
142
|
+
if (s.mode === "proxy") return s.proxy.upstream ? `a WebSocket proxy at /${s.proxy.path || "…"} to ${hostLabel(s.proxy.upstream)}, served only under mallory serve` : "a WebSocket proxy (set its upstream)";
|
|
143
|
+
if (isDemoUrl(s.url)) return "a WebSocket client on the demo echo (it lives in this page); it connects when you press ▶";
|
|
144
|
+
return s.url ? `a WebSocket client for ${hostLabel(s.url)}; it connects when you press ▶` : "a WebSocket client (set its URL)";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** "host:port/path" of a URL, without a query (which may hold a placeholder's name) or credentials. */
|
|
148
|
+
export function hostLabel(url) {
|
|
149
|
+
try {
|
|
150
|
+
const u = new URL(url);
|
|
151
|
+
return `${u.host}${u.pathname === "/" ? "" : u.pathname}`;
|
|
152
|
+
} catch {
|
|
153
|
+
return "an invalid URL";
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// The demo echo of the ws-client pane (prism's "Connect demo", WSK-04): a socket that lives in this page and goes nowhere. A pane whose URL is
|
|
2
|
+
// `demo:echo` is connected to it by the person's play, like any socket, but nothing is dialled, no network rule is asked and no tap entry is
|
|
3
|
+
// made: there is no network to reach. It greets, echoes every message back (`{ echo, n }`; bytes come back as bytes), and closes when it is sent
|
|
4
|
+
// the text `close`. It exists so a canvas (an example, a lesson, a test) can show the table and the send box with no server.
|
|
5
|
+
export const DEMO_URL = "demo:echo";
|
|
6
|
+
export const isDemoUrl = (url) => url === DEMO_URL;
|
|
7
|
+
|
|
8
|
+
/** @returns {{ protocol: string, send(data: string | Uint8Array): void, close(code?: number, reason?: string): void }} */
|
|
9
|
+
export function demoSocket({ onmessage, onclose }) {
|
|
10
|
+
let closed = false;
|
|
11
|
+
let n = 0;
|
|
12
|
+
const later = (fn) => queueMicrotask(() => !closed && fn());
|
|
13
|
+
later(() => onmessage(JSON.stringify({ demo: true, hello: "this is the demo echo: it lives in this page and nothing leaves it" })));
|
|
14
|
+
return {
|
|
15
|
+
protocol: "",
|
|
16
|
+
send(data) {
|
|
17
|
+
if (closed) throw new Error("websocket: the socket is closed");
|
|
18
|
+
if (data === "close") {
|
|
19
|
+
later(() => {
|
|
20
|
+
closed = true;
|
|
21
|
+
onclose({ code: 1000, reason: "the demo closed" });
|
|
22
|
+
});
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
later(() => onmessage(typeof data === "string" ? JSON.stringify({ echo: data, n: ++n }) : data.slice().buffer));
|
|
26
|
+
},
|
|
27
|
+
close() {
|
|
28
|
+
closed = true;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// WebSocket frames as the ws-client pane and the traffic list hold them (prism parity wave P-F; docs/design/traffic.md "Wave P-F"). Pure: no
|
|
2
|
+
// DOM, no network, no clock but what the caller passes. A frame is one message: a direction, a time, a size and a preview (its text, or the
|
|
3
|
+
// first bytes of a binary message). Everything is bounded: how many frames a log keeps, how many characters of text they hold together, and how
|
|
4
|
+
// long one preview is.
|
|
5
|
+
//
|
|
6
|
+
// Two kinds of record come out of here, and they are not the same thing:
|
|
7
|
+
// - the pane's own frames (`createFrameLog`): the raw preview, as the http pane's value holds a response. The pane is the person's table; a
|
|
8
|
+
// value can hold what the socket sent, including a secret an upstream echoes, so it is never written to a log or to history;
|
|
9
|
+
// - a LOGGED frame (`logFrame`, `redactFrames`): text through the shared redaction policy (credential-named JSON and form fields, key shapes)
|
|
10
|
+
// and nothing of a binary message but its size. The tap, the served log, history and every export hold only these.
|
|
11
|
+
import { SECRET_SHAPES, redactBody } from "../security/redact.js";
|
|
12
|
+
|
|
13
|
+
export const FRAME_LIMITS = Object.freeze({ keep: [10, 1000], bytes: [4096, 4194304], preview: 4096, logPreview: 1024, hex: 32, logKeep: 200, logChars: 262144 });
|
|
14
|
+
export const FRAME_DEFAULTS = Object.freeze({ keep: 200, bytes: 1048576 });
|
|
15
|
+
|
|
16
|
+
const SHAPES = SECRET_SHAPES.map((r) => ({ re: new RegExp(r.pattern, r.flags), replacement: r.replacement }));
|
|
17
|
+
const enc = new TextEncoder();
|
|
18
|
+
const dec = new TextDecoder("utf-8", { fatal: false });
|
|
19
|
+
|
|
20
|
+
const bytesOf = (data) => (data instanceof ArrayBuffer ? new Uint8Array(data) : ArrayBuffer.isView(data) ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength) : undefined);
|
|
21
|
+
const hexOf = (bytes) => [...bytes.subarray(0, FRAME_LIMITS.hex)].map((b) => b.toString(16).padStart(2, "0")).join(" ");
|
|
22
|
+
|
|
23
|
+
/** Text for a log: credential-named fields and key shapes taken out. Idempotent. */
|
|
24
|
+
export function redactFrameText(text) {
|
|
25
|
+
let out = redactBody(String(text ?? ""), "");
|
|
26
|
+
for (const { re, replacement } of SHAPES) out = out.replace(re, replacement);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The raw frame record for a message (text or bytes): `{ n, dir, at, bytes, binary, text | hex, truncated }`. */
|
|
31
|
+
export function frameRecord(n, dir, data, at) {
|
|
32
|
+
const bytes = bytesOf(data);
|
|
33
|
+
if (bytes) return { n, dir, at, bytes: bytes.byteLength, binary: true, hex: hexOf(bytes), truncated: bytes.byteLength > FRAME_LIMITS.hex };
|
|
34
|
+
const text = String(data ?? "");
|
|
35
|
+
const size = enc.encode(text).byteLength;
|
|
36
|
+
return { n, dir, at, bytes: size, binary: false, text: text.slice(0, FRAME_LIMITS.preview), truncated: text.length > FRAME_LIMITS.preview };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The same message as a LOGGED frame: redacted text (shorter preview), or only a binary message's size. With `text: false` (the person has not
|
|
41
|
+
* turned frame text on, as bodies are for the http tap) a text message is only its size too, marked `noText`.
|
|
42
|
+
*/
|
|
43
|
+
export function logFrame(n, dir, data, at, { text: keepText = true } = {}) {
|
|
44
|
+
const raw = frameRecord(n, dir, data, at);
|
|
45
|
+
if (raw.binary) return { n, dir, at, bytes: raw.bytes, binary: true };
|
|
46
|
+
if (!keepText) return { n, dir, at, bytes: raw.bytes, binary: false, noText: true };
|
|
47
|
+
const text = redactFrameText(raw.text);
|
|
48
|
+
const cut = text.length > FRAME_LIMITS.logPreview;
|
|
49
|
+
return { n, dir, at, bytes: raw.bytes, binary: false, text: cut ? text.slice(0, FRAME_LIMITS.logPreview) : text, truncated: raw.truncated || cut };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Frames read from anywhere (a tap entry, a served log, a pasted HAR) made into logged frames: shape-checked, text redacted again, a binary
|
|
54
|
+
* frame's preview dropped, at most `logKeep` of them and `logChars` characters together (the newest are kept).
|
|
55
|
+
*/
|
|
56
|
+
export function redactFrames(list, budget) {
|
|
57
|
+
if (!Array.isArray(list)) return undefined;
|
|
58
|
+
const out = [];
|
|
59
|
+
const start = budget ? Math.min(FRAME_LIMITS.logChars, budget.left) : FRAME_LIMITS.logChars;
|
|
60
|
+
let left = start;
|
|
61
|
+
for (const f of list.slice(-FRAME_LIMITS.logKeep)) {
|
|
62
|
+
if (!f || typeof f !== "object" || (f.dir !== "in" && f.dir !== "out")) continue;
|
|
63
|
+
const bytes = Number.isFinite(f.bytes) && f.bytes >= 0 ? f.bytes : 0;
|
|
64
|
+
const at = typeof f.at === "number" || typeof f.at === "string" ? f.at : null;
|
|
65
|
+
const n = Number.isInteger(f.n) ? f.n : out.length + 1;
|
|
66
|
+
if (f.noText === true && f.binary !== true) {
|
|
67
|
+
out.push({ n, dir: f.dir, at, bytes, binary: false, noText: true });
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (f.binary === true || typeof f.text !== "string") {
|
|
71
|
+
out.push({ n, dir: f.dir, at, bytes, binary: true });
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
let text = redactFrameText(f.text);
|
|
75
|
+
let truncated = f.truncated === true;
|
|
76
|
+
const room = Math.max(0, Math.min(FRAME_LIMITS.logPreview, left));
|
|
77
|
+
if (text.length > room) {
|
|
78
|
+
text = text.slice(0, room);
|
|
79
|
+
truncated = true;
|
|
80
|
+
}
|
|
81
|
+
left -= text.length;
|
|
82
|
+
out.push({ n, dir: f.dir, at, bytes, binary: false, text, truncated });
|
|
83
|
+
}
|
|
84
|
+
if (budget) budget.left = Math.max(0, budget.left - (start - left));
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A bounded log of the pane's own raw frames. `add(dir, data, at)` keeps the newest `keep`, and drops the oldest while the text they hold passes
|
|
90
|
+
* `bytes`. `snapshot()` gives `{ frames, total, dropped, framesIn, framesOut, bytesIn, bytesOut }`: the counts are of every frame, not only the kept.
|
|
91
|
+
*/
|
|
92
|
+
export function createFrameLog({ keep = FRAME_DEFAULTS.keep, bytes = FRAME_DEFAULTS.bytes } = {}) {
|
|
93
|
+
let frames = [];
|
|
94
|
+
let held = 0;
|
|
95
|
+
const stats = { total: 0, dropped: 0, framesIn: 0, framesOut: 0, bytesIn: 0, bytesOut: 0 };
|
|
96
|
+
return {
|
|
97
|
+
add(dir, data, at = Date.now()) {
|
|
98
|
+
const record = frameRecord(++stats.total, dir, data, at);
|
|
99
|
+
if (dir === "in") {
|
|
100
|
+
stats.framesIn++;
|
|
101
|
+
stats.bytesIn += record.bytes;
|
|
102
|
+
} else {
|
|
103
|
+
stats.framesOut++;
|
|
104
|
+
stats.bytesOut += record.bytes;
|
|
105
|
+
}
|
|
106
|
+
frames.push(record);
|
|
107
|
+
held += (record.text?.length ?? 0) + 64;
|
|
108
|
+
while (frames.length > 1 && (frames.length > keep || held > bytes)) {
|
|
109
|
+
const gone = frames.shift();
|
|
110
|
+
held -= (gone.text?.length ?? 0) + 64;
|
|
111
|
+
stats.dropped++;
|
|
112
|
+
}
|
|
113
|
+
return record;
|
|
114
|
+
},
|
|
115
|
+
snapshot: () => ({ frames: frames.map((f) => ({ ...f })), ...stats }),
|
|
116
|
+
clear() {
|
|
117
|
+
frames = [];
|
|
118
|
+
held = 0;
|
|
119
|
+
},
|
|
120
|
+
get size() {
|
|
121
|
+
return frames.length;
|
|
122
|
+
},
|
|
123
|
+
/** Change the bounds (a setting edit); the log is cut to them at once. */
|
|
124
|
+
resize(next) {
|
|
125
|
+
keep = next.keep ?? keep;
|
|
126
|
+
bytes = next.bytes ?? bytes;
|
|
127
|
+
while (frames.length > 1 && (frames.length > keep || held > bytes)) {
|
|
128
|
+
held -= (frames.shift().text?.length ?? 0) + 64;
|
|
129
|
+
stats.dropped++;
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A frame as one line for a table or a note: "← 12 B {"hello":…". */
|
|
136
|
+
export function frameLine(f) {
|
|
137
|
+
const arrow = f.dir === "in" ? "←" : "→";
|
|
138
|
+
const body = f.binary ? `binary ${f.hex ?? ""}`.trim() : f.noText ? "(text not kept)" : String(f.text ?? "").replace(/\s+/g, " ").slice(0, 80);
|
|
139
|
+
return `${arrow} ${f.bytes} B ${body}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
// A canvas's WebSocket client connections: one per ws-client pane (src/panes/ws-client.js), kept on the canvas as `canvas.wsHub` in this page's (or
|
|
2
|
+
// this process's) memory only. A pane's run connects only on an immediate run (▶, run all, or an agent's run of the pane, as for an http pane's send) or when `ws.auto` is on, and then only the first time; every connect is decided by the network policy first: opening,
|
|
3
|
+
// pasting, merging or reloading a canvas starts nothing, and a socket that closes is not reopened by itself.
|
|
4
|
+
//
|
|
5
|
+
// The socket is opened by `runtime.socket()` (src/runtime/runtime.js), which is the network policy (src/network/sockets.js): the same per-host
|
|
6
|
+
// decision as a fetch, the same local-network block, the tap, the frame limits. Frames go into a bounded log (src/model/ws-frames.js) and the pane
|
|
7
|
+
// is run again, a few times a second at most, so its value and its table follow. The log is raw (the pane is the person's table, as the http pane's
|
|
8
|
+
// response is); the tap and the served log hold the redacted copy. Nothing here is reachable from WebMCP.
|
|
9
|
+
import { cleanWs } from "./ws-client.js";
|
|
10
|
+
import { createFrameLog } from "./ws-frames.js";
|
|
11
|
+
import { demoSocket, isDemoUrl } from "./ws-demo.js";
|
|
12
|
+
|
|
13
|
+
/** How many of the newest frames the pane's VALUE carries (the table draws the whole log from the hub). */
|
|
14
|
+
export const VALUE_FRAMES = 50;
|
|
15
|
+
const REFRESH_MS = 100;
|
|
16
|
+
const message = (error) => String(error?.message ?? error).slice(0, 300);
|
|
17
|
+
|
|
18
|
+
/** The canvas's hub, made on first use. */
|
|
19
|
+
export function wsHubOf(canvas) {
|
|
20
|
+
canvas.wsHub ??= new WsHub(canvas);
|
|
21
|
+
return canvas.wsHub;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class WsHub {
|
|
25
|
+
constructor(canvas, { setTimer = (fn, ms) => setTimeout(fn, ms), clearTimer = (t) => clearTimeout(t) } = {}) {
|
|
26
|
+
this.canvas = canvas;
|
|
27
|
+
this.conns = new Map(); // pane id -> connection record
|
|
28
|
+
this.setTimer = setTimer;
|
|
29
|
+
this.clearTimer = clearTimer;
|
|
30
|
+
this.version = 0; // bumped on every change, so a body can tell
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#conn(id, settings) {
|
|
34
|
+
let c = this.conns.get(id);
|
|
35
|
+
if (!c) {
|
|
36
|
+
c = { status: "idle", key: "", link: null, log: createFrameLog({ keep: settings.keep, bytes: settings.bytes }), error: undefined, closed: undefined, openedAt: undefined, waiting: false, abort: null, timer: null, generation: 0, connectedBefore: false };
|
|
37
|
+
this.conns.set(id, c);
|
|
38
|
+
}
|
|
39
|
+
return c;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Does the pane's connection exist (a body asks before it draws a table)? */
|
|
43
|
+
has(id) {
|
|
44
|
+
return this.conns.has(id);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Bring the connection in step with the pane's settings (`pane.ws`) and say what a run should do: a different URL or subprotocol list closes the old
|
|
49
|
+
* socket and starts over from "idle"; the log's bounds follow the settings.
|
|
50
|
+
*/
|
|
51
|
+
sync(id, settings) {
|
|
52
|
+
const c = this.#conn(id, settings);
|
|
53
|
+
const key = JSON.stringify([settings.url, settings.protocols]);
|
|
54
|
+
if (c.key && c.key !== key) this.#reset(id, c);
|
|
55
|
+
c.key = key;
|
|
56
|
+
c.log.resize({ keep: settings.keep, bytes: settings.bytes });
|
|
57
|
+
return c;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
#reset(id, c) {
|
|
61
|
+
c.generation++;
|
|
62
|
+
c.abort?.abort();
|
|
63
|
+
c.abort = null;
|
|
64
|
+
try {
|
|
65
|
+
c.link?.close(1000, "settings changed");
|
|
66
|
+
} catch {}
|
|
67
|
+
Object.assign(c, { status: "idle", link: null, error: undefined, closed: undefined, openedAt: undefined, waiting: false, connectedBefore: false });
|
|
68
|
+
c.log.clear();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Open the socket for pane `id` through `runtime.socket`. Resolves once it is open or has failed (the failure is in the record, not thrown).
|
|
73
|
+
* `pane`: the sandbox id the policy names (the pane's runaway key), `signal`: the run's.
|
|
74
|
+
*/
|
|
75
|
+
async connect(id, { runtime, pane, signal }) {
|
|
76
|
+
const settings = cleanWs(this.canvas.panes.get(id)?.ws);
|
|
77
|
+
const c = this.sync(id, settings);
|
|
78
|
+
if (c.status === "open" || c.status === "connecting") return c;
|
|
79
|
+
const generation = ++c.generation;
|
|
80
|
+
const abort = new AbortController();
|
|
81
|
+
c.abort = abort;
|
|
82
|
+
const stop = () => abort.abort();
|
|
83
|
+
signal?.addEventListener?.("abort", stop, { once: true });
|
|
84
|
+
Object.assign(c, { status: "connecting", error: undefined, closed: undefined, waiting: false, connectedBefore: true });
|
|
85
|
+
c.log.clear();
|
|
86
|
+
this.#bump();
|
|
87
|
+
try {
|
|
88
|
+
// The demo echo lives in this page: no network rule applies because nothing is dialled (src/model/ws-demo.js).
|
|
89
|
+
const link = isDemoUrl(settings.url)
|
|
90
|
+
? demoSocket({
|
|
91
|
+
onmessage: (data) => {
|
|
92
|
+
if (c.generation !== generation) return;
|
|
93
|
+
c.log.add("in", data);
|
|
94
|
+
this.#changed(id);
|
|
95
|
+
},
|
|
96
|
+
onclose: (info) => {
|
|
97
|
+
if (c.generation !== generation) return;
|
|
98
|
+
Object.assign(c, { status: "closed", link: null, closed: { code: info.code, reason: info.reason } });
|
|
99
|
+
this.#changed(id, true);
|
|
100
|
+
},
|
|
101
|
+
})
|
|
102
|
+
: await runtime.socket(settings.url, { protocols: settings.protocols }, {
|
|
103
|
+
pane,
|
|
104
|
+
signal: abort.signal,
|
|
105
|
+
onmessage: (data) => {
|
|
106
|
+
if (c.generation !== generation) return;
|
|
107
|
+
c.log.add("in", data);
|
|
108
|
+
this.#changed(id);
|
|
109
|
+
},
|
|
110
|
+
onclose: (info) => {
|
|
111
|
+
if (c.generation !== generation) return;
|
|
112
|
+
Object.assign(c, { status: "closed", link: null, closed: { code: info.code, reason: info.reason } });
|
|
113
|
+
this.#changed(id, true);
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
if (c.generation !== generation || abort.signal.aborted) {
|
|
117
|
+
link.close(1000, "superseded");
|
|
118
|
+
return c;
|
|
119
|
+
}
|
|
120
|
+
Object.assign(c, { status: "open", link, openedAt: Date.now() });
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (c.generation !== generation) return c;
|
|
123
|
+
const kind = error?.fetchFailure ?? (error?.name === "AbortError" ? "stopped" : "error");
|
|
124
|
+
Object.assign(c, { status: "failed", link: null, error: { kind, message: message(error) }, waiting: kind === "refused" });
|
|
125
|
+
} finally {
|
|
126
|
+
signal?.removeEventListener?.("abort", stop);
|
|
127
|
+
}
|
|
128
|
+
this.#bump();
|
|
129
|
+
return c;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Send one message (text, or bytes) on pane `id`'s open socket; it is also a row in the pane's table. Throws if it can't go. */
|
|
133
|
+
send(id, data) {
|
|
134
|
+
const c = this.conns.get(id);
|
|
135
|
+
if (!c || c.status !== "open" || !c.link) throw new Error("the socket isn't open: press ▶ to connect");
|
|
136
|
+
c.link.send(data);
|
|
137
|
+
c.log.add("out", data);
|
|
138
|
+
this.#changed(id);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Close pane `id`'s socket (the person's Disconnect). */
|
|
142
|
+
disconnect(id) {
|
|
143
|
+
const c = this.conns.get(id);
|
|
144
|
+
if (!c) return;
|
|
145
|
+
c.generation++;
|
|
146
|
+
c.abort?.abort();
|
|
147
|
+
c.abort = null;
|
|
148
|
+
const link = c.link;
|
|
149
|
+
Object.assign(c, { status: "closed", link: null, closed: { code: 1000, reason: "closed here" }, waiting: false });
|
|
150
|
+
try {
|
|
151
|
+
link?.close(1000, "closed");
|
|
152
|
+
} catch {}
|
|
153
|
+
this.#changed(id, true);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Forget the table's frames (the socket stays open). */
|
|
157
|
+
clear(id) {
|
|
158
|
+
this.conns.get(id)?.log.clear();
|
|
159
|
+
this.#changed(id, true);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The pane pane `id` is gone: close and forget. */
|
|
163
|
+
forget(id) {
|
|
164
|
+
const c = this.conns.get(id);
|
|
165
|
+
if (!c) return;
|
|
166
|
+
c.generation++;
|
|
167
|
+
c.abort?.abort();
|
|
168
|
+
if (c.timer) this.clearTimer(c.timer);
|
|
169
|
+
try {
|
|
170
|
+
c.link?.close(1000, "closed");
|
|
171
|
+
} catch {}
|
|
172
|
+
this.conns.delete(id);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
dispose() {
|
|
176
|
+
for (const id of [...this.conns.keys()]) this.forget(id);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** How pane `id` stands, for its body: `{ status, url, frames, total, dropped, …, error?, closed?, waiting }`. Whole log. */
|
|
180
|
+
snapshot(id) {
|
|
181
|
+
const c = this.conns.get(id);
|
|
182
|
+
const s = cleanWs(this.canvas.panes.get(id)?.ws);
|
|
183
|
+
if (!c) return { status: "idle", url: s.url, frames: [], total: 0, dropped: 0, framesIn: 0, framesOut: 0, bytesIn: 0, bytesOut: 0, waiting: false };
|
|
184
|
+
return { status: c.status, url: s.url, ...c.log.snapshot(), ...(c.error ? { error: c.error } : {}), ...(c.closed ? { closed: c.closed } : {}), ...(c.openedAt ? { openedAt: c.openedAt } : {}), waiting: c.waiting };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** The pane's value: the state and the newest frames. It holds what the socket sent, raw (as the http pane's response does). */
|
|
188
|
+
value(id) {
|
|
189
|
+
const snap = this.snapshot(id);
|
|
190
|
+
const s = cleanWs(this.canvas.panes.get(id)?.ws);
|
|
191
|
+
const { frames, ...rest } = snap;
|
|
192
|
+
const shown = frames.slice(-VALUE_FRAMES);
|
|
193
|
+
const lastIn = [...frames].reverse().find((f) => f.dir === "in");
|
|
194
|
+
return { mode: "client", ...rest, protocols: s.protocols, frames: shown, ...(lastIn ? { last: lastIn } : {}) };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
#bump() {
|
|
198
|
+
this.version++;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Something changed: run the pane again soon (not as if ▶ were pressed), so its value follows. `now`: without the pause. */
|
|
202
|
+
#changed(id, now = false) {
|
|
203
|
+
this.#bump();
|
|
204
|
+
const c = this.conns.get(id);
|
|
205
|
+
if (!c) return;
|
|
206
|
+
const go = () => {
|
|
207
|
+
c.timer = null;
|
|
208
|
+
const canvas = this.canvas;
|
|
209
|
+
if (canvas.disposed || !canvas.attached?.has(id) || canvas.trusted?.value === false) return;
|
|
210
|
+
canvas.hint?.(`ws-client ${canvas.panes.get(id)?.name || id}`);
|
|
211
|
+
canvas.flow?.run(id);
|
|
212
|
+
};
|
|
213
|
+
if (c.timer) {
|
|
214
|
+
if (!now) return;
|
|
215
|
+
this.clearTimer(c.timer);
|
|
216
|
+
}
|
|
217
|
+
c.timer = this.setTimer(go, now ? 0 : REFRESH_MS);
|
|
218
|
+
}
|
|
219
|
+
}
|