devtools

package
v0.16.397 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package devtools is the DevTools record of the preview slots: what a slot's page did on the network and in its console, and the conditions it runs under (throttling, blocked addresses, cache). The proxy and the page's bridge write, the panel, the CLI and the MCP server read. Rings, not logs: the newest requests and console entries per slot.

Index

Constants

View Source
const (
	DefaultLimit   = 100
	MaxLimit       = 500
	MaxServerLines = 1000
)
View Source
const (
	MaxReportEvents = 100
	MaxReportBytes  = 256 * 1024

	MaxEventsPerWindow = 500
)
View Source
const (
	NetworkLimit = 1000
	ConsoleLimit = 500

	MaxBlocked = 50
	MaxPattern = 300
)
View Source
const NeighbourPrefix = "/__tk/p/"

NeighbourPrefix is the path of the main slot under which neighbour ports (MFE remotes) are served.

Variables

View Source
var Presets = map[string]Throttle{
	"none":    {"none", 0, 0, 0},
	"fast-4g": {"fast-4g", 165, 9000, 1500},
	"slow-4g": {"slow-4g", 563, 1600, 750},
	"3g":      {"3g", 2000, 400, 400},
	"offline": {"offline", 0, 0, 0},
}

Presets are Chrome DevTools' own, so a number seen here means the same as there.

Functions

func BlockedBy

func BlockedBy(url string, patterns []string) string

BlockedBy is the first pattern found anywhere in the full URL, case-insensitively; "" when none.

func IsOffline

func IsOffline(t Throttle) bool

func IsThrottled

func IsThrottled(t Throttle) bool

func JSNumber

func JSNumber(text string) float64

JSNumber is Number(text) for a query value.

func ParseBlocked

func ParseBlocked(v any) ([]string, bool)

ParseBlocked keeps the strings, trimmed, non-empty and not too long, once each, at most MaxBlocked; false for a value that is not a list.

func ParseServerLogQuery

func ParseServerLogQuery(q Query) (bool, int, string)

func ToTargetText

func ToTargetText(text, slot string, target *Target) string

ToTargetText turns every slot address in a text (a URL, a message, a stack trace) into the target's: "<slot>/__tk/p/5173/x" is the neighbour's "http://localhost:5173/x". A page's report speaks of the slot; people and agents know the target.

Types

type API

type API struct {
	Store *Store
	// CLI is the command people and agents type ("acme"), named in the messages.
	CLI string
	// ServerLog reads the dev server's terminal of a slot.
	ServerLog func(slot string, all bool, lines int) (ServerLog, error)
}

API is the agents' view of a store.

func (*API) ChangeSettings

func (a *API) ChangeSettings(asked string, body any) Outcome

ChangeSettings applies what the panel sends (throttle, blocked, disableCache) plus blockedAdd and blockedRemove, which edit the current list in one step instead of replacing it.

func (*API) ClearLogs

func (a *API) ClearLogs(asked string) Outcome

func (*API) Console

func (a *API) Console(asked string, q ConsoleQuery) Outcome

func (*API) Network

func (a *API) Network(asked string, q NetworkQuery) Outcome

func (*API) ServerLogOf

func (a *API) ServerLogOf(asked string, all bool, lines int) Outcome

func (*API) SettingsOf

func (a *API) SettingsOf(asked string) Outcome

func (*API) Slots

func (a *API) Slots() SlotsView

type BlockedChanges

type BlockedChanges struct {
	Added   []string `json:"added"`
	Removed []string `json:"removed"`
	Missing []string `json:"missing"`
}

type ClearedView

type ClearedView struct {
	Slot    string `json:"slot"`
	Cleared bool   `json:"cleared"`
}

type ConsoleEntry

type ConsoleEntry struct {
	At     int64   `json:"at"`
	Level  string  `json:"level"`
	Source string  `json:"source"`
	Text   string  `json:"text"`
	Stack  *string `json:"stack"`
	URL    *string `json:"url"`
	ID     int64   `json:"id"`
	Count  int64   `json:"count"`
}

type ConsoleQuery

type ConsoleQuery struct {
	Since int64
	Level string
	Limit int
}

func ParseConsoleQuery

func ParseConsoleQuery(q Query) (ConsoleQuery, string)

type ConsoleView

type ConsoleView struct {
	Slot    string  `json:"slot"`
	Target  *Target `json:"target"`
	Cursor  int64   `json:"cursor"`
	Matched int     `json:"matched"`
	Entries any     `json:"entries"`
}

type Listener

type Listener struct {
	Port    int
	Host    string
	PID     int
	Command string
	HasPID  bool
}

Listener is whoever listens on a port; PID 0 for another user's process, which ss does not name.

type LogLine

type LogLine struct {
	Text  string `json:"text"`
	Level string `json:"level"`
}

