viewserver

package
v0.2.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package viewserver serves the embedded review viewer and one review document on a loopback address.

The server only ever serves the embedded app and the single document it was given; request paths never reach the file system. It never fetches anything. The one thing it computes from this machine is GET /api/sessions, the sessions linked to the document's pull request, through the function a host supplies in Options.Sessions, GET /api/sessions/{id}, the redacted transcript of one of them, GET /api/patch, the patch of one file the document cites, through Options.Patch, and GET /api/commit and GET /api/commit-patch, one linked commit's message, changed files and file patches, through Options.Commit and Options.CommitPatch, and GET /api/file, one file the document cites as it is at the head, through Options.File. Host checking, security headers and routing are fixed here; a host such as the daemon's app server extends it through Mux and Use.

Index

Constants

View Source
const MaxDocumentBytes = 32 << 20

MaxDocumentBytes is the largest document the server accepts, at start-up and when a watched file changes. Reviews are far smaller; the cap keeps a wrong file from filling memory.

Variables

View Source
var ErrNoVersion = errors.New("no such review version")

ErrNoVersion is the error a VersionsFunc returns for a version that does not exist.

Functions

func LoadDocument

func LoadDocument(path string) ([]byte, error)

LoadDocument reads the document at path. It must be valid JSON and no larger than MaxDocumentBytes.

func WithHunkURLs

func WithHunkURLs(document []byte) []byte

WithHunkURLs is the document as it is served: each indexed hunk without a github_url, as a locally stored document holds none, gets the one its generation would have recorded (review.DeriveHunkURLs). A document whose index cannot be read that way is served as given.

Types

type CommitFunc

type CommitFunc func(ctx context.Context, review Review, oid string) (locallink.CommitDetail, error)

CommitFunc reads one commit's message body and changed files from this machine, for GET /api/commit. It is only ever asked about a commit the sessions answer lists, and it should stop when ctx ends.

type CommitPatchFunc

type CommitPatchFunc func(ctx context.Context, review Review, commit locallink.CommitDetail, file locallink.CommitFile) (locallink.PatchResult, error)

CommitPatchFunc reads one file's patch in a commit, for GET /api/commit-patch. commit is CommitFunc's available answer and file one of its files.

type FileFunc

type FileFunc func(ctx context.Context, review Review, path string) (locallink.FileResult, error)

FileFunc reads one file at the document's head (review.Head) from this machine, for GET /api/file. It is only ever asked about a path the document cites, and it should stop when ctx ends.

type Options

type Options struct {
	// Path is the document file. It is read at New; with Watch it is polled
	// for changes. Exactly one of Path and Document must be set.
	Path string
	// Document is a fixed document, such as the sample. It cannot be watched.
	Document []byte
	// Watch reloads the viewer when the file at Path changes.
	Watch bool
	// Validate returns the findings for a document. They are printed to Warn
	// each time a document is loaded; the document is served either way.
	Validate func(document []byte) []string
	// Warn receives warnings. It defaults to discarding them.
	Warn io.Writer
	// Assets is the app to serve, with index.html at its root. It defaults to
	// the embedded viewer.
	Assets fs.FS
	// Sample marks the document as the built-in sample, which belongs to no
	// clone: /api/sessions then answers that sessions are unavailable.
	Sample bool
	// Sessions links the sessions on this machine to the document's pull
	// request. It is called on the first request for /api/sessions, never at
	// start-up, and its available answers are kept for at most a minute, or until
	// invalidation or a change to the document's repository, head or base. Unset, /api/sessions is unavailable.
	Sessions SessionsFunc
	// Patch reads one file's patch of the document's pull request from this
	// machine, for GET /api/patch. It is called when a file's patch is first
	// asked for, never at start-up. Unset, every patch is unavailable.
	Patch PatchFunc
	// Commit and CommitPatch read one linked commit's message and changed
	// files, and one of those files' patch, for GET /api/commit and
	// GET /api/commit-patch. They are called on demand, only for a commit
	// GET /api/commits lists. Unset, every commit is unavailable.
	Commit      CommitFunc
	CommitPatch CommitPatchFunc
	// File reads one file at the document's head from this machine, for
	// GET /api/file. It is called on demand, only for a path the document
	// cites. Unset, every file is unavailable.
	File FileFunc
	// Versions reads another saved version of the document's pull request,
	// for GET /api/patch?version=. Unset, only the served document's version
	// has patches.
	Versions VersionsFunc
	// Home is the user's home directory, written as ~ in the transcripts
	// GET /api/sessions/{id} serves. It defaults to os.UserHomeDir, else the
	// account's home directory.
	Home string
	// PollInterval is how often a watched file is checked; it defaults to
	// 500ms. PingInterval is how often an idle event stream sends a comment
	// line; it defaults to 20s.
	PollInterval time.Duration
	PingInterval time.Duration
}

