package render import ( "errors" "fmt" "strings" "time" "unicode" "unicode/utf8" ) // Limits of one render call (spec section 3, "Contract" and "Sandbox"). const ( // CallTimeout bounds one call: launch, every page and the report. // Past it the sandbox is killed and the call is CodeTimeout. CallTimeout = 60 * time.Second // MemoryLimit bounds the sandbox's memory: every process's data // segment (RLIMIT_DATA, set before the browser starts), and the sum // of every process's resident memory, watched by the runner. Past it // the sandbox is killed and the call is CodeCrashed. MemoryLimit = 2 << 30 // MaxPages is the most pages one call renders. MaxPages = 6 // MaxCaptureHeight clips a full-page capture, in CSS pixels. MaxCaptureHeight = 10000 // IdleWait is the longest a page waits after its load event for the // network to go idle. IdleWait = 5 * time.Second maxConsoleErrors = 20 maxFailedLoads = 30 maxBrokenLinks = 30 maxOutline = 8 << 10 // Bounds on single strings from the page. maxTitle = 300 maxMessage = 1000 maxURL = 2000 maxName = 200 ) // Error codes of a call the driver itself ends (the launch's are // release.RendererError; the rest are render_page's, task 10). const ( CodeTimeout = "timeout" CodeCrashed = "crashed" ) // Error is a call that ended with one of the fixed codes. Detail is a // diagnostic for the runner's log, never shown as page content. type Error struct { Code string Detail string } func (e *Error) Error() string { if e.Detail == "" { return "render: " + e.Code } return fmt.Sprintf("render: %s (%s)", e.Code, e.Detail) } // ErrorCode is err's code when it is an *Error, else "". func ErrorCode(err error) string { var e *Error if errors.As(err, &e) { return e.Code } return "" } // Viewport is one of the fixed viewports. type Viewport string const ( Phone Viewport = "phone" Tablet Viewport = "tablet" Desktop Viewport = "desktop" ) // Size is the viewport's width and height in CSS pixels; ok is false // for a viewport that is not one of the fixed set. func (v Viewport) Size() (width, height int, ok bool) { switch v { case Phone: return 390, 844, true case Tablet: return 834, 1194, true case Desktop: return 1440, 900, true } return 0, 0, false } // mobile is whether the viewport is emulated as a mobile device (the // page's viewport meta tag applies). func (v Viewport) mobile() bool { return v == Phone || v == Tablet } // Page is one page to render: a site path, a viewport and whether the // capture is the full page (clipped at MaxCaptureHeight) or the viewport. type Page struct { Path string Viewport Viewport FullPage bool } // ReportSchema is the report's schema version. A change to the JSON // below is a new version. const ReportSchema = 1 // ReportName is the report's file name in the out directory. const ReportName = "report.json" // Report is what one call wrote: report.json in the out directory, beside // each page's capture. Every string in it came from the page and is // page content: bounded, with control characters removed, never trusted. type Report struct { Schema int `json:"schema"` // Error is CodeTimeout or CodeCrashed for a call that ended early; // Pages then holds the pages finished before it. Error string `json:"error,omitempty"` ElapsedMS int64 `json:"elapsed_ms"` Pages []PageReport `json:"pages"` } // PageReport is one rendered page. type PageReport struct { Path string `json:"path"` Viewport Viewport `json:"viewport"` Width int `json:"width"` Height int `json:"height"` FullPage bool `json:"full_page"` // Capture is the PNG's file name in the out directory, as the // browser wrote it; CaptureWidth and CaptureHeight are its size. Capture string `json:"capture"` CaptureWidth int `json:"capture_width"` CaptureHeight int `json:"capture_height"` Title string `json:"title"` // ConsoleErrors are the first maxConsoleErrors; Omitted counts the // rest, repeats included. The same for failed loads (a repeat of // one listed is not counted) and broken links. ConsoleErrors []string `json:"console_errors"` ConsoleErrorsOmitted int `json:"console_errors_omitted"` FailedLoads []FailedLoad `json:"failed_loads"` FailedLoadsOmitted int `json:"failed_loads_omitted"` // BrokenLinks are same-site anchors whose URL answered 404, of // LinksChecked distinct same-site anchors. BrokenLinks []string `json:"broken_links"` BrokenLinksOmitted int `json:"broken_links_omitted"` LinksChecked int `json:"links_checked"` // Outline is the accessibility tree, one node a line, indented by // depth, cut at maxOutline bytes (OutlineCut). Outline string `json:"outline"` OutlineCut bool `json:"outline_cut"` } // Kinds of failed load. const ( LoadExternalBlocked = "external blocked" LoadNotFound = "not found" LoadFont = "font" ) // FailedLoad is a resource the page asked for that did not load: Kind // is one of the three kinds, Status the HTTP status when one came. type FailedLoad struct { Kind string `json:"kind"` URL string `json:"url"` Status int `json:"status,omitempty"` } // Clean is clean, for the reply that quotes page text again. func Clean(s string, max int) string { return clean(s, max) } // clean makes page text safe to hold: invalid UTF-8 and control // characters (line breaks included) become spaces, runs of space // collapse, and the result is at most max bytes, cut at a rune. func clean(s string, max int) string { var b strings.Builder space := false for _, r := range s { if r == utf8.RuneError || unicode.IsControl(r) || unicode.Is(unicode.Cf, r) || unicode.IsSpace(r) { if !space && b.Len() > 0 { space = true } continue } need := utf8.RuneLen(r) if space { need++ } if b.Len()+need > max { break } if space { b.WriteByte(' ') space = false } b.WriteRune(r) } return b.String() }