func LastLines

func LastLines(text string, limit int) []LogLine

LastLines is the last limit lines as they are, each with its level.

func ProblemLines

func ProblemLines(text string, limit int) []LogLine

ProblemLines is the last limit problems, each with its stack or code frame, and the successes between them, consecutive ones as one.

type LogSource

type LogSource struct {
	Kind    string
	Session string
	Pane    string
	Path    string
	Command string
}

func (LogSource) MarshalJSON

func (s LogSource) MarshalJSON() ([]byte, error)

type MCP

type MCP struct {
	API *API
	// contains filtered or unexported fields
}

MCP is the preview DevTools' tools of the agents' MCP server (internal/mcpserver).

func (*MCP) Call added in v0.15.40

func (m *MCP) Call(_ context.Context, name string, args *jsvalue.Object) *jsvalue.Object

func (*MCP) Has added in v0.15.40

func (m *MCP) Has(name string) bool

func (*MCP) Instructions added in v0.15.40

func (m *MCP) Instructions() string

func (*MCP) Tools added in v0.15.40

func (m *MCP) Tools() []any

type NetworkEntry

type NetworkEntry struct {
	At         int64   `json:"at"`
	Source     string  `json:"source"`
	Method     string  `json:"method"`
	URL        string  `json:"url"`
	Host       string  `json:"host"`
	Type       string  `json:"type"`
	Status     *int64  `json:"status"`
	Mime       *string `json:"mime"`
	Size       *int64  `json:"size"`
	DurationMs *int64  `json:"durationMs"`
	ThirdParty bool    `json:"thirdParty"`
	Blocked    bool    `json:"blocked"`
	Throttled  bool    `json:"throttled"`
	Error      *string `json:"error"`
	ID         int64   `json:"id"`
}

NetworkEntry is one request; Id grows by one per slot and is the cursor of `since`.

type NetworkQuery

type NetworkQuery struct {
	Since      int64
	Errors     bool
	ThirdParty bool
	URL        string
	Types      []string
	Limit      int
}

func ParseNetworkQuery

func ParseNetworkQuery(q Query) (NetworkQuery, string)

ParseNetworkQuery is the HTTP form: since, errors=1, thirdParty=1, url, type (comma-separated), limit.

type NetworkView

type NetworkView struct {
	Slot     string   `json:"slot"`
	Target   *Target  `json:"target"`
	Settings Settings `json:"settings"`
	Cursor   int64    `json:"cursor"`
	Matched  int      `json:"matched"`
	Entries  any      `json:"entries"`
}

type Outcome

type Outcome struct {
	Value  any
	Status int
	Error  string
}

Outcome is a value, or a refusal with its status and words.

func (Outcome) OK

func (o Outcome) OK() bool

type Pacer

type Pacer struct {
	OnSlice func(int)
	// contains filtered or unexported fields
}

Pacer passes a body on at the slot's rate, in slices of a quarter second at most. OnSlice sees every slice that left.

func (*Pacer) Write

func (p *Pacer) Write(b []byte) (int, error)

type Patch

type Patch struct {
	Throttle     *Throttle
	Blocked      []string // nil: unchanged
	DisableCache *bool
}

Patch is a validated partial change of a slot's settings; nil fields stay.

func ParseSettingsPatch

func ParseSettingsPatch(v any) (Patch, string)

ParseSettingsPatch validates a partial change; the error names the field in words.

type Query

type Query map[string][]string

Query is a handler's req.query: every value of a key, in order.

func (Query) Slot

func (q Query) Slot() string

Slot is the ?slot= an agent route names.

type Report

type Report struct {
	Network []NetworkEntry
	Console []ConsoleEntry
}

Report is a report's valid events.

func ParseReport

func ParseReport(v any, slot string, target *Target, now int64) *Report

ParseReport is nil when the body is not a report at all; otherwise its valid events, the first MaxReportEvents.

type ServerLog

type ServerLog struct {
	Source *LogSource `json:"source"`
	Lines  []LogLine  `json:"lines"`
	Reason *string    `json:"reason"`
}

type ServerLogView

type ServerLogView struct {
	Slot   string  `json:"slot"`
	Target *Target `json:"target"`
	ServerLog
}

type ServerLogs

type ServerLogs struct {
	Store    *Store
	Tmux     *tmux.Tmux
	Listener func(port int) (*Listener, error)
	// Panes and Capture default to the tmux servers': every pane by session, and a pane's text with its history.
	Panes   func() (map[string]tmux.PaneInfo, bool)
	Capture func(session string, lines int) (string, bool)
	// Outside names where a dev server should run for its output to be found (the platform's terminals).
	Outside  string
	ProcRoot string
	// contains filtered or unexported fields
}

ServerLogs reads and briefly remembers the dev server output of the slots' ports.