Options configures a Server.

type PatchFunc

type PatchFunc func(ctx context.Context, review Review, path string) (locallink.PatchResult, error)

PatchFunc reads the patch of one file of the document's pull request from this machine. It may run twice at once for the same file when the document changes during a computation, and it should stop when ctx ends.

type Review

type Review struct {
	Repository  string
	PullRequest int
	Head, Base  string
	// MergeBase is the merge base the document records, when it does.
	MergeBase string
	// Version is the document's review version.
	Version int
}

Review names the pull request a document reviews.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server serves the viewer and a document. Create it with New, call Listen, then Serve.

func New

func New(opts Options) (*Server, error)

New loads the document and returns a server that is not yet listening.

func (*Server) CachedBytes added in v0.2.0

func (s *Server) CachedBytes() int64

CachedBytes estimates the memory the server holds for its document: the served bytes and the finished answers its patch, commit and file caches keep. A host bounding several servers can use it as their size.

func (*Server) Close

func (s *Server) Close()

Close cancels computations when a host serves Mux without Listen/Serve. Call it only after requests using that mux have finished.

func (*Server) InvalidateSessions

func (s *Server) InvalidateSessions()

InvalidateSessions marks cached evidence stale. An in-flight read is allowed to finish, then its waiters coalesce onto one read of the new revision.

func (*Server) LinkedSessions

func (s *Server) LinkedSessions(ctx context.Context) (locallink.Result, bool)

LinkedSessions is the /api/sessions answer for the document being served, sharing its computation; false when ctx ends or the server stops first.

func (*Server) Listen

func (s *Server) Listen(port int) (string, error)

Listen binds 127.0.0.1 on port, or on a free port when port is 0, and returns the viewer's URL. Once it has the port, it prints the findings for the initial document.

func (*Server) Mux

func (s *Server) Mux() *http.ServeMux

Mux returns the router, so a host can add routes before Serve. Its routes sit behind the host check and the security headers.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve serves until ctx is cancelled, then shuts down gracefully: event streams end at once and requests in flight get a short grace period. It returns nil after a clean shutdown, and only once the shutdown and the file watcher have finished.

func (*Server) Use

func (s *Server) Use(middleware func(http.Handler) http.Handler)

Use adds middleware that wraps the router, inside the host check, for example authentication. The first one added is the outermost. Add it before Serve: the chain is fixed when Serve starts, so a later call panics rather than being silently ignored.

type SessionsFunc

type SessionsFunc func(ctx context.Context, review Review) (locallink.Result, error)

SessionsFunc links local sessions to a pull request. It may run twice at once when the document changes during a computation, and it should stop when ctx ends.

type VersionsFunc

type VersionsFunc func(ctx context.Context, version int) ([]byte, error)

VersionsFunc reads the document of another saved version of the served document's pull request. A version that does not exist is ErrNoVersion.

Jump to

Keyboard shortcuts

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