func (*ServerLogs) Get

func (l *ServerLogs) Get(slot string, all bool, lines int) (ServerLog, error)

Get is the slot's dev server output; lines 0 takes the default.

type Settings

type Settings struct {
	Throttle     Throttle `json:"throttle"`
	Blocked      []string `json:"blocked"`
	DisableCache bool     `json:"disableCache"`
}

func DefaultSettings

func DefaultSettings() Settings

type SettingsChangeView

type SettingsChangeView struct {
	Slot     string         `json:"slot"`
	Target   *Target        `json:"target"`
	Settings Settings       `json:"settings"`
	Changes  BlockedChanges `json:"changes"`
}

type SettingsView

type SettingsView struct {
	Slot     string   `json:"slot"`
	Target   *Target  `json:"target"`
	Settings Settings `json:"settings"`
}

type SlotSummary

type SlotSummary struct {
	Slot     string   `json:"slot"`
	Target   *Target  `json:"target"`
	LastSeen int64    `json:"lastSeen"`
	Settings Settings `json:"settings"`
	Requests int      `json:"requests"`
	Errors   int      `json:"errors"`
}

type SlotsView

type SlotsView struct {
	Slots []SlotSummary `json:"slots"`
}

type Snapshot

type Snapshot struct {
	Slot     string         `json:"slot"`
	Target   *Target        `json:"target"`
	Settings Settings       `json:"settings"`
	Network  []NetworkEntry `json:"network"`
	Console  []ConsoleEntry `json:"console"`
	Cursor   int64          `json:"cursor"`
}

type Store

type Store struct {
	Now func() time.Time

	// OnFailed, when set, is told every request that failed (blocked, an error, 4xx or 5xx) and OnError every console
	// error, as they are recorded (the machine's journal). They are called under the store's lock: they must
	// not block or call back into the store.
	OnFailed func(slot string, e NetworkEntry)
	OnError  func(slot string, e ConsoleEntry)
	// contains filtered or unexported fields
}

Store holds every slot's record of this process.

func NewStore

func NewStore() *Store

func (*Store) Clear

func (s *Store) Clear(slot string)

func (*Store) Finish

func (s *Store) Finish(slot string, id int64, patch func(*NetworkEntry))

Finish fills in the answer of a pending request. It moves to the end of the ring with a fresh id, so `since` readers see it again once it is finished.

func (*Store) List

func (s *Store) List() []SlotSummary

List is every slot, newest first.

func (*Store) NewPacer

func (s *Store) NewPacer(slot string, down bool, w io.Writer, done <-chan struct{}, onSlice func(int)) *Pacer

NewPacer writes to w at the slot's rate in that direction until done closes.

func (*Store) Read

func (s *Store) Read(slot string, since int64) Snapshot

func (*Store) RecordConsole

func (s *Store) RecordConsole(slot string, e ConsoleEntry)

RecordConsole adds a console entry; one identical to the last folds into it, as in Chrome.

func (*Store) RecordRequest

func (s *Store) RecordRequest(slot string, e NetworkEntry) int64

RecordRequest adds an entry and returns its id.

func (*Store) Resolve

func (s *Store) Resolve(asked string) string

Resolve is the slot the caller named, or the one the person used last: agents rarely know the slot's name.

func (*Store) SetSettings

func (s *Store) SetSettings(slot string, p Patch) Settings

SetSettings applies a validated patch.

func (*Store) Settings

func (s *Store) Settings(slot string) Settings

func (*Store) TakeQuota

func (s *Store) TakeQuota(slot string, wanted int, now int64) int

TakeQuota is how many of wanted events the slot may still record in this window: a page logging in a loop fills the rings with its last words, not the runtime's memory.

func (*Store) TargetOf

func (s *Store) TargetOf(slot string) *Target

TargetOf is what the slot shows now, as the proxy last named it; nil before its first request.

func (*Store) Touch

func (s *Store) Touch(slot string, target Target)

Touch: the proxy names the slot's current target with every request it admits.

type Target

type Target struct {
	Kind   string
	Port   int
	Origin string
}

Target is what a slot shows: a local port or an external origin.

func (Target) MarshalJSON

func (t Target) MarshalJSON() ([]byte, error)

func (Target) TargetOrigin

func (t Target) TargetOrigin() string

TargetOrigin is where the slot's page really lives.

type Throttle

type Throttle struct {
	Preset    string `json:"preset"`
	LatencyMs int64  `json:"latencyMs"`
	DownKbps  int64  `json:"downKbps"`
	UpKbps    int64  `json:"upKbps"`
}

func ParseThrottle

func ParseThrottle(v any) (Throttle, bool)

ParseThrottle: a preset fills in its numbers, custom keeps the given ones; false for anything else.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL